
    Vh7:                         d dl mZmZmZ eZdZdZd dlZd dl	Z	d dl
Z
d dlmZmZ d dlmZmZ d dlmZ d dlmZmZ dZ	 d dlZd	Z G d de      Zd Zedk(  r e        yy# e$ r d
Z e
j4                         ZY 6w xY w)    )absolute_importdivisionprint_functiona3
  
module: proxmox_template
short_description: Management of OS templates in Proxmox VE cluster
description:
  - Allows you to upload/delete templates in Proxmox VE cluster.
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
  action_group:
    version_added: 9.0.0
options:
  node:
    description:
      - Proxmox VE node on which to operate.
    type: str
  src:
    description:
      - Path to uploaded file.
      - 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.
      - Required for O(state=absent) to delete a template.
      - Required for O(state=present) to download an appliance container template (pveam).
    type: str
  content_type:
    description:
      - Content type.
      - Required only for O(state=present).
    type: str
    default: 'vztmpl'
    choices: ['vztmpl', 'iso']
  storage:
    description:
      - Target storage.
    type: str
    default: 'local'
  timeout:
    description:
      - Timeout for operations.
    type: int
    default: 30
  force:
    description:
      - It can only be used with O(state=present), existing template will be overwritten.
    type: bool
    default: false
  state:
    description:
      - Indicate desired state of the template.
    type: str
    choices: ['present', 'absent']
    default: present
  checksum_algorithm:
    description:
      - Algorithm used to verify the checksum.
      - If specified, O(checksum) must also be specified.
    type: str
    choices: ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512']
    version_added: 10.3.0
  checksum:
    description:
      - The checksum to validate against.
      - Checksums are often provided by software distributors to verify that a download is not corrupted.
      - Checksums can usually be found on the distributors download page in the form of a file or string.
      - If specified, O(checksum_algorithm) must also be specified.
    type: str
    version_added: 10.3.0
notes:
  - Requires C(proxmoxer) and C(requests) modules on host. Those modules can be installed with M(ansible.builtin.pip).
  - C(proxmoxer) >= 1.2.0 requires C(requests_toolbelt) to upload files larger than 256 MB.
author: Sergei Antipov (@UnderGreen)
extends_documentation_fragment:
  - community.general.proxmox.actiongroup_proxmox
  - community.general.proxmox.documentation
  - community.general.attributes
a  
---
- name: Upload new openvz template with minimal options
  community.general.proxmox_template:
    node: uk-mc02
    api_user: root@pam
    api_password: 1q2w3e
    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)
  community.general.proxmox_template:
    node: uk-mc02
    api_user: root@pam
    api_host: node1
    src: ~/ubuntu-14.04-x86_64.tar.gz

- name: Upload 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
    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
    api_user: root@pam
    api_password: 1q2w3e
    api_host: node1
    template: ubuntu-14.04-x86_64.tar.gz
    state: absent

- name: Download proxmox appliance container template
  community.general.proxmox_template:
    node: uk-mc02
    api_user: root@pam
    api_password: 1q2w3e
    api_host: node1
    storage: local
    content_type: vztmpl
    template: ubuntu-20.04-standard_20.04-1_amd64.tar.gz

- name: Download and verify a template's checksum
  community.general.proxmox_template:
    node: uk-mc02
    api_user: root@pam
    api_password: 1q2w3e
    api_host: node1
    url: ubuntu-20.04-standard_20.04-1_amd64.tar.gz
    checksum_algorithm: sha256
    checksum: 65d860160bdc9b98abf72407e14ca40b609417de7939897d3b58d55787aaef69
N)AnsibleModulemissing_required_lib)proxmox_auth_argument_specProxmoxAnsible)LooseVersion)urlparse	urlencodeTFc                   6    e Zd Zd Zd Zd Zd Zd Zd Zd Z	y)	ProxmoxTemplateAnsiblec                 2   |d|d|	 t        fd| j                  j                  |      j                  |      j                  j                         D              S # t        $ r,}| j                  j                  dd|       Y d }~y d }~ww xY w)N:/c              3   .   K   | ]  }|d    k(    yw)volidN ).0tmplr   s     v/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/proxmox_template.py	<genexpr>z6ProxmoxTemplateAnsible.has_template.<locals>.<genexpr>   s     v$tG}-vs   zFailed to retrieve template 'z': msg)	anyproxmox_apinodesstoragecontentget	Exceptionmodule	fail_json)selfnoder   content_typetemplateer   s         @r   has_templatez#ProxmoxTemplateAnsible.has_template   s    %|X>	[v$:J:J:P:PQU:V:^:^_f:g:o:o:s:s:uvvv 	[KK!!PUWX&Y!ZZ	[s   AA! !	B*"BBc                 "   |r| j                  ||      ry| j                  ||      rb| j                  j                  d| j                  j                  |      j                  |      j                  j                         d   z         |dz
  }|dk(  rb| j                  j                  d| j                  j                  |      j                  |      j                  j                         dd z         t        j                  d       |ry	)
