Class: Moose::Inventory::Cli::Host
- Inherits:
-
Thor
- Object
- Thor
- Moose::Inventory::Cli::Host
- Defined in:
- lib/moose_inventory/cli/host.rb,
lib/moose_inventory/cli/host_rm.rb,
lib/moose_inventory/cli/host_add.rb,
lib/moose_inventory/cli/host_get.rb,
lib/moose_inventory/cli/host_list.rb,
lib/moose_inventory/cli/host_rmvar.rb,
lib/moose_inventory/cli/host_addvar.rb,
lib/moose_inventory/cli/host_rmgroup.rb,
lib/moose_inventory/cli/host_addgroup.rb
Overview
implementation of the “addgroup” method of the CLI
Instance Method Summary collapse
-
#add(*argv) ⇒ Object
rubocop:disable Metrics/LineLength.
-
#addgroup(*args) ⇒ Object
rubocop:disable Metrics/LineLength.
-
#addvar(*args) ⇒ Object
rubocop:disable Metrics/LineLength.
-
#get(*argv) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#list ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#rm(*argv) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#rmgroup(*args) ⇒ Object
rubocop:disable Metrics/LineLength.
-
#rmvar(*args) ⇒ Object
rubocop:disable Metrics/LineLength.
Instance Method Details
#add(*argv) ⇒ Object
rubocop:disable Metrics/LineLength
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 |
# 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 [:groups].nil? && [:groups] = '' groups = [: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) ⇒ Object
rubocop:disable Metrics/LineLength
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 |
# 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) ⇒ Object
rubocop:disable Metrics/LineLength
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 |
# 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) ⇒ Object
rubocop:disable Metrics/AbcSize
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 |
# 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 ⇒ 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 |
# 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 |
#rm(*argv) ⇒ Object
rubocop:disable Metrics/AbcSize
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 |
# 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) ⇒ Object
rubocop:disable Metrics/LineLength
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 |
# 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) ⇒ Object
rubocop:disable Metrics/LineLength
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 |
# 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 |