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 # main configuration options
def region def region
return provider_config['region'] if provider_config['region'] provider_config['region'] if provider_config['region']
end end
# main configuration options, overridable for each pool # main configuration options, overridable for each pool
def zone(pool_name) def zone(pool_name)
return pool_config(pool_name)['zone'] if pool_config(pool_name)['zone'] 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 end
def amisize(pool_name) def amisize(pool_name)
return pool_config(pool_name)['amisize'] if pool_config(pool_name)['amisize'] 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 end
def volume_size(pool_name) def volume_size(pool_name)
return pool_config(pool_name)['volume_size'] if pool_config(pool_name)['volume_size'] 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 end
# dns # dns
@ -107,7 +107,7 @@ module Vmpooler
end end
def to_provision(pool_name) 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 end
# Base methods that are implemented: # Base methods that are implemented:
@ -271,7 +271,7 @@ module Vmpooler
created_instance = get_vm(pool_name, new_vmname) created_instance = get_vm(pool_name, new_vmname)
@redis.with_metrics do |redis| @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 end
# extra setup steps # 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 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) instance_hash = get_vm(pool_name, vm_name)
debug_logger("trigger delete_instance #{vm_name}") debug_logger("trigger delete_instance #{vm_name}")
# vm_hash = get_vm(pool_name, vm_name)
instances.terminate instances.terminate
begin begin
connection.client.wait_until(:instance_terminated, { instance_ids: [instances.id] }) 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 # 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 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) def vm_ready?(pool_name, vm_name)
begin begin
# TODO: we could use a healthcheck resource attached to instance
domain_set = domain domain_set = domain
if domain_set.nil? if domain_set.nil?
vm_ip = get_vm(pool_name, vm_name)['private_dns_name'] vm_ip = get_vm(pool_name, vm_name)['private_dns_name']
@ -428,20 +426,18 @@ module Vmpooler
vm_hash = get_vm(pool, vm_name) vm_hash = get_vm(pool, vm_name)
return false if vm_hash.nil? return false if vm_hash.nil?
new_labels = vm_hash['labels'] filters = [{
# bailing in this case since labels should exist, and continuing would mean losing them name: 'tag:vm_name',
return false if new_labels.nil? values: [vm_name]
}]
instances = connection.instances(filters: filters).first
return false if instances.nil?
# add new label called token-user, with value as user # add new label called token-user, with value as user
new_labels['token-user'] = user instances.create_tags(tags:[key:"token-user", value: 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
true true
rescue StandardError => _e
return false
end end
# END BASE METHODS # END BASE METHODS
@ -497,15 +493,8 @@ module Vmpooler
pool_configuration = pool_config(pool_name) pool_configuration = pool_config(pool_name)
return nil if pool_configuration.nil? 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, # '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! '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, '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 'status' => vm_object.state&.name, # One of the following values: pending, running, shutting-down, terminated, stopping, stopped
# 'zone' => vm_object.zone, # 'zone' => vm_object.zone,
'image_size' => vm_object.instance_type, '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_ip_address' => vm_object.private_ip_address,
'private_dns_name' => vm_object.private_dns_name 'private_dns_name' => vm_object.private_dns_name
} }

View file

@ -88,7 +88,8 @@ EOT
} }
skip 'gets a vm' do skip 'gets a vm' do
result = subject.create_vm(poolname, vmname) result = subject.create_vm(poolname, vmname)
result = subject.destroy_vm(poolname, vmname) subject.tag_vm_user(poolname, vmname)
#result = subject.destroy_vm(poolname, vmname)
#subject.vms_in_pool("amazon-6-x86_64-ec2") #subject.vms_in_pool("amazon-6-x86_64-ec2")
#subject.provision_node_aws("ip-10-227-4-97.amz-dev.puppet.net", poolname) #subject.provision_node_aws("ip-10-227-4-97.amz-dev.puppet.net", poolname)
# subject.create_snapshot(poolname, vmname, "foo") # subject.create_snapshot(poolname, vmname, "foo")
@ -160,7 +161,7 @@ EOT
context 'when VM exists but is missing information' do context 'when VM exists but is missing information' do
before(:each) do before(:each) do
tags = [ tags = [
MockTag.new(key: "name", value: vmname), MockTag.new(key: "Name", value: vmname),
MockTag.new(key: "vm_name", value: vmname) MockTag.new(key: "vm_name", value: vmname)
] ]
allow(connection).to receive(:instances).and_return([MockInstance.new(tags: tags)]) allow(connection).to receive(:instances).and_return([MockInstance.new(tags: tags)])
@ -170,7 +171,8 @@ EOT
expect(subject.get_vm(poolname, vmname)).to be_kind_of(Hash) expect(subject.get_vm(poolname, vmname)).to be_kind_of(Hash)
end end
it 'should return the VM name' do it 'should return the VM name when domain set' do
config[:providers][:ec2]['domain'] = "foobar.com"
result = subject.get_vm(poolname, vmname) result = subject.get_vm(poolname, vmname)
expect(result['name']).to eq(vmname) expect(result['name']).to eq(vmname)
@ -197,7 +199,7 @@ EOT
instance_type: "a1.large", instance_type: "a1.large",
private_ip_address: "1.1.1.1", private_ip_address: "1.1.1.1",
tags: [ tags: [
MockTag.new(key: "name", value: vmname), MockTag.new(key: "Name", value: vmname),
MockTag.new(key: "pool", value: poolname) MockTag.new(key: "pool", value: poolname)
] ]
) )