zg
        Check the task status and wait until the task is completed or the timeout is reached.
        TzTask error: %s
exitstatusr      r   zfReached timeout while waiting for uploading/downloading template. Last line in task before timeout: %sNF)api_task_okapi_task_failedr"   r#   r   r   tasksstatusr    logtimesleep)r$   r%   taskidtimeouts       r   task_statusz"ProxmoxTemplateAnsible.task_status   s    f-%%dF3%%*:T=M=M=S=STX=Y=_=_`f=g=n=n=r=r=t  vB  >C  +C%  DkG!|%%  +S&*&6&6&<&<T&B&H&H&P&T&T&X&X&Z[]\]&^+_% ` JJqM      c                    t        j                  |      }t        | j                        t        d      k\  r;|j                  dkD  r,t
        s&| j                  j                  dt        d             	 | j                  j                  |      j                  |      j                  j                  |t        |d            }| j                  |||      S # t         $ r,}| j                  j                  d|d	|
       Y d }~y d }~ww xY w)Nz1.2.0i   zH'requests_toolbelt' module is required to upload files larger than 256MBrequests_toolbelt)r   	exceptionrb)r   filenamezUploading template  failed with error: r   )osstatr
   proxmoxer_versionst_sizeHAS_REQUESTS_TOOLBELTr"   r#   r   r   r   r   uploadpostopenr6   r!   )	r$   r%   r   r&   realpathr5   statsr4   r(   s	            r   upload_templatez&ProxmoxTemplateAnsible.upload_template   s    !//0L4II	)2GKK!!&p,@AT,U " W	e%%++D199'BIINNWcnrs{  ~B  oCN  DF##D&':: 	eKK!!W_ab&c!dd	es   3A!C 	D
"DD
c                 X   	  | j                   j                  |      j                  |      d      j                  ||t        j
                  j                  |            }| j                  |||      S # t        $ r,}| j                  j                  d|d|       Y d}~yd}~ww xY w)zWFetch a template from a web url source using the proxmox download-url endpoint
        zdownload-url)urlr   r<   zFetching template from url r=   r   N)r   r   r   rD   r>   pathbasenamer6   r!   r"   r#   )r$   r%   r   r&   rJ   r5   r4   r(   s           r   fetch_templatez%ProxmoxTemplateAnsible.fetch_template   s    	hBT%%++D199'B>RWW8H8H8M X F ##D&':: 	hKK!!_bde&f!gg	hs   A1A4 4	B)="B$$B)c                    	 | j                   j                  |      j                  j                  ||      }| j	                  |||      S # t
        $ r,}| j                  j                  d|d|       Y d }~y d }~ww xY w)N)r   r'   zDownloading template r=   r   )r   r   aplinforD   r6   r!   r"   r#   )r$   r%   r   r'   r5   r4   r(   s          r   download_templatez(ProxmoxTemplateAnsible.download_template   s{    	g%%++D199>>wYa>bF##D&':: 	gKK!!Yacd&e!ff	gs   AA 	B "A;;B c                 H   |d|d|}| j                   j                  |      j                  |      j                  j	                  |       |rS| j                  ||||      sy|dz
  }|dk(  r| j                  j                  d       t        j                  d       |rSy)	Nr   r   Tr,   r   z4Reached timeout while waiting for deleting template.r   F)
r   r   r   r   deleter)   r"   r#   r2   r3   )r$   r%   r   r&   r'   r5   r   s          r   delete_templatez&ProxmoxTemplateAnsible.delete_template   s    %|X>t$,,W5==DDUK$$T7L(KkG!|%%*`%aJJqM  r7   c                    ||t         j                  j                  |      ||d}	 | j                  j	                  |      j                  |      j                  dj                  t        |                  }	| j                  ||	|      S # t        $ r)}
| j                  j                  d|
z         Y d}
~
yd}
~
ww xY w)zK Fetch a template from a web url, then verify it using a checksum.
        )rJ   r   r<   checksumzchecksum-algorithmzdownload-url?{}zChecksum mismatch: %sr   N)r>   rK   rL   r   r   r   rD   formatr   r6   r!   r"   r#   )r$   r%   r   rJ   r&   r5   rU   checksum_algorithmdatar4   r(   s              r   fetch_and_verifyz'ProxmoxTemplateAnsible.fetch_and_verify
  s     #((- "46	E%%++D199'BGGHYH`H`ajkoapHqrF##D&':: 	EKK!!&=&C!DD	Es   A#B 	B=B88B=N)
