Class: Morpheus::Cli::Groups

Inherits:
Object
  • Object
show all
Extended by:
Term::ANSIColor
Includes:
CliCommand, InfrastructureHelper
Defined in:
lib/morpheus/cli/groups.rb

Constant Summary collapse

@@groups =
nil

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Class Method Summary collapse

Instance Method Summary collapse

Methods included from InfrastructureHelper

#cloud_type_for_id, #cloud_type_for_name, #cloud_type_for_name_or_id, #clouds_interface, #find_cloud_by_id, #find_cloud_by_name, #find_cloud_by_name_or_id, #find_group_by_id, #find_group_by_name, #find_group_by_name_or_id, #get_available_cloud_types, #groups_interface, included

Methods included from CliCommand

#build_common_options, #build_option_type_options, #command_name, #default_subcommand, #establish_remote_appliance_connection, #handle_subcommand, included, #interactive?, #noninteractive, #print_usage, #subcommand_aliases, #subcommand_usage, #subcommands, #usage, #verify_access_token!

Constructor Details

#initializeGroups

Returns a new instance of Groups.



18
19
20
# File 'lib/morpheus/cli/groups.rb', line 18

def initialize()
  # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
end

Class Method Details

.active_group(appliance_name = nil) ⇒ Object

Provides the current active group information (just the ID right now)



511
512
513
514
515
516
517
518
519
# File 'lib/morpheus/cli/groups.rb', line 511

def active_group(appliance_name=nil)
  if appliance_name == nil
    appliance_name, appliance_url = Morpheus::Cli::Remote.active_appliance
  end
  if !appliance_name
    return nil
  end
  return active_groups_map[appliance_name.to_sym]
end

.active_group_id(appliance_name = nil) ⇒ Object

alias (unused)



522
523
524
# File 'lib/morpheus/cli/groups.rb', line 522

def active_group_id(appliance_name=nil)
  active_group(appliance_name)
end

.active_groupsObject



506
507
508
# File 'lib/morpheus/cli/groups.rb', line 506

def active_groups
  active_groups_map
end

.active_groups_mapObject

Provides the current active group information



502
503
504
# File 'lib/morpheus/cli/groups.rb', line 502

def active_groups_map
  @@groups ||= load_group_file || {}
end

.clear_active_group(appliance_name) ⇒ Object



532
533
534
535
536
# File 'lib/morpheus/cli/groups.rb', line 532

def clear_active_group(appliance_name)
  the_groups = active_groups_map
  the_groups.delete(appliance_name.to_sym)
  save_groups(the_groups)
end

.groups_file_pathObject



548
549
550
# File 'lib/morpheus/cli/groups.rb', line 548

def groups_file_path
  return File.join(Morpheus::Cli.home_directory, "groups")
end

.load_group_fileObject



538
539
540
541
542
543
544
545
546
# File 'lib/morpheus/cli/groups.rb', line 538

def load_group_file
  fn = groups_file_path
  if File.exist? fn
    print "#{dark} #=> loading groups file #{fn}#{reset}\n" if Morpheus::Logging.debug?
    return YAML.load_file(fn)
  else
    {}
  end
end

.save_groups(groups_map) ⇒ Object



552
553
554
555
556
# File 'lib/morpheus/cli/groups.rb', line 552

def save_groups(groups_map)
  File.open(groups_file_path, 'w') {|f| f.write groups_map.to_yaml } #Store
  FileUtils.chmod(0600, groups_file_path)
  @@groups = groups_map
end

.set_active_group(appliance_name, group_id) ⇒ Object



526
527
528
529
530
# File 'lib/morpheus/cli/groups.rb', line 526

def set_active_group(appliance_name, group_id)
  the_groups = active_groups_map
  the_groups[appliance_name.to_sym] = group_id
  save_groups(the_groups)
end

Instance Method Details

#add(args) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/morpheus/cli/groups.rb', line 125

