Class: Kamal::Configuration

Inherits:
Object
  • Object
show all
Includes:
Validation
Defined in:
lib/kamal/configuration.rb

Defined Under Namespace

Modules: Validation Classes: Accessory, Alias, Boot, Builder, Env, Logging, Proxy, Registry, Role, Servers, Ssh, Sshkit, Validator, Volume

Constant Summary collapse

PROXY_MINIMUM_VERSION =
"v0.8.2.6"
PROXY_HTTP_PORT =
80
PROXY_HTTPS_PORT =
443
PROXY_LOG_MAX_SIZE =
"10m"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validation

#validate!, #validation_yml

Constructor Details

#initialize(raw_config, destination: nil, version: nil, validate: true) ⇒ Configuration

Returns a new instance of Configuration.



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
80
81
82
83
# File 'lib/kamal/configuration.rb', line 51

def initialize(raw_config, destination: nil, version: nil, validate: true)
  @raw_config = ActiveSupport::InheritableOptions.new(raw_config)
  @destination = destination
  @declared_version = version

  validate! raw_config, example: validation_yml.symbolize_keys, context: "", with: Kamal::Configuration::Validator::Configuration

  @secrets = Kamal::Secrets.new(destination: destination)

  # Eager load config to validate it, these are first as they have dependencies later on
  @servers = Servers.new(config: self)
  @registry = Registry.new(config: self)

  @accessories = @raw_config.accessories&.keys&.collect { |name| Accessory.new(name, config: self) } || []
  @aliases = @raw_config.aliases&.keys&.to_h { |name| [ name, Alias.new(name, config: self) ] } || {}
  @boot = Boot.new(config: self)
  @builder = Builder.new(config: self)
  @env = Env.new(config: @raw_config.env || {}, secrets: secrets)

  @logging = Logging.new(logging_config: @raw_config.logging)
  @proxy = Proxy.new(config: self, proxy_config: @raw_config.proxy || {})
  @ssh = Ssh.new(config: self)
  @sshkit = Sshkit.new(config: self)

  ensure_destination_if_required
  ensure_required_keys_present
  ensure_valid_kamal_version
  ensure_retain_containers_valid
  ensure_valid_service_name
  ensure_no_traefik_reboot_hooks
  ensure_one_host_for_ssl_roles
  ensure_unique_hosts_for_ssl_roles
end

Instance Attribute Details

#accessoriesObject (readonly)

Returns the value of attribute accessories.



13
14
15
# File 'lib/kamal/configuration.rb', line 13

def accessories
  @accessories
end

#aliasesObject (readonly)

Returns the value of attribute aliases.



13
14
15
# File 'lib/kamal/configuration.rb', line 13

def aliases
  @aliases
end

#bootObject (readonly)

Returns the value of attribute boot.



13
14
15
# File 'lib/kamal/configuration.rb', line 13

def boot
  @boot
end

#builderObject (readonly)

Returns the value of attribute builder.



13
14
15
# File 'lib/kamal/configuration.rb', line 13

def builder
  @builder
end

#destinationObject (readonly)

Returns the value of attribute destination.



12
13
14
# File 'lib/kamal/configuration.rb', line 12

def destination
  @destination
end

#envObject (readonly)

Returns the value of attribute env.



13
14
15
# File 'lib/kamal/configuration.rb', line 13

def env
  @env
end

#loggingObject (readonly)

Returns the value of attribute logging.



13
14
15
# File 'lib/kamal/configuration.rb', line 13

def logging
  @logging
end

#proxyObject (readonly)

Returns the value of attribute proxy.



13
14
15
# File 'lib/kamal/configuration.rb', line 13

def proxy
  @proxy
end

#raw_configObject (readonly)

Returns the value of attribute raw_config.



12
13
14
# File 'lib/kamal/configuration.rb', line 12

def raw_config
  @raw_config
end

#registryObject (readonly)

Returns the value of attribute registry.



13
14
15
# File 'lib/kamal/configuration.rb', line 13

def registry
  @registry
end

#secretsObject (readonly)

Returns the value of attribute secrets.



12
13
14
# File 'lib/kamal/configuration.rb', line 12

def secrets
  @secrets
end

#serversObject (readonly)

Returns the value of attribute servers.



13
14
15
# File 'lib/kamal/configuration.rb', line 13

def servers
  @servers
end

#sshObject (readonly)

Returns the value of attribute ssh.



