Class: Moose::Inventory::Cli::Group
- Inherits:
-
Thor
- Object
- Thor
- Moose::Inventory::Cli::Group
- Includes:
- Helpers
- 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,
lib/moose_inventory/cli/group_listvars.rb
Overview
implementation of the “group listvars” method of the CLI
Constant Summary
Constants included from Helpers
Instance Method Summary collapse
- #add(*argv) ⇒ Object
- #addchild(*argv) ⇒ Object
- #addhost(*args) ⇒ Object
- #addvar(*args) ⇒ Object
-
#get(*argv) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#list ⇒ Object
rubocop:disable Metrics/AbcSize.
- #listvars(*argv) ⇒ Object
- #rm(*argv) ⇒ Object
- #rmchild(*argv) ⇒ Object
- #rmhost(*args) ⇒ Object
- #rmvar(*args) ⇒ Object
Instance Method Details
#add(*argv) ⇒ Object
17 18 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/group_add.rb', line 17 def add(*argv) abort_if_missing_args(argv, 1, '1 or more') names = normalize_names(argv) hosts = csv_option_names([:hosts]) abort_if_automatic_group( names, "ERROR: Cannot manually manipulate the automatic group 'ungrouped'\n" ) result = Moose::Inventory::Operations::AddGroups .new(context: Moose::Inventory::InventoryContext.new(db: db)) .call(names: names, hosts: hosts) render_add_groups_events(result.events) if result.warning_count.zero? puts 'Succeeded' else puts 'Succeeded, with warnings.' end end |
#addchild(*argv) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/moose_inventory/cli/group_addchild.rb', line 17 def addchild(*argv) abort_if_missing_args(argv, 2, '2 or more') pname = argv[0].downcase cnames = normalize_names(argv.slice(1, argv.length - 1)) abort_if_automatic_group([pname] + cnames) result = add_children_to_group(pname, cnames) if result.warning_count.zero? puts 'Succeeded.' else puts 'Succeeded, with warnings.' end end |
#addhost(*args) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/moose_inventory/cli/group_addhost.rb', line 17 def addhost(*args) abort_if_missing_args(args, 2, '2 or more') name = args[0].downcase hosts = normalize_names(args.slice(1, args.length - 1)) abort_if_automatic_group([name]) result = add_hosts_to_group(name, hosts) if result.warning_count.zero? 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 49 50 51 52 |
# File 'lib/moose_inventory/cli/group_get.rb', line 11 def get(*argv) # rubocop:disable Metrics/AbcSize if argv.empty? 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) next if group.nil? hosts = group.hosts_dataset.map(:name) children = group.children_dataset.map(:name) groupvars = {} group.groupvars_dataset.each do |gv| groupvars[gv[:name].to_sym] = gv[:value] end results[group[:name].to_sym] = {} results[group[:name].to_sym][:hosts] = hosts unless hosts.empty? unless children.empty? results[group[:name].to_sym][:children] = children end unless groupvars.empty? results[group[:name].to_sym][:groupvars] = groupvars end end fmt.dump(results) end |
#list ⇒ 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 |
# File 'lib/moose_inventory/cli/group_list.rb', line 13 def list # rubocop:disable Metrics/AbcSize # Convenience db = Moose::Inventory::DB confopts = Moose::Inventory::Config._confopts # Process results = {} db.models[:group].all.each do |group| hosts = group.hosts_dataset.map(:name) # Hide the automatic ungrouped group, if it's empty next if group[:name] == 'ungrouped' && hosts.empty? children = group.children_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.empty? && (confopts[:ansible] != true) results[group[:name].to_sym][:hosts] = hosts end unless children.empty? results[group[:name].to_sym][:children] = children end next if groupvars.empty? if confopts[:ansible] == true results[group[:name].to_sym][:vars] = groupvars else results[group[:name].to_sym][:groupvars] = groupvars end end Formatter.out(results) end |
#listvars(*argv) ⇒ Object
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 |
# File 'lib/moose_inventory/cli/group_listvars.rb', line 15 def listvars(*argv) # Convenience confopts = Moose::Inventory::Config._confopts # Note, the Ansible spects don't call for a "--group GROUPNAME" method. # So, strictly, there is no Ansible compatibility for this method. # Instead, the Ansible compatibility included herein is for consistency # with the "hosts listvars" method, which services the Ansible # "--host HOSTNAME" specs. # sanity if confopts[:ansible] == true if argv.length != 1 abort('ERROR: Wrong number of arguments for Ansible mode, '\ "#{args.length} for 1.") end else if argv.empty? abort('ERROR: Wrong number of arguments, '\ "#{args.length} for 1 or more.") end end # Convenience db = Moose::Inventory::DB fmt = Moose::Inventory::Cli::Formatter # Arguments names = argv.uniq.map(&:downcase) # process results = {} if confopts[:ansible] == true # This is the implementation per Ansible specs name = names.first group = db.models[:group].find(name: name) if group.nil? fmt.warn "The Group #{name} does not exist." else group.groupvars_dataset.each do |gv| results[gv[:name].to_sym] = gv[:value] end end else # This our more flexible implementation names.each do |name| group = db.models[:group].find(name: name) next if group.nil? results[name.to_sym] = {} group.groupvars_dataset.each do |gv| results[name.to_sym][gv[:name].to_sym] = gv[:value] end end end fmt.dump(results) end |
#rm(*argv) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/moose_inventory/cli/group_rm.rb', line 21 def rm(*argv) abort_if_missing_args(argv, 1, '1 or more') names = normalize_names(argv) abort_if_automatic_group( names, "Cannot manually manipulate the automatic group 'ungrouped'\n" ) result = remove_groups(names) if result.warning_count.zero? puts 'Succeeded.' else puts 'Succeeded, with warnings.' end end |
#rmchild(*argv) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/moose_inventory/cli/group_rmchild.rb', line 22 def rmchild(*argv) abort_if_missing_args(argv, 2, '2 or more') pname = argv[0].downcase cnames = normalize_names(argv.slice(1, argv.length - 1)) abort_if_automatic_group([pname] + cnames) result = remove_children_from_group(pname, cnames) if result.warning_count.zero? puts 'Succeeded.' else puts 'Succeeded, with warnings.' end end |
#rmhost(*args) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/moose_inventory/cli/group_rmhost.rb', line 17 def rmhost(*args) abort_if_missing_args(args, 2, '2 or more') name = args[0].downcase hosts = normalize_names(args.slice(1, args.length - 1)) abort_if_automatic_group([name]) result = remove_hosts_from_group(name, hosts) if result.warning_count.zero? 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 |