class Moose::Inventory::Cli::Host
Class implementing the “host” methods of the CLI
Class implementing the “host” methods of the CLI
implementation of the “addgroup” method of the CLI
implementation of the “host addvar” method of the CLI
Class implementing the “host get” method of the CLI
Implementation of the “host list” method of the CLI
implementation of the “host listvars” method of the CLI
implementation of the “host rm” method of the CLI
implementation the “host rmgroup” methods of the CLI
implementation of the “host rmvar” method of the CLI
Public Instance Methods
add(*argv)
click to toggle source
rubocop:disable Metrics/LineLength
# File lib/moose_inventory/cli/host_add.rb, line 19 def add(*argv) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity # rubocop:enable Metrics/LineLength 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) # split(/\W+/) splits on hyphens too, which is not what we want #groups = options[:groups].downcase.split(/\W+/).uniq options[:groups].nil? && options[:groups] = '' groups = options[:groups].downcase.split(',').uniq # Sanity if groups.include?('ungrouped') abort("ERROR: Cannot manually manipulate "\ "the automatic group 'ungrouped'.") end # Process db.transaction do # Transaction start fmt.reset_indent names.each do |name| puts "Add host '#{name}':" fmt.puts 2, "- Creating host '#{name}'..." host = db.models[:host].find(name: name) groups_ds = nil if host.nil? host = db.models[:host].create(name: name) else fmt.warn "The host '#{name}' already exists, skipping creation.\n" groups_ds = host.groups_dataset end fmt.puts 4, "- OK" groups.each do |g| next if g.nil? || g.empty? fmt.puts 2, "- Adding association {host:#{name} <-> group:#{g}}..." group = db.models[:group].find(name: g) if group.nil? fmt.warn "The group '#{g}' doesn't exist, but will be created.\n" group = db.models[:group].create(name: g) end if !groups_ds.nil? && groups_ds[name: g].nil? fmt.warn "Association {host:#{name} <-> group:#{ g }} already exists, skipping creation.\n" else host.add_group(group) end fmt.puts 4, '- OK' end # Handle the automatic 'ungrouped' group groups_ds = host.groups_dataset if !groups_ds.nil? && groups_ds.count == 0 fmt.puts 2, "- Adding automatic association {host:#{name} <-> group:ungrouped}..." ungrouped = db.models[:group].find_or_create(name: 'ungrouped') host.add_group(ungrouped) fmt.puts 4, "- OK" end fmt.puts 2, "- All OK" end end # Transaction end puts 'Succeeded' end
addgroup(*args)
click to toggle source
rubocop:disable Metrics/LineLength
# File lib/moose_inventory/cli/host_addgroup.rb, line 16 def addgroup(*args) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity # rubocop:enable Metrics/LineLength # Sanity 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 groups = args.slice(1, args.length - 1).uniq.map(&:downcase) # Sanity if groups.include?('ungrouped') abort 'ERROR: Cannot manually manipulate the automatic '\ 'group \'ungrouped\'.' end # Transaction db.transaction do # Transaction start puts "Associate host '#{name}' with groups '#{groups.join(',')}':" # Get the target host fmt.puts 2, "- Retrieve host '#{name}'..." host = db.models[:host].find(name: name) if host.nil? fail db.exceptions[:moose], "The host '#{name}' "\ 'was not found in the database.' end fmt.puts 4, '- OK' # Associate host with the groups groups_ds = host.groups_dataset groups.each do |g| fmt.puts 2, "- Add association {host:#{name} <-> group:#{ g }}..." # Check against existing associations if !groups_ds[name: g].nil? fmt.warn "Association {host:#{name} <-> group:#{g}} already exists, skipping." fmt.puts 4, "- Already exists, skipping." else # Add new association group = db.models[:group].find(name: g) if group.nil? fmt.warn "Group '#{g}' does not exist and will be created." fmt.puts 4, "- Group does not exist, creating now..." group = db.models[:group].create(name: g) fmt.puts 6, "- OK" end host.add_group(group) end fmt.puts 4, '- OK' end # Handle 'ungrouped' group automation unless groups_ds[name: 'ungrouped'].nil? fmt.puts 2, '- Remove automatic association '\ "{host:#{name} <-> group:ungrouped}..." ungrouped = db.models[:group].find(name: 'ungrouped') host.remove_group(ungrouped) unless ungrouped.nil? fmt.puts 4, '- OK' end fmt.puts 2, '- All OK' end # Transaction end puts 'Succeeded' end
addvar(*args)
click to toggle source
rubocop:disable Metrics/LineLength
# File lib/moose_inventory/cli/host_addvar.rb, line 16 def addvar(*args) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity # rubocop:enable Metrics/LineLength 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 host '#{name}':" fmt.puts 2,"- retrieve host '#{name}'..." host = db.models[:host].find(name: name) if host.nil? fail db.exceptions[:moose], "The host '#{name}' does not exist." end fmt.puts 4, '- OK' hostvars_ds = host.hostvars_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 hostvar = hostvars_ds[name: vararray[0]] if !hostvar.nil? unless hostvar[:value] == vararray[1] fmt.puts 4, '- already exists, applying as an update...' update = db.models[:hostvar].find(id: hostvar[:id]) update[:value] = vararray[1] update.save end else # hostvar doesn't exist, so create and associate hostvar = db.models[:hostvar].create(name: vararray[0], value: vararray[1]) host.add_hostvar(hostvar) end fmt.puts 4, '- OK' end fmt.puts 2, '- all OK' end # Transaction end puts 'Succeeded.' end
get(*argv)
click to toggle source
# File lib/moose_inventory/cli/host_get.rb, line 18 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| # rubocop:disable Style/Next host = db.models[:host].find(name: name) unless host.nil? groups = host.groups_dataset.map(:name) hostvars = {} host.hostvars_dataset.each do |hv| hostvars[hv[:name].to_sym] = hv[:value] end results[host[:name].to_sym] = {} unless groups.length == 0 results[host[:name].to_sym][:groups] = groups end unless hostvars.length == 0 results[host[:name].to_sym][:hostvars] = hostvars end end end fmt.dump(results) end
list()
click to toggle source
# File lib/moose_inventory/cli/host_list.rb, line 14 def list # rubocop:disable Metrics/AbcSize # Convenience db = Moose::Inventory::DB fmt = Moose::Inventory::Cli::Formatter # Process results = {} db.models[:host].all.each do |host| groups = host.groups_dataset.map(:name) results[host[:name].to_sym] = {} results[host[:name].to_sym][:groups] = groups hostvars = {} host.hostvars_dataset.each do |hv| hostvars[hv[:name].to_sym] = hv[:value] end unless hostvars.length == 0 results[host[:name].to_sym][:hostvars] = hostvars end end fmt.dump(results) end
listvars(*argv)
click to toggle source
# File lib/moose_inventory/cli/host_listvars.rb, line 15 def listvars(*argv) # Convenience confopts = Moose::Inventory::Config._confopts # 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.length < 1 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 host = db.models[:host].find(name: name) if host.nil? fmt.warn "The host #{name} does not exist.\n" else host.hostvars_dataset.each do |hv| results[hv[:name].to_sym] = hv[:value] end end # Add the Ansible 1.3 '_meta' tag # see http://docs.ansible.com/ansible/developing_inventory.html#tuning-the-external-inventory-script results['_meta'.to_sym] = {} results['_meta'.to_sym]['hostvars'.to_sym] = {} db.models[:host].each do |host| results['_meta'.to_sym]['hostvars'.to_sym][host.name().to_sym] = {} host.hostvars_dataset.each do |hv| results['_meta'.to_sym]['hostvars'.to_sym][host.name().to_sym][hv[:name].to_sym] = hv[:value] end end else # This our more flexible implementation, which is not compatible # with the Ansible specs names.each do |name| host = db.models[:host].find(name: name) unless host.nil? results[name.to_sym] = {} host.hostvars_dataset.each do |hv| results[name.to_sym][hv[:name].to_sym] = hv[:value] end end end end fmt.dump(results) end
rm(*argv)
click to toggle source
# File lib/moose_inventory/cli/host_rm.rb, line 16 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) # Transaction warn_count = 0 db.transaction do # Transaction start names.each do |name| puts "Remove host '#{name}':" fmt.puts 2, "- Retrieve host '#{name}'..." host = db.models[:host].find(name: name) if host.nil? warn_count += 1 fmt.warn "Host '#{name}' does not exist, skipping.\n" fmt.puts 4, "- No such host, skipping." end fmt.puts 4, "- OK" unless host.nil? fmt.puts 2, "- Destroy host '#{name}'..." host.remove_all_groups host.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
rmgroup(*args)
click to toggle source
rubocop:disable Metrics/LineLength
# File lib/moose_inventory/cli/host_rmgroup.rb, line 17 def rmgroup(*args) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity # rubocop:enable Metrics/LineLength 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 groups = args.slice(1, args.length - 1).uniq.map(&:downcase) # Sanity if groups.include?('ungrouped') abort 'ERROR: Cannot manually manipulate the automatic '\ 'group \'ungrouped\'.' end # Transaction db.transaction do # Transaction start puts "Dissociate host '#{name}' from groups '#{groups.join(',')}':" fmt.puts 2, "- Retrieve host '#{name}'..." host = db.models[:host].find(name: name) if host.nil? fail db.exceptions[:moose], "The host '#{name}' was not found in the database." end fmt.puts 4, '- OK' # dissociate host from the groups groups_ds = host.groups_dataset groups.each do |g| fmt.puts 2, "- Remove association {host:#{name} <-> group:#{g}}..." # Check against existing associations if groups_ds[name: g].nil? fmt.warn "Association {host:#{name} <-> group:#{g}} doesn't exist, skipping.\n" fmt.puts 4, "- Doesn't exist, skipping." else group = db.models[:group].find(name: g) host.remove_group(group) unless group.nil? end fmt.puts 4, '- OK' end # Handle 'ungrouped' group automation if host.groups_dataset.count == 0 fmt.puts 2, '- Add automatic association '\ "{host:#{name} <-> group:ungrouped}..." ungrouped = db.models[:group].find_or_create(name: 'ungrouped') host.add_group(ungrouped) unless ungrouped.nil? fmt.puts 4, '- OK' end fmt.puts 2, '- All OK' end # End transaction puts 'Succeeded' end
rmvar(*args)
click to toggle source
rubocop:disable Metrics/LineLength
# File lib/moose_inventory/cli/host_rmvar.rb, line 16 def rmvar(*args) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize # rubocop:enableMetrics/LineLength 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 host '#{name}':" fmt.puts 2, "- retrieve host '#{name}'..." host = db.models[:host].find(name: name) if host.nil? fail db.exceptions[:moose], "The host '#{name}' does not exist." end fmt.puts 4, '- OK' hostvars_ds = host.hostvars_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 hostvar = hostvars_ds[name: vararray[0]] unless hostvar.nil? # remove the association host.remove_hostvar(hostvar) hostvar.destroy end fmt.puts 4, '- OK' end fmt.puts 2, '- all OK' end # Transaction end puts 'Succeeded.' end