floatyhelper 1.0 → 2.0.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 15888ba1cfc729b4e52f5a0dd5c33e3c7873439e8c6c5422509c17ac264d0878
4
- data.tar.gz: bae08f4e75b2530ba10d9a028f2346a3c89e5706015fea4258e8f1fd118e2bb6
3
+ metadata.gz: ab5a427189944d8960bab3ac42f1def0053e19fbed5a76d23fe866bef85ea28d
4
+ data.tar.gz: acddc4079e86b93b58a3ac1f4df3163fa440595e5f499f130ad5babeaa42f0cd
5
5
  SHA512:
6
- metadata.gz: 888ebc61101ac904f1893d3698edf7adbfc41d31b34c722dbe3a21ac8c9aa538db6711f1485f2297251527787fbb4e13fb5bf1ae066cbe676ba461ac1c360743
7
- data.tar.gz: 36a956a699d35d51e7af96d6a646392dfb4871605194a924a7843031742b390da071ffdc5df147c7752eb2741473c7be9a146a2d702477b36ac070bd53c3935d
6
+ metadata.gz: 97a90ee999e59877c3125cd96611b3de3a5eead211800eb0f233e9e2a9951680583309b053b173a3cb402c2b8ed4f8e7f698fb44e5484c86520e1875f5820604
7
+ data.tar.gz: cbb27ccfe2310492034eb123a0bff8c7e39dc681091fc6bec444084a390c581c8e18d8d1b9ffd6a2729e1b591c0c1d52c93d7bb34bdf37e6c460f7d0e428bf6f
data/README.md CHANGED
@@ -1,28 +1,14 @@
1
1
  # Floatyhelper
2
2
 
3
- A CLI app for manipulating groups of VMs provisioned with vmpooler. Works on top of https://github.com/briancain/vmfloaty
3
+ A CLI app for manipulating groups of VMs provisioned with vmpooler. Works on top of https://github.com/puppetlabs/vmfloaty
4
4
 
5
- This was written to aid the installer and management team with testing various configurations of Puppet Enterprise. Groups of hosts can be added (either directly from a Beaker run from inside the pe_acceptance_tests folder it was run with, or manually with the --hosts flag) and given a "tag" to refer to the group of hosts by. These hosts can be snapshotted together, with these snapshots given a "snaptag" that can then be reverted to if needed.
5
+ This was written to aid Puppet development teams with testing various configurations of Puppet Enterprise or other Puppet products. Groups of hosts can be added (either directly from a Beaker run from inside the pe_acceptance_tests folder it was run with, or manually with the --hosts flag) and given a "tag" to refer to the group of hosts by. These hosts can be snapshotted together, with these snapshots given a "snaptag" that can then be reverted to if needed.
6
6
 
7
- NOTE: This requires that you have already set up your .vmfloaty file with the appropriate token. The app currently does not check that this is the case.
8
-
9
- This tool could use some specs and linting, probably.
7
+ The app will install vmfloaty for you and ensure your .vmfloaty.yml config file is correct. If no tokens are present in the file, it will request new tokens for you.
10
8
 
11
9
  ## Installation
12
10
 
13
- Add this line to your application's Gemfile:
14
-
15
- ```ruby
16
- gem 'floatyhelper'
17
- ```
18
-
19
- And then execute:
20
-
21
- $ bundle install
22
-
23
- Or install it yourself as:
24
-
25
- $ gem install floatyhelper
11
+ This app lives on RubyGems, so to install, simply run `gem install floatyhelper`
26
12
 
27
13
  ## Usage
28
14
 
