Defined Type: jenkins::plugin

Defined in:
manifests/plugin.pp

Summary

Manage the state of an installed plugin

Overview

This can be used to manage the state of individual plugins. Note that it does no dependency management and that’s all up to the user. This is particularly important to remember when also purging plugins.

Parameters:

  • version (Optional[String]) (defaults to: undef)

    The version to ensure

  • config_filename (Optional[String]) (defaults to: undef)

    Name of the config file for this plugin. Note config_content must also be set.

  • config_content (Optional[String]) (defaults to: 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. config_filename must also be set.

  • update_url (Optional[String]) (defaults to: undef)
  • source (Optional[String]) (defaults to: 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.,

    example.org/myplugin.hpi

  • extension (Enum['hpi', 'jpi']) (defaults to: 'hpi')

    When no source is given, this extension is used

  • digest_string (Optional[String]) (defaults to: undef)

    An optional digest string to verify integrity. The digest_type parameter describes content of this string. It’s passed to puppet-archive to verify the downloaded plugin.

  • digest_type (String) (defaults to: 'sha1')

    This parameter describes the content of digest_string. It’s passed to puppet-archive to verify the downloaded plugin.

  • enabled (Boolean) (defaults to: true)

    Ensure whether the plugin is enabled or not. Disabled plugins are still installed.

  • pin (Boolean) (defaults to: false)

    Pin the plugin to a specific version. This prevents the updater from updating it.

  • download_options (Array[String[1]]) (defaults to: ['--http1.1'])

    Add options to Archive’s curl



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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'manifests/plugin.pp', line 51

define jenkins::plugin (
  Optional[String] $version          = undef,
  Optional[String] $config_filename  = undef,
  Optional[String] $config_content   = undef,
  Optional[String] $update_url       = undef,
  Optional[String] $source           = undef,
  Enum['hpi', 'jpi'] $extension      = 'hpi',
  Optional[String] $digest_string    = undef,
  Boolean $enabled                   = true,
  String $digest_type                = 'sha1',
  Boolean $pin                       = false,
  Array[String[1]] $download_options = ['--http1.1'],
) {
  include jenkins

  if $jenkins::manage_service {
    $notify = Class['jenkins::service']
  } else {
    $notify = undef
  }

  if $jenkins::manage_datadirs {
    $plugindir = File[$jenkins::plugin_dir]
  } else {
    $plugindir = undef
  }

  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    = '' # lint:ignore:empty_string_assignment
    $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}.${extension}",
    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 = fact('jenkins_plugins') ? {
    undef   => [],
    default => strip(split($facts['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  => $notify,
    }

    $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  => $notify,
    }

    if $digest_string {
      $checksum_verify = true
      $checksum        = $digest_string
      $checksum_type   = $digest_type
    } else {
      $checksum_verify = false
      $checksum        = undef
      $checksum_type   = undef
    }

    exec { "force ${plugin}-${version}":
      command => "/bin/rm -rf ${jenkins::plugin_dir}/${plugin}",
    }
    -> 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          => $plugindir,
      notify           => $notify,
      download_options => $download_options,
    }
    $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  => $notify,
  }

  if $config_filename {
    if $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  => file,
      content => $config_content,
      owner   => $jenkins::user,
      group   => $jenkins::group,
      mode    => '0644',
      notify  => $notify,
    }
  }
}