mirror of
https://github.com/puppetlabs/vmfloaty.git
synced 2026-01-26 05:28:40 -05:00
(#5) Document how to use Pooler class for ruby scripts
This commit is contained in:
parent
19d409b722
commit
b82d7756de
1 changed files with 67 additions and 0 deletions
67
README.md
67
README.md
|
|
@ -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
|
||||
```
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue