Skip to content

Commit 93e842a

Browse files
committed
(#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.
1 parent 64a8810 commit 93e842a

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ A CLI helper tool for [Puppet Labs vmpooler](https://github.com/puppetlabs/vmpoo
1212
Grab the latest from ruby gems...
1313

1414
```
15-
gem install vmfloaty
15+
$ gem install vmfloaty
16+
...
17+
...
18+
$ floaty --help
1619
```
1720

1821
## Usage
@@ -26,6 +29,7 @@ gem install vmfloaty
2629
query Get information about a given vm
2730
revert Reverts a vm to a specified snapshot
2831
snapshot Takes a snapshot of a given vm
32+
ssh Grabs a single vm and sshs into it
2933
status Prints the status of vmpooler
3034
summary Prints the summary of vmpooler
3135
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
6468

6569
```yaml
6670
#file at /Users/me/.vmfloaty.yml
67-
url: 'http://vmpooler.mycompany.net/api/v1'
71+
url: 'https://vmpooler.mycompany.net/api/v1'
6872
user: 'brian'
6973
token: 'tokenstring'
7074
```

lib/vmfloaty.rb

+44
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
require 'vmfloaty/version'
99
require 'vmfloaty/conf'
1010
require 'vmfloaty/utils'
11+
require 'vmfloaty/ssh'
1112

1213
class Vmfloaty
1314
include Commander::Methods
@@ -50,6 +51,9 @@ def run
5051
else
5152
unless token
5253
puts "No token found. Retrieving a token..."
54+
if !user
55+
raise "You did not provide a user to authenticate to vmpooler with"
56+
end
5357
pass = password "Enter your password please:", '*'
5458
token = Auth.get_token(verbose, url, user, pass)
5559
puts "\nToken retrieved!"
@@ -345,6 +349,46 @@ def run
345349
end
346350
end
347351

352+
command :ssh do |c|
353+
c.syntax = 'floaty ssh os_type'
354+
c.summary = 'Grabs a single vm and sshs into it'
355+
c.description = ''
356+
c.example 'SSHs into a centos vm', 'floaty ssh centos7 --url https://vmpooler.example.com'
357+
c.option '--verbose', 'Enables verbose output'
358+
c.option '--url STRING', String, 'URL of vmpooler'
359+
c.option '--user STRING', String, 'User to authenticate with'
360+
c.option '--token STRING', String, 'Token for vmpooler'
361+
c.option '--notoken', 'Makes a request without a token'
362+
c.action do |args, options|
363+
verbose = options.verbose || config['verbose']
364+
url = options.url ||= config['url']
365+
token = options.token ||= config['token']
366+
user = options.user ||= config['user']
367+
no_token = options.notoken
368+
369+
if args.empty?
370+
STDERR.puts "No operating systems provided to obtain. See `floaty ssh --help` for more information on how to get VMs."
371+
exit 1
372+
end
373+
374+
host_os = args.first
375+
376+
if !no_token && !token
377+
puts "No token found. Retrieving a token..."
378+
if !user
379+
raise "You did not provide a user to authenticate to vmpooler with"
380+
end
381+
pass = password "Enter your password please:", '*'
382+
token = Auth.get_token(verbose, url, user, pass)
383+
puts "\nToken retrieved!"
384+
puts token
385+
end
386+
387+
Ssh.ssh(verbose, host_os, token, url)
388+
exit 0
389+
end
390+
end
391+
348392
run!
349393
end
350394
end

lib/vmfloaty/ssh.rb

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Ssh
2+
3+
def self.which(cmd)
4+
# Gets path of executable for given command
5+
6+
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
7+
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
8+
exts.each { |ext|
9+
exe = File.join(path, "#{cmd}#{ext}")
10+
return exe if File.executable?(exe) && !File.directory?(exe)
11+
}
12+
end
13+
return nil
14+
end
15+
16+
def self.ssh(verbose, host_os, token, url)
17+
ssh_path = which("ssh")
18+
if !ssh_path
19+
raise "Could not determine path to ssh"
20+
end
21+
os_types = {}
22+
os_types[host_os] = 1
23+
24+
response = Pooler.retrieve(verbose, os_types, token, url)
25+
if response["ok"] == true
26+
if host_os =~ /win/
27+
user = "Administrator"
28+
else
29+
user = "root"
30+
end
31+
32+
hostname = "#{response[host_os]["hostname"]}.#{response["domain"]}"
33+
cmd = "#{ssh_path} #{user}@#{hostname}"
34+
35+
# TODO: Should this respect more ssh settings? Can it be configured
36+
# by users ssh config and does this respect those settings?
37+
Kernel.exec(cmd)
38+
else
39+
raise "Could not get vm from vmpooler:\n #{response}"
40+
end
41+
return
42+
end
43+
end

0 commit comments

Comments
 (0)