Class: HDeploy::Conf

Inherits:
Object
  • Object
show all
Defined in:
lib/hdeploy/conf.rb

Constant Summary collapse

@@instance =
nil
@@default_values =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Conf

Returns a new instance of Conf.



13
14
15
16
# File 'lib/hdeploy/conf.rb', line 13

def initialize(file)
  @file = file
  reload
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



11
12
13
# File 'lib/hdeploy/conf.rb', line 11

def file
  @file
end

Class Method Details

.instance(path = '/opt/hdeploy/etc/hdeploy.conf.json') ⇒ Object

FIXME: find a good way to set default path



19
20
21
# File 'lib/hdeploy/conf.rb', line 19

def self.instance(path = '/opt/hdeploy/etc/hdeploy.conf.json')
  @@instance ||= new(path)
end

Instance Method Details

#[](k) ⇒ Object




39
40
41
# File 'lib/hdeploy/conf.rb', line 39

def [](k)
  @conf[k] 
end

#add_defaults(h) ⇒ Object




44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hdeploy/conf.rb', line 44

def add_defaults(h)
  # This is pretty crappy code in that it loads stuff twice etc. But that way no re-implementing a variation of deep_merge for default stuff...
  @@default_values << h.__deep_clone__

  rebuild_conf = {}
  @@default_values.each do |defval|
    rebuild_conf.deep_merge!(defval)
  end

  @conf = rebuild_conf.deep_merge!(@conf)
end

#reloadObject




25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hdeploy/conf.rb', line 25

def reload
  raise "unable to find conf file #{@file}" unless File.exists? @file
  
  st = File.stat(@file)
  raise "config file #{@file} must not be a symlink" if File.symlink?(@file)
  raise "config file #{@file} must be a regular file" unless st.file?
  raise "config file #{@file} must have uid 0" unless st.uid == 0 or Process.uid != 0
  raise "config file #{@file} must not allow group/others to write" unless sprintf("%o", st.mode) =~ /^100[46][04][04]/
  
  # Seems we have checked everything. Woohoo!
  @conf = JSON.parse(File.read(@file))
end