(#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

@ -12,7 +12,10 @@ A CLI helper tool for [Puppet Labs vmpooler](https://github.com/puppetlabs/vmpoo
Grab the latest from ruby gems... Grab the latest from ruby gems...
``` ```
gem install vmfloaty $ gem install vmfloaty
...
...
$ floaty --help
``` ```
## Usage ## Usage
@ -26,6 +29,7 @@ gem install vmfloaty
query Get information about a given vm query Get information about a given vm
revert Reverts a vm to a specified snapshot revert Reverts a vm to a specified snapshot
snapshot Takes a snapshot of a given vm snapshot Takes a snapshot of a given vm
ssh Grabs a single vm and sshs into it
status Prints the status of vmpooler status Prints the status of vmpooler
summary Prints the summary of vmpooler summary Prints the summary of vmpooler
token Retrieves or deletes a token token Retrieves or deletes a token
@ -64,7 +68,7 @@ If you do not wish to continuely specify various config options with the cli, yo
```yaml ```yaml
#file at /Users/me/.vmfloaty.yml #file at /Users/me/.vmfloaty.yml
url: 'http://vmpooler.mycompany.net/api/v1' url: 'https://vmpooler.mycompany.net/api/v1'
user: 'brian' user: 'brian'
token: 'tokenstring' token: 'tokenstring'
``` ```

View file

@ -8,6 +8,7 @@ require 'vmfloaty/pooler'
require 'vmfloaty/version' require 'vmfloaty/version'
require 'vmfloaty/conf' require 'vmfloaty/conf'
require 'vmfloaty/utils' require 'vmfloaty/utils'
require 'vmfloaty/ssh'
class Vmfloaty class Vmfloaty
include Commander::Methods include Commander::Methods
@ -50,6 +51,9 @@ class Vmfloaty
else else
unless token unless token
puts "No token found. Retrieving a 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:", '*' pass = password "Enter your password please:", '*'
token = Auth.get_token(verbose, url, user, pass) token = Auth.get_token(verbose, url, user, pass)
puts "\nToken retrieved!" puts "\nToken retrieved!"
@ -345,6 +349,46 @@ class Vmfloaty
end end
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! run!
end end
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