From 9c5a0d114be8f83211f57c01e27cfe2147d6d557 Mon Sep 17 00:00:00 2001 From: "kirby@puppetlabs.com" Date: Wed, 21 Aug 2019 12:43:06 -0700 Subject: [PATCH] (POOLER-142) Add running host to vm API data This change adds the running host for a VM to the API data available via /vm/hostname. Without this change the running host would be logged to vmpooler log, but not available any other way. Additionally, the data will specify if a machine has been migrated. Without this change parent host data for a vmpooler machine is not available via the vmpooler API. --- CHANGELOG.md | 3 ++- docs/API.md | 4 +++- lib/vmpooler/api/v1.rb | 4 ++++ lib/vmpooler/providers/vsphere.rb | 4 +++- spec/helpers.rb | 1 + spec/integration/api/v1/vm_spec.rb | 1 + spec/unit/providers/vsphere_spec.rb | 6 +++++- 7 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d7cc66..b2b2bd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,8 @@ git logs & PR history. # [Unreleased](https://github.com/puppetlabs/vmpooler/compare/0.6.3...master) ### Added -- Add capability to disable linked clones for vsphere provider +- Add capability to disable linked clones for vsphere provider (POOLER-147) +- Add running host to VM data returned from /vm/hostname (POOLER-142) # [0.6.3](https://github.com/puppetlabs/vmpooler/compare/0.6.2...0.6.3) diff --git a/docs/API.md b/docs/API.md index 30405da..560d4b1 100644 --- a/docs/API.md +++ b/docs/API.md @@ -226,7 +226,9 @@ $ curl --url vmpooler.example.com/api/v1/vm/pxpmtoonx7fiqg6 "user": "jdoe" }, "ip": "192.168.0.1", - "domain": "example.com" + "domain": "example.com", + "host": "host1.example.com", + "migrated": "true" } } ``` diff --git a/lib/vmpooler/api/v1.rb b/lib/vmpooler/api/v1.rb index 048e8a7..4b84e3e 100644 --- a/lib/vmpooler/api/v1.rb +++ b/lib/vmpooler/api/v1.rb @@ -829,6 +829,10 @@ module Vmpooler if config['domain'] result[params[:hostname]]['domain'] = config['domain'] end + + result[params[:hostname]]['host'] = rdata['host'] if rdata['host'] + result[params[:hostname]]['migrated'] = rdata['migrated'] if rdata['migrated'] + end JSON.pretty_generate(result) diff --git a/lib/vmpooler/providers/vsphere.rb b/lib/vmpooler/providers/vsphere.rb index 029c2de..7ad1f6d 100644 --- a/lib/vmpooler/providers/vsphere.rb +++ b/lib/vmpooler/providers/vsphere.rb @@ -939,6 +939,7 @@ module Vmpooler begin connection = ensured_vsphere_connection(pool_object) vm_hash = get_vm_details(pool_name, vm_name, connection) + $redis.hset("vmpooler__vm__#{vm_name}", 'host', vm_hash['host_name']) migration_limit = @config[:config]['migration_limit'] if @config[:config].key?('migration_limit') migration_count = $redis.scard('vmpooler__migration') if migration_enabled? @config @@ -965,9 +966,10 @@ module Vmpooler def migrate_vm_to_new_host(pool_name, vm_name, vm_hash, connection) $redis.sadd('vmpooler__migration', vm_name) target_host_name = select_next_host(pool_name, @provider_hosts, vm_hash['architecture']) + $redis.hset("vmpooler__vm__#{vm_name}", 'host', target_host_name) + $redis.hset("vmpooler__vm__#{vm_name}", 'migrated', true) target_host_object = find_host_by_dnsname(connection, target_host_name) finish = migrate_vm_and_record_timing(pool_name, vm_name, vm_hash, target_host_object, target_host_name) - #logger.log('s', "Provider_hosts is: #{provider.provider_hosts}") logger.log('s', "[>] [#{pool_name}] '#{vm_name}' migrated from #{vm_hash['host_name']} to #{target_host_name} in #{finish} seconds") ensure $redis.srem('vmpooler__migration', vm_name) diff --git a/spec/helpers.rb b/spec/helpers.rb index 77820f7..bd3c748 100644 --- a/spec/helpers.rb +++ b/spec/helpers.rb @@ -51,6 +51,7 @@ def create_running_vm(template, name, token = nil, user = nil) redis.sadd("vmpooler__running__#{template}", name) redis.hset("vmpooler__vm__#{name}", 'template', template) redis.hset("vmpooler__vm__#{name}", 'checkout', Time.now) + redis.hset("vmpooler__vm__#{name}", 'host', 'host1') end def create_pending_vm(template, name, token = nil) diff --git a/spec/integration/api/v1/vm_spec.rb b/spec/integration/api/v1/vm_spec.rb index de1b254..9bbe08a 100644 --- a/spec/integration/api/v1/vm_spec.rb +++ b/spec/integration/api/v1/vm_spec.rb @@ -53,6 +53,7 @@ describe Vmpooler::API::V1 do expect(response_body["end_time"]).to eq(current_time.to_datetime.rfc3339) expect(response_body["state"]).to eq("running") expect(response_body["ip"]).to eq("") + expect(response_body["host"]).to eq("host1") end end diff --git a/spec/unit/providers/vsphere_spec.rb b/spec/unit/providers/vsphere_spec.rb index 047acdd..77dfb0a 100644 --- a/spec/unit/providers/vsphere_spec.rb +++ b/spec/unit/providers/vsphere_spec.rb @@ -76,6 +76,7 @@ EOT let(:connection_options) {{}} let(:connection) { mock_RbVmomi_VIM_Connection(connection_options) } let(:vmname) { 'vm1' } + let(:redis) { MockRedis.new } subject { Vmpooler::PoolManager::Provider::VSphere.new(config, logger, metrics, 'vsphere', provider_options) } @@ -138,7 +139,6 @@ EOT let(:power_off_task) { mock_RbVmomi_VIM_Task() } let(:destroy_task) { mock_RbVmomi_VIM_Task() } let(:data_ttl) { 1 } - let(:redis) { MockRedis.new } let(:finish) { '0.00' } let(:now) { Time.now } @@ -2572,6 +2572,7 @@ EOT subject.connection_pool.with_metrics do |pool_object| expect(subject).to receive(:ensured_vsphere_connection).with(pool_object).and_return(connection) expect(subject).to receive(:get_vm_details).and_return(vm_details) + allow($redis).to receive(:hset) expect(subject).to receive(:run_select_hosts).with(poolname, {}) expect(subject).to receive(:vm_in_target?).and_return false expect(subject).to receive(:migration_enabled?).and_return true @@ -2601,6 +2602,7 @@ EOT subject.connection_pool.with_metrics do |pool_object| expect(subject).to receive(:ensured_vsphere_connection).with(pool_object).and_return(connection) expect(subject).to receive(:get_vm_details).and_return(vm_details) + expect($redis).to receive(:hset).with("vmpooler__vm__#{vmname}", 'host', parent_host) expect(subject).to receive(:run_select_hosts).with(poolname, {}) expect(subject).to receive(:vm_in_target?).and_return true expect(subject).to receive(:migration_enabled?).and_return true @@ -2690,6 +2692,8 @@ EOT it' migrates a vm' do expect($redis).to receive(:sadd).with('vmpooler__migration', vmname) expect(subject).to receive(:select_next_host).and_return(new_host) + expect($redis).to receive(:hset).with("vmpooler__vm__#{vmname}", 'host', new_host) + expect($redis).to receive(:hset).with("vmpooler__vm__#{vmname}", 'migrated', true) expect(subject).to receive(:find_host_by_dnsname).and_return(new_host_object) expect(subject).to receive(:migrate_vm_and_record_timing).and_return(format('%.2f', (Time.now - (Time.now - 15)))) expect(logger).to receive(:log).with('s', "[>] [#{poolname}] '#{vmname}' migrated from host1 to host2 in 15.00 seconds")