Class: Freighter::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/freighter/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path) ⇒ Parser

Returns a new instance of Parser.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/freighter/parser.rb', line 5

def config
  @config
end

Instance Method Details

#environmentObject



58
59
60
61
62
63
64
# 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) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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

#optsObject



22
23
24
# File 'lib/freighter/parser.rb', line 22

def opts
  OPTIONS
end

#test_config_option(option, opt_array = [], context = nil) ⇒ Object

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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