A CLI helper tool for Puppet vmpooler to help you stay afloat
Find a file
Brian Cain 1d0fc42c80 (#31) Improve how users can provide arguments to commands
Prior this commit, some commands did not accept some options as an
argument and instead only worked through a flag. This commit updates
that behavior to allow users to specify some options through arguments,
while leaving the ability to continue to specify those options through
flags. Command line arguments take precedence over flags. It also fixes
an issue where if a snapshot sha was nil, it would submit a request to
take a snapshot to vmpooler.
2016-09-17 16:07:21 -07:00
bin (#1) Update vmfloaty to use new vmpooler api 2015-09-05 16:17:45 -07:00
lib (#31) Improve how users can provide arguments to commands 2016-09-17 16:07:21 -07:00
spec (#31) Improve how users can provide arguments to commands 2016-09-17 16:07:21 -07:00
.gitignore Initial commit of vmfloaty 2014-10-28 22:05:33 -07:00
.travis.yml Add travis 2015-10-13 21:19:50 -07:00
Gemfile (maint) Pin webmock to 1.21.0 2016-06-03 10:45:34 -07:00
LICENSE Initial commit of vmfloaty 2014-10-28 22:05:33 -07:00
README.md (#28) Add new ssh command to vmfloaty 2016-08-26 10:01:08 -07:00
vmfloaty.gemspec (#31) Improve how users can provide arguments to commands 2016-09-17 16:07:21 -07:00
vmfloaty.yml.example (#19) Update vmfloaty to expect /api/v1 in URL for disk endpoint 2016-05-03 14:57:00 -07:00

vmfloaty

Gem Version Build Status

A CLI helper tool for Puppet Labs vmpooler to help you stay afloat.

Install

Grab the latest from ruby gems...

$ gem install vmfloaty
...
...
$ floaty --help

Usage

    delete   Schedules the deletion of a host or hosts
    get      Gets a vm or vms based on the os flag
    help     Display global or [command] help documentation
    list     Shows a list of available vms from the pooler
    modify   Modify a vms tags and TTL
    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

  GLOBAL OPTIONS:

    -h, --help
        Display help documentation

    -v, --version
        Display version information

    -t, --trace
        Display backtrace when an error occurs

Example workflow

Grabbing a token for authenticated pooler requests:

floaty token get --user username --url https://vmpooler.mycompany.net/api/v1

This command will then ask you to log in. If successful, it will return a token that you can save either in a dotfile or use with other cli commands.

Grabbing vms:

floaty get centos-7-x86_64=2 debian-7-x86_64=1 windows-10=3 --token mytokenstring --url https://vmpooler.mycompany.net/api/v1

vmfloaty dotfile

If you do not wish to continuely specify various config options with the cli, you can have a dotfile in your home directory for some defaults. For example:

#file at /Users/me/.vmfloaty.yml
url: 'https://vmpooler.mycompany.net/api/v1'
user: 'brian'
token: 'tokenstring'

Now vmfloaty will use those config files if no flag was specified.

Valid config keys

Here are the keys that vmfloaty currently supports:

  • verbose
    • Boolean
  • token
    • String
  • user
    • String
  • url
    • String

vmpooler API

This cli tool uses the vmpooler API.

Using the Pooler class

An example of an application using vmfloaty as a library can be seen in vagrant-vmpooler.

Scripting

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.

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/api/v1'
  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