This repository was archived by the owner on Mar 3, 2023. It is now read-only.
forked from voxpupuli/puppet-jenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.pp
216 lines (196 loc) · 6.47 KB
/
plugin.pp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#
# config_filename = undef
# Name of the config file for this plugin.
#
# config_content = undef
# Content of the config file for this plugin. It is up to the caller to
# create this content from a template or any other mean.
#
# update_url = undef
#
# source = undef
# Direct URL from which to download plugin without modification. This is
# particularly useful for development and testing of plugins which may not be
# hosted in the typical Jenkins' plugin directory structure. E.g.,
#
# https://example.org/myplugin.hpi
#
define jenkins::plugin(
$version = undef,
$manage_config = false,
$config_filename = undef,
$config_content = undef,
$update_url = undef,
$enabled = true,
$source = undef,
$digest_string = undef,
$digest_type = 'sha1',
$pin = false,
# no worky
$timeout = undef,
# deprecated
$plugin_dir = undef,
$username = undef,
$group = undef,
$create_user = undef,
) {
validate_string($version)
validate_bool($manage_config)
validate_string($config_filename)
validate_string($config_content)
validate_string($update_url)
validate_bool($enabled)
validate_string($source)
validate_string($digest_string)
validate_string($digest_type)
validate_bool($pin)
if $timeout {
warning('jenkins::plugin::timeout presently has effect')
}
if $plugin_dir {
warning('jenkins::plugin::plugin_dir is deprecated and has no effect -- see jenkins::localstatedir')
}
if $username {
warning('jenkins::plugin::username is deprecated and has no effect -- see jenkins::user')
}
if $group {
warning('jenkins::plugin::group is deprecated and has no effect -- see jenkins::group')
}
if $create_user {
warning('jenkins::plugin::create_user is deprecated and has no effect')
}
include ::jenkins
if $version {
$plugins_host = $update_url ? {
undef => $::jenkins::default_plugins_host,
default => $update_url,
}
$base_url = "${plugins_host}/download/plugins/${name}/${version}/"
# Escape +'s in $version when constructing $search.
# * We can't use single quotes for the replacement string because
# puppet 3 and puppet 4 interpret '\\' differently.
# * We can't use double quotes without a variable interpolation or
# lint complains.
$empty = ''
$escver = regsubst ($version, '\+', "${empty}\\\\+", 'G')
$search = "^${name} ${escver}$"
}
else {
$plugins_host = $update_url ? {
undef => $::jenkins::default_plugins_host,
default => $update_url,
}
$base_url = "${plugins_host}/latest/"
$search = "^${name} "
}
# if $source is specified, it overrides any other URL construction
$download_url = $source ? {
undef => "${base_url}${name}.hpi",
default => $source,
}
$plugin_ext = regsubst($download_url, '^.*\.(hpi|jpi)$', '\1')
$plugin = "${name}.${plugin_ext}"
# sanity check extension
if ! $plugin_ext {
fail("unsupported plugin extension in source url: ${download_url}")
}
$installed_plugins = $::jenkins_plugins ? {
undef => [],
default => strip(split($::jenkins_plugins, ',')),
}
# create a file resource for the download + unpacked plugin dir to prevent it
# from being recursively deleted
if $::jenkins::purge_plugins {
file { "${::jenkins::plugin_dir}/${name}": }
}
if (empty(grep($installed_plugins, $search))) {
$enabled_ensure = $enabled ? {
false => present,
default => absent,
}
# at least as of jenkins 1.651, if the version of a plugin being downloaded
# has a .hpi extension, and there is an existing version of the plugin
# present with a .jpi extension, jenkins will actually delete the .hpi
# version when restarted. Essentially making it impossible to
# (up|down)grade a plugin from .jpi -> .hpi via puppet across extension
# changes. Regardless, we should be relying on jenkins to guess which
# plugin archive to use and cleanup any conflicting extensions.
$inverse_plugin_ext = $plugin_ext ? {
'hpi' => 'jpi',
'jpi' => 'hpi',
}
$inverse_plugin = "${name}.${inverse_plugin_ext}"
file {[
"${::jenkins::plugin_dir}/${inverse_plugin}",
"${::jenkins::plugin_dir}/${inverse_plugin}.disabled",
"${::jenkins::plugin_dir}/${inverse_plugin}.pinned",
]:
ensure => absent,
before => Archive[$plugin],
}
# Allow plugins that are already installed to be enabled/disabled.
file { "${::jenkins::plugin_dir}/${plugin}.disabled":
ensure => $enabled_ensure,
owner => $::jenkins::user,
group => $::jenkins::group,
mode => '0644',
require => Archive[$plugin],
notify => Service['jenkins'],
}
$pinned_ensure = $pin ? {
true => file,
default => undef,
}
file { "${::jenkins::plugin_dir}/${plugin}.pinned":
ensure => $pinned_ensure,
owner => $::jenkins::user,
group => $::jenkins::group,
require => Archive[$plugin],
notify => Service['jenkins'],
}
if $digest_string {
$checksum_verify = true
$checksum = $digest_string
$checksum_type = $digest_type
} else {
$checksum_verify = false
$checksum = undef
$checksum_type = undef
}
archive { $plugin:
source => $download_url,
path => "${::jenkins::plugin_dir}/${plugin}",
checksum_verify => $checksum_verify,
checksum => $checksum,
checksum_type => $checksum_type,
proxy_server => $::jenkins::proxy::url,
cleanup => false,
extract => false,
require => File[$::jenkins::plugin_dir],
notify => Service['jenkins'],
}
$archive_require = Archive[$plugin]
} else {
$archive_require = undef
}
file { "${::jenkins::plugin_dir}/${plugin}" :
owner => $::jenkins::user,
group => $::jenkins::group,
mode => '0644',
require => $archive_require,
before => Service['jenkins'],
}
if $manage_config {
if $config_filename == undef or $config_content == undef {
fail 'To deploy config file for plugin, you need to specify both $config_filename and $config_content'
}
file {"${::jenkins::localstatedir}/${config_filename}":
ensure => present,
content => $config_content,
owner => $::jenkins::user,
group => $::jenkins::group,
mode => '0644',
notify => Service['jenkins'],
}
}
}