Class: Kdeploy::Inventory
- Inherits:
-
Object
- Object
- Kdeploy::Inventory
- Defined in:
- lib/kdeploy/inventory.rb
Overview
Inventory class for managing host inventory and configuration
Instance Attribute Summary collapse
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
-
#hosts ⇒ Object
readonly
Returns the value of attribute hosts.
-
#vars ⇒ Object
readonly
Returns the value of attribute vars.
Instance Method Summary collapse
-
#all_hosts ⇒ Array<Host>
Get all hosts.
-
#global_var(var_name) ⇒ Object
Get global variable.
-
#group_var(group_name, var_name) ⇒ Object
Get group variable.
-
#host(hostname) ⇒ Host?
Get host by hostname.
-
#hosts_in_group(group_name) ⇒ Array<Host>
Get all hosts in a group.
-
#hosts_with_role(role) ⇒ Array<Host>
Get all hosts with specific role.
-
#initialize(inventory_file = nil) ⇒ Inventory
constructor
A new instance of Inventory.
-
#load_from_file(inventory_file) ⇒ Object
Load inventory from YAML file.
-
#summary ⇒ Hash
Export inventory summary.
Constructor Details
#initialize(inventory_file = nil) ⇒ Inventory
Returns a new instance of Inventory.
8 9 10 11 12 13 |
# File 'lib/kdeploy/inventory.rb', line 8 def initialize(inventory_file = nil) @hosts = {} @groups = {} @vars = {} load_from_file(inventory_file) if inventory_file && File.exist?(inventory_file) end |
Instance Attribute Details
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
6 7 8 |
# File 'lib/kdeploy/inventory.rb', line 6 def groups @groups end |
#hosts ⇒ Object (readonly)
Returns the value of attribute hosts.
6 7 8 |
# File 'lib/kdeploy/inventory.rb', line 6 def hosts @hosts end |
#vars ⇒ Object (readonly)
Returns the value of attribute vars.
6 7 8 |
# File 'lib/kdeploy/inventory.rb', line 6 def vars @vars end |
Instance Method Details
#all_hosts ⇒ Array<Host>
Get all hosts
46 47 48 |
# File 'lib/kdeploy/inventory.rb', line 46 def all_hosts @hosts.values end |
#global_var(var_name) ⇒ Object
Get global variable
71 72 73 |
# File 'lib/kdeploy/inventory.rb', line 71 def global_var(var_name) @vars[var_name.to_s] || @vars[var_name.to_sym] end |
#group_var(group_name, var_name) ⇒ Object
Get group variable
61 62 63 64 65 66 |
# File 'lib/kdeploy/inventory.rb', line 61 def group_var(group_name, var_name) group_name = group_name.to_s return nil unless @groups[group_name] @groups[group_name][:vars][var_name.to_s] || @groups[group_name][:vars][var_name.to_sym] end |
#host(hostname) ⇒ Host?
Get host by hostname
53 54 55 |
# File 'lib/kdeploy/inventory.rb', line 53 def host(hostname) @hosts[hostname] end |
#hosts_in_group(group_name) ⇒ Array<Host>
Get all hosts in a group
30 31 32 33 34 35 |
# File 'lib/kdeploy/inventory.rb', line 30 def hosts_in_group(group_name) group_name = group_name.to_s return [] unless @groups[group_name] @groups[group_name][:hosts].map { |hostname| @hosts[hostname] }.compact end |
#hosts_with_role(role) ⇒ Array<Host>
Get all hosts with specific role
40 41 42 |
# File 'lib/kdeploy/inventory.rb', line 40 def hosts_with_role(role) @hosts.values.select { |host| host.has_role?(role) } end |
#load_from_file(inventory_file) ⇒ Object
Load inventory from YAML file
18 19 20 21 22 23 24 25 |
# File 'lib/kdeploy/inventory.rb', line 18 def load_from_file(inventory_file) inventory_data = YAML.load_file(inventory_file) parse_inventory(inventory_data) rescue Psych::SyntaxError => e raise ConfigurationError, "Invalid YAML syntax in inventory file: #{e.}" rescue StandardError => e raise ConfigurationError, "Failed to load inventory file: #{e.}" end |
#summary ⇒ Hash
Export inventory summary
77 78 79 80 81 82 83 84 |
# File 'lib/kdeploy/inventory.rb', line 77 def summary { total_hosts: @hosts.size, total_groups: @groups.size, hosts: @hosts.keys, groups: @groups.keys } end |