opennebula-cli 5.10.5 → 5.12.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/bin/oneacct +4 -2
  3. data/bin/oneacl +4 -2
  4. data/bin/onecluster +4 -2
  5. data/bin/onedatastore +4 -2
  6. data/bin/oneflow +151 -552
  7. data/bin/oneflow-template +173 -293
  8. data/bin/onegroup +4 -2
  9. data/bin/onehook +4 -2
  10. data/bin/onehost +78 -10
  11. data/bin/oneimage +4 -2
  12. data/bin/onemarket +4 -2
  13. data/bin/onemarketapp +17 -4
  14. data/bin/onesecgroup +4 -2
  15. data/bin/oneshowback +4 -2
  16. data/bin/onetemplate +4 -2
  17. data/bin/oneuser +4 -2
  18. data/bin/onevcenter +4 -2
  19. data/bin/onevdc +4 -2
  20. data/bin/onevm +170 -17
  21. data/bin/onevmgroup +4 -2
  22. data/bin/onevnet +16 -41
  23. data/bin/onevntemplate +4 -2
  24. data/bin/onevrouter +4 -2
  25. data/bin/onezone +7 -2
  26. data/lib/cli_helper.rb +54 -30
  27. data/lib/command_parser.rb +53 -19
  28. data/lib/one_helper.rb +258 -6
  29. data/lib/one_helper/oneacct_helper.rb +1 -1
  30. data/lib/one_helper/oneacl_helper.rb +1 -1
  31. data/lib/one_helper/onecluster_helper.rb +4 -4
  32. data/lib/one_helper/onedatastore_helper.rb +1 -1
  33. data/lib/one_helper/oneflow_helper.rb +423 -0
  34. data/lib/one_helper/oneflowtemplate_helper.rb +312 -0
  35. data/lib/one_helper/onegroup_helper.rb +1 -1
  36. data/lib/one_helper/onehook_helper.rb +1 -1
  37. data/lib/one_helper/onehost_helper.rb +148 -68
  38. data/lib/one_helper/oneimage_helper.rb +2 -2
  39. data/lib/one_helper/onemarket_helper.rb +1 -1
  40. data/lib/one_helper/onemarketapp_helper.rb +1 -1
  41. data/lib/one_helper/oneprovision_helper.rb +104 -60
  42. data/lib/one_helper/onequota_helper.rb +1 -1
  43. data/lib/one_helper/onesecgroup_helper.rb +1 -1
  44. data/lib/one_helper/onetemplate_helper.rb +9 -180
  45. data/lib/one_helper/oneuser_helper.rb +1 -1
  46. data/lib/one_helper/onevcenter_helper.rb +5 -4
  47. data/lib/one_helper/onevdc_helper.rb +1 -1
  48. data/lib/one_helper/onevm_helper.rb +117 -21
  49. data/lib/one_helper/onevmgroup_helper.rb +1 -1
  50. data/lib/one_helper/onevnet_helper.rb +66 -2
  51. data/lib/one_helper/onevntemplate_helper.rb +1 -1
  52. data/lib/one_helper/onevrouter_helper.rb +1 -1
  53. data/lib/one_helper/onezone_helper.rb +3 -1
  54. metadata +8 -6
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  # -------------------------------------------------------------------------- #
4
- # Copyright 2002-2019, OpenNebula Project, OpenNebula Systems #
4
+ # Copyright 2002-2020, OpenNebula Project, OpenNebula Systems #
5
5
  # #
6
6
  # Licensed under the Apache License, Version 2.0 (the "License"); you may #
7
7
  # not use this file except in compliance with the License. You may obtain #
@@ -20,139 +20,62 @@ ONE_LOCATION = ENV['ONE_LOCATION']
20
20
 
21
21
  if !ONE_LOCATION
22
22
  RUBY_LIB_LOCATION = '/usr/lib/one/ruby'
23
+ ONEFLOW_LOCATION = '/usr/lib/one/oneflow/lib'
23
24
  GEMS_LOCATION = '/usr/share/one/gems'
