Class: Kdeploy::Inventory

Inherits:
Object
  • Object
show all
Defined in:
lib/kdeploy/inventory.rb

Overview

Inventory class for managing host inventory and configuration

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#groupsObject (readonly)

Returns the value of attribute groups.



6
7
8
# File 'lib/kdeploy/inventory.rb', line 6

def groups
  @groups
end

#hostsObject (readonly)

Returns the value of attribute hosts.



6
7
8
# File 'lib/kdeploy/inventory.rb', line 6

def hosts
  @hosts
end

#varsObject (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_hostsArray<Host>

Get all hosts

Returns:

  • (Array<Host>)

    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

Parameters:

  • var_name (String, Symbol)

    Variable name

Returns:

  • (Object)

    Variable value



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

Parameters:

  • group_name (String, Symbol)

    Group name

  • var_name (String, Symbol)

    Variable name

Returns:

  • (Object)

    Variable value



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

Parameters:

  • hostname (String)

    Hostname

Returns:

  • (Host, nil)

    Host object or nil



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

Parameters:

  • group_name (String, Symbol)

    Group name

Returns:

  • (Array<Host>)

    Hosts in the 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

Parameters:

  • role (String, Symbol)

    Role name

Returns:

  • (Array<Host>)

    Hosts with the 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

Parameters:

  • inventory_file (String)

    Path to inventory file

Raises:



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.message}"
rescue StandardError => e
  raise ConfigurationError, "Failed to load inventory file: #{e.message}"
end

#summaryHash

Export inventory summary

Returns:

  • (Hash)

    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