moose-inventory 1.0.8 → 2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ci.yml +49 -0
  3. data/.github/workflows/release.yml +58 -0
  4. data/.gitignore +1 -1
  5. data/.gitleaks.toml +9 -0
  6. data/.rubocop.yml +19 -784
  7. data/BACKLOG.md +290 -0
  8. data/Gemfile.lock +95 -0
  9. data/README.md +38 -9
  10. data/Rakefile +1 -1
  11. data/bin/moose-inventory +1 -1
  12. data/docs/release/publishing.md +109 -0
  13. data/docs/release/release-readiness.md +55 -0
  14. data/docs/security-audit-2026-05-21.md +71 -0
  15. data/docs/security-audit-2026-05-26-rerun.md +75 -0
  16. data/docs/security-audit-2026-05-26.md +63 -0
  17. data/lib/moose_inventory/cli/formatter.rb +16 -17
  18. data/lib/moose_inventory/cli/group.rb +4 -1
  19. data/lib/moose_inventory/cli/group_add.rb +89 -75
  20. data/lib/moose_inventory/cli/group_addchild.rb +84 -71
  21. data/lib/moose_inventory/cli/group_addhost.rb +78 -69
  22. data/lib/moose_inventory/cli/group_addvar.rb +37 -37
  23. data/lib/moose_inventory/cli/group_get.rb +23 -26
  24. data/lib/moose_inventory/cli/group_list.rb +12 -15
  25. data/lib/moose_inventory/cli/group_listvars.rb +12 -14
  26. data/lib/moose_inventory/cli/group_rm.rb +104 -76
  27. data/lib/moose_inventory/cli/group_rmchild.rb +99 -54
  28. data/lib/moose_inventory/cli/group_rmhost.rb +64 -60
  29. data/lib/moose_inventory/cli/group_rmvar.rb +5 -5
  30. data/lib/moose_inventory/cli/helpers.rb +76 -0
  31. data/lib/moose_inventory/cli/host.rb +4 -1
  32. data/lib/moose_inventory/cli/host_add.rb +51 -66
  33. data/lib/moose_inventory/cli/host_addgroup.rb +77 -68
  34. data/lib/moose_inventory/cli/host_addvar.rb +6 -6
  35. data/lib/moose_inventory/cli/host_get.rb +15 -18
  36. data/lib/moose_inventory/cli/host_list.rb +3 -3
  37. data/lib/moose_inventory/cli/host_listvars.rb +21 -23
  38. data/lib/moose_inventory/cli/host_rm.rb +9 -9
  39. data/lib/moose_inventory/cli/host_rmgroup.rb +63 -60
  40. data/lib/moose_inventory/cli/host_rmvar.rb +3 -3
  41. data/lib/moose_inventory/config/config.rb +43 -40
  42. data/lib/moose_inventory/db/db.rb +92 -52
  43. data/lib/moose_inventory/db/models.rb +11 -12
  44. data/lib/moose_inventory/inventory_context.rb +50 -0
  45. data/lib/moose_inventory/operations/add_associations.rb +127 -0
  46. data/lib/moose_inventory/operations/add_groups.rb +115 -0
  47. data/lib/moose_inventory/operations/add_hosts.rb +110 -0
  48. data/lib/moose_inventory/operations/group_child_relations.rb +118 -0
  49. data/lib/moose_inventory/operations/group_cleanup.rb +55 -0
  50. data/lib/moose_inventory/operations/remove_associations.rb +101 -0
  51. data/lib/moose_inventory/operations/remove_groups.rb +79 -0
  52. data/lib/moose_inventory/version.rb +1 -1
  53. data/moose-inventory.gemspec +38 -20
  54. data/scripts/check.sh +10 -0
  55. data/scripts/ci/check_permissions.sh +35 -0
  56. data/scripts/ci/check_rubocop.sh +28 -0
  57. data/scripts/ci/check_secrets.sh +26 -0
  58. data/scripts/ci/check_security.sh +68 -0
  59. data/scripts/ci/install_security_tools.sh +47 -0
  60. data/scripts/ci/package_sanity.sh +46 -0
  61. data/scripts/files.rb +1 -4
  62. data/scripts/install_dependencies.sh +19 -0
  63. data/scripts/reports.sh +2 -2
  64. data/spec/lib/moose_inventory/cli/cli_spec.rb +13 -14
  65. data/spec/lib/moose_inventory/cli/group_add_spec.rb +118 -119
  66. data/spec/lib/moose_inventory/cli/group_addchild_spec.rb +49 -51
  67. data/spec/lib/moose_inventory/cli/group_addhost_spec.rb +80 -83
  68. data/spec/lib/moose_inventory/cli/group_addvar_spec.rb +91 -91
  69. data/spec/lib/moose_inventory/cli/group_get_spec.rb +22 -23
  70. data/spec/lib/moose_inventory/cli/group_list_spec.rb +19 -20
  71. data/spec/lib/moose_inventory/cli/group_listvar_spec.rb +35 -36
  72. data/spec/lib/moose_inventory/cli/group_rm_spec.rb +115 -78
  73. data/spec/lib/moose_inventory/cli/group_rmchild_spec.rb +86 -45
  74. data/spec/lib/moose_inventory/cli/group_rmhost_spec.rb +43 -46
  75. data/spec/lib/moose_inventory/cli/group_rmvar_spec.rb +131 -131
  76. data/spec/lib/moose_inventory/cli/group_spec.rb +9 -9
  77. data/spec/lib/moose_inventory/cli/host_add_spec.rb +103 -43
  78. data/spec/lib/moose_inventory/cli/host_addgroup_spec.rb +78 -80
  79. data/spec/lib/moose_inventory/cli/host_addvar_spec.rb +122 -122
  80. data/spec/lib/moose_inventory/cli/host_get_spec.rb +16 -16
  81. data/spec/lib/moose_inventory/cli/host_list_spec.rb +8 -8
  82. data/spec/lib/moose_inventory/cli/host_listvar_spec.rb +50 -52
  83. data/spec/lib/moose_inventory/cli/host_rm_spec.rb +12 -12
  84. data/spec/lib/moose_inventory/cli/host_rmgroup_spec.rb +48 -51
  85. data/spec/lib/moose_inventory/cli/host_rmvar_spec.rb +136 -136
  86. data/spec/lib/moose_inventory/config/config_spec.rb +16 -3
  87. data/spec/lib/moose_inventory/db/db_spec.rb +386 -2
  88. data/spec/lib/moose_inventory/db/models_spec.rb +10 -11
  89. data/spec/lib/moose_inventory/operations/add_associations_spec.rb +77 -0
  90. data/spec/lib/moose_inventory/operations/add_groups_spec.rb +65 -0
  91. data/spec/lib/moose_inventory/operations/add_hosts_spec.rb +69 -0
  92. data/spec/lib/moose_inventory/operations/group_child_relations_spec.rb +76 -0
  93. data/spec/lib/moose_inventory/operations/remove_associations_spec.rb +78 -0
  94. data/spec/lib/moose_inventory/operations/remove_groups_spec.rb +57 -0
  95. data/spec/shared/shared_config_setup.rb +2 -2
  96. data/spec/spec_helper.rb +7 -8
  97. metadata +157 -105
  98. data/.coveralls.yml +0 -0
  99. data/Guardfile +0 -38
  100. data/config/dotfiles/coveralls.yml +0 -0
  101. data/config/dotfiles/gitignore +0 -20
  102. data/config/dotfiles/rubocop.yml +0 -793
  103. data/scripts/guard_quality.sh +0 -3
  104. data/scripts/guard_test.sh +0 -2
