Class: Baha::Config
- Inherits:
-
Object
- Object
- Baha::Config
- 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
-
#configdir ⇒ Object
readonly
Returns the value of attribute configdir.
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#secure ⇒ Object
readonly
Returns the value of attribute secure.
-
#workspace ⇒ Object
readonly
Returns the value of attribute workspace.
Class Method Summary collapse
Instance Method Summary collapse
- #each_image ⇒ Object
-
#init_docker! ⇒ Object
Initialize Docker Client.
- #init_security ⇒ Object
-
#initialize(config) ⇒ Config
constructor
A new instance of Config.
- #inspect ⇒ Object
- #resolve_file(file) ⇒ Object
Constructor Details
#initialize(config) ⇒ Config
Returns a new instance of Config.
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
#configdir ⇒ Object (readonly)
Returns the value of attribute configdir.
27 28 29 |
# File 'lib/baha/config.rb', line 27 def configdir @configdir end |
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
28 29 30 |
# File 'lib/baha/config.rb', line 28 def defaults @defaults end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
27 28 29 |
# File 'lib/baha/config.rb', line 27 def @options end |
#secure ⇒ Object (readonly)
Returns the value of attribute secure.
27 28 29 |
# File 'lib/baha/config.rb', line 27 def secure @secure end |
#workspace ⇒ Object (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
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_image ⇒ Object
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 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..inspect}"} Docker.validate_version! end |
#init_security ⇒ Object
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 = '' = { } if ENV['DOCKER_CERT_PATH'] cert_path = Pathname.new(ENV['DOCKER_CERT_PATH']) [:ssl_verify_peer] = (ENV['DOCKER_TLS_VERIFY'] == '1') [:client_cert] = (cert_path + 'cert.pem')..to_s [:client_key] = (cert_path + 'key.pem')..to_s [:ssl_ca_file] = (cert_path + 'ca.pem')..to_s elsif @config.has_key?('ssl') ssl = @config['ssl'] [:ssl_verify_peer] = ssl['verify'] || false if ssl.has_key?('cert_path') cert_path = Pathname.new(ssl['cert_path']) [:client_cert] = (cert_path + 'cert.pem')..to_s [:client_key] = (cert_path + 'key.pem')..to_s [:ssl_ca_file] = (cert_path + 'ca.pem')..to_s else [:client_cert] = ssl['cert'] [:client_key] = ssl['key'] [:ssl_ca_file] = ssl['ca'] end end @options.merge!() end |
#inspect ⇒ Object
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 |