Class: Vanagon::Engine::Pooler

Inherits:
Base
  • Object
show all
Defined in:
lib/vanagon/engine/pooler.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#remote_workdir, #target

Instance Method Summary collapse

Methods inherited from Base

#dispatch, #get_remote_workdir, #parse_target_defaults, #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



15
16
17
18
19
20
21
22
23
24
# File 'lib/vanagon/engine/pooler.rb', line 15

def initialize(platform, target = nil, **opts)
  super

  @available_poolers = %w[
    https://vmpooler-prod.k8s.infracore.puppet.net
    https://nspooler-prod.k8s.infracore.puppet.net
  ]
  @token = load_token
  @required_attributes << "vmpooler_template"
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



12
13
14
# File 'lib/vanagon/engine/pooler.rb', line 12

def token
  @token
end

Instance Method Details

#add_tags_to_target(pooler, hostname) ⇒ Object

Add tags to a provisioned target using the pooler API



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/vanagon/engine/pooler.rb', line 125

def add_tags_to_target(pooler, hostname)
  tags = {
    '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',
    tags.to_json,
    { 'X-AUTH-TOKEN' => @token }
  )
end

#build_host_nameObject

Return the vmpooler template name to build on



32
33
34
35
36
37
38
39
# File 'lib/vanagon/engine/pooler.rb', line 32

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_tokenString?

Retrieve the pooler token from an environment variable (“VMPOOLER_TOKEN”) or from a number of potential configuration files (~/.vanagon-token or ~/.vmfloaty.yml).

Returns:

  • (String, nil)

    token for use with the vmpooler



45
46
47
# File 'lib/vanagon/engine/pooler.rb', line 45

def load_token
  ENV['VMPOOLER_TOKEN'] || token_from_file
end

#nameObject

Get the engine name



27
28
29
# File 'lib/vanagon/engine/pooler.rb', line 27

def name
  'pooler'
end

#select_targetObject

This method is used to obtain a vm to build upon using Puppet’s internal vmpooler (github.com/puppetlabs/vmpooler) or other pooler technologies leveraging the same API

Raises:



90
91
92
93
94
95
96
# File 'lib/vanagon/engine/pooler.rb', line 90

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.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/vanagon/engine/pooler.rb', line 100

def select_target_from(pooler)
  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'}]"
    add_tags_to_target(pooler, response[build_host_name]['hostname'])
  else
    pooler = ''
  end
  pooler
end

#teardownObject

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.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/vanagon/engine/pooler.rb', line 143

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"
    VanagonLogger.info "#{@target} has been destroyed"
  else
    Vanagon::Driver.logger.info "#{@target} could not be destroyed"
    VanagonLogger.info "#{@target} could not be destroyed"
  end
rescue Vanagon::Error => e
  Vanagon::Driver.logger.info "#{@target} could not be destroyed (#{e.message})"
  VanagonLogger.error "#{@target} could not be destroyed (#{e.message})"
end