@@ -1,10 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'thor'
2
4
  require 'json'
3
5
  require 'indentation'
4
6
 
5
- require_relative './formatter.rb'
6
- require_relative '../db/exceptions.rb'
7
-
7
+ require_relative 'formatter'
8
+ require_relative '../db/exceptions'
9
+ require_relative '../inventory_context'
10
+ require_relative '../operations/add_hosts'
11
+
8
12
  module Moose
9
13
  module Inventory
10
14
  module Cli
@@ -15,78 +19,59 @@ module Moose
15
19
  desc 'add HOSTNAME_1 [HOSTNAME_2 ...]',
16
20
  'Add a hosts HOSTNAME_n to the inventory'
17
21
  option :groups
18
- # rubocop:disable Metrics/LineLength
19
- def add(*argv) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
20
- # rubocop:enable Metrics/LineLength
21
- if argv.length < 1
22
- abort('ERROR: Wrong number of arguments, '\
23
- "#{argv.length} for 1 or more.")
24
- end
22
+ def add(*argv)
23
+ abort_if_missing_args(argv, 1, '1 or more')
25
24
 
26
- # Convenience
27
- db = Moose::Inventory::DB
28
- fmt = Moose::Inventory::Cli::Formatter
29
-
30
25
  # Arguments
31
- names = argv.uniq.map(&:downcase)
26
+ names = normalize_names(argv)
32
27
 