24
25
  else
25
26
  RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby'
27
+ ONEFLOW_LOCATION = ONE_LOCATION + '/lib/oneflow/lib'
26
28
  GEMS_LOCATION = ONE_LOCATION + '/share/gems'
27
29
  end
28
30
 
29
31
  if File.directory?(GEMS_LOCATION)
30
- Gem.use_paths(GEMS_LOCATION)
32
+ $LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ }
33
+ require 'rubygems'
34
+ Gem.use_paths(File.realpath(GEMS_LOCATION))
31
35
  end
32
36
 
33
37
  $LOAD_PATH << RUBY_LIB_LOCATION
38
+ $LOAD_PATH << ONEFLOW_LOCATION
34
39
  $LOAD_PATH << RUBY_LIB_LOCATION + '/cli'
40
+ $LOAD_PATH << RUBY_LIB_LOCATION + '/oneflow/lib'
41
+
42
+ require 'json'
43
+ require 'English'
44
+ require 'tempfile'
35
45
 
36
46
  require 'command_parser'
37
47
  require 'opennebula/oneflow_client'
38
- require 'English'
48
+ require 'models'
39
49
  require 'cli_helper'
40
- require 'one_helper'
41
-
42
- require 'json'
50
+ require 'one_helper/oneflowtemplate_helper'
43
51
 
44
52
  USER_AGENT = 'CLI'
45
53
 
46
54
  # Base Path representing the resource to be used in the requests
47
55
  RESOURCE_PATH = '/service_template'
48
56
 
49
- #
50
- # Table
51
- #
52
-
53
- TABLE = CLIHelper::ShowTable.new(nil, self) do
54
- column :ID, 'ID', :size => 10 do |d|
55
- d['ID']
56
- end
57
-
58
- column :USER, 'Username', :left, :size => 15 do |d|
59
- d['UNAME']
60
- end
61
-
62
- column :GROUP, 'Group', :left, :size => 15 do |d|
63
- d['GNAME']
64
- end
65
-
66
- column :NAME, 'Name', :left, :size => 37 do |d|
67
- d['NAME']
68
- end
69
-
70
- default :ID, :USER, :GROUP, :NAME
71
- end
72
-
73
- # Show the service template information. This method is used in top and
74
- # show commands
75
- # @param [Service::Client] client
76
- # @param [Array] args
77
- # @param [Hash] options
78
- # @return [[Integer, String], Integer] Returns the exit_code and optionally
79
- # a String to be printed
80
- def show_service_template(client, args, options)
81
- response = client.get("#{RESOURCE_PATH}/#{args[0]}")
82
-
83
- if CloudClient.is_error?(response)
84
- [response.code.to_i, response.to_s]
85
- else
86
- # [0,response.body]
87
- if options[:json]
88
- [0, response.body]
89
- else
90
- str = '%-20s: %-20s'
91
- str_h1 = '%-80s'
92
-
93
- document_hash = JSON.parse(response.body)
94
- template = document_hash['DOCUMENT']['TEMPLATE']['BODY']
95
-
96
- CLIHelper.print_header(str_h1 %
97
- "SERVICE TEMPLATE #{document_hash['DOCUMENT']['ID']} "\
98
- 'INFORMATION')
99
-
100
- puts Kernel.format str, 'ID', document_hash['DOCUMENT']['ID']
101
- puts Kernel.format str, 'NAME', document_hash['DOCUMENT']['NAME']
102
- puts Kernel.format str, 'USER', document_hash['DOCUMENT']['UNAME']
103
- puts Kernel.format str, 'GROUP', document_hash['DOCUMENT']['GNAME']
104
-
105
- puts
106
-
107
- CLIHelper.print_header(str_h1 % 'PERMISSIONS', false)
108
-
109
- %w[OWNER GROUP OTHER].each do |e|
110
- mask = '---'
111
- permissions_hash = document_hash['DOCUMENT']['PERMISSIONS']
112
- mask[0] = 'u' if permissions_hash["#{e}_U"] == '1'
113
- mask[1] = 'm' if permissions_hash["#{e}_M"] == '1'
114
- mask[2] = 'a' if permissions_hash["#{e}_A"] == '1'
115
-
116
- puts Kernel.format str, e, mask
117
- end
118
-
119
- puts
120
-
121
- CLIHelper.print_header(str_h1 % 'TEMPLATE CONTENTS', false)
122
- puts JSON.pretty_generate(template)
123
-
124
- 0
125
- end
126
- end
127
- end
128
-
129
- # List the services. This method is used in top and list commands
130
- # @param [Service::Client] client
131
- # @param [Hash] options
132
- # @return [[Integer, String], Integer] Returns the exit_code and optionally
133
- # a String to be printed
134
- def list_service_templates(client, options)
135
- response = client.get(RESOURCE_PATH)
136
-
137
- if CloudClient.is_error?(response)
138
- [response.code.to_i, response.to_s]
139
- else
140
- # [0,response.body]
141
- if options[:json]
142
- [0, response.body]
143
- else
144
- array_list = JSON.parse(response.body)
145
- TABLE.show(array_list['DOCUMENT_POOL']['DOCUMENT'])
146
- 0
147
- end
148
- end
149
- end
150
-
151
- #
152
- # Commands
153
- #
154
-
155
57
  CommandParser::CmdParser.new(ARGV) do
