class Reagan::Config
builds a single config from passed flags, yaml config, and knife.rb
Public Class Methods
cli_flags()
click to toggle source
grabs the flags passed in via command line
# File lib/reagan/config.rb 50 def self::cli_flags 51 if @cli_flags 52 @cli_flags 53 else 54 flags = { 'config' => '/etc/reagan.yml' } 55 OptionParser.new do |opts| 56 opts.banner = 'Usage: reagan [options]' 57 opts.on('-o', '--override cb1,cb2', Array, 'Comma separated list of cookbooks to test') do |cookbooks| 58 flags[:override_cookbooks] = cookbooks 59 end 60 61 opts.on('-p', '--print', 'Print the config options that will be used') do |config| 62 flags['print_config'] = config 63 end 64 65 opts.on('-p', '--pull_num 123', Integer, 'Github pull number to test') do |pull| 66 flags['pull'] = pull 67 end 68 69 opts.on('-c', '--config reagan.yml', 'Path to config file (defaults to /etc/reagan.yml)') do |config| 70 flags['config'] = config 71 end 72 73 opts.on('-h', '--help', 'Displays Help') do 74 puts opts 75 exit 76 end 77 end.parse! 78 79 @cli_flags = flags 80 flags 81 end 82 end
config_file()
click to toggle source
loads the reagan.yml config file from /etc/reagan.yml or the passed location
# File lib/reagan/config.rb 85 def self::config_file 86 config = YAML.load_file(cli_flags['config']) 87 88 config 89 rescue Errno::ENOENT 90 puts "ERROR: Cannot load Reagan config file at #{cli_flags['config']}".to_red 91 exit 1 92 rescue Psych::SyntaxError 93 puts "ERROR: Syntax error in Reagan config file at #{cli_flags['config']}".to_red 94 exit 1 95 end
merge_config()
click to toggle source
join the config file with the passed flags into a single object
# File lib/reagan/config.rb 120 def self::merge_config 121 config = config_file 122 config['flags'] = {} 123 config['flags'].merge!(cli_flags) 124 125 # if no pull request provided at the CLI use the Jenkins environmental variable 126 unless config['flags']['pull'] 127 config['flags']['pull'] = ENV['ghprbPullId'] 128 end 129 130 # merge in the jenkins workspace variable on top of the config file 131 if ENV['WORKSPACE'] 132 config['jenkins'] = {} unless config['jenkins'] # create the higher level hash if it doesn't exist 133 config['jenkins']['workspace_dir'] = ENV['WORKSPACE'] 134 end 135 136 config 137 end
pretty_print(hash = nil, spaces = 0)
click to toggle source
pretty print the config hash
# File lib/reagan/config.rb 35 def self::pretty_print(hash = nil, spaces = 0) 36 hash = @settings if hash.nil? 37 hash.each do |k, v| 38 spaces.times { print ' ' } 39 print k.to_s + ': ' 40 if v.class == Hash 41 print "\n" 42 pretty_print(v, spaces + 2) 43 else 44 puts v 45 end 46 end 47 end
settings()
click to toggle source
lazy load config settings
# File lib/reagan/config.rb 30 def self::settings 31 @settings ||= merge_config 32 end
validate()
click to toggle source
make sure the config was properly loaded and contains the various keys we need
# File lib/reagan/config.rb 98 def self::validate 99 settings = Config.settings 100 101 if settings == false 102 puts "ERROR: Reagan config at #{cli_flags['config']} does not contain any configuration data".to_red 103 exit 1 104 end 105 106 # make sure a pull request is specified 107 unless settings['flags']['pull'] 108 puts 'Jenkins ghprbPullId environmental variable not set or --pull option not used. Cannot continue'.to_red 109 exit 1 110 end 111 112 # make sure either jenkins gave us a workspace variable or its defined in the config 113 unless ENV['WORKSPACE'] || (settings['jenkins'] && settings['jenkins']['workspace_dir']) 114 puts 'Jenkins workspace_dir not defined in the config file and $WORKSPACE env variable empty. Exiting'.to_red 115 exit 1 116 end 117 end