Class: Hako::Container
- Inherits:
-
Object
- Object
- Hako::Container
- Defined in:
- lib/hako/container.rb
Direct Known Subclasses
Constant Summary collapse
- PROVIDERS_KEY =
'$providers'
Instance Attribute Summary collapse
-
#definition ⇒ Object
readonly
Returns the value of attribute definition.
Instance Method Summary collapse
- #default_config ⇒ Hash private
- #default_labels ⇒ Hash<String, String> private
- #env ⇒ Hash<String, String>
- #expand_env(env) ⇒ Hash<String, String> private
- #extra_hosts ⇒ Array<Hash>?
-
#health_check ⇒ Object
@return[Hash, nil].
-
#initialize(app, definition, dry_run:) ⇒ Container
constructor
A new instance of Container.
- #linux_parameters ⇒ Hash?
- #load_providers(provider_configs) ⇒ Array<EnvProvider> private
- #log_configuration ⇒ Hash?
- #mount_points ⇒ Array<Hash>
- #port_mappings ⇒ Array<Hash>
- #secrets ⇒ Array<Hash>?
- #ulimits ⇒ Array<Hash>?
- #volumes_from ⇒ Array<Hash>
Constructor Details
#initialize(app, definition, dry_run:) ⇒ Container
Returns a new instance of Container.
15 16 17 18 19 20 |
# File 'lib/hako/container.rb', line 15 def initialize(app, definition, dry_run:) @app = app @definition = default_config.merge(definition) @definition['docker_labels'].merge!(default_labels) @dry_run = dry_run end |
Instance Attribute Details
#definition ⇒ Object (readonly)
Returns the value of attribute definition.
10 11 12 |
# File 'lib/hako/container.rb', line 10 def definition @definition end |
Instance Method Details
#default_config ⇒ Hash (private)
220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/hako/container.rb', line 220 def default_config { 'cpu' => 0, 'env' => {}, 'docker_labels' => {}, 'links' => [], 'essential' => true, 'mount_points' => [], 'port_mappings' => [], 'volumes_from' => [], 'privileged' => false, } end |
#default_labels ⇒ Hash<String, String> (private)
235 236 237 238 239 |
# File 'lib/hako/container.rb', line 235 def default_labels { 'cc.wanko.hako.version' => VERSION, } end |
#env ⇒ Hash<String, String>
47 48 49 |
# File 'lib/hako/container.rb', line 47 def env @expanded_env ||= (@definition.fetch('env')) end |
#expand_env(env) ⇒ Hash<String, String> (private)
198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/hako/container.rb', line 198 def (env) env = env.dup provider_types = env.delete(PROVIDERS_KEY) || [] providers = load_providers(provider_types) = EnvExpander.new(providers) if @dry_run .validate!(env) env else .(env) end end |
#extra_hosts ⇒ Array<Hash>?
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/hako/container.rb', line 139 def extra_hosts if @definition.key?('extra_hosts') @definition['extra_hosts'].map do |extra_host| { hostname: extra_host.fetch('hostname'), ip_address: extra_host.fetch('ip_address'), } end end end |
#health_check ⇒ Object
@return[Hash, nil]
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/hako/container.rb', line 107 def health_check if @definition.key?('health_check') conf = @definition['health_check'] ret = { command: conf.fetch('command'), interval: conf.fetch('interval', 30), retries: conf.fetch('retries', 3), timeout: conf.fetch('timeout', 5), } if conf.key?('start_period') ret[:start_period] = conf.fetch('start_period') end ret end end |
#linux_parameters ⇒ Hash?
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/hako/container.rb', line 151 def linux_parameters if @definition.key?('linux_parameters') ret = {} conf = @definition['linux_parameters'] if conf.key?('capabilities') cap = conf['capabilities'] ret[:capabilities] = { add: cap.fetch('add', []), drop: cap.fetch('drop', []) } end if conf.key?('devices') ret[:devices] = conf['devices'].map do |d| { host_path: d.fetch('host_path'), container_path: d.fetch('container_path', nil), permissions: d.fetch('permissions', []) } end end ret[:init_process_enabled] = conf.fetch('init_process_enabled', nil) ret[:shared_memory_size] = conf.fetch('shared_memory_size', nil) if conf.key?('tmpfs') ret[:tmpfs] = conf['tmpfs'].map do |t| { container_path: t.fetch('container_path'), mount_options: t.fetch('mount_options', []), size: t.fetch('size') } end end ret end end |
#load_providers(provider_configs) ⇒ Array<EnvProvider> (private)
213 214 215 216 217 |
# File 'lib/hako/container.rb', line 213 def load_providers(provider_configs) provider_configs.map do |provider_config| Loader.new(Hako::EnvProviders, 'hako/env_providers').load(provider_config.fetch('type')).new(@app.root_path, provider_config) end end |
#log_configuration ⇒ Hash?
96 97 98 99 100 101 102 103 104 |
# File 'lib/hako/container.rb', line 96 def log_configuration if @definition.key?('log_configuration') conf = @definition['log_configuration'] { log_driver: conf.fetch('log_driver'), options: conf.fetch('options'), } end end |
#mount_points ⇒ Array<Hash>
75 76 77 78 79 80 81 82 83 |
# File 'lib/hako/container.rb', line 75 def mount_points @definition['mount_points'].map do |mount_point| { source_volume: mount_point.fetch('source_volume'), container_path: mount_point.fetch('container_path'), read_only: mount_point.fetch('read_only', false), } end end |
#port_mappings ⇒ Array<Hash>
64 65 66 67 68 69 70 71 72 |
# File 'lib/hako/container.rb', line 64 def port_mappings @definition['port_mappings'].map do |port_mapping| { container_port: port_mapping.fetch('container_port'), host_port: port_mapping.fetch('host_port'), protocol: port_mapping.fetch('protocol', 'tcp'), } end end |
#secrets ⇒ Array<Hash>?
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/hako/container.rb', line 52 def secrets if @definition['secrets'] @definition['secrets'].map do |secret| { name: secret.fetch('name'), value_from: secret.fetch('value_from'), } end end end |
#ulimits ⇒ Array<Hash>?
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/hako/container.rb', line 126 def ulimits if @definition.key?('ulimits') @definition['ulimits'].map do |ulimit| { name: ulimit.fetch('name'), soft_limit: ulimit.fetch('soft_limit'), hard_limit: ulimit.fetch('hard_limit'), } end end end |
#volumes_from ⇒ Array<Hash>
86 87 88 89 90 91 92 93 |
# File 'lib/hako/container.rb', line 86 def volumes_from @definition['volumes_from'].map do |volumes_from| { source_container: volumes_from.fetch('source_container'), read_only: volumes_from.fetch('read_only', false), } end end |