Skip to content

Commit

Permalink
Add server-side artifact fetching to proxmox_template module (#9113)
Browse files Browse the repository at this point in the history
* Add server-side artifact fetching to proxmox_template module

* Update docs, format per feedback.

* Formatting plugins/modules/proxmox_template.py

Co-authored-by: Alexei Znamensky <[email protected]>

---------

Co-authored-by: spencer <Spencer>
Co-authored-by: Alexei Znamensky <[email protected]>
  • Loading branch information
boyersd and russoz authored Nov 21, 2024
1 parent 4b0d5cb commit 8078a08
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- proxmox_template - add server side artifact fetching support (https://github.com/ansible-collections/community.general/pull/9113).
75 changes: 63 additions & 12 deletions plugins/modules/proxmox_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@
src:
description:
- Path to uploaded file.
- Required only for O(state=present).
- Exactly one of O(src) or O(url) is required for O(state=present).
type: path
url:
description:
- URL to file to download
- Exactly one of O(src) or O(url) is required for O(state=present).
type: str
version_added: 10.1.0
template:
description:
- The template name.
Expand Down Expand Up @@ -85,6 +91,14 @@
api_host: node1
src: ~/ubuntu-14.04-x86_64.tar.gz
- name: Pull new openvz template with minimal options
community.general.proxmox_template:
node: uk-mc02
api_user: root@pam
api_password: 1q2w3e
api_host: node1
url: https://ubuntu-mirror/ubuntu-14.04-x86_64.tar.gz
- name: >
Upload new openvz template with minimal options use environment
PROXMOX_PASSWORD variable(you should export it before)
Expand All @@ -105,6 +119,17 @@
src: ~/ubuntu-14.04-x86_64.tar.gz
force: true
- name: Pull new openvz template with all options and force overwrite
community.general.proxmox_template:
node: uk-mc02
api_user: root@pam
api_password: 1q2w3e
api_host: node1
storage: local
content_type: vztmpl
url: https://ubuntu-mirror/ubuntu-14.04-x86_64.tar.gz
force: true
- name: Delete template with minimal options
community.general.proxmox_template:
node: uk-mc02
Expand Down Expand Up @@ -132,6 +157,7 @@
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible_collections.community.general.plugins.module_utils.proxmox import (proxmox_auth_argument_spec, ProxmoxAnsible)
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
from ansible.module_utils.six.moves.urllib.parse import urlparse

REQUESTS_TOOLBELT_ERR = None
try:
Expand Down Expand Up @@ -179,6 +205,17 @@ def upload_template(self, node, storage, content_type, realpath, timeout):
except Exception as e:
self.module.fail_json(msg="Uploading template %s failed with error: %s" % (realpath, e))

def fetch_template(self, node, storage, content_type, url, timeout):
"""Fetch a template from a web url source using the proxmox download-url endpoint
"""
try:
taskid = self.proxmox_api.nodes(node).storage(storage)("download-url").post(
url=url, content=content_type, filename=os.path.basename(url)
)
return self.task_status(node, taskid, timeout)
except Exception as e:
self.module.fail_json(msg="Fetching template from url %s failed with error: %s" % (url, e))

def download_template(self, node, storage, template, timeout):
try:
taskid = self.proxmox_api.nodes(node).aplinfo.post(storage=storage, template=template)
Expand All @@ -205,6 +242,7 @@ def main():
template_args = dict(
node=dict(),
src=dict(type='path'),
url=dict(),
template=dict(),
content_type=dict(default='vztmpl', choices=['vztmpl', 'iso']),
storage=dict(default='local'),
Expand All @@ -218,7 +256,8 @@ def main():
argument_spec=module_args,
required_together=[('api_token_id', 'api_token_secret')],
required_one_of=[('api_password', 'api_token_id')],
required_if=[('state', 'absent', ['template'])]
required_if=[('state', 'absent', ['template'])],
mutually_exclusive=[("src", "url")],
)

proxmox = ProxmoxTemplateAnsible(module)
Expand All @@ -231,9 +270,10 @@ def main():
if state == 'present':
content_type = module.params['content_type']
src = module.params['src']
url = module.params['url']

# download appliance template
if content_type == 'vztmpl' and not src:
if content_type == 'vztmpl' and not (src or url) :
template = module.params['template']

if not template:
Expand All @@ -245,16 +285,27 @@ def main():
if proxmox.download_template(node, storage, template, timeout):
module.exit_json(changed=True, msg='template with volid=%s:%s/%s downloaded' % (storage, content_type, template))

template = os.path.basename(src)
if proxmox.has_template(node, storage, content_type, template) and not module.params['force']:
module.exit_json(changed=False, msg='template with volid=%s:%s/%s is already exists' % (storage, content_type, template))
elif not src:
module.fail_json(msg='src param to uploading template file is mandatory')
elif not (os.path.exists(src) and os.path.isfile(src)):
module.fail_json(msg='template file on path %s not exists' % src)
if not src and not url:
module.fail_json(msg='src or url param for uploading template file is mandatory')
elif not url:
template = os.path.basename(src)
if proxmox.has_template(node, storage, content_type, template) and not module.params['force']:
module.exit_json(changed=False, msg='template with volid=%s:%s/%s already exists' % (storage, content_type, template))
elif not (os.path.exists(src) and os.path.isfile(src)):
module.fail_json(msg='template file on path %s not exists' % src)

if proxmox.upload_template(node, storage, content_type, src, timeout):
module.exit_json(changed=True, msg='template with volid=%s:%s/%s uploaded' % (storage, content_type, template))
if proxmox.upload_template(node, storage, content_type, src, timeout):
module.exit_json(changed=True, msg='template with volid=%s:%s/%s uploaded' % (storage, content_type, template))
elif not src:
template = os.path.basename(urlparse(url).path)
if proxmox.has_template(node, storage, content_type, template):
if not module.params['force']:
module.exit_json(changed=False, msg='template with volid=%s:%s/%s already exists' % (storage, content_type, template))
elif not proxmox.delete_template(node, storage, content_type, template, timeout):
module.fail_json(changed=False, msg='failed to delete template with volid=%s:%s/%s' % (storage, content_type, template))

if proxmox.fetch_template(node, storage, content_type, url, timeout):
module.exit_json(changed=True, msg='template with volid=%s:%s/%s uploaded' % (storage, content_type, template))

elif state == 'absent':
try:
Expand Down

0 comments on commit 8078a08

Please sign in to comment.