58
+ MULTIPLE = {
59
+ :name => 'multiple',
60
+ :short => '-m x',
61
+ :large => '--multiple x',
62
+ :format => Integer,
63
+ :description => 'Instance multiple templates'
64
+ }
65
+
66
+ RECURSIVE = {
67
+ :name => 'recursive',
68
+ :short => '-r',
69
+ :large => '--recursive',
70
+ :description => 'Clone the template recursively (templates and images)'
71
+ }
72
+
73
+ RECURSIVE_TEMPLATES = {
74
+ :name => 'recursive_templates',
75
+ :large => '--recursive-templates',
76
+ :description => 'Clone the template recursively (just templates)'
77
+ }
78
+
156
79
  usage '`oneflow-template` <command> [<args>] [<options>]'
157
80
  version OpenNebulaHelper::ONE_VERSION
158
81
 
@@ -160,9 +83,12 @@ CommandParser::CmdParser.new(ARGV) do
160
83
  set :option, CommandParser::VERSION
161
84
  set :option, CommandParser::HELP
162
85
 
163
- #
86
+ # create helper object
87
+ helper = OneFlowTemplateHelper.new
88
+
89
+ ############################################################################
164
90
  # Formatters for arguments
165
- #
91
+ ############################################################################
166
92
  set :format, :groupid, OpenNebulaHelper.rname_to_id_desc('GROUP') do |arg|
167
93
  OpenNebulaHelper.rname_to_id(arg, 'GROUP')
168
94
  end
@@ -181,81 +107,52 @@ CommandParser::CmdParser.new(ARGV) do
181
107
  Service.list_to_id(arg, 'SERVICE TEMPLATE')
182
108
  end
183
109
 
184
- #
185
- # List
186
- #
110
+ ###
187
111
 
188
112
  list_desc = <<-EOT.unindent
189
113
  List the available Service Templates
190
114
  EOT
191
115
 
192
116
  command :list, list_desc, :options => Service::JSON_FORMAT do
193
- client = Service::Client.new(
194
- :username => options[:username],
195
- :password => options[:password],
196
- :url => options[:server],
197
- :user_agent => USER_AGENT
198
- )
199
-
200
- list_service_templates(client, options)
117
+ helper.list_service_template_pool(helper.client(options), options)
201
118
  end
202
119
 
203
- #
204
- # Top
205
- #
120
+ ###
206
121
 
207
122
  top_desc = <<-EOT.unindent
208
123
  List the available Service Templates continuously
209
124
  EOT
210
125
 
211
- command :top, top_desc,
126
+ command :top,
127
+ top_desc,
212
128
  :options => [Service::JSON_FORMAT,
213
129
  Service::TOP,
214
130
  CLIHelper::DELAY] do
215
- client = Service::Client.new(
216
- :username => options[:username],
217
- :password => options[:password],
218
- :url => options[:server],
219
- :user_agent => USER_AGENT
220
- )
131
+ Signal.trap('INT') { exit(-1) }
221
132
 
222
- options[:delay] ? delay = options[:delay] : delay = 3
133
+ helper.top_service_template_pool(helper.client(options), options)
223
134
 
224
- begin
225
- loop do
226
- CLIHelper.scr_cls
227
- CLIHelper.scr_move(0, 0)
135
+ 0
136
+ end
228
137
 
229
- rc, message = list_service_templates(client, options)
138
+ ###
230
139
 
231
- if rc != 0
232
- raise message
233
- end
140
+ show_desc = <<-EOT.unindent
141
+ Show detailed information of a given Service Template
142
+ EOT
234
143
 
235
- sleep delay
236
- end
237
- rescue StandardError => e
238
- puts e.message
239
- -1
240
- end
144
+ command :show, show_desc, :templateid, :options => Service::JSON_FORMAT do
145
+ helper.format_resource(helper.client(options), args[0], options)
241
146
  end
242
147
 
243
- #
244
- # Create
245
- #
148
+ ###
246
149
 
247
150
  create_desc = <<-EOT.unindent
248
151
  Create a new Service Template
249
152
  EOT
250
153
 
251
154
  command :create, create_desc, :file, :options => Service::JSON_FORMAT do
252
- client = Service::Client.new(
253
- :username => options[:username],
254
- :password => options[:password],
255
- :url => options[:server],
256
- :user_agent => USER_AGENT
257
- )
258
-
155
+ client = helper.client(options)
259
156
  response = client.post(RESOURCE_PATH, File.read(args[0]))
260
157
 
261
158
  if CloudClient.is_error?(response)
@@ -264,180 +161,178 @@ CommandParser::CmdParser.new(ARGV) do
264
161
  if options[:json]
265
162
  [0, response.body]
266
163
  else
267
- template = JSON.parse(response.body)
268
- puts "ID: #{template['DOCUMENT']['ID']}"
164
+ puts "ID: #{JSON.parse(response.body)['DOCUMENT']['ID']}"
165
+
269
166
  0
270
167
  end
271
168
  end
272
169
  end
273
170
 
274
- #
275
- # Show
276
- #
277
-
278
- show_desc = <<-EOT.unindent
279
- Show detailed information of a given Service Template
280
- EOT
281
-
282
- command :show, show_desc, :templateid, :options => Service::JSON_FORMAT do
283
- client = Service::Client.new(
284
- :username => options[:username],
285
- :password => options[:password],
286
- :url => options[:server],
287
- :user_agent => USER_AGENT
288
- )
289
-
290
- show_service_template(client, args, options)
291
- end
292
-
293
- #
294
- # Delete
295
- #
171
+ ###
296
172
 
297
173
  delete_desc = <<-EOT.unindent
298
174
  Delete a given Service Template
299
175
  EOT
300
176
 
301
177
  command :delete, delete_desc, [:range, :templateid_list] do
302
- client = Service::Client.new(
303
- :username => options[:username],
304
- :password => options[:password],
305
- :url => options[:server],
306
- :user_agent => USER_AGENT
307
- )
178
+ client = helper.client(options)
308
179
 
309
180
  Service.perform_actions(args[0]) do |template_id|
310
181
  client.delete("#{RESOURCE_PATH}/#{template_id}")
311
182
  end
312
183
  end
313
184
 
314
- #
315
- # Instantiate
316
- #
185
+ ###
317
186
 
318
187
  instantiate_desc = <<-EOT.unindent
319
188
  Instantiate a Service Template
320
189
  EOT
321
190
 
322
- command :instantiate, instantiate_desc, :templateid, [:file, nil],
323
- :options => [Service::JSON_FORMAT, Service::TOP] do
324
- client = Service::Client.new(
325
- :username => options[:username],
326
- :password => options[:password],
327
- :url => options[:server],
328
- :user_agent => USER_AGENT
329
- )
330
-
191
+ command :instantiate,
192
+ instantiate_desc,
193
+ :templateid,
194
+ [:file, nil],
195
+ :options => [MULTIPLE, Service::JSON_FORMAT, Service::TOP] do
196
+ number = options[:multiple] || 1
331
197
  params = {}
198
+ rc = 0
332
199
 
333
- if args[1]
334
- params['merge_template'] = JSON.parse(File.read(args[1]))
335
- end
200
+ number.times do
201
+ params['merge_template'] = nil
202
+ params['merge_template'] = JSON.parse(File.read(args[1])) if args[1]
336
203
 
337
- json_str = Service.build_json_action('instantiate', params)
204
+ unless params['merge_template']
205
+ service_template = OpenNebula::ServiceTemplate
206
+ .new_with_id(args[0],
207
+ OpenNebula::Client.new)
208
+ service_template.info
338
209
 
339
- response = client.post("#{RESOURCE_PATH}/#{args[0]}/action", json_str)
210
+ body = JSON.parse(service_template['/DOCUMENT/TEMPLATE/BODY'])
340
211
 
341
- if CloudClient.is_error?(response)
342
- [response.code.to_i, response.to_s]
343
- else
344
- if options[:json]
345
- [0, response.body]
212
+ params['merge_template'] = helper.custom_attrs(
213
+ body['custom_attrs']
214
+ )
215
+
216
+ params['merge_template'] = {} unless params['merge_template']
217
+
218
+ vnets = helper.networks(body['networks'])
219
+ params['merge_template'].merge!(vnets) unless vnets.nil?
220
+ end
221
+
222
+ json = Service.build_json_action('instantiate', params)
223
+ client = helper.client(options)
224
+ response = client.post("#{RESOURCE_PATH}/#{args[0]}/action", json)
225
+
226
+ if CloudClient.is_error?(response)
227
+ rc = [response.code.to_i, response.to_s]
228
+ break
346
229
  else
347
- template = JSON.parse(response.body)
348
- puts "ID: #{template['DOCUMENT']['ID']}"
349
- 0
230
+ if options[:json]
231
+ rc = [0, response.body]
232
+ break
233
+ else
234
+ rc = 0
235
+ puts "ID: #{JSON.parse(response.body)['DOCUMENT']['ID']}"
236
+ end
350
237
  end
351
238
  end
239
+
240
+ rc
352
241
  end
353
242
 
243
+ ###
244
+
354
245
  chgrp_desc = <<-EOT.unindent
355
246
  Changes the service template group
356
247
  EOT
357
248
 
358
249
  command :chgrp, chgrp_desc, [:range, :templateid_list], :groupid do
359
- client = Service::Client.new(
360
- :username => options[:username],
361
- :password => options[:password],
362
- :url => options[:server],
363
- :user_agent => USER_AGENT
364
- )
250
+ client = helper.client(options)
365
251
 
366
252
  Service.perform_actions(args[0]) do |service_id|
367
253
  params = {}
368
254
  params['group_id'] = args[1].to_i
369
255
 
370
- json_action = Service.build_json_action('chgrp', params)
256
+ json = Service.build_json_action('chgrp', params)
371
257
 
372
- client.post("#{RESOURCE_PATH}/#{service_id}/action", json_action)
258
+ client.post("#{RESOURCE_PATH}/#{service_id}/action", json)
373
259
  end
374
260
  end
375
261
 
262
+ ###
263
+
376
264
  chown_desc = <<-EOT.unindent
377
265
  Changes the service template owner and group
378
266
  EOT
379
267
 
