(#28) Add new ssh command to vmfloaty

This commit adds a new feature to vmfloaty. It grabs a single vm from
the pooler based on the os template provided and then attempts to ssh
into it from the host machine.
This commit is contained in:
Brian Cain 2016-08-26 09:54:43 -07:00
parent 64a88106a0
commit 93e842a2aa
3 changed files with 93 additions and 2 deletions

View file

@ -8,6 +8,7 @@ require 'vmfloaty/pooler'
require 'vmfloaty/version'
require 'vmfloaty/conf'
require 'vmfloaty/utils'
require 'vmfloaty/ssh'
class Vmfloaty
include Commander::Methods
@ -50,6 +51,9 @@ class Vmfloaty
else
unless token
puts "No token found. Retrieving a token..."
if !user
raise "You did not provide a user to authenticate to vmpooler with"
end
pass = password "Enter your password please:", '*'
token = Auth.get_token(verbose, url, user, pass)
puts "\nToken retrieved!"
@ -345,6 +349,46 @@ class Vmfloaty
end
end
command :ssh do |c|
c.syntax = 'floaty ssh os_type'
c.summary = 'Grabs a single vm and sshs into it'
c.description = ''
c.example 'SSHs into a centos vm', 'floaty ssh centos7 --url https://vmpooler.example.com'
c.option '--verbose', 'Enables verbose output'
c.option '--url STRING', String, 'URL of vmpooler'
c.option '--user STRING', String, 'User to authenticate with'
c.option '--token STRING', String, 'Token for vmpooler'
c.option '--notoken', 'Makes a request without a token'
c.action do |args, options|
verbose = options.verbose || config['verbose']
url = options.url ||= config['url']
token = options.token ||= config['token']
user = options.user ||= config['user']
no_token = options.notoken
if args.empty?
STDERR.puts "No operating systems provided to obtain. See `floaty ssh --help` for more information on how to get VMs."
exit 1
end
host_os = args.first
if !no_token && !token
puts "No token found. Retrieving a token..."
if !user
raise "You did not provide a user to authenticate to vmpooler with"
end
pass = password "Enter your password please:", '*'
token = Auth.get_token(verbose, url, user, pass)
puts "\nToken retrieved!"
puts token
end
Ssh.ssh(verbose, host_os, token, url)
exit 0
end
end
run!
end
end

43
lib/vmfloaty/ssh.rb Normal file
View file

@ -0,0 +1,43 @@
class Ssh
def self.which(cmd)
# Gets path of executable for given command
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each { |ext|
exe = File.join(path, "#{cmd}#{ext}")
return exe if File.executable?(exe) && !File.directory?(exe)
}
end
return nil
end
def self.ssh(verbose, host_os, token, url)
ssh_path = which("ssh")
if !ssh_path
raise "Could not determine path to ssh"
end
os_types = {}
os_types[host_os] = 1
response = Pooler.retrieve(verbose, os_types, token, url)
if response["ok"] == true
if host_os =~ /win/
user = "Administrator"
else
user = "root"
end
hostname = "#{response[host_os]["hostname"]}.#{response["domain"]}"
cmd = "#{ssh_path} #{user}@#{hostname}"
# TODO: Should this respect more ssh settings? Can it be configured
# by users ssh config and does this respect those settings?
Kernel.exec(cmd)
else
raise "Could not get vm from vmpooler:\n #{response}"
end
return
end
end