
    VhM                         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m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: deploy_helper
author: "Ramon de la Fuente (@ramondelafuente)"
short_description: Manages some of the steps common in deploying projects
description:
  - The Deploy Helper manages some of the steps common in deploying software. It creates a folder structure, manages a symlink
    for the current release and cleans up old releases.
  - Running it with the O(state=query) or O(state=present) will return the C(deploy_helper) fact. C(project_path), whatever
    you set in the O(path) parameter, C(current_path), the path to the symlink that points to the active release, C(releases_path),
    the path to the folder to keep releases in, C(shared_path), the path to the folder to keep shared resources in, C(unfinished_filename),
    the file to check for to recognize unfinished builds, C(previous_release), the release the 'current' symlink is pointing
    to, C(previous_release_path), the full path to the 'current' symlink target, C(new_release), either the O(release) parameter
    or a generated timestamp, C(new_release_path), the path to the new release folder (not created by the module).
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none

options:
  path:
    type: path
    required: true
    aliases: ['dest']
    description:
      - The root path of the project. Returned in the C(deploy_helper.project_path) fact.
  state:
    type: str
    description:
      - The state of the project.
      - V(query) will only gather facts.
      - V(present) will create the project C(root) folder, and in it the C(releases) and C(shared) folders.
      - V(finalize) will remove the unfinished_filename file, create a symlink to the newly deployed release and optionally
        clean old releases.
      - V(clean) will remove failed & old releases.
      - V(absent) will remove the project folder (synonymous to the M(ansible.builtin.file) module with O(state=absent)).
    choices: [present, finalize, absent, clean, query]
    default: present

  release:
    type: str
    description:
      - The release version that is being deployed. Defaults to a timestamp format C(%Y%m%d%H%M%S) (for example V(20141119223359)).
        This parameter is optional during O(state=present), but needs to be set explicitly for O(state=finalize). You can
        use the generated fact C(release={{ deploy_helper.new_release }}).
  releases_path:
    type: str
    description:
      - The name of the folder that will hold the releases. This can be relative to O(path) or absolute. Returned in the C(deploy_helper.releases_path)
        fact.
    default: releases

  shared_path:
    type: path
    description:
      - The name of the folder that will hold the shared resources. This can be relative to O(path) or absolute. If this is
        set to an empty string, no shared folder will be created. Returned in the C(deploy_helper.shared_path) fact.
    default: shared

  current_path:
    type: path
    description:
      - The name of the symlink that is created when the deploy is finalized. Used in O(state=finalize) and O(state=clean).
        Returned in the C(deploy_helper.current_path) fact.
    default: current

  unfinished_filename:
    type: str
    description:
      - The name of the file that indicates a deploy has not finished. All folders in the O(releases_path) that contain this
        file will be deleted on O(state=finalize) with O(clean=true), or O(state=clean). This file is automatically deleted
        from the C(new_release_path) during O(state=finalize).
    default: DEPLOY_UNFINISHED

  clean:
    description:
      - Whether to run the clean procedure in case of O(state=finalize).
    type: bool
    default: true

  keep_releases:
    type: int
    description:
      - The number of old releases to keep when cleaning. Used in O(state=finalize) and O(state=clean). Any unfinished builds
        will be deleted first, so only correct releases will count. The current version will not count.
    default: 5

notes:
  - Facts are only returned for O(state=query) and O(state=present). If you use both, you should pass any overridden parameters
    to both calls, otherwise the second call will overwrite the facts of the first one.
  - When using O(state=clean), the releases are ordered by I(creation date). You should be able to switch to a new naming
    strategy without problems.
  - Because of the default behaviour of generating the C(new_release) fact, this module will not be idempotent unless you
    pass your own release name with O(release). Due to the nature of deploying software, this should not be much of a problem.
extends_documentation_fragment:
  - ansible.builtin.files
  - community.general.attributes
ai  
# General explanation, starting with an example folder structure for a project:

# root:
#     releases:
#         - 20140415234508
#         - 20140415235146
#         - 20140416082818
#
#     shared:
#         - sessions
#         - uploads
#
#     current: releases/20140416082818


