Skip to content

Commit

Permalink
Move to native systemd integration
Browse files Browse the repository at this point in the history
Jenkins 2.332 started to include a systemd unit file which means we can
greatly simplify this module. It aligns all distros on a single
standard.

No attempt is made at supporting Jenkins < 2.332 but the old override
file is cleaned up.
  • Loading branch information
ekohl committed May 3, 2022
1 parent 544e47e commit 704f6b7
Show file tree
Hide file tree
Showing 31 changed files with 107 additions and 592 deletions.
1 change: 0 additions & 1 deletion .fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ fixtures:
zypprepo: "https://github.com/voxpupuli/puppet-zypprepo.git"
archive: "https://github.com/voxpupuli/puppet-archive.git"
systemd: "https://github.com/voxpupuli/puppet-systemd"
transition: "https://github.com/puppetlabs/puppetlabs-transition"
yumrepo_core:
repo: https://github.com/puppetlabs/puppetlabs-yumrepo_core.git
puppet_version: ">= 6.0.0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ configuration are all driven through this script.
When an API-based resource is defined, the Jenkins' CLI is installed and run
against the local system (127.0.0.1). Jenkins is assumed to be listening on
port 8080, but the module is smart enough to notice if you've configured an
alternate port using jenkins::config_hash['HTTP_PORT'].
alternate port using jenkins::config_hash['JENKINS_PORT'].

Users and credentials are Puppet-managed, meaning that changes made to them
from outside Puppet will be reset at the next puppet run. In this way, you can
Expand Down
22 changes: 0 additions & 22 deletions lib/facter/jenkins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,3 @@
plugins.keys.sort.map { |plugin| "#{plugin} #{plugins[plugin][:plugin_version]}" }.join(', ')
end
end

Facter.add(:jenkins_version) do
confine kernel: 'Linux'

setcode do
libdir = case Facter.value('os.family')
when 'Debian'
'/usr/share/jenkins'
when 'Archlinux'
'/usr/share/java/jenkins'
else
'/usr/lib/jenkins'
end
war = libdir + '/jenkins.war'

if Facter::Util::Resolution.which('java') and File.exist?(war)
Facter::Util::Resolution.exec(
'java -jar %s --version' % [war]
)
end
end
end
8 changes: 1 addition & 7 deletions lib/puppet/parser/functions/jenkins_port.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ module Puppet::Parser::Functions
ENDHEREDOC

config_hash = lookupvar('::jenkins::config_hash')
if config_hash && \
config_hash['HTTP_PORT'] && \
config_hash['HTTP_PORT']['value']
return config_hash['HTTP_PORT']['value']
else
return 8080
end
config_hash&.dig('JENKINS_PORT', 'value') || 8080
end
end
8 changes: 1 addition & 7 deletions lib/puppet/parser/functions/jenkins_prefix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ module Puppet::Parser::Functions
ENDHEREDOC

config_hash = lookupvar('::jenkins::config_hash')
if config_hash && \
config_hash['PREFIX'] && \
config_hash['PREFIX']['value']
return config_hash['PREFIX']['value']
else
return ''
end
config_hash&.dig('PREFIX', 'value') || ''
end
end
4 changes: 2 additions & 2 deletions lib/puppet/x/jenkins/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class Puppet::X::Jenkins::Config
class UnknownConfig < ArgumentError; end

DEFAULTS = {
cli_jar: '/usr/lib/jenkins/jenkins-cli.jar',
cli_jar: '/usr/share/java/jenkins-cli.jar',
url: 'http://localhost:8080',
ssh_private_key: nil,
puppet_helper: '/usr/lib/jenkins/puppet_helper.groovy',
puppet_helper: '/usr/share/java/puppet_helper.groovy',
cli_tries: 30,
cli_try_sleep: 2,
cli_username: nil,
Expand Down
6 changes: 5 additions & 1 deletion manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
$jenkins::params::config_hash_defaults,
$jenkins::config_hash
)
create_resources('jenkins::sysconfig', $config_hash)

