class GGConfig

Public Class Methods

new(options = {:conf_dir => "/conf", :conf_file => "gaddy_*.gcf"} ) click to toggle source
# File lib/gg_config/gg_config.rb, line 27
def initialize(options = {:conf_dir => "/conf", :conf_file => "gaddy_*.gcf"} )
  @conf_file = options[:conf_file]
  @conf_dir = File.expand_path(options[:conf_dir])
end

Public Instance Methods

config() click to toggle source
# File lib/gg_config/gg_config.rb, line 84
def config
  @config_content ||= read_config_file
end
config=(new_config) click to toggle source
# File lib/gg_config/gg_config.rb, line 88
def config= (new_config)
  @config_content = new_config.symbolize_keys!
end
config_file_name() click to toggle source

Get the config file name

# File lib/gg_config/gg_config.rb, line 63
def config_file_name
  Dir.glob(File.join(@conf_dir, @conf_file))[0]
end
device_id() click to toggle source
# File lib/gg_config/gg_config.rb, line 33
def device_id
  config[:device_id]
end
exist?() click to toggle source

Check if config file exist

# File lib/gg_config/gg_config.rb, line 68
def exist?
  config_file_name != nil
end
get_config_for(config_type, conf_name, extra_conf_file) click to toggle source

Generic method to get the initial config for a config_type, could be device_name or user_name, will also try to find all kind of configs files with device_name or user_name

# File lib/gg_config/gg_config.rb, line 111
def get_config_for(config_type, conf_name, extra_conf_file)
  config_value = nil
  begin
    if config
      logger.debug "Config exist and is #{config} looking for config type #{config_type}"
      config_value = config[config_type]
    end
  rescue Exception => e
    logger.info "Could not find a gaddygaddy config file, message is #{e.message}"
  end
  unless config_value
    files = Dir.glob(File.join(@conf_dir, 'config.*'))
    files << File.join(@conf_dir, extra_conf_file) if File.exist? File.join(@conf_dir, extra_conf_file)
    files.each do |conf_file_name|
      logger.debug "The file #{conf_file_name} has file size #{File.size(conf_file_name)}"
      if File.size(conf_file_name).to_i < 1000
        conf_file = File.open(conf_file_name,"r")
        lines = conf_file.readlines
        # Check if there are not a lot of lines in the config
        if lines.size < 4
          lines.each do |line|
            if line.index("=")
              args = line.split("=")
              # Validate that the syntax of the property is ok
              if args.size == 2
                config_value = args[1].strip if args[0].strip == conf_name || args[0].strip == "config_value"
              else
                logger.info "The file #{conf_file_name} has too many = in the line #{line}"
              end
            else
              # Validate that there are no spaces in the config_value
              config_value = line.strip
            end
          end
        else
          logger.info "The file #{conf_file_name} is not a config file as it's too many lines"
        end
      else
        logger.info "Will not check file #{conf_file_name} as it is to big"
      end
    end
  end
  # Check that we have found a config_value and that the config_value is a valid host name, if not raise a exception with topic info to be able to help the user further
  unless config_value
    raise JNoGaddyGaddyConfigFile.new({:message => "Could not found any file with config in the config dir #{@conf_dir}",
                                       :topic => TPC_NO_CONFIG_FILE,
                                       :extra_info => {:conf_dir => @conf_dir}
                                      })
  end
  config_value
end
get_device_name() click to toggle source
# File lib/gg_config/gg_config.rb, line 37
def get_device_name
  device_name = get_config_for(:device_name,"host","device")
  unless valid_host_name?(device_name)
    raise JException.new({:message => "The host name #{device_name} is not a valid host name",
                          :status => ERR_NO_VALID_HOSTNAME,
                          :extra_info => {:host_name => device_name}
                         })
  end
  device_name
end
read_config_file() click to toggle source
# File lib/gg_config/gg_config.rb, line 72
def read_config_file
  begin
    config_file = File.open(config_file_name)
    file_content = config_file.read
    config = JSON.parse(file_content)
    logger.debug "Have read config file #{config_file_name} and found content #{config.to_s}"
  rescue Exception => e
    raise JNoGaddyGaddyConfigFile.new({:message => e.message})
  end
  config.symbolize_keys!
end
save() click to toggle source
# File lib/gg_config/gg_config.rb, line 96
def save
  unless valid_config?
    raise JCouldNotSaveConfigFileException.new({:message => "Missing configuration information, could not save config file for #{config.inspect}"})
  end
  begin
    config_file = File.open(config_file_name,"w")
    config_file.write config.to_json.to_s
  rescue Exception => e
    raise JCouldNotSaveConfigFileException.new({:message => e.message})
  end
end
token() click to toggle source
# File lib/gg_config/gg_config.rb, line 48
def token
  config[:token]
end
user_email() click to toggle source
# File lib/gg_config/gg_config.rb, line 52
def user_email
  get_config_for(:user_email,"user","user")
end
user_id_salt() click to toggle source
# File lib/gg_config/gg_config.rb, line 56
def user_id_salt
  get_config_for(:user_id_salt,"user","user_id_salt")
end
valid_config?() click to toggle source
# File lib/gg_config/gg_config.rb, line 163
def valid_config?
  config[:user_id_salt] && config[:token] && config[:device_name]
end
valid_host_name?(host_name) click to toggle source
# File lib/gg_config/gg_config.rb, line 92
def valid_host_name?(host_name)
  host_name.size <= 63 and not (host_name.rindex('-', 0) or host_name.index('-', -1) or host_name.scan(/[^a-z\d-]/i).any?)
end