@@ -31,21 +17,27 @@ floatyhelper <command> <options>
31
17
  ```
32
18
  The following commands are currently available:
33
19
 
34
- `floatyhelper tags` - List all currently tracked tags that specify groups of hosts
20
+ `floatyhelper config` - Lists, gets, or sets floatyhelper configuration settings.
21
+
22
+ `floatyhelper tags` - List all currently tracked tags that specify groups of hosts.
35
23
 
36
- `floatyhelper list` - List all VMs and their associated tags, and checks the remaining lifetime for each VM. If --nocheck is passed, lifetime checking will not occur.
24
+ `floatyhelper list` - List all VMs and their associated tags, and optionally checks the type and remaining lifetime for each VM.
37
25
 
38
- `floatyhelper snaplist <tag>` - List all of the snaptags associated with the given VM group tag
26
+ `floatyhelper check` - Same as `floatyhelper list --check`.
27
+
28
+ `floatyhelper snaplist <tag>` - List all of the snaptags associated with the given VM group tag.
39
29
 
40
30
  `floatyhelper showconfig` - Dumps the .floatyhelper.yaml file. Primarily used for debug purposes.
41
31
 
42
- `floatyhelper addhosts <tag> [--hosts HOST1,HOST2,...] [--file sutfile]` - Takes all hosts defined in a beaker sut.log file and adds them to the list of hosts floatyhelper is tracking. These hosts will be grouped under the given tag, so that this tag can be used to operate on the whole set of hosts at once. If `--file` is not specified, this assumes you are inside a pe_acceptance_tests folder and grabs the lsit of hosts from `log/latest/sut.log`. If --hosts is specified, it will add the given comma-separated list of hosts.
32
+ `floatyhelper addhosts <tag> [--hosts HOST1,HOST2,...] [--file sutfile]` - Takes all hosts defined in a beaker sut.log file and adds them to the list of hosts floatyhelper is tracking. These hosts will be grouped under the given tag, so that this tag can be used to operate on the whole set of hosts at once. If `--file` is not specified, this assumes you are inside a pe_acceptance_tests folder and grabs the list of hosts from `log/latest/sut.log`. If --hosts is specified, it will add the given comma-separated list of hosts.
33
+
34
+ `floatyhelper movehost <host> <tag>` - Moves a host between two different tags.
43
35
 
44
- `floatyhelper appendhosts <tag> [--hosts HOST1,HOST2,...] [--file sutfile]` - Similar to the addhosts command, but adds the given hosts to the list of hosts tracked under a given tag.
36
+ `floatyhelper destroy <tag> [--all] [--hosts HOSTS]` - Calls `floaty delete` on the vmpooler hosts with the given tag, or the given hosts, or all hosts managed by floatyhelper.
45
37
 
46
- `floatyhelper destroy <tag> [--all] [--hosts HOSTS]` - Calls `floaty delete` on the vmpooler hosts with the given tag.
38
+ `floatyhelper destroy_from_token` - This destroys all hosts tied to your vmpooler token. Note that this does not currently remove the hosts from floatyhelper management. This is mostly a helper for when you have been using `floaty` directly to request VMs and you have some that are not managed by floatyhelper.
47
39
 
48
- `floatyhelper increaselife <tag> [options]` - Increases the lifetime of the given host or tagged hosts. Defaults to 100 hours.
40
+ `floatyhelper increaselife <tag> [options]` - Increases the lifetime of the given host or tagged hosts. Defaults to the increaselife config setting value.
49
41
 
50
42
  `floatyhelper snapshot <host|tag> <snaptag>` - Snapshots the given VM or group of tagged hosts, and defines a string to use as a snaptag to refer to this group of snapshots.
51
43
 
@@ -53,13 +45,13 @@ The following commands are currently available:
53
45
 
54
46
  `floatyhelper refresh` - Removes tags that contain only expired VMs. You may pass the -y argument to run non-interactively.
55
47
 
56
- `floatyhelper getvm [platform] [tag]` - Request a VM from floaty, and add the host to the given tag. Defaults to centos-7-x86_64 if no platform is given, and defaults to "Unassigned" tag.
48
+ `floatyhelper getvm [platform] [tag]` - Request a VM from floaty, and add the host to the given tag. Defaults to centos-7-x86_64 if no platform is given, and defaults to "Unassigned" tag. If requesting a platform that is currently pooled, it will request it from vmpooler. Otherwise, it will request it from ABS (or if the --abs flag is used).
57
49
 
58
- `floatyhelper movehost <host> <tag>` - Moves the given host from its existing tag to the given tag
50
+ `floatyhelper checktokens` - Checks the tokens in .vmfloaty.yml and requests new ones if needed.
59
51
 
60
52
  ## Contributing
61
53
 
62
- Bug reports and pull requests are welcome on GitHub at https://github.com/nmburgan/floatyhelper.
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/puppetlabs/floatyhelper.
63
55
 
64
56
  To build locally,
65
57
  ```
