Class: Moose::Inventory::Cli::Group

Inherits:
Thor
  • Object
show all
Includes:
Helpers
Defined in:
lib/moose_inventory/cli/group.rb,
lib/moose_inventory/cli/group_rm.rb,
lib/moose_inventory/cli/group_add.rb,
lib/moose_inventory/cli/group_get.rb,
lib/moose_inventory/cli/group_list.rb,
lib/moose_inventory/cli/group_tags.rb,
lib/moose_inventory/cli/group_rmvar.rb,
lib/moose_inventory/cli/group_addvar.rb,
lib/moose_inventory/cli/group_rmhost.rb,
lib/moose_inventory/cli/group_addhost.rb,
lib/moose_inventory/cli/group_rmchild.rb,
lib/moose_inventory/cli/group_addchild.rb,
lib/moose_inventory/cli/group_listvars.rb

Overview

implementation of the “group listvars” method of the CLI

Constant Summary

Constants included from Helpers

Helpers::AUTOMATIC_GROUP

Instance Method Summary collapse

Instance Method Details

#add(*argv) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/moose_inventory/cli/group_add.rb', line 19

def add(*argv)
  abort_if_missing_args(argv, 1, '1 or more')
  validate_machine_plan_request!

  names = normalize_names(argv)
  hosts = csv_option_names(options[:hosts])

  abort_if_automatic_group(
    names,
    "ERROR: Cannot manually manipulate the automatic group 'ungrouped'\n"
  )

  result = build_operation(Moose::Inventory::Operations::AddGroups)
           .call(names: names, hosts: hosts, dry_run: options[:dry_run])
  return if machine_plan_output_rendered?(result, command: 'group add')

  record_audit({ command: 'group add', action: 'add', entity_type: 'group',
                 entity_names: names }, result: result, dry_run: options[:dry_run])
  render_add_groups_events(result.events)
  print_warning_summary(result, success_message: 'Succeeded')
end

#addchild(*argv) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/moose_inventory/cli/group_addchild.rb', line 19

def addchild(*argv)
  abort_if_missing_args(argv, 2, '2 or more')
  validate_machine_plan_request!

  pname = argv[0].downcase
  cnames = normalize_names(argv.slice(1, argv.length - 1))

  abort_if_automatic_group([pname] + cnames)

  result = add_children_to_group(pname, cnames)
  return if machine_plan_output_rendered?(result, command: 'group addchild')

  record_audit({ command: 'group addchild', action: 'associate_child', entity_type: 'group',
                 entity_names: pname }, result: result, dry_run: options[:dry_run])
  print_warning_summary(result)
end

#addhost(*args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/moose_inventory/cli/group_addhost.rb', line 19

def addhost(*args)
  abort_if_missing_args(args, 2, '2 or more')
  validate_machine_plan_request!

  name = args[0].downcase
  hosts = normalize_names(args.slice(1, args.length - 1))

  abort_if_automatic_group([name])

  result = add_hosts_to_group(name, hosts)
  return if machine_plan_output_rendered?(result, command: 'group addhost')

  record_audit({ command: 'group addhost', action: 'associate', entity_type: 'group',
                 entity_names: name }, result: result, dry_run: options[:dry_run])
  print_warning_summary(result)
end

#addtag(*args) ⇒ Object



8
9
10
11
12
# File 'lib/moose_inventory/cli/group_tags.rb', line 8

def addtag(*args)
  abort_if_missing_args(args, 2, '2 or more')

  add_tags('group', args[0].downcase, args.slice(1, args.length - 1))
end

#addvar(*args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/moose_inventory/cli/group_addvar.rb', line 18

def addvar(*args)
  abort_if_missing_args(args, 2, '2 or more')
  validate_machine_plan_request!

  name = args[0].downcase
  vars = args.slice(1, args.length - 1).uniq
  operation = build_operation(Moose::Inventory::Operations::AddVariables,
                              entity_type: :group,
                              emitter: machine_plan_emitter(group_addvar_emitter(name, vars)))

  result = db.transaction do
    operation.call(name: name, vars: vars, dry_run: options[:dry_run])
  end

  return if machine_plan_output_rendered?(result, command: 'group addvar')

  record_audit({ command: 'group addvar', action: 'add_variable', entity_type: 'group',
                 entity_names: name }, result: result, dry_run: options[:dry_run])
  print_success_summary
end

#get(*argv) ⇒ Object



13
14
15
16
17
18
# File 'lib/moose_inventory/cli/group_get.rb', line 13

