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)


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

def initialize(config)
  @config = config
  config_workspace

  # 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]
  end
  @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.



31
32
33
# File 'lib/baha/config.rb', line 31

def configdir
  @configdir
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



32
33
34
# File 'lib/baha/config.rb', line 32

def defaults
  @defaults
end

#optionsObject (readonly)

Returns the value of attribute options.



31
32
33
# File 'lib/baha/config.rb', line 31

def options
  @options
end

#secureObject (readonly)

Returns the value of attribute secure.



31
32
33
# File 'lib/baha/config.rb', line 31

def secure
  @secure
end

#workspaceObject (readonly)

Returns the value of attribute workspace.



31
32
33
# File 'lib/baha/config.rb', line 31

def workspace
  @workspace
end

Class Method Details

.load(file) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
# File 'lib/baha/config.rb', line 20

def load(file)
  LOG.debug { "Loading file #{file}"}
  filepath = Pathname.new(file)
  LOG.debug { "Loading file #{filepath.expand_path}"}
  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



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

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



112
113
114
115
116
117
118
# File 'lib/baha/config.rb', line 112

def init_docker!
  Docker.options = @options
  set_docker_url
  LOG.debug { "Docker URL: #{Docker.url}"}
  LOG.debug { "Docker Options: #{Docker.options.inspect}"}
  Docker.validate_version!
end

#inspectObject



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/baha/config.rb', line 120

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

#nonnil(*args) ⇒ Object



136
137
138
# File 'lib/baha/config.rb', line 136

def nonnil(*args)
  args.find{ |x| not x.nil? }
end

#resolve_file(file) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/baha/config.rb', line 89

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

#workspace_for(image) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/baha/config.rb', line 81

def workspace_for(image)
  if @ws_mount
    @ws_mount + image
  else
    @workspace + image
  end
end