33
28
  # split(/\W+/) splits on hyphens too, which is not what we want
34
- #groups = options[:groups].downcase.split(/\W+/).uniq
35
- options[:groups].nil? && options[:groups] = ''
36
- groups = options[:groups].downcase.split(',').uniq
29
+ # groups = options[:groups].downcase.split(/\W+/).uniq
30
+ groups = csv_option_names(options[:groups])
37
31
 
38
32
  # Sanity
39
- if groups.include?('ungrouped')
40
- abort("ERROR: Cannot manually manipulate "\
41
- "the automatic group 'ungrouped'.")
42
- end
33
+ abort_if_automatic_group(groups)
43
34
 
44
- # Process
45
- db.transaction do # Transaction start
46
- fmt.reset_indent
47
-
48
- names.each do |name|
49
- puts "Add host '#{name}':"
50
- fmt.puts 2, "- Creating host '#{name}'..."
51
- host = db.models[:host].find(name: name)
52
- groups_ds = nil
53
- if host.nil?
54
- host = db.models[:host].create(name: name)
55
- else
56
- fmt.warn "The host '#{name}' already exists, skipping creation.\n"
57
- groups_ds = host.groups_dataset
58
- end
59
- fmt.puts 4, "- OK"
60
-
61
- groups.each do |g|
62
- next if g.nil? || g.empty?
63
- fmt.puts 2, "- Adding association {host:#{name} <-> group:#{g}}..."
64
- group = db.models[:group].find(name: g)
65
- if group.nil?
66
- fmt.warn "The group '#{g}' doesn't exist, but will be created.\n"
67
- group = db.models[:group].create(name: g)
68
- end
69
- if !groups_ds.nil? && groups_ds[name: g].nil?
70
- fmt.warn "Association {host:#{name} <-> group:#{ g }} already exists, skipping creation.\n"
71
- else
72
- host.add_group(group)
73
- end
74
- fmt.puts 4, '- OK'
75
- end
76
-
77
- # Handle the automatic 'ungrouped' group
78
- groups_ds = host.groups_dataset
79
- if !groups_ds.nil? && groups_ds.count == 0
80
- fmt.puts 2, "- Adding automatic association {host:#{name} <-> group:ungrouped}..."
81
- ungrouped = db.models[:group].find_or_create(name: 'ungrouped')
82
- host.add_group(ungrouped)
83
- fmt.puts 4, "- OK"
84
- end
85
- fmt.puts 2, "- All OK"
86
- end
87
- end # Transaction end
35
+ result = Moose::Inventory::Operations::AddHosts
36
+ .new(context: Moose::Inventory::InventoryContext.new(db: db))
37
+ .call(names: names, groups: groups)
38
+ render_add_hosts_events(result.events)
88
39
  puts 'Succeeded'
89
40
  end
41
+
42
+ private
43
+
44
+ def render_add_hosts_events(events)
45
+ fmt.reset_indent
46
+ events.each { |event| render_add_hosts_event(event) }
47
+ end
48
+
49
+ def render_add_hosts_event(event) # rubocop:disable Metrics/CyclomaticComplexity
50
+ payload = event.payload
51
+ case event.type
52
+ when :host_started
53
+ puts "Add host '#{payload[:name]}':"
54
+ when :creating_host
55
+ fmt.puts 2, "- Creating host '#{payload[:name]}'..."
56
+ when :host_exists
57
+ fmt.warn "The host '#{payload[:name]}' already exists, skipping creation.\n"
58
+ when :ok
59
+ fmt.puts payload[:indent], '- OK'
60
+ when :adding_association
61
+ fmt.puts 2, "- Adding association {host:#{payload[:host]} <-> group:#{payload[:group]}}..."
62
+ when :group_missing_created
63
+ fmt.warn "The group '#{payload[:name]}' doesn't exist, but will be created.\n"
64
+ when :association_exists
65
+ fmt.warn(
66
+ "Association {host:#{payload[:host]} <-> group:#{payload[:group]}} " \
67
+ "already exists, skipping creation.\n"
68
+ )
69
+ when :adding_automatic_group
70
+ fmt.puts 2, "- Adding automatic association {host:#{payload[:host]} <-> group:#{payload[:group]}}..."
71
+ when :host_complete
72
+ fmt.puts 2, '- All OK'
73
+ end
74
+ end
90
75
  end