@@ -68,19 +60,10 @@ bundle exec rake build
68
60
  ```
69
61
  This will create the gem in the `pkg` directory. You can then install it locally with `gem install`.
70
62
 
71
- ## License
63
+ Run specs with `bundle exec rake spec` and Rubocop with `bundle exec rake rubocop`. These will be run automatically by Travis on all PRs.
72
64
 
73
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
65
+ To release a new version, update lib/version.rb, then run `bundle exec build release`.
74
66
 
75
- ## Changelog
76
-
77
- ### 0.9
78
- * First release
67
+ ## License
79
68
 
80
- ### 1.0
81
- * Added refresh command
82
- * Added getvm command
83
- * Added movehost command
84
- * Added functionality to check for remaining VM lifetime with the list command
85
- * Added --nocheck option for list command to prevent checking VM lifetime
86
- * Tag automatically added if appendhosts is used with a tag that doesn't exist
69
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/lib/floatyhelper.rb CHANGED
@@ -3,10 +3,12 @@
3
3
  require 'rubygems'
4
4
  require 'commander'
5
5
 
6
- require 'floatyhelper/conf'
6
+ require 'floatyhelper/config'
7
7
  require 'floatyhelper/hosts'
8
+ require 'floatyhelper/floaty'
8
9
  require 'floatyhelper/groups'
9
10
  require 'floatyhelper/vm'
11
+ require 'floatyhelper/version'
10
12
  require 'colorize'
11
13
 
12
14
  class Floatyhelper
@@ -14,68 +16,143 @@ class Floatyhelper
14
16
 
15
17
  def run
16
18
  program :name, 'floatyhelper'
17
- program :version, '1.0.0'
18
- program :description, <<-EOT
19
- Commands for manipulating vmpooler VMs with floaty. Use the addhosts command from within a pe_acceptance_tests folder
20
- that has recently finished a run to add those hosts to the list of hosts managed by floatyhelper (or specify a sut.log
21
- file directly).
19
+ program :version, FloatyhelperVersion::VERSION
20
+ program :description, <<~EOT
21
+ Commands for manipulating vmpooler VMs with floaty. Use the addhosts command from within a pe_acceptance_tests folder
22
+ that has recently finished a run to add those hosts to the list of hosts managed by floatyhelper (or specify a sut.log
23
+ file directly).
22
24
  EOT
23
25
 
24
26
  default_command :help
25
27
 
28
+ command :config do |c|
29
+ c.syntax = 'floatyhelper config [get <setting>|set <setting> <value>|list]'
30
+ c.summary = 'Get, set, or list floatyhelper configuration options'
31
+ c.description = <<~EOT
32
+ This command will get the current value or set a new value for configuration options, or list all
33
+ available configuration options.
34
+ EOT
35
+ c.action do |args|
36
+ # Need to add type checking and value validation here at some point
37
+ valid_settings = Config::VALID_SETTINGS
38
+ action = args[0]
39
+ setting = args[1]
40
+ value = args[2]
41
+ case action
42
+ when 'get'
43
+ unless setting && valid_settings.keys.include?(setting)
44
+ puts "Please provide a valid config setting to get (floatyhelper get <setting>). For a list of valid settings, use 'floatyhelper config list'.".red
45
+ exit 1
46
+ end
47
+ value = Config.get_config_setting(setting) || ''
48
+ puts value
49
+ when 'set'
50
+ unless setting && value
51
+ puts 'Please provide a config setting and value to set (floatyhelper set <setting> <value>).'.red
52
+ exit 1
53
+ end
54
+ unless valid_settings.keys.include?(setting)
55
+ puts "Please provide a valid config setting to set (floatyhelper get <setting>). For a list of valid settings, use 'floatyhelper config list'.".red
56
+ exit 1
57
+ end
58
+ Config.set_config_setting(setting, value)
59
+ puts "'#{setting}' set to #{value}".green
60
+ when 'list'
61
+ valid_settings.each do |s,info|
62
+ puts "#{s}:"
63
+ puts " #{info['description']}"
64
+ puts " Default: #{info['default']}"
65
+ end
66
+ else
67
+ puts 'Please provide a valid action (get, set, or list) to the config command.'.red
68
+ exit 1
69
+ end
70
+ end
71
+ end
72
+
73
+
26
74
  command :tags do |c|
27
75
  c.syntax = 'floatyhelper tags'
28
76
  c.summary = 'List all VM group tags'
29
77
  c.description = 'This lists all of the VM group tags that are currently defined, added via the addhosts command.'
30
- c.action do |args|
31
- data = Conf.load_data
32
- if data['vms'].keys.length == 0
78
+ c.action do
79
+ tags = Groups.get_tags
80
+ if tags.length.zero?
33
81
  puts 'No VM groups are defined'.yellow
34
82
  else
35
- puts data['vms'].keys
83
+ puts tags
36
84
  end
37
85
  end
38
86
  end
39
87
 
40
88
  command :list do |c|
41
- c.syntax = 'floatyhelper list'
89
+ c.syntax = 'floatyhelper list [tag]'
42
90
  c.summary = 'List all VMs and their associated tags'
43
- c.description = 'This lists all VMs currently defined in the status.yaml file, added via the addhosts command, and their associated tags.'
44
- c.option '--nocheck', 'Do not check remaining lifetime of each VM'
91
+ c.description = <<~EOT
92
+ This lists all VMs currently defined in the status.yaml file, added via the addhosts command, and their associated tags.
93
+ If a tag is specified, it will only lists the hosts in that tag.
94
+ EOT
95
+ c.option '--check', 'Check remaining lifetime of each VM'
45
96
  c.action do |args, options|
46
- data = Conf.load_data
47
- if data['vms'].keys.length == 0
48
- puts 'No VM groups are defined'.yellow
49
- else
50
- data['vms'].each do |tag, hosts|
51
- puts "#{tag}:"
52
- hosts.each do |host|
53
- remaining = ''
54
- unless options.nocheck
55
- query = VM.query(host)
56
- remaining = VM.alive(host, query) ? "#{query[host]['remaining']} hours remaining" : "Expired"
57
- end
58
- puts " #{host}\t#{remaining}"
97
+ tags = Groups.get_tags
98
+ if tags.empty?
99
+ puts 'No hosts are currently managed by floatyhelper. To add a host, use the addhosts command.'.yellow
100
+ exit 0
101
+ end
102
+ requested_tag = args.count.positive? ? args[0] : nil
103
+ if requested_tag && !tags.include?(requested_tag)
104
+ puts "Tag #{requested_tag} not found".red
105
+ exit 1
106
+ end
107
+ tags_to_fetch = requested_tag ? [requested_tag] : tags
108
+ tags_to_fetch.each do |tag|
109
+ puts "#{tag}:"
110
+ hosts = Groups.get_hosts(tag)
111
+ hosts.each do |host|
112
+ remaining = ''
113
+ type = ''
114
+ if options.check
115
+ query = VM.query(host)
116
+ type = query[host]['template'].gsub('-pixa4','')
117
+ remaining = VM.alive(host, query) ? "#{query[host]['remaining']} hours remaining" : 'Expired'
59
118
  end
119
+ puts ' %-15s %-28s %s' % [host, type, remaining]
60
120
  end
61
121
  end
62
122
  end
63
123
  end
64
124
 
125
+ command :check do |c|
126
+ c.syntax = 'floatyhelper check [tag]'
127
+ c.summary = 'Checks the remaining lifetime of each tracked VM.'
128
+ c.description = <<~EOT
129
+ Same as floatyhelper list --check. Checks the remaining lifetime of each tracked VM.
130
+ If a tag is specified, it will only check hosts in that tag.
131
+ EOT
132
+ c.action do |args|
133
+ command(:list).run(*args,'--check')
134
+ end
135
+ end
136
+
65
137
  command :snaplist do |c|
66
138
  c.syntax = 'floatyhelper snaplist <tag>'
67
139
  c.summary = 'List all snaptags saved for the given tag'
68
140
  c.description = 'Lists all of the snaptags available for the given VM group tag'
69
141
  c.action do |args|
70
- if args.length == 0
142
+ if args.empty?
71
143
  puts 'Please provide a tag'.red
72
144
  exit 1
73
145
  end
74
- data = Conf.load_data
75
- if !data['snapshots'].keys.include?(args[0])
76
- puts "Could not find any snaptags for VM group tag #{args[0]}".red
146
+ tag = args[0]
147
+ if !Groups.tag?(tag)
148
+ puts "#{tag} is not a valid VM group tag.".red
149
+ exit 1
150
+ end
151
+ snaptags = VM.snaplist(tag)
152
+ if snaptags.empty?
153
+ puts "Could not find any snaptags for VM group tag #{tag}".yellow
77
154
  else
78
- data['snapshots'][args[0]].keys.each { |snaptag| puts snaptag }
155
+ snaptags.each { |snaptag| puts snaptag }
79
156
  end
80
157
  end
81
158
  end
@@ -84,74 +161,46 @@ class Floatyhelper
84
161
  c.syntax = 'floatyhelper showconfig'
85
162
  c.summary = 'Show the .floatyhelper.yaml file'
86
163
  c.description = 'This prints the raw contents of the .floatyhelper.yaml file. Mostly just for debug purposes.'
87
- c.action do |args|
88
- puts File.read(Conf::STATUSFILE)
164
+ c.action do
165
+ puts File.read(Config.fhfile)
89
166
  end
90
167
  end
91
168
 
92
169
  command :addhosts do |c|
93
170
  c.syntax = 'floatyhelper addhosts <tag> [--hosts HOST1,HOST2,...] [--file sutfile]'
94
171
  c.summary = 'Adds hosts to list of hosts floatyhelper is tracking, with the given tag'
95
- c.description = <<-EOT
96
- This takes all of the hosts defined in a beaker sut.log file, and adds them to the list of hosts floatyhelper is tracking.
97
- These hosts will be grouped under the given tag, so that this tag can be used to operate on the whole set of hosts at once.
98
- If --file is not specified, this assumes you are inside a pe_acceptance_tests folder and grabs the list of hosts
99
- from log/latest/sut.log. If --hosts is specified, it will add the given comma-separated list of hosts.'
172
+ c.description = <<~EOT
173
+ This takes all of the hosts defined in a beaker sut.log file, and adds them to the list of hosts floatyhelper is tracking.
174
+ These hosts will be grouped under the given tag, so that this tag can be used to operate on the whole set of hosts at once.
175
+ If --file is not specified, this assumes you are inside a pe_acceptance_tests folder and grabs the list of hosts
176
+ from log/latest/sut.log. If --hosts is specified, it will add the given comma-separated list of hosts. If the tag already exists,
177
+ it will append the given hosts to the given tag.'
100
178
  EOT
101
179
  c.option '--hosts HOSTS', String, 'Comma separated list of hosts to add instead of reading sut.log'
102
180
  c.option '--file SUTFILE', String, 'Path to a sut.log file to get hosts from'
103
181
  c.action do |args, options|
104
182
  disallowed_tags = ['all','All','Unassigned']
105
- if args.size == 0
183
+ if args.empty?
106
184
  puts 'Please add a tag to use.'.red
107
- elsif disallowed_tags.include?(args[0])
108
- puts "Can not use a tag named #{args[0]}".red
109
- else
110
- tag = args[0]
111
- if options.hosts
112
- hosts = Hosts.get_options_hosts(options.hosts)
113
- else
114
- sutlog = options.file ? options.file : "#{Dir.pwd}/log/latest/sut.log"
115
- hosts = Hosts.get_hosts_from_sut_log(sutlog)
116
- end
117
- Groups.addhosts(hosts, tag)
118
- VM.increaselife(tag, nil)
119
- puts "Added the following hosts with tag #{tag}:".green
120
- hosts.each { |host| puts host }
185
+ exit 1
186
+ end
187
+ tag = args[0]
188
+ if disallowed_tags.include?(tag)
189
+ puts "Can not use a tag named #{tag}".red
190
+ exit 1
121
191
  end
122
- end
123
- end
124
192
 
125
- command :appendhosts do |c|
126
- c.syntax = 'floatyhelper appendhosts <tag> [--hosts HOST1,HOST2,...] [--file sutfile]'
127
- c.summary = 'Adds additional hosts to the list of hosts floatyhelper is tracking under the given tag'
128
- c.description = <<-EOT
129
- This is similar to the addhosts command, but adds the given hosts to the list of hosts tracked under a given
130
- tag. As with the addhosts command, you may specify the --hosts flag to add a hostname directly, provide a
131
- beaker sut.log file with the --file flag, or simply be inside a pe_acceptance_tests folder with the sut.log
132
- file under log/latest. Note that this will probably break things if you try reverting a snapshot taken before
133
- the hosts were appended. So don't do that.
134
- EOT
135
- c.option '--hosts HOSTS', String, 'Comma separated list of hosts to add instead of reading sut.log'
136
- c.option '--file SUTFILE', String, 'Path to a sut.log file to get hosts from'
137
- c.action do |args, options|
138
- if args.size == 0
139
- puts 'Please add a tag to use.'.red
140
- elsif !Groups.is_tag?(args[0])
141
- puts "Could not find tag #{args[0]}".red
193
+ if options.hosts
194
+ hosts = Hosts.get_options_hosts(options.hosts)
142
195
  else
143
- tag = args[0]
144
- if options.hosts
145
- hosts = Hosts.get_options_hosts(options.hosts)
146
- else
147
- sutlog = options.file ? options.file : "#{Dir.pwd}/log/latest/sut.log"
148
- hosts = Hosts.get_hosts_from_sut_log(sutlog)
149
- end
150
- Groups.appendhosts(hosts, tag)
151
- VM.increaselife(hosts, nil)
152
- puts "Added the following hosts to tag #{tag}".green
153
- hosts.each { |host| puts host }
196
+ sutlog = options.file || "#{Dir.pwd}/log/latest/sut.log"
197
+ hosts = Hosts.get_hosts_from_sut_log(sutlog)
154
198
  end
199
+ Groups.addhosts(hosts, tag)
200
+ amount = Config.get_config_setting('increaselife').to_i
201
+ VM.increaselife(tag, amount)
202
+ puts "Added the following hosts with tag #{tag}:".green
203
+ hosts.each { |host| puts host }
155
204
  end
156
205
  end
157
206
 
@@ -162,13 +211,18 @@ class Floatyhelper
162
211
  c.action do |args|
163
212
  if args.size < 2
164
213
  puts 'Please specify both a host and a tag.'.red
165
- elsif !Groups.is_managed_host?(args[0])
166
- puts "Could not find host #{args[0]}".red
167
- else
168
- Groups.removehosts([args[0]], Groups.host_tag(args[0]))
169
- Groups.appendhosts([args[0]], args[1])
170
- puts "Moved #{args[0]} to #{args[1]}".green
214
+ exit 1
171
215
  end
216
+ host = args[0]
217
+ tag = args[1]
218
+ if !Groups.managed_host?(host)
219
+ puts "Could not find host #{host}".red
220
+ exit 1
221
+ end
222
+
223
+ Groups.removehosts([host], Groups.host_tag(host))
224
+ Groups.addhosts([host], tag)
225
+ puts "Moved #{host} to #{tag}".green
172
226
  end
173
227
  end
174
228
 
@@ -180,11 +234,16 @@ class Floatyhelper
180
234
  c.option '--all', 'Destroys all hosts in status file. USE WITH CAUTION!'
181
235
  c.option '--hosts HOSTS', String, 'Comma separated list of hosts to destroy instead of using a tag. Note that this will NOT remove the hosts from the status file.'
182
236
  c.action do |args, options|
183
- if args.length == 0 && !options.all && !options.hosts
237
+ if args.empty? && !options.all && !options.hosts
184
238
  puts 'Please specify a host or tag'.red
239
+ exit 1
240
+ end
241
+ tag = args[0]
242
+ id = options.all ? 'all' : options.hosts ? Hosts.get_options_hosts(options.hosts) : tag
243
+ VM.destroy(id)
244
+ if options.all
245
+ puts 'Destroyed all hosts managed by floatyhelper'.green
185
246
  else
186
- id = options.all ? 'all' : options.hosts ? Hosts.get_options_hosts(options.hosts) : args[0]
187
- VM.destroy(id)
188
247
  puts "Destroyed hosts in #{id}".green
189
248
  end
190
249
  end
@@ -194,15 +253,16 @@ class Floatyhelper
194
253
  c.syntax = 'floatyhelper destroy_from_token'
195
254
  c.summary = 'Destroys all VMs assigned to the user\'s token'
196
255
  c.description = 'This runs a "floaty token status" to get all VMs currently assigned to the user\'s token, and then deletes them all. Use with care.'
197
- c.action do |args, options|
198
- info = eval(`floaty token status`)
256
+ c.action do
257
+ info = Floaty.floaty_cmd('token status --service vmpooler')
258
+ info = Floaty.parse_floaty_json_output(info)
199
259
  info.delete('ok')
200
260
  vms = info[info.keys[0]]['vms']
201
261
  running = vms.nil? ? nil : vms['running']
202
- if !running.nil?
203
- VM.destroy(running)
204
- else
262
+ if running.nil?
205
263
  puts 'No running VMs assigned to token to delete'.yellow
264
+ else
265
+ VM.destroy(running)
206
266
  end
207
267
  end
208
268
  end
@@ -210,41 +270,37 @@ class Floatyhelper
210
270
  command :increaselife do |c|
211
271
  c.syntax = 'floatyhelper increaselife <tag> [options]'
212
272
  c.summary = 'Increase the lifetime of the given hosts'
213
- c.description = 'Increases the current life of the given host or tagged hosts. If no host is given, increases all hosts. Defaults to 100 hours. May be used with the --hosts flag instead of a tag.'
214
- c.option '--amount HOURS', Integer, 'Amount of hours to increase life. Default = 100'
273
+ c.description = 'Increases the current life of the given host or tagged hosts. If no host is given, increases all hosts. Defaults to 12 hours. May be used with the --hosts flag instead of a tag.'
274
+ c.option '--amount HOURS', Integer, 'Amount of hours to increase life. Default = 12'
215
275
  c.option '--all', 'Destroys all hosts in status file. USE WITH CAUTION!'
216
276
  c.option '--hosts HOST', String, 'Comma separated list of hosts to destroy instead of using a tag.'
217
277
  c.action do |args, options|
218
- if args.length == 0 && !options.all && !options.hosts
278
+ if args.empty? && !options.all && !options.hosts
219
279
  puts 'Please specify a host or tag'.red
220
- else
221
- id = options.all ? 'all' : options.hosts ? Hosts.get_options_hosts(options.hosts) : args[0]
222
- VM.increaselife(id,options.amount)
280
+ exit 1
223
281
  end
282
+ tag = args[0]
283
+ id = options.all ? 'all' : options.hosts ? Hosts.get_options_hosts(options.hosts) : tag
284
+ VM.increaselife(id,options.amount)
224
285
  end
225
286
  end
226
287
 
227
- #command :query do |c|
228
- # c.syntax = 'floatyhelper query [options]'
229
- # c.summary = ''
230
- # c.description = ''
231
- # c.example 'description', 'command example'
232
- # c.option '--some-switch', 'Some switch that does something'
233
- # c.action do |args, options|
234
- # # Do something or c.when_called Floatyhelper::Commands::Query
235
- # end
236
- #end
237
-
238
288
  command :revert do |c|
239
289
  c.syntax = 'floatyhelper revert <host|tag> <snaptag>'
240
290
  c.summary = 'Revert VM to the given snaptag'
241
291
  c.description = 'Reverts the given VM or host group to given snaptag. This snaptag is created when the snapshot command is run.'
242
- c.action do |args, options|
292
+ c.action do |args|
243
293
  if args.length < 2
244
- puts "Please specify both a host/tag and a snaptag".red
245
- else
246
- VM.revert(args[0], args[1])
294
+ puts 'Please specify both a host/tag and a snaptag'.red
295
+ exit 1
296
+ end
297
+ host_or_tag = args[0]
298
+ snaptag = args[1]
299
+ if !VM.snaptag_exists?(host_or_tag, snaptag)
300
+ puts "Snaptag '#{snaptag}' not found for #{host_or_tag}".red
301
+ exit 1
247
302
  end
303
+ VM.revert(host_or_tag, snaptag)
248
304
  end
249
305
  end
250
306
 
@@ -252,16 +308,19 @@ class Floatyhelper
252
308
  c.syntax = 'floatyhelper snapshot <host|tag> <snaptag>'
253
309
  c.summary = 'Snapshot the given VM or group of taggedhosts'
254
310
  c.description = 'Creates a snapshot of a host or group of tagged hosts, and defines a string to use as a "snaptag" to refer to this group of snapshots.'
255
- c.action do |args, options|
311
+ c.action do |args|
256
312
  if args.length < 2
257
- puts "Please specify both a host/tag and a snaptag"
258
- else
259
- if VM.snaptag_exists?(args[0], args[1])
260
- answer = ask "Snaptag #{args[1]} already exists. Are you sure you want to overwrite? [y/N] ".yellow
261
- return unless answer.capitalize == 'Y'
262
- end
263
- VM.snapshot(args[0], args[1])
313
+ puts 'Please specify both a host/tag and a snaptag'.red
314
+ exit 1
264
315
  end
316
+
317
+ host_or_tag = args[0]
318
+ snaptag = args[1]
319
+ if VM.snaptag_exists?(host_or_tag, snaptag)
320
+ answer = ask "Snaptag #{snaptag} already exists. Are you sure you want to overwrite? [y/N] ".yellow
321
+ return unless answer.capitalize == 'Y'
322
+ end
323
+ VM.snapshot(host_or_tag, snaptag)
265
324
  end
266
325
  end
267
326
 
@@ -270,50 +329,70 @@ class Floatyhelper
270
329
  c.summary = 'Check for any expired VMs, and remove them from the list of hosts floatyhelper is tracking'
271
330
  c.description = 'Runs floaty query on VMs that floatyhelper is tracking, to check for any expired VMs, and allows the user to remove tags that contain only expired VMs.'
272
331
  c.option '-y', 'Do not prompt to remove tags with expired VMs'
273
- c.action do |args, options|
274
- data = Conf.load_data
332
+ c.action do |_args, options|
275
333
  expired = false
276
- data['vms'].each do |tag, hosts|
277
- is_alive = false
334
+ tags = Groups.get_tags
335
+ tags.each do |tag|
336
+ expired_hosts = []
337
+ hosts = Groups.get_hosts(tag)
278
338
  hosts.each do |host|
279
- is_alive |= VM.alive(host)
339
+ expired_hosts << host unless VM.alive(host)
280
340
  end
281
- unless is_alive
282
- expired = true
283
- if options.y
284
- answer = 'y'
285
- else
286
- answer = ask "All hosts in tag #{tag} have expired. Delete this tag? [Y/n] ".yellow
287
- end
341
+ next if expired_hosts.empty?
342
+
343
+ expired = true
344
+ if expired_hosts & hosts == hosts
345
+ answer = options.y ? 'y' : ask("All hosts in tag #{tag} have expired. Delete this tag? [Y/n] ".yellow)
288
346
  if answer.empty? || answer.capitalize == 'Y'
289
- Groups.delete_tag(tag)
347
+ Groups.delete_tag(tag)
290
348
  puts "Tag #{tag} deleted".green
291
349
  end
350
+ else
351
+ expired_hosts.each { |host| puts host.yellow } unless options.y
352
+ answer = options.y ? 'y' : ask("The above hosts in tag #{tag} have expired. Delete these hosts from the tag? [Y/n]".yellow)
353
+ if answer.empty? || answer.capitalize == 'Y'
354
+ Groups.removehosts(expired_hosts, tag)
355
+ puts "Expired hosts from #{tag} deleted".green
356
+ end
292
357
  end
293
358
  end
294
- puts "No managed VMs have expired".green unless expired
359
+ puts 'No managed VMs have expired'.green unless expired
295
360
  end
296
361
  end
297
362
 
298
363
  command :getvm do |c|
299
364
  c.syntax = 'floatyhelper getvm <platform> <tag>'
300
365
  c.summary = 'Request a VM from floaty'
301
- c.description = 'Request a VM of the given platform type from floaty, and add the host to either the given tag or the Unassigned tag, if none is given. If no platform is given, default to centos-7-x86_64.'
302
- c.action do |args|
303
- platform = args.length > 0 ? args[0] : 'centos-7-x86_64'
366
+ c.description = <<~EOT
367
+ Request a VM of the given platform type from floaty, and add the host to either the given tag or the Unassigned tag, if none is given.
368
+ If no platform is given, default to centos-7-x86_64. This will use vmpooler for pooled platforms, and ABS otherwise. Force usage of
369
+ ABS with the --abs flag.
370
+ EOT
371
+ c.option '--abs', 'Force use of ABS to get platform, even if platform is pooled.'
372
+ c.action do |args, options|
373
+ platform = args[0] || 'centos-7-x86_64'
304
374
  tag = args.length > 1 ? args[1] : 'Unassigned'
305
375
  begin
306
- host = VM.get_vm(platform)
307
- Groups.appendhosts([host], tag)
376
+ host = VM.get_vm(platform: platform, force_abs: options.abs)
377
+ Groups.addhosts([host], tag)
308
378
  puts "Added #{host} to tag #{tag}".green
309
- VM.increaselife(host, nil)
310
- rescue => exception
311
- puts exception.message.red
379
+ amount = Config.get_config_setting('increaselife').to_i
380
+ VM.increaselife(host, amount)
381
+ rescue StandardError => e
382
+ puts e.message.red
312
383
  end
313
384
  end
314
385
  end
315
386
 
387
+ command :checktokens do |c|
388
+ c.syntax = 'floatyhelper checktokens'
389
+ c.summary = 'Verify tokens for all services are valid'
390
+ c.description = 'Verify that tokens for abs, vmpooler, and nspooler are valid in .vmfloaty.yml. If they are not, get new tokens.'
391
+ c.action do
392
+ Floaty.check_tokens
393
+ end
394
+ end
395
+
316
396
  run!
317
397
  end
318
398
  end
319
-