13
14
15
# File 'lib/kamal/configuration.rb', line 13

def ssh
  @ssh
end

#sshkitObject (readonly)

Returns the value of attribute sshkit.



13
14
15
# File 'lib/kamal/configuration.rb', line 13

def sshkit
  @sshkit
end

Class Method Details

.create_from(config_file:, destination: nil, version: nil) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/kamal/configuration.rb', line 23

def create_from(config_file:, destination: nil, version: nil)
  ENV["KAMAL_DESTINATION"] = destination

  raw_config = load_config_files(config_file, *destination_config_file(config_file, destination))

  new raw_config, destination: destination, version: version
end

Instance Method Details

#abbreviated_versionObject



94
95
96
97
98
99
100
101
102
103
# File 'lib/kamal/configuration.rb', line 94

def abbreviated_version
  if version
    # Don't abbreviate <sha>_uncommitted_<etc>
    if version.include?("_")
      version
    else
      version[0...7]
    end
  end
end

#absolute_imageObject



159
160
161
# File 'lib/kamal/configuration.rb', line 159

def absolute_image
  "#{repository}:#{version}"
end

#accessory(name) ⇒ Object



118
119
120
# File 'lib/kamal/configuration.rb', line 118

def accessory(name)
  accessories.detect { |a| a.name == name.to_s }
end

#all_hostsObject



123
124
125
# File 'lib/kamal/configuration.rb', line 123

def all_hosts
  (roles + accessories).flat_map(&:hosts).uniq
end

#allow_empty_roles?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/kamal/configuration.rb', line 139

def allow_empty_roles?
  raw_config.allow_empty_roles
end

#app_directoryObject



218
219
220
# File 'lib/kamal/configuration.rb', line 218

def app_directory
  File.join apps_directory, [ service, destination ].compact.join("-")
end

#apps_directoryObject



214
215
216
# File 'lib/kamal/configuration.rb', line 214

def apps_directory
  File.join run_directory, "apps"
end

#asset_pathObject



235
236
237
# File 'lib/kamal/configuration.rb', line 235

def asset_path
  raw_config.asset_path
end

#assets_directoryObject



226
227
228
# File 'lib/kamal/configuration.rb', line 226

def assets_directory
  File.join app_directory, "assets"
end

#deploy_timeoutObject



201
202
203
# File 'lib/kamal/configuration.rb', line 201

def deploy_timeout
  raw_config.deploy_timeout || 30
end

#drain_timeoutObject



205
206
207
# File 'lib/kamal/configuration.rb', line 205

def drain_timeout
  raw_config.drain_timeout || 30
end

#env_directoryObject



222
223
224
# File 'lib/kamal/configuration.rb', line 222

def env_directory
  File.join app_directory, "env"
end

#env_tag(name) ⇒ Object



248
249
250
# File 'lib/kamal/configuration.rb', line 248

def env_tag(name)
  env_tags.detect { |t| t.name == name.to_s }
end

#env_tagsObject



240
241
242
243
244
245
246
# File 'lib/kamal/configuration.rb', line 240

def env_tags
  @env_tags ||= if (tags = raw_config.env["tags"])
    tags.collect { |name, config| Env::Tag.new(name, config: config, secrets: secrets) }
  else
    []
  end
end

#hooks_pathObject



231
232
233
# File 'lib/kamal/configuration.rb', line 231

def hooks_path
  raw_config.hooks_path || ".kamal/hooks"
end

#latest_imageObject



163
164
165
# File 'lib/kamal/configuration.rb', line 163

def latest_image
  "#{repository}:#{latest_tag}"
end

#latest_tagObject



167
168
169
# File 'lib/kamal/configuration.rb', line 167

def latest_tag
  [ "latest", *destination ].join("-")
end

#logging_argsObject



192
193
194
# File 'lib/kamal/configuration.rb', line 192

def logging_args
  logging.args
end

#minimum_versionObject



105
106
107
# File 'lib/kamal/configuration.rb', line 105

def minimum_version
  raw_config.minimum_version
end

#primary_hostObject



127
128
129
# File 'lib/kamal/configuration.rb', line 127

def primary_host
  primary_role&.primary_host
end

#primary_roleObject



135
136
137
# File 'lib/kamal/configuration.rb', line 135

def primary_role
  role(primary_role_name)
end

#primary_role_nameObject



131
132
133
# File 'lib/kamal/configuration.rb', line 131

