diff --git a/README.md b/README.md index 07ce638..65d4322 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,10 @@ A CLI helper tool for [Puppet Labs vmpooler](https://github.com/puppetlabs/vmpoo Grab the latest from ruby gems... ``` -gem install vmfloaty +$ gem install vmfloaty +... +... +$ floaty --help ``` ## Usage @@ -26,6 +29,7 @@ gem install vmfloaty query Get information about a given vm revert Reverts a vm to a specified snapshot snapshot Takes a snapshot of a given vm + ssh Grabs a single vm and sshs into it status Prints the status of vmpooler summary Prints the summary of vmpooler 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 #file at /Users/me/.vmfloaty.yml -url: 'http://vmpooler.mycompany.net/api/v1' +url: 'https://vmpooler.mycompany.net/api/v1' user: 'brian' token: 'tokenstring' ``` diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index 76759c2..c46f701 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -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 diff --git a/lib/vmfloaty/ssh.rb b/lib/vmfloaty/ssh.rb new file mode 100644 index 0000000..4ee4b8c --- /dev/null +++ b/lib/vmfloaty/ssh.rb @@ -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