vmfloaty 0.8.2 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'faraday'
2
4
  require 'json'
3
5
  require 'vmfloaty/http'
@@ -7,46 +9,36 @@ class Auth
7
9
  def self.get_token(verbose, url, user, password)
8
10
  conn = Http.get_conn_with_auth(verbose, url, user, password)
9
11
 
10
- resp = conn.post "token"
12
+ resp = conn.post 'token'
11
13
 
12
14
  res_body = JSON.parse(resp.body)
13
- if res_body["ok"]
14
- return res_body["token"]
15
- else
16
- raise TokenError, "HTTP #{resp.status}: There was a problem requesting a token:\n#{res_body}"
17
- end
15
+ return res_body['token'] if res_body['ok']
16
+
17
+ raise TokenError, "HTTP #{resp.status}: There was a problem requesting a token:\n#{res_body}"
18
18
  end
19
19
 
20
20
  def self.delete_token(verbose, url, user, password, token)
21
- if token.nil?
22
- raise TokenError, 'You did not provide a token'
23
- end
21
+ raise TokenError, 'You did not provide a token' if token.nil?
24
22
 
25
23
  conn = Http.get_conn_with_auth(verbose, url, user, password)
26
24
 
27
25
  response = conn.delete "token/#{token}"
28
26
  res_body = JSON.parse(response.body)
29
- if res_body["ok"]
30
- return res_body
31
- else
32
- raise TokenError, "HTTP #{response.status}: There was a problem deleting a token:\n#{res_body}"
33
- end
27
+ return res_body if res_body['ok']
28
+
29
+ raise TokenError, "HTTP #{response.status}: There was a problem deleting a token:\n#{res_body}"
34
30
  end
35
31
 
36
32
  def self.token_status(verbose, url, token)
37
- if token.nil?
38
- raise TokenError, 'You did not provide a token'
39
- end
33
+ raise TokenError, 'You did not provide a token' if token.nil?
40
34
 
41
35
  conn = Http.get_conn(verbose, url)
42
36
 
43
37
  response = conn.get "token/#{token}"
44
38
  res_body = JSON.parse(response.body)
45
39
 
46
- if res_body["ok"]
47
- return res_body
48
- else
49
- raise TokenError, "HTTP #{response.status}: There was a problem getting the status of a token:\n#{res_body}"
50
- end
40
+ return res_body if res_body['ok']
41
+
42
+ raise TokenError, "HTTP #{response.status}: There was a problem getting the status of a token:\n#{res_body}"
51
43
  end
52
44
  end
@@ -1,12 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'yaml'
2
4
 
3
5
  class Conf
4
-
5
6
  def self.read_config
6
7
  conf = {}
7
8
  begin
8
9
  conf = YAML.load_file("#{Dir.home}/.vmfloaty.yml")
9
- rescue
10
+ rescue StandardError
10
11
  STDERR.puts "WARNING: There was no config file at #{Dir.home}/.vmfloaty.yml"
11
12
  end
12
13
  conf
@@ -1,23 +1,25 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class AuthError < StandardError
2
- def initialize(msg="Could not authenticate to pooler")
4
+ def initialize(msg = 'Could not authenticate to pooler')
3
5
  super
4
6
  end
5
7
  end
6
8
 
7
9
  class TokenError < StandardError
8
- def initialize(msg="Could not do operation with token provided")
10
+ def initialize(msg = 'Could not do operation with token provided')
9
11
  super
10
12
  end
11
13
  end
12
14
 
13
15
  class MissingParamError < StandardError
14
- def initialize(msg="Argument provided to function is missing")
16
+ def initialize(msg = 'Argument provided to function is missing')
15
17
  super
16
18
  end
17
19
  end
18
20
 
19
21
  class ModifyError < StandardError
20
- def initialize(msg="Could not modify VM")
22
+ def initialize(msg = 'Could not modify VM')
21
23
  super
22
24
  end
23
25
  end
@@ -1,60 +1,49 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'faraday'
2
4
  require 'uri'
3
5
 
4
6
  class Http
5
- def self.is_url(url)
7
+ def self.url?(url)
6
8
  # This method exists because it seems like Farady
7
9
  # has no handling around if a user gives us a URI
