
    VhC                         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Zd dlZd dlmZ  G d de      Zd Zed	k(  r e        yy)
    )absolute_importdivisionprint_functiona  
module: solaris_zone
short_description: Manage Solaris zones
description:
  - Create, start, stop and delete Solaris zones.
  - This module does not currently allow changing of options for a zone that is already been created.
author:
  - Paul Markham (@pmarkham)
requirements:
  - Solaris 10 or 11
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  state:
    description:
      - V(present), configure and install the zone.
      - V(installed), synonym for V(present).
      - V(running), if the zone already exists, boot it, otherwise, configure and install the zone first, then boot it.
      - V(started), synonym for V(running).
      - V(stopped), shutdown a zone.
      - V(absent), destroy the zone.
      - V(configured), configure the ready so that it is to be attached.
      - V(attached), attach a zone, but do not boot it.
      - V(detached), shutdown and detach a zone.
    type: str
    choices: [absent, attached, configured, detached, installed, present, running, started, stopped]
    default: present
  name:
    description:
      - Zone name.
      - A zone name must be unique name.
      - A zone name must begin with an alphanumeric character.
      - The name can contain alphanumeric characters, underscores V(_), hyphens V(-), and periods V(.).
      - The name cannot be longer than 64 characters.
    type: str
    required: true
  path:
    description:
      - The path where the zone will be created. This is required when the zone is created, but not used otherwise.
    type: str
  sparse:
    description:
      - Whether to create a sparse (V(true)) or whole root (V(false)) zone.
    type: bool
    default: false
  root_password:
    description:
      - The password hash for the root account. If not specified, the zone's root account will not have a password.
    type: str
  config:
    description:
      - The C(zonecfg) configuration commands for this zone. See zonecfg(1M) for the valid options and syntax. Typically this
        is a list of options separated by semi-colons or new lines, for example V(set auto-boot=true;add net;set physical=bge0;set
        address=10.1.1.1;end).
    type: str
    default: ''
  create_options:
    description:
      - Extra options to the zonecfg(1M) create command.
    type: str
    default: ''
  install_options:
    description:
      - Extra options to the zoneadm(1M) install command. To automate Solaris 11 zone creation, use this to specify the profile
        XML file, for example O(install_options=-c sc_profile.xml).
    type: str
    default: ''
  attach_options:
    description:
      - Extra options to the zoneadm attach command. For example, this can be used to specify whether a minimum or full update
        of packages is required and if any packages need to be deleted. For valid values, see zoneadm(1M).
    type: str
    default: ''
  timeout:
    description:
      - Timeout, in seconds, for zone to boot.
    type: int
    default: 600
a  
- name: Create and install a zone, but don't boot it
  community.general.solaris_zone:
    name: zone1
    state: present
    path: /zones/zone1
    sparse: true
    root_password: Be9oX7OSwWoU.
    config: 'set autoboot=true; add net; set physical=bge0; set address=10.1.1.1; end'

- name: Create and install a zone and boot it
  community.general.solaris_zone:
    name: zone1
    state: running
    path: /zones/zone1
    root_password: Be9oX7OSwWoU.
    config: 'set autoboot=true; add net; set physical=bge0; set address=10.1.1.1; end'

- name: Boot an already installed zone
  community.general.solaris_zone:
    name: zone1
    state: running

- name: Stop a zone
  community.general.solaris_zone:
    name: zone1
    state: stopped

- name: Destroy a zone
  community.general.solaris_zone:
    name: zone1
    state: absent

- name: Detach a zone
  community.general.solaris_zone:
    name: zone1
    state: detached

- name: Configure a zone, ready to be attached
  community.general.solaris_zone:
    name: zone1
    state: configured
    path: /zones/zone1
    root_password: Be9oX7OSwWoU.
    config: 'set autoboot=true; add net; set physical=bge0; set address=10.1.1.1; end'

- name: Attach zone1
  community.general.solaris_zone:
    name: zone1
    state: attached
    attach_options: -u
N)AnsibleModulec                       e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)Zonec                    d| _         g | _        || _        | 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d      | _        | j                  j                  dd      | _        | j                  j                  dd      | _        | j                  j"                  r| j                  j%                  d       t'        j(                         dk7  r| j                  j+                  d       t'        j,                         j/                  d      \  | _        | _        t5        | j2                        dk  r| j                  j+                  d       t7        j8                  d| j
                        }|s| j                  j+                  d       y y )NFpathnamesparseroot_passwordtimeoutconfigcreate_optionsinstall_optionsattach_optionszoneadmTzonecfgz
