Add stop/start and state for vps

This commit is contained in:
Mario
2024-08-04 23:24:04 +02:00
parent 00724077e1
commit b79dbe4708
3 changed files with 67 additions and 0 deletions

View File

@@ -20,6 +20,11 @@ class VPSServer(models.Model):
proxmox_server_id = fields.Many2one('proxmox.server', string='Proxmox Server', tracking=True) proxmox_server_id = fields.Many2one('proxmox.server', string='Proxmox Server', tracking=True)
ct_id = fields.Integer(string='CT ID (LXC)', tracking=True) ct_id = fields.Integer(string='CT ID (LXC)', tracking=True)
vm_id = fields.Integer(string='VM ID (QEMU)', tracking=True) vm_id = fields.Integer(string='VM ID (QEMU)', tracking=True)
state = fields.Selection([
('running', 'Running'),
('stopped', 'Stopped'),
('unknown', 'Unknown')
], string='State', default='unknown', tracking=True)
def _compute_access_url(self): def _compute_access_url(self):
super()._compute_access_url() super()._compute_access_url()
@@ -31,6 +36,61 @@ class VPSServer(models.Model):
if self.ct_id and self.vm_id: if self.ct_id and self.vm_id:
raise UserError(_("Please provide either CT ID or VM ID, not both.")) raise UserError(_("Please provide either CT ID or VM ID, not both."))
def _proxmox_request(self, method, endpoint, data=None):
self.ensure_one()
proxmox = self.proxmox_server_id
base_url = proxmox.url.rstrip('/')
headers = {
"Authorization": f"PVEAPIToken={proxmox.api_token_id}={proxmox.api_token_secret}"
}
url = f"{base_url}/api2/json/{endpoint}"
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
try:
response = requests.request(method, url, headers=headers, json=data, verify=False)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
raise UserError(_("Proxmox API request failed: %s") % str(e))
def action_start(self):
for server in self:
vm_type = 'lxc' if server.ct_id else 'qemu'
vm_id = server.ct_id or server.vm_id
self._proxmox_request('POST', f'nodes/pve/{vm_type}/{vm_id}/status/start')
self.fetch_state()
def action_shutdown(self):
for server in self:
vm_type = 'lxc' if server.ct_id else 'qemu'
vm_id = server.ct_id or server.vm_id
self._proxmox_request('POST', f'nodes/pve/{vm_type}/{vm_id}/status/shutdown')
self.fetch_state()
def action_stop(self):
for server in self:
vm_type = 'lxc' if server.ct_id else 'qemu'
vm_id = server.ct_id or server.vm_id
self._proxmox_request('POST', f'nodes/pve/{vm_type}/{vm_id}/status/stop')
self.fetch_state()
def action_reboot(self):
for server in self:
vm_type = 'lxc' if server.ct_id else 'qemu'
vm_id = server.ct_id or server.vm_id
self._proxmox_request('POST', f'nodes/pve/{vm_type}/{vm_id}/status/reboot')
self.fetch_state()
def fetch_state(self):
for server in self:
vm_type = 'lxc' if server.ct_id else 'qemu'
vm_id = server.ct_id or server.vm_id
status = self._proxmox_request('GET', f'nodes/pve/{vm_type}/{vm_id}/status/current')
server.state = 'running' if status['data']['status'] == 'running' else 'stopped'
def action_fetch_proxmox_data(self): def action_fetch_proxmox_data(self):
self.ensure_one() self.ensure_one()
if not self.proxmox_server_id: if not self.proxmox_server_id:
@@ -129,6 +189,8 @@ class VPSServer(models.Model):
else: # QEMU else: # QEMU
self.os = config_data.get('ostype', 'Unknown') self.os = config_data.get('ostype', 'Unknown')
self.fetch_state()
return { return {
'type': 'ir.actions.client', 'type': 'ir.actions.client',
'tag': 'display_notification', 'tag': 'display_notification',

View File

@@ -6,7 +6,12 @@
<field name="arch" type="xml"> <field name="arch" type="xml">
<form> <form>
<header> <header>
<button name="action_start" string="Start" type="object" icon="fa-play" attrs="{'invisible': [('state', '=', 'running')]}" class="oe_highlight"/>
<button name="action_shutdown" string="Shutdown" type="object" icon="fa-power-off" attrs="{'invisible': [('state', '!=', 'running')]}" class="oe_highlight"/>
<button name="action_stop" string="Stop" type="object" icon="fa-stop" attrs="{'invisible': [('state', '!=', 'running')]}" class="text-danger"/>
<button name="action_reboot" string="Reboot" type="object" icon="fa-refresh" attrs="{'invisible': [('state', '!=', 'running')]}" class="oe_highlight"/>
<button name="action_fetch_proxmox_data" string="Fetch Proxmox Data" type="object" class="oe_highlight"/> <button name="action_fetch_proxmox_data" string="Fetch Proxmox Data" type="object" class="oe_highlight"/>
<field name="state" widget="statusbar"/>
</header> </header>
<sheet> <sheet>
<group> <group>