# The 'releases' folder holds all the available releases. A release is a complete build of the application being
# deployed. This can be a clone of a repository for example, or a sync of a local folder on your filesystem.
# Having timestamped folders is one way of having distinct releases, but you could choose your own strategy like
# git tags or commit hashes.
#
# During a deploy, a new folder should be created in the releases folder and any build steps required should be
# performed. Once the new build is ready, the deploy procedure is 'finalized' by replacing the 'current' symlink
# with a link to this build.
#
# The 'shared' folder holds any resource that is shared between releases. Examples of this are web-server
# session files, or files uploaded by users of your application. It's quite common to have symlinks from a release
# folder pointing to a shared/subfolder, and creating these links would be automated as part of the build steps.
#
# The 'current' symlink points to one of the releases. Probably the latest one, unless a deploy is in progress.
# The web-server's root for the project will go through this symlink, so the 'downtime' when switching to a new
# release is reduced to the time it takes to switch the link.
#
# To distinguish between successful builds and unfinished ones, a file can be placed in the folder of the release
# that is currently in progress. The existence of this file will mark it as unfinished, and allow an automated
# procedure to remove it during cleanup.


# Typical usage
- name: Initialize the deploy root and gather facts
  community.general.deploy_helper:
    path: /path/to/root
- name: Clone the project to the new release folder
  ansible.builtin.git:
    repo: ansible.builtin.git://foosball.example.org/path/to/repo.git
    dest: '{{ deploy_helper.new_release_path }}'
    version: v1.1.1
- name: Add an unfinished file, to allow cleanup on successful finalize
  ansible.builtin.file:
    path: '{{ deploy_helper.new_release_path }}/{{ deploy_helper.unfinished_filename }}'
    state: touch
- name: Perform some build steps, like running your dependency manager for example
  composer:
    command: install
    working_dir: '{{ deploy_helper.new_release_path }}'
- name: Create some folders in the shared folder
  ansible.builtin.file:
    path: '{{ deploy_helper.shared_path }}/{{ item }}'
    state: directory
  with_items:
    - sessions
    - uploads
- name: Add symlinks from the new release to the shared folder
  ansible.builtin.file:
    path: '{{ deploy_helper.new_release_path }}/{{ item.path }}'
    src: '{{ deploy_helper.shared_path }}/{{ item.src }}'
    state: link
  with_items:
    - path: app/sessions
      src: sessions
    - path: web/uploads
      src: uploads
- name: Finalize the deploy, removing the unfinished file and switching the symlink
  community.general.deploy_helper:
    path: /path/to/root
    release: '{{ deploy_helper.new_release }}'
    state: finalize

# Retrieving facts before running a deploy
- name: Run 'state=query' to gather facts without changing anything
  community.general.deploy_helper:
    path: /path/to/root
    state: query
# Remember to set the 'release' parameter when you actually call 'state=present' later
- name: Initialize the deploy root
  community.general.deploy_helper:
    path: /path/to/root
    release: '{{ deploy_helper.new_release }}'
    state: present

# all paths can be absolute or relative (to the 'path' parameter)
- community.general.deploy_helper:
    path: /path/to/root
    releases_path: /var/www/project/releases
    shared_path: /var/www/shared
    current_path: /var/www/active

# Using your own naming strategy for releases (a version tag in this case):
- community.general.deploy_helper:
    path: /path/to/root
    release: v1.1.1
    state: present
- community.general.deploy_helper:
    path: /path/to/root
    release: '{{ deploy_helper.new_release }}'
    state: finalize

# Using a different unfinished_filename:
- community.general.deploy_helper:
    path: /path/to/root
    unfinished_filename: README.md
    release: '{{ deploy_helper.new_release }}'
    state: finalize

# Postponing the cleanup of older builds:
- community.general.deploy_helper:
    path: /path/to/root
    release: '{{ deploy_helper.new_release }}'
    state: finalize
    clean: false
- community.general.deploy_helper:
    path: /path/to/root
    state: clean
# Or running the cleanup ahead of the new deploy
- community.general.deploy_helper:
    path: /path/to/root
    state: clean
- community.general.deploy_helper:
    path: /path/to/root
    state: present

# Keeping more old releases:
- community.general.deploy_helper:
    path: /path/to/root
    release: '{{ deploy_helper.new_release }}'
    state: finalize
    keep_releases: 10
# Or, if you use 'clean=false' on finalize:
- community.general.deploy_helper:
    path: /path/to/root
    state: clean
    keep_releases: 10

# Removing the entire project root folder
- community.general.deploy_helper:
    path: /path/to/root
    state: absent

# Debugging the facts returned by the module
- community.general.deploy_helper:
    path: /path/to/root
- ansible.builtin.debug:
    var: deploy_helper
N)AnsibleModule)	to_nativec                   T    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y)DeployHelperc                    || _         |j                  |j                        | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _	        |j                  d   | _
        |j                  d   | _        |j                  d	   | _        y )