8
10
  # with no protocol on the beginning of the URL
9
11
 
10
12
  uri = URI.parse(url)
11
13
 
12
- if uri.kind_of?(URI::HTTP) or uri.kind_of?(URI::HTTPS)
13
- return true
14
- end
14
+ return true if uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
15
15
 
16
- return false
16
+ false
17
17
  end
18
18
 
19
19
  def self.get_conn(verbose, url)
20
- if url.nil?
21
- raise "Did not provide a url to connect to"
22
- end
20
+ raise 'Did not provide a url to connect to' if url.nil?
23
21
 
24
- unless is_url(url)
25
- url = "https://#{url}"
26
- end
22
+ url = "https://#{url}" unless url?(url)
27
23
 
28
- conn = Faraday.new(:url => url, :ssl => {:verify => false}) do |faraday|
24
+ conn = Faraday.new(:url => url, :ssl => { :verify => false }) do |faraday|
29
25
  faraday.request :url_encoded
30
26
  faraday.response :logger if verbose
31
27
  faraday.adapter Faraday.default_adapter
32
28
  end
33
29
 
34
- return conn
30
+ conn
35
31
  end
36
32
 
37
33
  def self.get_conn_with_auth(verbose, url, user, password)
38
- if url.nil?
39
- raise "Did not provide a url to connect to"
40
- end
34
+ raise 'Did not provide a url to connect to' if url.nil?
41
35
 
42
- if user.nil?
43
- raise "You did not provide a user to authenticate with"
44
- end
36
+ raise 'You did not provide a user to authenticate with' if user.nil?
45
37
 
46
- unless is_url(url)
47
- url = "https://#{url}"
48
- end
38
+ url = "https://#{url}" unless url?(url)
49
39
 
50
- conn = Faraday.new(:url => url, :ssl => {:verify => false}) do |faraday|
40
+ conn = Faraday.new(:url => url, :ssl => { :verify => false }) do |faraday|
51
41
  faraday.request :url_encoded
52
42
  faraday.request :basic_auth, user, password
53
43
  faraday.response :logger if verbose
54
44
  faraday.adapter Faraday.default_adapter
55
45
  end
56
46
 
57
- return conn
47
+ conn
58
48
  end
59
-
60
49
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'vmfloaty/errors'
2
4
  require 'vmfloaty/http'
3
5
  require 'faraday'
