vmfloaty 0.6.2 → 0.7.0

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: 1bb5fd73f29e77b581fadb37b382fceb4131497b
4
- data.tar.gz: c947c5941fd8d4f6b6aaaa4be3c320b5276b66f0
3
+ metadata.gz: 9bfa904ec8ca9ec10fa9c6c9571d5ebbb3a0647e
4
+ data.tar.gz: c6d347f9075dd4ecf5056ae39c4ff4f5f2b7e6e7
5
5
  SHA512:
6
- metadata.gz: 1c4cc430096ff5b521deac4a1f8c4ad70ef2739d3419f4dbe54fff3bf9da87e0f6d94c4b9cb629565ba025ba8238fe079351f3d7b31a0a4985b6b7c4433412d9
7
- data.tar.gz: 065e1253192e6f2c48caa3b152573c63cddfc36fe2190610403469e34ce43dd7252f09c86574df91fdec0cf7721da94d0c64c65d04b32d320cbf41a560d472ff
6
+ metadata.gz: 231d4ef21dc8ebb75b57511c4e4d33d8c77b2b0566027973c0a9e9cc790bc79a9bca532ab433b42600473e9811e867523a74927988524a11b4330542b639b85c
7
+ data.tar.gz: 88b32545eae35d0b49e366c5883052b20dcb38445c4ae534987b7de0bbb6246635ca5004c5cd8e095308692e0c817649655cf82aceab02b38a27bb380bb6d26f
data/lib/vmfloaty.rb CHANGED
@@ -45,24 +45,45 @@ class Vmfloaty
45
45
 
46
46
  unless os_types.empty?
47
47
  if no_token
48
- response = Pooler.retrieve(verbose, os_types, nil, url)
48
+ begin
49
+ response = Pooler.retrieve(verbose, os_types, nil, url)
50
+ rescue MissingParamError
51
+ STDERR.puts e
52
+ STDERR.puts "See `floaty get --help` for more information on how to get VMs."
53
+ rescue AuthError => e
54
+ STDERR.puts e
55
+ exit 1
56
+ end
49
57
  puts Utils.format_hosts(response)
50
58
  exit 0
51
59
  else
52
60
  unless token
53
61
  puts "No token found. Retrieving a token..."
54
62
  if !user
55
- raise "You did not provide a user to authenticate to vmpooler with"
63
+ STDERR.puts "You did not provide a user to authenticate to vmpooler with"
64
+ exit 1
56
65
  end
57
66
  pass = password "Enter your password please:", '*'
58
- token = Auth.get_token(verbose, url, user, pass)
59
- unless token.nil?
60
- puts "\nToken retrieved!"
61
- puts token
67
+ begin
68
+ token = Auth.get_token(verbose, url, user, pass)
69
+ rescue TokenError => e
70
+ STDERR.puts e
71
+ exit 1
62
72
  end
73
+
74
+ puts "\nToken retrieved!"
75
+ puts token
63
76
  end
64
77
 
65
- response = Pooler.retrieve(verbose, os_types, token, url)
78
+ begin
79
+ response = Pooler.retrieve(verbose, os_types, token, url)
80
+ rescue MissingParamError
81
+ STDERR.puts e
82
+ STDERR.puts "See `floaty get --help` for more information on how to get VMs."
83
+ rescue AuthError => e
84
+ STDERR.puts e
85
+ exit 1
86
+ end
66
87
  puts Utils.format_hosts(response)
67
88
  exit 0
68
89
  end
@@ -91,16 +112,18 @@ class Vmfloaty
91
112
 
92
113
  if active
93
114
  # list active vms
94
- status = Auth.token_status(verbose, url, token)
95
- unless status.nil?
96
- # print vms
97
- vms = status[token]['vms']
98
- if vms.nil?
99
- STDERR.puts "You have no running vms"
100
- exit 0
101
- end
102
- else
103
- STDERR.puts "Could not retrieve active vms"
115
+ begin
116
+ status = Auth.token_status(verbose, url, token)
117
+ rescue TokenError => e
118
+ STDERR.puts e
119
+ exit 1
120
+ end
121
+
122
+ # print vms
123
+ vms = status[token]['vms']
124
+ if vms.nil?
125
+ STDERR.puts "You have no running vms"
126
+ exit 0
104
127
  end
105
128
 
106
129
  running_vms = vms['running']
