Class: ConfCtl::Swpins::ClusterName

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine, channels) ⇒ ClusterName

Returns a new instance of ClusterName.

Parameters:



23
24
25
26
27
28
29
30
31
# File 'lib/confctl/swpins/cluster_name.rb', line 23

def initialize(machine, channels)
  @machine = machine
  @name = machine.name
  @path = File.join(ConfCtl::Swpins.cluster_dir, "#{name.gsub('/', ':')}.json")
  @channels = channels.select do |c|
    machine['swpins']['channels'].include?(c.name)
  end
  @nix_specs = machine['swpins']['pins']
end

Instance Attribute Details

#channelsArray<Swpins::Channel> (readonly)

Returns:



19
20
21
# File 'lib/confctl/swpins/cluster_name.rb', line 19

def channels
  @channels
end

#machineMachine (readonly)

Returns:



7
8
9
# File 'lib/confctl/swpins/cluster_name.rb', line 7

def machine
  @machine
end

#nameString (readonly)

Returns:

  • (String)


10
11
12
# File 'lib/confctl/swpins/cluster_name.rb', line 10

def name
  @name
end

#pathString (readonly)

Returns:

  • (String)


13
14
15
# File 'lib/confctl/swpins/cluster_name.rb', line 13

def path
  @path
end

#specsHash<String, Swpins::Specs::Base> (readonly)

Returns:



16
17
18
# File 'lib/confctl/swpins/cluster_name.rb', line 16

def specs
  @specs
end

Instance Method Details

#parseObject



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
# File 'lib/confctl/swpins/cluster_name.rb', line 33

def parse
  @specs = {}

  # Add specs from channels
  channels.each do |chan|
    chan.specs.each do |name, chan_spec|
      s = chan_spec.clone
      s.channel = chan.name
      specs[name] = s
    end
  end

  # Add machine-specific specs
  @json_specs = if File.exist?(path)
                  JSON.parse(File.read(path))
                else
                  {}
                end

  nix_specs.each do |name, nix_opts|
    specs[name] = Swpins::Spec.for(nix_opts['type'].to_sym).new(
      name,
      nix_opts[nix_opts['type']],
      json_specs[name]
    )
  end
end

#saveObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/confctl/swpins/cluster_name.rb', line 65

def save
  custom = {}
  specs.each do |name, s|
    custom[name] = s unless s.from_channel?
  end

  if custom.empty?
    begin
      File.unlink(path)
    rescue Errno::ENOENT
      # ignore
    end
  else
    tmp = "#{path}.new"

    FileUtils.mkdir_p(ConfCtl::Swpins.cluster_dir)

    File.open(tmp, 'w') do |f|
      f.puts(JSON.pretty_generate(custom))
    end

    File.rename(tmp, path)
  end
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  specs.values.all?(&:valid?)
end