vmfloaty 0.2.15 → 0.2.16

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cf2985c45b997c54451e9d295f228d064f385c78
4
- data.tar.gz: 1b94780071b7258f34fda134c720b468fd8e909c
3
+ metadata.gz: 1a1ad61f6bedb9c15e1b21bc4f2dd15318cf9303
4
+ data.tar.gz: ab8a768ccf3e445dec61240b1e03ea50f2f14958
5
5
  SHA512:
6
- metadata.gz: 8e41473307c941b2aa4078457110036a35743a896ec26c658c431ee9edc7b3356c716aa6e0b282352056a291c2b557179f0f1931fe3f6180e16d48644abdbc32
7
- data.tar.gz: 31d3f725a8551c259134eb250ec7f148edb69320e12276775cb1d57e12f87ba5fa0a40fd62899c9c8886fcb4c1f7d5025fd4edb8aea3c63005432b563af18c08
6
+ metadata.gz: 5ed7675e01385ec0b5d1339fc5a5d3392ef28b5eae58ebd8cc26f9f4956e2679d0fc9af3267f58482cbf6b92acd5ecc1d0db8d1441e85be1141037fe9d23e0f3
7
+ data.tar.gz: 2bbbc795099999f7c0a076d748f42f7f757842822f15a7217892dbe68e2046fbb7847c79a9f62470498783042d915d92839c7801130af0914903835c0dd96cc2
data/lib/vmfloaty/auth.rb CHANGED
@@ -29,7 +29,7 @@ class Auth
29
29
  response = conn.delete "/token/#{token}"
30
30
  res_body = JSON.parse(response.body)
31
31
  if res_body["ok"]
32
- puts res_body
32
+ return res_body
33
33
  else
34
34
  STDERR.puts "There was a problem with your request:"
35
35
  puts res_body
@@ -49,7 +49,7 @@ class Auth
49
49
  res_body = JSON.parse(response.body)
50
50
 
51
51
  if res_body["ok"]
52
- res_body
52
+ return res_body
53
53
  else
54
54
  STDERR.puts "There was a problem with your request:"
55
55
  puts res_body
data/lib/vmfloaty/http.rb CHANGED
@@ -3,8 +3,7 @@ require 'faraday'
3
3
  class Http
4
4
  def self.get_conn(verbose, url)
5
5
  if url.nil?
6
- STDERR.puts "The url you provided was empty"
7
- exit 1
6
+ raise "Did not provide a url to connect to"
8
7
  end
9
8
 
10
9
  conn = Faraday.new(:url => url, :ssl => {:verify => false}) do |faraday|
@@ -18,13 +17,11 @@ class Http
18
17
 
19
18
  def self.get_conn_with_auth(verbose, url, user, password)
20
19
  if url.nil?
21
- STDERR.puts "The url you provided was empty"
22
- exit 1
20
+ raise "Did not provide a url to connect to"
23
21
  end
24
22
 
25
23
  if user.nil?
26
- STDERR.puts "You did not provide a user to authenticate with"
27
- exit 1
24
+ raise "You did not provide a user to authenticate with"
28
25
  end
29
26
 
30
27
  conn = Faraday.new(:url => url, :ssl => {:verify => false}) do |faraday|
@@ -34,8 +34,7 @@ class Pooler
34
34
  os_string = os_string.chomp("+")
35
35
 
36
36
  if os_string.size == 0
37
- STDERR.puts "No request was made, os hash specified no vms #{os_type}"
38
- exit 1
37
+ raise "No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs."
39
38
  end
40
39
 
41
40
  response = conn.post "/vm/#{os_string}"
@@ -44,9 +43,7 @@ class Pooler
44
43
  if res_body["ok"]
45
44
  res_body
46
45
  else
47
- STDERR.puts "There was a problem with your request"
48
- STDERR.puts res_body
49
- exit 1
46
+ raise "Failed to obtain VMs from the pooler at #{url}/vm/#{os_string}. #{res_body}"
50
47
  end
51
48
  end
52
49
 
@@ -85,7 +82,8 @@ class Pooler
85
82
  if res_body['ok']
86
83
  puts "Deletion for vm #{host} successfully scheduled"
87
84
  else
