
    Vhh&                     r    d dl mZmZmZ eZdZdZd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: imgadm
short_description: Manage SmartOS images
description:
  - Manage SmartOS virtual machine images through imgadm(1M).
author: Jasper Lievisse Adriaanse (@jasperla)
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  force:
    required: false
    type: bool
    description:
      - Force a given operation (where supported by imgadm(1M)).
  pool:
    required: false
    default: zones
    description:
      - The zpool to import to or delete images from.
    type: str
  source:
    required: false
    description:
      - URI for the image source.
    type: str
  state:
    required: true
    choices: [present, absent, deleted, imported, updated, vacuumed]
    description:
      - State the object operated on should be in. V(imported) is an alias for for V(present) and V(deleted) for V(absent).
        When set to V(vacuumed) and O(uuid=*), it will remove all unused images.
    type: str

  type:
    required: false
    choices: [imgapi, docker, dsapi]
    default: imgapi
    description:
      - Type for image sources.
    type: str

  uuid:
    required: false
    description:
      - Image UUID. Can either be a full UUID or V(*) for all images.
    type: str
a#  
- name: Import an image
  community.general.imgadm:
    uuid: '70e3ae72-96b6-11e6-9056-9737fd4d0764'
    state: imported

- name: Delete an image
  community.general.imgadm:
    uuid: '70e3ae72-96b6-11e6-9056-9737fd4d0764'
    state: deleted

- name: Update all images
  community.general.imgadm:
    uuid: '*'
    state: updated

- name: Update a single image
  community.general.imgadm:
    uuid: '70e3ae72-96b6-11e6-9056-9737fd4d0764'
    state: updated

- name: Add a source
  community.general.imgadm:
    source: 'https://datasets.project-fifo.net'
    state: present

- name: Add a Docker source
  community.general.imgadm:
    source: 'https://docker.io'
    type: docker
    state: present

- name: Remove a source
  community.general.imgadm:
    source: 'https://docker.io'
    state: absent
a  
source:
  description: Source that is managed.
  returned: When not managing an image.
  type: str
  sample: https://datasets.project-fifo.net
uuid:
  description: UUID for an image operated on.
  returned: When not managing an image source.
  type: str
  sample: 70e3ae72-96b6-11e6-9056-9737fd4d0764
state:
  description: State of the target, after execution.
  returned: success
  type: str
  sample: 'present'
N)AnsibleModulec                   *    e Zd Zd Zd Zd Zd Zd Zy)Imgadmc                    || _         |j                  | _        |j                  dd      | _        d| _        |j                  d   | _        | j                  d   dv rd| _        nd| _        | j
                  rS| j
                  dk7  rCt        j                  d	| j
                  t        j                        s|j                  d
       y y y y )NimgadmT)requiredFuuidstate)presentimportedupdated*z+^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$z3Provided value for uuid option is not a valid UUID.msg)moduleparamsget_bin_pathcmdchangedr   r   rematch
IGNORECASE	fail_json)selfr   s     l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/imgadm.py__init__zImgadm.__init__   s    mm&&x$&?MM&)	 ;;w#EEDL DL 99c)88I499VXVcVcd  %Z [ e *9    c                 Z    t        j                  d|      }|r|j                         d   S y)Nz$^imgadm .*?: error \(\w+\): (.*): .*r   zUnexpected failure)r   r   groups)r   stderrr   s      r   errmsgzImgadm.errmsg   s*    @&I<<>!$$'r    c                 p   | j                   dk(  rdj                  | j                        }n&dj                  | j                  | j                         }| j                  j	                  |      \  }}}|dk7  r:| j                  j                  dj                  | j                  |                   d| _        y )Nr   z
{0} updatez{0} update {1}r   zFailed to update images: {0}r   T)r   formatr   r   run_commandr   r$   r   )r   r   rcstdoutr#   s        r   update_imageszImgadm.update_images   s    99%%dhh/C"))$((DII>C#{{66s;VV7KK!!&D&K&KDKKX^L_&`!a
 r    c                    | j                   d   }| j                   d   }| j                   d   }dj                  | j                        }|r|dz  }| j                  rdj                  |||      }| j                  j                  |      \  }}}|dk7  r:| j                  j                  dj                  | j                  |            	       d
