class Freighter::Parser

Attributes

config[R]

Public Class Methods

new(config_path) click to toggle source
# File lib/freighter/parser.rb, line 7
def initialize(config_path)
  begin
    @config = opts.config = YAML.load_file(config_path)
    LOGGER.debug "config file parsed"
  rescue Errno::ENOENT, Psych::SyntaxError => e
    LOGGER.error "Error parsing freighter config file.\n  path: #{config_path}\n  #{e}"
  rescue
    LOGGER.error "There is something wrong with the path to your yaml config file: #{config_path}\n  #{$!.message}"
  end
  
  # Do some basic checking to make sure the config file has what we need
  %w[environments connection/type].each { |option| test_config_option option }
  set_defaults
end

Public Instance Methods

environment() click to toggle source
# File lib/freighter/parser.rb, line 58
def environment
  begin
    config.fetch('environments').fetch(opts.environment)
  rescue KeyError => e
    LOGGER.error "Error fetching environment: #{e.message}"
  end
end
images(host) click to toggle source
# File lib/freighter/parser.rb, line 43
def images(host)
  host_config = environment.fetch('hosts').detect { |h| h.fetch('host') == host }
  host_images = host_config.fetch('images')
  raise "app(s) to deploy not specified" unless opts.deploy_all or opts.app_name
  if opts.deploy_all
    host_images
  else
    host_images.select do |host_image|
      !host_image.fetch('containers').detect do |container|
        container['name'] == opts.app_name
      end.nil?
    end
  end
end
opts() click to toggle source
# File lib/freighter/parser.rb, line 22
def opts
  OPTIONS
end
test_config_option(option, opt_array=[], context=nil) click to toggle source

recursively tests for keys in a nested hash by separating nested keys with '/'

# File lib/freighter/parser.rb, line 27
def test_config_option(option, opt_array=[], context=nil)
  opts_2_test = option.split('/')
  opts_2_test.each_with_index do |opt, i|
    opt_array << opt
    context ||= opts.config
    begin
      if next_opt = opts_2_test[i+1]
        new_context = context.fetch(opt)
        test_config_option(next_opt, opt_array.clone, new_context.clone)
      end
    rescue KeyError
      LOGGER.config_error opt_array.join('/')
    end
  end
end

Private Instance Methods

set_defaults() click to toggle source
# File lib/freighter/parser.rb, line 68
def set_defaults
  opts.config.tap do |conf|
    conf['connection']['docker'] ||= {}
    conf['connection']['docker']['socket'] ||= 'unix:///var/run/docker.sock'
    conf['connection']['docker']['port']   ||= nil
  end
  opts.deploy_all ||= false
  opts.pull_image = true if opts.pull_image.nil?
end