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

#config_volume_mapObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/shiplane/deploy/container_configuration.rb', line 16

def config_volume_map
  {
    proxy_config: {
      key: :proxy_conf_volume,
      default: "/etc/docker/nginx-proxy/proxy.conf",
      mount_path: "/etc/nginx/proxy.conf",
    },
    conf_d_config: {
      key: :conf_d_volume,
      default: "/etc/docker/nginx-proxy/conf.d",
      mount_path: "/etc/nginx/conf.d",
    },
    vhost_d_config: {
      key: :vhost_d_volume,
      default: "/etc/docker/nginx-proxy/vhost.d",
      mount_path: "/etc/nginx/vhost.d",
    },
    share_config: {
      key: :share_volume,
      default: "/etc/docker/nginx-proxy/share",
      mount_path: "/etc/nginx/share",
    },
  }
end

#config_volumesObject



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

def config_volumes
  @config_volumes ||= config_volume_map.select do |config_volume|
    env.fetch(:"#{config_volume[:key]}_mount", true) || env.key?(:"#{config_volume[:key]}_location")
  end.map{|config_volume| "#{env.fetch(:"#{config_volume[:key]}_location", config_volume[:default])}:#{config_volume[:mount_path]}" }
end

#container_nameObject



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

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

#environmentObject



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

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

#exposed_portsObject



49
50
51
# File 'lib/shiplane/deploy/container_configuration.rb', line 49

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

#flagsObject



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

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

#image_nameObject



69
70
71
# File 'lib/shiplane/deploy/container_configuration.rb', line 69

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

#image_tagObject



73
74
75
# File 'lib/shiplane/deploy/container_configuration.rb', line 73

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

#letsencrypt_emailObject



89
90
91
# File 'lib/shiplane/deploy/container_configuration.rb', line 89

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

#letsencrypt_hostObject



85
86
87
# File 'lib/shiplane/deploy/container_configuration.rb', line 85

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



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/shiplane/deploy/container_configuration.rb', line 101

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



93
94
95
# File 'lib/shiplane/deploy/container_configuration.rb', line 93

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

#published_portsObject



45
46
47
# File 'lib/shiplane/deploy/container_configuration.rb', line 45

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

#run_command(role) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/shiplane/deploy/container_configuration.rb', line 114

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



134
135
136
137
138
139
# File 'lib/shiplane/deploy/container_configuration.rb', line 134

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

#startup_commandObject



97
98
99
# File 'lib/shiplane/deploy/container_configuration.rb', line 97

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

#unique_container_nameObject



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

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

#virtual_hostObject



77
78
79
80
81
82
83
# File 'lib/shiplane/deploy/container_configuration.rb', line 77

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



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

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