diff --git a/lib/vmpooler/providers/ec2.rb b/lib/vmpooler/providers/ec2.rb index b018119..33231ae 100644 --- a/lib/vmpooler/providers/ec2.rb +++ b/lib/vmpooler/providers/ec2.rb @@ -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 } diff --git a/spec/unit/providers/ec2_spec.rb b/spec/unit/providers/ec2_spec.rb index f657824..1125100 100644 --- a/spec/unit/providers/ec2_spec.rb +++ b/spec/unit/providers/ec2_spec.rb @@ -88,7 +88,8 @@ EOT } skip 'gets a vm' do 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.provision_node_aws("ip-10-227-4-97.amz-dev.puppet.net", poolname) # subject.create_snapshot(poolname, vmname, "foo") @@ -160,7 +161,7 @@ EOT context 'when VM exists but is missing information' do before(:each) do tags = [ - MockTag.new(key: "name", value: vmname), + MockTag.new(key: "Name", value: vmname), MockTag.new(key: "vm_name", value: vmname) ] 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) 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) expect(result['name']).to eq(vmname) @@ -197,7 +199,7 @@ EOT instance_type: "a1.large", private_ip_address: "1.1.1.1", tags: [ - MockTag.new(key: "name", value: vmname), + MockTag.new(key: "Name", value: vmname), MockTag.new(key: "pool", value: poolname) ] )