Class: Vanagon::Engine::Pooler
- Defined in:
- lib/vanagon/engine/pooler.rb
Instance Attribute Summary collapse
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Attributes inherited from Base
Instance Method Summary collapse
-
#add_tags_to_target(pooler, hostname) ⇒ Object
Add tags to a provisioned target using the pooler API.
-
#build_host_name ⇒ Object
Return the vmpooler template name to build on.
-
#initialize(platform, target = nil, **opts) ⇒ Pooler
constructor
The vmpooler_template is required to use the pooler engine.
-
#load_token ⇒ String?
Retrieve the pooler token from an environment variable (“VMPOOLER_TOKEN”) or from a number of potential configuration files (~/.vanagon-token or ~/.vmfloaty.yml).
-
#name ⇒ Object
Get the engine name.
-
#select_target ⇒ Object
This method is used to obtain a vm to build upon using the Puppet Labs’ vmpooler (github.com/puppetlabs/vmpooler) or other pooler technologies leveraging the same API.
-
#select_target_from(pooler) ⇒ Object
Attempt to provision a host from a specific pooler.
-
#teardown ⇒ Object
This method is used to tell the vmpooler to delete the instance of the vm that was being used so the pool can be replenished.
Methods inherited from Base
#dispatch, #get_remote_workdir, #retrieve_built_artifact, #setup, #ship_workdir, #startup, #validate_platform
Constructor Details
#initialize(platform, target = nil, **opts) ⇒ Pooler
The vmpooler_template is required to use the pooler engine
10 11 12 13 14 15 16 |
# File 'lib/vanagon/engine/pooler.rb', line 10 def initialize(platform, target = nil, **opts) super @available_poolers = ["http://vmpooler.delivery.puppetlabs.net", "https://nspooler-service-prod-1.delivery.puppetlabs.net"] @token = load_token @required_attributes << "vmpooler_template" end |
Instance Attribute Details
#token ⇒ Object (readonly)
Returns the value of attribute token.
7 8 9 |
# File 'lib/vanagon/engine/pooler.rb', line 7 def token @token end |
Instance Method Details
#add_tags_to_target(pooler, hostname) ⇒ Object
Add tags to a provisioned target using the pooler API
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/vanagon/engine/pooler.rb', line 117 def (pooler, hostname) = { 'tags' => { 'jenkins_build_url' => ENV['BUILD_URL'], 'project' => ENV['JOB_NAME'] || 'vanagon', 'created_by' => ENV['USER'] || ENV['USERNAME'] || 'unknown' } } Vanagon::Utilities.http_request( "#{pooler}/vm/#{hostname}", 'PUT', .to_json, { 'X-AUTH-TOKEN' => @token } ) end |
#build_host_name ⇒ Object
Return the vmpooler template name to build on
24 25 26 27 28 29 30 31 |
# File 'lib/vanagon/engine/pooler.rb', line 24 def build_host_name if @build_host_template_name.nil? validate_platform @build_host_template_name = @platform.vmpooler_template end @build_host_template_name end |
#load_token ⇒ String?
Retrieve the pooler token from an environment variable (“VMPOOLER_TOKEN”) or from a number of potential configuration files (~/.vanagon-token or ~/.vmfloaty.yml).
37 38 39 |
# File 'lib/vanagon/engine/pooler.rb', line 37 def load_token ENV['VMPOOLER_TOKEN'] || token_from_file end |
#name ⇒ Object
Get the engine name
19 20 21 |
# File 'lib/vanagon/engine/pooler.rb', line 19 def name 'pooler' end |
#select_target ⇒ Object
This method is used to obtain a vm to build upon using the Puppet Labs’ vmpooler (github.com/puppetlabs/vmpooler) or other pooler technologies leveraging the same API
82 83 84 85 86 87 88 |
# File 'lib/vanagon/engine/pooler.rb', line 82 def select_target @available_poolers.each do |current_pooler| @pooler = select_target_from(current_pooler) break unless @pooler.empty? end raise Vanagon::Error, "Something went wrong getting a target vm to build on, maybe the pool for #{build_host_name} is empty?" if @pooler.empty? end |
#select_target_from(pooler) ⇒ Object
Attempt to provision a host from a specific pooler.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/vanagon/engine/pooler.rb', line 92 def select_target_from(pooler) # rubocop:disable Metrics/AbcSize response = Vanagon::Utilities.http_request( "#{pooler}/vm", 'POST', '{"' + build_host_name + '":"1"}', { 'X-AUTH-TOKEN' => @token } ) if response["ok"] @target = response[build_host_name]['hostname'] # The nspooler does not use 'domain' in the response: 'hostname' just returns the fqdn. # in the future we should make the two APIs the same in this sense, but for now, just check # if 'domain' is a thing and use it if so. if response['domain'] @target += '.' + response['domain'] end Vanagon::Driver.logger.info "Reserving #{@target} (#{build_host_name}) [#{@token ? 'token used' : 'no token used'}]" (pooler, response[build_host_name]['hostname']) else pooler = '' end pooler end |
#teardown ⇒ Object
This method is used to tell the vmpooler to delete the instance of the vm that was being used so the pool can be replenished.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/vanagon/engine/pooler.rb', line 135 def teardown response = Vanagon::Utilities.http_request( "#{@pooler}/vm/#{@target}", "DELETE", nil, { 'X-AUTH-TOKEN' => @token } ) if response and response["ok"] Vanagon::Driver.logger.info "#{@target} has been destroyed" warn "#{@target} has been destroyed" else Vanagon::Driver.logger.info "#{@target} could not be destroyed" warn "#{@target} could not be destroyed" end rescue Vanagon::Error => e Vanagon::Driver.logger.info "#{@target} could not be destroyed (#{e.})" warn "#{@target} could not be destroyed (#{e.})" end |