88
- STDERR.puts "There was a problem with your request for vm #{host}"
85
+ STDERR.puts "There was a problem with your request for vm #{host}."
86
+ STDERR.puts res_body
89
87
  end
90
88
  end
91
89
  end
@@ -0,0 +1,50 @@
1
+
2
+ require 'vmfloaty/pooler'
3
+
4
+ class Utils
5
+ # TODO: Takes the json response body from an HTTP GET
6
+ # request and "pretty prints" it
7
+ def self.format_hosts(hostname_hash)
8
+ host_hash = {}
9
+
10
+ hostname_hash.delete("ok")
11
+ hostname_hash.each do |type, hosts|
12
+ if type == "domain"
13
+ host_hash[type] = hosts
14
+ else
15
+ host_hash[type] = hosts["hostname"]
16
+ end
17
+ end
18
+
19
+ host_hash.to_json
20
+ end
21
+
22
+ def self.generate_os_hash(os_args)
23
+ # expects args to look like:
24
+ # ["centos", "debian=5", "windows=1"]
25
+
26
+ # Build vm hash where
27
+ #
28
+ # [operating_system_type1 -> total,
29
+ # operating_system_type2 -> total,
30
+ # ...]
31
+ os_types = {}
32
+ os_args.each do |arg|
33
+ os_arr = arg.split("=")
34
+ if os_arr.size == 1
35
+ # assume they didn't specify an = sign if split returns 1 size
36
+ os_types[os_arr[0]] = 1
37
+ else
38
+ os_types[os_arr[0]] = os_arr[1].to_i
39
+ end
40
+ end
41
+ os_types
42
+ end
43
+
44
+ def self.prettyprint_hosts(hosts)
45
+ puts "Running VMs:"
46
+ hosts.each do |vm|
47
+ puts "- #{vm}"
48
+ end
49
+ end
50
+ end
@@ -1,6 +1,6 @@
1
1
 
2
2
  class Version
3
- @version = '0.2.15'
3
+ @version = '0.2.16'
4
4
 
5
5
  def self.get
6
6
  @version
data/lib/vmfloaty.rb CHANGED
@@ -7,7 +7,7 @@ require 'vmfloaty/auth'
7
7
  require 'vmfloaty/pooler'
8
8
  require 'vmfloaty/version'
9
9
  require 'vmfloaty/conf'
10
- require 'vmfloaty/format'
10
+ require 'vmfloaty/utils'
11
11
 
12
12
  class Vmfloaty
13
13
  include Commander::Methods
@@ -22,7 +22,7 @@ class Vmfloaty
22
22
  c.syntax = 'floaty get os_type1=x ox_type2=y ...'
23
23
  c.summary = 'Gets a vm or vms based on the os flag'
24
24
  c.description = ''
25
- c.example 'Gets 3 vms', 'floaty get centos=3 debian=1 --user brian --url http://vmpooler.example.com'
25
+ c.example 'Gets 3 vms', 'floaty get centos=3 debian --user brian --url http://vmpooler.example.com'
26
26
  c.option '--verbose', 'Enables verbose output'
27
27
  c.option '--user STRING', String, 'User to authenticate with'
28
28
  c.option '--url STRING', String, 'URL of vmpooler'
@@ -33,40 +33,36 @@ class Vmfloaty
33
33
  token = options.token || config['token']
34
34
  user = options.user ||= config['user']
35
35
  url = options.url ||= config['url']
36
+ no_token = options.notoken
36
37
 
37
38
  if args.empty?
38
- STDERR.puts "You did not provide any vms to grab"
39
- end
40
-
41
- os_types = {}
42
- args.each do |arg|
43
- os_arr = arg.split("=")
44
- if os_arr.size == 1
45
- # assume they didn't specify an = sign if split returns 1 size
46
- os_types[os_arr[0]] = 1
47
- else
48
- os_types[os_arr[0]] = os_arr[1].to_i
49
- end
39
+ STDERR.puts "No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs."
40
+ exit 1
50
41
  end
51
42
 
52
- no_token = options.notoken
53
-
54
- if no_token
55
- response = Pooler.retrieve(verbose, os_types, nil, url)
56
- puts response
57
- return
58
- end
43
+ os_types = Utils.generate_os_hash(args)
59
44
 