def primary_role_name
  raw_config.primary_role || "web"
end

#proxy_container_nameObject



268
269
270
# File 'lib/kamal/configuration.rb', line 268

def proxy_container_name
  "kamal-proxy"
end

#proxy_directoryObject



272
273
274
# File 'lib/kamal/configuration.rb', line 272

def proxy_directory
  File.join run_directory, "proxy"
end

#proxy_hostsObject



151
152
153
# File 'lib/kamal/configuration.rb', line 151

def proxy_hosts
  proxy_roles.flat_map(&:hosts).uniq
end

#proxy_imageObject



264
265
266
# File 'lib/kamal/configuration.rb', line 264

def proxy_image
  "nocoffeehq/kamal-proxy:#{PROXY_MINIMUM_VERSION}"
end

#proxy_logging_args(max_size) ⇒ Object



256
257
258
# File 'lib/kamal/configuration.rb', line 256

def proxy_logging_args(max_size)
  argumentize "--log-opt", "max-size=#{max_size}" if max_size.present?
end

#proxy_options_defaultObject



260
261
262
# File 'lib/kamal/configuration.rb', line 260

def proxy_options_default
  [ *proxy_publish_args(PROXY_HTTP_PORT, PROXY_HTTPS_PORT), *proxy_logging_args(PROXY_LOG_MAX_SIZE) ]
end

#proxy_options_fileObject



276
277
278
# File 'lib/kamal/configuration.rb', line 276

def proxy_options_file
  File.join proxy_directory, "options"
end

#proxy_publish_args(http_port, https_port) ⇒ Object



252
253
254
# File 'lib/kamal/configuration.rb', line 252

def proxy_publish_args(http_port, https_port)
  argumentize "--publish", [ "#{http_port}:#{PROXY_HTTP_PORT}", "#{https_port}:#{PROXY_HTTPS_PORT}" ]
end

#proxy_role_namesObject



147
148
149
# File 'lib/kamal/configuration.rb', line 147

def proxy_role_names
  proxy_roles.flat_map(&:name)
end

#proxy_rolesObject



143
144
145
# File 'lib/kamal/configuration.rb', line 143

def proxy_roles
  roles.select(&:running_proxy?)
end

#readiness_delayObject



197
198
199
# File 'lib/kamal/configuration.rb', line 197

def readiness_delay
  raw_config.readiness_delay || 7
end

#repositoryObject



155
156
157
# File 'lib/kamal/configuration.rb', line 155

def repository
  [ registry.server, image ].compact.join("/")
end

#require_destination?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/kamal/configuration.rb', line 175

def require_destination?
  raw_config.require_destination
end

#retain_containersObject



179
180
181
# File 'lib/kamal/configuration.rb', line 179

def retain_containers
  raw_config.retain_containers || 5
end

#role(name) ⇒ Object



114
115
116
# File 'lib/kamal/configuration.rb', line 114

def role(name)
  roles.detect { |r| r.name == name.to_s }
end

#rolesObject



110
111
112
# File 'lib/kamal/configuration.rb', line 110

def roles
  servers.roles
end

#run_directoryObject



210
211
212
# File 'lib/kamal/configuration.rb', line 210

def run_directory
  ".kamal"
end

#service_with_versionObject



171
172
173
# File 'lib/kamal/configuration.rb', line 171

def service_with_version
  "#{service}-#{version}"
end

#to_hObject



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/kamal/configuration.rb', line 281

def to_h
  {
    roles: role_names,
    hosts: all_hosts,
    primary_host: primary_host,
    version: version,
    repository: repository,
    absolute_image: absolute_image,
    service_with_version: service_with_version,
    volume_args: volume_args,
    ssh_options: ssh.to_h,
    sshkit: sshkit.to_h,
    builder: builder.to_h,
    accessories: raw_config.accessories,
    logging: logging_args
  }.compact
end

#versionObject



90
91
92
# File 'lib/kamal/configuration.rb', line 90

def version
  @declared_version.presence || ENV["VERSION"] || git_version
end

#version=(version) ⇒ Object



86
87
88
# File 'lib/kamal/configuration.rb', line 86

def version=(version)
  @declared_version = version
end

#volume_argsObject



184
185
186
187
188
189
190
# File 'lib/kamal/configuration.rb', line 184

def volume_args
  if raw_config.volumes.present?
    argumentize "--volume", raw_config.volumes
  else
    []
  end
end