mirror of
https://github.com/puppetlabs/vmfloaty.git
synced 2026-01-26 05:28:40 -05:00
Add ondemand flag and api v2 support to floaty ssh
This commit is contained in:
parent
4103fdeccc
commit
667dacbcea
4 changed files with 115 additions and 34 deletions
|
|
@ -484,7 +484,7 @@ class Vmfloaty
|
||||||
|
|
||||||
FloatyLogger.info "Can't ssh to multiple hosts; Using #{host_os} only..." if args.length > 1
|
FloatyLogger.info "Can't ssh to multiple hosts; Using #{host_os} only..." if args.length > 1
|
||||||
|
|
||||||
service.ssh(verbose, host_os, use_token)
|
service.ssh(verbose, host_os, use_token, options.ondemand)
|
||||||
exit 0
|
exit 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -87,7 +87,7 @@ class Service
|
||||||
@service_object.wait_for_request verbose, requestid, url
|
@service_object.wait_for_request verbose, requestid, url
|
||||||
end
|
end
|
||||||
|
|
||||||
def ssh(verbose, host_os, use_token = true)
|
def ssh(verbose, host_os, use_token = true, ondemand = nil)
|
||||||
token_value = nil
|
token_value = nil
|
||||||
if use_token
|
if use_token
|
||||||
begin
|
begin
|
||||||
|
|
@ -97,7 +97,7 @@ class Service
|
||||||
FloatyLogger.info 'Could not get token... requesting vm without a token anyway...'
|
FloatyLogger.info 'Could not get token... requesting vm without a token anyway...'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Ssh.ssh(verbose, self, host_os, token_value)
|
Ssh.ssh(verbose, self, host_os, token_value, ondemand)
|
||||||
end
|
end
|
||||||
|
|
||||||
def query(verbose, hostname)
|
def query(verbose, hostname)
|
||||||
|
|
|
||||||
|
|
@ -14,27 +14,45 @@ class Ssh
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.command_string(verbose, service, host_os, use_token)
|
def self.command_string(verbose, service, host_os, use_token, ondemand = nil)
|
||||||
ssh_path = which('ssh')
|
ssh_path = which('ssh')
|
||||||
raise 'Could not determine path to ssh' unless ssh_path
|
raise 'Could not determine path to ssh' unless ssh_path
|
||||||
|
os_types = Utils.generate_os_hash([host_os])
|
||||||
os_types = {}
|
|
||||||
os_types[host_os] = 1
|
os_types[host_os] = 1
|
||||||
|
|
||||||
response = service.retrieve(verbose, os_types, use_token)
|
response = service.retrieve(verbose, os_types, use_token, ondemand)
|
||||||
raise "Could not get vm from #{service.type}:\n #{response}" unless response['ok']
|
raise "Could not get vm from #{service.type}:\n #{response}" unless response['ok']
|
||||||
|
|
||||||
user = /win/.match?(host_os) ? 'Administrator' : 'root'
|
user = /win/.match?(host_os) ? 'Administrator' : 'root'
|
||||||
|
|
||||||
|
if ondemand
|
||||||
|
requestid = response['request_id']
|
||||||
|
service.wait_for_request(verbose, requestid)
|
||||||
|
hosts = service.check_ondemandvm(verbose, requestid, service.url)
|
||||||
|
if hosts['domain'].nil?
|
||||||
|
hostname = hosts[host_os]['hostname']
|
||||||
|
hostname = hosts[host_os]['hostname'][0] if hosts[host_os]['hostname'].is_a?(Array)
|
||||||
|
else
|
||||||
|
# Provides backwards compatibility with VMPooler API v1
|
||||||
|
hostname = "#{hosts[host_os]['hostname']}.#{hosts['domain']}"
|
||||||
|
hostname = "#{hosts[host_os]['hostname'][0]}.#{hosts['domain']}" if hosts[host_os]['hostname'].is_a?(Array)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if response['domain'].nil?
|
||||||
hostname = response[host_os]['hostname']
|
hostname = response[host_os]['hostname']
|
||||||
hostname = response[host_os]['hostname'][0] if response[host_os]['hostname'].is_a?(Array)
|
hostname = response[host_os]['hostname'][0] if response[host_os]['hostname'].is_a?(Array)
|
||||||
hostname = "#{hostname}.#{response['domain']}" unless hostname.end_with?('puppetlabs.net')
|
else
|
||||||
|
# Provides backwards compatibility with VMPooler API v1
|
||||||
|
hostname = "#{response[host_os]['hostname']}.#{response['domain']}"
|
||||||
|
hostname = "#{response[host_os]['hostname'][0]}.#{response['domain']}" if response[host_os]['hostname'].is_a?(Array)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
"#{ssh_path} #{user}@#{hostname}"
|
"#{ssh_path} #{user}@#{hostname}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.ssh(verbose, service, host_os, use_token)
|
def self.ssh(verbose, service, host_os, use_token, ondemand)
|
||||||
cmd = command_string(verbose, service, host_os, use_token)
|
cmd = command_string(verbose, service, host_os, use_token, ondemand)
|
||||||
# TODO: Should this respect more ssh settings? Can it be configured
|
# TODO: Should this respect more ssh settings? Can it be configured
|
||||||
# by users ssh config and does this respect those settings?
|
# by users ssh config and does this respect those settings?
|
||||||
Kernel.exec(cmd)
|
Kernel.exec(cmd)
|
||||||
|
|
|
||||||
|
|
@ -4,24 +4,35 @@ require 'spec_helper'
|
||||||
require 'vmfloaty/ssh'
|
require 'vmfloaty/ssh'
|
||||||
|
|
||||||
class ServiceStub
|
class ServiceStub
|
||||||
def retrieve(_verbose, os_types, _use_token)
|
def retrieve(_verbose, os_types, _use_token, ondemand)
|
||||||
if os_types.keys[0] == 'abs_host_string'
|
if os_types.keys[0] == 'abs_host_string'
|
||||||
return {
|
return {
|
||||||
os_types.keys[0] => { 'hostname' => ['abs-hostname.delivery.puppetlabs.net'] },
|
os_types.keys[0] => { 'hostname' => ['abs-hostname.delivery.puppetlabs.net'] },
|
||||||
'ok' => true
|
'ok' => true
|
||||||
}
|
}
|
||||||
end
|
|
||||||
|
|
||||||
{
|
elsif os_types.keys[0] == 'vmpooler_api_v2_host_string'
|
||||||
os_types.keys[0] => { 'hostname' => 'vmpooler-hostname' },
|
return {
|
||||||
|
os_types.keys[0] => { 'hostname' => ['vmpooler-v2-hostname.delivery.puppetlabs.net'] },
|
||||||
|
'ok' => true
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
return {
|
||||||
|
os_types.keys[0] => { 'hostname' => 'vmpooler-v1-hostname' },
|
||||||
'domain' => 'delivery.puppetlabs.net',
|
'domain' => 'delivery.puppetlabs.net',
|
||||||
'ok' => true
|
'ok' => true
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def type
|
def type
|
||||||
return 'abs' if os_types == 'abs_host_string'
|
return 'abs' if os_types == 'abs_host_string'
|
||||||
return 'vmpooler' if os_types == 'vmpooler_host_string'
|
return 'vmpooler' if os_types == 'vmpooler_api_v1_host_string' || os_types == 'vmpooler_api_v2_host_string'
|
||||||
|
end
|
||||||
|
|
||||||
|
def wait_for_request(verbose, requestid)
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -29,6 +40,7 @@ describe Ssh do
|
||||||
before :each do
|
before :each do
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "for pooled requests" do
|
||||||
it 'gets a hostname string for abs' do
|
it 'gets a hostname string for abs' do
|
||||||
verbose = false
|
verbose = false
|
||||||
service = ServiceStub.new
|
service = ServiceStub.new
|
||||||
|
|
@ -38,12 +50,63 @@ describe Ssh do
|
||||||
expect(cmd).to match(/ssh root@abs-hostname.delivery.puppetlabs.net/)
|
expect(cmd).to match(/ssh root@abs-hostname.delivery.puppetlabs.net/)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'gets a hostname string for vmpooler' do
|
it 'gets a hostname string for vmpooler api v1' do
|
||||||
verbose = false
|
verbose = true
|
||||||
service = ServiceStub.new
|
service = ServiceStub.new
|
||||||
host_os = 'vmpooler_host_string'
|
host_os = 'vmpooler_api_v1_host_string'
|
||||||
use_token = false
|
use_token = false
|
||||||
cmd = Ssh.command_string(verbose, service, host_os, use_token)
|
cmd = Ssh.command_string(verbose, service, host_os, use_token)
|
||||||
expect(cmd).to match(/ssh root@vmpooler-hostname.delivery.puppetlabs.net/)
|
expect(cmd).to match(/ssh root@vmpooler-v1-hostname.delivery.puppetlabs.net/)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'gets a hostname string for vmpooler api v2' do
|
||||||
|
verbose = false
|
||||||
|
service = ServiceStub.new
|
||||||
|
host_os = 'vmpooler_api_v2_host_string'
|
||||||
|
use_token = false
|
||||||
|
cmd = Ssh.command_string(verbose, service, host_os, use_token)
|
||||||
|
expect(cmd).to match(/ssh root@vmpooler-v2-hostname.delivery.puppetlabs.net/)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "for ondemand requests" do
|
||||||
|
let(:service) { ServiceStub.new }
|
||||||
|
let(:url) { 'http://pooler.example.com' }
|
||||||
|
|
||||||
|
it 'gets a hostname string for abs' do
|
||||||
|
verbose = false
|
||||||
|
host_os = 'abs_host_string'
|
||||||
|
use_token = false
|
||||||
|
ondemand = true
|
||||||
|
response = {'abs_host_string' => { 'hostname' => ['abs-hostname.delivery.puppetlabs.net']}}
|
||||||
|
allow(service).to receive(:url)
|
||||||
|
allow(service).to receive(:check_ondemandvm).and_return(response)
|
||||||
|
cmd = Ssh.command_string(verbose, service, host_os, use_token, ondemand)
|
||||||
|
expect(cmd).to match(/ssh root@abs-hostname.delivery.puppetlabs.net/)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'gets a hostname string for abs' do
|
||||||
|
verbose = false
|
||||||
|
host_os = 'vmpooler_api_v1_host_string'
|
||||||
|
use_token = false
|
||||||
|
ondemand = true
|
||||||
|
response = {'vmpooler_api_v1_host_string' => { 'hostname' => ['vmpooler_api_v1_host_string.delivery.puppetlabs.net']}}
|
||||||
|
allow(service).to receive(:url)
|
||||||
|
allow(service).to receive(:check_ondemandvm).and_return(response)
|
||||||
|
cmd = Ssh.command_string(verbose, service, host_os, use_token, ondemand)
|
||||||
|
expect(cmd).to match(/ssh root@vmpooler_api_v1_host_string.delivery.puppetlabs.net/)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'gets a hostname string for abs' do
|
||||||
|
verbose = false
|
||||||
|
host_os = 'vmpooler_api_v2_host_string'
|
||||||
|
use_token = false
|
||||||
|
ondemand = true
|
||||||
|
response = {'vmpooler_api_v2_host_string' => { 'hostname' => ['vmpooler_api_v2_host_string.delivery.puppetlabs.net']}}
|
||||||
|
allow(service).to receive(:url)
|
||||||
|
allow(service).to receive(:check_ondemandvm).and_return(response)
|
||||||
|
cmd = Ssh.command_string(verbose, service, host_os, use_token, ondemand)
|
||||||
|
expect(cmd).to match(/ssh root@vmpooler_api_v2_host_string.delivery.puppetlabs.net/)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue