Skip to content

Commit b82d775

Browse files
committed
(#5) Document how to use Pooler class for ruby scripts
1 parent 19d409b commit b82d775

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

README.md

+67
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,70 @@ Here are the keys that vmfloaty currently supports:
8686
## vmpooler API
8787
8888
This cli tool uses the [vmpooler API](https://github.com/puppetlabs/vmpooler/blob/master/API.md).
89+
90+
## Using the Pooler class
91+
92+
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.
93+
94+
```ruby
95+
require 'vmfloaty/pooler'
96+
require 'vmfloaty/auth'
97+
require 'io/console'
98+
require 'net/ssh'
99+
100+
def aquire_token(verbose, url)
101+
STDOUT.flush
102+
puts "Enter username:"
103+
user = $stdin.gets.chomp
104+
puts "Enter password:"
105+
password = STDIN.noecho(&:gets).chomp
106+
token = Auth.get_token(verbose, url, user, password)
107+
108+
puts "Your token:\n#{token}"
109+
token
110+
end
111+
112+
def grab_vms(os_string, token, url, verbose)
113+
response_body = Pooler.retrieve(verbose, os_string, token, url)
114+
115+
if response_body['ok'] == false
116+
STDERR.puts "There was a problem with your request"
117+
exit 1
118+
end
119+
120+
response_body[os_string]
121+
end
122+
123+
def run_puppet_on_host(hostname)
124+
STDOUT.flush
125+
puts "Enter 'root' password for vm:"
126+
password = STDIN.noecho(&:gets).chomp
127+
user = 'root'
128+
# run puppet
129+
run_puppet = "/opt/puppetlabs/puppet/bin/puppet agent -t"
130+
131+
begin
132+
ssh = Net::SSH.start(hostname, user, :password => password)
133+
output = ssh.exec!(run_puppet)
134+
puts output
135+
ssh.close
136+
rescue
137+
STDERR.puts "Unable to connect to #{hostname} using #{user}"
138+
exit 1
139+
end
140+
end
141+
142+
if __FILE__ == $0
143+
verbose = true
144+
url = 'https://vmpooler.mycompany.net'
145+
token = aquire_token(verbose, url)
146+
os = ARGV[0]
147+
148+
hostname = grab_vm(os, token, url, verbose)
149+
run_puppet_on_host(hostname)
150+
end
151+
```
152+
153+
```
154+
ruby myscript.rb centos-7-x86_64
155+
```

0 commit comments

Comments
 (0)