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

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



46
47
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
74
75
76
77
78
79
80
81
# File 'lib/kamal/configuration.rb', line 46

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, secrets_path: secrets_path)

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

  @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, secrets: secrets)
  @proxy_boot = Proxy::Boot.new(config: self)
  @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
  ensure_local_registry_remote_builder_has_ssh_url
  ensure_no_conflicting_proxy_runs
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

#proxy_bootObject (readonly)

Returns the value of attribute proxy_boot.



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

def proxy_boot
  @proxy_boot
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



18
19
20
21
22
23
24
# File 'lib/kamal/configuration.rb', line 18

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



91
92
93
94
95
96
97
98
99
100
# File 'lib/kamal/configuration.rb', line 91

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

#absolute_imageObject



186
187
188
# File 'lib/kamal/configuration.rb', line 186

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



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

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

#allow_empty_roles?Boolean



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

def allow_empty_roles?
  raw_config.allow_empty_roles
end

#app_directoryObject



242
243
244
# File 'lib/kamal/configuration.rb', line 242

def app_directory
  File.join apps_directory, service_and_destination
end

#app_hostsObject



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

def app_hosts
  roles.flat_map(&:hosts).uniq
end

#apps_directoryObject



238
239
240
# File 'lib/kamal/configuration.rb', line 238

def apps_directory
  File.join run_directory, "apps"
end

#asset_pathObject



262
263
264
# File 'lib/kamal/configuration.rb', line 262

def asset_path
  raw_config.asset_path
end

#assets_directoryObject



250
251
252
# File 'lib/kamal/configuration.rb', line 250

def assets_directory
  File.join app_directory, "assets"
end

#deploy_timeoutObject



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

def deploy_timeout
  raw_config.deploy_timeout || 30
end

#drain_timeoutObject



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

def drain_timeout
  raw_config.drain_timeout || 30
end

#env_directoryObject



246
247
248
# File 'lib/kamal/configuration.rb', line 246

def env_directory
  File.join app_directory, "env"
end

#env_tag(name) ⇒ Object



278
279
280
# File 'lib/kamal/configuration.rb', line 278

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

#env_tagsObject



270
271
272
273
274
275
276
# File 'lib/kamal/configuration.rb', line 270

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

#error_pages_pathObject



266
267
268
# File 'lib/kamal/configuration.rb', line 266

def error_pages_path
  raw_config.error_pages_path
end

#hooks_pathObject



254
255
256
# File 'lib/kamal/configuration.rb', line 254

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

#host_accessories(host) ⇒ Object



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

def host_accessories(host)
  accessories.select { |accessory| accessory.hosts.include?(host) }
end

#host_roles(host) ⇒ Object



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

def host_roles(host)
  roles.select { |role| role.hosts.include?(host) }
end

#imageObject



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

def image
  name = raw_config&.image.presence
  name ||= raw_config&.service if registry.local?

  name
end

#latest_imageObject



190
191
192
# File 'lib/kamal/configuration.rb', line 190

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

#latest_tagObject



194
195
196
# File 'lib/kamal/configuration.rb', line 194

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

#logging_argsObject



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

def logging_args
  logging.args
end

#minimum_versionObject



102
103
104
# File 'lib/kamal/configuration.rb', line 102

def minimum_version
  raw_config.minimum_version
end

#primary_hostObject



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

def primary_host
  primary_role&.primary_host
end

#primary_roleObject



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

def primary_role
  role(primary_role_name)
end

#primary_role_nameObject



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

def primary_role_name
  raw_config.primary_role || "web"
end

#proxy_accessoriesObject



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

def proxy_accessories
  accessories.select(&:running_proxy?)
end

#proxy_hostsObject



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

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

#proxy_role_namesObject



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

def proxy_role_names
  proxy_roles.flat_map(&:name)
end

#proxy_rolesObject



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

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

#proxy_run(host) ⇒ Object



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

def proxy_run(host)
  # We validate that all the config are identical for a host
  proxy_runs(host.to_s).first
end

#readiness_delayObject



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

def readiness_delay
  raw_config.readiness_delay || 7
end

#repositoryObject



182
183
184
# File 'lib/kamal/configuration.rb', line 182

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

#require_destination?Boolean



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

def require_destination?
  raw_config.require_destination
end

#retain_containersObject



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

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



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

def run_directory
  ".kamal"
end

#secrets_pathObject



258
259
260
# File 'lib/kamal/configuration.rb', line 258

def secrets_path
  raw_config.secrets_path || ".kamal/secrets"
end

#service_and_destinationObject



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

def service_and_destination
  [ service, destination ].compact.join("-")
end

#service_with_versionObject



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

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

#to_hObject



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

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



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

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

#version=(version) ⇒ Object



83
84
85
# File 'lib/kamal/configuration.rb', line 83

def version=(version)
  @declared_version = version
end

#volume_argsObject



210
211
212
213
214
215
216
# File 'lib/kamal/configuration.rb', line 210

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