ssh-keygenzRunning in check modeSunOSzThis module requires Solarismsg.
   z(This module requires Solaris 10 or laterz!^[a-zA-Z0-9][-_.a-zA-Z0-9]{0,62}$zmProvided zone name is not a valid zone name. Please refer documentation for correct zone name specifications.)changedr   moduleparamsr
   r   r   r   r   r   r   r   r   get_bin_pathzoneadm_cmdzonecfg_cmdssh_keygen_cmd
check_modeappendplatformsystem	fail_jsonreleasesplitos_majoros_minorintrematch)selfr   r,   s      r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/solaris_zone.py__init__zZone.__init__   s   KK&&v.	KK&&v.	kk((2![[//@{{)))4kk((2"kk001AB#{{112CD"kk001AB;;33ItD;;33ItD"kk66|TJ;;!!HHOO34??'KK!!&D!E)1)9)9);)A)A#)F&t}}"KK!!&P!Q<diiHKK!! 'i! j     c                    | j                   s| j                  j                  d       | j                  j                  sot	        j
                  dd      }| j                  r:|j                  d| j                  z         | j                  j                  d       n9|j                  d| j                  z         | j                  j                  d	       |j                  d
| j                   z         |j                  d| j                  z         |j                          | j                  d| j                  d|j                  }| j                  j                  |      \  }}}|dk7  r"| j                  j                  d||z   z         t!        j"                  |j                         d| _        | j                  j                  d       y )NzMissing required argument: pathr   Fwt)deletemodez
create %s
zcreating sparse-root zonezcreate -b %s
zcreating whole-root zonezset zonepath=%s
z%s
 -z  -f r   zFailed to create zone. %sTzzone configured)r
   r   r%   r!   tempfileNamedTemporaryFiler   writer   r   r"   r   closer   r   run_commandosunlinkr   )r-   tcmdrcouterrs         r.   	configurezZone.configure   sL   yyKK!!&G!H{{%%++5tDA{{(;(;;< ;<(4+>+>>? :;GG'$))34GGFT[[()GGI&*&6&6		166JC![[44S9NRcQw%%*Es*S%TIIaff)*r0   c                    | j                   j                  s| j                  d| j                  d| j                  }| j                   j                  |      \  }}}|dk7  r"| j                   j                  d||z   z         t        | j                        dk(  r| j                          | j                          | j                          d| _        | j                  j                  d       y )	Nr5   z	 install r   zFailed to install zone. %sr   r   Tzzone installed)r   r!   r   r   r   r;   r%   r*   r)   configure_sysidconfigure_passwordconfigure_ssh_keysr   r   r"   r-   r?   r@   rA   rB   s        r.   installzZone.install   s    {{%%+/+;+;TYYH\H\]C![[44S9NRcQw%%*F#PS)*T%U4==!R'$$&##%##%()r0   c                 Z   | j                         r| j                  j                  sb| j                  d| j                  d}| j                  j                  |      \  }}}|dk7  r"| j                  j                  d||z   z         d| _        | j                  j                  d       y y )Nr5   z uninstall -Fr   zFailed to uninstall zone. %sr   Tzzone uninstalled)
is_installedr   r!   r   r   r;   r%   r   r   r"   rH   s        r.   	uninstallzZone.uninstall   s    ;;))151A1A499M!%!8!8!=S#7KK)).LPSVYPY.Z)[DLHHOO./ r0   c                 t   t         j                  j                  d| j                  z        r"t        j                  d| j                  z         t	        d| j                  z  d      j                          t	        d| j                  z  d      5 }|j                  | j                         d d d        t	        d| j                  z  d      5 }|j                  d       |j                  d       |j                  d       |j                  d	       |j                  d
       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nz%s/root/etc/.UNCONFIGUREDz%s/root/noautoshutdownwz%s/root/etc/nodenamez%s/root/etc/.sysIDtool.statez(1       # System previously configured?
z 1       # Bootparams succeeded?
z"1       # System is on a network?
z11       # Extended network information gathered?
z 0       # Autobinder succeeded?
z1       # Network has subnets?
z&1       # root password prompted for?
z(1       # locale and term prompted for?
z#1       # security policy in place
z"1       # NFSv4 domain configured
z'0       # Auto Registration Configured
vt100)r<   r
   isfiler=   openr:   r9   r   )r-   nodeids      r.   rE   zZone.configure_sysid   sW   77>>5		ABII1DII=>%		137==?(4994c: 	"dJJtyy!	" 0499<cB 	bHH@AHH89HH:;HHIJHH89HH78HH>?HH@AHH;<HH:;HH?@HHW	 		" 	"	 	s   F"CF."F+.F7c                     d| j                   z  }d| j                   z  }t        j                   j                  |      sX| j                  d|d}| j                  j                  |      \  }}}|dk7  r"| j                  j                  d||z   z         t        j                   j                  |      sZ| j                  d|d}| j                  j                  |      \  }}}|dk7  r#| j                  j                  d	||z   z         y y y )
