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

Returns a new instance of 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
# 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)

  # 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
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



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

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

#absolute_imageObject



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

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

#accessory(name) ⇒ Object



116
117
118
# File 'lib/kamal/configuration.rb', line 116

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

#all_hostsObject



120
121
122
# File 'lib/kamal/configuration.rb', line 120

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

#allow_empty_roles?Boolean

Returns:

  • (Boolean)


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

def allow_empty_roles?
  raw_config.allow_empty_roles
end

#app_directoryObject



220
221
222
# File 'lib/kamal/configuration.rb', line 220

def app_directory
  File.join apps_directory, service_and_destination
end

#app_hostsObject



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

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

#apps_directoryObject



216
217
218
# File 'lib/kamal/configuration.rb', line 216

def apps_directory
  File.join run_directory, "apps"
end

#asset_pathObject



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

def asset_path
  raw_config.asset_path
end

#assets_directoryObject



228
229
230
# File 'lib/kamal/configuration.rb', line 228

def assets_directory
  File.join app_directory, "assets"
end

#deploy_timeoutObject



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

def deploy_timeout
  raw_config.deploy_timeout || 30
end

#drain_timeoutObject



208
209
210
# File 'lib/kamal/configuration.rb', line 208

def drain_timeout
  raw_config.drain_timeout || 30
end

#env_directoryObject



224
225
226
# File 'lib/kamal/configuration.rb', line 224

def env_directory
  File.join app_directory, "env"
end

#env_tag(name) ⇒ Object



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

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

#env_tagsObject



244
245
246
247
248
249
250
# File 'lib/kamal/configuration.rb', line 244

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



240
241
242
# File 'lib/kamal/configuration.rb', line 240

def error_pages_path
  raw_config.error_pages_path
end

#hooks_pathObject



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

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

#latest_imageObject



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

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

#latest_tagObject



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

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

#logging_argsObject



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

def logging_args
  logging.args
end

#minimum_versionObject



100
101
102
# File 'lib/kamal/configuration.rb', line 100

def minimum_version
  raw_config.minimum_version
end

#primary_hostObject



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

def primary_host
  primary_role&.primary_host
end

#primary_roleObject



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

def primary_role
  role(primary_role_name)
end

#primary_role_nameObject



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

def primary_role_name
  raw_config.primary_role || "web"
end

#proxy_accessoriesObject



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

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

#proxy_hostsObject



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

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

#proxy_role_namesObject



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

def proxy_role_names
  proxy_roles.flat_map(&:name)
end

#proxy_rolesObject



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

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

#readiness_delayObject



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

def readiness_delay
  raw_config.readiness_delay || 7
end

#repositoryObject



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

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

#require_destination?Boolean

Returns:

  • (Boolean)


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

def require_destination?
  raw_config.require_destination
end

#retain_containersObject



184
185
186
# File 'lib/kamal/configuration.rb', line 184

def retain_containers
  raw_config.retain_containers || 5
end

#role(name) ⇒ Object



112
113
114
# File 'lib/kamal/configuration.rb', line 112

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

#rolesObject



108
109
110
# File 'lib/kamal/configuration.rb', line 108

def roles
  servers.roles
end

#run_directoryObject



212
213
214
# File 'lib/kamal/configuration.rb', line 212

def run_directory
  ".kamal"
end

#service_and_destinationObject



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

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

#service_with_versionObject



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

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

#to_hObject



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/kamal/configuration.rb', line 256

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



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

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

#version=(version) ⇒ Object



81
82
83
# File 'lib/kamal/configuration.rb', line 81

def version=(version)
  @declared_version = version
end

#volume_argsObject



188
189
190
191
192
193
194
# File 'lib/kamal/configuration.rb', line 188

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