Skip to content

Commit

Permalink
(#5) Document how to use Pooler class for ruby scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
briancain committed Oct 2, 2015
1 parent 19d409b commit b82d775
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,70 @@ Here are the keys that vmfloaty currently supports:
## vmpooler API
This cli tool uses the [vmpooler API](https://github.com/puppetlabs/vmpooler/blob/master/API.md).
## Using the Pooler class
If you want to write some ruby scripts around the vmpooler api, vmfloaty provides a `Pooler` and `Auth` class to make things easier. The ruby script below shows off an example of a script that gets a token, grabs a vm, runs some commands through ssh, and then destroys the vm.

```ruby
require 'vmfloaty/pooler'
require 'vmfloaty/auth'
require 'io/console'
require 'net/ssh'
def aquire_token(verbose, url)
STDOUT.flush
puts "Enter username:"
user = $stdin.gets.chomp
puts "Enter password:"
password = STDIN.noecho(&:gets).chomp
token = Auth.get_token(verbose, url, user, password)
puts "Your token:\n#{token}"
token
end
def grab_vms(os_string, token, url, verbose)
response_body = Pooler.retrieve(verbose, os_string, token, url)
if response_body['ok'] == false
STDERR.puts "There was a problem with your request"
exit 1
end
response_body[os_string]
end
def run_puppet_on_host(hostname)
STDOUT.flush
puts "Enter 'root' password for vm:"
password = STDIN.noecho(&:gets).chomp
user = 'root'
# run puppet
run_puppet = "/opt/puppetlabs/puppet/bin/puppet agent -t"
begin
ssh = Net::SSH.start(hostname, user, :password => password)
output = ssh.exec!(run_puppet)
puts output
ssh.close
rescue
STDERR.puts "Unable to connect to #{hostname} using #{user}"
exit 1
end
end
if __FILE__ == $0
verbose = true
url = 'https://vmpooler.mycompany.net'
token = aquire_token(verbose, url)
os = ARGV[0]
hostname = grab_vm(os, token, url, verbose)
run_puppet_on_host(hostname)
end
```

```
ruby myscript.rb centos-7-x86_64
```

0 comments on commit b82d775

Please sign in to comment.