380
- command :chown, chown_desc, [:range, :templateid_list],
381
- :userid, [:groupid, nil] do
382
- client = Service::Client.new(
383
- :username => options[:username],
384
- :password => options[:password],
385
- :url => options[:server],
386
- :user_agent => USER_AGENT
387
- )
268
+ command :chown,
269
+ chown_desc,
270
+ [:range, :templateid_list],
271
+ :userid,
272
+ [:groupid, nil] do
273
+ client = helper.client(options)
388
274
 
389
275
  Service.perform_actions(args[0]) do |service_id|
390
276
  params = {}
391
277
  params['owner_id'] = args[1]
392
278
  params['group_id'] = args[2] if args[2]
393
279
 
394
- json_action = Service.build_json_action('chown', params)
280
+ json = Service.build_json_action('chown', params)
395
281
 
396
- client.post("#{RESOURCE_PATH}/#{service_id}/action", json_action)
282
+ client.post("#{RESOURCE_PATH}/#{service_id}/action", json)
397
283
  end
398
284
  end
399
285
 
286
+ ###
287
+
400
288
  chmod_desc = <<-EOT.unindent
401
289
  Changes the service template permissions
402
290
  EOT
403
291
 
404
292
  command :chmod, chmod_desc, [:range, :templateid_list], :octet do
405
- client = Service::Client.new(
406
- :username => options[:username],
407
- :password => options[:password],
408
- :url => options[:server],
409
- :user_agent => USER_AGENT
410
- )
293
+ if !/\A\d+\z/.match(args[1])
294
+ STDERR.puts "Invalid '#{args[1]}' octed permissions"
295
+ exit(-1)
296
+ end
297
+
298
+ client = helper.client(options)
411
299
 
412
300
  Service.perform_actions(args[0]) do |service_id|
413
301
  params = {}
414
302
  params['octet'] = args[1]
415
303
 
416
- json_action = Service.build_json_action('chmod', params)
304
+ json = Service.build_json_action('chmod', params)
417
305
 
418
- client.post("#{RESOURCE_PATH}/#{service_id}/action", json_action)
306
+ client.post("#{RESOURCE_PATH}/#{service_id}/action", json)
419
307
  end
420
308
  end
421
309
 
310
+ ###
311
+
422
312
  clone_desc = <<-EOT.unindent
423
313
  Creates a new Service Template from an existing one
424
314
  EOT
425
315
 
426
- command :clone, clone_desc, :templateid, :name do
427
- client = Service::Client.new(
428
- :username => options[:username],
429
- :password => options[:password],
430
- :url => options[:server],
431
- :user_agent => USER_AGENT
432
- )
433
-
316
+ command :clone,
317
+ clone_desc,
318
+ :templateid,
319
+ :name,
320
+ :options => [RECURSIVE, RECURSIVE_TEMPLATES] do
434
321
  params = {}
435
322
  params['name'] = args[1]
436
323
 
437
- json_action = Service.build_json_action('clone', params)
324
+ if options.key?(:recursive)
325
+ params['recursive'] = 'all'
326
+ elsif options.key?(:recursive_templates)
327
+ params['recurisve'] = 'templates'
328
+ else
329
+ params['recursive'] = 'none'
330
+ end
331
+
332
+ json = Service.build_json_action('clone', params)
333
+ client = helper.client(options)
438
334
 
439
- response = client.post("#{RESOURCE_PATH}/#{args[0]}/action",
440
- json_action)
335
+ response = client.post("#{RESOURCE_PATH}/#{args[0]}/action", json)
441
336
 
442
337
  if CloudClient.is_error?(response)
443
338
  [response.code.to_i, response.to_s]
@@ -445,40 +340,33 @@ CommandParser::CmdParser.new(ARGV) do
445
340
  if options[:json]
446
341
  [0, response.body]
447
342
  else
448
- template = JSON.parse(response.body)
449
- puts "ID: #{template['DOCUMENT']['ID']}"
343
+ puts "ID: #{JSON.parse(response.body)['DOCUMENT']['ID']}"
344
+
450
345
  0
451
346
  end
452
347
  end
453
348
  end
454
349
 
