Class: ConfCtl::Machine

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

Defined Under Namespace

Classes: CarriedMachine

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, machine_list:) ⇒ Machine

Returns a new instance of Machine.

Parameters:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/confctl/machine.rb', line 45

def initialize(opts, machine_list:)
  @meta = opts['metaConfig']
  @name = opts['name']
  @safe_name = name.gsub('/', ':')
  @managed = meta['managed']
  @spin = meta['spin']
  @is_carrier = meta.fetch('carrier', {}).fetch('enable', false)
  @carrier_name = opts['carrier']
  @cluster_name = opts['clusterName']
  @safe_cluster_name = cluster_name.gsub('/', ':')
  @carried_alias = opts['alias'] || @cluster_name
  @safe_carried_alias = @carried_alias.gsub('/', ':')
  @machine_list = machine_list
end

Instance Attribute Details

#carried_aliasString (readonly)

Alias for this machine on the carrier

Returns:

  • (String)


34
35
36
# File 'lib/confctl/machine.rb', line 34

def carried_alias
  @carried_alias
end

#carrier_nameString (readonly)

Returns:

  • (String)


24
25
26
# File 'lib/confctl/machine.rb', line 24

def carrier_name
  @carrier_name
end

#cluster_nameString (readonly)

Returns:

  • (String)


27
28
29
# File 'lib/confctl/machine.rb', line 27

def cluster_name
  @cluster_name
end

#managedBoolean (readonly)

Returns:

  • (Boolean)


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

def managed
  @managed
end

#metaHash (readonly)

Returns machine metadata.

Returns:

  • (Hash)

    machine metadata



41
42
43
# File 'lib/confctl/machine.rb', line 41

def meta
  @meta
end

#nameString (readonly)

Returns:

  • (String)


12
13
14
# File 'lib/confctl/machine.rb', line 12

def name
  @name
end

#safe_carried_aliasString (readonly)

Alias for this machine on the carrier

Returns:

  • (String)


38
39
40
# File 'lib/confctl/machine.rb', line 38

def safe_carried_alias
  @safe_carried_alias
end

#safe_cluster_nameString (readonly)

Returns:

  • (String)


30
31
32
# File 'lib/confctl/machine.rb', line 30

def safe_cluster_name
  @safe_cluster_name
end

#safe_nameString (readonly)

Returns:

  • (String)


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

def safe_name
  @safe_name
end

#spinString (readonly)

Returns:

  • (String)


21
22
23
# File 'lib/confctl/machine.rb', line 21

def spin
  @spin
end

Instance Method Details

#[](key) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/confctl/machine.rb', line 165

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

#carried?Boolean

True if this machine is on a carrier

Returns:

  • (Boolean)


78
79
80
# File 'lib/confctl/machine.rb', line 78

def carried?
  !@carrier_name.nil?
end

#carried_machinesArray<CarriedMachine>

Returns:



66
67
68
69
70
71
72
73
74
75
# File 'lib/confctl/machine.rb', line 66

def carried_machines
  meta.fetch('carrier', {}).fetch('machines', []).map do |m|
    CarriedMachine.new(
      carrier: self,
      name: m['machine'],
      alias: m['alias'] || m['machine'],
      attribute: m['attribute']
    )
  end
end

#carrier?Boolean

True if this machine carries other machines

Returns:

  • (Boolean)


61
62
63
# File 'lib/confctl/machine.rb', line 61

def carrier?
  @is_carrier
end

#carrier_machineMachine

Returns carrier.

Returns:



83
84
85
86
87
88
89
90
91
# File 'lib/confctl/machine.rb', line 83

def carrier_machine
  carrier = @machine_list[@carrier_name]

  if carrier.nil?
    raise "Carrier #{@carrier_name} not found in machine list"
  end

  carrier
end

#health_checksObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/confctl/machine.rb', line 119

def health_checks
  return @health_checks if @health_checks

  @health_checks = []

  meta['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)


97
98
99
# File 'lib/confctl/machine.rb', line 97

def localhost?
  target_host == 'localhost'
end

#nix_pathsObject



110
111
112
113
114
115
116
117
# File 'lib/confctl/machine.rb', line 110

def nix_paths
  meta['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

#profileString

Returns path to nix-env managed profile.

Returns:

  • (String)

    path to nix-env managed profile



102
103
104
105
106
107
108
# File 'lib/confctl/machine.rb', line 102

def profile
  if carried?
    "/nix/var/nix/profiles/confctl-#{safe_carried_alias}"
  else
    '/nix/var/nix/profiles/system'
  end
end

#target_hostObject



93
94
95
# File 'lib/confctl/machine.rb', line 93

def target_host
  meta.fetch('host', {}).fetch('target', name)
end

#to_sObject



177
178
179
# File 'lib/confctl/machine.rb', line 177

def to_s
  name
end