Skip to content

Commit

Permalink
(SIMP-5932) Add puppetserver wait option to bootstrap (#102)
Browse files Browse the repository at this point in the history
* (SIMP-5932) Add puppetserver wait option to bootstrap

- Added a `simp bootstrap` option to set the wait time for
  the puppetserver to start during the bootstrap process.
- Adjusted the help message so it will fit within an
  80-character console window.
- Add unit testing with Ruby for Puppet 6.x to .travis.yml

SIMP-5932 #close
  • Loading branch information
lnemsick-simp authored and trevor-vaughan committed Jan 28, 2019
1 parent 2192cb1 commit c35a986
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 21 deletions.
30 changes: 26 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ language: ruby
cache: bundler
sudo: false

stages:
- check
- spec
- name: deploy
if: 'tag IS present'

bundler_args: --without development system_tests --path .vendor

notifications:
Expand All @@ -18,19 +24,35 @@ addons:

before_install:
- rm -f Gemfile.lock
- gem install -v '~> 1.16' bundler


jobs:
allow_failures:
- name: 'Ruby packaged with Puppet 6.x (allowed to fail)'

include:
- stage: check
rvm: 2.1.9
name: 'Syntax, style, and validation checks'
rvm: 2.4.4
script:
- bundle exec rake pkg:compare_latest_tag
- bundle exec rake pkg:create_tag_changelog
- bundle exec rake pkg:gem

# Test with Ruby versions packaged with standard Puppet All-in-one installs
- stage: spec
name: 'Ruby packaged with Puppet 4.x'
rvm: 2.1.9
env:
- PUPPET_VERSION="~> 4.10"
- SIMP_SKIP_NON_SIMPOS_TESTS=1
- SIMP_CLI_GEMSPEC_NO_PUPPET_VERSION=yes
script:
- bundle exec rake spec

- stage: spec
name: 'Ruby packaged with Puppet 5.x'
rvm: 2.4.4
env:
- PUPPET_VERSION="~> 5.5"
Expand All @@ -40,9 +62,10 @@ jobs:
- bundle exec rake spec

- stage: spec
rvm: 2.1.9
name: 'Ruby packaged with Puppet 6.x (allowed to fail)'
rvm: 2.5.1
env:
- PUPPET_VERSION="~> 4.10"
- PUPPET_VERSION="~> 6.0"
- SIMP_SKIP_NON_SIMPOS_TESTS=1
- SIMP_CLI_GEMSPEC_NO_PUPPET_VERSION=yes
script:
Expand All @@ -62,6 +85,5 @@ jobs:
secure: "R6KNY9wDFtWVDXA0tC/We+OunS+8wiu4YHymDh5NBO/NWm0Dt5I6+Ado8QfjW5jZWja0Bl1cpjsf65Ln+XMQ6MDsiRvidnqMKTyyzjF/Y5U1inRm6i2+XGCCxKPGVJY6IrSidJg0NIlqKKghaq7DYDT5cDopcA/xHqY/S4mxvJf1OQQ7JdN0GtOrNv2h+lL9qlpvE8ogvb0fQyZXyKkz3TCCyauzb2w6ay4TjIqD2yo8yy3nUInyRDJf6XIyxWdiOFNHe3cwiYblqmosPUMYBf6qkzGPzmsQsmPoqN0+WopMnaNShPoiVoTdXIJDPe0deWmFbvFUvYL1zYoxZ+Vggjd+wXXJBiB3i/7xC3MOj9EPDQtY4AdjG5ZM6mqyjX7jFzv31DfK8WPa11OhDeOYgWfTc8b3MkeDWsKqC/IjPs83M8Qz03Fwxtocx0cNP2BmIDxxMi8GRT45CG3BYONVFHvXo/uGmUdY1jm4ke8RNOpem73kLbYmFrb314UM+AwwF3QxKc5PRHYIRy4HHFlCRagbl0+pga42w1Obp5PEVPCoyt9HAJ5FRIaR5Y7hogn1HiqpZXlnDRGWBVHqASvyntc2sHtXwIhtihUzAT037+Bk5+BS1tsYbJhvCmj12GUypWvUyryUH0354SvKitoHbfPITH8Tn7dKissftfF4J9o="
on:
tags: true
rvm: 2.4.4
condition: "($SKIP_PUBLISH != true)"

6 changes: 5 additions & 1 deletion build/rubygem-simp-cli.spec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

%global gemdir /usr/share/simp/ruby
%global geminstdir %{gemdir}/gems/%{gemname}-%{version}
%global cli_version 4.3.2
%global cli_version 4.4.0
%global highline_version 1.7.8

# gem2ruby's method of installing gems into mocked build roots will blow up
Expand Down Expand Up @@ -126,6 +126,10 @@ EOM
%doc %{gemdir}/doc

%changelog
* Tue Jan 15 2019 Liz Nemsick <[email protected]> - 4.4.0
- Added a `simp bootstrap` option to set the wait time for the
puppetserver to start during the bootstrap process.

* Tue Jan 01 2019 Jeanne Greulich <[email protected]> - 4.3.2
- Fixed error in Changelog

Expand Down
60 changes: 45 additions & 15 deletions lib/simp/cli/commands/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ class Simp::Cli::Commands::Bootstrap < Simp::Cli::Commands::Command

require 'facter'

DEFAULT_PUPPETSERVER_WAIT_MINUTES = 5

def initialize

@is_pe = Simp::Cli::Utils.puppet_info[:is_pe]

@puppetserver_service = 'puppetserver'
@puppetdb_service = 'puppetdb'
@puppetserver_wait_minutes = DEFAULT_PUPPETSERVER_WAIT_MINUTES

if @is_pe
@puppetserver_service = 'pe-puppetserver'
Expand Down Expand Up @@ -167,29 +170,51 @@ def parse_command_line(args)
opts.separator "Prior to modification, config files are backed up to #{Simp::Cli::SIMP_CLI_HOME}/simp_bootstrap.backup.*\n\n"
opts.separator "OPTIONS:\n"

opts.on('-k', '--kill_agent', 'Ignore the status of agent_catalog_run_lockfile, and',
'force kill active puppet agents at the beginning of',
'bootstrap') do |k|
opts.on('-k', '--kill_agent',
'Ignore agent_catalog_run_lockfile',
'status and force kill active puppet',
'agents at the beginning of bootstrap.'
) do |k|
@kill_agent = true
end

opts.on('-r', '--[no-]remove_ssldir', 'Remove the existing puppet ssldir. If unspecified',
'user will be prompted for action to take.') do |r|
opts.on('-r', '--[no-]remove_ssldir',
'Remove the existing puppet ssldir.',
'If unspecified, user will be prompted',
'for action to take.'
) do |r|
@remove_ssldir = r
end

opts.on('-t', '--[no-]track', 'Enables/disables the tracker. Default is enabled.') do |t|
opts.on('-t', '--[no-]track',
'Enables/disables the tracker.',
'Default is enabled.'
) do |t|
@track = t
end

opts.on('-u', '--unsafe', "Run bootstrap in 'unsafe' mode. Interrupts are NOT ",
'captured and ignored, which may result in a corrupt',
'system. Useful for debugging. Default is SAFE.') do |u|
opts.on('-u', '--unsafe',
"Run bootstrap in 'unsafe' mode.",
'Interrupts are NOT captured and ignored,',
'which may result in a corrupt system.',
'Useful for debugging.',
'Default is SAFE.'
) do |u|
@unsafe = true
end

opts.on('-v', '--[no-]verbose', 'Enables/disables verbose mode. Prints out verbose',
'information.') do |v|
opts.on('-w', '--puppetserver-wait-minutes MIN', Float,
'Number of minutes to wait for the',
'puppetserver to start.',
"Default is #{DEFAULT_PUPPETSERVER_WAIT_MINUTES} minutes."
) do |w|
@puppetserver_wait_minutes = w
end

opts.on('-v', '--[no-]verbose',
'Enables/disables verbose mode. Prints out',
'verbose information.'
) do |v|
@verbose = true
end

Expand All @@ -201,6 +226,11 @@ def parse_command_line(args)
end

opt_parser.parse!(args)

unless @puppetserver_wait_minutes > 0
msg = "Invalid puppetserver wait minutes '#{@puppetserver_wait_minutes}'. Must be > 0"
raise OptionParser::ParseError.new(msg)
end
end


Expand Down Expand Up @@ -445,12 +475,12 @@ def ensure_puppetserver_running(port = nil)
debug(curl_cmd)
running = (%x{#{curl_cmd} 2>&1} =~ /CRL/)
unless running
debug("System not running, attempting to restart puppetserver")
debug('System not running, attempting to restart puppetserver')
system(%(puppet resource service #{@puppetserver_service} ensure="running" enable=true > /dev/null 2>&1 &))
stages = ["\\",'|','/','-']
rest = 0.1
timeout = 5
Timeout::timeout(timeout*60) {
debug("Waiting up to #{@puppetserver_wait_minutes} minutes for puppetserver to respond")
Timeout::timeout(@puppetserver_wait_minutes * 60) {
while not running do
running = (%x{#{curl_cmd} 2>&1} =~ /CRL/)
stages.each{ |x|
Expand All @@ -463,7 +493,7 @@ def ensure_puppetserver_running(port = nil)
$stdout.flush
end
rescue Timeout::Error
fail("The Puppet Server did not start within #{timeout} minutes. Please start puppetserver by hand and inspect any issues.")
fail("The Puppet Server did not start within #{@puppetserver_wait_minutes} minutes. Please start puppetserver by hand and inspect any issues.")
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/simp/cli/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Simp; end

class Simp::Cli
VERSION = '4.3.2'
VERSION = '4.4.0'
end
77 changes: 77 additions & 0 deletions spec/lib/simp/cli/commands/bootstrap_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require 'simp/cli/commands/bootstrap'

describe 'Simp::Cli::Command::Bootstrap#run' do
let(:files_dir) { File.join(File.dirname(__FILE__), 'files') }

before(:each) do
@tmp_dir = Dir.mktmpdir( File.basename(__FILE__) )
test_env_dir = File.join(@tmp_dir, 'environments')
simp_env_dir = File.join(test_env_dir, 'simp')
FileUtils.mkdir(test_env_dir)
FileUtils.cp_r(File.join(files_dir, 'environments', 'simp'), test_env_dir)

allow(Simp::Cli::Utils).to receive(:puppet_info).and_return( {
:config => {
'agent_disabled_lockfile' => File.join(@tmp_dir, 'agent_disable_lockfile'),
'codedir' => @tmp_dir,
'confdir' => @tmp_dir,
'hostcert' => File.join(@tmp_dir, 'test_host.pem'),
'hostprivkey' => File.join(@tmp_dir, 'test_host.key'),
'rundir' => File.join(@tmp_dir, 'rundir'),
'ssldir' => File.join(@tmp_dir, 'ssldir'),
'vardir' => File.join(@tmp_dir, 'vardir')
},
:environment_path => test_env_dir,
:simp_environment_path => simp_env_dir,
:fake_ca_path => File.join(test_env_dir, 'simp', 'FakeCA'),
:is_pe => false
} )

@bootstrap = Simp::Cli::Commands::Bootstrap.new
end

after :each do
FileUtils.remove_entry_secure @tmp_dir, true
Facter.reset # make sure this test's facts don't affect other tests
end

context 'help' do
it 'prints help message' do
options_help = <<-EOM
OPTIONS:
-k, --kill_agent Ignore agent_catalog_run_lockfile
status and force kill active puppet
agents at the beginning of bootstrap.
-r, --[no-]remove_ssldir Remove the existing puppet ssldir.
If unspecified, user will be prompted
for action to take.
-t, --[no-]track Enables/disables the tracker.
Default is enabled.
-u, --unsafe Run bootstrap in 'unsafe' mode.
Interrupts are NOT captured and ignored,
which may result in a corrupt system.
Useful for debugging.
Default is SAFE.
-w MIN, Number of minutes to wait for the
--puppetserver-wait-minutes puppetserver to start.
Default is 5 minutes.
-v, --[no-]verbose Enables/disables verbose mode. Prints out
verbose information.
-h, --help Print out this message.
EOM
expected_regex = Regexp.new(Regexp.escape(options_help.strip))
expect{ @bootstrap.run(['-h']) }.to output(expected_regex).to_stdout
end
end

context 'invalid options' do
it 'fails unless --puppetserver-wait-minutes argument is > 0' do
expect{ @bootstrap.run(['--puppetserver-wait-minutes', '0']) }.to raise_error(/Invalid puppetserver wait minutes/)
expect{ @bootstrap.run(['-w', '-1']) }.to raise_error(/Invalid puppetserver wait minutes/)
end

it 'fails unless --puppetserver-wait-minutes argument parses to a number' do
expect{ @bootstrap.run(['--puppetserver-wait-minutes', 'oops']) }.to raise_error(OptionParser::InvalidArgument)
end
end
end

0 comments on commit c35a986

Please sign in to comment.