Class: Moose::Inventory::Cli::Host

Inherits:
Thor
  • Object
show all
Includes:
Helpers
Defined in:
lib/moose_inventory/cli/host.rb,
lib/moose_inventory/cli/host_rm.rb,
lib/moose_inventory/cli/host_add.rb,
lib/moose_inventory/cli/host_get.rb,
lib/moose_inventory/cli/host_list.rb,
lib/moose_inventory/cli/host_tags.rb,
lib/moose_inventory/cli/host_rmvar.rb,
lib/moose_inventory/cli/host_addvar.rb,
lib/moose_inventory/cli/host_rmgroup.rb,
lib/moose_inventory/cli/host_addgroup.rb,
lib/moose_inventory/cli/host_listvars.rb

Overview

implementation of the “host listvars” method of the CLI

Constant Summary collapse

ADD_HOST_EVENT_RENDERERS =
{
  host_started: :render_add_host_started,
  creating_host: :render_add_host_creation,
  host_exists: :render_add_host_exists_warning,
  ok: :render_add_host_ok,
  adding_association: :render_add_host_association,
  group_missing_created: :render_add_host_missing_group_warning,
  association_exists: :render_add_host_association_exists_warning,
  adding_automatic_group: :render_add_host_automatic_group,
  host_complete: :render_add_host_complete,
  dry_run_summary: :render_dry_run_summary
}.freeze

Constants included from Helpers

Moose::Inventory::Cli::Helpers::AUTOMATIC_GROUP

Instance Method Summary collapse

Instance Method Details

#add(*argv) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/moose_inventory/cli/host_add.rb', line 37

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

  # Arguments
  names = normalize_names(argv)

  # split(/\W+/) splits on hyphens too, which is not what we want
  # groups = options[:groups].downcase.split(/\W+/).uniq
  groups = csv_option_names(options[:groups])

  # Sanity
  abort_if_automatic_group(groups)

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

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

#addgroup(*args) ⇒ Object



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

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

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

  abort_if_automatic_group(groups)

  result = add_groups_to_host(name, groups)
  unless machine_plan_output_rendered?(
    result, command: 'host addgroup'
  )
    record_audit({ command: 'host addgroup', action: 'associate', entity_type: 'host',
                   entity_names: name }, result: result, dry_run: options[:dry_run])
    print_warning_summary(result, success_message: 'Succeeded',
                                  warning_message: 'Succeeded')
  end
end

#addtag(*args) ⇒ Object



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

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

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

#addvar(*args) ⇒ Object



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

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: :host,
                              emitter: machine_plan_emitter(host_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: 'host addvar')

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

#get(*argv) ⇒ Object



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

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

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

#listObject



18
19
20
# File 'lib/moose_inventory/cli/host_list.rb', line 18

def list
  fmt.dump(inventory_query.list_hosts(filters: host_list_filters), output_format)
end

#listtags(*args) ⇒ Object



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

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

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

#listvars(*argv) ⇒ Object



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

def listvars(*argv)
  validate_listvars_args(argv)

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

  fmt.dump(results, output_format)
end

#rm(*argv) ⇒ Object



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

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

  names = normalize_names(argv)
  confirm_destructive_action!("host rm #{names.join(',')}")
  result = remove_hosts_operation.call(names: names, dry_run: options[:dry_run])
  return if machine_plan_output_rendered?(result, command: 'host rm')

  record_audit({ command: 'host rm', action: 'remove', entity_type: 'host',
                 entity_names: names }, result: result, dry_run: options[:dry_run])
  render_remove_hosts_events(result.events)
  print_warning_summary(result)
end

#rmgroup(*args) ⇒ Object



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

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

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

  abort_if_automatic_group(groups)
  confirm_destructive_action!("host rmgroup #{name} #{groups.join(',')}")

  result = remove_groups_from_host(name, groups)
  unless machine_plan_output_rendered?(
    result, command: 'host rmgroup'
  )
    record_audit({ command: 'host rmgroup', action: 'dissociate', entity_type: 'host',
                   entity_names: name }, result: result, dry_run: options[:dry_run])
    print_warning_summary(result, success_message: 'Succeeded',
                                  warning_message: 'Succeeded')
  end
end

#rmtag(*args) ⇒ Object



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

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

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

#rmvar(*args) ⇒ Object



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

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!("host rmvar #{name} #{vars.join(',')}")
  operation = build_operation(Moose::Inventory::Operations::RemoveVariables,
                              entity_type: :host,
                              emitter: machine_plan_emitter(host_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: 'host rmvar')

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