def add(args)
  options = {}
  params = {}
  use_it = false
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name]")
    build_option_type_options(opts, options, add_group_option_types())
    # opts.on( '-l', '--location LOCATION', "Location" ) do |val|
    #   params[:location] = val
    # end
    opts.on( '--use', '--use', "Make this the current active group" ) do
      use_it = true
    end

    build_common_options(opts, options, [:options, :json, :dry_run, :remote])
    opts.footer = "Create a new group."
  end
  optparse.parse!(args)
  # if args.count < 1
  #   puts optparse
  #   exit 1
  # end
  connect(options)
  begin
    # group = {name: args[0], location: params[:location]}
    # payload = {group: group}
    group_payload = {}
    if args[0]
      group_payload[:name] = args[0]
      options[:options]['name'] = args[0] # to skip prompt
    end
    if params[:location]
      group_payload[:name] = params[:location]
      options[:options]['location'] = params[:location] # to skip prompt
    end
    all_option_types = add_group_option_types()
    params = Morpheus::Cli::OptionTypes.prompt(all_option_types, options[:options], @api_client, {})
    group_payload.merge!(params)
    payload = {group: group_payload}

    if options[:dry_run]
      print_dry_run @groups_interface.dry.create(payload)
      return
    end
    json_response = @groups_interface.create(payload)
    group = json_response['group']
    if use_it
      ::Morpheus::Cli::Groups.set_active_group(@appliance_name, group['id'])
    end
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "Added group #{group['name']}"
      list([])
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#add_cloud(args) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/morpheus/cli/groups.rb', line 239

def add_cloud(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name]", "CLOUD")
    build_common_options(opts, options, [:json, :dry_run, :remote])
    opts.footer = "Add a cloud to a group."
  end
  optparse.parse!(args)
  if args.count < 2
    puts optparse
    exit 1
  end
  connect(options)
  begin
    group = find_group_by_name_or_id(args[0])
    cloud = find_cloud_by_name_or_id(args[1])
    current_zones = group['zones']
    found_zone = current_zones.find {|it| it["id"] == cloud["id"] }
    if found_zone
      print_red_alert "Cloud #{cloud['name']} is already in group #{group['name']}."
      exit 1
    end
    new_zones = current_zones + [{'id' => cloud['id']}]
    payload = {group: {id: group["id"], zones: new_zones}}
    if options[:dry_run]
      print_dry_run @groups_interface.dry.update_zones(group["id"], payload)
      return
    end
    json_response = @groups_interface.update_zones(group["id"], payload)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "Added cloud #{cloud["id"]} to group #{group['name']}"
      #list([])
      get([group["id"]])
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#connect(opts) ⇒ Object



22
23
24
25
26
27
# File 'lib/morpheus/cli/groups.rb', line 22

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @groups_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).groups
  @clouds_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).clouds
  @active_group_id = Morpheus::Cli::Groups.active_groups[@appliance_name]
end

#get(args) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/morpheus/cli/groups.rb', line 79

def get(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:json, :remote])
    opts.footer = "This outputs details about a specific group."
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  connect(options)
  begin
    group = find_group_by_name_or_id(args[0])
    #json_response = @groups_interface.get(group['id'])
    json_response = {'group' => group}
    if options[:json]
      print JSON.pretty_generate(json_response), "\n"
      return
    end
    group = json_response['group']

    is_active = @active_group_id && (@active_group_id == group['id'])

    print "\n" ,cyan, bold, "Group Details\n","==================", reset, "\n\n"
    print cyan
    puts "ID: #{group['id']}"
    puts "Name: #{group['name']}"
    puts "Code: #{group['code']}"
    puts "Location: #{group['location']}"
    puts "Clouds: #{group['zones'].collect {|it| it['name'] }.join(', ')}"
    puts "Hosts: #{group['serverCount']}"
    if is_active
      puts "\n => This is the active group."
    end

    print reset,"\n"

    #puts instance
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#handle(args) ⇒ Object



29
30
31
# File 'lib/morpheus/cli/groups.rb', line 29

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/morpheus/cli/groups.rb', line 33

def list(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [:list, :json, :remote])
    opts.footer = "This outputs a paginated list of groups."
  end
  optparse.parse!(args)
  connect(options)
  begin
    [:phrase, :offset, :max, :sort, :direction].each do |k|
      params[k] = options[k] unless options[k].nil?
    end
    json_response = @groups_interface.get(params)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
      return
    end
    groups = json_response['groups']
    print "\n" ,cyan, bold, "Morpheus Groups\n","==================", reset, "\n\n"
    if groups.empty?
      puts yellow,"No groups currently configured.",reset
    else
      print_groups_table(groups)
      print_results_pagination(json_response)
      if @active_group_id
        active_group = groups.find { |it| it['id'] == @active_group_id }
        active_group = active_group || find_group_by_name_or_id(@active_group_id)
        #unless @appliances.keys.size == 1
          print cyan, "\n# => Currently using #{active_group['name']}\n", reset
        #end
      else
        unless options[:remote]
          print "\n# => No active group, see `groups use`\n", reset
        end
      end
    end
    print reset,"\n"
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end


429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/morpheus/cli/groups.rb', line 429

