Class: ConfCtl::Machine

Inherits:
Object
  • Object
show all
Defined in:
lib/confctl/machine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Machine

Returns a new instance of Machine.

Parameters:

  • opts (Hash)


6
7
8
9
10
11
12
# File 'lib/confctl/machine.rb', line 6

def initialize(opts)
  @opts = opts
  @name = opts['name']
  @safe_name = opts['name'].gsub('/', ':')
  @managed = opts['managed']
  @spin = opts['spin']
end

Instance Attribute Details

#managedObject (readonly)

Returns the value of attribute managed.



3
4
5
# File 'lib/confctl/machine.rb', line 3

def managed
  @managed
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/confctl/machine.rb', line 3

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



3
4
5
# File 'lib/confctl/machine.rb', line 3

def opts
  @opts
end

#safe_nameObject (readonly)

Returns the value of attribute safe_name.



3
4
5
# File 'lib/confctl/machine.rb', line 3

def safe_name
  @safe_name
end

#spinObject (readonly)

Returns the value of attribute spin.



3
4
5
# File 'lib/confctl/machine.rb', line 3

def spin
  @spin
end

Instance Method Details

#[](key) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/confctl/machine.rb', line 77

def [](key)
  if key.index('.')
    get(opts, key.split('.'))
  elsif key == 'checks'
    health_checks.length
  else
    opts[key]
  end
end

#health_checksObject



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
# File 'lib/confctl/machine.rb', line 31

def health_checks
  return @health_checks if @health_checks

  @health_checks = []

  opts['healthChecks'].each do |type, checks|
    case type
    when 'systemd'
      next if !checks['enable'] || spin != 'nixos'

      if checks['systemProperties'].any?
        @health_checks << HealthChecks::Systemd::Properties.new(
          self,
          property_checks: checks['systemProperties'].map do |v|
            HealthChecks::Systemd::PropertyCheck.new(v)
          end
        )
      end

      checks['unitProperties'].each do |unit_name, prop_checks|
        health_checks << HealthChecks::Systemd::Properties.new(
          self,
          pattern: unit_name,
          property_checks: prop_checks.map do |v|
            HealthChecks::Systemd::PropertyCheck.new(v)
          end
        )
      end

    when 'builderCommands', 'machineCommands'
      checks.each do |cmd|
        health_checks << HealthChecks::RunCommand.new(
          self,
          HealthChecks::RunCommand::Command.new(self, cmd),
          remote: type == 'machineCommands'
        )
      end

    else
      raise "Unsupported health-check type #{type.inspect}"
    end
  end

  @health_checks
end

#localhost?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/confctl/machine.rb', line 18

def localhost?
  target_host == 'localhost'
end

#nix_pathsObject



22
23
24
25
26
27
28
29
# File 'lib/confctl/machine.rb', line 22

def nix_paths
  opts['nix']['nixPath'].to_h do |v|
    eq = v.index('=')
    raise "'#{v}' is not a valid nix path entry " if eq.nil?

    [v[0..eq - 1], v[eq + 1..]]
  end
end

#target_hostObject



14
15
16
# File 'lib/confctl/machine.rb', line 14

def target_host
  (opts['host'] && opts['host']['target']) || name
end

#to_sObject



87
88
89
# File 'lib/confctl/machine.rb', line 87

def to_s
  name
end