Add puppet/archive module
[mirror/dsa-puppet.git] / 3rdparty / modules / archive / manifests / artifactory.pp
1 # Define: archive::artifactory
2 # ============================
3 #
4 # archive wrapper for downloading files from artifactory
5 #
6 # Parameters
7 # ----------
8 #
9 # * path: fully qualified filepath for the download the file or use archive_path and only supply filename. (namevar).
10 # * ensure: ensure the file is present/absent.
11 # * url: artifactory download URL.
12 # * owner: file owner (see archive params for defaults).
13 # * group: file group (see archive params for defaults).
14 # * mode: file mode (see archive params for defaults).
15 # * archive_path: the parent directory of local filepath.
16 # * extract: whether to extract the files (true/false).
17 # * creates: the file created when the archive is extracted (true/false).
18 # * cleanup: remove archive file after file extraction (true/false).
19 #
20 # Examples
21 # --------
22 #
23 # archive::artifactory { '/tmp/logo.png':
24 #   url   => 'https://repo.jfrog.org/artifactory/distributions/images/Artifactory_120x75.png',
25 #   owner => 'root',
26 #   group => 'root',
27 #   mode  => '0644',
28 # }
29 #
30 # $dirname = 'gradle-1.0-milestone-4-20110723151213+0300'
31 # $filename = "${dirname}-bin.zip"
32 #
33 # archive::artifactory { $filename:
34 #   archive_path => '/tmp',
35 #   url          => "http://repo.jfrog.org/artifactory/distributions/org/gradle/${filename}",
36 #   extract      => true,
37 #   extract_path => '/opt',
38 #   creates      => "/opt/${dirname}",
39 #   cleanup      => true,
40 # }
41 #
42 define archive::artifactory (
43   Stdlib::HTTPUrl                $url,
44   String                         $path         = $name,
45   Enum['present', 'absent']      $ensure       = present,
46   Optional[String]               $owner        = undef,
47   Optional[String]               $group        = undef,
48   Optional[String]               $mode         = undef,
49   Optional[Boolean]              $extract      = undef,
50   Optional[String]               $extract_path = undef,
51   Optional[String]               $creates      = undef,
52   Optional[Boolean]              $cleanup      = undef,
53   Optional[Stdlib::Absolutepath] $archive_path = undef,
54 ) {
55
56   include ::archive::params
57
58   if $archive_path {
59     $file_path = "${archive_path}/${name}"
60   } else {
61     $file_path = $path
62   }
63
64   assert_type(Stdlib::Absolutepath, $file_path) |$expected, $actual| {
65     fail("archive::artifactory[${name}]: \$name or \$archive_path must be '${expected}', not '${actual}'")
66   }
67
68   $maven2_data = archive::parse_artifactory_url($url)
69   if $maven2_data and $maven2_data['folder_iteg_rev'] == 'SNAPSHOT'{
70     # URL represents a SNAPSHOT version. eg 'http://artifactory.example.com/artifactory/repo/com/example/artifact/0.0.1-SNAPSHOT/artifact-0.0.1-SNAPSHOT.zip'
71     # Only Artifactory Pro lets you download this directly but the corresponding fileinfo endpoint (where the sha1 checksum is published) doesn't exist.
72     # This means we can't use the artifactory_sha1 function
73
74     $latest_url_data = archive::artifactory_latest_url($url, $maven2_data)
75
76     $file_url = $latest_url_data['url']
77     $sha1     = $latest_url_data['sha1']
78   } else {
79     $file_url = $url
80     $sha1     = archive::artifactory_checksum($url,'sha1')
81   }
82
83   archive { $file_path:
84     ensure        => $ensure,
85     path          => $file_path,
86     extract       => $extract,
87     extract_path  => $extract_path,
88     source        => $file_url,
89     checksum      => $sha1,
90     checksum_type => 'sha1',
91     creates       => $creates,
92     cleanup       => $cleanup,
93   }
94
95   $file_owner = pick($owner, $archive::params::owner)
96   $file_group = pick($group, $archive::params::group)
97   $file_mode  = pick($mode, $archive::params::mode)
98
99   file { $file_path:
100     owner   => $file_owner,
101     group   => $file_group,
102     mode    => $file_mode,
103     require => Archive[$file_path],
104   }
105 }