@@ -15,27 +17,17 @@ class NonstandardPooler
15
17
  os_filter ? os_list.select { |i| i[/#{os_filter}/] } : os_list
16
18
  end
17
19
 
18
- def self.list_active(verbose, url, token)
20
+ def self.list_active(verbose, url, token, _user)
19
21
  status = Auth.token_status(verbose, url, token)
20
22
  status['reserved_hosts'] || []
21
23
  end
22
24
 
23
- def self.retrieve(verbose, os_type, token, url)
25
+ def self.retrieve(verbose, os_type, token, url, _user, _options)
24
26
  conn = Http.get_conn(verbose, url)
25
27
  conn.headers['X-AUTH-TOKEN'] = token if token
26
28
 
27
- os_string = ''
28
- os_type.each do |os, num|
29
- num.times do |_i|
30
- os_string << os + '+'
31
- end
32
- end
33
-
34
- os_string = os_string.chomp('+')
35
-
36
- if os_string.empty?
37
- raise MissingParamError, 'No operating systems provided to obtain.'
38
- end
29
+ os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
30
+ raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?
39
31
 
40
32
  response = conn.post "host/#{os_string}"
41
33
 
@@ -51,14 +43,10 @@ class NonstandardPooler
51
43
  end
52
44
 
53
45
  def self.modify(verbose, url, hostname, token, modify_hash)
54
- if token.nil?
55
- raise TokenError, 'Token provided was nil; Request cannot be made to modify VM'
56
- end
46
+ raise TokenError, 'Token provided was nil; Request cannot be made to modify VM' if token.nil?
57
47
 
58
- modify_hash.each do |key, value|
59
- unless [:reason, :reserved_for_reason].include? key
60
- raise ModifyError, "Configured service type does not support modification of #{key}"
61
- end
48
+ modify_hash.each do |key, _value|
49
+ raise ModifyError, "Configured service type does not support modification of #{key}" unless %i[reason reserved_for_reason].include? key
62
50
  end
63
51
 
64
52
  if modify_hash[:reason]
@@ -77,22 +65,20 @@ class NonstandardPooler
77
65
  response.body.empty? ? {} : JSON.parse(response.body)
78
66
  end
79
67
 
80
- def self.disk(verbose, url, hostname, token, disk)
68
+ def self.disk(_verbose, _url, _hostname, _token, _disk)
81
69
  raise ModifyError, 'Configured service type does not support modification of disk space'
82
70
  end
83
71
 
84
- def self.snapshot(verbose, url, hostname, token)
72
+ def self.snapshot(_verbose, _url, _hostname, _token)
85
73
  raise ModifyError, 'Configured service type does not support snapshots'
86
74
  end
87
75
 
88
- def self.revert(verbose, url, hostname, token, snapshot_sha)
76
+ def self.revert(_verbose, _url, _hostname, _token, _snapshot_sha)
89
77
  raise ModifyError, 'Configured service type does not support snapshots'
90
78
  end
91
79
 
92
80
  def self.delete(verbose, url, hosts, token)
93
- if token.nil?
94
- raise TokenError, 'Token provided was nil; Request cannot be made to delete VM'
95
- end
81
+ raise TokenError, 'Token provided was nil; Request cannot be made to delete VM' if token.nil?
96
82
 
97
83
  conn = Http.get_conn(verbose, url)
98
84
 
@@ -100,9 +86,7 @@ class NonstandardPooler
100
86
 
101
87
  response_body = {}
102
88
 
103
- unless hosts.is_a? Array
104
- hosts = hosts.split(',')
105
- end
89
+ hosts = hosts.split(',') unless hosts.is_a? Array
106
90
  hosts.each do |host|
107
91
  response = conn.delete "host/#{host}"
108
92
  res_body = JSON.parse(response.body)
@@ -1,60 +1,48 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'faraday'
2
4
  require 'vmfloaty/http'
3
5
  require 'json'
4
6
  require 'vmfloaty/errors'
5
7
 
6
8
  class Pooler
7
- def self.list(verbose, url, os_filter=nil)
9
+ def self.list(verbose, url, os_filter = nil)
8
10
  conn = Http.get_conn(verbose, url)
9
11
 
10
12
  response = conn.get 'vm'
11
13
  response_body = JSON.parse(response.body)
12
14
 
13
- if os_filter
14
- hosts = response_body.select { |i| i[/#{os_filter}/] }
15
- else
16
- hosts = response_body
17
- end
15
+ hosts = if os_filter
16
+ response_body.select { |i| i[/#{os_filter}/] }
17
+ else
18
+ response_body
19
+ end
18
20
 
19
21
  hosts
20
22
  end
21
23
 
22
- def self.list_active(verbose, url, token)
24
+ def self.list_active(verbose, url, token, _user)
23
25
  status = Auth.token_status(verbose, url, token)
24
26
  vms = []
25
- if status[token] && status[token]['vms']
26
- vms = status[token]['vms']['running']
27
- end
27
+ vms = status[token]['vms']['running'] if status[token] && status[token]['vms']
28
28
  vms
29
29
  end
30
30
 
31
- def self.retrieve(verbose, os_type, token, url)
31
+ def self.retrieve(verbose, os_type, token, url, _user, _options)
32
32
  # NOTE:
33
33
  # Developers can use `Utils.generate_os_hash` to
34
34
  # generate the os_type param.
35
35
  conn = Http.get_conn(verbose, url)
36
- if token
37
- conn.headers['X-AUTH-TOKEN'] = token
38
- end
39
-
40
- os_string = ""
41
- os_type.each do |os,num|
42
- num.times do |i|
43
- os_string << os+"+"
44
- end
45
- end
46
-
47
- os_string = os_string.chomp("+")
36
+ conn.headers['X-AUTH-TOKEN'] = token if token
48
37
 
49
- if os_string.size == 0
50
- raise MissingParamError, "No operating systems provided to obtain."
51
- end
38
+ os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
39
+ raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?
52
40
 
53
41
  response = conn.post "vm/#{os_string}"
54
42
 
55
43
  res_body = JSON.parse(response.body)
56
44
 
57
- if res_body["ok"]
45
+ if res_body['ok']
58
46
  res_body
59
47
  elsif response.status == 401
60
48
  raise AuthError, "HTTP #{response.status}: The token provided could not authenticate to the pooler.\n#{res_body}"
@@ -64,14 +52,10 @@ class Pooler
64
52
  end
65
53
 
66
54
  def self.modify(verbose, url, hostname, token, modify_hash)
67
- if token.nil?
68
- raise TokenError, "Token provided was nil. Request cannot be made to modify vm"
69
- end
55
+ raise TokenError, 'Token provided was nil. Request cannot be made to modify vm' if token.nil?
70
56
 
71
57
  modify_hash.keys.each do |key|
72
- unless [:tags, :lifetime, :disk].include? key
73
- raise ModifyError, "Configured service type does not support modification of #{key}."
74
- end
58
+ raise ModifyError, "Configured service type does not support modification of #{key}." unless %i[tags lifetime disk].include? key
75
59
  end
76
60
 
77
61
  conn = Http.get_conn(verbose, url)
@@ -88,13 +72,18 @@ class Pooler
88
72
  end
89
73
 
90
74
  res_body = JSON.parse(response.body)
91
- res_body
75
+
76
+ if res_body['ok']
77
+ res_body
78
+ elsif response.status == 401
79
+ raise AuthError, "HTTP #{response.status}: The token provided could not authenticate to the pooler.\n#{res_body}"
80
+ else
81
+ raise ModifyError, "HTTP #{response.status}: Failed to modify VMs from the pooler vm/#{hostname}. #{res_body}"
82
+ end
92
83
  end
93
84
 
94
85
  def self.disk(verbose, url, hostname, token, disk)
95
- if token.nil?
96
- raise TokenError, "Token provided was nil. Request cannot be made to modify vm"
97
- end
86
+ raise TokenError, 'Token provided was nil. Request cannot be made to modify vm' if token.nil?
98
87
 
99
88
  conn = Http.get_conn(verbose, url)
100
89
  conn.headers['X-AUTH-TOKEN'] = token
@@ -106,15 +95,11 @@ class Pooler
106
95
  end
107
96
 
108
97
  def self.delete(verbose, url, hosts, token)
109
- if token.nil?
110
- raise TokenError, "Token provided was nil. Request cannot be made to delete vm"
111
- end
98
+ raise TokenError, 'Token provided was nil. Request cannot be made to delete vm' if token.nil?
112
99
 
113
100
  conn = Http.get_conn(verbose, url)
114
101
 
115
- if token
116
- conn.headers['X-AUTH-TOKEN'] = token
117
- end
102
+ conn.headers['X-AUTH-TOKEN'] = token if token
118
103
 
119
104
  response_body = {}
120
105
 
@@ -153,9 +138,7 @@ class Pooler
153
138
  end
154
139
 
155
140
  def self.snapshot(verbose, url, hostname, token)
156
- if token.nil?
157
- raise TokenError, "Token provided was nil. Request cannot be made to snapshot vm"
158
- end
141
+ raise TokenError, 'Token provided was nil. Request cannot be made to snapshot vm' if token.nil?
159
142
 
160
143
  conn = Http.get_conn(verbose, url)
161
144
  conn.headers['X-AUTH-TOKEN'] = token
@@ -166,16 +149,12 @@ class Pooler
166
149
  end
167
150
 
168
151
  def self.revert(verbose, url, hostname, token, snapshot_sha)
169
- if token.nil?
170
- raise TokenError, "Token provided was nil. Request cannot be made to revert vm"
171
- end
152
+ raise TokenError, 'Token provided was nil. Request cannot be made to revert vm' if token.nil?
172
153
 
173
154
  conn = Http.get_conn(verbose, url)
174
155
  conn.headers['X-AUTH-TOKEN'] = token
175
156
 
176
- if snapshot_sha.nil?
177
- raise "Snapshot SHA provided was nil, could not revert #{hostname}"
178
- end
157
+ raise "Snapshot SHA provided was nil, could not revert #{hostname}" if snapshot_sha.nil?
179
158
 
180
159
  response = conn.post "vm/#{hostname}/snapshot/#{snapshot_sha}"
181
160
  res_body = JSON.parse(response.body)
@@ -1,10 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'commander/user_interaction'
2
4
  require 'commander/command'
3
5
  require 'vmfloaty/utils'
4
6
  require 'vmfloaty/ssh'
5
7
 
6
8
  class Service
7
-
8
9
  attr_reader :config
9
10
 
10
11
  def initialize(options, config_hash = {})
@@ -13,14 +14,18 @@ class Service
13
14
  @service_object = Utils.get_service_object @config['type']
14
15
  end
15
16
 
16
- def method_missing(m, *args, &block)
17
- if @service_object.respond_to? m
18
- @service_object.send(m, *args, &block)
17
+ def method_missing(method_name, *args, &block)
18
+ if @service_object.respond_to?(method_name)
19
+ @service_object.send(method_name, *args, &block)
19
20
  else
20
21
  super
21
22
  end
22
23
  end
23
24
 
25
+ def respond_to_missing?(method_name, *)
26
+ @service_object.respond_to?(method_name) || super
27
+ end
28
+
24
29
  def url
25
30
  @config['url']
26
31
  end
@@ -31,7 +36,7 @@ class Service
31
36
 
32
37
  def user
33
38
  unless @config['user']
34
- puts "Enter your pooler service username:"
39
+ puts "Enter your #{@config['url']} service username:"
35
40
  @config['user'] = STDIN.gets.chomp
36
41
  end
37
42
  @config['user']
@@ -39,7 +44,7 @@ class Service
39
44
 
40
45
  def token
41
46
  unless @config['token']
42
- puts "No token found. Retrieving a token..."
47
+ puts 'No token found. Retrieving a token...'
43
48
  @config['token'] = get_new_token(nil)
44
49
  end
45
50
  @config['token']
@@ -47,13 +52,13 @@ class Service
47
52
 
48
53
  def get_new_token(verbose)
49
54
  username = user
50
- pass = Commander::UI::password "Enter your pooler service password:", '*'
55
+ pass = Commander::UI.password "Enter your #{@config['url']} service password:", '*'
51
56
  Auth.get_token(verbose, url, username, pass)
52
57
  end
53
58
 
54
59
  def delete_token(verbose, token_value = @config['token'])
55
60
  username = user
56
- pass = Commander::UI::password "Enter your pooler service password:", '*'
61
+ pass = Commander::UI.password "Enter your #{@config['url']} service password:", '*'
57
62
  Auth.delete_token(verbose, url, username, pass, token_value)
58
63
  end
59
64
 
@@ -67,13 +72,13 @@ class Service
67
72
  end
68
73
 
69
74
  def list_active(verbose)
70
- @service_object.list_active verbose, url, token
75
+ @service_object.list_active verbose, url, token, user
71
76
  end
72
77
 
73
78
  def retrieve(verbose, os_types, use_token = true)
74
79
  puts 'Requesting a vm without a token...' unless use_token
75
80
  token_value = use_token ? token : nil
76
- @service_object.retrieve verbose, os_types, token_value, url
81
+ @service_object.retrieve verbose, os_types, token_value, url, user, @config
77
82
  end
78
83
 
79
84
  def ssh(verbose, host_os, use_token = true)
@@ -91,9 +96,9 @@ class Service
91
96
 
92
97
  def pretty_print_running(verbose, hostnames = [])
93
98
  if hostnames.empty?
94
- puts "You have no running VMs."
99
+ puts 'You have no running VMs.'
95
100
  else
96
- puts "Running VMs:"
101
+ puts 'Running VMs:'
97
102
  @service_object.pretty_print_hosts(verbose, hostnames, url)
98
103
  end
99
104
  end
@@ -107,7 +112,7 @@ class Service
107
112
  end
108
113
 
109
114
  def delete(verbose, hosts)
110
- @service_object.delete verbose, url, hosts, token
115
+ @service_object.delete verbose, url, hosts, token, user
111
116
  end
112
117
 
113
118
  def status(verbose)
@@ -129,5 +134,4 @@ class Service
129
134
  def disk(verbose, hostname, disk)
130
135
  @service_object.disk(verbose, url, hostname, token, disk)
131
136
  end
132
-
133
- end
137
+ end