60
- unless token
61
- pass = password "Enter your password please:", '*'
62
- token = Auth.get_token(verbose, url, user, pass)
63
- end
45
+ unless os_types.empty?
46
+ if no_token
47
+ response = Pooler.retrieve(verbose, os_types, nil, url)
48
+ puts response
49
+ exit 0
50
+ else
51
+ unless token
52
+ puts "No token found. Retrieving a token..."
53
+ pass = password "Enter your password please:", '*'
54
+ token = Auth.get_token(verbose, url, user, pass)
55
+ puts "\nToken retrieved!"
56
+ puts token
57
+ end
64
58
 
65
- unless os_types.nil?
66
- response = Pooler.retrieve(verbose, os_types, token, url)
67
- puts Format.get_hosts(response)
59
+ response = Pooler.retrieve(verbose, os_types, token, url)
60
+ puts Utils.format_hosts(response)
61
+ exit 0
62
+ end
68
63
  else
69
- puts 'You did not provide an OS to get'
64
+ STDERR.puts "No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs."
65
+ exit 1
70
66
  end
71
67
  end
72
68
  end
@@ -127,7 +123,7 @@ class Vmfloaty
127
123
  if modify_req["ok"]
128
124
  puts "Successfully modified vm #{hostname}."
129
125
  else
130
- STDERR.puts "Something went wrong with your request"
126
+ STDERR.puts "Could not modify given host #{hostname} at #{url}."
131
127
  puts modify_req
132
128
  exit 1
133
129
  end
@@ -163,10 +159,7 @@ class Vmfloaty
163
159
  running_vms = vms['running']
164
160
 
165
161
  if ! running_vms.nil?
166
- puts "Running VMs:"
167
- running_vms.each do |vm|
168
- puts "- #{vm}"
169
- end
162
+ Utils.prettyprint_hosts(running_vms)
170
163
  # query y/n
171
164
  puts ""
172
165
  ans = agree("Delete all VMs associated with token #{token}? [y/N]")
@@ -182,10 +175,11 @@ class Vmfloaty
182
175
  if hostnames.nil?
183
176
  STDERR.puts "You did not provide any hosts to delete"
184
177
  exit 1
178
+ else
179
+ hosts = hostnames.split(',')
180
+ Pooler.delete(verbose, url, hosts, token)
181
+ exit 0
185
182
  end
186
-
187
- hosts = hostnames.split(',')
188
- Pooler.delete(verbose, url, hosts, token)
189
183
  end
190
184
  end
191
185
 
@@ -280,12 +274,15 @@ class Vmfloaty
280
274
  case action
281
275
  when "get"
282
276
  pass = password "Enter your password please:", '*'
283
- puts Auth.get_token(verbose, url, user, pass)
277
+ token = Auth.get_token(verbose, url, user, pass)
278
+ puts token
284
279
  when "delete"
285
280
  pass = password "Enter your password please:", '*'
286
- Auth.delete_token(verbose, url, user, pass, token)
281
+ result = Auth.delete_token(verbose, url, user, pass, token)
282
+ puts result
287
283
  when "status"
288
- puts Auth.token_status(verbose, url, token)
284
+ status = Auth.token_status(verbose, url, token)
285
+ puts status
289
286
  when nil
290
287
  STDERR.puts "No action provided"
291
288
  else
@@ -33,7 +33,7 @@ describe Pooler do
33
33
  with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.9.1'}).
34
34
  to_return(:status => 200, :body => @delete_token_response, :headers => {})
35
35
 
36
- #expect(Auth.delete_token(false, @vmpooler_url, "first.last", "password", @token)).to eq @delete_token_response
36
+ expect(Auth.delete_token(false, @vmpooler_url, "first.last", "password", @token)).to eq JSON.parse(@delete_token_response)
37
37
  end
38
38
  end
