Class: ConfCtl::Swpins::ClusterName
- Inherits:
-
Object
- Object
- ConfCtl::Swpins::ClusterName
- Defined in:
- lib/confctl/swpins/cluster_name.rb
Instance Attribute Summary collapse
- #channels ⇒ Array<Swpins::Channel> readonly
- #machine ⇒ Machine readonly
- #name ⇒ String readonly
- #path ⇒ String readonly
- #specs ⇒ Hash<String, Swpins::Specs::Base> readonly
Instance Method Summary collapse
-
#initialize(machine, channels) ⇒ ClusterName
constructor
A new instance of ClusterName.
- #parse ⇒ Object
- #save ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(machine, channels) ⇒ ClusterName
Returns a new instance of ClusterName.
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
#channels ⇒ Array<Swpins::Channel> (readonly)
19 20 21 |
# File 'lib/confctl/swpins/cluster_name.rb', line 19 def channels @channels end |
#machine ⇒ Machine (readonly)
7 8 9 |
# File 'lib/confctl/swpins/cluster_name.rb', line 7 def machine @machine end |
#name ⇒ String (readonly)
10 11 12 |
# File 'lib/confctl/swpins/cluster_name.rb', line 10 def name @name end |
#path ⇒ String (readonly)
13 14 15 |
# File 'lib/confctl/swpins/cluster_name.rb', line 13 def path @path end |
#specs ⇒ Hash<String, Swpins::Specs::Base> (readonly)
16 17 18 |
# File 'lib/confctl/swpins/cluster_name.rb', line 16 def specs @specs end |
Instance Method Details
#parse ⇒ Object
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 |
#save ⇒ Object
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
61 62 63 |
# File 'lib/confctl/swpins/cluster_name.rb', line 61 def valid? specs.values.all?(&:valid?) end |