dev/provisioning/modules/puppi/manifests/netinstall.pp
changeset 28 b0b56e0f8c7f
equal deleted inserted replaced
27:a2342f26c9de 28:b0b56e0f8c7f
       
     1 # Define: puppi::netinstall
       
     2 #
       
     3 # This defines simplifies the installation of a file
       
     4 # downloaded from the web. It provides arguments to manage
       
     5 # different kind of downloads and custom commands.
       
     6 # It's used, among the others, by NextGen modules of webapps
       
     7 # when the argument install is set to => source
       
     8 #
       
     9 # == Variables
       
    10 #
       
    11 # [*url*]
       
    12 #   The Url of the file to retrieve. Required.
       
    13 #   Example: http://www.example42.com/file.tar.gz
       
    14 #
       
    15 # [*destination_dir*]
       
    16 #   The final destination where to unpack or copy what has been
       
    17 #   downloaded. Required.
       
    18 #   Example: /var/www/html
       
    19 #
       
    20 # [*retrieve_args*]
       
    21 #   A string of arguments to pass to wget.
       
    22 #
       
    23 # [*extracted_dir*]
       
    24 #   The name of a directory or file created after the extraction
       
    25 #   Needed only if its name is different from the downloaded file name
       
    26 #   (without suffixes). Optional.
       
    27 #
       
    28 # [*owner*]
       
    29 #   The user owner of the directory / file created. Default: root
       
    30 #
       
    31 # [*group*]
       
    32 #   The group owner of the directory / file created. Default: root
       
    33 #
       
    34 # [*timeout*]
       
    35 #   The timeout in seconds for each command executed
       
    36 #
       
    37 # [*work_dir*]
       
    38 #   A temporary work dir where file is downloaded. Default: /var/tmp
       
    39 #
       
    40 # [*path*]
       
    41 #  Define the path for the exec commands.
       
    42 #  Default: /bin:/sbin:/usr/bin:/usr/sbin
       
    43 #
       
    44 # [*exec_env*]
       
    45 #   Define any additional environment variables to be used with the
       
    46 #   exec commands. Note that if you use this to set PATH, it will
       
    47 #   override the path attribute. Multiple environment variables
       
    48 #   should be specified as an array.
       
    49 #
       
    50 # [*extract_command*]
       
    51 #   The command used to extract the downloaded file.
       
    52 #   By default is autocalculated accoring to the file extension
       
    53 #   Set 'rsync' if the file has to be placed in the destination_dir
       
    54 #   as is (for example for war files)
       
    55 #
       
    56 # [*preextract_command*]
       
    57 #   An optional custom command to run before extracting the file.
       
    58 #
       
    59 # [*postextract_command*]
       
    60 #   An optional custom command to run after having extracted the file.
       
    61 #
       
    62 define puppi::netinstall (
       
    63   $url,
       
    64   $destination_dir,
       
    65   $extracted_dir       = '',
       
    66   $retrieve_command    = 'wget',
       
    67   $retrieve_args       = '',
       
    68   $owner               = 'root',
       
    69   $group               = 'root',
       
    70   $timeout             = '3600',
       
    71   $work_dir            = '/var/tmp',
       
    72   $path                = '/bin:/sbin:/usr/bin:/usr/sbin',
       
    73   $extract_command     = '',
       
    74   $preextract_command  = '',
       
    75   $postextract_command = '',
       
    76   $postextract_cwd     = '',
       
    77   $exec_env            = []
       
    78   ) {
       
    79 
       
    80   $source_filename = url_parse($url,'filename')
       
    81   $source_filetype = url_parse($url,'filetype')
       
    82   $source_dirname = url_parse($url,'filedir')
       
    83 
       
    84   $real_extract_command = $extract_command ? {
       
    85     ''      => $source_filetype ? {
       
    86       '.tgz'     => 'tar -zxf',
       
    87       '.gz'      => 'tar -zxf',
       
    88       '.bz2'     => 'tar -jxf',
       
    89       '.tar'     => 'tar -xf',
       
    90       '.zip'     => 'unzip',
       
    91       default    => 'tar -zxf',
       
    92     },
       
    93     default => $extract_command,
       
    94   }
       
    95 
       
    96   $extract_command_second_arg = $real_extract_command ? {
       
    97     /^cp.*/    => '.',
       
    98     /^rsync.*/ => '.',
       
    99     default    => '',
       
   100   }
       
   101 
       
   102   $real_extracted_dir = $extracted_dir ? {
       
   103     ''      => $real_extract_command ? {
       
   104       /(^cp.*|^rsync.*)/         => $source_filename,
       
   105       /(^tar -zxf*|^tar -jxf*)/  => regsubst($source_dirname,'.tar',''),
       
   106       default                    => $source_dirname,
       
   107     },
       
   108     default => $extracted_dir,
       
   109   }
       
   110 
       
   111   $real_postextract_cwd = $postextract_cwd ? {
       
   112     ''      => "${destination_dir}/${real_extracted_dir}",
       
   113     default => $postextract_cwd,
       
   114   }
       
   115 
       
   116   if $preextract_command {
       
   117     exec { "PreExtract ${source_filename} in ${destination_dir} - ${title}":
       
   118       command     => $preextract_command,
       
   119       subscribe   => Exec["Retrieve ${url} in ${work_dir} - ${title}"],
       
   120       refreshonly => true,
       
   121       path        => $path,
       
   122       environment => $exec_env,
       
   123       timeout     => $timeout,
       
   124     }
       
   125   }
       
   126 
       
   127   exec { "Retrieve ${url} in ${work_dir} - ${title}":
       
   128     cwd         => $work_dir,
       
   129     command     => "${retrieve_command} ${retrieve_args} ${url}",
       
   130     creates     => "${work_dir}/${source_filename}",
       
   131     timeout     => $timeout,
       
   132     path        => $path,
       
   133     environment => $exec_env,
       
   134   }
       
   135 
       
   136   exec { "Extract ${source_filename} from ${work_dir} - ${title}":
       
   137     command     => "mkdir -p ${destination_dir} && cd ${destination_dir} && ${real_extract_command} ${work_dir}/${source_filename} ${extract_command_second_arg}",
       
   138     unless      => "ls ${destination_dir}/${real_extracted_dir}",
       
   139     creates     => "${destination_dir}/${real_extracted_dir}",
       
   140     timeout     => $timeout,
       
   141     require     => Exec["Retrieve ${url} in ${work_dir} - ${title}"],
       
   142     path        => $path,
       
   143     environment => $exec_env,
       
   144     notify      => Exec["Chown ${source_filename} in ${destination_dir} - ${title}"],
       
   145   }
       
   146 
       
   147   exec { "Chown ${source_filename} in ${destination_dir} - ${title}":
       
   148     command     => "chown -R ${owner}:${group} ${destination_dir}/${real_extracted_dir}",
       
   149     refreshonly => true,
       
   150     timeout     => $timeout,
       
   151     require     => Exec["Extract ${source_filename} from ${work_dir} - ${title}"],
       
   152     path        => $path,
       
   153     environment => $exec_env,
       
   154   }
       
   155 
       
   156   if $postextract_command {
       
   157     exec { "PostExtract ${source_filename} in ${destination_dir} - ${title}":
       
   158       command     => $postextract_command,
       
   159       cwd         => $real_postextract_cwd,
       
   160       subscribe   => Exec["Extract ${source_filename} from ${work_dir} - ${title}"],
       
   161       refreshonly => true,
       
   162       timeout     => $timeout,
       
   163       require     => [Exec["Retrieve ${url} in ${work_dir} - ${title}"],Exec["Chown ${source_filename} in ${destination_dir} - ${title}"]],
       
   164       path        => $path,
       
   165       environment => $exec_env,
       
   166     }
       
   167   }
       
   168 }
       
   169