Class: Lambom::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/lambom/config.rb

Constant Summary collapse

CONFIG_DIR =
"/etc/riyic"
FILE =
"#{CONFIG_DIR}/lambom.conf"
UNIX_PATH_REGEX =
/^[\w\s.\/\-_+%]+$/i
URL_REGEX =
/^(http(s)?|ftp):\/\/(([A-Za-z0-9-]+\.)*([A-Za-z0-9-]+\.[A-Za-z0-9]+))+((\/?)(([A-Za-z0-9\._\-]+)(\/){0,1}[A-Za-z0-9.-\/]*)){0,1}/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lambom/config.rb', line 27

def initialize
  @server = nil
  @private_key_file = nil
  @json_file = nil
  @berksfile = nil
  @cached = false
  @environment = 'production'
  @loglevel = 'debug'
  @logdir = '/var/log/riyic'
  @logfile = "solo_#{Time.now.strftime("%Y%m%d%H%M%S")}"
  @download_tarball = nil
  @api_url = nil
end

Instance Attribute Details

#api_urlObject

Returns the value of attribute api_url.



12
13
14
# File 'lib/lambom/config.rb', line 12

def api_url
  @api_url
end

#berksfileObject

Returns the value of attribute berksfile.



12
13
14
# File 'lib/lambom/config.rb', line 12

def berksfile
  @berksfile
end

#cachedObject

Returns the value of attribute cached.



12
13
14
# File 'lib/lambom/config.rb', line 12

def cached
  @cached
end

#download_tarballObject

Returns the value of attribute download_tarball.



12
13
14
# File 'lib/lambom/config.rb', line 12

def download_tarball
  @download_tarball
end

#environmentObject

Returns the value of attribute environment.



12
13
14
# File 'lib/lambom/config.rb', line 12

def environment
  @environment
end

#json_fileObject

Returns the value of attribute json_file.



12
13
14
# File 'lib/lambom/config.rb', line 12

def json_file
  @json_file
end

#logdirObject (readonly)

Returns the value of attribute logdir.



24
25
26
# File 'lib/lambom/config.rb', line 24

def logdir
  @logdir
end

#logfileObject (readonly)

Returns the value of attribute logfile.



24
25
26
# File 'lib/lambom/config.rb', line 24

def logfile
  @logfile
end

#loglevelObject

Returns the value of attribute loglevel.



12
13
14
# File 'lib/lambom/config.rb', line 12

def loglevel
  @loglevel
end

#private_key_fileObject

Returns the value of attribute private_key_file.



12
13
14
# File 'lib/lambom/config.rb', line 12

def private_key_file
  @private_key_file
end

#serverObject

Returns the value of attribute server.



12
13
14
# File 'lib/lambom/config.rb', line 12

def server
  @server
end

Instance Method Details

#load(argv = {}) ⇒ Object



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
76
77
78
79
# File 'lib/lambom/config.rb', line 42

def load(argv={})

  h = {}

  # evitamos cascar si o ficheiro de configuracion non existe
  if File.exists?(FILE)

    begin
      h = YAML.load(IO::read(FILE))
    rescue Exception => e
      e.to_s =~ /line (\d+) column/
      raise "Error loading yaml config file #{FILE}, syntax error in line #{$1}"
    end

    puts "hash de opcions: #{h.inspect}" if $debug

    raise "Error in config file, YAML loaded is not a hash" unless h.class == Hash

    # convert keys from string to symbols
    h = h.each_with_object({}) {|(k,v),o| o[k.to_sym] = v}
  
  end

  # merge line command hash with config file loaded
  h.merge!(argv)
  
  # validate parametros
  h.each do |k,v|
    if validate(k,v)
      instance_variable_set "@#{k}".to_sym, v
    else
      raise "Invalid value '#{v}' to parameter '#{k}'"
    end
  end
  
  return self

end

#merge(options = {}) ⇒ Object



81
82
83
84
85
86
# File 'lib/lambom/config.rb', line 81

def merge(options={})
  options.each do |k,v|
    puts "mergeando #{k} con valor #{v} en config" if $debug
    self.send("#{k}=",v) if validate(k,v)
  end
end

#validate(parametro, valor) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/lambom/config.rb', line 89

def validate(parametro,valor)

  case parametro.to_sym
  when :server
    # uuid
    valor =~ /^[\d\w\-]{36}$/
  when :private_key_file
    #unix path
    valor =~ UNIX_PATH_REGEX && File.exists?(valor)
  when :environment
    #env de rails
    ["production","development","test"].include?(valor)
  when :loglevel
    #loglevel de chef
    %w{debug info}.include?(valor)
  when :berksfile
    valor =~ UNIX_PATH_REGEX && File.exists?(valor)
  when :json_file
    valor =~ UNIX_PATH_REGEX && File.exists?(valor)
  when :cached
    [true,false].include?(valor)
  when :api_url
    valor =~ URL_REGEX
  when :download_tarball
    valor =~ URL_REGEX
  else
    false
    #raise "Invalid parameter #{parametro}"
  end

end