Class: Kamal::Configuration::Role
- Inherits:
-
Object
- Object
- Kamal::Configuration::Role
- Includes:
- Validation
- Defined in:
- lib/kamal/configuration/role.rb
Constant Summary collapse
- CORD_FILE =
"cord"
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#name ⇒ Object
(also: #to_s)
readonly
Returns the value of attribute name.
-
#specialized_env ⇒ Object
readonly
Returns the value of attribute specialized_env.
-
#specialized_healthcheck ⇒ Object
readonly
Returns the value of attribute specialized_healthcheck.
-
#specialized_logging ⇒ Object
readonly
Returns the value of attribute specialized_logging.
Instance Method Summary collapse
- #asset_extracted_path(version = nil) ⇒ Object
- #asset_path ⇒ Object
- #asset_volume(version = nil) ⇒ Object
- #asset_volume_args ⇒ Object
- #asset_volume_path(version = nil) ⇒ Object
- #assets? ⇒ Boolean
- #cmd ⇒ Object
- #container_name(version = nil) ⇒ Object
- #container_prefix ⇒ Object
- #cord_container_directory ⇒ Object
- #cord_container_file ⇒ Object
- #cord_host_directory ⇒ Object
- #cord_host_file ⇒ Object
- #cord_volume ⇒ Object
- #env(host) ⇒ Object
- #env_args(host) ⇒ Object
- #env_tags(host) ⇒ Object
- #health_check_args(cord: true) ⇒ Object
- #health_check_cmd_with_cord ⇒ Object
- #healthcheck ⇒ Object
- #hosts ⇒ Object
-
#initialize(name, config:) ⇒ Role
constructor
A new instance of Role.
- #label_args ⇒ Object
- #labels ⇒ Object
- #logging ⇒ Object
- #logging_args ⇒ Object
- #option_args ⇒ Object
- #primary? ⇒ Boolean
- #primary_host ⇒ Object
- #running_traefik? ⇒ Boolean
- #uses_cord? ⇒ Boolean
Methods included from Validation
Constructor Details
#initialize(name, config:) ⇒ Role
Returns a new instance of Role.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/kamal/configuration/role.rb', line 11 def initialize(name, config:) @name, @config = name.inquiry, config validate! \ specializations, example: validation_yml["servers"]["workers"], context: "servers/#{name}", with: Kamal::Configuration::Validator::Role @specialized_env = Kamal::Configuration::Env.new \ config: specializations.fetch("env", {}), secrets_file: File.join(config.host_env_directory, "roles", "#{container_prefix}.env"), context: "servers/#{name}/env" @specialized_logging = Kamal::Configuration::Logging.new \ logging_config: specializations.fetch("logging", {}), context: "servers/#{name}/logging" @specialized_healthcheck = Kamal::Configuration::Healthcheck.new \ healthcheck_config: specializations.fetch("healthcheck", {}), context: "servers/#{name}/healthcheck" end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/kamal/configuration/role.rb', line 7 def config @config end |
#name ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute name.
7 8 9 |
# File 'lib/kamal/configuration/role.rb', line 7 def name @name end |
#specialized_env ⇒ Object (readonly)
Returns the value of attribute specialized_env.
7 8 9 |
# File 'lib/kamal/configuration/role.rb', line 7 def specialized_env @specialized_env end |
#specialized_healthcheck ⇒ Object (readonly)
Returns the value of attribute specialized_healthcheck.
7 8 9 |
# File 'lib/kamal/configuration/role.rb', line 7 def specialized_healthcheck @specialized_healthcheck end |
#specialized_logging ⇒ Object (readonly)
Returns the value of attribute specialized_logging.
7 8 9 |
# File 'lib/kamal/configuration/role.rb', line 7 def specialized_logging @specialized_logging end |
Instance Method Details
#asset_extracted_path(version = nil) ⇒ Object
181 182 183 |
# File 'lib/kamal/configuration/role.rb', line 181 def asset_extracted_path(version = nil) File.join config.run_directory, "assets", "extracted", container_name(version) end |
#asset_path ⇒ Object
166 167 168 |
# File 'lib/kamal/configuration/role.rb', line 166 def asset_path specializations["asset_path"] || config.asset_path end |
#asset_volume(version = nil) ⇒ Object
174 175 176 177 178 179 |
# File 'lib/kamal/configuration/role.rb', line 174 def asset_volume(version = nil) if assets? Kamal::Configuration::Volume.new \ host_path: asset_volume_path(version), container_path: asset_path end end |
#asset_volume_args ⇒ Object
83 84 85 |
# File 'lib/kamal/configuration/role.rb', line 83 def asset_volume_args asset_volume&.docker_args end |
#asset_volume_path(version = nil) ⇒ Object
185 186 187 |
# File 'lib/kamal/configuration/role.rb', line 185 def asset_volume_path(version = nil) File.join config.run_directory, "assets", "volumes", container_name(version) end |
#assets? ⇒ Boolean
170 171 172 |
# File 'lib/kamal/configuration/role.rb', line 170 def assets? asset_path.present? && running_traefik? end |
#cmd ⇒ Object
45 46 47 |
# File 'lib/kamal/configuration/role.rb', line 45 def cmd specializations["cmd"] end |
#container_name(version = nil) ⇒ Object
157 158 159 |
# File 'lib/kamal/configuration/role.rb', line 157 def container_name(version = nil) [ container_prefix, version || config.version ].compact.join("-") end |
#container_prefix ⇒ Object
161 162 163 |
# File 'lib/kamal/configuration/role.rb', line 161 def container_prefix [ config.service, name, config.destination ].compact.join("-") end |
#cord_container_directory ⇒ Object
148 149 150 |
# File 'lib/kamal/configuration/role.rb', line 148 def cord_container_directory .fetch("cord", nil) end |
#cord_container_file ⇒ Object
152 153 154 |
# File 'lib/kamal/configuration/role.rb', line 152 def cord_container_file File.join cord_volume.container_path, CORD_FILE end |
#cord_host_directory ⇒ Object
132 133 134 |
# File 'lib/kamal/configuration/role.rb', line 132 def cord_host_directory File.join config.run_directory_as_docker_volume, "cords", [ container_prefix, config.run_id ].join("-") end |
#cord_host_file ⇒ Object
144 145 146 |
# File 'lib/kamal/configuration/role.rb', line 144 def cord_host_file File.join cord_volume.host_path, CORD_FILE end |
#cord_volume ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/kamal/configuration/role.rb', line 136 def cord_volume if (cord = healthcheck.cord) @cord_volume ||= Kamal::Configuration::Volume.new \ host_path: File.join(config.run_directory, "cords", [ container_prefix, config.run_id ].join("-")), container_path: cord end end |
#env(host) ⇒ Object
74 75 76 77 |
# File 'lib/kamal/configuration/role.rb', line 74 def env(host) @envs ||= {} @envs[host] ||= [ config.env, specialized_env, *(host).map(&:env) ].reduce(:merge) end |
#env_args(host) ⇒ Object
79 80 81 |
# File 'lib/kamal/configuration/role.rb', line 79 def env_args(host) env(host).args end |
#env_tags(host) ⇒ Object
41 42 43 |
# File 'lib/kamal/configuration/role.rb', line 41 def (host) tagged_hosts.fetch(host).collect { |tag| config.env_tag(tag) } end |
#health_check_args(cord: true) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/kamal/configuration/role.rb', line 88 def health_check_args(cord: true) if running_traefik? || healthcheck.set_port_or_path? if cord && uses_cord? optionize({ "health-cmd" => health_check_cmd_with_cord, "health-interval" => healthcheck.interval }) .concat(cord_volume.docker_args) else optionize({ "health-cmd" => healthcheck.cmd, "health-interval" => healthcheck.interval }) end else [] end end |
#health_check_cmd_with_cord ⇒ Object
110 111 112 |
# File 'lib/kamal/configuration/role.rb', line 110 def health_check_cmd_with_cord "(#{healthcheck.cmd}) && (stat #{cord_container_file} > /dev/null || exit 1)" end |
#healthcheck ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/kamal/configuration/role.rb', line 101 def healthcheck @healthcheck ||= if running_traefik? config.healthcheck.merge(specialized_healthcheck) else specialized_healthcheck end end |
#hosts ⇒ Object
37 38 39 |
# File 'lib/kamal/configuration/role.rb', line 37 def hosts tagged_hosts.keys end |
#label_args ⇒ Object
61 62 63 |
# File 'lib/kamal/configuration/role.rb', line 61 def label_args argumentize "--label", labels end |
#labels ⇒ Object
57 58 59 |
# File 'lib/kamal/configuration/role.rb', line 57 def labels default_labels.merge(traefik_labels).merge(custom_labels) end |
#logging ⇒ Object
69 70 71 |
# File 'lib/kamal/configuration/role.rb', line 69 def logging @logging ||= config.logging.merge(specialized_logging) end |
#logging_args ⇒ Object
65 66 67 |
# File 'lib/kamal/configuration/role.rb', line 65 def logging_args logging.args end |
#option_args ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/kamal/configuration/role.rb', line 49 def option_args if args = specializations["options"] optionize args else [] end end |
#primary? ⇒ Boolean
123 124 125 |
# File 'lib/kamal/configuration/role.rb', line 123 def primary? self == @config.primary_role end |
#primary_host ⇒ Object
33 34 35 |
# File 'lib/kamal/configuration/role.rb', line 33 def primary_host hosts.first end |
#running_traefik? ⇒ Boolean
115 116 117 118 119 120 121 |
# File 'lib/kamal/configuration/role.rb', line 115 def running_traefik? if specializations["traefik"].nil? primary? else specializations["traefik"] end end |
#uses_cord? ⇒ Boolean
128 129 130 |
# File 'lib/kamal/configuration/role.rb', line 128 def uses_cord? running_traefik? && cord_volume && healthcheck.cmd.present? end |