dev/provisioning/modules/puppi/manifests/project/archive.pp
changeset 28 b0b56e0f8c7f
equal deleted inserted replaced
27:a2342f26c9de 28:b0b56e0f8c7f
       
     1 # = Define puppi::project::archive
       
     2 #
       
     3 # This is a puppi deployment project to be used for archives
       
     4 # like tarballs and zips
       
     5 #
       
     6 #
       
     7 # It uses different "core" defines (puppi::project, puppi:deploy (many),
       
     8 # puppi::rollback (many)) to build a full featured template project for
       
     9 # automatic deployments.
       
    10 # If you need to customize it, either change the template defined here or
       
    11 # build up your own custom ones.
       
    12 #
       
    13 # == Variables:
       
    14 #
       
    15 # [*source*]
       
    16 #   The full URL of the main file to retrieve.
       
    17 #   Format should be in URI standard (http:// file:// ssh:// rsync://).
       
    18 #
       
    19 # [*deploy_root*]
       
    20 #   The destination directory where the retrieved file(s) are deployed.
       
    21 #
       
    22 # [*user*]
       
    23 #   (Optional) - The user to be used for deploy operations.
       
    24 #
       
    25 # [*predeploy_customcommand*]
       
    26 #   (Optional) -  Full path with arguments of an eventual custom command to
       
    27 #   execute before the deploy. The command is executed as $predeploy_user.
       
    28 #
       
    29 # [*predeploy_user*]
       
    30 #   (Optional) - The user to be used to execute the $predeploy_customcommand.
       
    31 #   By default is the same of $user.
       
    32 #
       
    33 # [*predeploy_priority*]
       
    34 #   (Optional) - The priority (execution sequence number) that defines when,
       
    35 #   during the deploy procedure, the $predeploy_customcommand is executed
       
    36 #   Default: 39 (immediately before the copy of files on the deploy root).
       
    37 #
       
    38 # [*postdeploy_customcommand*]
       
    39 #   (Optional) -  Full path with arguments of an eventual custom command to
       
    40 #   execute after the deploy. The command is executed as $postdeploy_user.
       
    41 #
       
    42 # [*postdeploy_user*]
       
    43 #   (Optional) - The user to be used to execute the $postdeploy_customcommand.
       
    44 #   By default is the same of $user.
       
    45 #
       
    46 # [*postdeploy_priority*]
       
    47 #   (Optional) - The priority (execution sequence number) that defines when,
       
    48 #   during the deploy procedure, the $postdeploy_customcommand is executed
       
    49 #   Default: 41 (immediately after the copy of files on the deploy root).
       
    50 #
       
    51 # [*disable_services*]
       
    52 #   (Optional) - The names (space separated) of the services you might want to
       
    53 #   stop during deploy. By default is blank. Example: "apache puppet monit".
       
    54 #
       
    55 # [*firewall_src_ip*]
       
    56 #   (Optional) - The IP address of a loadbalancer you might want to block out
       
    57 #   during a deploy.
       
    58 #
       
    59 # [*firewall_dst_port*]
       
    60 #   (Optional) - The local port to block from the loadbalancer during deploy
       
    61 #   (Default all).
       
    62 #
       
    63 # [*firewall_delay*]
       
    64 #   (Optional) - A delay time in seconds to wait after the block of
       
    65 #   $firewall_src_ip. Should be at least as long as the loadbalancer check
       
    66 #   interval for the services stopped during deploy (Default: 1).
       
    67 #
       
    68 # [*report_email*]
       
    69 #   (Optional) - The (space separated) email(s) to notify of deploy/rollback
       
    70 #   operations. If none is specified, no email is sent.
       
    71 #
       
    72 # [*clean_deploy*]
       
    73 #   (Optional, default false) - If during the deploy procedure, all the
       
    74 #   existing files that are not on the source have to be deleted.
       
    75 #   (When true, a --delete option is added to the rsync command)
       
    76 #   Do not set to true if source files are incremental.
       
    77 #
       
    78 # [*backup_enable*]
       
    79 #   (Optional, default true) - If the backup of files in the deploy dir
       
    80 #   is done (before deploy). If set to false, rollback is disabled.
       
    81 #
       
    82 # [*backup_rsync_options*]
       
    83 #   (Optional) - The extra options to pass to rsync for backup operations. Use
       
    84 #   it, for example, to exclude directories that you don't want to archive.
       
    85 #   IE: "--exclude .snapshot --exclude cache --exclude www/cache".
       
    86 #
       
    87 # [*backup_retention*]
       
    88 #   (Optional) - Number of backup archives to keep. (Default 5).
       
    89 #   Lower the default value if your backups are too large and may fill up the
       
    90 #   filesystem.
       
    91 #
       
    92 # [*run_checks*]
       
    93 #   (Optional) - If you want to run local puppi checks before and after the
       
    94 #   deploy procedure. Default: "true".
       
    95 #
       
    96 # [*always_deploy*]
       
    97 #   (Optional) - If you always deploy what has been downloaded. Default="yes",
       
    98 #   if set to "no" a checksum is made between the files previously downloaded
       
    99 #   and the new files. If they are the same the deploy is not done.
       
   100 #
       
   101 # [*auto_deploy*]
       
   102 #   (Optional) - If you want to automatically run this puppi deploy when
       
   103 #   Puppet runs. Default: 'false'
       
   104 #
       
   105 define puppi::project::archive (
       
   106   $source,
       
   107   $deploy_root,
       
   108   $user                     = 'root',
       
   109   $predeploy_customcommand  = '',
       
   110   $predeploy_user           = '',
       
   111   $predeploy_priority       = '39',
       
   112   $postdeploy_customcommand = '',
       
   113   $postdeploy_user          = '',
       
   114   $postdeploy_priority      = '41',
       
   115   $disable_services         = '',
       
   116   $firewall_src_ip          = '',
       
   117   $firewall_dst_port        = '0',
       
   118   $firewall_delay           = '1',
       
   119   $report_email             = '',
       
   120   $clean_deploy             = false,
       
   121   $backup_enable            = true,
       
   122   $backup_rsync_options     = '--exclude .snapshot',
       
   123   $backup_retention         = '5',
       
   124   $run_checks               = true,
       
   125   $always_deploy            = true,
       
   126   $auto_deploy              = false,
       
   127   $enable                   = true ) {
       
   128 
       
   129   require puppi
       
   130   require puppi::params
       
   131 
       
   132   # Set default values
       
   133   $predeploy_real_user = $predeploy_user ? {
       
   134     ''      => $user,
       
   135     default => $predeploy_user,
       
   136   }
       
   137 
       
   138   $postdeploy_real_user = $postdeploy_user ? {
       
   139     ''      => $user,
       
   140     default => $postdeploy_user,
       
   141   }
       
   142 
       
   143   $real_always_deploy = any2bool($always_deploy) ? {
       
   144     false   => 'no',
       
   145     true    => 'yes',
       
   146   }
       
   147 
       
   148 
       
   149   $bool_run_checks = any2bool($run_checks)
       
   150   $bool_clean_deploy = any2bool($clean_deploy)
       
   151   $bool_backup_enable = any2bool($backup_enable)
       
   152   $bool_auto_deploy = any2bool($auto_deploy)
       
   153 
       
   154   $source_type = url_parse($source,filetype)
       
   155 
       
   156   $real_source_type = $source_type ? {
       
   157     '.tar'     => 'tar',
       
   158     '.tar.gz'  => 'tarball',
       
   159     '.gz'      => 'tarball',
       
   160     '.tgz'     => 'tarball',
       
   161     '.zip'     => 'zip',
       
   162   }
       
   163 
       
   164 ### CREATE PROJECT
       
   165     puppi::project { $name:
       
   166       enable => $enable ,
       
   167     }
       
   168 
       
   169 ### DEPLOY SEQUENCE
       
   170   if ($bool_run_checks == true) {
       
   171     puppi::deploy { "${name}-Run_PRE-Checks":
       
   172       priority  => '10' ,
       
   173       command   => 'check_project.sh' ,
       
   174       arguments => $name ,
       
   175       user      => 'root' ,
       
   176       project   => $name ,
       
   177       enable    => $enable ,
       
   178     }
       
   179   }
       
   180 
       
   181     # Here source file is retrieved
       
   182     puppi::deploy { "${name}-Retrieve_Archive":
       
   183       priority  => '20' ,
       
   184       command   => 'get_file.sh' ,
       
   185       arguments => "-s ${source} -t ${real_source_type} -a ${real_always_deploy}" ,
       
   186       user      => 'root' ,
       
   187       project   => $name ,
       
   188       enable    => $enable ,
       
   189     }
       
   190 
       
   191     puppi::deploy { "${name}-PreDeploy_Archive":
       
   192       priority => '25' ,
       
   193       command  => 'predeploy.sh' ,
       
   194       user     => 'root' ,
       
   195       project  => $name ,
       
   196       enable   => $enable ,
       
   197     }
       
   198 
       
   199   if ($firewall_src_ip != '') {
       
   200     puppi::deploy { "${name}-Load_Balancer_Block":
       
   201       priority  => '25' ,
       
   202       command   => 'firewall.sh' ,
       
   203       arguments => "${firewall_src_ip} ${firewall_dst_port} on ${firewall_delay}" ,
       
   204       user      => 'root',
       
   205       project   => $name ,
       
   206       enable    => $enable ,
       
   207     }
       
   208   }
       
   209 
       
   210   if ($bool_backup_enable == true) {
       
   211     puppi::deploy { "${name}-Backup_existing_Files":
       
   212       priority  => '30' ,
       
   213       command   => 'archive.sh' ,
       
   214       arguments => "-b ${deploy_root} -o '${backup_rsync_options}' -n ${backup_retention}" ,
       
   215       user      => 'root' ,
       
   216       project   => $name ,
       
   217       enable    => $enable ,
       
   218     }
       
   219   }
       
   220 
       
   221   if ($disable_services != '') {
       
   222     puppi::deploy { "${name}-Disable_extra_services":
       
   223       priority  => '36' ,
       
   224       command   => 'service.sh' ,
       
   225       arguments => "stop ${disable_services}" ,
       
   226       user      => 'root',
       
   227       project   => $name ,
       
   228       enable    => $enable ,
       
   229     }
       
   230   }
       
   231 
       
   232   if ($predeploy_customcommand != '') {
       
   233     puppi::deploy { "${name}-Run_Custom_PreDeploy_Script":
       
   234       priority  => $predeploy_priority ,
       
   235       command   => 'execute.sh' ,
       
   236       arguments => $predeploy_customcommand ,
       
   237       user      => $predeploy_real_user ,
       
   238       project   => $name ,
       
   239       enable    => $enable ,
       
   240     }
       
   241   }
       
   242 
       
   243     # Here is done the deploy on $deploy_root
       
   244     puppi::deploy { "${name}-Deploy":
       
   245       priority  => '40' ,
       
   246       command   => 'deploy_files.sh' ,
       
   247       arguments => "-d ${deploy_root} -c ${bool_clean_deploy}",
       
   248       user      => $user ,
       
   249       project   => $name ,
       
   250       enable    => $enable ,
       
   251     }
       
   252 
       
   253   if ($postdeploy_customcommand != '') {
       
   254     puppi::deploy { "${name}-Run_Custom_PostDeploy_Script":
       
   255       priority  => $postdeploy_priority ,
       
   256       command   => 'execute.sh' ,
       
   257       arguments => $postdeploy_customcommand ,
       
   258       user      => $postdeploy_real_user ,
       
   259       project   => $name ,
       
   260       enable    => $enable ,
       
   261     }
       
   262   }
       
   263 
       
   264   if ($disable_services != '') {
       
   265     puppi::deploy { "${name}-Enable_extra_services":
       
   266       priority  => '44' ,
       
   267       command   => 'service.sh' ,
       
   268       arguments => "start ${disable_services}" ,
       
   269       user      => 'root',
       
   270       project   => $name ,
       
   271       enable    => $enable ,
       
   272     }
       
   273   }
       
   274 
       
   275   if ($firewall_src_ip != '') {
       
   276     puppi::deploy { "${name}-Load_Balancer_Unblock":
       
   277       priority  => '46' ,
       
   278       command   => 'firewall.sh' ,
       
   279       arguments => "${firewall_src_ip} ${firewall_dst_port} off 0" ,
       
   280       user      => 'root',
       
   281       project   => $name ,
       
   282       enable    => $enable ,
       
   283     }
       
   284   }
       
   285 
       
   286   if ($bool_run_checks == true) {
       
   287     puppi::deploy { "${name}-Run_POST-Checks":
       
   288       priority  => '80' ,
       
   289       command   => 'check_project.sh' ,
       
   290       arguments => $name ,
       
   291       user      => 'root' ,
       
   292       project   => $name ,
       
   293       enable    => $enable ,
       
   294     }
       
   295   }
       
   296 
       
   297 
       
   298 ### ROLLBACK PROCEDURE
       
   299 
       
   300   if ($bool_backup_enable == true) {
       
   301     if ($firewall_src_ip != '') {
       
   302       puppi::rollback { "${name}-Load_Balancer_Block":
       
   303         priority  => '25' ,
       
   304         command   => 'firewall.sh' ,
       
   305         arguments => "${firewall_src_ip} ${firewall_dst_port} on ${firewall_delay}" ,
       
   306         user      => 'root',
       
   307         project   => $name ,
       
   308         enable    => $enable ,
       
   309       }
       
   310     }
       
   311 
       
   312     if ($disable_services != '') {
       
   313       puppi::rollback { "${name}-Disable_extra_services":
       
   314         priority  => '37' ,
       
   315         command   => 'service.sh' ,
       
   316         arguments => "stop ${disable_services}" ,
       
   317         user      => 'root',
       
   318         project   => $name ,
       
   319         enable    => $enable ,
       
   320       }
       
   321     }
       
   322 
       
   323     if ($predeploy_customcommand != '') {
       
   324       puppi::rollback { "${name}-Run_Custom_PreDeploy_Script":
       
   325         priority  => $predeploy_priority ,
       
   326         command   => 'execute.sh' ,
       
   327         arguments => $predeploy_customcommand ,
       
   328         user      => $predeploy_real_user ,
       
   329         project   => $name ,
       
   330         enable    => $enable ,
       
   331       }
       
   332     }
       
   333 
       
   334     puppi::rollback { "${name}-Recover_Files_To_Deploy":
       
   335       priority  => '40' ,
       
   336       command   => 'archive.sh' ,
       
   337       arguments => "-r ${deploy_root} -o '${backup_rsync_options}'" ,
       
   338       user      => $user ,
       
   339       project   => $name ,
       
   340       enable    => $enable ,
       
   341     }
       
   342 
       
   343     if ($postdeploy_customcommand != '') {
       
   344       puppi::rollback { "${name}-Run_Custom_PostDeploy_Script":
       
   345         priority  => $postdeploy_priority ,
       
   346         command   => 'execute.sh' ,
       
   347         arguments => $postdeploy_customcommand ,
       
   348         user      => $postdeploy_real_user ,
       
   349         project   => $name ,
       
   350         enable    => $enable ,
       
   351       }
       
   352     }
       
   353 
       
   354     if ($disable_services != '') {
       
   355       puppi::rollback { "${name}-Enable_extra_services":
       
   356         priority  => '44' ,
       
   357         command   => 'service.sh' ,
       
   358         arguments => "start ${disable_services}" ,
       
   359         user      => 'root',
       
   360         project   => $name ,
       
   361         enable    => $enable ,
       
   362       }
       
   363     }
       
   364 
       
   365     if ($firewall_src_ip != '') {
       
   366       puppi::rollback { "${name}-Load_Balancer_Unblock":
       
   367         priority  => '46' ,
       
   368         command   => 'firewall.sh' ,
       
   369         arguments => "${firewall_src_ip} ${firewall_dst_port} off 0" ,
       
   370         user      => 'root',
       
   371         project   => $name ,
       
   372         enable    => $enable ,
       
   373       }
       
   374     }
       
   375 
       
   376     if ($bool_run_checks == true) {
       
   377       puppi::rollback { "${name}-Run_POST-Checks":
       
   378         priority  => '80' ,
       
   379         command   => 'check_project.sh' ,
       
   380         arguments => $name ,
       
   381         user      => 'root' ,
       
   382         project   => $name ,
       
   383         enable    => $enable ,
       
   384       }
       
   385     }
       
   386   }
       
   387 
       
   388 ### REPORTING
       
   389 
       
   390   if ($report_email != '') {
       
   391     puppi::report { "${name}-Mail_Notification":
       
   392       priority  => '20' ,
       
   393       command   => 'report_mail.sh' ,
       
   394       arguments => $report_email ,
       
   395       user      => 'root',
       
   396       project   => $name ,
       
   397       enable    => $enable ,
       
   398     }
       
   399   }
       
   400 
       
   401 ### AUTO DEPLOY DURING PUPPET RUN
       
   402   if ($bool_auto_deploy == true) {
       
   403     puppi::run { $name: }
       
   404   }
       
   405 
       
   406 }