class BoxGrinder::Config

Public Class Methods

new(values = {}) click to toggle source
# File lib/boxgrinder-core/models/config.rb, line 26
def initialize(values = {})
  super({})

  merge!(
      :file => ENV['BG_CONFIG_FILE'] || "#{ENV['HOME']}/.boxgrinder/config",
      :name => 'BoxGrinder Build',
      :platform => :none,
      :delivery => :none,
      :force => false,
      :log_level => :info,
      :backtrace => false,
      :dir => {
          :root => Dir.pwd,
          :build => 'build',
          :cache => '/var/cache/boxgrinder', # required for appliance-creator
          :tmp => '/tmp'
      },
      :os_config => {},
      :platform_config => {},
      :delivery_config => {},
      :additional_plugins => []
  )

  if ENV['BG_CONFIG_FILE']
    raise "You specified empty configuration file path. Please make sure you set correct path for BG_CONFIG_FILE environment variable." if ENV['BG_CONFIG_FILE'].strip.empty?
    raise "Configuration file '#{ENV['BG_CONFIG_FILE']}' couldn't be found. Please make sure you set correct path for BG_CONFIG_FILE environment variable." unless File.exists?(ENV['BG_CONFIG_FILE'])
  end

  deep_merge!(YAML.load_file(self.file)) if File.exists?(self.file)
  merge_with_symbols!(values)

  self.backtrace = true if [:debug, :trace].include?(self.log_level)

  populate_user_ids!
end

Public Instance Methods

deep_merge(first, second) click to toggle source
# File lib/boxgrinder-core/models/config.rb, line 70
def deep_merge(first, second)
  second.each_key do |k|
    if first[k.to_sym].is_a?(Hash) and second[k].is_a?(Hash)
      deep_merge(first[k.to_sym], second[k])
    else
      first[k.to_sym] = second[k]
    end
  end if second
end
deep_merge!(h) click to toggle source
# File lib/boxgrinder-core/models/config.rb, line 66
def deep_merge!(h)
  deep_merge(self, h)
end
merge_with_symbols!(values) click to toggle source
# File lib/boxgrinder-core/models/config.rb, line 62
def merge_with_symbols!(values)
  merge!(values.inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo })
end
populate_user_ids!() click to toggle source
# File lib/boxgrinder-core/models/config.rb, line 80
def populate_user_ids!
  self.uid = Process.uid
  self.gid = Process.gid
  begin
    if env = ENV['SUDO_USER'] || ENV['LOGNAME']
      user = Etc.getpwnam(env)
      self.uid = user.uid
      self.gid = user.gid
    end
  rescue ArgumentError #No such name, just use initial defaults.
  end
end