28
|
1 |
# == Definition: archive::extract |
|
2 |
# |
|
3 |
# Archive extractor. |
|
4 |
# |
|
5 |
# Parameters: |
|
6 |
# |
|
7 |
# - *$target: Destination directory |
|
8 |
# - *$src_target: Default value "/usr/src". |
|
9 |
# - *$root_dir: Default value "". |
|
10 |
# - *$extension: Default value ".tar.gz". |
|
11 |
# - *$timeout: Default value 120. |
|
12 |
# - *$strip_components: Default value 0. |
|
13 |
# - *$user: The user used to do the extraction. |
|
14 |
# |
|
15 |
# Example usage: |
|
16 |
# |
|
17 |
# archive::extract {"apache-tomcat-6.0.26": |
|
18 |
# ensure => present, |
|
19 |
# target => "/opt", |
|
20 |
# } |
|
21 |
# |
|
22 |
# This means we want to extract the local archive |
|
23 |
# (maybe downloaded with archive::download) |
|
24 |
# '/usr/src/apache-tomcat-6.0.26.tar.gz' in '/src/apache-tomcat-6.0.26' |
|
25 |
# |
|
26 |
# Warning: |
|
27 |
# |
|
28 |
# The parameter *$root_dir* must be used if the root directory of the archive |
|
29 |
# is different from the name of the archive *$name*. To extract the name of |
|
30 |
# the root directory use the commands "tar tf archive.tar.gz" or |
|
31 |
# "unzip -l archive.zip" |
|
32 |
# |
|
33 |
define archive::extract ( |
|
34 |
$target, |
|
35 |
$ensure=present, |
|
36 |
$src_target='/usr/src', |
|
37 |
$root_dir=undef, |
|
38 |
$extension='tar.gz', |
|
39 |
$timeout=120, |
|
40 |
$path=$::path, |
|
41 |
$strip_components=0, |
|
42 |
$purge=false, |
|
43 |
$user=undef, |
|
44 |
) { |
|
45 |
|
|
46 |
if $root_dir { |
|
47 |
$extract_dir = "${target}/${root_dir}" |
|
48 |
} else { |
|
49 |
$extract_dir = "${target}/${name}" |
|
50 |
} |
|
51 |
|
|
52 |
case $ensure { |
|
53 |
'present': { |
|
54 |
|
|
55 |
$extract_zip = "unzip -o ${src_target}/${name}.${extension} -d ${target}" |
|
56 |
$extract_targz = "tar --no-same-owner --no-same-permissions --strip-components=${strip_components} -xzf ${src_target}/${name}.${extension} -C ${target}" |
|
57 |
$extract_tarbz2 = "tar --no-same-owner --no-same-permissions --strip-components=${strip_components} -xjf ${src_target}/${name}.${extension} -C ${target}" |
|
58 |
$extract_tarxz = "tar --no-same-owner --no-same-permissions --strip-components=${strip_components} -xpf ${src_target}/${name}.${extension} -C ${target}" |
|
59 |
|
|
60 |
$purge_command = $purge ? { |
|
61 |
true => "rm -rf ${target} && ", |
|
62 |
default => '', |
|
63 |
} |
|
64 |
|
|
65 |
$command = $extension ? { |
|
66 |
'zip' => "${purge_command}mkdir -p ${target} && ${extract_zip}", |
|
67 |
'tar.gz' => "${purge_command}mkdir -p ${target} && ${extract_targz}", |
|
68 |
'tgz' => "${purge_command}mkdir -p ${target} && ${extract_targz}", |
|
69 |
'tar.bz2' => "${purge_command}mkdir -p ${target} && ${extract_tarbz2}", |
|
70 |
'tgz2' => "${purge_command}mkdir -p ${target} && ${extract_tarbz2}", |
|
71 |
'tar.xz' => "${purge_command}mkdir -p ${target} && ${extract_tarxz}", |
|
72 |
'txz' => "${purge_command}mkdir -p ${target} && ${extract_tarxz}", |
|
73 |
default => fail ( "Unknown extension value '${extension}'" ), |
|
74 |
} |
|
75 |
exec {"${name} unpack": |
|
76 |
command => $command, |
|
77 |
creates => $extract_dir, |
|
78 |
timeout => $timeout, |
|
79 |
user => $user, |
|
80 |
path => $path, |
|
81 |
} |
|
82 |
} |
|
83 |
'absent': { |
|
84 |
file {$extract_dir: |
|
85 |
ensure => absent, |
|
86 |
recurse => true, |
|
87 |
purge => true, |
|
88 |
force => true, |
|
89 |
} |
|
90 |
} |
|
91 |
default: { err ( "Unknown ensure value: '${ensure}'" ) } |
|
92 |
} |
|
93 |
} |