
    Vhb                         d dl mZmZmZ eZdZdZd dlZd dl	Z	dZ
	 d dlmZ dZd dlmZmZmZ d	 Zd
 Zd Zd Zedk(  r e        yy# e$ r  e	j                          Z
dZY >w xY w)    )absolute_importdivisionprint_functiona  
module: linode
short_description: Manage instances on the Linode Public Cloud
description:
  - Manage Linode Public Cloud instances and optionally wait for it to be 'running'.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  state:
    description:
      - Indicate desired state of the resource.
    choices: [absent, active, deleted, present, restarted, started, stopped]
    default: present
    type: str
  api_key:
    description:
      - Linode API key.
      - E(LINODE_API_KEY) environment variable can be used instead.
    type: str
    required: true
  name:
    description:
      - Name to give the instance (alphanumeric, dashes, underscore).
      - To keep sanity on the Linode Web Console, name is prepended with C(LinodeID-).
    required: true
    type: str
  displaygroup:
    description:
      - Add the instance to a Display Group in Linode Manager.
    type: str
    default: ''
  linode_id:
    description:
      - Unique ID of a Linode server. This value is read-only in the sense that if you specify it on creation of a Linode
        it will not be used. The Linode API generates these IDs and we can those generated value here to reference a Linode
        more specifically. This is useful for idempotence.
    aliases: [lid]
    type: int
  additional_disks:
    description:
      - List of dictionaries for creating additional disks that are added to the Linode configuration settings.
      - Dictionary takes Size, Label, Type. Size is in MB.
    type: list
    elements: dict
  alert_bwin_enabled:
    description:
      - Set status of bandwidth in alerts.
    type: bool
  alert_bwin_threshold:
    description:
      - Set threshold in MB of bandwidth in alerts.
    type: int
  alert_bwout_enabled:
    description:
      - Set status of bandwidth out alerts.
    type: bool
  alert_bwout_threshold:
    description:
      - Set threshold in MB of bandwidth out alerts.
    type: int
  alert_bwquota_enabled:
    description:
      - Set status of bandwidth quota alerts as percentage of network transfer quota.
    type: bool
  alert_bwquota_threshold:
    description:
      - Set threshold in MB of bandwidth quota alerts.
    type: int
  alert_cpu_enabled:
    description:
      - Set status of receiving CPU usage alerts.
    type: bool
  alert_cpu_threshold:
    description:
      - Set percentage threshold for receiving CPU usage alerts. Each CPU core adds 100% to total.
    type: int
  alert_diskio_enabled:
    description:
      - Set status of receiving disk IO alerts.
    type: bool
  alert_diskio_threshold:
    description:
      - Set threshold for average IO ops/sec over 2 hour period.
    type: int
  backupweeklyday:
    description:
      - Day of the week to take backups.
    type: int
  backupwindow:
    description:
      - The time window in which backups will be taken.
    type: int
  plan:
    description:
      - Plan to use for the instance (Linode plan).
    type: int
  payment_term:
    description:
      - Payment term to use for the instance (payment term in months).
    default: 1
    choices: [1, 12, 24]
    type: int
  password:
    description:
      - Root password to apply to a new server (auto generated if missing).
    type: str
  private_ip:
    description:
      - Add private IPv4 address when Linode is created.
      - Default is V(false).
    type: bool
  ssh_pub_key:
    description:
      - SSH public key applied to root user.
    type: str
  swap:
    description:
      - Swap size in MB.
    default: 512
    type: int
  distribution:
    description:
      - Distribution to use for the instance (Linode Distribution).
    type: int
  datacenter:
    description:
      - Datacenter to create an instance in (Linode Datacenter).
    type: int
  kernel_id:
    description:
      - Kernel to use for the instance (Linode Kernel).
    type: int
  wait:
    description:
      - Wait for the instance to be in state V(running) before returning.
    type: bool
    default: true
  wait_timeout:
    description:
      - How long before wait gives up, in seconds.
    default: 300
    type: int
  watchdog:
    description:
      - Set status of Lassie watchdog.
    type: bool
    default: true
requirements:
  - linode-python
author:
  - Vincent Viallet (@zbal)