def get(*argv)
  abort("ERROR: Wrong number of arguments, #{argv.length} for 1 or more") if argv.empty?

  names = normalize_names(argv)
  fmt.dump(inventory_query.get_groups(names: names), output_format)
end

#listObject



14
15
16
17
# File 'lib/moose_inventory/cli/group_list.rb', line 14

def list
  results = inventory_query.list_groups(ansible: ansible_mode?)
  fmt.dump(results, output_format)
end

#listtags(*args) ⇒ Object



25
26
27
28
29
# File 'lib/moose_inventory/cli/group_tags.rb', line 25

def listtags(*args)
  abort_if_missing_args(args, 1, '1')

  list_tags('group', args[0].downcase)
end

#listvars(*argv) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/moose_inventory/cli/group_listvars.rb', line 17

def listvars(*argv)
  validate_listvars_args(argv)

  names = normalize_names(argv)
  results = inventory_query.list_group_vars(names: names, ansible: ansible_mode?)
  warn_if_missing_ansible_listvars_entity(:group, names.first)

  fmt.dump(results, output_format)
end

#rm(*argv) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/moose_inventory/cli/group_rm.rb', line 24

def rm(*argv)
  abort_if_missing_args(argv, 1, '1 or more')
  validate_machine_plan_request!

  names = normalize_names(argv)

  abort_if_automatic_group(
    names,
    "Cannot manually manipulate the automatic group 'ungrouped'\n"
  )
  confirm_destructive_action!("group rm #{names.join(',')}")

  result = remove_groups(names)
  record_audit({ command: 'group rm', action: 'remove', entity_type: 'group',
                 entity_names: names }, result: result, dry_run: options[:dry_run])
  print_warning_summary(result) unless machine_plan_output_requested?
end

#rmchild(*argv) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/moose_inventory/cli/group_rmchild.rb', line 25

def rmchild(*argv)
  abort_if_missing_args(argv, 2, '2 or more')
  validate_machine_plan_request!

  pname = argv[0].downcase
  cnames = normalize_names(argv.slice(1, argv.length - 1))

  abort_if_automatic_group([pname] + cnames)
  confirm_destructive_action!("group rmchild #{pname} #{cnames.join(',')}")

  result = remove_children_from_group(pname, cnames)
  return if machine_plan_output_rendered?(result, command: 'group rmchild')

  record_audit({ command: 'group rmchild', action: 'dissociate_child', entity_type: 'group',
                 entity_names: pname }, result: result, dry_run: options[:dry_run])
  print_warning_summary(result)
end

#rmhost(*args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/moose_inventory/cli/group_rmhost.rb', line 20

def rmhost(*args)
  abort_if_missing_args(args, 2, '2 or more')
  validate_machine_plan_request!

  name = args[0].downcase
  hosts = normalize_names(args.slice(1, args.length - 1))

  abort_if_automatic_group([name])
  confirm_destructive_action!("group rmhost #{name} #{hosts.join(',')}")

  result = remove_hosts_from_group(name, hosts)
  return if machine_plan_output_rendered?(result, command: 'group rmhost')

  record_audit({ command: 'group rmhost', action: 'dissociate', entity_type: 'group',
                 entity_names: name }, result: result, dry_run: options[:dry_run])
  print_warning_summary(result)
end

#rmtag(*args) ⇒ Object



16
17
18
19
20
21
# File 'lib/moose_inventory/cli/group_tags.rb', line 16

def rmtag(*args)
  abort_if_missing_args(args, 2, '2 or more')
  confirm_destructive_action!("group rmtag #{args[0].downcase} #{args.slice(1, args.length - 1).join(',')}")

  remove_tags('group', args[0].downcase, args.slice(1, args.length - 1))
end

#rmvar(*args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/moose_inventory/cli/group_rmvar.rb', line 19

def rmvar(*args)
  abort_if_missing_args(args, 2, '2 or more')
  validate_machine_plan_request!

  name = args[0].downcase
  vars = args.slice(1, args.length - 1).uniq
  confirm_destructive_action!("group rmvar #{name} #{vars.join(',')}")
  operation = build_operation(Moose::Inventory::Operations::RemoveVariables,
                              entity_type: :group,
                              emitter: machine_plan_emitter(group_rmvar_emitter(name, vars)))

  result = db.transaction do
    operation.call(name: name, vars: vars, dry_run: options[:dry_run])
  end

  return if machine_plan_output_rendered?(result, command: 'group rmvar')

  record_audit({ command: 'group rmvar', action: 'remove_variable', entity_type: 'group',
                 entity_names: name }, result: result, dry_run: options[:dry_run])
  print_success_summary
end