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.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lambom/config.rb', line 22

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("%F_%T")}"
    @download_tarball = nil
    @api_url = nil
end

Instance Attribute Details

#api_urlObject

Returns the value of attribute api_url.



10
11
12
# File 'lib/lambom/config.rb', line 10

def api_url
  @api_url
end

#berksfileObject

Returns the value of attribute berksfile.



10
11
12
# File 'lib/lambom/config.rb', line 10

def berksfile
  @berksfile
end

#cachedObject

Returns the value of attribute cached.



10
11
12
# File 'lib/lambom/config.rb', line 10

def cached
  @cached
end

#download_tarballObject

Returns the value of attribute download_tarball.



10
11
12
# File 'lib/lambom/config.rb', line 10

def download_tarball
  @download_tarball
end

#environmentObject

Returns the value of attribute environment.



10
11
12
# File 'lib/lambom/config.rb', line 10

def environment
  @environment
end

#json_fileObject

Returns the value of attribute json_file.



10
11
12
# File 'lib/lambom/config.rb', line 10

def json_file
  @json_file
end

#logdirObject (readonly)

Returns the value of attribute logdir.



20
21
22
# File 'lib/lambom/config.rb', line 20

def logdir
  @logdir
end

#logfileObject (readonly)

Returns the value of attribute logfile.



20
21
22
# File 'lib/lambom/config.rb', line 20

def logfile
  @logfile
end

#loglevelObject

Returns the value of attribute loglevel.



10
11
12
# File 'lib/lambom/config.rb', line 10

def loglevel
  @loglevel
end

#private_key_fileObject

Returns the value of attribute private_key_file.



10
11
12
# File 'lib/lambom/config.rb', line 10

def private_key_file
  @private_key_file
end

#serverObject

Returns the value of attribute server.



10
11
12
# File 'lib/lambom/config.rb', line 10

def server
  @server
end

Instance Method Details

#loadObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lambom/config.rb', line 36

def load
    # evitamos cascar si o ficheiro de configuracion non existe
    return self unless File.exists?(FILE)

    h = {}
    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

    # evitamos cargar as opcions si non e un hash
    if h.class == Hash 
        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
    end
    
    self

end

#merge(options) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/lambom/config.rb', line 65

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)
        #instance_variable_set "@#{k}".to_sym, v if validate(k,v)
    end
end

#validate(parametro, valor) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/lambom/config.rb', line 73

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