Class: KuberKit::ServiceDeployer::Strategies::Docker

Inherits:
Abstract show all
Defined in:
lib/kuber_kit/service_deployer/strategies/docker.rb

Constant Summary collapse

STRATEGY_OPTIONS =
[
  :namespace,
  :container_name,
  :env_file,
  :image_name,
  :detached,
  :command_name,
  :custom_args,
  :delete_if_exists,
  :volumes,
  :networks,
  :expose,
  :publish,
  :env_file_names,
  :env_vars
]

Instance Method Summary collapse

Instance Method Details

#deploy(shell, service) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/kuber_kit/service_deployer/strategies/docker.rb', line 28

def deploy(shell, service)
  strategy_options = service.attribute(:deployer, default: {})
  unknown_options  = strategy_options.keys.map(&:to_sym) - STRATEGY_OPTIONS
  if unknown_options.any?
    raise KuberKit::Error, "Unknow options for deploy strategy: #{unknown_options}. Available options: #{STRATEGY_OPTIONS}"
  end
  
  namespace       = strategy_options.fetch(:namespace, nil)
  container_name  = strategy_options.fetch(:container_name, [namespace, service.name].compact.join("_"))
  command_name    = strategy_options.fetch(:command_name, nil)
  custom_env_file = strategy_options.fetch(:env_file, nil)
  custom_args     = strategy_options.fetch(:custom_args, nil)
  networks        = strategy_options.fetch(:networks, [])
  volumes         = strategy_options.fetch(:volumes, [])
  expose_ports    = strategy_options.fetch(:expose, [])
  publish_ports   = strategy_options.fetch(:publish, [])
  hostname        = strategy_options.fetch(:hostname, container_name)

  env_file_names  = strategy_options.fetch(:env_file_names, [])
  env_files       = prepare_env_files(shell, env_file_names)
  env_vars        = strategy_options.fetch(:env_vars, {})

  image_name = strategy_options.fetch(:image_name, nil)
  if image_name.nil?
    raise KuberKit::Error, "image_name is mandatory attribute for this deploy strategy"
  end
  image = image_store.get_image(image_name.to_sym)

  delete_enabled = strategy_options.fetch(:delete_if_exists, false)
  if delete_enabled && docker_commands.container_exists?(shell, container_name)
    docker_commands.delete_container(shell, container_name)
  end

  custom_args = Array(custom_args)
  if container_name
    custom_args << "--name #{container_name}"
  end
  if custom_env_file
    custom_args << "--env-file #{custom_env_file}"
  end
  if hostname
    custom_args << "--hostname #{hostname}"
  end
  networks.each do |network|
    docker_commands.create_network(shell, network)
    custom_args << "--network #{network}"
  end
  volumes.each do |volume|
    volume_name, _ = volume.split(":")
    docker_commands.create_volume(shell, volume_name) unless volume_name.start_with?("/")
    custom_args << "--volume #{volume}"
  end
  Array(expose_ports).each do |expose_port|
    custom_args << "--expose #{expose_port}"
  end
  Array(publish_ports).each do |publish_port|
    custom_args << "--publish #{publish_port}"
  end
  Array(env_files).each do |env_file|
    custom_args << "--env-file #{env_file}"
  end
  env_vars.each do |key, value|
    custom_args << "--env #{key}=#{value}"
  end

  docker_commands.run(
    shell, image.remote_registry_url, 
    command:      command_name,
    args:         custom_args, 
    detached:     !!strategy_options[:detached],
    interactive:  !strategy_options[:detached]
  )
end