3a1171c731ece7cbbe975e0480a768d7c5e41460
[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 filepath. NOTE: replaces server, port, url_path parameters.
12 # * server: artifactory server name (deprecated).
13 # * port: artifactory server port (deprecated).
14 # * url_path: artifactory file path http://{server}:{port}/artifactory/{url_path} (deprecated).
15 # * owner: file owner (see archive params for defaults).
16 # * group: file group (see archive params for defaults).
17 # * mode: file mode (see archive params for defaults).
18 # * archive_path: the parent directory of local filepath.
19 # * extract: whether to extract the files (true/false).
20 # * creates: the file created when the archive is extracted (true/false).
21 # * cleanup: remove archive file after file extraction (true/false).
22 #
23 # Examples
24 # --------
25 #
26 # archive::artifactory { '/tmp/logo.png':
27 #   url   => 'https://repo.jfrog.org/artifactory/distributions/images/Artifactory_120x75.png',
28 #   owner => 'root',
29 #   group => 'root',
30 #   mode  => '0644',
31 # }
32 #
33 # $dirname = 'gradle-1.0-milestone-4-20110723151213+0300'
34 # $filename = "${dirname}-bin.zip"
35 #
36 # archive::artifactory { $filename:
37 #   archive_path => '/tmp',
38 #   url          => "http://repo.jfrog.org/artifactory/distributions/org/gradle/${filename}",
39 #   extract      => true,
40 #   extract_path => '/opt',
41 #   creates      => "/opt/${dirname}",
42 #   cleanup      => true,
43 # }
44 #
45 define archive::artifactory (
46   String                                  $path         = $name,
47   Enum['present', 'absent']               $ensure       = present,
48   Optional[Pattern[/^https?:\/\//]]       $url          = undef,
49   Optional[String]                        $server       = undef,
50   Optional[Integer]                       $port         = undef,
51   Optional[String]                        $url_path     = undef,
52   Optional[String]                        $owner        = undef,
53   Optional[String]                        $group        = undef,
54   Optional[String]                        $mode         = undef,
55   Optional[Boolean]                       $extract      = undef,
56   Optional[String]                        $extract_path = undef,
57   Optional[String]                        $creates      = undef,
58   Optional[Boolean]                       $cleanup      = undef,
59   Optional[Stdlib::Compat::Absolute_path] $archive_path = undef,
60 ) {
61
62   include ::archive::params
63
64   if $archive_path {
65     $file_path = "${archive_path}/${name}"
66   } else {
67     $file_path = $path
68   }
69
70   if $file_path !~ Stdlib::Compat::Absolute_path {
71     fail("archive::artifactory[${name}]: \$name or \$archive_path must be an absolute path!") # lint:ignore:trailing_comma
72   }
73
74   if $url {
75     $maven2_data = archive::parse_artifactory_url($url)
76     if $maven2_data and $maven2_data['folder_iteg_rev'] == 'SNAPSHOT'{
77       # 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'
78       # Only Artifactory Pro lets you download this directly but the corresponding fileinfo endpoint (where the sha1 checksum is published) doesn't exist.
79       # This means we can't use the artifactory_sha1 function
80
81       $latest_url_data = archive::artifactory_latest_url($url, $maven2_data)
82
83       $file_url = $latest_url_data['url']
84       $sha1     = $latest_url_data['sha1']
85     } else {
86       $file_url = $url
87       $sha1_url = regsubst($url, '/artifactory/', '/artifactory/api/storage/')
88       $sha1     = undef
89     }
90   } elsif $server and $port and $url_path {
91     warning('archive::artifactory attribute: server, port, url_path are deprecated')
92     $art_url = "http://${server}:${port}/artifactory"
93     $file_url = "${art_url}/${url_path}"
94     $sha1_url = "${art_url}/api/storage/${url_path}"
95     $sha1     = undef
96   } else {
97     fail('Please provide fully qualified url path for artifactory file.')
98   }
99
100   if $sha1 {
101     $_sha1 = $sha1
102   } else {
103     $_sha1 = artifactory_sha1($sha1_url)
104   }
105   archive { $file_path:
106     ensure        => $ensure,
107     path          => $file_path,
108     extract       => $extract,
109     extract_path  => $extract_path,
110     source        => $file_url,
111     checksum      => $_sha1,
112     checksum_type => 'sha1',
113     creates       => $creates,
114     cleanup       => $cleanup,
115   }
116
117   $file_owner = pick($owner, $archive::params::owner)
118   $file_group = pick($group, $archive::params::group)
119   $file_mode  = pick($mode, $archive::params::mode)
120
121   file { $file_path:
122     owner   => $file_owner,
123     group   => $file_group,
124     mode    => $file_mode,
125     require => Archive[$file_path],
126   }
127 }