Class: Facter::Util::SolarisZones Private

Inherits:
Object
  • Object
show all
Defined in:
lib/facter/util/solaris_zones.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Provide a set of utility methods to interact with Solaris zones. This class is expected to be instantiated once per set of resolutions in order to cache the output of the zoneadm command, which can be quite expensive.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ SolarisZones

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of SolarisZones.

Parameters:

  • opts (Hash) (defaults to: {})

    the options to create the instance with

Options Hash (opts):

  • :zoneadm_cmd (String) — default: '/usr/sbin/zoneadm list -cp'

    the system command to inspect zones

  • :zoneadm_output (String) — default: nil

    the cached output of the zoneadm_cmd



52
53
54
55
56
57
58
# File 'lib/facter/util/solaris_zones.rb', line 52

def initialize(opts = {})
  @zoneadm_keys = [:id, :name, :status, :path, :uuid, :brand, :iptype]
  @zoneadm_cmd = opts[:zoneadm_cmd] || '/usr/sbin/zoneadm list -cp'
  if opts[:zoneadm_output]
    @zoneadm_output = opts[:zoneadm_output]
  end
end

Instance Attribute Details

#zone_hashObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



12
13
14
# File 'lib/facter/util/solaris_zones.rb', line 12

def zone_hash
  @zone_hash
end

#zoneadm_cmdObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



13
14
15
# File 'lib/facter/util/solaris_zones.rb', line 13

def zoneadm_cmd
  @zoneadm_cmd
end

#zoneadm_keysObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
# File 'lib/facter/util/solaris_zones.rb', line 15

def zoneadm_keys
  @zoneadm_keys
end

#zoneadm_outputObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



14
15
16
# File 'lib/facter/util/solaris_zones.rb', line 14

def zoneadm_output
  @zoneadm_output
end

Class Method Details

.add_factsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

add_facts defines all of the facts for solaris zones, for example ‘zones`, `zone_global_id`, `zone_global_status`, etc… This method defines the static fact named `zones`. The value of this fact is the numver of zones reported by the zoneadm system command. The `zones` fact also defines all of the dynamic facts describing the following seven attribute values for each zone.

Zones may be added to the system while Facter is loaded. In order to define new dynamic facts that reflect this new information, the ‘virtual` will define new facts as a side effect of refreshing it’s own value.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/facter/util/solaris_zones.rb', line 30

def self.add_facts
  model = new
  model.refresh
  model.add_dynamic_facts
  Facter.add("zones") do
    setcode do
      model.refresh if model.flushed?
      model.add_dynamic_facts
      model.count
    end
    on_flush do
      model.flush!
    end
  end
end

Instance Method Details

#add_dynamic_factsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

add_dynamic_facts defines all of the dynamic facts derived from parsing the output of the zoneadm command. The zone facts are dynamic, so this method has the behavior of figuring out what dynamic zone facts need to be defined and how they should be resolved.

Parameters:

  • model (SolarisZones)

    the model used to store data from the system



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/facter/util/solaris_zones.rb', line 69

def add_dynamic_facts
  model = self
  zone_hash.each_pair do |zone, attr_hsh|
    attr_hsh.keys.each do |attr|
      Facter.add("zone_#{zone}_#{attr}") do
        setcode do
          model.refresh if model.flushed?
          # Don't resolve if the zone has since been deleted
          if zone_hsh = model.zone_hash[zone]
            zone_hsh[attr] # the value
          end
        end
        on_flush do
          model.flush!
        end
      end
    end
  end
end

#countFixnum?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

count returns the number of running zones, including the global zone. This method is intended to be used from the setcode block of the ‘zones` fact.

Returns:

  • (Fixnum, nil)

    the number of running zones or nil if the number could not be determined.



131
132
133
134
135
# File 'lib/facter/util/solaris_zones.rb', line 131

def count
  if @zone_hash
    @zone_hash.size
  end
end

#flush!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

flush! purges the saved data from the zoneadm_cmd output



141
142
143
144
# File 'lib/facter/util/solaris_zones.rb', line 141

def flush!
  @zoneadm_output = nil
  @zone_hash = nil
end

#flushed?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

flushed? returns true if the instance has no parsed data accessible via the #zone_hash method.

Returns:

  • (Boolean)

    true if there is no parsed data, false otherwise



153
154
155
# File 'lib/facter/util/solaris_zones.rb', line 153

def flushed?
  !@zone_hash
end

#refreshHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

refresh executes the zoneadm_cmd and stores the output data.

Returns:

  • (Hash)

    the parsed output of the zoneadm command



95
96
97
98
# File 'lib/facter/util/solaris_zones.rb', line 95

def refresh
  @zoneadm_output = Facter::Core::Execution.execute(zoneadm_cmd, {:on_fail => nil})
  parse!
end