Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ action_groups:
- proxmox_user
- proxmox_user_info
- proxmox_vm_info
- proxmox_zone
64 changes: 64 additions & 0 deletions plugins/module_utils/proxmox.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,67 @@ def get_storage_content(self, node, storage, content=None, vmid=None):
msg="Unable to list content on %s, %s for %s and %s: %s"
% (node, storage, content, vmid, e)
)

def get_global_sdn_lock(self):
"""Acquire global SDN lock. Needed for any changes under SDN.

@return: lock-token
"""
try:
return self.proxmox_api.cluster().sdn().lock().post()
except Exception as e:
self.module.fail_json(
msg=f'Failed to acquire global sdn lock {e}'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Thulium-Drake The last time i contributed to community.general f-strings were not allowed due to backwards compatibility. But sanity-tests pass on this code. Do you know why? Has python2 compatibility been deprecated for this collection?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In README python 3.7 is mentioned so I thought it should be ok. If not let me know i can update but python2 deprecated when the only python I knew was a snake... :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah i see, very nice.

So we could use type hints, as well now, @Thulium-Drake ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ehm, good question, @felixfontein what are the guidelines in community.general for this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether Python 2 compatibility is still needed depends on a) the supported ansible-core versions of the collection, and b) on the collection itself as well (there might be further restrictions on target version numbers).

For community.general, we usually support all ansible-core versions that are not EOL and haven't been EOL for more than a few weeks at the time of a new major release. That means that community.general 11.0.0 (released in spring) still supports ansible-core 2.16, which still supports Python 2.7 on the target. But the next major release, community.general 12.0.0 (should be released in November), will only support ansible-core >= 2.17, and thus will be Python 3.7+.

(I'm not saying this policy is great, but at least it is predictable and doesn't let the CI matrix explode... like community.crypto 2.x.y, which supports everything from Ansible 2.9 up to ansible-core 2.19, or community.internal_test_tools and community.library_inventory_filtering_v1, which even cover Ansible 2.9 up to ansible-core devel :) )

community.proxmox 1.0.0 said it only supports ansible-core 2.17+ from the beginning, so it was always Python 3.7+.

)

def apply_sdn_changes_and_release_lock(self, lock, release_lock=True):
"""Apply all SDN changes done under a lock token.

@param lock: Global SDN lock token
@param release_lock: if True release lock after successfully applying changes
"""
lock_params = {
'lock-token': lock,
'release-lock': ansible_to_proxmox_bool(release_lock)
}
try:
self.proxmox_api.cluster().sdn().put(**lock_params)
except Exception as e:
self.rollback_sdn_changes_and_release_lock(lock)
self.module.fail_json(
msg=f'Failed to apply sdn changes {e}. Rolling back all pending changes.'
)

def rollback_sdn_changes_and_release_lock(self, lock, release_lock=True):
"""Rollback all changes done under a lock token.

@param lock: Global SDN lock token
@param release_lock: if True release lock after successfully rolling back changes
"""
lock_params = {
'lock-token': lock,
'release-lock': ansible_to_proxmox_bool(release_lock)
}
try:
self.proxmox_api.cluster().sdn().rollback().post(**lock_params)
except Exception as e:
self.module.fail_json(
msg=f'Rollback attempt failed - {e}. Manually clear lock by deleting /etc/pve/sdn/.lock'
)

def release_lock(self, lock, force=False):
"""Release lock

@param lock: Global SDN lock token
@param force: if true, allow releasing lock without providing the token
"""
lock_params = {
'lock-token': lock,
'force': ansible_to_proxmox_bool(force)
}
try:
self.proxmox_api.cluster().sdn().lock().delete(**lock_params)
except Exception as e:
self.module.fail_json(
msg=f'Failed to release lock - {e}. Manually clear lock by deleting /etc/pve/sdn/.lock'
)
Loading