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