Class: Beaker::Options::Presets

Inherits:
Object
  • Object
show all
Defined in:
lib/beaker/options/presets.rb

Overview

A class representing the environment variables and preset argument values to be incorporated into the Beaker options Object.

Constant Summary collapse

ENVIRONMENT_SPEC =

This is a constant that describes the variables we want to collect from the environment. The keys correspond to the keys in `presets` (flattened) The values are an optional array of environment variable names to look for. The array structure allows us to define multiple environment variables for the same configuration value. They are checked in the order they are arrayed so that preferred and “fallback” values work as expected.

'JOB_NAME' and 'BUILD_URL' envs are supplied by Jenkins wiki.jenkins-ci.org/display/JENKINS/Building+a+software+project

{
  :home                 => 'HOME',
  :project              => ['BEAKER_PROJECT', 'BEAKER_project', 'JOB_NAME'],
  :department           => ['BEAKER_DEPARTMENT', 'BEAKER_department'],
  :jenkins_build_url    => ['BEAKER_BUILD_URL', 'BUILD_URL'],
  :created_by           => ['BEAKER_CREATED_BY'],
  :consoleport          => ['BEAKER_CONSOLEPORT', 'consoleport'],
  :is_pe                => ['BEAKER_IS_PE', 'IS_PE'],
  :pe_dir               => ['BEAKER_PE_DIR', 'pe_dist_dir'],
  :puppet_agent_version => ['BEAKER_PUPPET_AGENT_VERSION'],
  :puppet_agent_sha     => ['BEAKER_PUPPET_AGENT_SHA'],
  :puppet_collection    => ['BEAKER_PUPPET_COLLECTION'],
  :pe_version_file      => ['BEAKER_PE_VERSION_FILE', 'pe_version_file'],
  :pe_ver               => ['BEAKER_PE_VER', 'pe_ver'],
  :forge_host           => ['BEAKER_FORGE_HOST', 'forge_host'],
  :package_proxy        => ['BEAKER_PACKAGE_PROXY'],
  :release_apt_repo_url => ['BEAKER_RELEASE_APT_REPO', 'RELEASE_APT_REPO'],
  :release_yum_repo_url => ['BEAKER_RELEASE_YUM_REPO', 'RELEASE_YUM_REPO'],
  :dev_builds_url       => ['BEAKER_DEV_BUILDS_URL', 'DEV_BUILDS_URL'],
  :vbguest_plugin       => ['BEAKER_VB_GUEST_PLUGIN', 'BEAKER_vb_guest_plugin'],
  :test_tag_and         => ['BEAKER_TAG', 'BEAKER_TEST_TAG_AND'],
  :test_tag_or          => ['BEAKER_TEST_TAG_OR'],
  :test_tag_exclude     => ['BEAKER_EXCLUDE_TAG', 'BEAKER_TEST_TAG_EXCLUDE'],
  :run_in_parallel      => ['BEAKER_RUN_IN_PARALLEL'],
}

Instance Method Summary collapse

Instance Method Details

#calculate_env_varsOptionsHash

Generates an OptionsHash of the environment variables of interest to Beaker

Returns:

  • (OptionsHash)

    The supported environment variables in an OptionsHash, empty or nil environment variables are removed from the OptionsHash


103
104
105
106
107
108
109
110
# File 'lib/beaker/options/presets.rb', line 103

def calculate_env_vars
  found = Beaker::Options::OptionsHash.new
  found = found.merge(format_found_env_vars( collect_env_vars( ENVIRONMENT_SPEC )))
  found[:answers] = select_env_by_regex('\\Aq_')

  found.delete_if {|key, value| value.nil? or value.empty? }
  found
end

#collect_env_vars(env_var_spec) ⇒ Hash

Takes an environment_spec and searches the processes environment variables accordingly

Parameters:

  • env_var_spec (Hash{Symbol=>Array,String})

    the spec of what env vars to search for

Returns:

  • (Hash)

    Found environment values


60
61
62
63
64
65
66
67
68
69
# File 'lib/beaker/options/presets.rb', line 60

def collect_env_vars( env_var_spec )
  env_var_spec.inject({}) do |memo, key_value|
    key, value = key_value[0], key_value[1]

    set_env_var = Array(value).detect {|possible_variable| ENV[possible_variable] }
    memo[key] = ENV[set_env_var] if set_env_var

    memo
  end
end

#env_varsOptionsHash

Return an OptionsHash of environment variables used in this run of Beaker

Returns:

  • (OptionsHash)

    The supported environment variables in an OptionsHash, empty or nil environment variables are removed from the OptionsHash


116
117
118
# File 'lib/beaker/options/presets.rb', line 116

def env_vars
  @env ||= calculate_env_vars
end

#format_found_env_vars(found_env_vars) ⇒ Hash

Takes a hash where the values are found environment configuration values and formats them to appropriate Beaker configuration values

Parameters:

  • found_env_vars (Hash{Symbol=>String})

    Environment variables to munge

Returns:

  • (Hash)

    Environment config values formatted appropriately


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/beaker/options/presets.rb', line 77

