Class: Mrsk::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/mrsk/configuration.rb

Defined Under Namespace

Classes: Accessory, Boot, Builder, Role

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Configuration.



40
41
42
43
44
45
# File 'lib/mrsk/configuration.rb', line 40

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

Instance Attribute Details

#destinationObject

Returns the value of attribute destination.



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

def destination
  @destination
end

#raw_configObject

Returns the value of attribute raw_config.



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

def raw_config
  @raw_config
end

Class Method Details

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



16
17
18
19
20
# File 'lib/mrsk/configuration.rb', line 16

def create_from(config_file:, destination: nil, version: nil)
  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



56
57
58
# File 'lib/mrsk/configuration.rb', line 56

def abbreviated_version
  Mrsk::Utils.abbreviate_version(version)
end

#absolute_imageObject



99
100
101
# File 'lib/mrsk/configuration.rb', line 99

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

#accessoriesObject



69
70
71
# File 'lib/mrsk/configuration.rb', line 69

def accessories
  @accessories ||= raw_config.accessories&.keys&.collect { |name| Mrsk::Configuration::Accessory.new(name, config: self) } || []
end

#accessory(name) ⇒ Object



73
74
75
# File 'lib/mrsk/configuration.rb', line 73

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

#all_hostsObject



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

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

#bootObject



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

def boot
  Mrsk::Configuration::Boot.new(config: self)
end

#builderObject



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

def builder
  Mrsk::Configuration::Builder.new(config: self)
end

#ensure_env_availableObject

Will raise KeyError if any secret ENVs are missing



209
210
211
212
213
214
# File 'lib/mrsk/configuration.rb', line 209

def ensure_env_available
  env_args
  roles.each(&:env_args)

  true
end

#env_argsObject



112
113
114
115
116
117
118
# File 'lib/mrsk/configuration.rb', line 112

def env_args
  if raw_config.env.present?
    argumentize_env_with_secrets(raw_config.env)
  else
    []
  end
end

#healthcheckObject



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

def healthcheck
  { "path" => "/up", "port" => 3000, "max_attempts" => 7 }.merge(raw_config.healthcheck || {})
end

#hooks_pathObject



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

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

#latest_imageObject



103
104
105
# File 'lib/mrsk/configuration.rb', line 103

def latest_image
  "#{repository}:latest"
end

#logging_argsObject



128
129
130
131
132
133
134
135
# File 'lib/mrsk/configuration.rb', line 128

def logging_args
  if raw_config.logging.present?
    optionize({ "log-driver" => raw_config.logging["driver"] }.compact) +
      argumentize("--log-opt", raw_config.logging["options"])
  else
    argumentize("--log-opt", { "max-size" => "10m" })
  end
end

#minimum_versionObject



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

def minimum_version
  raw_config.minimum_version
end

#primary_web_hostObject



82
83
84
# File 'lib/mrsk/configuration.rb', line 82

def primary_web_host
  role(:web).primary_host
end

#readiness_delayObject



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

def readiness_delay
  raw_config.readiness_delay || 7
end

#repositoryObject



95
96
97
# File 'lib/mrsk/configuration.rb', line 95

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

#role(name) ⇒ Object



65
66
67
# File 'lib/mrsk/configuration.rb', line 65

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

#rolesObject



61
62
63
# File 'lib/mrsk/configuration.rb', line 61

def roles
  @roles ||= role_names.collect { |role_name| Role.new(role_name, config: self) }
end

#service_with_versionObject



107
108
109
# File 'lib/mrsk/configuration.rb', line 107

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

#ssh_optionsObject



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

def ssh_options
  { user: ssh_user, proxy: ssh_proxy, auth_methods: [ "publickey" ] }.compact
end

#ssh_proxyObject



146
147
148
149
150
151
152
153
# File 'lib/mrsk/configuration.rb', line 146

def ssh_proxy
  if raw_config.ssh.present? && raw_config.ssh["proxy"]
    Net::SSH::Proxy::Jump.new \
      raw_config.ssh["proxy"].include?("@") ? raw_config.ssh["proxy"] : "root@#{raw_config.ssh["proxy"]}"
  elsif raw_config.ssh.present? && raw_config.ssh["proxy_command"]
    Net::SSH::Proxy::Command.new(raw_config.ssh["proxy_command"])
  end
end

#ssh_userObject



138
139
140
141
142
143
144
# File 'lib/mrsk/configuration.rb', line 138

def ssh_user
  if raw_config.ssh.present?
    raw_config.ssh["user"] || "root"
  else
    "root"
  end
end

#to_hObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/mrsk/configuration.rb', line 177

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

#traefikObject



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

def traefik
  raw_config.traefik || {}
end

#traefik_hostsObject



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

def traefik_hosts
  roles.select(&:running_traefik?).flat_map(&:hosts).uniq
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  ensure_required_keys_present && ensure_valid_mrsk_version
end

#versionObject



52
53
54
# File 'lib/mrsk/configuration.rb', line 52

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

#version=(version) ⇒ Object



48
49
50
# File 'lib/mrsk/configuration.rb', line 48

def version=(version)
  @declared_version = version
end

#volume_argsObject



120
121
122
123
124
125
126
# File 'lib/mrsk/configuration.rb', line 120

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