moose-inventory 1.0.7 → 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 +23 -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 +36 -21
- 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 +103 -49
- 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
|
@@ -15,37 +15,37 @@ module Moose
|
|
|
15
15
|
def self.dump(arg, format = nil)
|
|
16
16
|
out(arg, format)
|
|
17
17
|
end
|
|
18
|
-
|
|
18
|
+
|
|
19
19
|
def self.out(arg, format = nil)
|
|
20
20
|
return if arg.nil?
|
|
21
21
|
|
|
22
22
|
if format.nil?
|
|
23
23
|
format = Moose::Inventory::Config._confopts[:format].downcase
|
|
24
24
|
end
|
|
25
|
-
|
|
25
|
+
|
|
26
26
|
case format
|
|
27
|
-
when 'yaml','y'
|
|
27
|
+
when 'yaml', 'y'
|
|
28
28
|
$stdout.puts arg.to_yaml
|
|
29
29
|
|
|
30
|
-
when 'prettyjson','pjson','p'
|
|
30
|
+
when 'prettyjson', 'pjson', 'p'
|
|
31
31
|
$stdout.puts JSON.pretty_generate(arg)
|
|
32
32
|
|
|
33
|
-
when 'json','j'
|
|
33
|
+
when 'json', 'j'
|
|
34
34
|
$stdout.puts arg.to_json
|
|
35
35
|
|
|
36
36
|
else
|
|
37
37
|
abort("Output format '#{format}' is not yet supported.")
|
|
38
38
|
end
|
|
39
39
|
end
|
|
40
|
-
|
|
40
|
+
|
|
41
41
|
#---------------
|
|
42
42
|
attr_accessor :indent
|
|
43
43
|
|
|
44
44
|
def reset_indent
|
|
45
45
|
@indent = 2
|
|
46
46
|
end
|
|
47
|
-
|
|
48
|
-
def puts(indent, msg, stream='STDOUT')
|
|
47
|
+
|
|
48
|
+
def puts(indent, msg, stream = 'STDOUT')
|
|
49
49
|
case stream
|
|
50
50
|
when 'STDOUT'
|
|
51
51
|
$stdout.puts msg.indent(indent)
|
|
@@ -55,8 +55,8 @@ module Moose
|
|
|
55
55
|
abort("Output stream '#{stream}' is not known.")
|
|
56
56
|
end
|
|
57
57
|
end
|
|
58
|
-
|
|
59
|
-
def print(indent, msg, stream='STDOUT')
|
|
58
|
+
|
|
59
|
+
def print(indent, msg, stream = 'STDOUT')
|
|
60
60
|
case stream
|
|
61
61
|
when 'STDOUT'
|
|
62
62
|
$stdout.print msg.indent(indent)
|
|
@@ -67,25 +67,24 @@ module Moose
|
|
|
67
67
|
end
|
|
68
68
|
end
|
|
69
69
|
|
|
70
|
-
def info(
|
|
70
|
+
def info(_indent, _msg, stream = 'STDOUT')
|
|
71
71
|
case stream
|
|
72
72
|
when 'STDOUT'
|
|
73
|
-
$stdout.print
|
|
73
|
+
$stdout.print 'INFO: {msg}'
|
|
74
74
|
when 'STDERR'
|
|
75
|
-
$stderr.print
|
|
75
|
+
$stderr.print 'INFO: {msg}'
|
|
76
76
|
else
|
|
77
77
|
abort("Output stream '#{stream}' is not known.")
|
|
78
78
|
end
|
|
79
79
|
end
|
|
80
|
-
|
|
80
|
+
|
|
81
81
|
def warn(msg)
|
|
82
|
-
|
|
82
|
+
$stderr.print "WARNING: #{msg}"
|
|
83
83
|
end
|
|
84
84
|
|
|
85
85
|
def error(msg)
|
|
86
|
-
|
|
86
|
+
$stderr.print "ERROR: #{msg}"
|
|
87
87
|
end
|
|
88
|
-
|
|
89
88
|
end
|
|
90
89
|
end
|
|
91
90
|
end
|
|
@@ -13,7 +13,7 @@ module Moose
|
|
|
13
13
|
# rubocop:disable Metrics/LineLength
|
|
14
14
|
def add(*argv) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
|
15
15
|
# rubocop:enable Metrics/LineLength
|
|
16
|
-
if argv.
|
|
16
|
+
if argv.empty?
|
|
17
17
|
abort("ERROR: Wrong number of arguments, #{argv.length} for 1 or more.")
|
|
18
18
|
end
|
|
19
19
|
|
|
@@ -27,10 +27,10 @@ module Moose
|
|
|
27
27
|
abort("ERROR: Cannot manually manipulate the automatic group 'ungrouped'\n")
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
# Convenience
|
|
31
|
+
db = Moose::Inventory::DB
|
|
32
|
+
fmt = Moose::Inventory::Cli::Formatter
|
|
33
|
+
|
|
34
34
|
# Transaction
|
|
35
35
|
warn_count = 0
|
|
36
36
|
db.transaction do # Transaction start
|
|
@@ -39,48 +39,47 @@ module Moose
|
|
|
39
39
|
puts "Add group '#{name}':"
|
|
40
40
|
group = db.models[:group].find(name: name)
|
|
41
41
|
hosts_ds = nil
|
|
42
|
-
fmt.puts 2,
|
|
42
|
+
fmt.puts 2, '- create group...'
|
|
43
43
|
if group.nil?
|
|
44
44
|
group = db.models[:group].create(name: name)
|
|
45
45
|
fmt.puts 4, '- OK'
|
|
46
46
|
else
|
|
47
47
|
warn_count += 1
|
|
48
48
|
fmt.warn "Group '#{name}' already exists, skipping creation.\n"
|
|
49
|
-
fmt.puts 4,
|
|
49
|
+
fmt.puts 4, '- already exists, skipping.'
|
|
50
50
|
hosts_ds = group.hosts_dataset
|
|
51
51
|
fmt.puts 4, '- OK'
|
|
52
52
|
end
|
|
53
|
-
|
|
53
|
+
|
|
54
54
|
# Associate with hosts
|
|
55
55
|
hosts.each do |h|
|
|
56
56
|
next if h.nil? || h.empty?
|
|
57
|
-
fmt.puts 2, "- add association {group:#{name} <-> host:#{
|
|
57
|
+
fmt.puts 2, "- add association {group:#{name} <-> host:#{h}}..."
|
|
58
58
|
host = db.models[:host].find(name: h)
|
|
59
59
|
if host.nil?
|
|
60
60
|
warn_count += 1
|
|
61
61
|
fmt.warn "Host '#{h}' doesn't exist, but will be created.\n"
|
|
62
62
|
fmt.puts 4, "- host doesn't exist, creating now..."
|
|
63
63
|
host = db.models[:host].create(name: h)
|
|
64
|
-
fmt.puts 6,
|
|
64
|
+
fmt.puts 6, '- OK'
|
|
65
65
|
end
|
|
66
66
|
if !hosts_ds.nil? && !hosts_ds[name: h].nil?
|
|
67
67
|
warn_count += 1
|
|
68
|
-
fmt.warn "Association {group:#{name} <-> host:#{
|
|
68
|
+
fmt.warn "Association {group:#{name} <-> host:#{h}}"\
|
|
69
69
|
" already exists, skipping creation.\n"
|
|
70
|
-
|
|
71
|
-
else
|
|
70
|
+
fmt.puts 4, '- already exists, skipping.'
|
|
71
|
+
else
|
|
72
72
|
group.add_host(host)
|
|
73
73
|
end
|
|
74
74
|
fmt.puts 4, '- OK'
|
|
75
|
-
|
|
75
|
+
|
|
76
76
|
# Handle the host's automatic 'ungrouped' group
|
|
77
77
|
ungrouped = host.groups_dataset[name: 'ungrouped']
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
end
|
|
78
|
+
next if ungrouped.nil?
|
|
79
|
+
fmt.puts 2, '- remove automatic association {group:ungrouped'\
|
|
80
|
+
" <-> host:#{h}}..."
|
|
81
|
+
host.remove_group(ungrouped) unless ungrouped.nil?
|
|
82
|
+
fmt.puts 4, '- OK'
|
|
84
83
|
end
|
|
85
84
|
fmt.puts 2, '- all OK'
|
|
86
85
|
end
|
|
@@ -95,4 +94,3 @@ module Moose
|
|
|
95
94
|
end
|
|
96
95
|
end
|
|
97
96
|
end
|
|
98
|
-
|
|
@@ -6,34 +6,33 @@ module Moose
|
|
|
6
6
|
module Cli
|
|
7
7
|
##
|
|
8
8
|
# Implemention of the "group addchild" methods of the CLI
|
|
9
|
-
class Group < Thor
|
|
9
|
+
class Group < Thor
|
|
10
10
|
#==========================
|
|
11
11
|
desc 'addchild PARENTGROUP CHILDGROUP_1 [CHILDGROUP_2 ... ]',
|
|
12
12
|
'Associate one or more child-groups CHILDGROUP_n with PARENTGROUP'
|
|
13
|
-
def addchild(*
|
|
14
|
-
|
|
13
|
+
def addchild(*_argv)
|
|
15
14
|
# Sanity check
|
|
16
15
|
if args.length < 2
|
|
17
16
|
abort("ERROR: Wrong number of arguments, #{args.length} "\
|
|
18
|
-
|
|
17
|
+
'for 2 or more.')
|
|
19
18
|
end
|
|
20
|
-
|
|
19
|
+
|
|
21
20
|
# Arguments
|
|
22
21
|
pname = args[0].downcase
|
|
23
22
|
cnames = args.slice(1, args.length - 1).uniq.map(&:downcase)
|
|
24
|
-
|
|
23
|
+
|
|
25
24
|
# Sanity
|
|
26
25
|
if pname == 'ungrouped' || cnames.include?('ungrouped')
|
|
27
26
|
abort("ERROR: Cannot manually manipulate the automatic group 'ungrouped'.")
|
|
28
27
|
end
|
|
29
|
-
|
|
28
|
+
|
|
30
29
|
# Convenience
|
|
31
30
|
db = Moose::Inventory::DB
|
|
32
31
|
fmt = Moose::Inventory::Cli::Formatter
|
|
33
32
|
|
|
34
33
|
# Transaction
|
|
35
34
|
warn_count = 0
|
|
36
|
-
begin
|
|
35
|
+
begin
|
|
37
36
|
db.transaction do # Transaction start
|
|
38
37
|
puts "Associate parent group '#{pname}' with child group(s) '#{cnames.join(',')}':"
|
|
39
38
|
# Get the target group
|
|
@@ -42,51 +41,48 @@ module Moose
|
|
|
42
41
|
if pgroup.nil?
|
|
43
42
|
abort("ERROR: The group '#{pname}' does not exist.")
|
|
44
43
|
end
|
|
45
|
-
fmt.puts 4,
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
fmt.puts 4, '- OK'
|
|
45
|
+
|
|
46
|
+
# Associate parent group with the child groups
|
|
47
|
+
|
|
48
|
+
groups_ds = pgroup.children_dataset
|
|
49
|
+
cnames.each do |cname|
|
|
50
|
+
fmt.puts 2, "- add association {group:#{pname} <-> group:#{cname}}..."
|
|
48
51
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
warn_count += 1
|
|
56
|
-
fmt.warn "Association {group:#{pname} <-> group:#{cname}}}"\
|
|
57
|
-
" already exists, skipping.\n"
|
|
58
|
-
fmt.puts 4, '- already exists, skipping.'
|
|
59
|
-
fmt.puts 4, '- OK'
|
|
60
|
-
next
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
# Add new association
|
|
64
|
-
cgroup = db.models[:group].find(name: cname)
|
|
65
|
-
if cgroup.nil?
|
|
66
|
-
warn_count += 1
|
|
67
|
-
fmt.warn "Group '#{cname}' does not exist and will be created.\n"
|
|
68
|
-
fmt.puts 4, '- child group does not exist, creating now...'
|
|
69
|
-
cgroup = db.models[:group].create(name: cname)
|
|
70
|
-
fmt.puts 6, '- OK'
|
|
71
|
-
end
|
|
72
|
-
pgroup.add_child(cgroup)
|
|
52
|
+
# Check against existing associations
|
|
53
|
+
unless groups_ds[name: cname].nil?
|
|
54
|
+
warn_count += 1
|
|
55
|
+
fmt.warn "Association {group:#{pname} <-> group:#{cname}}}"\
|
|
56
|
+
" already exists, skipping.\n"
|
|
57
|
+
fmt.puts 4, '- already exists, skipping.'
|
|
73
58
|
fmt.puts 4, '- OK'
|
|
59
|
+
next
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Add new association
|
|
63
|
+
cgroup = db.models[:group].find(name: cname)
|
|
64
|
+
if cgroup.nil?
|
|
65
|
+
warn_count += 1
|
|
66
|
+
fmt.warn "Group '#{cname}' does not exist and will be created.\n"
|
|
67
|
+
fmt.puts 4, '- child group does not exist, creating now...'
|
|
68
|
+
cgroup = db.models[:group].create(name: cname)
|
|
69
|
+
fmt.puts 6, '- OK'
|
|
74
70
|
end
|
|
75
|
-
|
|
71
|
+
pgroup.add_child(cgroup)
|
|
72
|
+
fmt.puts 4, '- OK'
|
|
73
|
+
end
|
|
74
|
+
fmt.puts 2, '- all OK'
|
|
76
75
|
end # Transaction end
|
|
77
76
|
rescue db.exceptions[:moose] => e
|
|
78
|
-
|
|
77
|
+
abort("ERROR: #{e}")
|
|
79
78
|
end
|
|
80
79
|
if warn_count == 0
|
|
81
80
|
puts 'Succeeded.'
|
|
82
81
|
else
|
|
83
82
|
puts 'Succeeded, with warnings.'
|
|
84
83
|
end
|
|
85
|
-
|
|
86
84
|
end
|
|
87
85
|
end
|
|
88
86
|
end
|
|
89
87
|
end
|
|
90
88
|
end
|
|
91
|
-
|
|
92
|
-
|
|
@@ -14,7 +14,7 @@ module Moose
|
|
|
14
14
|
# Sanity
|
|
15
15
|
if args.length < 2
|
|
16
16
|
abort("ERROR: Wrong number of arguments, #{args.length} "\
|
|
17
|
-
|
|
17
|
+
'for 2 or more.')
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
# Arguments
|
|
@@ -32,27 +32,25 @@ module Moose
|
|
|
32
32
|
|
|
33
33
|
# Transaction
|
|
34
34
|
warn_count = 0
|
|
35
|
-
begin
|
|
35
|
+
begin
|
|
36
36
|
db.transaction do # Transaction start
|
|
37
37
|
puts "Associate group '#{name}' with host(s) '#{hosts.join(',')}':"
|
|
38
38
|
# Get the target group
|
|
39
39
|
fmt.puts 2, "- retrieve group '#{name}'..."
|
|
40
40
|
group = db.models[:group].find(name: name)
|
|
41
|
-
if group.nil?
|
|
42
|
-
|
|
43
|
-
end
|
|
44
|
-
fmt.puts 4, '- OK'
|
|
41
|
+
abort("ERROR: The group '#{name}' does not exist.") if group.nil?
|
|
42
|
+
fmt.puts 4, '- OK'
|
|
45
43
|
|
|
46
44
|
# Associate group with the hosts
|
|
47
|
-
ungrouped
|
|
45
|
+
ungrouped = db.models[:group].find_or_create(name: 'ungrouped')
|
|
48
46
|
hosts_ds = group.hosts_dataset
|
|
49
|
-
hosts.each do |h|
|
|
50
|
-
fmt.puts 2, "- add association {group:#{name} <-> host:#{
|
|
47
|
+
hosts.each do |h|
|
|
48
|
+
fmt.puts 2, "- add association {group:#{name} <-> host:#{h}}..."
|
|
51
49
|
|
|
52
50
|
# Check against existing associations
|
|
53
51
|
unless hosts_ds[name: h].nil?
|
|
54
52
|
warn_count += 1
|
|
55
|
-
fmt.warn "Association {group:#{name} <-> host:#{
|
|
53
|
+
fmt.warn "Association {group:#{name} <-> host:#{h}} already"\
|
|
56
54
|
" exists, skipping.\n"
|
|
57
55
|
fmt.puts 4, '- already exists, skipping.'
|
|
58
56
|
fmt.puts 4, '- OK'
|
|
@@ -68,17 +66,16 @@ module Moose
|
|
|
68
66
|
host = db.models[:host].create(name: h)
|
|
69
67
|
fmt.puts 6, '- OK'
|
|
70
68
|
end
|
|
71
|
-
|
|
69
|
+
|
|
72
70
|
group.add_host(host)
|
|
73
71
|
fmt.puts 4, '- OK'
|
|
74
72
|
|
|
75
73
|
# Remove the host from the ungrouped group, if necessary
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
end
|
|
74
|
+
next if host.groups_dataset[name: 'ungrouped'].nil?
|
|
75
|
+
fmt.puts 2, '- remove automatic association '\
|
|
76
|
+
"{group:ungrouped <-> host:#{h}}..."
|
|
77
|
+
host.remove_group(ungrouped)
|
|
78
|
+
fmt.puts 4, '- OK'
|
|
82
79
|
end
|
|
83
80
|
fmt.puts 2, '- all OK'
|
|
84
81
|
end # Transaction end
|
|
@@ -91,7 +88,6 @@ module Moose
|
|
|
91
88
|
puts 'Succeeded, with warnings.'
|
|
92
89
|
end
|
|
93
90
|
end
|
|
94
|
-
|
|
95
91
|
end
|
|
96
92
|
end
|
|
97
93
|
end
|
|
@@ -22,49 +22,49 @@ module Moose
|
|
|
22
22
|
# Arguments
|
|
23
23
|
name = args[0].downcase
|
|
24
24
|
vars = args.slice(1, args.length - 1).uniq
|
|
25
|
-
|
|
26
|
-
# Transaction
|
|
27
|
-
db.transaction do # Transaction start
|
|
28
|
-
puts "Add variables '#{vars.join(",")}' to group '#{name}':"
|
|
29
|
-
fmt.puts 2,"- retrieve group '#{name}'..."
|
|
30
|
-
group = db.models[:group].find(name: name)
|
|
31
|
-
if group.nil?
|
|
32
|
-
fail db.exceptions[:moose],
|
|
33
|
-
"The group '#{name}' does not exist."
|
|
34
|
-
end
|
|
35
|
-
fmt.puts 4, '- OK'
|
|
36
|
-
|
|
37
|
-
groupvars_ds = group.groupvars_dataset
|
|
38
|
-
vars.each do |v|
|
|
39
|
-
fmt.puts 2, "- add variable '#{v}'..."
|
|
40
|
-
vararray = v.split('=')
|
|
41
25
|
|
|
42
|
-
|
|
26
|
+
# Transaction
|
|
27
|
+
db.transaction do # Transaction start
|
|
28
|
+
puts "Add variables '#{vars.join(',')}' to group '#{name}':"
|
|
29
|
+
fmt.puts 2, "- retrieve group '#{name}'..."
|
|
30
|
+
group = db.models[:group].find(name: name)
|
|
31
|
+
if group.nil?
|
|
43
32
|
fail db.exceptions[:moose],
|
|
44
|
-
"
|
|
33
|
+
"The group '#{name}' does not exist."
|
|
45
34
|
end
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
35
|
+
fmt.puts 4, '- OK'
|
|
36
|
+
|
|
37
|
+
groupvars_ds = group.groupvars_dataset
|
|
38
|
+
vars.each do |v|
|
|
39
|
+
fmt.puts 2, "- add variable '#{v}'..."
|
|
40
|
+
vararray = v.split('=')
|
|
41
|
+
|
|
42
|
+
if v.start_with?('=') || v.end_with?('=') || vararray.length != 2
|
|
43
|
+
fail db.exceptions[:moose],
|
|
44
|
+
"Incorrect format in '{#{v}}'. Expected 'key=value'."
|
|
55
45
|
end
|
|
56
|
-
|
|
57
|
-
#
|
|
58
|
-
groupvar =
|
|
59
|
-
|
|
60
|
-
|
|
46
|
+
|
|
47
|
+
# Check against existing associations
|
|
48
|
+
groupvar = groupvars_ds[name: vararray[0]]
|
|
49
|
+
if !groupvar.nil?
|
|
50
|
+
unless groupvar[:value] == vararray[1]
|
|
51
|
+
fmt.puts 4, '- already exists, applying as an update...'
|
|
52
|
+
update = db.models[:groupvar].find(id: groupvar[:id])
|
|
53
|
+
update[:value] = vararray[1]
|
|
54
|
+
update.save
|
|
55
|
+
end
|
|
56
|
+
else
|
|
57
|
+
# groupvar doesn't exist, so create and associate
|
|
58
|
+
groupvar = db.models[:groupvar].create(name: vararray[0],
|
|
59
|
+
value: vararray[1])
|
|
60
|
+
group.add_groupvar(groupvar)
|
|
61
|
+
end
|
|
62
|
+
fmt.puts 4, '- OK'
|
|
61
63
|
end
|
|
62
|
-
fmt.puts
|
|
63
|
-
end
|
|
64
|
-
fmt.puts 2, '- all OK'
|
|
65
|
-
end # Transaction end
|
|
64
|
+
fmt.puts 2, '- all OK'
|
|
65
|
+
end # Transaction end
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
puts 'Succeeded.'
|
|
68
68
|
end
|
|
69
69
|
end
|
|
70
70
|
end
|
|
@@ -9,13 +9,13 @@ module Moose
|
|
|
9
9
|
class Group
|
|
10
10
|
desc 'get GROUP_1 [GROUP_2 ...]', 'Get groups GROUP_n from the inventory'
|
|
11
11
|
def get(*argv) # rubocop:disable Metrics/AbcSize
|
|
12
|
-
if argv.
|
|
12
|
+
if argv.empty?
|
|
13
13
|
abort('ERROR: Wrong number of arguments, '\
|
|
14
14
|
"#{argv.length} for 1 or more")
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
# Convenience
|
|
18
|
-
db
|
|
18
|
+
db = Moose::Inventory::DB
|
|
19
19
|
fmt = Moose::Inventory::Cli::Formatter
|
|
20
20
|
|
|
21
21
|
# Arguments
|
|
@@ -25,32 +25,29 @@ module Moose
|
|
|
25
25
|
results = {}
|
|
26
26
|
names.each do |name|
|
|
27
27
|
group = db.models[:group].find(name: name)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
unless groupvars.length == 0
|
|
49
|
-
results[group[:name].to_sym][:groupvars] = groupvars
|
|
50
|
-
end
|
|
28
|
+
|
|
29
|
+
next if group.nil?
|
|
30
|
+
hosts = group.hosts_dataset.map(:name)
|
|
31
|
+
|
|
32
|
+
children = group.children_dataset.map(:name)
|
|
33
|
+
|
|
34
|
+
groupvars = {}
|
|
35
|
+
group.groupvars_dataset.each do |gv|
|
|
36
|
+
groupvars[gv[:name].to_sym] = gv[:value]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
results[group[:name].to_sym] = {}
|
|
40
|
+
results[group[:name].to_sym][:hosts] = hosts unless hosts.empty?
|
|
41
|
+
|
|
42
|
+
unless children.empty?
|
|
43
|
+
results[group[:name].to_sym][:children] = children
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
unless groupvars.empty?
|
|
47
|
+
results[group[:name].to_sym][:groupvars] = groupvars
|
|
51
48
|
end
|
|
52
49
|
end
|
|
53
|
-
|
|
50
|
+
|
|
54
51
|
fmt.dump(results)
|
|
55
52
|
end
|
|
56
53
|
end
|
|
@@ -12,41 +12,38 @@ module Moose
|
|
|
12
12
|
'List the groups, together with any associated hosts and groupvars'
|
|
13
13
|
def list # rubocop:disable Metrics/AbcSize
|
|
14
14
|
# Convenience
|
|
15
|
-
db
|
|
15
|
+
db = Moose::Inventory::DB
|
|
16
16
|
confopts = Moose::Inventory::Config._confopts
|
|
17
|
-
|
|
17
|
+
|
|
18
18
|
# Process
|
|
19
19
|
results = {}
|
|
20
20
|
db.models[:group].all.each do |group|
|
|
21
|
-
|
|
22
21
|
hosts = group.hosts_dataset.map(:name)
|
|
23
22
|
|
|
24
23
|
# Hide the automatic ungrouped group, if it's empty
|
|
25
|
-
next if group[:name] == 'ungrouped' && hosts.
|
|
24
|
+
next if group[:name] == 'ungrouped' && hosts.empty?
|
|
26
25
|
|
|
27
26
|
children = group.children_dataset.map(:name)
|
|
28
|
-
|
|
27
|
+
|
|
29
28
|
groupvars = {}
|
|
30
29
|
group.groupvars_dataset.each do |gv|
|
|
31
30
|
groupvars[gv[:name].to_sym] = gv[:value]
|
|
32
31
|
end
|
|
33
32
|
|
|
34
|
-
|
|
35
33
|
results[group[:name].to_sym] = {}
|
|
36
|
-
unless hosts.
|
|
34
|
+
unless hosts.empty? && (confopts[:ansible] != true)
|
|
37
35
|
results[group[:name].to_sym][:hosts] = hosts
|
|
38
36
|
end
|
|
39
37
|
|
|
40
|
-
unless children.
|
|
38
|
+
unless children.empty?
|
|
41
39
|
results[group[:name].to_sym][:children] = children
|
|
42
40
|
end
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
end
|
|
41
|
+
|
|
42
|
+
next if groupvars.empty?
|
|
43
|
+
if confopts[:ansible] == true
|
|
44
|
+
results[group[:name].to_sym][:vars] = groupvars
|
|
45
|
+
else
|
|
46
|
+
results[group[:name].to_sym][:groupvars] = groupvars
|
|
50
47
|
end
|
|
51
48
|
end
|
|
52
49
|
Formatter.out(results)
|
|
@@ -19,23 +19,22 @@ module Moose
|
|
|
19
19
|
# Note, the Ansible spects don't call for a "--group GROUPNAME" method.
|
|
20
20
|
# So, strictly, there is no Ansible compatibility for this method.
|
|
21
21
|
# Instead, the Ansible compatibility included herein is for consistency
|
|
22
|
-
# with the "hosts listvars" method, which services the Ansible
|
|
22
|
+
# with the "hosts listvars" method, which services the Ansible
|
|
23
23
|
# "--host HOSTNAME" specs.
|
|
24
|
-
|
|
24
|
+
|
|
25
25
|
# sanity
|
|
26
26
|
if confopts[:ansible] == true
|
|
27
27
|
if argv.length != 1
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
abort('ERROR: Wrong number of arguments for Ansible mode, '\
|
|
29
|
+
"#{args.length} for 1.")
|
|
30
30
|
end
|
|
31
31
|
else
|
|
32
|
-
if argv.
|
|
32
|
+
if argv.empty?
|
|
33
33
|
abort('ERROR: Wrong number of arguments, '\
|
|
34
34
|
"#{args.length} for 1 or more.")
|
|
35
35
|
end
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
-
|
|
39
38
|
# Convenience
|
|
40
39
|
db = Moose::Inventory::DB
|
|
41
40
|
fmt = Moose::Inventory::Cli::Formatter
|
|
@@ -43,10 +42,10 @@ module Moose
|
|
|
43
42
|
# Arguments
|
|
44
43
|
names = argv.uniq.map(&:downcase)
|
|
45
44
|
|
|
46
|
-
#process
|
|
45
|
+
# process
|
|
47
46
|
results = {}
|
|
48
|
-
|
|
49
|
-
if confopts[:ansible] == true
|
|
47
|
+
|
|
48
|
+
if confopts[:ansible] == true
|
|
50
49
|
# This is the implementation per Ansible specs
|
|
51
50
|
name = names.first
|
|
52
51
|
group = db.models[:group].find(name: name)
|
|
@@ -61,11 +60,10 @@ module Moose
|
|
|
61
60
|
# This our more flexible implementation
|
|
62
61
|
names.each do |name|
|
|
63
62
|
group = db.models[:group].find(name: name)
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
end
|
|
63
|
+
next if group.nil?
|
|
64
|
+
results[name.to_sym] = {}
|
|
65
|
+
group.groupvars_dataset.each do |gv|
|
|
66
|
+
results[name.to_sym][gv[:name].to_sym] = gv[:value]
|
|
69
67
|
end
|
|
70
68
|
end
|
|
71
69
|
end
|