@@ -199,10 +222,10 @@ class Vmfloaty
199
222
 
200
223
  if delete_all
201
224
  # get vms with token
202
- status = Auth.token_status(verbose, url, token)
203
-
204
- if status.nil?
205
- STDERR.puts "Could not retrieve status with token"
225
+ begin
226
+ status = Auth.token_status(verbose, url, token)
227
+ rescue TokenError => e
228
+ STDERR.puts e
206
229
  exit 1
207
230
  end
208
231
 
@@ -348,27 +371,33 @@ class Vmfloaty
348
371
  case action
349
372
  when "get"
350
373
  pass = password "Enter your password please:", '*'
351
- token = Auth.get_token(verbose, url, user, pass)
352
- unless token.nil?
353
- puts token
354
- else
355
- STDERR.puts 'Could not make a request for a token'
374
+ begin
375
+ token = Auth.get_token(verbose, url, user, pass)
376
+ rescue TokenError => e
377
+ STDERR.puts e
378
+ exit 1
356
379
  end
380
+ puts token
381
+ exit 0
357
382
  when "delete"
358
383
  pass = password "Enter your password please:", '*'
359
- result = Auth.delete_token(verbose, url, user, pass, token)
360
- unless result.nil?
361
- puts result
362
- else
363
- STDERR.puts 'Could not make a request to delete a token'
384
+ begin
385
+ result = Auth.delete_token(verbose, url, user, pass, token)
386
+ rescue TokenError => e
387
+ STDERR.puts e
388
+ exit 1
364
389
  end
390
+ puts result
391
+ exit 0
365
392
  when "status"
366
- status = Auth.token_status(verbose, url, token)
367
- unless status.nil?
368
- puts status
369
- else
370
- STDERR.puts 'Could not make a request to get token status'
393
+ begin
394
+ status = Auth.token_status(verbose, url, token)
395
+ rescue TokenError => e
396
+ STDERR.puts e
397
+ exit 1
371
398
  end
399
+ puts status
400
+ exit 0
372
401
  when nil
373
402
  STDERR.puts "No action provided"
374
403
  else
@@ -404,15 +433,18 @@ class Vmfloaty
404
433
  if !no_token && !token
405
434
  puts "No token found. Retrieving a token..."
406
435
  if !user
407
- raise "You did not provide a user to authenticate to vmpooler with"
436
+ STDERR.puts "You did not provide a user to authenticate to vmpooler with"
437
+ exit 1
408
438
  end
409
439
  pass = password "Enter your password please:", '*'
410
- token = Auth.get_token(verbose, url, user, pass)
411
- unless token.nil?
440
+ begin
441
+ token = Auth.get_token(verbose, url, user, pass)
442
+ rescue TokenError => e
443
+ STDERR.puts e
444
+ STDERR.puts 'Could not get token...requesting vm without a token anyway...'
445
+ else
412
446
  puts "\nToken retrieved!"
413
447
  puts token
414
- else
415
- STDERR.puts 'Could not get token...requesting vm without a token anyway...'
416
448
  end
417
449
  end
418
450
 
data/lib/vmfloaty/auth.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'faraday'
2
2
  require 'json'
3
3
  require 'vmfloaty/http'
4
+ require 'vmfloaty/errors'
4
5
 
5
6
  class Auth
6
7
  def self.get_token(verbose, url, user, password)
@@ -12,15 +13,13 @@ class Auth
12
13
  if res_body["ok"]
13
14
  return res_body["token"]
14
15
  else
15
- STDERR.puts "There was a problem with your request:\n#{res_body}"
16
- return nil
16
+ raise TokenError, "HTTP #{resp.status}: There was a problem requesting a token:\n#{res_body}"
17
17
  end
18
18
  end
19
19
 
20
20
  def self.delete_token(verbose, url, user, password, token)
21
21
  if token.nil?
22
- STDERR.puts 'You did not provide a token'
23
- return nil
22
+ raise TokenError, 'You did not provide a token'
24
23
  end
25
24
 
26
25
  conn = Http.get_conn_with_auth(verbose, url, user, password)
@@ -30,15 +29,13 @@ class Auth
30
29
  if res_body["ok"]
31
30
  return res_body
32
31
  else
33
- STDERR.puts "There was a problem with your request:\n#{res_body}"
34
- return nil
32
+ raise TokenError, "HTTP #{response.status}: There was a problem deleting a token:\n#{res_body}"
35
33
  end
