13
13
require 'vmfloaty/utils'
14
14
require 'vmfloaty/service'
15
15
require 'vmfloaty/ssh'
16
+ require 'vmfloaty/logger'
16
17
17
18
class Vmfloaty
18
19
include Commander ::Methods
19
20
21
+ def self . logger
22
+ @logger ||= FloatyLogger . new
23
+ end
24
+
20
25
def run # rubocop:disable Metrics/AbcSize
21
26
program :version , Vmfloaty ::VERSION
22
27
program :description , "A CLI helper tool for Puppet's vmpooler to help you stay afloat"
@@ -45,7 +50,7 @@ def run # rubocop:disable Metrics/AbcSize
45
50
force = options . force
46
51
47
52
if args . empty?
48
- STDERR . puts 'No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs.'
53
+ logger . error 'No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs.'
49
54
exit 1
50
55
end
51
56
@@ -54,13 +59,13 @@ def run # rubocop:disable Metrics/AbcSize
54
59
max_pool_request = 5
55
60
large_pool_requests = os_types . select { |_ , v | v > max_pool_request }
56
61
if !large_pool_requests . empty? && !force
57
- STDERR . puts "Requesting vms over #{ max_pool_request } requires a --force flag."
58
- STDERR . puts 'Try again with `floaty get --force`'
62
+ logger . error "Requesting vms over #{ max_pool_request } requires a --force flag."
63
+ logger . error 'Try again with `floaty get --force`'
59
64
exit 1
60
65
end
61
66
62
67
if os_types . empty?
63
- STDERR . puts 'No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs.'
68
+ logger . error 'No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs.'
64
69
exit 1
65
70
end
66
71
@@ -71,9 +76,9 @@ def run # rubocop:disable Metrics/AbcSize
71
76
hosts = Utils . standardize_hostnames ( response )
72
77
73
78
if options . json || options . ondemand
74
- STDOUT . puts JSON . pretty_generate ( hosts )
79
+ puts JSON . pretty_generate ( hosts )
75
80
else
76
- STDOUT . puts Utils . format_host_output ( hosts )
81
+ puts Utils . format_host_output ( hosts )
77
82
end
78
83
end
79
84
end
@@ -99,15 +104,15 @@ def run # rubocop:disable Metrics/AbcSize
99
104
running_vms = service . list_active ( verbose )
100
105
host = URI . parse ( service . url ) . host
101
106
if running_vms . empty?
102
- STDOUT . puts "You have no running VMs on #{ host } "
107
+ puts "You have no running VMs on #{ host } "
103
108
else
104
- STDOUT . puts "Your VMs on #{ host } :"
109
+ puts "Your VMs on #{ host } :"
105
110
Utils . pretty_print_hosts ( verbose , service , running_vms )
106
111
end
107
112
else
108
113
# list available vms from pooler
109
114
os_list = service . list ( verbose , filter )
110
- STDOUT . puts os_list
115
+ puts os_list
111
116
end
112
117
end
113
118
end
@@ -151,7 +156,7 @@ def run # rubocop:disable Metrics/AbcSize
151
156
modify_all = options . all
152
157
153
158
if hostname . nil? && !modify_all
154
- STDERR . puts 'ERROR: Provide a hostname or specify --all.'
159
+ logger . error 'ERROR: Provide a hostname or specify --all.'
155
160
exit 1
156
161
end
157
162
running_vms = modify_all ? service . list_active ( verbose ) : hostname . split ( ',' )
@@ -172,17 +177,17 @@ def run # rubocop:disable Metrics/AbcSize
172
177
begin
173
178
modified_hash [ vm ] = service . modify ( verbose , vm , modify_hash )
174
179
rescue ModifyError => e
175
- STDERR . puts e
180
+ logger . error e
176
181
ok = false
177
182
end
178
183
end
179
184
if ok
180
185
if modify_all
181
- STDOUT . puts 'Successfully modified all VMs.'
186
+ puts 'Successfully modified all VMs.'
182
187
else
183
- STDOUT . puts "Successfully modified VM #{ hostname } ."
188
+ puts "Successfully modified VM #{ hostname } ."
184
189
end
185
- STDOUT . puts 'Use `floaty list --active` to see the results.'
190
+ puts 'Use `floaty list --active` to see the results.'
186
191
end
187
192
end
188
193
end
@@ -214,11 +219,10 @@ def run # rubocop:disable Metrics/AbcSize
214
219
if delete_all
215
220
running_vms = service . list_active ( verbose )
216
221
if running_vms . empty?
217
- STDOUT . puts 'You have no running VMs.'
222
+ puts 'You have no running VMs.'
218
223
else
219
224
Utils . pretty_print_hosts ( verbose , service , running_vms , true )
220
225
# Confirm deletion
221
- STDERR . puts
222
226
confirmed = true
223
227
confirmed = agree ( 'Delete all these VMs? [y/N]' ) unless force
224
228
if confirmed
@@ -243,23 +247,23 @@ def run # rubocop:disable Metrics/AbcSize
243
247
end
244
248
end
245
249
else
246
- STDERR . puts 'You did not provide any hosts to delete'
250
+ logger . info 'You did not provide any hosts to delete'
247
251
exit 1
248
252
end
249
253
250
254
unless failures . empty?
251
- STDERR . puts 'Unable to delete the following VMs:'
255
+ logger . info 'Unable to delete the following VMs:'
252
256
failures . each do |hostname |
253
- STDERR . puts "- #{ hostname } "
257
+ logger . info "- #{ hostname } "
254
258
end
255
- STDERR . puts 'Check `floaty list --active`; Do you need to specify a different service?'
259
+ logger . info 'Check `floaty list --active`; Do you need to specify a different service?'
256
260
end
257
261
258
262
unless successes . empty?
259
- STDERR . puts unless failures . empty?
260
- STDOUT . puts 'Scheduled the following VMs for deletion:'
263
+ logger . info unless failures . empty?
264
+ puts 'Scheduled the following VMs for deletion:'
261
265
successes . each do |hostname |
262
- STDOUT . puts "- #{ hostname } "
266
+ puts "- #{ hostname } "
263
267
end
264
268
end
265
269
@@ -284,11 +288,11 @@ def run # rubocop:disable Metrics/AbcSize
284
288
begin
285
289
snapshot_req = service . snapshot ( verbose , hostname )
286
290
rescue TokenError , ModifyError => e
287
- STDERR . puts e
291
+ logger . error e
288
292
exit 1
289
293
end
290
294
291
- STDOUT . puts "Snapshot pending. Use `floaty query #{ hostname } ` to determine when snapshot is valid."
295
+ puts "Snapshot pending. Use `floaty query #{ hostname } ` to determine when snapshot is valid."
292
296
pp snapshot_req
293
297
end
294
298
end
@@ -309,12 +313,12 @@ def run # rubocop:disable Metrics/AbcSize
309
313
hostname = args [ 0 ]
310
314
snapshot_sha = args [ 1 ] || options . snapshot
311
315
312
- STDERR . puts "Two snapshot arguments were given....using snapshot #{ snapshot_sha } " if args [ 1 ] && options . snapshot
316
+ logger . info "Two snapshot arguments were given....using snapshot #{ snapshot_sha } " if args [ 1 ] && options . snapshot
313
317
314
318
begin
315
319
revert_req = service . revert ( verbose , hostname , snapshot_sha )
316
320
rescue TokenError , ModifyError => e
317
- STDERR . puts e
321
+ logger . error e
318
322
exit 1
319
323
end
320
324
@@ -379,24 +383,24 @@ def run # rubocop:disable Metrics/AbcSize
379
383
case action
380
384
when 'get'
381
385
token = service . get_new_token ( verbose )
382
- STDOUT . puts token
386
+ puts token
383
387
when 'delete'
384
388
result = service . delete_token ( verbose , options . token )
385
- STDOUT . puts result
389
+ puts result
386
390
when 'status'
387
391
token_value = options . token
388
392
token_value = args [ 1 ] if token_value . nil?
389
393
status = service . token_status ( verbose , token_value )
390
- STDOUT . puts status
394
+ puts status
391
395
when nil
392
- STDERR . puts 'No action provided'
396
+ logger . error 'No action provided'
393
397
exit 1
394
398
else
395
- STDERR . puts "Unknown action: #{ action } "
399
+ logger . error "Unknown action: #{ action } "
396
400
exit 1
397
401
end
398
402
rescue TokenError => e
399
- STDERR . puts e
403
+ logger . error e
400
404
exit 1
401
405
end
402
406
exit 0
@@ -420,13 +424,13 @@ def run # rubocop:disable Metrics/AbcSize
420
424
use_token = !options . notoken
421
425
422
426
if args . empty?
423
- STDERR . puts 'No operating systems provided to obtain. See `floaty ssh --help` for more information on how to get VMs.'
427
+ logger . error 'No operating systems provided to obtain. See `floaty ssh --help` for more information on how to get VMs.'
424
428
exit 1
425
429
end
426
430
427
431
host_os = args . first
428
432
429
- STDERR . puts "Can't ssh to multiple hosts; Using #{ host_os } only..." if args . length > 1
433
+ logger . info "Can't ssh to multiple hosts; Using #{ host_os } only..." if args . length > 1
430
434
431
435
service . ssh ( verbose , host_os , use_token )
432
436
exit 0
@@ -450,10 +454,10 @@ def run # rubocop:disable Metrics/AbcSize
450
454
completion_file = File . expand_path ( File . join ( '..' , '..' , 'extras' , 'completions' , "floaty.#{ shell } " ) , __FILE__ )
451
455
452
456
if File . exist? ( completion_file )
453
- STDOUT . puts completion_file
457
+ puts completion_file
454
458
exit 0
455
459
else
456
- STDERR . puts "Could not find completion file for '#{ shell } ': No such file #{ completion_file } "
460
+ logger . error "Could not find completion file for '#{ shell } ': No such file #{ completion_file } "
457
461
exit 1
458
462
end
459
463
end
0 commit comments