moose-inventory 1.0.8 → 1.0.9
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.
- checksums.yaml +5 -5
- data/.github/workflows/ci.yml +35 -0
- data/.gitignore +1 -1
- data/BACKLOG.md +184 -0
- data/Gemfile.lock +60 -0
- data/README.md +14 -5
- data/bin/moose-inventory +1 -1
- data/docs/release/publishing.md +113 -0
- data/docs/release/release-readiness.md +41 -0
- data/docs/security-audit-2026-05-21.md +71 -0
- data/lib/moose_inventory/cli/formatter.rb +16 -17
- data/lib/moose_inventory/cli/group.rb +1 -1
- data/lib/moose_inventory/cli/group_add.rb +19 -21
- data/lib/moose_inventory/cli/group_addchild.rb +36 -40
- data/lib/moose_inventory/cli/group_addhost.rb +14 -18
- data/lib/moose_inventory/cli/group_addvar.rb +37 -37
- data/lib/moose_inventory/cli/group_get.rb +23 -26
- data/lib/moose_inventory/cli/group_list.rb +12 -15
- data/lib/moose_inventory/cli/group_listvars.rb +12 -14
- data/lib/moose_inventory/cli/group_rm.rb +21 -23
- data/lib/moose_inventory/cli/group_rmchild.rb +5 -6
- data/lib/moose_inventory/cli/group_rmhost.rb +12 -16
- data/lib/moose_inventory/cli/group_rmvar.rb +5 -5
- data/lib/moose_inventory/cli/host.rb +1 -1
- data/lib/moose_inventory/cli/host_add.rb +18 -18
- data/lib/moose_inventory/cli/host_addgroup.rb +9 -9
- data/lib/moose_inventory/cli/host_addvar.rb +6 -6
- data/lib/moose_inventory/cli/host_get.rb +15 -18
- data/lib/moose_inventory/cli/host_list.rb +3 -3
- data/lib/moose_inventory/cli/host_listvars.rb +21 -23
- data/lib/moose_inventory/cli/host_rm.rb +9 -9
- data/lib/moose_inventory/cli/host_rmgroup.rb +5 -5
- data/lib/moose_inventory/cli/host_rmvar.rb +3 -3
- data/lib/moose_inventory/config/config.rb +43 -40
- data/lib/moose_inventory/db/db.rb +70 -50
- data/lib/moose_inventory/db/models.rb +11 -12
- data/lib/moose_inventory/version.rb +1 -1
- data/moose-inventory.gemspec +35 -20
- data/scripts/check.sh +8 -0
- data/scripts/ci/check_permissions.sh +32 -0
- data/scripts/ci/check_security.sh +50 -0
- data/scripts/ci/package_sanity.sh +46 -0
- data/scripts/files.rb +1 -4
- data/scripts/install_dependencies.sh +17 -0
- data/scripts/reports.sh +2 -2
- data/spec/lib/moose_inventory/cli/cli_spec.rb +13 -14
- data/spec/lib/moose_inventory/cli/group_add_spec.rb +118 -119
- data/spec/lib/moose_inventory/cli/group_addchild_spec.rb +49 -51
- data/spec/lib/moose_inventory/cli/group_addhost_spec.rb +80 -83
- data/spec/lib/moose_inventory/cli/group_addvar_spec.rb +91 -91
- data/spec/lib/moose_inventory/cli/group_get_spec.rb +22 -23
- data/spec/lib/moose_inventory/cli/group_list_spec.rb +19 -20
- data/spec/lib/moose_inventory/cli/group_listvar_spec.rb +35 -36
- data/spec/lib/moose_inventory/cli/group_rm_spec.rb +75 -78
- data/spec/lib/moose_inventory/cli/group_rmchild_spec.rb +41 -45
- data/spec/lib/moose_inventory/cli/group_rmhost_spec.rb +43 -46
- data/spec/lib/moose_inventory/cli/group_rmvar_spec.rb +131 -131
- data/spec/lib/moose_inventory/cli/group_spec.rb +9 -9
- data/spec/lib/moose_inventory/cli/host_add_spec.rb +103 -43
- data/spec/lib/moose_inventory/cli/host_addgroup_spec.rb +78 -80
- data/spec/lib/moose_inventory/cli/host_addvar_spec.rb +122 -122
- data/spec/lib/moose_inventory/cli/host_get_spec.rb +16 -16
- data/spec/lib/moose_inventory/cli/host_list_spec.rb +8 -8
- data/spec/lib/moose_inventory/cli/host_listvar_spec.rb +50 -52
- data/spec/lib/moose_inventory/cli/host_rm_spec.rb +12 -12
- data/spec/lib/moose_inventory/cli/host_rmgroup_spec.rb +48 -51
- data/spec/lib/moose_inventory/cli/host_rmvar_spec.rb +136 -136
- data/spec/lib/moose_inventory/config/config_spec.rb +16 -3
- data/spec/lib/moose_inventory/db/db_spec.rb +224 -2
- data/spec/lib/moose_inventory/db/models_spec.rb +10 -11
- data/spec/shared/shared_config_setup.rb +2 -2
- data/spec/spec_helper.rb +7 -8
- metadata +99 -136
- data/.coveralls.yml +0 -0
- data/.rubocop.yml +0 -793
- data/Guardfile +0 -38
- data/config/dotfiles/coveralls.yml +0 -0
- data/config/dotfiles/gitignore +0 -20
- data/config/dotfiles/rubocop.yml +0 -793
- data/scripts/guard_quality.sh +0 -3
- data/scripts/guard_test.sh +0 -2
|
@@ -7,20 +7,19 @@ module Moose
|
|
|
7
7
|
##
|
|
8
8
|
# Implementation of "group rm" methods of the CLI
|
|
9
9
|
class Group
|
|
10
|
-
|
|
11
10
|
#==========================
|
|
12
11
|
desc 'rm NAME',
|
|
13
12
|
'Remove a group NAME from the inventory'
|
|
14
13
|
def rm(*argv) # rubocop:disable Metrics/AbcSize
|
|
15
14
|
#
|
|
16
15
|
# Sanity
|
|
17
|
-
if argv.
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
end
|
|
21
|
-
|
|
16
|
+
if argv.empty?
|
|
17
|
+
abort('ERROR: Wrong number of arguments, '\
|
|
18
|
+
"#{argv.length} for 1 or more.")
|
|
19
|
+
end
|
|
20
|
+
|
|
22
21
|
# Convenience
|
|
23
|
-
db
|
|
22
|
+
db = Moose::Inventory::DB
|
|
24
23
|
fmt = Moose::Inventory::Cli::Formatter
|
|
25
24
|
|
|
26
25
|
# Arguments
|
|
@@ -30,7 +29,7 @@ module Moose
|
|
|
30
29
|
if names.include?('ungrouped')
|
|
31
30
|
abort("Cannot manually manipulate the automatic group 'ungrouped'\n")
|
|
32
31
|
end
|
|
33
|
-
|
|
32
|
+
|
|
34
33
|
# Transaction
|
|
35
34
|
warn_count = 0
|
|
36
35
|
db.transaction do # Transaction start
|
|
@@ -40,10 +39,10 @@ module Moose
|
|
|
40
39
|
group = db.models[:group].find(name: name)
|
|
41
40
|
if group.nil?
|
|
42
41
|
warn_count += 1
|
|
43
|
-
fmt.warn "Group '#{
|
|
44
|
-
fmt.puts 4,
|
|
42
|
+
fmt.warn "Group '#{name}' does not exist, skipping.\n"
|
|
43
|
+
fmt.puts 4, '- No such group, skipping.'
|
|
45
44
|
end
|
|
46
|
-
fmt.puts 4,
|
|
45
|
+
fmt.puts 4, '- OK'
|
|
47
46
|
unless group.nil?
|
|
48
47
|
# Dissociate from any parent groups
|
|
49
48
|
pgroups_ds = group.parents_dataset
|
|
@@ -58,34 +57,33 @@ module Moose
|
|
|
58
57
|
groups_ds.each do |child|
|
|
59
58
|
fmt.puts 2, "- Remove association {group:#{name} <-> group:#{child.name}}..."
|
|
60
59
|
group.remove_child(child)
|
|
61
|
-
# TODO: Should we propagate the delete to orphaned children?
|
|
60
|
+
# TODO: Should we propagate the delete to orphaned children?
|
|
62
61
|
fmt.puts 4, '- OK'
|
|
63
62
|
end
|
|
64
|
-
|
|
63
|
+
|
|
65
64
|
# Handle automatic group for any associated hosts
|
|
66
65
|
hosts_ds = group.hosts_dataset
|
|
67
66
|
hosts_ds.each do |host|
|
|
68
67
|
host_groups_ds = host.groups_dataset
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
end
|
|
68
|
+
next unless host_groups_ds.count == 1 # We're the only group
|
|
69
|
+
fmt.puts 2, "- Adding automatic association {group:ungrouped <-> host:#{host[:name]}}..."
|
|
70
|
+
ungrouped = db.models[:group].find_or_create(name: 'ungrouped')
|
|
71
|
+
host.add_group(ungrouped)
|
|
72
|
+
fmt.puts 4, '- OK'
|
|
75
73
|
end
|
|
76
74
|
# Remove the group
|
|
77
75
|
fmt.puts 2, "- Destroy group '#{name}'..."
|
|
78
76
|
group.remove_all_hosts
|
|
79
77
|
group.destroy
|
|
80
|
-
fmt.puts 4,
|
|
78
|
+
fmt.puts 4, '- OK'
|
|
81
79
|
end
|
|
82
|
-
fmt.puts 2,
|
|
80
|
+
fmt.puts 2, '- All OK'
|
|
83
81
|
end
|
|
84
82
|
end # Transaction end
|
|
85
83
|
if warn_count == 0
|
|
86
|
-
puts
|
|
84
|
+
puts 'Succeeded.'
|
|
87
85
|
else
|
|
88
|
-
puts
|
|
86
|
+
puts 'Succeeded, with warnings.'
|
|
89
87
|
end
|
|
90
88
|
end
|
|
91
89
|
end
|
|
@@ -7,16 +7,15 @@ module Moose
|
|
|
7
7
|
module Cli
|
|
8
8
|
##
|
|
9
9
|
# Implemention of the "group rmchild" methods of the CLI
|
|
10
|
-
class Group
|
|
10
|
+
class Group
|
|
11
11
|
#==========================
|
|
12
12
|
desc 'rmchild PARENTGROUP CHILDGROUP_1 [CHILDGROUP_2 ... ]',
|
|
13
|
-
|
|
14
|
-
def rmchild(*
|
|
15
|
-
|
|
13
|
+
'Dissociate one or more child-groups CHILDGROUP_n from PARENTGROUP'
|
|
14
|
+
def rmchild(*_argv)
|
|
16
15
|
# Sanity check
|
|
17
16
|
if args.length < 2
|
|
18
17
|
abort("ERROR: Wrong number of arguments, #{args.length} "\
|
|
19
|
-
|
|
18
|
+
'for 2 or more.')
|
|
20
19
|
end
|
|
21
20
|
|
|
22
21
|
# Arguments
|
|
@@ -43,7 +42,7 @@ module Moose
|
|
|
43
42
|
if pgroup.nil?
|
|
44
43
|
abort("ERROR: The group '#{pname}' does not exist.")
|
|
45
44
|
end
|
|
46
|
-
fmt.puts 4,
|
|
45
|
+
fmt.puts 4, '- OK'
|
|
47
46
|
|
|
48
47
|
# Dissociate parent group from the child groups
|
|
49
48
|
groups_ds = pgroup.children_dataset
|
|
@@ -7,7 +7,6 @@ module Moose
|
|
|
7
7
|
##
|
|
8
8
|
# Implementation of the "group rmhost" method of the CLI
|
|
9
9
|
class Group
|
|
10
|
-
|
|
11
10
|
#==========================
|
|
12
11
|
desc 'rmhost GROUPNAME HOSTNAME_1 [HOSTNAME_2 ...]',
|
|
13
12
|
'Dissociate the hosts HOSTNAME_n from the group NAME'
|
|
@@ -19,7 +18,7 @@ module Moose
|
|
|
19
18
|
abort("ERROR: Wrong number of arguments, #{args.length} for 2 or more.")
|
|
20
19
|
end
|
|
21
20
|
|
|
22
|
-
# Arguments
|
|
21
|
+
# Arguments
|
|
23
22
|
name = args[0].downcase
|
|
24
23
|
hosts = args.slice(1, args.length - 1).uniq.map(&:downcase)
|
|
25
24
|
|
|
@@ -29,7 +28,7 @@ module Moose
|
|
|
29
28
|
end
|
|
30
29
|
|
|
31
30
|
# Convenience
|
|
32
|
-
db
|
|
31
|
+
db = Moose::Inventory::DB
|
|
33
32
|
fmt = Moose::Inventory::Cli::Formatter
|
|
34
33
|
|
|
35
34
|
# Transaction
|
|
@@ -40,21 +39,19 @@ module Moose
|
|
|
40
39
|
puts "Dissociate group '#{name}' from host(s) '#{hosts.join(',')}':"
|
|
41
40
|
fmt.puts 2, "- retrieve group '#{name}'..."
|
|
42
41
|
group = db.models[:group].find(name: name)
|
|
43
|
-
if group.nil?
|
|
44
|
-
abort("ERROR: The group '#{name}' does not exist.")
|
|
45
|
-
end
|
|
42
|
+
abort("ERROR: The group '#{name}' does not exist.") if group.nil?
|
|
46
43
|
fmt.puts 4, '- OK'
|
|
47
44
|
|
|
48
45
|
# dissociate group from the hosts
|
|
49
|
-
ungrouped
|
|
46
|
+
ungrouped = db.models[:group].find_or_create(name: 'ungrouped')
|
|
50
47
|
hosts_ds = group.hosts_dataset
|
|
51
|
-
hosts.each do |h|
|
|
52
|
-
fmt.puts 2, "- remove association {group:#{name} <-> host:#{
|
|
48
|
+
hosts.each do |h|
|
|
49
|
+
fmt.puts 2, "- remove association {group:#{name} <-> host:#{h}}..."
|
|
53
50
|
|
|
54
51
|
# Check against existing associations
|
|
55
52
|
if hosts_ds[name: h].nil?
|
|
56
53
|
warn_count += 1
|
|
57
|
-
fmt.warn "Association {group:#{name} <-> host:#{
|
|
54
|
+
fmt.warn "Association {group:#{name} <-> host:#{h}} doesn't"\
|
|
58
55
|
" exist, skipping.\n"
|
|
59
56
|
fmt.puts 4, '- doesn\'t exist, skipping.'
|
|
60
57
|
fmt.puts 4, '- OK'
|
|
@@ -63,14 +60,13 @@ module Moose
|
|
|
63
60
|
|
|
64
61
|
host = db.models[:host].find(name: h)
|
|
65
62
|
group.remove_host(host) unless host.nil?
|
|
66
|
-
fmt.puts 4,'- OK'
|
|
63
|
+
fmt.puts 4, '- OK'
|
|
67
64
|
|
|
68
65
|
# Add the host to the ungrouped group if not in any other group
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
end
|
|
66
|
+
next unless host.groups_dataset.count == 0
|
|
67
|
+
fmt.puts 2, "- add automatic association {group:ungrouped <-> host:#{h}}..."
|
|
68
|
+
host.add_group(ungrouped)
|
|
69
|
+
fmt.puts 4, '- OK'
|
|
74
70
|
end
|
|
75
71
|
fmt.puts 2, '- all OK'
|
|
76
72
|
end # Transaction end
|
|
@@ -15,7 +15,7 @@ module Moose
|
|
|
15
15
|
abort('ERROR: Wrong number of arguments, ' \
|
|
16
16
|
"#{args.length} for 2 or more.")
|
|
17
17
|
end
|
|
18
|
-
|
|
18
|
+
|
|
19
19
|
# Convenience
|
|
20
20
|
db = Moose::Inventory::DB
|
|
21
21
|
fmt = Moose::Inventory::Cli::Formatter
|
|
@@ -26,8 +26,8 @@ module Moose
|
|
|
26
26
|
|
|
27
27
|
# Transaction
|
|
28
28
|
db.transaction do # Transaction start
|
|
29
|
-
puts "Remove variable(s) '#{vars.join(
|
|
30
|
-
|
|
29
|
+
puts "Remove variable(s) '#{vars.join(',')}' from group '#{name}':"
|
|
30
|
+
|
|
31
31
|
fmt.puts 2, "- retrieve group '#{name}'..."
|
|
32
32
|
group = db.models[:group].find(name: name)
|
|
33
33
|
if group.nil?
|
|
@@ -35,7 +35,7 @@ module Moose
|
|
|
35
35
|
"The group '#{name}' does not exist."
|
|
36
36
|
end
|
|
37
37
|
fmt.puts 4, '- OK'
|
|
38
|
-
|
|
38
|
+
|
|
39
39
|
groupvars_ds = group.groupvars_dataset
|
|
40
40
|
vars.each do |v|
|
|
41
41
|
fmt.puts 2, "- remove variable '#{v}'..."
|
|
@@ -45,7 +45,7 @@ module Moose
|
|
|
45
45
|
"Incorrect format in {#{v}}. " \
|
|
46
46
|
'Expected \'key\' or \'key=value\'.'
|
|
47
47
|
end
|
|
48
|
-
|
|
48
|
+
|
|
49
49
|
# Check against existing associations
|
|
50
50
|
groupvar = groupvars_ds[name: vararray[0]]
|
|
51
51
|
unless groupvar.nil?
|
|
@@ -4,7 +4,7 @@ require 'indentation'
|
|
|
4
4
|
|
|
5
5
|
require_relative './formatter.rb'
|
|
6
6
|
require_relative '../db/exceptions.rb'
|
|
7
|
-
|
|
7
|
+
|
|
8
8
|
module Moose
|
|
9
9
|
module Inventory
|
|
10
10
|
module Cli
|
|
@@ -18,7 +18,7 @@ module Moose
|
|
|
18
18
|
# rubocop:disable Metrics/LineLength
|
|
19
19
|
def add(*argv) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
20
20
|
# rubocop:enable Metrics/LineLength
|
|
21
|
-
if argv.
|
|
21
|
+
if argv.empty?
|
|
22
22
|
abort('ERROR: Wrong number of arguments, '\
|
|
23
23
|
"#{argv.length} for 1 or more.")
|
|
24
24
|
end
|
|
@@ -26,25 +26,25 @@ module Moose
|
|
|
26
26
|
# Convenience
|
|
27
27
|
db = Moose::Inventory::DB
|
|
28
28
|
fmt = Moose::Inventory::Cli::Formatter
|
|
29
|
-
|
|
29
|
+
|
|
30
30
|
# Arguments
|
|
31
31
|
names = argv.uniq.map(&:downcase)
|
|
32
32
|
|
|
33
33
|
# 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] = ''
|
|
34
|
+
# groups = options[:groups].downcase.split(/\W+/).uniq
|
|
35
|
+
options[:groups].nil? && options[:groups] = ''
|
|
36
36
|
groups = options[:groups].downcase.split(',').uniq
|
|
37
37
|
|
|
38
38
|
# Sanity
|
|
39
39
|
if groups.include?('ungrouped')
|
|
40
|
-
abort(
|
|
40
|
+
abort('ERROR: Cannot manually manipulate '\
|
|
41
41
|
"the automatic group 'ungrouped'.")
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
# Process
|
|
45
45
|
db.transaction do # Transaction start
|
|
46
46
|
fmt.reset_indent
|
|
47
|
-
|
|
47
|
+
|
|
48
48
|
names.each do |name|
|
|
49
49
|
puts "Add host '#{name}':"
|
|
50
50
|
fmt.puts 2, "- Creating host '#{name}'..."
|
|
@@ -56,8 +56,8 @@ module Moose
|
|
|
56
56
|
fmt.warn "The host '#{name}' already exists, skipping creation.\n"
|
|
57
57
|
groups_ds = host.groups_dataset
|
|
58
58
|
end
|
|
59
|
-
fmt.puts 4,
|
|
60
|
-
|
|
59
|
+
fmt.puts 4, '- OK'
|
|
60
|
+
|
|
61
61
|
groups.each do |g|
|
|
62
62
|
next if g.nil? || g.empty?
|
|
63
63
|
fmt.puts 2, "- Adding association {host:#{name} <-> group:#{g}}..."
|
|
@@ -66,23 +66,23 @@ module Moose
|
|
|
66
66
|
fmt.warn "The group '#{g}' doesn't exist, but will be created.\n"
|
|
67
67
|
group = db.models[:group].create(name: g)
|
|
68
68
|
end
|
|
69
|
-
if !groups_ds.nil? && groups_ds[name: g].nil?
|
|
70
|
-
fmt.warn "Association {host:#{name} <-> group:#{
|
|
69
|
+
if !groups_ds.nil? && !groups_ds[name: g].nil?
|
|
70
|
+
fmt.warn "Association {host:#{name} <-> group:#{g}} already exists, skipping creation.\n"
|
|
71
71
|
else
|
|
72
72
|
host.add_group(group)
|
|
73
73
|
end
|
|
74
74
|
fmt.puts 4, '- OK'
|
|
75
75
|
end
|
|
76
|
-
|
|
76
|
+
|
|
77
77
|
# Handle the automatic 'ungrouped' group
|
|
78
78
|
groups_ds = host.groups_dataset
|
|
79
|
-
if !groups_ds.nil?
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
84
|
end
|
|
85
|
-
fmt.puts 2,
|
|
85
|
+
fmt.puts 2, '- All OK'
|
|
86
86
|
end
|
|
87
87
|
end # Transaction end
|
|
88
88
|
puts 'Succeeded'
|
|
@@ -33,11 +33,11 @@ module Moose
|
|
|
33
33
|
if groups.include?('ungrouped')
|
|
34
34
|
abort 'ERROR: Cannot manually manipulate the automatic '\
|
|
35
35
|
'group \'ungrouped\'.'
|
|
36
|
-
end
|
|
37
|
-
|
|
36
|
+
end
|
|
37
|
+
|
|
38
38
|
# Transaction
|
|
39
39
|
db.transaction do # Transaction start
|
|
40
|
-
puts "Associate host '#{name}' with groups '#{groups.join(',')}':"
|
|
40
|
+
puts "Associate host '#{name}' with groups '#{groups.join(',')}':"
|
|
41
41
|
# Get the target host
|
|
42
42
|
fmt.puts 2, "- Retrieve host '#{name}'..."
|
|
43
43
|
host = db.models[:host].find(name: name)
|
|
@@ -50,20 +50,20 @@ module Moose
|
|
|
50
50
|
# Associate host with the groups
|
|
51
51
|
groups_ds = host.groups_dataset
|
|
52
52
|
groups.each do |g|
|
|
53
|
-
fmt.puts 2, "- Add association {host:#{name} <-> group:#{
|
|
53
|
+
fmt.puts 2, "- Add association {host:#{name} <-> group:#{g}}..."
|
|
54
54
|
|
|
55
55
|
# Check against existing associations
|
|
56
56
|
if !groups_ds[name: g].nil?
|
|
57
57
|
fmt.warn "Association {host:#{name} <-> group:#{g}} already exists, skipping."
|
|
58
|
-
fmt.puts 4,
|
|
58
|
+
fmt.puts 4, '- Already exists, skipping.'
|
|
59
59
|
else
|
|
60
60
|
# Add new association
|
|
61
61
|
group = db.models[:group].find(name: g)
|
|
62
62
|
if group.nil?
|
|
63
63
|
fmt.warn "Group '#{g}' does not exist and will be created."
|
|
64
|
-
fmt.puts 4,
|
|
64
|
+
fmt.puts 4, '- Group does not exist, creating now...'
|
|
65
65
|
group = db.models[:group].create(name: g)
|
|
66
|
-
fmt.puts 6,
|
|
66
|
+
fmt.puts 6, '- OK'
|
|
67
67
|
end
|
|
68
68
|
host.add_group(group)
|
|
69
69
|
end
|
|
@@ -72,9 +72,9 @@ module Moose
|
|
|
72
72
|
|
|
73
73
|
# Handle 'ungrouped' group automation
|
|
74
74
|
unless groups_ds[name: 'ungrouped'].nil?
|
|
75
|
-
fmt.puts 2,
|
|
75
|
+
fmt.puts 2, '- Remove automatic association '\
|
|
76
76
|
"{host:#{name} <-> group:ungrouped}..."
|
|
77
|
-
ungrouped
|
|
77
|
+
ungrouped = db.models[:group].find(name: 'ungrouped')
|
|
78
78
|
host.remove_group(ungrouped) unless ungrouped.nil?
|
|
79
79
|
fmt.puts 4, '- OK'
|
|
80
80
|
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(
|
|
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?('=') ||
|
|
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
|
-
|
|
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.
|
|
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|
|
|
33
|
+
names.each do |name|
|
|
34
34
|
host = db.models[:host].find(name: name)
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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.
|
|
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
|
-
|
|
23
|
-
|
|
22
|
+
abort('ERROR: Wrong number of arguments for Ansible mode, '\
|
|
23
|
+
"#{args.length} for 1.")
|
|
24
24
|
end
|
|
25
25
|
else
|
|
26
|
-
if argv.
|
|
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
|
|
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
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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.
|
|
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,
|
|
41
|
+
fmt.puts 4, '- No such host, skipping.'
|
|
42
42
|
end
|
|
43
|
-
fmt.puts 4,
|
|
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,
|
|
48
|
+
fmt.puts 4, '- OK'
|
|
49
49
|
end
|
|
50
|
-
fmt.puts 2,
|
|
50
|
+
fmt.puts 2, '- All OK'
|
|
51
51
|
end
|
|
52
52
|
end # Transaction end
|
|
53
|
-
if warn_count == 0
|
|
54
|
-
puts
|
|
53
|
+
if warn_count == 0
|
|
54
|
+
puts 'Succeeded.'
|
|
55
55
|
else
|
|
56
|
-
puts
|
|
56
|
+
puts 'Succeeded, with warnings.'
|
|
57
57
|
end
|
|
58
58
|
end
|
|
59
59
|
end
|