j                  ||      }t        j                  ||      rd| _
        d|d|d}t        j                  ||      rd| _
        y y |d|z  z  }| j                  j                  |      \  }}}|dk7  r:| j                  j                  dj                  | j                  |            	       d|z  }t        j                  ||      rd| _
        d|z  }t        j                  ||      rd| _
        y y )Nforcesourcetypez{0} sourcesz -fz{0} -a {1} -t {2}r   zFailed to add source: {0}r   z0Already have "{0}" image source "{1}", no changeFzAdded "z" image source ""Tz -d %szFailed to remove source: {0}z(Do not have image source "%s", no changezDeleted ".*" image source "%s")r   r&   r   r   r   r'   r   r$   r   r   r   )	r   r,   r-   imgtyper   r(   r)   r#   regexs	            r   manage_sourceszImgadm.manage_sources   s   G$X&++f%""488,5LC<<%,,S&'BC#';;#:#:3#? RQw%%*E*L*LT[[Y_M`*a%b GMMgW]^Exxv&$6=vFExxv&# ' 8f$$C#';;#:#:3#? RQw%%*H*O*OPTP[P[\bPc*d%e>GExxv&$4v=Exxv&# 'r    c                 
   | j                   d   }| j                   d   }|dk(  rdj                  | j                        }| j                  j	                  |      \  }}}|dk7  r;| j                  j                  dj                  | j                  |                   n|dk(  rd	| _        nd
| _        | j                  r'dj                  | j                  || j                        }| j                  j	                  |      \  }}}|dk7  r:| j                  j                  dj                  | j                  |                   dj                  | j                        }t        j                  ||      rd	| _        d}t        j                  ||      rd	| _        dj                  | j                        }t        j                  ||j                         d         rd
| _        y y dj                  | j                  || j                        }| j                  j	                  |      \  }}}d}t        j                  ||      rd	| _        dj                  | j                        }t        j                  ||      rd
| _        y y )Npoolr   vacuumedz{0} vacuum -fr   zFailed to vacuum images: {0}r    FTz{0} import -P {1} -q {2}zFailed to import image: {0}z/Image {0} \(.*\) is already installed, skippingz.*ActiveImageNotFound.*zImported image {0}.*z{0} delete -P {1} {2}z.*ImageNotInstalled.*zDeleted image {0})r   r&   r   r   r'   r   r$   r   r   r   r   r   
splitlines)r   r4   r   r   r(   r)   r#   r1   s           r   manage_imageszImgadm.manage_images   s   {{6"G$J!((2C#';;#:#:3#? RQw%%*H*O*OPTP[P[\bPc*d%eR<#(DL#'DL<<,33DHHdDIINC#';;#:#:3#? RQw%%*G*N*Nt{{[aOb*c%dFMMdiiXExxv&$-Exxv&$*11$))<Exxv002267# 8 *004KC#';;#:#:3#? R+Exxv&  %'..tyy9Exxv&# 'r    N)__name__
__module____qualname__r   r$   r*   r2   r9    r    r   r   r      s    \(( *$X1$r    r   c                  0   t        t        t        d      t        d      t               t        dg d      t        dg d	
      t                     d      } t        |       }| j                  d   }| j                  d   }| j                  d   }d|i}|r||d<   |j	                          nG||d<   |dk(  r|j                          n,|dk(  r|dk7  r| j                  d       |j                          |j                  |d<    | j                  di | y )Nbool)r.   zones)defaultT)r   absentdeletedr   r   r5   )r   choicesimgapi)rE   dockerdsapi)rA   rD   )r,   r4   r-   r   r.   r   F)argument_specsupports_check_moder   r-   r   r   r   r5   z3Can only specify uuid as "*" when updating image(s)r   r   r=   )
r   dictr   r   r2   r*   r   r9   r   	exit_json)r   r
   r   r-   r   results         r   mainrM     s   F#g&6.qrh0MN
 "F F^F== D]]8$FMM'"EuF !xvI  " %:"5  %Z [  "F9Fvr    __main__)
__future__r   r   r   r.   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   ansible.module_utils.basicr   objectr   rM   r:   r=   r    r   <module>rV      s[    A @3j$L
$ 
 4I$V I$X'T zF r    