Class: Shiplane::Deploy::ContainerConfiguration

Inherits:
Configuration show all
Defined in:
lib/shiplane/deploy/container_configuration.rb

Instance Attribute Summary

Attributes inherited from Configuration

#env, #name, #options

Instance Method Summary collapse

Methods inherited from Configuration

#docker_command, #initialize

Constructor Details

This class inherits a constructor from Shiplane::Deploy::Configuration

Instance Method Details

#container_nameObject



30
31
32
# File 'lib/shiplane/deploy/container_configuration.rb', line 30

def container_name
  @container_name ||= "#{env.fetch(:application)}_#{name}"
end

#environmentObject



22
23
24
# File 'lib/shiplane/deploy/container_configuration.rb', line 22

def environment
  @environment ||= options.fetch(:environment, {})
end

#exposed_portsObject



18
19
20
# File 'lib/shiplane/deploy/container_configuration.rb', line 18

def exposed_ports
  @exposed_ports ||= [options.fetch(:expose, [])].flatten - published_ports
end

#flagsObject



26
27
28
# File 'lib/shiplane/deploy/container_configuration.rb', line 26

def flags
  @flags ||= options.fetch(:flags, {})
end

#image_nameObject



38
39
40
# File 'lib/shiplane/deploy/container_configuration.rb', line 38

def image_name
  @image_name ||= "#{options.fetch(:repo)}:#{image_tag}"
end

#image_tagObject



42
43
44
# File 'lib/shiplane/deploy/container_configuration.rb', line 42

def image_tag
  @image_tag ||= options.fetch(:tag, "#{env.fetch(:stage)}-#{env.fetch(:sha)}")
end

#letsencrypt_emailObject



58
59
60
# File 'lib/shiplane/deploy/container_configuration.rb', line 58

def letsencrypt_email
  @letsencrypt_email ||= options[:letsencrypt_email]
end

#letsencrypt_hostObject



54
55
56
# File 'lib/shiplane/deploy/container_configuration.rb', line 54

def letsencrypt_host
  @letsencrypt_host ||= options.fetch(:letsencrypt_host, virtual_host)
end

#network_aliasObject



6
7
8
# File 'lib/shiplane/deploy/container_configuration.rb', line 6

def network_alias
  @network_alias ||= options.fetch(:alias, container_name)
end

#network_connect_commands(role) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/shiplane/deploy/container_configuration.rb', line 70

def network_connect_commands(role)
  @network_commands ||= networks[1..-1].map do |network|
    [
      docker_command(role),
      "network connect",
      "--alias #{network_alias}",
      network,
      unique_container_name,
      "|| true",
    ].flatten.compact.join(" ")
  end
end

#networksObject



62
63
64
# File 'lib/shiplane/deploy/container_configuration.rb', line 62

def networks
  @networks ||= options.fetch(:networks, [])
end

#published_portsObject



14
15
16
# File 'lib/shiplane/deploy/container_configuration.rb', line 14

def published_ports
  @published_ports ||= [options.fetch(:publish, [])].flatten
end

#run_command(role) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/shiplane/deploy/container_configuration.rb', line 83

def run_command(role)
  @command ||= [
    docker_command(role),
    "run -d",
    volumes.map{|volume_set| "-v #{volume_set}" },
    published_ports.map{|port| "-p #{port}" },
    exposed_ports.map{|port| "--expose #{port}" },
    "--name #{unique_container_name}",
    "--network=#{networks.first}",
    "--network-alias=#{network_alias}",
    virtual_host ? "-e VIRTUAL_HOST=#{virtual_host}" : nil,
    letsencrypt_host ? "-e LETSENCRYPT_HOST=#{letsencrypt_host}" : nil,
    letsencrypt_email ? "-e LETSENCRYPT_EMAIL=#{letsencrypt_email}" : nil,
    environment.map{ |key, value| "-e #{key}=#{value}" },
    flags.map{ |key, value| "--#{key}=#{value}" },
    image_name,
    startup_command ? startup_command : nil,
  ].flatten.compact.join(" ")
end

#run_commands(role) ⇒ Object



103
104
105
106
107
108
# File 'lib/shiplane/deploy/container_configuration.rb', line 103

def run_commands(role)
  @run_commands ||= [
    run_command(role),
    network_connect_commands(role),
  ].flatten
end

#startup_commandObject



66
67
68
# File 'lib/shiplane/deploy/container_configuration.rb', line 66

def startup_command
  @startup_command ||= options[:command]
end

#unique_container_nameObject



34
35
36
# File 'lib/shiplane/deploy/container_configuration.rb', line 34

def unique_container_name
  @unique_container_name ||= "#{container_name}_#{env.fetch(:sha)}"
end

#virtual_hostObject



46
47
48
49
50
51
52
# File 'lib/shiplane/deploy/container_configuration.rb', line 46

def virtual_host
  return @virtual_host if defined?(@virtual_host) && @virtual_host

  if options[:virtual_host]
    @virtual_host = options[:virtual_host].is_a?(Proc) ? options[:virtual_host].call : options[:virtual_host]
  end
end

#volumesObject



10
11
12
# File 'lib/shiplane/deploy/container_configuration.rb', line 10

def volumes
  @volumes ||= options.fetch(:volumes, [])
end