Class: Indocker::DeployContext

Inherits:
Object
  • Object
show all
Defined in:
lib/indocker/deploy_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger:, configuration:, server:) ⇒ DeployContext

Returns a new instance of DeployContext.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/indocker/deploy_context.rb', line 7

def initialize(logger:, configuration:, server:)
  @logger = logger
  @configuration = configuration
  @server = server
  @restart_policy = Indocker::Containers::RestartPolicy.new(configuration, logger)

  if server
    @session = Indocker::SshSession.new(
      host: server.host,
      user: server.user,
      port: server.port,
      logger: @logger
    )
  end
end

Instance Attribute Details

#serverObject (readonly)

Returns the value of attribute server.



5
6
7
# File 'lib/indocker/deploy_context.rb', line 5

def server
  @server
end

#sessionObject (readonly)

Returns the value of attribute session.



5
6
7
# File 'lib/indocker/deploy_context.rb', line 5

def session
  @session
end

Instance Method Details

#busy?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/indocker/deploy_context.rb', line 35

def busy?
  !!@busy
end

#close_sessionObject



27
28
29
# File 'lib/indocker/deploy_context.rb', line 27

def close_session
  @session.close if @session
end

#deploy(container, force_restart) ⇒ Object



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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/indocker/deploy_context.rb', line 39

def deploy(container, force_restart)
  @logger.info("Deploying container: #{container.name.to_s.green}")
  @logger.debug("Deploy dir: #{Indocker.deploy_dir}")

  Indocker::Docker.pull(container.image.registry_url) if !container.image.registry.is_local?

  container.networks.each do |network|
    Indocker::Docker.create_network(
      Indocker::NetworkHelper.name(@configuration.name, network)
    )
  end

  container.volumes.each do |volume|
    if volume.is_a?(Indocker::Volumes::External)
      Indocker::Docker.create_volume(
        Indocker::VolumeHelper.name(@configuration.name, volume)
      )
    end
  end

  container.get_start_option(:scale).times do |number|
    arg_list = Indocker::DockerRunArgs
      .get(container, @configuration, number)
      .join(' ')

    hostname = Indocker::ContainerHelper.hostname(@configuration.name, container, number)

    # timestamp generation
    run_cmd = Indocker::Docker.run_command(container.image.registry_url, arg_list, container.start_command, container.get_start_option(:service_args))

    env_files = container.get_start_option(:env_files, default: [])
      .map { |env_file|
        env_file = @configuration.env_files.fetch(env_file)
        File.read(Indocker::EnvFileHelper.path(env_file))
      }
      .join

    image_id = Indocker::Docker.image_id(container.image.registry_url)
    timestamp = Digest::MD5.hexdigest(run_cmd + image_id.to_s + env_files)

    binary_path = File.join(File.expand_path(Indocker.deploy_dir), 'bin')
    FileUtils.mkdir_p(binary_path)

    binary_path = File.join(binary_path, hostname)

    File.open(binary_path, 'w') { |f|
      f.write("#!/bin/bash\n\n")
      f.write(run_cmd)
    }

    FileUtils.chmod('+x', binary_path)

    container_id = Indocker::Docker.container_id_by_name(hostname)

    if !container_id || @restart_policy.restart?(container, timestamp) || force_restart
      if container.before_start_proc
        container.before_start_proc.call(container, number)
      end

      if container_id
        Indocker::Docker.stop(hostname, skip_errors: true)
      else
        Indocker::Docker.rm(hostname, skip_errors: true)
      end

      Indocker::Docker.run(container.image.registry_url, arg_list, container.start_command, container.get_start_option(:service_args))

      if container.after_start_proc
        container.after_start_proc.call(container, number)
      end

      @restart_policy.update(container, timestamp)
    else
      @logger.info("Skipping restart for container #{container.name.to_s.green} as no changes were found")

      if !container_id
        @restart_policy.update(container, timestamp)
      end
    end

    if container.after_deploy_proc
      container.after_deploy_proc.call(container, number)
    end
  end

  nil
end

#exec!(command) ⇒ Object



23
24
25
# File 'lib/indocker/deploy_context.rb', line 23

def exec!(command)
  @session.exec!(command)
end

#set_busy(flag) ⇒ Object



31
32
33
# File 'lib/indocker/deploy_context.rb', line 31

def set_busy(flag)
  @busy = !!flag
end