def format_found_env_vars( found_env_vars )
  found_env_vars[:consoleport] &&= found_env_vars[:consoleport].to_i

  if found_env_vars[:is_pe]
    is_pe_val = found_env_vars[:is_pe]
    type = case is_pe_val
           when /yes|true/ then 'pe'
           when /no|false/ then 'foss'
           else
             raise "Invalid value for one of #{ENVIRONMENT_SPEC[:is_pe].join(' ,')}: #{is_pe_val}"
           end

    found_env_vars[:type] = type
  end
  if found_env_vars[:run_in_parallel]
    found_env_vars[:run_in_parallel] = found_env_vars[:run_in_parallel].split(',')
  end

  found_env_vars[:pe_version_file_win] = found_env_vars[:pe_version_file]
  found_env_vars
end

#presetsOptionsHash

Generates an OptionsHash of preset values for arguments supported by Beaker

Returns:

  • (OptionsHash)

    The supported arguments in an OptionsHash


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/beaker/options/presets.rb', line 123

def presets
  h = Beaker::Options::OptionsHash.new
  h.merge({
    :project                => 'Beaker',
    :department             => 'unknown',
    :created_by             => ENV['USER'] || ENV['USERNAME'] || 'unknown',
    :host_tags              => {},
    :openstack_api_key      => ENV['OS_PASSWORD'],
    :openstack_username     => ENV['OS_USERNAME'],
    :openstack_auth_url     => "#{ENV['OS_AUTH_URL']}/tokens",
    :openstack_tenant       => ENV['OS_TENANT_NAME'],
    :openstack_keyname      => ENV['OS_KEYNAME'],
    :openstack_network      => ENV['OS_NETWORK'],
    :openstack_region       => ENV['OS_REGION'],
    :openstack_volume_support => ENV['OS_VOLUME_SUPPORT'] || true,
    :jenkins_build_url      => nil,
    :validate               => true,
    :configure              => true,
    :log_level              => 'info',
    :trace_limit            => 10,
    :"master-start-curl-retries"  => 120,
    :masterless             => false,
    :options_file           => nil,
    :type                   => 'pe',
    :provision              => true,
    :preserve_hosts         => 'never',
    :root_keys              => false,
    :quiet                  => false,
    :project_root           => File.expand_path(File.join(File.dirname(__FILE__), "../")),
    :xml_dir                => 'junit',
    :xml_file               => 'beaker_junit.xml',
    :xml_time               => 'beaker_times.xml',
    :xml_time_enabled       => false,
    :xml_stylesheet         => 'junit.xsl',
    :default_log_prefix     => 'beaker_logs',
    :log_dir                => 'log',
    :log_sut_event          => 'sut.log',
    :color                  => true,
    :dry_run                => false,
    :test_tag_and           => '',
    :test_tag_or            => '',
    :test_tag_exclude       => '',
    :timeout                => 900, # 15 minutes
    :fail_mode              => 'slow',
    :accept_all_exit_codes  => false,
    :timesync               => false,
    :disable_iptables       => false,
    :set_env                => true,
    :disable_updates        => true,
    :repo_proxy             => false,
    :package_proxy          => false,
    :add_el_extras          => false,
    :epel_url               => "http://dl.fedoraproject.org/pub/epel",
    :epel_url_archive       => 'http://archive.fedoraproject.org/pub/archive/epel',
    :consoleport            => 443,
    :pe_dir                 => '/opt/enterprise/dists',
    :pe_version_file        => 'LATEST',
    :pe_version_file_win    => 'LATEST-win',
    :host_env               => {},
    :host_name_prefix       => nil,
    :ssh_env_file           => '~/.ssh/environment',
    :profile_d_env_file     => '/etc/profile.d/beaker_env.sh',
    :dot_fog                => File.join(ENV['HOME'], '.fog'),
    :ec2_yaml               => 'config/image_templates/ec2.yaml',
    :help                   => false,
    :collect_perf_data      => 'none',
    :puppetdb_port_ssl      => 8081,
    :puppetdb_port_nonssl   => 8080,
    :puppetserver_port      => 8140,
    :nodeclassifier_port    => 4433,
    :cache_files_locally    => false,
    :aws_keyname_modifier   => rand(10 ** 10).to_s.rjust(10,'0'), # 10 digit random number string
    :run_in_parallel        => [],
    :use_fog_credentials    => true,
    :ssh                    => {
                               :config                => false,
                               :verify_host_key       => false,
                               :auth_methods          => ["publickey"],
                               :port                  => 22,
                               :forward_agent         => true,
                               :keys                  => ["#{ENV['HOME']}/.ssh/id_rsa"],
                               :user_known_hosts_file => "#{ENV['HOME']}/.ssh/known_hosts",
                               :keepalive             => true
    }
  })
end

#select_env_by_regex(regex) ⇒ Hash

Select all environment variables whose name matches provided regex

Returns:

  • (Hash)

    Hash of environment variables


45
46
47
48
49
50
51
52
53
# File 'lib/beaker/options/presets.rb', line 45

def select_env_by_regex regex
  envs = Beaker::Options::OptionsHash.new
  ENV.each_pair do | k, v |
    if k.to_s =~ /#{regex}/
      envs[k] = v
    end
  end
  envs
end