Skip to content

Commit 6ae0bd3

Browse files
committed
Add container search from docker daemon
1 parent 8842739 commit 6ae0bd3

2 files changed

Lines changed: 30 additions & 18 deletions

File tree

lib/sagittarius/orchestrator/operator.rb

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@ module Sagittarius
44
module Orchestrator
55
class Operator
66
ORCHESTRATOR_LABEL_PREFIX = 'tech.code0.sagittarius.orchestrator'
7+
CONTAINER_NAME_LABEL = "#{ORCHESTRATOR_LABEL_PREFIX}.container.name".freeze
8+
NETWORK_NAME_LABEL = "#{ORCHESTRATOR_LABEL_PREFIX}.network.name".freeze
79
VOLUME_NAME_LABEL = "#{ORCHESTRATOR_LABEL_PREFIX}.volume.name".freeze
810
VOLUME_CONTAINER_LABEL = "#{ORCHESTRATOR_LABEL_PREFIX}.volume.container".freeze
911

12+
NoContainerError = Class.new(StandardError)
13+
1014
class << self
1115
def ensure_self_connected!
1216
ensure_network!
1317

1418
self_container_id = State.self_container_id
15-
return if self_container_id.nil?
19+
raise NoContainerError, 'self_container_id is nil' if self_container_id.nil?
1620

17-
network.connect(State.self_container_id)
21+
network.connect(self_container_id)
1822
end
1923

2024
def ensure_container_up!(container)
@@ -33,27 +37,27 @@ def ensure_container_down!(container)
3337
destroy_container!(container) unless State[container.name].internal_container.nil?
3438
end
3539

36-
# private
37-
38-
def ensure_network!
39-
return unless network.nil?
40-
41-
Docker::Network.create(
42-
unique_name('code0'),
43-
'Labels' => { ORCHESTRATOR_LABEL_PREFIX => 'network' }
44-
)
45-
end
46-
4740
def network
4841
network_id = Docker::Network.all(filters: JSON.dump(
49-
{ 'label' => ["#{ORCHESTRATOR_LABEL_PREFIX}=network"] }
42+
{ 'label' => ["#{NETWORK_NAME_LABEL}=main"] }
5043
)).first&.id
5144

5245
return nil if network_id.nil?
5346

5447
Docker::Network.get(network_id)
5548
end
5649

50+
private
51+
52+
def ensure_network!
53+
return unless network.nil?
54+
55+
Docker::Network.create(
56+
unique_name('code0'),
57+
'Labels' => { NETWORK_NAME_LABEL => 'main' }
58+
)
59+
end
60+
5761
def ensure_volumes!(container)
5862
container.volumes.each_key do |name|
5963
next if State.volumes[container.name]&.key?(name.to_s)
@@ -77,7 +81,7 @@ def create_container!(container)
7781
container.internal_container = Docker::Container.create(
7882
'name' => unique_name(container.name),
7983
'Image' => container.image,
80-
'Labels' => { ORCHESTRATOR_LABEL_PREFIX => container.name },
84+
'Labels' => { CONTAINER_NAME_LABEL => container.name },
8185
'Cmd' => container.cmd,
8286
'Env' => container.environment_variables,
8387
'HostConfig' => {

lib/sagittarius/orchestrator/state.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ def build!
1414
def build_containers!
1515
docker_containers = Docker::Container.all(
1616
all: true,
17-
filters: JSON.dump({ 'label' => [Operator::ORCHESTRATOR_LABEL_PREFIX] })
17+
filters: JSON.dump({ 'label' => [Operator::CONTAINER_NAME_LABEL] })
1818
).to_h do |c|
19-
label = c.info['Labels'][Operator::ORCHESTRATOR_LABEL_PREFIX]
19+
label = c.info['Labels'][Operator::CONTAINER_NAME_LABEL]
2020
next [nil, nil] if label.nil?
2121

2222
[label, c]
@@ -47,7 +47,7 @@ def [](container_name)
4747
end
4848

4949
def self_container_id
50-
container_id_from_cgroup
50+
container_id_from_cgroup || container_id_from_daemon_search
5151
end
5252

5353
private
@@ -57,6 +57,14 @@ def container_id_from_cgroup
5757
rescue Errno::ENOENT
5858
nil
5959
end
60+
61+
def container_id_from_daemon_search
62+
Docker::Container.all
63+
.filter { |c| c.info['Image'].include?('sagittarius') }
64+
.reject { |c| c.info['Labels'].key?(Operator::CONTAINER_NAME_LABEL) }
65+
.find { |c| c.info['Command'] == '/rails/bin/docker-entrypoint ./bin/rails server' }
66+
&.id
67+
end
6068
end
6169
end
6270
end

0 commit comments

Comments
 (0)