notes:
  - Please note, linode-python does not have python 3 support.
  - This module uses the now deprecated v3 of the Linode API.
  - Please review U(https://www.linode.com/api/linode) for determining the required parameters.
a	  
- name: Create a new Linode
  community.general.linode:
    name: linode-test1
    plan: 1
    datacenter: 7
    distribution: 129
    state: present
  register: linode_creation

- name: Create a server with a private IP Address
  community.general.linode:
    module: linode
    api_key: 'longStringFromLinodeApi'
    name: linode-test1
    plan: 1
    datacenter: 2
    distribution: 99
    password: 'superSecureRootPassword'
    private_ip: true
    ssh_pub_key: 'ssh-rsa qwerty'
    swap: 768
    wait: true
    wait_timeout: 600
    state: present
  delegate_to: localhost
  register: linode_creation

- name: Fully configure new server
  community.general.linode:
    api_key: 'longStringFromLinodeApi'
    name: linode-test1
    plan: 4
    datacenter: 2
    distribution: 99
    kernel_id: 138
    password: 'superSecureRootPassword'
    private_ip: true
    ssh_pub_key: 'ssh-rsa qwerty'
    swap: 768
    wait: true
    wait_timeout: 600
    state: present
    alert_bwquota_enabled: true
    alert_bwquota_threshold: 80
    alert_bwin_enabled: true
    alert_bwin_threshold: 10
    alert_cpu_enabled: true
    alert_cpu_threshold: 210
    alert_bwout_enabled: true
    alert_bwout_threshold: 10
    alert_diskio_enabled: true
    alert_diskio_threshold: 10000
    backupweeklyday: 1
    backupwindow: 2
    displaygroup: 'test'
    additional_disks:
      - {Label: 'disk1', Size: 2500, Type: 'raw'}
      - {Label: 'newdisk', Size: 2000}
    watchdog: true
  delegate_to: localhost
  register: linode_creation

- name: Ensure a running server (create if missing)
  community.general.linode:
    api_key: 'longStringFromLinodeApi'
    name: linode-test1
    plan: 1
    datacenter: 2
    distribution: 99
    password: 'superSecureRootPassword'
    ssh_pub_key: 'ssh-rsa qwerty'
    swap: 768
    wait: true
    wait_timeout: 600
    state: present
  delegate_to: localhost
  register: linode_creation

- name: Delete a server
  community.general.linode:
    api_key: 'longStringFromLinodeApi'
    name: linode-test1
    linode_id: "{{ linode_creation.instance.id }}"
    state: absent
  delegate_to: localhost

- name: Stop a server
  community.general.linode:
    api_key: 'longStringFromLinodeApi'
    name: linode-test1
    linode_id: "{{ linode_creation.instance.id }}"
    state: stopped
  delegate_to: localhost

- name: Reboot a server
  community.general.linode:
    api_key: 'longStringFromLinodeApi'
    name: linode-test1
    linode_id: "{{ linode_creation.instance.id }}"
    state: restarted
  delegate_to: localhost
N)apiTF)AnsibleModulemissing_required_libenv_fallbackc                     ddl ddlj                          dj                  fdt	        d      D              } dj                  fdt	        d      D              }dj                  fdt	        d      D              }dj                  fdt	        d      D              }| |z   |z   |z   }dj                  j                  |t        |                  S )	zL
    Generate a long random password that comply to Linode requirements
    r   N c              3   T   K   | ]  }j                  j                         ! y wN)choiceascii_lowercase.0xrandomstrings     l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/linode.py	<genexpr>zrandompass.<locals>.<genexpr>/        LaFMM&"8"89L   %(   c              3   T   K   | ]  }j                  j                         ! y wr   )r   ascii_uppercaser   s     r   r   zrandompass.<locals>.<genexpr>0  r   r   c              3   T   K   | ]  }j                  j                         ! y wr   )r   digitsr   s     r   r   zrandompass.<locals>.<genexpr>1  s     DaV]]6==1Dr   c              3   T   K   | ]  }j                  j                         ! y wr   )r   punctuationr   s     r   r   zrandompass.<locals>.<genexpr>2  s      H!FMM&"4"45Hr   )r   r   seedjoinrangesamplelen)loweruppernumberpunctpr   r   s        @@r   
