Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/puppet helper num executors #260

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ puppet module install rtyler/jenkins
```
Then the service should be running at [http://hostname.example.com:8080/](http://hostname.example.com:8080/).

### Jenkin's options

#### Master Executor Threads

```puppet
class { 'jenkins':
executors => 0,
}
```

### Managing Jenkins jobs


Expand Down Expand Up @@ -127,9 +137,11 @@ the following `require` statement:
2. Config Hash - jennkins::config
3. Configure Firewall - jenkins (init.pp)
4. Outbound Jenkins Proxy Config - jenkins (init.pp)
5. Jenkins Users
6. Credentials
7. Simple security model configuration
5. [CLI Helper](#cli-helper)
* [`exec_cli_helper`](#exec_cli_helper)
6. Jenkins Users
7. Credentials
8. Simple security model configuration

### API-based Resources and Settings (Users, Credentials, security)

Expand Down Expand Up @@ -191,6 +203,46 @@ security policy are configured in the correct order. For example:

touch $DONEFILE

#### `jenkins::cli::exec`

The defined type `jenkins::cli::exec` may be used to execute arbitrary CLI helper
commands.

Arguments to the CLI helper script may be specified as the resource's title.

```puppet
jenkins::cli::exec { 'set_num_executors 0': }
```

Or passed as an array to the `command` parameter. This example is
semantically equivalent to the first.

```puppet
jenkins::cli::exec { 'set_num_executors 0':
command => ['set_num_executors', '0'],
}
```

which is also equivalent to:

```puppet
jenkins::cli::exec { 'set_num_executors 0':
command => 'set_num_executors 0',
}
```

If the `unless` parameter is specified, an environment variable named
`$HELPER_CMD` is declared which contains the complete string needed to execute
the CLI helper script (minus arguments). This may be useful in constructing
idempotent `exec` statements.

```puppet
$num_executors = 0
jenkins::cli::exec { "set_num_executors ${num_executors}":
unless => "[ \$(\$HELPER_CMD get_num_executors) -eq ${num_executors} ]"
}
```

#### Users

Email and password are required.
Expand Down
24 changes: 24 additions & 0 deletions files/puppet_helper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,30 @@ class Actions {
instance.setAuthorizationStrategy(strategy)
instance.setSecurityRealm(realm)
}

////////////////////////
// get_num_executors
////////////////////////
/*
* Print the number of executors for the master
*/
void get_num_executors() {
def j = Jenkins.getInstance()
def n = j.getNumExecutors()
out.println(n)
}

////////////////////////
// set_num_executors
////////////////////////
/*
* Set the number of executors for the master
*/
void set_num_executors(String n) {
def j = Jenkins.getInstance()
j.setNumExecutors(n.toInteger())
j.save()
}
} // class Actions

///////////////////////////////////////////////////////////////////////////////
Expand Down
10 changes: 0 additions & 10 deletions manifests/cli.pp
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,6 @@
# The jenkins cli command with required parameter(s)
$cmd = "java -jar ${jar} -s http://localhost:${port}"

# Reload all Jenkins config from disk (only when notified)
exec { 'reload-jenkins':
command => "${cmd} reload-configuration",
path => ['/bin', '/usr/bin'],
tries => 10,
try_sleep => 2,
refreshonly => true,
require => File[$jar],
}

# Do a safe restart of Jenkins (only when notified)
exec { 'safe-restart-jenkins':
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why this has been removed, I presume it's because it is now redundant, but removing it does break API compatibility

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an explanation in the commit message: jhoblitt@06c50ce

command => "${cmd} safe-restart && /bin/sleep 10",
Expand Down
45 changes: 45 additions & 0 deletions manifests/cli/exec.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# == Define: jenkins::cli::exec
#
# A defined type for executing custom helper script commands via the Jenkins
# CLI.
#
define jenkins::cli::exec(
$command = $title,
$unless = undef,
) {
if !(is_string($command) or is_array($command)) {
fail('$command is not a string or an Array.')
}
validate_string($unless)

include ::jenkins
include ::jenkins::cli_helper
include ::jenkins::cli::reload

# $command may be either a string or an array due to the use of flatten()
$run = join(
delete_undef_values(
flatten([
$::jenkins::cli_helper::helper_cmd,
$command
])
),
' '
)

if $unless {
$environment_run = [ "HELPER_CMD=${::jenkins::cli_helper::helper_cmd}" ]
} else {
$environment_run = undef
}

exec { $title:
provider => 'shell',
command => $run,
environment => $environment_run,
unless => $unless,
tries => $::jenkins::cli_tries,
try_sleep => $::jenkins::cli_try_sleep,
notify => Class['jenkins::cli::reload'],
}
}
19 changes: 19 additions & 0 deletions manifests/cli/reload.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Class: jenkins::cli::reload
#
# Command Jenkins to reload config.xml via the CLI.
#
class jenkins::cli::reload {

if $caller_module_name != $module_name {
fail("Use of private class ${name} by ${caller_module_name}")
}

# Reload all Jenkins config from disk (only when notified)
exec { 'reload-jenkins':
command => "${::jenkins::cli::cmd} reload-configuration",
path => ['/bin', '/usr/bin'],
tries => 10,
try_sleep => 2,
refreshonly => true,
}
}
25 changes: 23 additions & 2 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
# config_hash = undef (Default)
# Hash with config options to set in sysconfig/jenkins defaults/jenkins
#
# executors = undef (Default)
# Integer number of executors on the Jenkin's master.
#
# Example use
#
# class{ 'jenkins':
Expand Down Expand Up @@ -140,6 +143,7 @@
$cli_try_sleep = $jenkins::params::cli_try_sleep,
$port = $jenkins::params::port,
$libdir = $jenkins::params::libdir,
$executors = undef,
) inherits jenkins::params {

validate_bool($lts, $install_java, $repo)
Expand All @@ -153,6 +157,10 @@
validate_array($no_proxy_list)
}

if $executors {
validate_integer($executors)
}

anchor {'jenkins::begin':}
anchor {'jenkins::end':}

Expand Down Expand Up @@ -199,6 +207,18 @@

if $cli {
include jenkins::cli
include jenkins::cli::reload
}

if $executors {
jenkins::cli::exec { 'set_num_executors':
command => ['set_num_executors', $executors],
unless => "[ \$(\$HELPER_CMD get_num_executors) -eq ${executors} ]"
}

Class['jenkins::cli'] ->
Jenkins::Cli::Exec['set_num_executors'] ->
Class['jenkins::jobs']
}

Anchor['jenkins::begin'] ->
Expand All @@ -213,8 +233,9 @@
Anchor['jenkins::begin'] ->
Class['jenkins::service'] ->
Class['jenkins::cli'] ->
Class['jenkins::jobs'] ->
Anchor['jenkins::end']
Class['jenkins::cli::reload'] ->
Class['jenkins::jobs'] ->
Anchor['jenkins::end']
}

if $install_java {
Expand Down
31 changes: 30 additions & 1 deletion spec/acceptance/class_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,33 @@ class {'jenkins':
end

end
end

context 'executors' do
it 'should work with no errors' do
pp = <<-EOS
class {'jenkins':
executors => 42,
}
EOS

# Run it twice and test for idempotency
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_changes => true)
end

describe port(8080) do
# jenkins should already have been running so we shouldn't have to
# sleep
it { should be_listening }
end

describe service('jenkins') do
it { should be_running }
it { should be_enabled }
end

describe file('/var/lib/jenkins/config.xml') do
it { should contain ' <numExecutors>42</numExecutors>' }
end
end # executors
end
29 changes: 29 additions & 0 deletions spec/classes/jenkins_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,34 @@
let(:pre_condition) { 'define firewall ($action, $state, $dport, $proto) {}' }
it { expect { should raise_error(Puppet::Error) } }
end

describe 'executors =>' do
context 'undef' do
it { should_not contain_class('jenkins::cli_helper') }
it { should_not contain_jenkins__cli__exec('set_num_executors') }
end

context '42' do
let(:params) {{ :executors => 42 }}

it { should contain_class('jenkins::cli_helper') }
it do
should contain_jenkins__cli__exec('set_num_executors').with(
:command => ['set_num_executors', 42],
:unless => '[ $($HELPER_CMD get_num_executors) -eq 42 ]',
)
end
it { should contain_jenkins__cli__exec('set_num_executors').that_requires('Class[jenkins::cli]') }
it { should contain_jenkins__cli__exec('set_num_executors').that_comes_before('Class[jenkins::jobs]') }
end

context '{}' do
let(:params) {{ :executors => {} }}

it 'should fail' do
should raise_error(Puppet::Error, /to be an Integer/)
end
end
end # executors =>
end
end
Loading