def print_current(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [])
    opts.footer = "Prints the name of the current active group"
  end
  optparse.parse!(args)
  connect(options)

  group = @active_group_id ? find_group_by_name_or_id(@active_group_id) : nil
  if group
    print cyan,group['name'].to_s,"\n",reset
  else
    print dark,"No active group. See `groups use`","\n",reset
  end
end

#remove(args) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/morpheus/cli/groups.rb', line 325

def remove(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name]")
    build_common_options(opts, options, [:json, :dry_run, :auto_confirm, :remote])
    opts.footer = "Delete a group."
    # more info to display here
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  connect(options)

  begin
    group = find_group_by_name_or_id(args[0])
    unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the group #{group['name']}?")
      exit
    end
    if options[:dry_run]
      print_dry_run @groups_interface.dry.destroy(group['id'])
      return
    end
    json_response = @groups_interface.destroy(group['id'])
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    elsif !options[:quiet]
      print_green_success "Removed group #{group['name']}"
      #list([])
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#remove_cloud(args) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/morpheus/cli/groups.rb', line 282

def remove_cloud(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name]", "CLOUD")
    build_common_options(opts, options, [:json, :dry_run, :remote])
    opts.footer = "Remove a cloud from a group."
  end
  optparse.parse!(args)
  if args.count < 2
    puts optparse
    exit 1
  end
  connect(options)
  begin
    group = find_group_by_name_or_id(args[0])
    cloud = find_cloud_by_name_or_id(args[1])
    current_zones = group['zones']
    found_zone = current_zones.find {|it| it["id"] == cloud["id"] }
    if !found_zone
      print_red_alert "Cloud #{cloud['name']} is not in group #{group['name']}."
      exit 1
    end
    new_zones = current_zones.reject {|it| it["id"] == cloud["id"] }
    payload = {group: {id: group["id"], zones: new_zones}}
    if options[:dry_run]
      print_dry_run @groups_interface.dry.update_zones(group["id"], payload)
      return
    end
    json_response = @groups_interface.update_zones(group["id"], payload)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "Removed cloud #{cloud['name']} from group #{group['name']}"
      # list([])
      get([group["id"]])
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#unuse(args) ⇒ Object



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/morpheus/cli/groups.rb', line 402

def unuse(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage()
    opts.footer = "" +
      "This will clear the current active group.\n" +
      "You will be prompted for a Group during provisioning."
    build_common_options(opts, options, [])
  end
  optparse.parse!(args)
  connect(options)

  if @active_group_id
    ::Morpheus::Cli::Groups.clear_active_group(@appliance_name)
    # unless options[:quiet]
    #   print cyan
    #   puts "Switched to no active group."
    #   puts "You will be prompted for Group during provisioning."
    #   print reset
    # end
    return true
  else
    puts "You are not using any group for appliance #{@appliance_name}"
    #return false
  end
end

#update(args) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/morpheus/cli/groups.rb', line 187

def update(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name] [options]")
    build_option_type_options(opts, options, update_group_option_types())
    # opts.on( '-l', '--location LOCATION', "Location" ) do |val|
    #   params[:location] = val
    # end
    build_common_options(opts, options, [:options, :json, :dry_run, :remote])
    opts.footer = "Update an existing group."
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  connect(options)
  begin
    group = find_group_by_name_or_id(args[0])
    group_payload = {id: group['id']}

    #params = Morpheus::Cli::OptionTypes.prompt(update_group_option_types, options[:options], @api_client, {})
    params = options[:options] || {}

    if params.empty?
      puts optparse
      exit 1
    end

    group_payload.merge!(params)

    payload = {group: group_payload}

    if options[:dry_run]
      print_dry_run @groups_interface.dry.update(group['id'], payload)
      return
    end
    json_response = @groups_interface.update(group['id'], payload)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      #list([])
      get([group["id"]])
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#use(args) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/morpheus/cli/groups.rb', line 363

def use(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[name]")
    opts.footer = "" +
      "This sets the active group.\n" +
      "The active group will be auto-selected for use during provisioning.\n" +
      "You can still use the --group option to override this."
    build_common_options(opts, options, [])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  connect(options)
  begin
    # todo: this is a problem for unprivileged users, need to use find_group_by_id_for_provisioning(group_id)
    group = find_group_by_name_or_id(args[0])
    if !group
      print_red_alert "Group not found by name #{args[0]}"
      exit 1
    end

    if @active_group_id == group['id']
      print reset,"Already using the group #{group['name']}","\n",reset
    else
      ::Morpheus::Cli::Groups.set_active_group(@appliance_name, group['id'])
      # ::Morpheus::Cli::Groups.save_groups(@active_groups)
      #print cyan,"Switched active group to #{group['name']}","\n",reset
      #list([])
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end

end