__name__
__module____qualname__r)   r6   rH   rM   rP   rS   rY   r   r7   r   r   r      s*    ["e	hgEr7   r   c                  z	   t               } t        t               t        d      t               t               t        dddg      t        d      t        dd	
      t        dd
      t        dddg      t        g d      t        d            }| j                  |       t        | ddgdgdddgfgdg      }t	        |      }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }	|dk(  r|j
                  d   }
|j
                  d    }|j
                  d!   }|
dk(  r|s|s|j
                  d   }|s|j                  d"#       |j                  |||
|      r,|j
                  d$   s|j                  dd%|d&|
d'|d()       |j                  ||||      r|j                  d*d%|d&|
d'|d+)       |s|s|j                  d,#       y |st        j                  j                  |      }|j                  |||
|      r-|j
                  d$   s|j                  dd%|d&|
d'|d()       nSt        j                  j                  |      rt        j                  j                  |      s|j                  d-|z  #       |j                  |||
||      r0|j                  d*d%|d&|
d'|d.)       y |st        j                  j                  t!        |      j                        }|j                  |||
|      r^|j
                  d$   s|j                  dd%|d&|
d'|d()       n1|j#                  |||
||      s|j                  dd/|d&|
d'|)       |r4|j%                  ||||
|||	      r|j                  d*d0|d&|
d'|d.)       |j'                  |||
||      r|j                  d*d%|d&|
d'|d.)       y y y y |dk(  r	 |j
                  d   }
|j
                  d   }|j                  |||
|      s|j                  dd%|d&|
d'|d1)       |j#                  |||
||      r|j                  d*d%|d&|
d'|d2)       y y y # t(        $ r"}|j                  d3d4|#       Y d }~y d }~ww xY w)5NrK   )typevztmpliso)defaultchoiceslocal)ra   int   )r^   ra   boolFpresentabsent)md5sha1sha224sha256sha384sha512)rb   str)r%   srcrJ   r'   r&   r   r5   forcestaterW   rU   )api_token_idapi_token_secret)rU   rW   )api_passwordrs   rr   r'   )rp   rJ   )argument_specrequired_togetherrequired_one_ofrequired_ifmutually_exclusiver%   r   r5   rU   rW   r&   rp   rJ   z>template param for downloading appliance template is mandatoryr   rq   ztemplate with volid=r   r   z already exists)changedr   Tz downloadedz9src or url param for uploading template file is mandatoryz#template file on path %s not existsz	 uploadedz%failed to delete template with volid=z'Checksum verified, template with volid=z is already deletedz deletedzdeleting of template z failed with exception: )r   dictupdater   r   paramsr#   r)   	exit_jsonrP   r>   rK   rL   existsisfilerH   r   rS   rY   rM   r!   )module_argstemplate_argsr"   proxmoxrr   r%   r   r5   rU   rW   r&   rp   rJ   r'   r(   s                  r   mainr     s   ,.KVfF(Xu4EFW%%,.9y(.CD(_`5!M }%!?Acd9:x*67*+F %V,GMM'"E== DmmI&GmmI&G}}Z(H';<	}}^4mmE"mmE" 8#SC}}Z0H  %e f##D'<JSYS`S`ahSi  elnz  }E  5F   G((w'J  `giuw  4A   B3!\]ww'',H##D'<JSYS`S`ahSi  elnz  }E  5F   GggnnS)bggnnS.A  %JS%P Q&&tWlCQ  ^egsu}3~ ww''(:(:;H##D'<J}}W-$$Uipr~  AI  9J$  K 00whX_`$$Ukr  uA  CK  9L$  M++D'3gW_ast$$Tu|  K  MU  8V$  W%%dG\3P  ^egsu}3~  Q  R  
(	
	f!==8L}}Z0H''g|XN  ipr~  AI  5J   K&&tWlHgV  ]dfrt|3} ~ W 
  	fX`bc!dee	fs   BR 	R:R55R:__main__)
__future__r   r   r   r^   __metaclass__DOCUMENTATIONEXAMPLESr>   r2   	tracebackansible.module_utils.basicr   r   Bansible_collections.community.general.plugins.module_utils.proxmoxr   r	   Bansible_collections.community.general.plugins.module_utils.versionr
   +ansible.module_utils.six.moves.urllib.parser   r   REQUESTS_TOOLBELT_ERRr9   rB   ImportError
format_excr   r   rZ   r   r7   r   <module>r      s    A @UnM^ 
   J { [ K 3 RE^ REjXfv zF k  3!0I0023s   A   A;:A;