Class: MobyDerp::Container

Inherits:
Object
  • Object
show all
Includes:
LoggingHelpers
Defined in:
lib/moby_derp/container.rb

Instance Method Summary collapse

Constructor Details

#initialize(pod:, container_config:, root_container: false) ⇒ Container

Returns a new instance of Container.



14
15
16
17
18
# File 'lib/moby_derp/container.rb', line 14

def initialize(pod:, container_config:, root_container: false)
	@logger = pod.logger

	@pod, @config, @root_container = pod, container_config, root_container
end

Instance Method Details

#runObject



20
21
22
23
24
25
26
27
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
# File 'lib/moby_derp/container.rb', line 20

def run
	container_name = @root_container ? @pod.name : @config.name
	@logger.debug(logloc) { "Calculated container name is #{container_name} (@root_container: #{@root_container.inspect}, @config.name: #{@config.name}, @pod.name: #{@pod.name})" }

	begin
		existing_container = Docker::Container.get(container_name)
		@logger.debug(logloc) { "Config hash for existing container #{container_name} is #{existing_container.info["Config"]["Labels"]["org.hezmatt.moby-derp.config-hash"].inspect}" }
		@logger.debug(logloc) { "New config hash is #{params_hash(container_creation_parameters).inspect}" }

		if existing_container.info["Config"]["Labels"]["org.hezmatt.moby-derp.config-hash"] == params_hash(container_creation_parameters)
			# Container is up-to-date
			@logger.info(logloc) { "Container #{container_name} is up-to-date" }
			@logger.debug(logloc) { "Container #{container_name} has restart=#{@config.restart}, state is #{existing_container.info.dig("State", "Status")}" }

			if @config.restart == "always" && existing_container.info.dig("State", "Status") != "running"
				existing_container.start
			end

			return existing_container.id
		end

		if existing_container.info["Config"]["Labels"]["org.hezmatt.moby-derp.pod-name"] != @pod.name
			raise ContainerError,
			      "container #{container_name} is not tagged as being part of this pod"
		end
		@logger.info(logloc) { "Deleting container #{container_name} (#{existing_container.id[0..11]}) because it is out-of-date" }
		existing_container.delete(force: true)
		@logger.info(logloc) { "Creating new container #{container_name}" }
	rescue Docker::Error::NotFoundError
		@logger.info(logloc) { "Container #{container_name} does not exist; creating it" }
		# Container doesn't exist, need to create it
	end

	begin
		c = Docker::Container.create(hash_labelled(container_creation_parameters))
		c.start!.object_id

		if @config.startup_health_check
			attempts = @config.startup_health_check[:attempts]

			while attempts > 0
				stdout, stderr, exitstatus = c.exec(@config.startup_health_check[:command])
				if exitstatus > 0
					stdout_lines = stdout.empty? ? [] : ["stdout:"] + stdout.join("\n").split("\n").map { |l| "  #{l}" }
					stderr_lines = stderr.empty? ? [] : ["stderr:"] + stderr.join("\n").split("\n").map { |l| "  #{l}" }
					output_lines = stdout_lines + stderr_lines
					@logger.warn(logloc) { "Startup health check failed on #{container_name} with status #{exitstatus}." + (output_lines.empty? ? "" : (["  Output:"] + output_lines.join("\n  "))) }

					attempts -= 1
					sleep @config.startup_health_check[:interval]
				else
					@logger.info(logloc) { "Startup health check passed." }
					break
				end
			end

			if attempts == 0
				raise MobyDerp::StartupHealthCheckError,
					"Container #{container_name} has failed the startup health check command #{@config.startup_health_check[:attempts]} times.  Aborting."
			end
		end

		c.id
	rescue Docker::Error::ClientError => ex
		raise MobyDerp::ContainerError,
		      "moby daemon returned error: #{ex.message}"
	end
end