Nz %s/root/etc/ssh/ssh_host_rsa_keyz %s/root/etc/ssh/ssh_host_dsa_keyr6   z -t rsa -N ""r   zFailed to create rsa key. %sr   z -t dsa -N ""zFailed to create dsa key. %s)r
   r<   rP   r    r   r;   r%   )r-   rsa_key_filedsa_key_filer?   r@   rA   rB   s          r.   rG   zZone.configure_ssh_keys
  s    9DIIE9DIIEww~~l+-1-@-@,OC![[44S9NRcQw%%*HCRUI*V%Www~~l+-1-@-@,OC![[44S9NRcQw%%*HCRUI*V%W  ,r0   c                    d| j                   z  }| j                  rt        |d      5 }|j                         }d d d        t	        dt                    D ]B  }||   j                  d      }|d   dk(  s | j                  |d<   dj                  |      ||<   D t        |d      5 }|D ]  }|j                  |        	 d d d        y y # 1 sw Y   xY w# 1 sw Y   y xY w)Nz%s/root/etc/shadowrr   :root   rN   )	r
   r   rQ   	readlinesrangelenr'   joinr9   )r-   shadowflinesifieldslines          r.   rF   zZone.configure_password  s    %		1fc" &a& 1c%j) 0q,!9& $ 2 2F1I"xx/E!H	0 fc" "a! "DGGDM"" " & &" "s   C'CCC c                    | j                   j                  s| j                  d| j                  d}| j                   j	                  |      \  }}}|dk7  r"| j                   j                  d||z   z         	 d}	 || j                  kD  r| j                   j                  d       t        j                  d| j                  z        }|dk(  rnt        j                  d	       |d	z  }nd| _        | j                  j                  d
       y )Nr5   z bootr   zFailed to boot zone. %sr   Tz"timed out waiting for zone to bootzGps -z %s -o args|grep "ttymon.*-d /dev/console" > /dev/null 2>/dev/nullr   zzone booted)r   r!   r   r   r;   r%   r   r<   r$   timesleepr   r   r"   )r-   r?   r@   rA   rB   elapseds         r.   bootz	Zone.boot*  s    {{%%%)%5%5tyyAC![[44S9NRcQw%%*CsSy*Q%R GT\\)KK)).R)SYYhkoktkttu7

22  &r0   c                    | j                         r| j                          | j                         r| j                          | j                  j
                  sb| j                  d| j                  d}| j                  j                  |      \  }}}|dk7  r"| j                  j                  d||z   z         d| _
        | j                  j                  d       y )Nr5   z
 delete -Fr   zFailed to delete zone. %sr   Tzzone deleted)