91
76
  end
92
77
  end
@@ -1,8 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'thor'
2
4
  require 'json'
3
5
 
4
- require_relative './formatter.rb'
5
- require_relative '../db/exceptions.rb'
6
+ require_relative 'formatter'
7
+ require_relative '../db/exceptions'
8
+ require_relative '../inventory_context'
9
+ require_relative '../operations/add_associations'
6
10
 
7
11
  module Moose
8
12
  module Inventory
@@ -12,76 +16,81 @@ module Moose
12
16
  class Host
13
17
  desc 'addgroup HOSTNAME GROUPNAME [GROUPNAME ...]',
14
18
  'Associate the host with a group'
15
- # rubocop:disable Metrics/LineLength
16
- def addgroup(*args) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
17
- # rubocop:enable Metrics/LineLength
18
- # Sanity
19
- if args.length < 2
20
- abort('ERROR: Wrong number of arguments, '\
21
- "#{args.length} for 2 or more.")
22
- end
19
+ def addgroup(*args)
20
+ abort_if_missing_args(args, 2, '2 or more')
21
+
22
+ name = args[0].downcase
23
+ groups = normalize_names(args.slice(1, args.length - 1))
24
+
25
+ abort_if_automatic_group(groups)
23
26
 
24
- # Convenience
25
- db = Moose::Inventory::DB
26
- fmt = Moose::Inventory::Cli::Formatter
27
-
28
- # Arguments
29
- name = args[0].downcase
30
- groups = args.slice(1, args.length - 1).uniq.map(&:downcase)
31
-
32
- # Sanity
33
- if groups.include?('ungrouped')
34
- abort 'ERROR: Cannot manually manipulate the automatic '\
35
- 'group \'ungrouped\'.'
36
- end
37
-
38
- # Transaction
39
- db.transaction do # Transaction start
40
- puts "Associate host '#{name}' with groups '#{groups.join(',')}':"
41
- # Get the target host
42
- fmt.puts 2, "- Retrieve host '#{name}'..."
43
- host = db.models[:host].find(name: name)
44
- if host.nil?
45
- fail db.exceptions[:moose], "The host '#{name}' "\
46
- 'was not found in the database.'
47
- end
48
- fmt.puts 4, '- OK'
49
-
50
- # Associate host with the groups
51
- groups_ds = host.groups_dataset
52
- groups.each do |g|
53
- fmt.puts 2, "- Add association {host:#{name} <-> group:#{ g }}..."
54
-
55
- # Check against existing associations
56
- if !groups_ds[name: g].nil?
57
- fmt.warn "Association {host:#{name} <-> group:#{g}} already exists, skipping."
58
- fmt.puts 4, "- Already exists, skipping."
59
- else
60
- # Add new association
61
- group = db.models[:group].find(name: g)
62
- if group.nil?
63
- fmt.warn "Group '#{g}' does not exist and will be created."
64
- fmt.puts 4, "- Group does not exist, creating now..."
65
- group = db.models[:group].create(name: g)
66
- fmt.puts 6, "- OK"
67
- end
68
- host.add_group(group)
69
- end
70
- fmt.puts 4, '- OK'
71
- end
72
-
73
- # Handle 'ungrouped' group automation
74
- unless groups_ds[name: 'ungrouped'].nil?
75
- fmt.puts 2, '- Remove automatic association '\
76
- "{host:#{name} <-> group:ungrouped}..."
77
- ungrouped = db.models[:group].find(name: 'ungrouped')
78
- host.remove_group(ungrouped) unless ungrouped.nil?
79
- fmt.puts 4, '- OK'
80
- end
27
+ context = Moose::Inventory::InventoryContext.new(db: db)
28
+ operation = Moose::Inventory::Operations::AddAssociations.new(context: context)
29
+
30
+ db.transaction do
31
+ puts "Associate host '#{name}' with groups '#{groups.join(',')}':"
32
+ host = fetch_existing_host_for_addgroup(context, name)
33
+ render_host_addgroup_events(
34
+ operation.host_to_groups(host: host, host_name: name, group_names: groups).events
35
+ )
81
36
  fmt.puts 2, '- All OK'
