-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proxmox_kvm Allow vm hibernation #9653
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
minor_changes: | ||
- proxmox_kvm - allow hibernation and suspending of VMs (https://github.com/ansible-collections/community.general/issues/9620, https://github.com/ansible-collections/community.general/pull/9653). |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -455,8 +455,9 @@ | |
- Indicates desired state of the instance. | ||
- If V(current), the current state of the VM will be fetched. You can access it with C(results.status). | ||
- V(template) was added in community.general 8.1.0. | ||
- V(paused) and V(hibernated) were added in community.general 10.4.0. | ||
type: str | ||
choices: ['present', 'started', 'absent', 'stopped', 'restarted', 'current', 'template'] | ||
choices: ['present', 'started', 'absent', 'stopped', 'restarted', 'current', 'template', 'paused', 'hibernated'] | ||
default: present | ||
storage: | ||
description: | ||
|
@@ -1208,6 +1209,16 @@ def migrate_vm(self, vm, target_node): | |
return False | ||
return True | ||
|
||
def suspend_vm(self, vm, timeout, todisk): | ||
vmid = vm['vmid'] | ||
proxmox_node = self.proxmox_api.nodes(vm['node']) | ||
taskid = proxmox_node.qemu(vmid).status.suspend.post(todisk=(1 if todisk else 0), timeout=timeout) | ||
if not self.wait_for_task(vm['node'], taskid): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any option for the user so that they do not have to wait for the operation to complete? As in a a fire-and-forget kind of operation. Apparently not, just making sure. If indeed it has no such option, I would suggest adding one (on a different PR). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that would be possible to implement, but the other states also doesn't have a "fire and forget". So Yeah I think that should be another PR implementing that for all possble states |
||
self.module.fail_json(msg='Reached timeout while waiting for suspending VM. Last line in task before timeout: %s' % | ||
proxmox_node.tasks(taskid).log.get()[:1]) | ||
return False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is never executed - There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. which line do you mean exactly? I tried to stay in line with the existing code base |
||
return True | ||
|
||
|
||
def main(): | ||
module_args = proxmox_auth_argument_spec() | ||
|
@@ -1287,7 +1298,7 @@ def main(): | |
sshkeys=dict(type='str', no_log=False), | ||
startdate=dict(type='str'), | ||
startup=dict(), | ||
state=dict(default='present', choices=['present', 'absent', 'stopped', 'started', 'restarted', 'current', 'template']), | ||
state=dict(default='present', choices=['present', 'absent', 'stopped', 'started', 'restarted', 'current', 'template', 'paused', 'hibernated']), | ||
storage=dict(type='str'), | ||
tablet=dict(type='bool'), | ||
tags=dict(type='list', elements='str'), | ||
|
@@ -1611,6 +1622,23 @@ def main(): | |
if status: | ||
module.exit_json(changed=False, vmid=vmid, msg="VM %s with vmid = %s is %s" % (name, vmid, current), **status) | ||
|
||
elif state in ['paused', 'hibernated']: | ||
if not vmid: | ||
module.fail_json(msg='VM with name = %s does not exist in cluster' % name) | ||
|
||
status = {} | ||
try: | ||
vm = proxmox.get_vm(vmid) | ||
current = proxmox.proxmox_api.nodes(vm['node']).qemu(vmid).status.current.get()['status'] | ||
status['status'] = current | ||
if current != 'running': | ||
module.exit_json(changed=False, vmid=vmid, msg="VM %s is not running" % vmid, **status) | ||
|
||
proxmox.suspend_vm(vm, force=module.params['force'], timeout=module.params['timeout'], todisk=(state == 'hibernated')) | ||
module.exit_json(changed=True, vmid=vmid, msg="VM %s is suspending" % vmid, **status) | ||
except Exception as e: | ||
module.fail_json(vmid=vmid, msg="suspending of VM %s failed with exception: %s" % (vmid, e), **status) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed typo in Felix's comment: version is 10.4.0 not 11.4.0 ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed in commit 739193e