Ncleancurrent_pathkeep_releasespathreleasereleases_pathshared_pathstateunfinished_filename)moduleload_file_common_argumentsparams	file_argsr   r   r   r   r   r   r   r   r   )selfr   s     s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/deploy_helper.py__init__zDeployHelper.__init__  s    ::6==I]]7+
"MM.9#]]?;MM&)	}}Y/#]]?;!==7]]7+
#)==1F#G     c           
         t         j                  j                  | j                  | j                        }t         j                  j                  | j                  | j                        }| j
                  r5t         j                  j                  | j                  | j
                        }nd }| j                  |      \  }}| j                  s8| j                  dk(  s| j                  dk(  rt        j                  d      | _        | j                  r+t         j                  j                  || j                        }nd }| j                  |||||| j                  || j                  d	S )Nquerypresentz%Y%m%d%H%M%S)	project_pathr   r   r   previous_releaseprevious_release_pathnew_releasenew_release_pathr   )osr   joinr   r   r   _get_last_releaser   r   timestrftimer   )r   r   r   r   r    r!   r#   s          r   gather_factszDeployHelper.gather_facts%  s	   ww||DIIt/@/@ATYY0B0BC'',,tyy$2B2BCKK262H2H2V//||w!6$**	:Q==8DL<<!ww||M4<<H# !II(*& 0%:<< 0#'#;#;

 
	
r   c                    t         j                  j                  |      syt         j                  j                  |      s| j                  j                  d|z         | j                  j                  s	 t        j                  |d       yy# t        $ rE}| j                  j                  dt        |      z  t        j                                Y d }~yd }~ww xY w)NF %s exists but is not a directorymsg)ignore_errorszrmtree failed: %s)r-   	exceptionT)r$   r   lexistsisdirr   	fail_json
check_modeshutilrmtree	Exceptionr   	traceback
format_exc)r   r   es      r   delete_pathzDeployHelper.delete_pathC  s    wwt$ww}}T"KK!!&H4&O!P{{%%pd%8   p%%*=	!*LXaXlXlXn%oops   6B 	C;CCc                 |   d}t         j                  j                  |      s.d}| j                  j                  sTt        j
                  |       n>t         j                  j                  |      s| j                  j                  d|z         || j                  j                  | j                  |      |      z  }|S )NFTr+   r,   )