82
- end # Transaction end
37
+ end
38
+
83
39
  puts 'Succeeded'
84
40
  end
41
+
42
+ private
43
+
44
+ def fetch_existing_host_for_addgroup(context, name)
45
+ fmt.puts 2, "- Retrieve host '#{name}'..."
46
+ host = context.find_host(name)
47
+ raise db.exceptions[:moose], "The host '#{name}' was not found in the database." if host.nil?
48
+
49
+ fmt.puts 4, '- OK'
50
+ host
51
+ end
52
+
53
+ def render_host_addgroup_events(events)
54
+ events.each { |event| render_host_addgroup_event(event) }
55
+ end
56
+
57
+ def render_host_addgroup_event(event)
58
+ payload = event.payload
59
+
60
+ return render_host_addgroup_warning(event.type, payload) if host_addgroup_warning?(event.type)
61
+ return render_host_addgroup_status(payload) if event.type == :already_exists_skipping
62
+
63
+ render_host_addgroup_output(event.type, payload)
64
+ end
65
+
66
+ def host_addgroup_warning?(type)
67
+ %i[host_group_association_exists group_missing_created].include?(type)
68
+ end
69
+
70
+ def render_host_addgroup_warning(type, payload)
71
+ if type == :host_group_association_exists
72
+ fmt.warn "Association {host:#{payload[:host]} <-> group:#{payload[:group]}} already exists, skipping."
73
+ else
74
+ fmt.warn "Group '#{payload[:name]}' does not exist and will be created."
75
+ end
76
+ end
77
+
78
+ def render_host_addgroup_status(payload)
79
+ fmt.puts payload[:indent], '- Already exists, skipping.'
80
+ end
81
+
82
+ def render_host_addgroup_output(type, payload)
83
+ case type
84
+ when :adding_host_group_association
85
+ fmt.puts 2, "- Add association {host:#{payload[:host]} <-> group:#{payload[:group]}}..."
86
+ when :group_creating_now
87
+ fmt.puts 4, '- Group does not exist, creating now...'
88
+ when :removing_automatic_group
89
+ fmt.puts 2, "- Remove automatic association {host:#{payload[:host]} <-> group:ungrouped}..."
90
+ when :ok
91
+ fmt.puts payload[:indent], '- OK'
92
+ end
93
+ end
85
94
  end
86
95
  end
87
96
  end
@@ -30,9 +30,9 @@ module Moose
30
30
 
31
31
  # Transaction
32
32
  db.transaction do # Transaction start
33
- puts "Add variables '#{vars.join(",")}' to host '#{name}':"
34
-
35
- fmt.puts 2,"- retrieve host '#{name}'..."
33
+ puts "Add variables '#{vars.join(',')}' to host '#{name}':"
34
+
35
+ fmt.puts 2, "- retrieve host '#{name}'..."
36
36
  host = db.models[:host].find(name: name)
37
37
  if host.nil?
38
38
  fail db.exceptions[:moose],
@@ -44,11 +44,11 @@ module Moose
44
44
  vars.each do |v|
45
45
  fmt.puts 2, "- add variable '#{v}'..."
46
46
  vararray = v.split('=')
47
- if v.start_with?('=') || v.end_with?('=') || vararray.length != 2
47
+ if v.start_with?('=') || v.end_with?('=') || vararray.length != 2
48
48
  fail db.exceptions[:moose],
49
49
  "Incorrect format in '{#{v}}'. Expected 'key=value'."
50
50
  end
51
-
51
+
52
52
  # Check against existing associations
53
53
  hostvar = hostvars_ds[name: vararray[0]]
