|
1 |
| -require 'vmfloaty/cli' |
2 |
| -require 'vmfloaty/hosts' |
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +require 'rubygems' |
| 4 | +require 'commander' |
| 5 | +require 'yaml' |
| 6 | +require 'vmfloaty/auth' |
| 7 | +require 'vmfloaty/pooler' |
3 | 8 |
|
4 | 9 | class Vmfloaty
|
| 10 | + include Commander::Methods |
| 11 | + |
| 12 | + def run |
| 13 | + program :version, '0.2.0' |
| 14 | + program :description, 'A CLI helper tool for Puppet Labs vmpooler to help you stay afloat' |
| 15 | + |
| 16 | + config = read_config |
| 17 | + |
| 18 | + command :get do |c| |
| 19 | + c.syntax = 'floaty get [options]' |
| 20 | + c.summary = 'Gets a vm or vms based on the os flag' |
| 21 | + c.description = '' |
| 22 | + c.example 'Gets 3 vms', 'floaty get --user brian --url http://vmpooler.example.com --os centos,centos,debian' |
| 23 | + c.option '--verbose', 'Enables verbose output' |
| 24 | + c.option '--user STRING', String, 'User to authenticate with' |
| 25 | + c.option '--url STRING', String, 'URL of vmpooler' |
| 26 | + c.option '--token STRING', String, 'Token for vmpooler' |
| 27 | + c.option '--os STRING', String, 'Operating systems to retrieve' |
| 28 | + c.option '--notoken', 'Makes a request without a token' |
| 29 | + c.action do |args, options| |
| 30 | + verbose = options.verbose || config['verbose'] |
| 31 | + token = options.token || config['token'] |
| 32 | + user = options.user ||= config['user'] |
| 33 | + url = options.url ||= config['url'] |
| 34 | + os_types = options.os |
| 35 | + no_token = options.notoken |
| 36 | + |
| 37 | + unless no_token.nil? |
| 38 | + response = Pooler.retrieve(verbose, os_types, token, url) |
| 39 | + puts response |
| 40 | + return |
| 41 | + end |
| 42 | + |
| 43 | + unless options.token |
| 44 | + pass = password "Enter your password please:", '*' |
| 45 | + token = Auth.get_token(verbose, url, user, pass) |
| 46 | + end |
| 47 | + |
| 48 | + unless os_types.nil? |
| 49 | + response = Pooler.retrieve(verbose, os_types, token, url) |
| 50 | + puts response |
| 51 | + else |
| 52 | + puts 'You did not provide an OS to get' |
| 53 | + end |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + command :list do |c| |
| 58 | + c.syntax = 'floaty list [options]' |
| 59 | + c.summary = 'Shows a list of available vms from the pooler' |
| 60 | + c.description = '' |
| 61 | + c.example 'Filter the list on centos', 'floaty list --filter centos --url http://vmpooler.example.com' |
| 62 | + c.option '--verbose', 'Enables verbose output' |
| 63 | + c.option '--filter STRING', String, 'A filter to apply to the list' |
| 64 | + c.option '--url STRING', String, 'URL of vmpooler' |
| 65 | + c.action do |args, options| |
| 66 | + verbose = options.verbose || config['verbose'] |
| 67 | + filter = options.filter |
| 68 | + url = options.url ||= config['url'] |
| 69 | + |
| 70 | + os_list = Pooler.list(verbose, url, filter) |
| 71 | + puts os_list |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + command :query do |c| |
| 76 | + c.syntax = 'floaty query [options]' |
| 77 | + c.summary = 'Get information about a given vm' |
| 78 | + c.description = '' |
| 79 | + c.example 'Get information about a sample host', 'floaty query --url http://vmpooler.example.com --host myvmhost.example.com' |
| 80 | + c.option '--verbose', 'Enables verbose output' |
| 81 | + c.option '--url STRING', String, 'URL of vmpooler' |
| 82 | + c.option '--host STRING', String, 'Hostname to query' |
| 83 | + c.action do |args, options| |
| 84 | + verbose = options.verbose || config['verbose'] |
| 85 | + url = options.url ||= config['url'] |
| 86 | + hostname = options.hostname |
| 87 | + |
| 88 | + query = Pooler.query(verbose, url, hostname) |
| 89 | + puts query |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + command :modify do |c| |
| 94 | + c.syntax = 'floaty modify [options]' |
| 95 | + c.summary = 'Modify a vms tags and TTL' |
| 96 | + c.description = '' |
| 97 | + c.example 'description', 'command example' |
| 98 | + c.option '--verbose', 'Enables verbose output' |
| 99 | + c.option '--url STRING', String, 'URL of vmpooler' |
| 100 | + c.option '--token STRING', String, 'Token for vmpooler' |
| 101 | + c.option '--host STRING', String, 'Hostname to modify' |
| 102 | + c.option '--lifetime INT', Integer, 'VM TTL (Integer, in hours)' |
| 103 | + c.option '--tags HASH', Hash, 'free-form VM tagging' |
| 104 | + c.action do |args, options| |
| 105 | + verbose = options.verbose || config['verbose'] |
| 106 | + url = options.url ||= config['url'] |
| 107 | + hostname = options.hostname |
| 108 | + lifetime = options.lifetime |
| 109 | + tags = options.tags |
| 110 | + token = options.token |
| 111 | + |
| 112 | + res_body = Pooler.modify(verbose, url, hostname, token, lifetime, tags) |
| 113 | + puts res_body |
| 114 | + end |
| 115 | + end |
5 | 116 |
|
6 |
| - def initialize(env) |
7 |
| - $vmpooler_url = env['VMPOOLER_URL'] |
| 117 | + command :delete do |c| |
| 118 | + c.syntax = 'floaty delete [options]' |
| 119 | + c.summary = 'Schedules the deletion of a host or hosts' |
| 120 | + c.description = '' |
| 121 | + c.example 'Schedules the deletion of a host or hosts', 'floaty delete --hosts myhost1,myhost2 --url http://vmpooler.example.com' |
| 122 | + c.option '--verbose', 'Enables verbose output' |
| 123 | + c.option '--hosts STRING', String, 'Hostname(s) to delete' |
| 124 | + c.option '--url STRING', String, 'URL of vmpooler' |
| 125 | + c.action do |args, options| |
| 126 | + verbose = options.verbose || config['verbose'] |
| 127 | + hosts = options.hosts |
| 128 | + url = options.url ||= config['url'] |
8 | 129 |
|
9 |
| - unless $vmpooler_url |
10 |
| - $vmpooler_url = 'http://vcloud.delivery.puppetlabs.net' |
| 130 | + Pool.delete(verbose, url, hosts) |
| 131 | + end |
11 | 132 | end
|
| 133 | + |
| 134 | + command :snapshot do |c| |
| 135 | + c.syntax = 'floaty snapshot [options]' |
| 136 | + c.summary = 'Takes a snapshot of a given vm' |
| 137 | + c.description = '' |
| 138 | + c.example 'Takes a snapshot for a given host', 'floaty snapshot --url http://vmpooler.example.com --host myvm.example.com --token a9znth9dn01t416hrguu56ze37t790bl' |
| 139 | + c.option '--verbose', 'Enables verbose output' |
| 140 | + c.option '--url STRING', String, 'URL of vmpooler' |
| 141 | + c.option '--host STRING', String, 'Hostname to modify' |
| 142 | + c.option '--token STRING', String, 'Token for vmpooler' |
| 143 | + c.action do |args, options| |
| 144 | + verbose = options.verbose || config['verbose'] |
| 145 | + url = options.url ||= config['url'] |
| 146 | + hostname = options.hostname |
| 147 | + token = options.token |
| 148 | + |
| 149 | + res_body = Pooler.snapshot(verbose, url, hostname, token) |
| 150 | + puts res_body |
| 151 | + end |
| 152 | + end |
| 153 | + |
| 154 | + command :revert do |c| |
| 155 | + c.syntax = 'floaty revert [options]' |
| 156 | + c.summary = 'Reverts a vm to a specified snapshot' |
| 157 | + c.description = '' |
| 158 | + c.example 'Reverts to a snapshot for a given host', 'floaty revert --url http://vmpooler.example.com --host myvm.example.com --token a9znth9dn01t416hrguu56ze37t790bl --snapshot n4eb4kdtp7rwv4x158366vd9jhac8btq' |
| 159 | + c.option '--verbose', 'Enables verbose output' |
| 160 | + c.option '--url STRING', String, 'URL of vmpooler' |
| 161 | + c.option '--host STRING', String, 'Hostname to modify' |
| 162 | + c.option '--token STRING', String, 'Token for vmpooler' |
| 163 | + c.option '--snapshot STRING', String, 'SHA of snapshot' |
| 164 | + c.action do |args, options| |
| 165 | + verbose = options.verbose || config['verbose'] |
| 166 | + url = options.url ||= config['url'] |
| 167 | + hostname = options.hostname |
| 168 | + token = options.token |
| 169 | + snapshot_sha = options.snapshot |
| 170 | + |
| 171 | + res_body = Pooler.revert(verbose, url, hostname, token, snapshot_sha) |
| 172 | + puts res_body |
| 173 | + end |
| 174 | + end |
| 175 | + |
| 176 | + command :status do |c| |
| 177 | + c.syntax = 'floaty status [options]' |
| 178 | + c.summary = 'Prints the status of vmpooler' |
| 179 | + c.description = '' |
| 180 | + c.example 'Gets the current vmpooler status', 'floaty status --url http://vmpooler.example.com' |
| 181 | + c.option '--verbose', 'Enables verbose output' |
| 182 | + c.option '--url STRING', String, 'URL of vmpooler' |
| 183 | + c.action do |args, options| |
| 184 | + verbose = options.verbose || config['verbose'] |
| 185 | + url = options.url ||= config['url'] |
| 186 | + |
| 187 | + status = Pooler.status(verbose, url) |
| 188 | + puts status |
| 189 | + end |
| 190 | + end |
| 191 | + |
| 192 | + command :summary do |c| |
| 193 | + c.syntax = 'floaty summary [options]' |
| 194 | + c.summary = 'Prints the summary of vmpooler' |
| 195 | + c.description = '' |
| 196 | + c.example 'Gets the current day summary of vmpooler', 'floaty summary --url http://vmpooler.example.com' |
| 197 | + c.option '--verbose', 'Enables verbose output' |
| 198 | + c.option '--url STRING', String, 'URL of vmpooler' |
| 199 | + c.action do |args, options| |
| 200 | + verbose = options.verbose || config['verbose'] |
| 201 | + url = options.url ||= config['url'] |
| 202 | + |
| 203 | + summary = Pooler.summary(verbose, url) |
| 204 | + puts summary |
| 205 | + end |
| 206 | + end |
| 207 | + |
| 208 | + command :token do |c| |
| 209 | + c.syntax = 'floaty token [get | delete | status]' |
| 210 | + c.summary = 'Retrieves or deletes a token' |
| 211 | + c.description = '' |
| 212 | + c.example '', '' |
| 213 | + c.option '--verbose', 'Enables verbose output' |
| 214 | + c.option '--url STRING', String, 'URL of vmpooler' |
| 215 | + c.option '--user STRING', String, 'User to authenticate with' |
| 216 | + c.option '--token STRING', String, 'Token for vmpooler' |
| 217 | + c.action do |args, options| |
| 218 | + verbose = options.verbose || config['verbose'] |
| 219 | + action = args.first |
| 220 | + url = options.url ||= config['url'] |
| 221 | + token = options.token |
| 222 | + user = options.user ||= config['user'] |
| 223 | + pass = password "Enter your password please:", '*' |
| 224 | + |
| 225 | + case action |
| 226 | + when "get" |
| 227 | + puts Auth.get_token(verbose, url, user, pass) |
| 228 | + when "delete" |
| 229 | + Auth.delete_token(verbose, url, user, pass, token) |
| 230 | + when "status" |
| 231 | + Auth.token_status(verbose, url, user, pass, token) |
| 232 | + when nil |
| 233 | + STDERR.puts "No action provided" |
| 234 | + else |
| 235 | + STDERR.puts "Unknown action: #{action}" |
| 236 | + end |
| 237 | + end |
| 238 | + end |
| 239 | + |
| 240 | + run! |
12 | 241 | end
|
13 | 242 |
|
14 |
| - def start |
15 |
| - CLI.start(ARGV) |
| 243 | + def read_config |
| 244 | + conf = {} |
| 245 | + begin |
| 246 | + conf = YAML.load_file("#{Dir.home}/.vmfloaty.yml") |
| 247 | + rescue |
| 248 | + STDERR.puts "There was no config file at #{Dir.home}/.vmfloaty.yml" |
| 249 | + end |
| 250 | + conf |
16 | 251 | end
|
17 | 252 | end
|
0 commit comments