r$   r   r0   r   r3   makedirsr1   r2   %set_directory_attributes_if_different_get_file_args)r   r   changeds      r   create_pathzDeployHelper.create_pathR  s    wwt$G;;))D!t$KK!!&H4&O!P4;;DDTEXEXY]E^`ghhr   c                     t         j                  j                  |      r@t         j                  j                  |      s | j                  j                  d|z         y y y )Nz$%s exists but is not a symbolic linkr,   )r$   r   r0   islinkr   r2   )r   r   s     r   
check_linkzDeployHelper.check_linka  sH    77??4 77>>$'%%*PSW*W%X ( !r   c                 8   t         j                  j                  |      rKt         j                  j                  t         j                  j	                  |            }t         j                  j                  t         j                  j	                  |            }||k(  rd}|S d}| j
                  j                  st         j                  j                  |      s| j
                  j                  d|z         |dz   | j                  z   }t         j                  j                  |      rt        j                  |       t        j                  ||       t        j                  ||       |S d}| j
                  j                  st        j                  ||       |S )NFTz$the symlink target %s doesn't existsr,   .)r$   r   rB   normpathrealpathr   r3   r0   r2   r   unlinksymlinkrename)r   source	link_name	norm_linknorm_sourcer?   tmp_link_names          r   create_linkzDeployHelper.create_linkf  s&   77>>)$(()9)9))DEI''**277+;+;F+CDKK'   {{--77??62--2X[a2a-b$-Od6N6N$NMww~~m4		-0JJv}5IImY7 	 G;;))

69-r   c                     d}t         j                  j                  || j                        }t         j                  j	                  |      r-d}| j
                  j                  st        j                  |       |S )NFT)r$   r   r%   r   r0   r   r3   remove)r   r#   r?   unfinished_file_paths       r   remove_unfinished_filez#DeployHelper.remove_unfinished_file}  sX    !ww||,<d>V>VW77??/0G;;))		./r   c                 l   d}t        j                  |      D ]  }t         j                  j                  t         j                  j	                  ||| j
                              sL| j                  j                  r|dz  }h|| j                  t         j                  j	                  ||            z  } |S )Nr      )	r$   listdirr   isfiler%   r   r   r3   r:   )r   r   changesr   s       r   remove_unfinished_buildsz%DeployHelper.remove_unfinished_builds  s    zz-0 	VGww~~bggll='4C[C[\];;))qLGt//]G0TUUG	V r   c                 2   d}| j                   s|S t        j                  j                  || j                   dz   | j                  z         }| j
                  j                  s6t        j                  j                  |      rd}t        j                  |       |S )NFrE   T)	r   r$   r   r%   r   r   r3   existsrR   )r   r   r?   rO   s       r   remove_unfinished_linkz#DeployHelper.remove_unfinished_link  sm    ||NT4<<#+=@X@X+XY{{%%"''..*GGIIm$r   c           	         d}t         j                  j                        rt        j                        D cg c]B  }t         j                  j	                  t         j                  j                  |            sA|D }}	 |j                  |       | j                  j                  s^|j                  fdd       || j                  d  D ]4  }|| j                  t         j                  j                  |            z  }6 |S t        |      | j                  kD  r|t        |      | j                  z
  z  }|S c c}w # t        $ r Y w xY w)Nr   c                 ~    t         j                  j                  t         j                  j                  |             S N)r$   r   getctimer%   )xr   s    r   <lambda>z&DeployHelper.cleanup.<locals>.<lambda>  s&    BGG,<,<RWW\\-YZ=[,\ r   T)keyreverse)r$   r   r0   rW   r1   r%   rR   
ValueErrorr   r3   sortr   r:   len)r   r   reserve_versionrY   freleasesr   s    `     r   cleanupzDeployHelper.cleanup  s   77??=)#%::m#<nabggll[hjkNl@mnHn0 ;;))"\fjk'(:(:(;< VGt//]G0TUUGV
  X!3!33CMD,>,>>? o  s   AD?>D?E 	EEc                 D    | j                   j                         }||d<   |S )Nr   )r   copy)r   r   r   s      r   r>   zDeployHelper._get_file_args  s$    NN'')	 	&r   c                     d }d }t         j                  j                  |      r>t         j                  j                  |      }t         j                  j	                  |      }||fS r`   )r$   r   r0   rG   basename)r   r   r    r!   s       r   r&   zDeployHelper._get_last_release  sU     $77??<($&GG$4$4\$B!!ww//0EF!666r   N)__name__
__module____qualname__r   r)   r:   r@   rC   rP   rT   rZ   r]   rl   r>   r&    r   r   r	   r	     s@    H
<Y
.
&
7r   r	   c                  \   t        t        t        dgdd      t        d      t        dd      t        dd	      t        dd
      t        dd      t        dd      t        dd      t        g dd      	      dddgfgdd      } t        |       }|j                         }d|j                  i}d}|j                  dk(  r	d|i|d<   n|j                  dk(  rn|j                  |d          ||j                  |d         z  }||j                  |d         z  }|j                  r||j                  |d         z  }d|i|d<   n>|j                  dk(  r|j                  dk  r| j                  d        ||j                  |d!         z  }||j                  |d!   |d         z  }|j                  r||j                  |d         z  }||j                  |d         z  }||j                  |d   |d"         z  }n|j                  d#k(  rJ||j                  |d         z  }||j                  |d         z  }||j                  |d   |d"         z  }n-|j                  d$k(  rdg i|d<   ||j!                  |d         z  }|dkD  rd|d%<   nd&|d%<    | j"                  d'i | y )(NdestTr   )aliasesrequiredtypestr)ry   rk   )ry   defaultsharedcurrentint   boolDEPLOY_UNFINISHED)r   absentr   finalizer   r   )choicesr{   )	r   r   r   r   r   r   r   r   r   r   r   r   )argument_specrequired_ifadd_file_common_argssupports_check_moder   r   deploy_helperansible_factsr   r   r   r   z$'keep_releases' should be at least 1r,   r#   r"   r   r   r?   Frt   )r   dictr	   r)   r   rC   r@   r   r   r2   rT   rP   r   r]   rZ   rl   r:   	exit_json)r   r   factsresultrY   s        r   mainr     s   vhFCe$E:>&(;69=E15FD1 $%9L MR\ef

 j9+.
 " !F& !(M&&(E 	$$F Gg%#2E":					)  ~!67=,,U>-BCC=,,U?-CDD$$}00}1EFFG#2E":			
	*&&!+!GH=77>P8QRR=,,U3E-FnH]^^};;E.<QRRG}==eO>TUUG},,U?-CU=EYZZG				'=77n8MNN=99%:PQQ=(()?}AUVV				(#2B"7=,,U>-BCC{ y!yFvr   __main__)
__future__r   r   r   ry   __metaclass__DOCUMENTATIONEXAMPLESr$   r4   r'   r7   ansible.module_utils.basicr   +ansible.module_utils.common.text.convertersr   objectr	   r   rq   rt   r   r   <module>r      sc    A @aFYv 
    4 Ak76 k7\DN zF r   