Class: MobyDerp::Pod

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pod_config) ⇒ Pod

Returns a new instance of Pod.



10
11
12
13
# File 'lib/moby_derp/pod.rb', line 10

def initialize(pod_config)
	@config = pod_config
	@logger = pod_config.logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/moby_derp/pod.rb', line 8

def logger
  @logger
end

Instance Method Details

#common_environmentObject



69
70
71
# File 'lib/moby_derp/pod.rb', line 69

def common_environment
	@config.common_environment
end

#common_labelsObject



61
62
63
# File 'lib/moby_derp/pod.rb', line 61

def common_labels
	@config.common_labels
end

#common_mountsObject



73
74
75
# File 'lib/moby_derp/pod.rb', line 73

def common_mounts
	@config.common_mounts
end

#exposeObject



89
90
91
# File 'lib/moby_derp/pod.rb', line 89

def expose
	@config.expose
end

#hostnameObject



85
86
87
# File 'lib/moby_derp/pod.rb', line 85

def hostname
	@config.hostname
end

#mount_rootObject



77
78
79
# File 'lib/moby_derp/pod.rb', line 77

def mount_root
	@config.mount_root
end

#nameObject



48
49
50
# File 'lib/moby_derp/pod.rb', line 48

def name
	@config.name
end

#network_nameObject



81
82
83
# File 'lib/moby_derp/pod.rb', line 81

def network_name
	@config.network_name
end

#root_container_idObject



52
53
54
55
56
57
58
59
# File 'lib/moby_derp/pod.rb', line 52

def root_container_id
	if @root_container_id.nil?
		raise MobyDerp::BugError,
		      "root_container_id requested before root container was spawned"
	else
		@root_container_id
	end
end

#root_labelsObject



65
66
67
# File 'lib/moby_derp/pod.rb', line 65

def root_labels
	@config.root_labels
end

#runObject



15
16
17
18
19
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
# File 'lib/moby_derp/pod.rb', line 15

def run
	@logger.info(logloc) { "Checking root container" }
	@root_container_id = root_container.run

	@logger.debug(logloc) { "Root container ID is #{@root_container_id}" }

	desired_container_names = @config.containers.map(&:name)

	Docker::Container.all(all: true).each do |c|
		c_name = c.info["Names"].first.sub(/^\//, '')

		if c.info["Labels"]["org.hezmatt.moby-derp.pod-name"] == name &&
				!c.info["Labels"]["org.hezmatt.moby-derp.root-container-id"].nil? &&
				!desired_container_names.include?(c_name)
			@logger.info(logloc) { "Removing stale container #{c_name}" }
			c.stop
			c.delete
		end
	end

	@config.containers.each do |cfg|
		@logger.info(logloc) { "Checking container #{cfg.name}" }

		begin
			MobyDerp::Container.new(pod: self, container_config: cfg).run
		rescue MobyDerp::ContainerError => ex
			@logger.error(logloc) {
				(["Error while running container #{cfg.name}: #{ex.message}"] + ex.backtrace.map { |l| "  #{l}" }).join("\n")
			}
		end
	end
end