Skip to content

Commit

Permalink
Merge pull request #207 from rtyler/issues/206-firewall-port-refactor
Browse files Browse the repository at this point in the history
Introduce the jenkins_port function
  • Loading branch information
R. Tyler Croy committed Oct 26, 2014
2 parents 72752a9 + 5d7184e commit 81c0618
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 27 deletions.
21 changes: 21 additions & 0 deletions lib/puppet/parser/functions/jenkins_port.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

module Puppet::Parser::Functions
newfunction(:jenkins_port, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
Return the configurad Jenkins port value
(corresponds to /etc/defaults/jenkins -> JENKINS_PORT
Example:
$port = jenkins_port()
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 lookupvar('::jenkins::params::port')
end
end
end
9 changes: 1 addition & 8 deletions manifests/cli.pp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@
require => Exec['jenkins-cli'],
}

# Get the value of JENKINS_PORT from config_hash or default
$hash = $::jenkins::config_hash
if is_hash($hash) and has_key($hash, 'JENKINS_PORT') and
has_key($hash['JENKINS_PORT'], 'value') {
$port = $hash['JENKINS_PORT']['value']
} else {
$port = '8080'
}
$port = jenkins_port()

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

$libdir = $::jenkins::libdir
$cli_jar = $::jenkins::cli::jar

if $::jenkins::config_hash and has_key($::jenkins::config_hash, 'HTTP_PORT') {
$http_port = $::jenkins::config_hash['HTTP_PORT']
} else {
$http_port = '8080'
}
$port = jenkins_port()

$helper_groovy = "${libdir}/puppet_helper.groovy"
file {$helper_groovy:
Expand All @@ -34,7 +29,7 @@
$helper_cmd = join([
'/usr/bin/java',
"-jar ${::jenkins::cli::jar}",
"-s http://127.0.0.1:${http_port}",
"-s http://127.0.0.1:${port}",
$auth_arg,
"groovy ${helper_groovy}",
], ' ')
Expand Down
8 changes: 1 addition & 7 deletions manifests/firewall.pp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@
fail("Use of private class ${name} by ${caller_module_name}")
}

if $::jenkins::config_hash and has_key($::jenkins::config_hash, 'HTTP_PORT') {
$http_port = $::jenkins::config_hash['HTTP_PORT']
} else {
$http_port = '8080'
}

firewall { '500 allow Jenkins inbound traffic':
action => 'accept',
state => 'NEW',
dport => [$http_port],
dport => [jenkins_port()],
proto => 'tcp',
}
}
Expand Down
4 changes: 2 additions & 2 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
#
# Example use
#
# class{ 'jenkins::config':
# class{ 'jenkins':
# config_hash => {
# 'HTTP_PORT' => { 'value' => '9090' }, 'AJP_PORT' => { 'value' => '9009' }
# }
# }
# V
#
# plugin_hash = undef (Default)
# Hash with config plugins to install
Expand Down
4 changes: 2 additions & 2 deletions spec/classes/jenkins_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

context '$cli => true' do
let(:params) {{ :cli => true,
:config_hash => { 'JENKINS_PORT' => { 'value' => '9000' } }
:config_hash => { 'HTTP_PORT' => { 'value' => '9000' } }
}}
it { should create_class('jenkins::cli') }
it { should contain_exec('jenkins-cli') }
it { should contain_exec('reload-jenkins').with_command(/http:\/\/localhost:9000/) }
it { should contain_exec('safe-restart-jenkins') }
it { should contain_jenkins__sysconfig('JENKINS_PORT').with_value('9000') }
it { should contain_jenkins__sysconfig('HTTP_PORT').with_value('9000') }
end
end

Expand Down
1 change: 0 additions & 1 deletion spec/classes/jenkins_firewall_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@
context 'firewall' do
it { should contain_firewall('500 allow Jenkins inbound traffic') }
end

end
30 changes: 30 additions & 0 deletions spec/functions/jenkins_port_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'spec_helper'

describe 'jenkins_port' do
let(:facts) { { :osfamily => 'RedHat', :operatingsystem => 'RedHat' } }
let(:pre_condition) { 'include ::jenkins' }
# Lazily loaded function call to be used in examples. Not overwriting
# `subject` since rspec-puppet is already defining that to return the
# function
let(:port) {
subject.call([])
}

it 'should default to 8080' do
expect(port).to eql '8080'
end

context 'with overwritten configuration' do
let(:pre_condition) do
<<-ENDPUPPET
class { 'jenkins':
config_hash => {'HTTP_PORT' => {'value' => '1337'}},
}
ENDPUPPET
end

it 'should be our overwritten port' do
expect(port).to eql('1337')
end
end
end

0 comments on commit 81c0618

Please sign in to comment.