add nanliu/staging to 3rdparty
[mirror/dsa-puppet.git] / 3rdparty / modules / staging / manifests / extract.pp
1 # Define resource to extract files from staging directories to target directories.
2 define staging::extract (
3   $target,              #: the target extraction directory
4   $source      = undef, #: the source compression file, supports tar, tar.gz, zip, war
5   $creates     = undef, #: the file created after extraction. if unspecified defaults ${staging::path}/${caller_module_name}/${name} ${target}/${name}
6   $unless      = undef, #: alternative way to conditionally check whether to extract file.
7   $onlyif      = undef, #: alternative way to conditionally check whether to extract file.
8   $user        = undef, #: extract file as this user.
9   $group       = undef, #:  extract file as this group.
10   $environment = undef, #: environment variables.
11   $strip       = undef, #: extract file with the --strip=X option. Only works with GNU tar.
12   $subdir      = $caller_module_name #: subdir per module in staging directory.
13 ) {
14
15   include staging
16
17   if $source {
18     $source_path = $source
19   } else {
20     $source_path = "${staging::path}/${subdir}/${name}"
21   }
22
23   # Use user supplied creates path, set default value if creates, unless or
24   # onlyif is not supplied.
25   if $creates {
26     $creates_path = $creates
27   } elsif ! ($unless or $onlyif) {
28     if $name =~ /.tar.gz$/ {
29       $folder       = staging_parse($name, 'basename', '.tar.gz')
30     } elsif $name =~ /.tar.bz2$/ {
31       $folder       = staging_parse($name, 'basename', '.tar.bz2')
32     } else {
33       $folder       = staging_parse($name, 'basename')
34     }
35     $creates_path = "${target}/${folder}"
36   } else {
37     $creates_path = undef
38   }
39
40   if scope_defaults('Exec','path') {
41     Exec{
42       cwd         => $target,
43       user        => $user,
44       group       => $group,
45       environment => $environment,
46       creates     => $creates_path,
47       unless      => $unless,
48       onlyif      => $onlyif,
49       logoutput   => on_failure,
50     }
51   } else {
52     Exec{
53       path        => $::path,
54       cwd         => $target,
55       user        => $user,
56       group       => $group,
57       environment => $environment,
58       creates     => $creates_path,
59       unless      => $unless,
60       onlyif      => $onlyif,
61       logoutput   => on_failure,
62     }
63   }
64
65   if $strip {
66     if $::osfamily == 'Solaris' or $name !~ /(.tar|.tgz|.tar.gz|.tar.bz2)$/ {
67       warning('strip is only supported with GNU tar, ignoring the parameter')
68       $strip_opt = ''
69     } else {
70       $strip_opt = " --strip=${strip}"
71     }
72   } else {
73     $strip_opt = ''
74   }
75
76   case $name {
77     /.tar$/: {
78       $command = "tar xf ${source_path}${strip_opt}"
79     }
80
81     /(.tgz|.tar.gz)$/: {
82       if $::osfamily == 'Solaris' {
83         $command = "gunzip -dc < ${source_path} | tar xf - "
84       } else {
85         $command = "tar xzf ${source_path}${strip_opt}"
86       }
87     }
88
89     /.tar.bz2$/: {
90       $command = "tar xjf ${source_path}${strip_opt}"
91     }
92
93     /.zip$/: {
94       $command = "unzip ${source_path}"
95     }
96
97     /(.war|.jar)$/: {
98       $command = "jar xf ${source_path}"
99     }
100
101     default: {
102       fail("staging::extract: unsupported file format ${name}.")
103     }
104   }
105
106   exec { "extract ${name}":
107     command => $command,
108   }
109 }