54
54
  if !hostvar.nil?
@@ -61,7 +61,7 @@ module Moose
61
61
  else
62
62
  # hostvar doesn't exist, so create and associate
63
63
  hostvar = db.models[:hostvar].create(name: vararray[0],
64
- value: vararray[1])
64
+ value: vararray[1])
65
65
  host.add_hostvar(hostvar)
66
66
  end
67
67
  fmt.puts 4, '- OK'
@@ -16,7 +16,7 @@ module Moose
16
16
  desc 'get HOST_1 [HOST_2 ...]',
17
17
  'Get hosts HOST_n from the inventory'
18
18
  def get(*argv) # rubocop:disable Metrics/AbcSize
19
- if argv.length < 1
19
+ if argv.empty?
20
20
  abort('ERROR: Wrong number of arguments, '\
21
21
  "#{argv.length} for 1 or more")
22
22
  end
@@ -24,30 +24,27 @@ module Moose
24
24
  # Convenience
25
25
  db = Moose::Inventory::DB
26
26
  fmt = Moose::Inventory::Cli::Formatter
27
-
27
+
28
28
  # Arguments
29
29
  names = argv.uniq.map(&:downcase)
30
30
 
31
31
  # Process
32
32
  results = {}
33
- names.each do |name| # rubocop:disable Style/Next
33
+ names.each do |name|
34
34
  host = db.models[:host].find(name: name)
35
35
 
36
- unless host.nil?
37
- groups = host.groups_dataset.map(:name)
38
-
39
- hostvars = {}
40
- host.hostvars_dataset.each do |hv|
41
- hostvars[hv[:name].to_sym] = hv[:value]
42
- end
43
-
44
- results[host[:name].to_sym] = {}
45
- unless groups.length == 0
46
- results[host[:name].to_sym][:groups] = groups
47
- end
48
- unless hostvars.length == 0
49
- results[host[:name].to_sym][:hostvars] = hostvars
50
- end
36
+ next if host.nil?
37
+ groups = host.groups_dataset.map(:name)
38
+
39
+ hostvars = {}
40
+ host.hostvars_dataset.each do |hv|
41
+ hostvars[hv[:name].to_sym] = hv[:value]
42
+ end
43
+
44
+ results[host[:name].to_sym] = {}
45
+ results[host[:name].to_sym][:groups] = groups unless groups.empty?
46
+ unless hostvars.empty?
47
+ results[host[:name].to_sym][:hostvars] = hostvars
51
48
  end
52
49
  end
53
50
 
@@ -24,11 +24,11 @@ module Moose
24
24
  results[host[:name].to_sym][:groups] = groups
25
25
 
26
26
  hostvars = {}
27
- host.hostvars_dataset.each do |hv|
28
- hostvars[hv[:name].to_sym] = hv[:value]
27
+ host.hostvars_dataset.each do |hv|
28
+ hostvars[hv[:name].to_sym] = hv[:value]
29
29
  end
30
30
 
31
- unless hostvars.length == 0
31
+ unless hostvars.empty?
32
32
  results[host[:name].to_sym][:hostvars] = hostvars
33
33
  end
34
34
  end
@@ -15,32 +15,31 @@ module Moose
15
15
  def listvars(*argv)
16
16
  # Convenience
17
17
  confopts = Moose::Inventory::Config._confopts
18
-
18
+
19
19
  # sanity
20
20
  if confopts[:ansible] == true
21
21
  if argv.length != 1
22
- abort('ERROR: Wrong number of arguments for Ansible mode, '\
23
- "#{args.length} for 1.")
22
+ abort('ERROR: Wrong number of arguments for Ansible mode, '\
23
+ "#{args.length} for 1.")
24
24
  end
25
25
  else
26
- if argv.length < 1
26
+ if argv.empty?
27
27
  abort('ERROR: Wrong number of arguments, '\
28
28
  "#{args.length} for 1 or more.")
29
29
  end
30
30
  end
31
31
 
32
-
33
32
  # Convenience
34
33
  db = Moose::Inventory::DB
35
- fmt = Moose::Inventory::Cli::Formatter
34
+ fmt = Moose::Inventory::Cli::Formatter
36
35
 
37
36
  # Arguments