39
39
 
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+ require 'json'
3
+ require_relative '../../lib/vmfloaty/utils'
4
+
5
+ describe Utils do
6
+
7
+ describe "#get_hosts" do
8
+ before :each do
9
+ @hostname_hash = "{\"ok\":true,\"debian-7-i386\":{\"hostname\":[\"sc0o4xqtodlul5w\",\"4m4dkhqiufnjmxy\"]},\"debian-7-x86_64\":{\"hostname\":\"zb91y9qbrbf6d3q\"},\"domain\":\"company.com\"}"
10
+ @format_hash = "{\"debian-7-i386\":[\"sc0o4xqtodlul5w\",\"4m4dkhqiufnjmxy\"],\"debian-7-x86_64\":\"zb91y9qbrbf6d3q\",\"domain\":\"company.com\"}"
11
+ end
12
+
13
+ it "formats a hostname hash into os, hostnames, and domain name" do
14
+
15
+ expect(Utils.format_hosts(JSON.parse(@hostname_hash))).to eq @format_hash
16
+ end
17
+ end
18
+
19
+ describe "#generate_os_hash" do
20
+ before :each do
21
+ @host_hash = {"centos"=>1, "debian"=>5, "windows"=>1}
22
+ end
23
+
24
+ it "takes an array of os arguments and returns a formatted hash" do
25
+ host_arg = ["centos", "debian=5", "windows=1"]
26
+ expect(Utils.generate_os_hash(host_arg)).to eq @host_hash
27
+ end
28
+
29
+ it "returns an empty hash if there are no arguments provided" do
30
+ host_arg = []
31
+ expect(Utils.generate_os_hash(host_arg)).to be_empty
32
+ end
33
+ end
34
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vmfloaty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.15
4
+ version: 0.2.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Cain
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-13 00:00:00.000000000 Z
11
+ date: 2015-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: commander
@@ -52,14 +52,14 @@ files:
52
52
  - lib/vmfloaty.rb
53
53
  - lib/vmfloaty/auth.rb
54
54
  - lib/vmfloaty/conf.rb
55
- - lib/vmfloaty/format.rb
56
55
  - lib/vmfloaty/http.rb
57
56
  - lib/vmfloaty/pooler.rb
57
+ - lib/vmfloaty/utils.rb
58
58
  - lib/vmfloaty/version.rb
59
59
  - spec/spec_helper.rb
60
60
  - spec/vmfloaty/auth_spec.rb
61
- - spec/vmfloaty/format_spec.rb
62
61
  - spec/vmfloaty/pooler_spec.rb
62
+ - spec/vmfloaty/utils_spec.rb
63
63
  homepage: https://github.com/briancain/vmfloaty
64
64
  licenses:
65
65
  - Apache
@@ -87,5 +87,5 @@ summary: CLI application to interface with vmpooler
87
87
  test_files:
88
88
  - spec/spec_helper.rb
89
89
  - spec/vmfloaty/auth_spec.rb
90
- - spec/vmfloaty/format_spec.rb
91
90
  - spec/vmfloaty/pooler_spec.rb
91
+ - spec/vmfloaty/utils_spec.rb
@@ -1,19 +0,0 @@
1
-
2
- class Format
3
- # TODO: Takes the json response body from an HTTP GET
4
- # request and "pretty prints" it
5
- def self.get_hosts(hostname_hash)
6
- host_hash = {}
7
-
8
- hostname_hash.delete("ok")
9
- hostname_hash.each do |type, hosts|
10
- if type == "domain"
11
- host_hash[type] = hosts
12
- else
13
- host_hash[type] = hosts["hostname"]
14
- end
15
- end
16
-
17
- host_hash.to_json
18
- end
19
- end
@@ -1,18 +0,0 @@
1
- require 'spec_helper'
2
- require 'json'
3
- require_relative '../../lib/vmfloaty/format'
4
-
5
- describe Pooler do
6
-
7
- describe "#get_hosts" do
8
- before :each do
9
- @hostname_hash = "{\"ok\":true,\"debian-7-i386\":{\"hostname\":[\"sc0o4xqtodlul5w\",\"4m4dkhqiufnjmxy\"]},\"debian-7-x86_64\":{\"hostname\":\"zb91y9qbrbf6d3q\"},\"domain\":\"company.com\"}"
10
- @format_hash = "{\"debian-7-i386\":[\"sc0o4xqtodlul5w\",\"4m4dkhqiufnjmxy\"],\"debian-7-x86_64\":\"zb91y9qbrbf6d3q\",\"domain\":\"company.com\"}"
11
- end
12
-
13
- it "formats a hostname hash into os, hostnames, and domain name" do
14
-
15
- expect(Format.get_hosts(JSON.parse(@hostname_hash))).to eq @format_hash
16
- end
17
- end
18
- end