36
34
  end
37
35
 
38
36
  def self.token_status(verbose, url, token)
39
37
  if token.nil?
40
- STDERR.puts 'You did not provide a token'
41
- return nil
38
+ raise TokenError, 'You did not provide a token'
42
39
  end
43
40
 
44
41
  conn = Http.get_conn(verbose, url)
@@ -49,8 +46,7 @@ class Auth
49
46
  if res_body["ok"]
50
47
  return res_body
51
48
  else
52
- STDERR.puts "There was a problem with your request:\n#{res_body}"
53
- return nil
49
+ raise TokenError, "HTTP #{response.status}: There was a problem getting the status of a token:\n#{res_body}"
54
50
  end
55
51
  end
56
52
  end
@@ -0,0 +1,17 @@
1
+ class AuthError < StandardError
2
+ def initialize(msg="Could not authenticate to pooler")
3
+ super
4
+ end
5
+ end
6
+
7
+ class TokenError < StandardError
8
+ def initialize(msg="Could not do operation with token provided")
9
+ super
10
+ end
11
+ end
12
+
13
+ class MissingParamError < StandardError
14
+ def initialize(msg="Argument provided to function is missing")
15
+ super
16
+ end
17
+ end
@@ -1,6 +1,7 @@
1
1
  require 'faraday'
2
2
  require 'vmfloaty/http'
3
3
  require 'json'
4
+ require 'vmfloaty/errors'
4
5
 
5
6
  class Pooler
6
7
  def self.list(verbose, url, os_filter=nil)
@@ -19,6 +20,9 @@ class Pooler
19
20
  end
20
21
 
21
22
  def self.retrieve(verbose, os_type, token, url)
23
+ # NOTE:
24
+ # Developers can use `Utils.generate_os_hash` to
25
+ # generate the os_type param.
22
26
  conn = Http.get_conn(verbose, url)
23
27
  if token
24
28
  conn.headers['X-AUTH-TOKEN'] = token
@@ -34,16 +38,19 @@ class Pooler
34
38
  os_string = os_string.chomp("+")
35
39
 
36
40
  if os_string.size == 0
37
- raise "No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs."
41
+ raise MissingParamError, "No operating systems provided to obtain."
38
42
  end
39
43
 
40
44
  response = conn.post "vm/#{os_string}"
41
45
 
42
46
  res_body = JSON.parse(response.body)
47
+
43
48
  if res_body["ok"]
44
49
  res_body
50
+ elsif response.status == 401
51
+ raise AuthError, "HTTP #{response.status}: The token provided could not authenticate to the pooler.\n#{res_body}"
45
52
  else
46
- raise "Failed to obtain VMs from the pooler at #{url}/vm/#{os_string}. #{res_body}"
53
+ raise "HTTP #{response.status}: Failed to obtain VMs from the pooler at #{url}/vm/#{os_string}. #{res_body}"
47
54
  end
48
55
  end
49
56
 
@@ -1,6 +1,6 @@
1
1
 
2
2
  class Version
3
- @version = '0.6.2'
3
+ @version = '0.7.0'
4
4
 
5
5
  def self.get
6
6
  @version
@@ -20,6 +20,14 @@ describe Pooler do
20
20
  token = Auth.get_token(false, @vmpooler_url, "first.last", "password")
21
21
  expect(token).to eq @token
22
22
  end
23
+
24
+ it "raises a token error if something goes wrong" do
25
+ stub_request(:post, "https://first.last:password@vmpooler.example.com/token").
26
+ with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length'=>'0', 'User-Agent'=>'Faraday v0.9.2'}).
27
+ to_return(:status => 500, :body => "{\"ok\":false}", :headers => {})
28
+
29
+ expect{ Auth.get_token(false, @vmpooler_url, "first.last", "password") }.to raise_error(TokenError)
30
+ end
23
31
  end
24
32
 
25
33
  describe "#delete_token" do
@@ -35,6 +43,18 @@ describe Pooler do
35
43
 
36
44
  expect(Auth.delete_token(false, @vmpooler_url, "first.last", "password", @token)).to eq JSON.parse(@delete_token_response)
37
45
  end