350
+ ###
351
+
455
352
  rename_desc = <<-EOT.unindent
456
353
  Renames the Service Template
457
354
  EOT
458
355
 
459
356
  command :rename, rename_desc, :templateid, :name do
460
- client = Service::Client.new(
461
- :username => options[:username],
462
- :password => options[:password],
463
- :url => options[:server],
464
- :user_agent => USER_AGENT
465
- )
466
-
467
- params = {}
468
- params['name'] = args[1]
469
-
470
- json_action = Service.build_json_action('rename', params)
357
+ Service.perform_action(args[0]) do |template_id|
358
+ params = {}
359
+ params['name'] = args[1]
471
360
 
472
- response = client.post("#{RESOURCE_PATH}/#{args[0]}/action",
473
- json_action)
361
+ json = Service.build_json_action('rename', params)
362
+ client = helper.client(options)
474
363
 
475
- if CloudClient.is_error?(response)
476
- [response.code.to_i, response.to_s]
477
- else
478
- response.code.to_i
364
+ client.post("#{RESOURCE_PATH}/#{template_id}/action", json)
479
365
  end
480
366
  end
481
367
 
368
+ ###
369
+
482
370
  update_desc = <<-EOT.unindent
483
371
  Update the template contents. If a path is not provided the editor will
484
372
  be launched to modify the current content.
@@ -486,30 +374,23 @@ CommandParser::CmdParser.new(ARGV) do
486
374
 
487
375
  command :update, update_desc, :templateid, [:file, nil] do
488
376
  template_id = args[0]
489
- client = Service::Client.new(
490
- :username => options[:username],
491
- :password => options[:password],
492
- :url => options[:server],
493
- :user_agent => USER_AGENT
494
- )
377
+ client = helper.client(options)
495
378
 
496
379
  if args[1]
497
380
  path = args[1]
498
381
  else
499
- require 'tempfile'
500
-
501
- tmp = Tempfile.new(template_id.to_s)
502
- path = tmp.path
503
-
504
382
  response = client.get("#{RESOURCE_PATH}/#{template_id}")
505
383
 
506
384
  if CloudClient.is_error?(response)
507
385
  exit_with_code response.code.to_i, response.to_s
508
386
  else
509
- document_hash = JSON.parse(response.body)
510
- template = document_hash['DOCUMENT']['TEMPLATE']['BODY']
387
+ document = JSON.parse(response.body)['DOCUMENT']
388
+ template = document['TEMPLATE']['BODY']
511
389
 
512
- tmp << JSON.pretty_generate(template)
390
+ tmp = Tempfile.new(template_id.to_s)
391
+ path = tmp.path
392
+
393
+ tmp.write(JSON.pretty_generate(template))
513
394
  tmp.flush
514
395
 
515
396
  if ENV['EDITOR']
@@ -517,10 +398,11 @@ CommandParser::CmdParser.new(ARGV) do
517
398
  else
518
399
  editor_path = OpenNebulaHelper::EDITOR_PATH
519
400
  end
401
+
520
402
  system("#{editor_path} #{path}")
521
403
 
522
404
  unless $CHILD_STATUS.exitstatus.zero?
523
- puts 'Editor not defined'
405
+ STDERR.puts 'Editor not defined'
524
406
  exit(-1)
525
407
  end
526
408
 
@@ -528,15 +410,13 @@ CommandParser::CmdParser.new(ARGV) do
528
410
  end
529
411
  end
530
412
 
531
- exit_code = 0
413
+ response = client.put("#{RESOURCE_PATH}/#{template_id}",
414
+ File.read(path))
532
415
 
533
- t_str = File.read(path)
534
- response = client.put("#{RESOURCE_PATH}/#{template_id}", t_str)
535
416
  if CloudClient.is_error?(response)
536
- puts response.to_s
537
- exit_code = response.code.to_i
417
+ [response.code.to_i, response.to_s]
418
+ else
419
+ 0
538
420
  end
539
-
540
- exit_code
541
421
  end
542
422
  end