randompassr*   #  s     
KKMGGL58LLEGGL58LLEWWD58DDFGGHuQxHHE&A776==CF+,,    c                    |d   |d   g g d}| j                  |d         D ]c  }|d   rd|vr|d   |d<   |d   |d	<   |d   r"|d
   j                  |d   |d   |d   d       C|d   j                  |d   |d   |d   d       e |S )zA
    Return the details of an instance, populating IPs, etc.
    LINODEIDLABEL)idnamepublicprivateLinodeIdISPUBLICipv4	IPADDRESS	RDNS_NAMEfqdnr1   IPADDRESSID)r6   r9   ip_idr2   )linode_ip_listappend)r   serverinstanceips       r   getInstanceDetailsrA   7  s     Z(wH   &*< = Ej>fH4!+HV!+HVj>X%%r+/1+02=0A'C D Y&&;02;13M1B(D EE Or+   c           
      H   g }d}d}g }g }g }g }|
r8|j                  |
      }|r$|j                  |
      }|j                  |
      }|dv r}|s||||fD ]  }|r| j                  |d|d        d}|dnt	        d	 |D              }	 |j                  |||
      }|d   }
|j                  |
|
d|        |j                  dJ|
||d| |j                  |
      }|r	 |j                  |
      }|s||
|fD ]  }|r| j                  |d|d        d}	 |s
t               }|sd}|d   d   z
  |z
  }|r|j                  |
||||d|
d|      }n|j                  |
|||d|
d|      }|j                  |d          |j                  |
d|d|
d|      }|r;|D ]6  } | j!                  d      d| d<   |j                  |
| d   | d    | d   !      }8 |j                  |d          |sK||
|fD ]  }|r| j                  |d|d        |j#                         D ]  }!|!d"   |k7  rd#}"|!d$   rd%}" n |	s3|j%                         D ]   }#|#d&   j'                  d'"z        s|#d(   }	 n g }$|j                  |
      D ]F  } | d)   d*k(  r|$j)                  dt+        | d+                *|$j                  t+        | d+                H t-        |$      d,k  r |$j                  d-       t-        |$      d,k  r d.j/                  |$      }%d}	 |j1                  |
|	|%d/|z  0       |j                  |
      }|D ]s  }&|j                  |&d1         d   }&|&d2   d3k7  r(|j3                  |
      }|j                  |d          d}t5        j4                         |z   }|rf|t5        j4                         kD  rO|j                  |&d1         d   }&|&d2   d4v rn/t5        j6                  d5       |r|t5        j4                         kD  rO|r6|t5        j4                         k  r| j                  d6|&d&   d7|&d1   d       |j                  |&d1         d   }&|&d2   d8k(  r| j                  |&d&   d7|&d1   d9       t9        ||&      }'|rd:|'d;<   nd<|'d;<   |r|s||'d=<   |j                  |'       v n|d>v rd|s| j                  d?|
z         |D ]G  }&t9        ||&      }'|&d2   d@k7  r	 |j;                  |
      }dA|'d;<   d}ndB|'d;<   |j                  |'       I n|dCv rY|s| j                  d?|
