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
- #depends_on ⇒ Object
- #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>
- #repository_credentials ⇒ Hash?
- #secrets ⇒ Array<Hash>?
- #system_controls ⇒ 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)
253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/hako/container.rb', line 253 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)
268 269 270 271 272 |
# File 'lib/hako/container.rb', line 268 def default_labels { 'cc.wanko.hako.version' => VERSION, } end |
#depends_on ⇒ Object
205 206 207 208 209 210 211 212 213 214 |
# File 'lib/hako/container.rb', line 205 def depends_on if @definition.key?('depends_on') @definition['depends_on'].map do |depends_on| { container_name: depends_on.fetch('container_name'), condition: depends_on.fetch('condition'), } end end end |
#env ⇒ Hash<String, String>
48 49 50 |
# File 'lib/hako/container.rb', line 48 def env @expanded_env ||= (@definition.fetch('env')) end |
#expand_env(env) ⇒ Hash<String, String> (private)
231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/hako/container.rb', line 231 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>?
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/hako/container.rb', line 140 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]
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/hako/container.rb', line 108 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?
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 191 |
# File 'lib/hako/container.rb', line 152 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)
246 247 248 249 250 |
# File 'lib/hako/container.rb', line 246 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?
97 98 99 100 101 102 103 104 105 |
# File 'lib/hako/container.rb', line 97 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>
76 77 78 79 80 81 82 83 84 |
# File 'lib/hako/container.rb', line 76 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>
65 66 67 68 69 70 71 72 73 |
# File 'lib/hako/container.rb', line 65 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 |
#repository_credentials ⇒ Hash?
217 218 219 220 221 222 223 |
# File 'lib/hako/container.rb', line 217 def repository_credentials if @definition.key?('repository_credentials') { credentials_parameter: @definition['repository_credentials'].fetch('credentials_parameter'), } end end |
#secrets ⇒ Array<Hash>?
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/hako/container.rb', line 53 def secrets if @definition['secrets'] @definition['secrets'].map do |secret| { name: secret.fetch('name'), value_from: secret.fetch('value_from'), } end end end |
#system_controls ⇒ Array<Hash>?
194 195 196 197 198 199 200 201 202 203 |
# File 'lib/hako/container.rb', line 194 def system_controls if @definition.key?('system_controls') @definition['system_controls'].map do |system_control| { namespace: system_control.fetch('namespace'), value: system_control.fetch('value'), } end end end |
#ulimits ⇒ Array<Hash>?
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/hako/container.rb', line 127 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>
87 88 89 90 91 92 93 94 |
# File 'lib/hako/container.rb', line 87 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 |