Class: Moose::Inventory::Cli::Group
- Inherits:
-
Thor
- Object
- Thor
- Moose::Inventory::Cli::Group
- 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_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
Overview
Implemention of the “group addchild” methods of the CLI
Instance Method Summary collapse
-
#add(*argv) ⇒ Object
rubocop:disable Metrics/LineLength.
- #addchild ⇒ Object
-
#addhost(*args) ⇒ Object
rubocop:disable Metrics/AbcSize.
- #addvar(*args) ⇒ Object
-
#get(*argv) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#list ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#rm(*argv) ⇒ Object
rubocop:disable Metrics/AbcSize.
- #rmchild ⇒ Object
-
#rmhost(*args) ⇒ Object
rubocop:disable Metrics/LineLength.
- #rmvar(*args) ⇒ Object
Instance Method Details
#add(*argv) ⇒ Object
rubocop:disable Metrics/LineLength
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/moose_inventory/cli/group_add.rb', line 14 def add(*argv) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity # rubocop:enable Metrics/LineLength if argv.length < 1 abort("ERROR: Wrong number of arguments, #{argv.length} for 1 or more.") end # Arguments names = argv.uniq.map(&:downcase) [:hosts] = '' if [:hosts].nil? hosts = [:hosts].downcase.split(',').uniq # sanity if names.include?('ungrouped') abort("ERROR: Cannot manually manipulate the automatic group 'ungrouped'\n") end # Convenience db = Moose::Inventory::DB fmt = Moose::Inventory::Cli::Formatter # Transaction warn_count = 0 db.transaction do # Transaction start names.each do |name| # Add the group puts "Add group '#{name}':" group = db.models[:group].find(name: name) hosts_ds = nil fmt.puts 2, "- create group..." if group.nil? group = db.models[:group].create(name: name) fmt.puts 4, '- OK' else warn_count += 1 fmt.warn "Group '#{name}' already exists, skipping creation.\n" fmt.puts 4, "- already exists, skipping." hosts_ds = group.hosts_dataset fmt.puts 4, '- OK' end # Associate with hosts hosts.each do |h| next if h.nil? || h.empty? fmt.puts 2, "- add association {group:#{name} <-> host:#{ h }}..." host = db.models[:host].find(name: h) if host.nil? warn_count += 1 fmt.warn "Host '#{h}' doesn't exist, but will be created.\n" fmt.puts 4, "- host doesn't exist, creating now..." host = db.models[:host].create(name: h) fmt.puts 6, "- OK" end if !hosts_ds.nil? && !hosts_ds[name: h].nil? warn_count += 1 fmt.warn "Association {group:#{name} <-> host:#{ h }}"\ " already exists, skipping creation.\n" fmt.puts 4, "- already exists, skipping." else group.add_host(host) end fmt.puts 4, '- OK' # Handle the host's automatic 'ungrouped' group ungrouped = host.groups_dataset[name: 'ungrouped'] unless ungrouped.nil? fmt.puts 2, "- remove automatic association {group:ungrouped"\ " <-> host:#{ h }}..." host.remove_group( ungrouped ) unless ungrouped.nil? fmt.puts 4, '- OK' end end fmt.puts 2, '- all OK' end end # Transaction end if warn_count == 0 puts 'Succeeded' else puts 'Succeeded, with warnings.' end end |
#addchild ⇒ Object
14 15 16 17 |
# File 'lib/moose_inventory/cli/group_addchild.rb', line 14 def addchild abort("The 'groups addchild GROUP' method is not yet implemented") puts 'group addchild' end |
#addhost(*args) ⇒ Object
rubocop:disable Metrics/AbcSize
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/moose_inventory/cli/group_addhost.rb', line 13 def addhost(*args) # rubocop:disable Metrics/AbcSize # Sanity if args.length < 2 abort("ERROR: Wrong number of arguments, #{args.length} for 2 or more.") end # Arguments name = args[0].downcase hosts = args.slice(1, args.length - 1).uniq.map(&:downcase) # Sanity if name == 'ungrouped' abort("ERROR: Cannot manually manipulate the automatic group 'ungrouped'.") end # Convenience db = Moose::Inventory::DB fmt = Moose::Inventory::Cli::Formatter # Transaction warn_count = 0 begin db.transaction do # Transaction start puts "Associate group '#{name}' with host(s) '#{hosts.join(',')}':" # Get the target group fmt.puts 2, "- retrieve group '#{name}'..." group = db.models[:group].find(name: name) if group.nil? abort("ERROR: The group '#{name}' does not exist.") end fmt.puts 4, '- OK' # Associate group with the hosts ungrouped = db.models[:group].find_or_create(name: 'ungrouped') hosts_ds = group.hosts_dataset hosts.each do |h| # rubocop:disable Style/Next fmt.puts 2, "- add association {group:#{name} <-> host:#{ h }}..." # Check against existing associations unless hosts_ds[name: h].nil? warn_count += 1 fmt.warn "Association {group:#{name} <-> host:#{ h }} already"\ " exists, skipping.\n" fmt.puts 4, '- already exists, skipping.' fmt.puts 4, '- OK' next end # Add new association host = db.models[:host].find(name: h) if host.nil? warn_count += 1 fmt.warn "Host '#{h}' does not exist and will be created.\n" fmt.puts 4, '- host does not exist, creating now...' host = db.models[:host].create(name: h) fmt.puts 6, '- OK' end group.add_host(host) fmt.puts 4, '- OK' # Remove the host from the ungrouped group, if necessary unless host.groups_dataset[name: 'ungrouped'].nil? fmt.puts 2,'- remove automatic association '\ "{group:ungrouped <-> host:#{h}}..." host.remove_group(ungrouped) fmt.puts 4, '- OK' end end fmt.puts 2, '- all OK' end # Transaction end rescue db.exceptions[:moose] => e abort("ERROR: #{e.message}") end if warn_count == 0 puts 'Succeeded.' else puts 'Succeeded, with warnings.' end end |
#addvar(*args) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/moose_inventory/cli/group_addvar.rb', line 13 def addvar(*args) if args.length < 2 abort('ERROR: Wrong number of arguments, '\ "#{args.length} for 2 or more.") end # Convenience db = Moose::Inventory::DB fmt = Moose::Inventory::Cli::Formatter # Arguments name = args[0].downcase vars = args.slice(1, args.length - 1).uniq # Transaction db.transaction do # Transaction start puts "Add variables '#{vars.join(",")}' to group '#{name}':" fmt.puts 2,"- retrieve group '#{name}'..." group = db.models[:group].find(name: name) if group.nil? fail db.exceptions[:moose], "The group '#{name}' does not exist." end fmt.puts 4, '- OK' groupvars_ds = group.groupvars_dataset vars.each do |v| fmt.puts 2, "- add variable '#{v}'..." vararray = v.split('=') if v.start_with?('=') || v.end_with?('=') || vararray.length != 2 fail db.exceptions[:moose], "Incorrect format in '{#{v}}'. Expected 'key=value'." end # Check against existing associations groupvar = groupvars_ds[name: vararray[0]] if !groupvar.nil? unless groupvar[:value] == vararray[1] fmt.puts 4, '- already exists, applying as an update...' update = db.models[:groupvar].find(id: groupvar[:id]) update[:value] = vararray[1] update.save end else # groupvar doesn't exist, so create and associate groupvar = db.models[:groupvar].create(name: vararray[0], value: vararray[1]) group.add_groupvar(groupvar) end fmt.puts 4, '- OK' end fmt.puts 2, '- all OK' end # Transaction end puts 'Succeeded.' end |
#get(*argv) ⇒ Object
rubocop:disable Metrics/AbcSize
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/moose_inventory/cli/group_get.rb', line 11 def get(*argv) # rubocop:disable Metrics/AbcSize if argv.length < 1 abort('ERROR: Wrong number of arguments, '\ "#{argv.length} for 1 or more") end # Convenience db = Moose::Inventory::DB fmt = Moose::Inventory::Cli::Formatter # Arguments names = argv.uniq.map(&:downcase) # Process results = {} names.each do |name| group = db.models[:group].find(name: name) unless group.nil? hosts = group.hosts_dataset.map(:name) groupvars = {} group.groupvars_dataset.each do |gv| groupvars[gv[:name].to_sym] = gv[:value] end results[group[:name].to_sym] = {} unless hosts.length == 0 results[group[:name].to_sym][:hosts] = hosts end unless groupvars.length == 0 results[group[:name].to_sym][:groupvars] = groupvars end end end fmt.dump(results) end |
#list ⇒ Object
rubocop:disable Metrics/AbcSize
14 15 16 17 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_list.rb', line 14 def list # rubocop:disable Metrics/AbcSize # Convenience db = Moose::Inventory::DB # Process results = {} db.models[:group].all.each do |group| hosts = group.hosts_dataset.map(:name) groupvars = {} group.groupvars_dataset.each do |gv| groupvars[gv[:name].to_sym] = gv[:value] end results[group[:name].to_sym] = {} unless hosts.length == 0 results[group[:name].to_sym][:hosts] = hosts end unless groupvars.length == 0 results[group[:name].to_sym][:groupvars] = groupvars end end Formatter.out(results) end |
#rm(*argv) ⇒ Object
rubocop:disable Metrics/AbcSize
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/moose_inventory/cli/group_rm.rb', line 14 def rm(*argv) # rubocop:disable Metrics/AbcSize # # Sanity if argv.length < 1 abort('ERROR: Wrong number of arguments, '\ "#{argv.length} for 1 or more.") end # Convenience db = Moose::Inventory::DB fmt = Moose::Inventory::Cli::Formatter # Arguments names = argv.uniq.map(&:downcase) # sanity if names.include?('ungrouped') abort("Cannot manually manipulate the automatic group 'ungrouped'\n") end # Transaction warn_count = 0 db.transaction do # Transaction start names.each do |name| puts "Remove group '#{name}':" fmt.puts 2, "- Retrieve group '#{name}'..." group = db.models[:group].find(name: name) if group.nil? warn_count += 1 fmt.warn "Group '#{ name }' does not exist, skipping.\n" fmt.puts 4, "- No such group, skipping." end fmt.puts 4, "- OK" unless group.nil? # Handle automatic group for any associated hosts hosts_ds = group.hosts_dataset hosts_ds.each do |host| host_groups_ds = host.groups_dataset if host_groups_ds.count == 1 # We're the only group fmt.puts 2, "- Adding automatic association {group:ungrouped <-> host:#{host[:name]}}..." ungrouped = db.models[:group].find_or_create(name: 'ungrouped') host.add_group(ungrouped) fmt.puts 4, "- OK" end end # Remove the group fmt.puts 2, "- Destroy group '#{name}'..." group.remove_all_hosts group.destroy fmt.puts 4, "- OK" end fmt.puts 2, "- All OK" end end # Transaction end if warn_count == 0 puts "Succeeded." else puts "Succeeded, with warnings." end end |
#rmchild ⇒ Object
13 14 15 16 |
# File 'lib/moose_inventory/cli/group_rmchild.rb', line 13 def rmchild abort("The 'groups rmchild GROUP' method is not yet implemented") puts 'group rmchild' end |
#rmhost(*args) ⇒ Object
rubocop:disable Metrics/LineLength
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/moose_inventory/cli/group_rmhost.rb', line 15 def rmhost(*args) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/CyclomaticComplexity # rubocop:enable Metrics/LineLength # Sanity if args.length < 2 abort("ERROR: Wrong number of arguments, #{args.length} for 2 or more.") end # Arguments name = args[0].downcase hosts = args.slice(1, args.length - 1).uniq.map(&:downcase) # Sanity if name == 'ungrouped' abort("ERROR: Cannot manually manipulate the automatic group 'ungrouped'.") end # Convenience db = Moose::Inventory::DB fmt = Moose::Inventory::Cli::Formatter # Transaction warn_count = 0 begin db.transaction do # Transaction start # Get the target group puts "Dissociate group '#{name}' from host(s) '#{hosts.join(',')}':" fmt.puts 2, "- retrieve group '#{name}'..." group = db.models[:group].find(name: name) if group.nil? abort("ERROR: The group '#{name}' does not exist.") end fmt.puts 4, '- OK' # dissociate group from the hosts ungrouped = db.models[:group].find_or_create(name: 'ungrouped') hosts_ds = group.hosts_dataset hosts.each do |h| # rubocop:disable Style/Next fmt.puts 2, "- remove association {group:#{name} <-> host:#{ h }}..." # Check against existing associations if hosts_ds[name: h].nil? warn_count += 1 fmt.warn "Association {group:#{name} <-> host:#{ h }} doesn't"\ " exist, skipping.\n" fmt.puts 4, '- doesn\'t exist, skipping.' fmt.puts 4, '- OK' next end host = db.models[:host].find(name: h) group.remove_host(host) unless host.nil? fmt.puts 4,'- OK' # Add the host to the ungrouped group if not in any other group if host.groups_dataset.count == 0 fmt.puts 2, "- add automatic association {group:ungrouped <-> host:#{h}}..." host.add_group(ungrouped) fmt.puts 4, '- OK' end end fmt.puts 2, '- all OK' end # Transaction end rescue db.exceptions[:moose] => e abort("ERROR: #{e.message}") end if warn_count == 0 puts 'Succeeded.' else puts 'Succeeded, with warnings.' end end |
#rmvar(*args) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/moose_inventory/cli/group_rmvar.rb', line 13 def rmvar(*args) if args.length < 2 abort('ERROR: Wrong number of arguments, ' \ "#{args.length} for 2 or more.") end # Convenience db = Moose::Inventory::DB fmt = Moose::Inventory::Cli::Formatter # Arguments name = args[0].downcase vars = args.slice(1, args.length - 1).uniq # Transaction db.transaction do # Transaction start puts "Remove variable(s) '#{vars.join(",")}' from group '#{name}':" fmt.puts 2, "- retrieve group '#{name}'..." group = db.models[:group].find(name: name) if group.nil? fail db.exceptions[:moose], "The group '#{name}' does not exist." end fmt.puts 4, '- OK' groupvars_ds = group.groupvars_dataset vars.each do |v| fmt.puts 2, "- remove variable '#{v}'..." vararray = v.split('=') if v.start_with?('=') || v.scan('=').count > 1 fail db.exceptions[:moose], "Incorrect format in {#{v}}. " \ 'Expected \'key\' or \'key=value\'.' end # Check against existing associations groupvar = groupvars_ds[name: vararray[0]] unless groupvar.nil? # remove the association group.remove_groupvar(groupvar) groupvar.destroy end fmt.puts 4, '- OK' end fmt.puts 2, '- all OK' end # Transaction end puts 'Succeeded.' end |