z         |D ]<  }&t9        ||&      }'	 |j=                  |&d1         }dD|'d;<   d}|j                  |'       > nF|dEv rB|D ]=  }&t9        ||&      }'	 |j?                  |&d1   dF       dG|'d;<   d}|j                  |'       ? t-        |      d3k(  r| jA                  ||d   H       | jA                  ||I       y # t        $ r0}| j                  d|j                  d   d   z         Y d }~Zd }~ww xY w# t        $ rC}| j                  d|j                  d   d   z  t        j                                Y d }~d }~ww xY w# t        $ rC}| j                  d|j                  d   d   z  t        j                                Y d }~d }~ww xY w# t        $ rC}| j                  d|j                  d   d   z  t        j                                Y d }~d }~ww xY w# t        $ rC}| j                  d|j                  d   d   z  t        j                                Y d }~ld }~ww xY w# t        $ rC}| j                  d|j                  d   d   z  t        j                                Y d }~Xd }~ww xY w# t        $ rC}| j                  d|j                  d   d   z  t        j                                Y d }~`d }~ww xY w)KNFr3   )activepresentstartedz is required for z state)msgTr   c              3   &   K   | ]	  }|d      yw)SizeN )r   disks     r   r   z linodeServers.<locals>.<genexpr>y  s     DoVZT&\Dos   )DatacenterIDPlanIDPaymentTermLinodeID-)r4   Label)r4   LPM_DISPLAYGROUPWATCHDOG%sERRORMESSAGE)rN   rF   	exception   TOTALHDz data disk (lid: ))r4   DistributionIDrootPass
rootSSHKeyrP   rH   )r4   rZ   r[   rP   rH   JobIDswapz swap disk (lid: )r4   TyperP   rH   r_   ext4rP   rH   )rN   rP   rH   r_   DISTRIBUTIONID32IS64BIT64r.   z	Latest %sKERNELIDTYPEext3DISKID	   r   ,z	%s config)r4   KernelIdDisklistrP   r-   STATUS   )rn      zTimeout waiting on z (lid: ro   z) failed to bootRunningstatusStartingpassword)stoppedzServer (lid: %s) not found   StoppingStopped)	restarted
Restarting)absentdeleted)r4   
skipChecksDeleting)changedr?   )r   	instancesrI   )!linode_listlinode_disk_listlinode_config_list	fail_jsonsumlinode_createlinode_update	Exceptionvaluelinode_ip_addprivate	traceback
format_excr*   "linode_disk_createfromdistributionr=   linode_disk_creategetavail_distributionsavail_kernels
startswithinsertstrr$   r!   linode_config_createlinode_boottimesleeprA   linode_shutdownlinode_rebootlinode_delete	exit_json)(moduler   stater0   displaygroupplanadditional_disksdistribution
datacenter	kernel_id	linode_idpayment_termrt   
private_ipssh_pub_keyr^   waitwait_timeoutwatchdogkwargsr   r   
new_serverserversdisksconfigsjobsargused_disk_spaceresesizerJ   distribarchkerneldisks_id
disks_listr>   r?   s(                                           r   linodeServersr   P  st	    IGJGEGD  //9/5 (()(<E,,i,@G 00 dL*= W$$#u)U$VW J $4#;aDo^nDoAoOH''Z4@ ( B
O	!!9yRV<W!X!!!q9|^fqjpq//9/=
 j..	.B i6 W$$#u)U$VW J%j)|HDqz),>E@@!*<!)k:>	J!	 A #C @@!*<!):>	J!	 A #C
 CL),,ifOSU^3_26 - 8 $ 0 D88F+3+1DL!44itT[}cghncovz  |B  wC4  D	D CL)
 i6 W$$#u)U$VW
 224 +,<9%D !//1 F!'?55kD6HI  &z 2I	 H,,i,@ 5<6)OOAs4>':;DN 34	5 h-!## h-!#(+J Jj(()i2<KRVDV ) X00)0D
  )	'F__fZ.@_A!DFh1$ooyo9CL)  99;5L<$))+5&2DEaH (#w.

