Jump To …

extract.pp

Define: staging::extract

Define resource to extract files from staging directories to target directories.

Parameters:

  • [target]: the target extraction directory (default: )
  • [source]: the source compression file, supports tar, tar.gz, zip, war (default: undef)
  • [creates]: the file created after extraction. if unspecified defaults ${staging::path}/${caller_module_name}/${name} ${target}/${name} (default: undef)
  • [unless]: alternative way to conditionally check whether to extract file. (default: undef)
  • [onlyif]: alternative way to conditionally check whether to extract file. (default: undef)
  • [user]: extract file as this user. (default: undef)
  • [group]: extract file as this group. (default: undef)
  • [environment]: environment variables. (default: undef)
  • [subdir]: subdir per module in staging directory. (default: $caller_module_name)

Usage:

$caller_module_name = 'demo'

class { 'staging':
  path => '/tmp/staging',
}

staging::file { 'sample.tar.gz':
  source => 'puppet:///modules/staging/sample.tar.gz'
}

staging::extract { 'sample.tar.gz':
  target  => '/tmp/staging',
  creates => '/tmp/staging/sample',
  require => Staging::File['sample.tar.gz'],
}
define staging::extract (
  $target,              
  $source      = undef, 
  $creates     = undef, 
  $unless      = undef, 
  $onlyif      = undef, 
  $user        = undef, 
  $group       = undef, 
  $environment = undef, 
  $subdir      = $caller_module_name 
) {

  include staging

  if $source {
    $source_path = $source
  } else {
    $source_path = "${staging::path}/${subdir}/${name}"
  }

Use user supplied creates path, set default value if creates, unless or onlyif is not supplied.

  if $creates {
    $creates_path = $creates
  } elsif ! ($unless or $onlyif) {
    if $name =~ /.tar.gz$/ {
      $folder       = staging_parse($name, 'basename', '.tar.gz')
      $creates_path = "${target}/${folder}"
    } else {
      $folder       = staging_parse($name, 'basename')
      $creates_path = "${target}/${folder}"
    }
  }

  if scope_defaults('Exec','path') {
    Exec{
      cwd         => $target,
      user        => $user,
      group       => $group,
      environment => $environment,
      creates     => $creates_path,
      unless      => $unless,
      onlyif      => $onlyif,
      logoutput   => on_failure,
    }
  } else {
    Exec{
      path        => $::path,
      cwd         => $target,
      user        => $user,
      group       => $group,
      environment => $environment,
      creates     => $creates_path,
      unless      => $unless,
      onlyif      => $onlyif,
      logoutput   => on_failure,
    }
  }

  case $name {
    /.tar$/: {
      $command = "tar xf ${source_path}"
    }

    /(.tgz|.tar.gz)$/: {
      if $::osfamily == 'Solaris' {
        $command = "gunzip -dc < ${source_path} | tar xf - "
      } else {
        $command = "tar xzf ${source_path}"
      }
    }

    /.zip$/: {
      $command = "unzip ${source_path}"
    }

    /.war$/: {
      $command = "jar xf ${source_path}"
    }

    default: {
      fail("staging::extract: unsupported file format ${name}.")
    }
  }

  exec { "extract ${name}":
    command => $command,
  }
}