38
37
  names = argv.uniq.map(&:downcase)
39
38
 
40
- #process
39
+ # process
41
40
  results = {}
42
-
43
- if confopts[:ansible] == true
41
+
42
+ if confopts[:ansible] == true
44
43
  # This is the implementation per Ansible specs
45
44
  name = names.first
46
45
  host = db.models[:host].find(name: name)
@@ -51,28 +50,27 @@ module Moose
51
50
  results[hv[:name].to_sym] = hv[:value]
52
51
  end
53
52
  end
54
-
55
- # Add the Ansible 1.3 '_meta' tag
53
+
54
+ # Add the Ansible 1.3 '_meta' tag
56
55
  # see http://docs.ansible.com/ansible/developing_inventory.html#tuning-the-external-inventory-script
57
- results['_meta'.to_sym] = {}
56
+ results['_meta'.to_sym] = {}
58
57
  results['_meta'.to_sym]['hostvars'.to_sym] = {}
59
- db.models[:host].each do |host|
60
- results['_meta'.to_sym]['hostvars'.to_sym][host.name().to_sym] = {}
58
+ db.models[:host].each do |host|
59
+ results['_meta'.to_sym]['hostvars'.to_sym][host.name.to_sym] = {}
61
60
  host.hostvars_dataset.each do |hv|
62
- results['_meta'.to_sym]['hostvars'.to_sym][host.name().to_sym][hv[:name].to_sym] = hv[:value]
61
+ results['_meta'.to_sym]['hostvars'.to_sym][host.name.to_sym][hv[:name].to_sym] = hv[:value]
63
62
  end
64
63
  end
65
-
64
+
66
65
  else
67
- # This our more flexible implementation, which is not compatible
68
- # with the Ansible specs
66
+ # This our more flexible implementation, which is not compatible
67
+ # with the Ansible specs
69
68
  names.each do |name|
70
69
  host = db.models[:host].find(name: name)
71
- unless host.nil?
72
- results[name.to_sym] = {}
73
- host.hostvars_dataset.each do |hv|
74
- results[name.to_sym][hv[:name].to_sym] = hv[:value]
75
- end
70
+ next if host.nil?
71
+ results[name.to_sym] = {}
72
+ host.hostvars_dataset.each do |hv|
73
+ results[name.to_sym][hv[:name].to_sym] = hv[:value]
76
74
  end
77
75
  end
78
76
  end
@@ -16,7 +16,7 @@ module Moose
16
16
  def rm(*argv) # rubocop:disable Metrics/AbcSize
17
17
  #
18
18
  # Sanity
19
- if argv.length < 1
19
+ if argv.empty?
20
20
  abort('ERROR: Wrong number of arguments, '\
21
21
  "#{argv.length} for 1 or more.")
22
22
  end
@@ -24,7 +24,7 @@ module Moose
24
24
  # Convenience
25
25
  db = Moose::Inventory::DB
26
26
  fmt = Moose::Inventory::Cli::Formatter
27
-
27
+
28
28
  # Arguments
29
29
  names = argv.uniq.map(&:downcase)
30
30
 
@@ -38,22 +38,22 @@ module Moose
38
38
  if host.nil?
39
39
  warn_count += 1
40
40
  fmt.warn "Host '#{name}' does not exist, skipping.\n"
41
- fmt.puts 4, "- No such host, skipping."
41
+ fmt.puts 4, '- No such host, skipping.'
42
42
  end
43
- fmt.puts 4, "- OK"
43
+ fmt.puts 4, '- OK'
44
44
  unless host.nil?
45
45
  fmt.puts 2, "- Destroy host '#{name}'..."
46
46
  host.remove_all_groups
47
47
  host.destroy
48
- fmt.puts 4, "- OK"
48
+ fmt.puts 4, '- OK'
49
49
  end
50
- fmt.puts 2, "- All OK"
50
+ fmt.puts 2, '- All OK'
51
51
  end
52
52
  end # Transaction end
53
- if warn_count == 0
54
- puts "Succeeded."
53
+ if warn_count == 0
54
+ puts 'Succeeded.'
55
55
  else
56
- puts "Succeeded, with warnings."
56
+ puts 'Succeeded, with warnings.'
57
57
  end
58
58
  end
59
59
  end