Class: Baha::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/baha/config.rb

Constant Summary collapse

DEFAULTS =
{
  :parent => 'ubuntu:14.04.1',
  :bind   => '/.baha',
  :command => ['/bin/bash','./init.sh'],
  :repository => nil,
  :maintainer => nil,
  :timeout => 1200
}
LOG =
Baha::Log.for_name("Config")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Config

Returns a new instance of Config.

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/baha/config.rb', line 30

def initialize(config)
  @config = config
  
  # Defaults
  defaults = config['defaults'] || {}
  raise ArgumentError.new("Expected Hash for defaults") unless defaults.is_a?(Hash)
  @defaults = {}
  DEFAULTS.keys.each do |k|
    @defaults[k] = defaults[k] || defaults[k.to_s] || DEFAULTS[k]
  end

  @configdir = Pathname.new(config['configdir'] || Pathname.pwd)
  @workspace = Pathname.new(config['workspace'] || @configdir + 'workspace')
  @secure = false
  @options = {}
  init_security if ENV.has_key?('DOCKER_CERT_PATH') || config.has_key?('ssl')
end

Instance Attribute Details

#configdirObject (readonly)

Returns the value of attribute configdir.



27
28
29
# File 'lib/baha/config.rb', line 27

def configdir
  @configdir
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



28
29
30
# File 'lib/baha/config.rb', line 28

def defaults
  @defaults
end

#optionsObject (readonly)

Returns the value of attribute options.



27
28
29
# File 'lib/baha/config.rb', line 27

def options
  @options
end

#secureObject (readonly)

Returns the value of attribute secure.



27
28
29
# File 'lib/baha/config.rb', line 27

def secure
  @secure
end

#workspaceObject (readonly)

Returns the value of attribute workspace.



27
28
29
# File 'lib/baha/config.rb', line 27

def workspace
  @workspace
end

Class Method Details

.load(file) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
# File 'lib/baha/config.rb', line 17

def load(file)
  LOG.debug { "Loading file #{file}"}
  filepath = Pathname.new(file)
  raise ArgumentError.new("Cannot read config file #{file}") unless filepath.readable?
  config = YAML.load_file(filepath)
  config['configdir'] ||= filepath.dirname
  Baha::Config.new(config)
end

Instance Method Details

#each_imageObject



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
104
# File 'lib/baha/config.rb', line 75

def each_image
  return unless @config.has_key?('images')
  @config['images'].each do |image|
    if image.has_key?('include')
      path = Pathname.new(image['include'])
      file = resolve_file(path)
      if file
        yml = YAML.load_file(file)
        yield Baha::Image.new(self,yml)
      else
        LOG.error { "Unable to find image include: #{path}"}
        next
      end
    elsif image.has_key?('dockerfile')
      path = Pathname.new(image['dockerfile'])
      file = resolve_file(path)
      if file
        dockerfile = Baha::Dockerfile.parse(file)
        dockerfile['name'] = image['name']
        dockerfile['tag'] = image['tag']
        yield Baha::Image.new(self,dockerfile)
      else
        LOG.error { "Unable to find dockerfile: #{path}"}
        next
      end
    else
      yield Baha::Image.new(self,image)
    end
  end
end

#init_docker!Object

Initialize Docker Client



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/baha/config.rb', line 129

def init_docker!
  Docker.options = @options
  if @config.has_key?('docker_url')
    url = @config['docker_url']
    Docker.url = url
  end
  if @secure
    Docker.url = Docker.url.gsub(/^tcp:/,'https:')
  end
  LOG.debug { "Docker URL: #{Docker.url}"}
  LOG.debug { "Docker Options: #{Docker.options.inspect}"}
  Docker.validate_version!
end

#init_securityObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/baha/config.rb', line 48

def init_security
  @secure = true
  cert_path = ''
  ssl_options = { }
  if ENV['DOCKER_CERT_PATH']
    cert_path = Pathname.new(ENV['DOCKER_CERT_PATH'])
    ssl_options[:ssl_verify_peer] = (ENV['DOCKER_TLS_VERIFY'] == '1')
    ssl_options[:client_cert] = (cert_path + 'cert.pem').expand_path.to_s
    ssl_options[:client_key] = (cert_path + 'key.pem').expand_path.to_s
    ssl_options[:ssl_ca_file] = (cert_path + 'ca.pem').expand_path.to_s
  elsif @config.has_key?('ssl')
    ssl = @config['ssl']
    ssl_options[:ssl_verify_peer] = ssl['verify'] || false
    if ssl.has_key?('cert_path')
      cert_path = Pathname.new(ssl['cert_path'])
      ssl_options[:client_cert] = (cert_path + 'cert.pem').expand_path.to_s
      ssl_options[:client_key] = (cert_path + 'key.pem').expand_path.to_s
      ssl_options[:ssl_ca_file] = (cert_path + 'ca.pem').expand_path.to_s
    else
      ssl_options[:client_cert] = ssl['cert']
      ssl_options[:client_key] = ssl['key']
      ssl_options[:ssl_ca_file] = ssl['ca']
    end
  end
  @options.merge!(ssl_options)
end

#inspectObject



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/baha/config.rb', line 143

def inspect
  <<-eos.gsub(/\n?\s{2,}/,'')
  #{self.class.name}<
    @config=#{@config.inspect},
    @configdir=#{@configdir},
    @workspace=#{@workspace},
    @defaults=#{@defaults.inspect},
    @secure=#{@secure},
    @options=#{@options.inspect}
  >
  eos
end

#resolve_file(file) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/baha/config.rb', line 106

def resolve_file(file)
  filepath = Pathname.new(file)
  LOG.debug { "resolve_file(#{file})" }
  paths = [ 
    filepath,            # 0. Absolute path
    @workspace + file,   # 1. Workspace
    @configdir + file,   # 2. Config
    Pathname.pwd + file  # 3. Current directory
  ]
  paths.reduce(nil) do |result,path|
    if result.nil?
      if path.exist?
        result = path
        LOG.debug("found file at: #{path}")
      else
        LOG.debug("did not find file at: #{path}")
      end
    end  
    result
  end
end