Class: Kamal::Configuration::Role

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validation

#validate!, #validation_yml

Constructor Details

#initialize(name, config:) ⇒ Role

Returns a new instance of Role.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kamal/configuration/role.rb', line 10

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: config.secrets,
    context: "servers/#{name}/env"

  @specialized_logging = Kamal::Configuration::Logging.new \
    logging_config: specializations.fetch("logging", {}),
    context: "servers/#{name}/logging"

  initialize_specialized_proxy
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/kamal/configuration/role.rb', line 6

def config
  @config
end

#nameObject (readonly) Also known as: to_s

Returns the value of attribute name.



6
7
8
# File 'lib/kamal/configuration/role.rb', line 6

def name
  @name
end

#specialized_envObject (readonly)

Returns the value of attribute specialized_env.



6
7
8
# File 'lib/kamal/configuration/role.rb', line 6

def specialized_env
  @specialized_env
end

#specialized_loggingObject (readonly)

Returns the value of attribute specialized_logging.



6
7
8
# File 'lib/kamal/configuration/role.rb', line 6

def specialized_logging
  @specialized_logging
end

#specialized_proxyObject (readonly)

Returns the value of attribute specialized_proxy.



6
7
8
# File 'lib/kamal/configuration/role.rb', line 6

def specialized_proxy
  @specialized_proxy
end

Instance Method Details

#asset_extracted_directory(version = config.version) ⇒ Object



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

def asset_extracted_directory(version = config.version)
  File.join config.assets_directory, "extracted", [ name, version ].join("-")
end

#asset_pathObject



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

def asset_path
  specializations["asset_path"] || config.asset_path
end

#asset_volume(version = config.version) ⇒ Object



137
138
139
140
141
142
# File 'lib/kamal/configuration/role.rb', line 137

def asset_volume(version = config.version)
  if assets?
    Kamal::Configuration::Volume.new \
      host_path: asset_volume_directory(version), container_path: asset_path
  end
end

#asset_volume_argsObject



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

def asset_volume_args
  asset_volume&.docker_args
end

#asset_volume_directory(version = config.version) ⇒ Object



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

def asset_volume_directory(version = config.version)
  File.join config.assets_directory, "volumes", [ name, version ].join("-")
end

#assets?Boolean

Returns:

  • (Boolean)


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

def assets?
  asset_path.present? && running_proxy?
end

#cmdObject



42
43
44
# File 'lib/kamal/configuration/role.rb', line 42

def cmd
  specializations["cmd"]
end

#container_name(version = nil) ⇒ Object



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

def container_name(version = nil)
  [ container_prefix, version || config.version ].compact.join("-")
end

#container_prefixObject



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

def container_prefix
  [ config.service, name, config.destination ].compact.join("-")
end

#ensure_one_host_for_sslObject



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

def ensure_one_host_for_ssl
  if running_proxy? && proxy.ssl? && hosts.size > 1
    raise Kamal::ConfigurationError, "SSL is only supported on a single server, found #{hosts.size} servers for role #{name}"
  end
end

#env(host) ⇒ Object



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

def env(host)
  @envs ||= {}
  @envs[host] ||= [ config.env, specialized_env, *env_tags(host).map(&:env) ].reduce(:merge)
end

#env_args(host) ⇒ Object



94
95
96
# File 'lib/kamal/configuration/role.rb', line 94

def env_args(host)
  [ *env(host).clear_args, *argumentize("--env-file", secrets_path) ]
end

#env_directoryObject



98
99
100
# File 'lib/kamal/configuration/role.rb', line 98

def env_directory
  File.join(config.env_directory, "roles")
end

#env_tags(host) ⇒ Object



38
39
40
# File 'lib/kamal/configuration/role.rb', line 38

def env_tags(host)
  tagged_hosts.fetch(host).collect { |tag| config.env_tag(tag) }
end

#hostsObject



34
35
36
# File 'lib/kamal/configuration/role.rb', line 34

def hosts
  tagged_hosts.keys
end

#label_argsObject



58
59
60
# File 'lib/kamal/configuration/role.rb', line 58

def label_args
  argumentize "--label", labels
end

#labelsObject



54
55
56
# File 'lib/kamal/configuration/role.rb', line 54

def labels
  default_labels.merge(custom_labels)
end

#loggingObject



66
67
68
# File 'lib/kamal/configuration/role.rb', line 66

def logging
  @logging ||= config.logging.merge(specialized_logging)
end

#logging_argsObject



62
63
64
# File 'lib/kamal/configuration/role.rb', line 62

def logging_args
  logging.args
end

#option_argsObject



46
47
48
49
50
51
52
# File 'lib/kamal/configuration/role.rb', line 46

def option_args
  if args = specializations["options"]
    optionize args
  else
    []
  end
end

#primary?Boolean

Returns:

  • (Boolean)


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

def primary?
  name == @config.primary_role_name
end

#primary_hostObject



30
31
32
# File 'lib/kamal/configuration/role.rb', line 30

def primary_host
  hosts.first
end

#proxyObject



70
71
72
# File 'lib/kamal/configuration/role.rb', line 70

def proxy
  @proxy ||= config.proxy.merge(specialized_proxy) if running_proxy?
end

#running_proxy?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/kamal/configuration/role.rb', line 74

def running_proxy?
  @running_proxy
end

#secrets_io(host) ⇒ Object



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

def secrets_io(host)
  env(host).secrets_io
end

#secrets_pathObject



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

def secrets_path
  File.join(config.env_directory, "roles", "#{name}.env")
end

#ssl?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/kamal/configuration/role.rb', line 78

def ssl?
  running_proxy? && proxy.ssl?
end

#stop_argsObject



82
83
84
85
86
87
# File 'lib/kamal/configuration/role.rb', line 82

def stop_args
  # When deploying with the proxy, kamal-proxy will drain request before returning so we don't need to wait.
  timeout = running_proxy? ? nil : config.drain_timeout

  [ *argumentize("-t", timeout) ]
end