is_runningstoprK   rL   r   r!   r   r   r;   r%   r   r   r"   rH   s        r.   destroyzZone.destroyD  s    ??IIKNN{{%%*.*:*:DIIFC![[44S9NRcQw%%*Es*S%T'r0   c                 8   | j                   j                  sb| j                  d| j                  d}| j                   j	                  |      \  }}}|dk7  r"| j                   j                  d||z   z         d| _        | j                  j                  d       y )Nr5   z haltr   zFailed to stop zone. %sr   Tzzone stopped	r   r!   r   r   r;   r%   r   r   r"   rH   s        r.   rm   z	Zone.stopQ  sz    {{%%%)%5%5tyyAC![[44S9NRcQw%%*CsSy*Q%R'r0   c                 8   | j                   j                  sb| j                  d| j                  d}| j                   j	                  |      \  }}}|dk7  r"| j                   j                  d||z   z         d| _        | j                  j                  d       y )Nr5   z detachr   zFailed to detach zone. %sr   Tzzone detachedrp   rH   s        r.   detachzZone.detachZ  sz    {{%%'+'7'7CC![[44S9NRcQw%%*Es*S%T(r0   c                 P   | j                   j                  sn| j                  d| j                  d| j                  }| j                   j                  |      \  }}}|dk7  r"| j                   j                  d||z   z         d| _        | j                  j                  d       y )Nr5   z attach r   zFailed to attach zone. %sr   Tzzone attached)
r   r!   r   r   r   r;   r%   r   r   r"   rH   s        r.   attachzZone.attachc  s    {{%%*.*:*:DIItGZGZ[C![[44S9NRcQw%%*Es*S%T(r0   c                     | j                   d| j                  d}| j                  j                  |      \  }}}|dk(  ryy)Nr5   z listr   TF)r   r   r   r;   rH   s        r.   existszZone.existsl  s=    !%!1!1499=005S#7r0   c                 (    | j                         dk(  S )Nrunningstatusr-   s    r.   rl   zZone.is_runningt  s    {{}	))r0   c                 (    | j                         dk(  S )N	installedry   r{   s    r.   rK   zZone.is_installedw  s    {{}++r0   c                 (    | j                         dk(  S )N
configuredry   r{   s    r.   is_configuredzZone.is_configuredz  s    {{},,r0   c                     | j                   d| j                  d}| j                  j                  |      \  }}}|dk(  r|j	                  d      d   S y)Nr5   z list -pr   rY      	undefined)r   r   r   r;   r'   rH   s        r.   rz   zZone.status}  sM    $($4$4dii@005S#799S>!$$r0   c                     | j                         r| j                  j                  d       y | j                          | j	                          y Nzzone already exists)rv   r   r"   rC   rI   r{   s    r.   state_presentzZone.state_present  s/    ;;=HHOO12NNLLNr0   c                     | j                          | j                         r| j                  j                  d       y | j	                          y )Nzzone already running)r   rl   r   r"   rj   r{   s    r.   state_runningzZone.state_running  s2    ??HHOO23IIKr0   c                 ~    | j                         r| j                          y | j                  j                  d       y )Nzone does not existr   )rv   rm   r   r%   r{   s    r.   state_stoppedzZone.state_stopped  s*    ;;=IIKKK!!&;!<r0   c                     | j                         r1| j                         r| j                          | j                          y | j                  j                  d       y )Nr   )rv   rl   rm   rn   r   r"   r{   s    r.   state_absentzZone.state_absent  s8    ;;= 		LLNHHOO12r0   c                 |    | j                         r| j                  j                  d       y | j                          y r   )rv   r   r"   rC   r{   s    r.   state_configuredzZone.state_configured  s&    ;;=HHOO12NNr0   c                     | j                         s| j                  j                  d       | j                         r| j                  j                  d       y | j                          | j                          y )Nr   r   zzone already detached)rv   r   r%   r   r   r"   rm   rr   r{   s    r.   state_detachedzZone.state_detached  sP    {{}KK!!&;!<HHOO34IIKKKMr0   c                     | j                         s| j                  j                  d       | j                         r| j	                          y | j                  j                  d       y )Nr   zzone already attached)rv   r   r"   r   rt   r{   s    r.   state_attachedzZone.state_attached  sB    {{}HHOO12KKMHHOO34r0   N)__name__
__module____qualname__r/   rC   rI   rL   rE   rG   rF   rj   rn   rm   rr   rt   rv   rl   rK   r   rz   r   r   r   r   r   r   r    r0   r.   r   r      s}     jD+6*0.X " '4(())*,-=35r0   r   c                  6   t        t        t        dd      t        ddg d      t        d      t        dd	
      t        dd      t        dd
      t        dd
      t        dd
      t        dd
      t        dd
      
      d      } t        |       }| j                  d   }|dk(  s|dk(  r|j	                          n|dk(  s|dk(  r|j                          n|dk(  r|j                          nm|dk(  r|j                          nW|dk(  r|j                          nA|dk(  r|j                          n+|dk(  r|j                          n| j                  d|z         | j                  |j                  dj                  |j                               y )NstrT)typerequiredpresent)	absentattachedr   detachedr}   r   rx   startedstopped)r   defaultchoices)r   boolF)r   r   )r   no_logr*   iX   )
r   stater
   r   r   r   r   r   r   r   )argument_specsupports_check_moder   rx   r   r}   r   r   r   r   r   zInvalid state: %sr   z, )r   r   )r   dictr   r   r   r   r   r   r   r   r   r%   	exit_jsonr   r_   r   )r   zoner   s      r.   mainr     sr   540E9  IJ5!VU3E$7eS1UB/UB7 eR8UB7
 !F" <DMM'"E	Ui/	)	u3	)		(		,		*		*	0589
T\\tyy/BCr0   __main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESr<   r#   r+   r7   rg   ansible.module_utils.basicr   objectr   r   r   r   r0   r.   <module>r      sb    A @Sj3j 
  	   4X56 X5v'DT zF r0   