1 <$))+5 		3  &QX/[abl[m%n o__fZ.@_A!DFh2%  "(/6*3E&G  H *#v6H%."%/" +'/$X&S)	'V 
,	!=!KL 	'F)#v6Hh1$n--y-AC &0"%."X&	' 
.	 !=!KL 	'F)#v6Hj''
1C'D ".HXGX&	' 
'	' 	'F)#v6Hj!!6*+=$!O ",HXGX&	' 9~9Q<@
W	:y  H  TAGGAJ~,F%F GGH  j  TAGGAJ~,F%FR[RfRfRh iijX  j  TAGGAJ~,F%FR[RfRfRh iijX  j  TAGGAJ~,F%FR[RfRfRh iijp ! n$$
>0J)JV_VjVjVl$mmn   j  TAGGAJ~,F%FR[RfRfRh iij  j  TAGGAJ~,F%FR[RfRfRh iijs   AX +Y
 (CZ &*[(  \7 ^&_	Y%YY
	Z8ZZ	[%"8[  [%(	\418\//\47	^ 8]>>^	_8__	`!8``!c                     t        t        d?i dt        ddg d      dt        dddt        dgf	      d
t        dd      dt        d      dt        d      dt        d      dt        d      dt        d      dt        d      dt        d      dt        d      dt        d      dt        d      dt        d      dt        d      dt        dd      dt        d      dt        d d!"      d#t        d      d$t        d      d%t        d      d&t        dd'g(      d)t        dd*g d+      d,t        dd-      d.t        d      d/t        d      d0t        dd1      d2t        dd      d3t        dd4      d5t        dd      dd6d&gfdd7d&gfg8      } t        s | j	                  t        d9      t        :       | j                  j                  d      }| j                  j                  d      }| j                  j                  d
      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }	| j                  j                  d      }
| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d#      }| j                  j                  d$      }| j                  j                  d%      }| j                  j                  d&      }| j                  j                  d)      }| j                  j                  d,      }| j                  j                  d.      }| j                  j                  d/      }| j                  j                  d0      }| j                  j                  d2      }| j                  j                  d3      }t        | j                  j                  d5            }t        ||||||	|
|||||;      }|j                         D  !ci c]  \  } }!|!	| |! }"} }!	 t        j                  |      }#|#j                          t%        | #|||||||||||||||||fi |" y c c}!} w # t        $ rB}$| j	                  d<|$j                  d=   d>   z  t!        j"                         :       Y d }$~$kd }$~$ww xY w)@Nr   r   rD   )r{   rC   r|   rD   ry   rE   ru   )typedefaultchoicesapi_keyTLINODE_API_KEY)r   no_logrequiredfallbackr0   )r   r   alert_bwin_enabledbool)r   alert_bwin_thresholdintalert_bwout_enabledalert_bwout_thresholdalert_bwquota_enabledalert_bwquota_thresholdalert_cpu_enabledalert_cpu_thresholdalert_diskio_enabledalert_diskio_thresholdbackupweeklydaybackupwindowr   r   )r   r   r   r   listdict)r   elementsr   r   r   r   lid)r   aliasesr   rn   )rn         rt   )r   r   r   r   r^   rW   r   r   i,  r   ry   ru   )argument_specrequired_ifzlinode-pythonrU   )r   r   r   r   r   r   r   r   r   r   r   r   rS   r   rT   rI   )r   r   r	   
HAS_LINODEr   r   LINODE_IMP_ERRparamsr   r   items
linode_apiApi	test_echor   r   r   r   r   )%r   r   r   r0   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rt   r   r   r^   r   r   r   check_itemskvr   r   r   s%                                        r   mainr   D  s     
E9ln 
 eD4<ZjYkJlm 
 540	 

  $0 
 "&5!1 
 !%& 1 
 #'E"2 
 #'F"3 
 %)e$4 
 #/ 
 !%% 0 
 "&6!2 
 $(U#3 
 !e, 
  5)! 
" 5"5# 
$ 5!% 
& "v?' 
( 5)) 
* '+ 
, &- 
. w7/ 
0 5![I1 
2 uT23 
4 (5 
6 %(7 
8 5#.9 
: 640; 
< 5#6= 
> vt4? 
D kK=1i+/
E&FP 1/Bn]MMg&Emm	*G==V$D**+?@!==,,-CD --++,AB"MM--.EF"MM--.EF$mm//0IJ))*=> --++,AB!==,,-CD#]]../GHmm''(9:O==$$^4L==$$^4L==V$D}}(();<==$$^4L""<0J!!+.I!!+.I==$$^4L}}  ,H""<0J--##M2K==V$D==V$D==$$^4L6==$$Z01H-1/33 7+/15'!K  +002Dtq!amadDFDbnnW% &#ud"L*i*k4	4 -3	4 E  bTAGGAJ~$>>)J^J^J`aabs$   
VV%V" "	W-+8W((W-__main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESr   r   r   linoder   r   r   ImportErrorr   ansible.module_utils.basicr   r   r	   r*   rA   r   r   __name__rI   r+   r   <module>r      s    A @`DfP  (J
 Y X-(2q;hg4T zF e  )Y))+NJs   A A#"A#