standardize Name tag, implement tag_vm_user method

This commit is contained in:
Samuel Beaulieu 2022-07-28 08:42:28 -05:00
parent 5f4a215621
commit 4df7c2b746
No known key found for this signature in database
GPG key ID: 12030F74136D0F34
2 changed files with 24 additions and 33 deletions

View file

@ -64,23 +64,23 @@ module Vmpooler
# main configuration options
def region
return provider_config['region'] if provider_config['region']
provider_config['region'] if provider_config['region']
end
# main configuration options, overridable for each pool
def zone(pool_name)
return pool_config(pool_name)['zone'] if pool_config(pool_name)['zone']
return provider_config['zone'] if provider_config['zone']
provider_config['zone'] if provider_config['zone']
end
def amisize(pool_name)
return pool_config(pool_name)['amisize'] if pool_config(pool_name)['amisize']
return provider_config['amisize'] if provider_config['amisize']
provider_config['amisize'] if provider_config['amisize']
end
def volume_size(pool_name)
return pool_config(pool_name)['volume_size'] if pool_config(pool_name)['volume_size']
return provider_config['volume_size'] if provider_config['volume_size']
provider_config['volume_size'] if provider_config['volume_size']
end
# dns
@ -107,7 +107,7 @@ module Vmpooler
end
def to_provision(pool_name)
return pool_config(pool_name)['provision'] if pool_config(pool_name)['provision']
pool_config(pool_name)['provision'] if pool_config(pool_name)['provision']
end
# Base methods that are implemented:
@ -271,7 +271,7 @@ module Vmpooler
created_instance = get_vm(pool_name, new_vmname)
@redis.with_metrics do |redis|
redis.hset("vmpooler__vm__#{new_vmname}", 'host', created_instance['name'])
redis.hset("vmpooler__vm__#{new_vmname}", 'host', created_instance['private_dns_name'])
end
# extra setup steps
provision_node_aws(created_instance['private_dns_name'], pool_name, new_vmname) if to_provision(pool_name) == 'true' || to_provision(pool_name) == true
@ -382,7 +382,6 @@ module Vmpooler
instance_hash = get_vm(pool_name, vm_name)
debug_logger("trigger delete_instance #{vm_name}")
# vm_hash = get_vm(pool_name, vm_name)
instances.terminate
begin
connection.client.wait_until(:instance_terminated, { instance_ids: [instances.id] })
@ -398,10 +397,9 @@ module Vmpooler
# check if a vm is ready by opening a socket on port 22
# if a domain is set, it will use vn_name.domain,
# if not then it will use the ip directly (AWS workaround)
# if not then it will use the private dns name directly (AWS workaround)
def vm_ready?(pool_name, vm_name)
begin
# TODO: we could use a healthcheck resource attached to instance
domain_set = domain
if domain_set.nil?
vm_ip = get_vm(pool_name, vm_name)['private_dns_name']
@ -428,20 +426,18 @@ module Vmpooler
vm_hash = get_vm(pool, vm_name)
return false if vm_hash.nil?
new_labels = vm_hash['labels']
# bailing in this case since labels should exist, and continuing would mean losing them
return false if new_labels.nil?
filters = [{
name: 'tag:vm_name',
values: [vm_name]
}]
instances = connection.instances(filters: filters).first
return false if instances.nil?
# add new label called token-user, with value as user
new_labels['token-user'] = user
begin
instances_set_labels_request_object = Google::Apis::ComputeV1::InstancesSetLabelsRequest.new(label_fingerprint: vm_hash['label_fingerprint'], labels: new_labels)
result = connection.set_instance_labels(project, zone(pool), vm_name, instances_set_labels_request_object)
wait_for_zone_operation(project, zone(pool), result)
rescue StandardError => _e
return false
end
instances.create_tags(tags:[key:"token-user", value: user])
true
rescue StandardError => _e
return false
end
# END BASE METHODS
@ -497,15 +493,8 @@ module Vmpooler
pool_configuration = pool_config(pool_name)
return nil if pool_configuration.nil?
domain_set = domain
name_to_use = if domain_set.nil?
vm_object.private_dns_name
else
vm_object.tags.detect { |f| f.key == 'Name' }&.value
end
{
'name' => name_to_use,
'name' => vm_object.tags.detect { |f| f.key == 'Name' }&.value,
# 'hostname' => vm_object.hostname,
'template' => pool_configuration&.key?('template') ? pool_configuration['template'] : nil, # was expecting to get it from API, not from config, but this is what vSphere does too!
'poolname' => vm_object.tags.detect { |f| f.key == 'pool' }&.value,
@ -513,7 +502,7 @@ module Vmpooler
'status' => vm_object.state&.name, # One of the following values: pending, running, shutting-down, terminated, stopping, stopped
# 'zone' => vm_object.zone,
'image_size' => vm_object.instance_type,
'ip' => vm_object.private_ip_address,
'ip' => vm_object.private_ip_address, # used by the cloud dns class to set the record to this value
'private_ip_address' => vm_object.private_ip_address,
'private_dns_name' => vm_object.private_dns_name
}