systemd::dropin_file { 'puppet-overrides.conf':
unit => 'jenkins.service',
content => epp("${module_name}/jenkins-override.epp", { 'environment' => $config_hash }),
}
}
52 changes: 19 additions & 33 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
# This class manages the [Jenkins CI/CD service](https://jenkins.io/index.html).
#
# Note that if different jenkins listening port(s) are configured via
# ``jenkins::port``, ``jenkins::config_hash`` and/or a ``jenkins::sysconf``
# resource, "bad things" are likely to happen. This is a known implementation
# problem with this module that can not be fixed without breaking backwards
# compatibility.
# ``jenkins::port`` and ``jenkins::config_hash`` resource, "bad things" are
# likely to happen. This is a known implementation problem with this module
# that can not be fixed without breaking backwards compatibility.
#
# @param version
# package to install
Expand Down Expand Up @@ -79,8 +78,7 @@
# @example Bulk sysconf
# class{ 'jenkins':
# config_hash => {
# 'HTTP_PORT' => { 'value' => '9090' },
# 'AJP_PORT' => { 'value' => '9009' },
# 'JENKINS_PORT' => { 'value' => '9090' },
# }
# }
#
Expand Down Expand Up @@ -205,16 +203,6 @@
# @param libdir
# Path to jenkins core files
#
# * Redhat: ``/usr/lib/jenkins``
# * Debian: ``/usr/share/jenkins``
#
# @param sysconfdir
# Controls the path to the "sysconfig" file that stores jenkins service
# start-up variables
#
# * RedHat: ``/etc/sysconfig/jenkins``
# * Debian: ``/etc/default/jenkins``
#
# @param manage_datadirs
# manage the local state dir, plugins dir and jobs dir
#
Expand Down Expand Up @@ -282,9 +270,6 @@
# version: '2.16.0'
# # /support-core deps
#
# @param systemd_type
# Define a systemd unit type
#
class jenkins (
String $version = 'installed',
Boolean $lts = true,
Expand All @@ -296,7 +281,7 @@
Boolean $manage_service = true,
Boolean $service_enable = true,
Enum['running', 'stopped'] $service_ensure = 'running',
Optional[String] $service_provider = $jenkins::params::service_provider,
Optional[String] $service_provider = undef,
Hash $config_hash = {},
Hash $plugin_hash = {},
Hash $job_hash = {},
Expand All @@ -315,8 +300,7 @@
Integer $cli_tries = 10,
Integer $cli_try_sleep = 10,
Integer $port = 8080,
Stdlib::Absolutepath $libdir = $jenkins::params::libdir,
Stdlib::Absolutepath $sysconfdir = $jenkins::params::sysconfdir,
Stdlib::Absolutepath $libdir = '/usr/share/java',
Boolean $manage_datadirs = true,
Stdlib::Absolutepath $localstatedir = '/var/lib/jenkins',
Optional[Integer] $executors = undef,
Expand All @@ -328,7 +312,6 @@
Variant[Array[String], Hash[String, Hash]] $default_plugins = $jenkins::params::default_plugins,
String $default_plugins_host = 'https://updates.jenkins.io',
Boolean $purge_plugins = false,
Enum['simple', 'forking'] $systemd_type = $jenkins::params::systemd_type,
) inherits jenkins::params {
if $purge_plugins and ! $manage_datadirs {
warning('jenkins::purge_plugins has no effect unless jenkins::manage_datadirs is true')
Expand Down Expand Up @@ -394,17 +377,20 @@
notice(sprintf('INFO: make sure you install the following plugins with your code using this module: %s',join($jenkins::params::default_plugins,','))) # lint:ignore:140chars
}

if $service_provider == 'systemd' {
jenkins::systemd { 'jenkins':
user => $user,
libdir => $libdir,
}
# puppet/jenkins used to implement systemd but Jenkins 2.332 moved to
# systemd and implements this natively. Clean up the old implementation.
$old_libdir = $facts['os']['family'] ? {
'Archlinux' => '/usr/share/java/jenkins/',
'Debian' => '/usr/share/jenkins',
default => '/usr/lib/jenkins',
}
file { "${old_libdir}/jenkins-run":
ensure => absent,
}

# jenkins::config manages the jenkins user resource, which is autorequired
# by the file resource for the run wrapper.
Class['jenkins::config']
-> Jenkins::Systemd['jenkins']
-> Anchor['jenkins::end']
file { '/etc/systemd/system/jenkins.service':
ensure => absent,
notify => Service['jenkins'],
}
}

Expand Down
53 changes: 3 additions & 50 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
# @api private
class jenkins::params {
$swarm_version = '2.2'
$_java_args = '-Djava.awt.headless=true -Djenkins.install.runSetupWizard=false'
$config_hash_defaults = {
'JAVA_OPTS' => { value => '-Djava.awt.headless=true -Djenkins.install.runSetupWizard=false' },
}
$default_plugins = {
# Pin to 1074 now - not pretty, but latest is incompatible with LTS now
'credentials' => { 'version' => '1074.v60e6c29b_b_44b_' }, # required by puppet_helper.groovy
Expand All @@ -12,71 +14,22 @@
'structs' => {}, # required by credentials plugin
}

if versioncmp(pick($facts['jenkins_version'], '2.313'), '2.313') >= 0 {
$systemd_type = 'simple'
} else {
$systemd_type = 'forking'
}

case $facts['os']['family'] {
'Debian': {
$repo = true
$libdir = '/usr/share/jenkins'
$package_provider = 'dpkg'
$service_provider = undef
$sysconfdir = '/etc/default'
$config_hash_defaults = {
'JAVA_ARGS' => { value => $_java_args },
'AJP_PORT' => { value => '-1' },
}
}
'RedHat': {
$repo = true
$libdir = '/usr/lib/jenkins'
$package_provider = 'rpm'
$sysconfdir = '/etc/sysconfig'
$config_hash_defaults = {
'JENKINS_JAVA_OPTIONS' => { value => $_java_args },
'JENKINS_AJP_PORT' => { value => '-1' },
}

# explicitly use systemd if it is available
# XXX only enable explicit systemd support on RedHat at this time due to
# the Debian packaging using variable interpolation in
# /etc/default/jenkins.
# XXX this param exists because of a historical work around to PUP-5353
# it is part of the public interface to ::jenkins; it needs to be
# maintained until at least a major version bump. It has been somewhat
# repurposed as a flag for specific systemd support.
if $facts['systemd'] {
$service_provider = 'systemd'
} else {
$service_provider = undef
}
}
'Archlinux': {
$repo = false
$libdir = '/usr/share/java/jenkins/'
$package_provider = 'pacman'
$service_provider = undef
$sysconfdir = '/etc/conf.d'
$config_hash_defaults = {
# Archlinux's jenkins package uses it's own variables
# which are not compatible with these.
#'JENKINS_JAVA_OPTIONS' => { value => $_java_args },
#'JENKINS_AJP_PORT' => { value => '-1' },
}
}
default: {
$repo = true
$libdir = '/usr/lib/jenkins'
$package_provider = undef
$service_provider = undef
$sysconfdir = '/etc/sysconfig'
$config_hash_defaults = {
'JENKINS_JAVA_OPTIONS' => { value => $_java_args },
'JENKINS_AJP_PORT' => { value => '-1' },
}
}
}
}
12 changes: 6 additions & 6 deletions manifests/slave.pp
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,17 @@

# customizations based on the OS family
case $facts['os']['family'] {
'Debian': {
$defaults_location = $jenkins::params::sysconfdir

ensure_packages(['daemon'])
Package['daemon'] -> Service['jenkins-slave']
'Archlinux': {
$defaults_location = '/etc/conf.d'
}
'Darwin': {
$defaults_location = $slave_home
}
'Debian': {
$defaults_location = '/etc/default'
}
default: {
$defaults_location = $jenkins::params::sysconfdir
$defaults_location = '/etc/sysconfig'
}
}

Expand Down
21 changes: 0 additions & 21 deletions manifests/sysconfig.pp

This file was deleted.

54 changes: 0 additions & 54 deletions manifests/systemd.pp

This file was deleted.

4 changes: 0 additions & 4 deletions metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@
{
"name": "puppet/systemd",
"version_requirement": ">= 3.1.0 < 4.0.0"
},
{
"name": "puppetlabs/transition",
"version_requirement": ">= 0.1.0 < 1.0.0"
}
],
"requirements": [
Expand Down
Loading

0 comments on commit 704f6b7

Please sign in to comment.