46
+
47
+ it "raises a token error if something goes wrong" do
48
+ stub_request(:delete, "https://first.last:password@vmpooler.example.com/token/utpg2i2xswor6h8ttjhu3d47z53yy47y").
49
+ with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.9.2'}).
50
+ to_return(:status => 500, :body => "{\"ok\":false}", :headers => {})
51
+
52
+ expect{ Auth.delete_token(false, @vmpooler_url, "first.last", "password", @token) }.to raise_error(TokenError)
53
+ end
54
+
55
+ it "raises a token error if no token provided" do
56
+ expect{ Auth.delete_token(false, @vmpooler_url, "first.last", "password", nil) }.to raise_error(TokenError)
57
+ end
38
58
  end
39
59
 
40
60
  describe "#token_status" do
@@ -50,5 +70,17 @@ describe Pooler do
50
70
 
51
71
  expect(Auth.token_status(false, @vmpooler_url, @token)).to eq JSON.parse(@token_status_response)
52
72
  end
73
+
74
+ it "raises a token error if something goes wrong" do
75
+ stub_request(:get, "#{@vmpooler_url}/token/utpg2i2xswor6h8ttjhu3d47z53yy47y").
76
+ with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.9.2'}).
77
+ to_return(:status => 500, :body => "{\"ok\":false}", :headers => {})
78
+
79
+ expect{ Auth.token_status(false, @vmpooler_url, @token) }.to raise_error(TokenError)
80
+ end
81
+
82
+ it "raises a token error if no token provided" do
83
+ expect{ Auth.token_status(false, @vmpooler_url, nil) }.to raise_error(TokenError)
84
+ end
53
85
  end
54
86
  end
@@ -44,6 +44,16 @@ describe Pooler do
44
44
  @retrieve_response_body_double = "{\"ok\":true,\"debian-7-i386\":{\"hostname\":[\"sc0o4xqtodlul5w\",\"4m4dkhqiufnjmxy\"]},\"centos-7-x86_64\":{\"hostname\":\"zb91y9qbrbf6d3q\"}}"
45
45
  end
46
46
 
47
+ it "raises an AuthError if the token is invalid" do
48
+ stub_request(:post, "#{@vmpooler_url}/vm/debian-7-i386").
49
+ with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length'=>'0', 'User-Agent'=>'Faraday v0.9.2', 'X-Auth-Token'=>'mytokenfile'}).
50
+ to_return(:status => 401, :body => "{\"ok\":false}", :headers => {})
51
+
52
+ vm_hash = {}
53
+ vm_hash['debian-7-i386'] = 1
54
+ expect{ Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url) }.to raise_error(AuthError)
55
+ end
56
+
47
57
  it "retrieves a single vm with a token" do
48
58
  stub_request(:post, "#{@vmpooler_url}/vm/debian-7-i386").
49
59
  with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length'=>'0', 'User-Agent'=>'Faraday v0.9.2', 'X-Auth-Token'=>'mytokenfile'}).
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vmfloaty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Cain
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-19 00:00:00.000000000 Z
11
+ date: 2016-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: commander
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '4.3'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: faraday
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0.9'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0.9'
41
41
  description: A helper tool for vmpooler to help you stay afloat
@@ -52,6 +52,7 @@ files:
52
52
  - lib/vmfloaty.rb
53
53
  - lib/vmfloaty/auth.rb
54
54
  - lib/vmfloaty/conf.rb
55
+ - lib/vmfloaty/errors.rb
55
56
  - lib/vmfloaty/http.rb
56
57
  - lib/vmfloaty/pooler.rb
57
58
  - lib/vmfloaty/ssh.rb
@@ -71,17 +72,17 @@ require_paths:
71
72
  - lib
72
73
  required_ruby_version: !ruby/object:Gem::Requirement
73
74
  requirements:
74
- - - '>='
75
+ - - ">="
75
76
  - !ruby/object:Gem::Version
76
77
  version: '0'
77
78
  required_rubygems_version: !ruby/object:Gem::Requirement
78
79
  requirements:
79
- - - '>='
80
+ - - ">="
80
81
  - !ruby/object:Gem::Version
81
82
  version: '0'
82
83
  requirements: []
83
84
  rubyforge_project:
84
- rubygems_version: 2.4.8
85
+ rubygems_version: 2.4.5.1
85
86
  signing_key:
86
87
  specification_version: 4
87
88
  summary: CLI application to interface with vmpooler