This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
compose.rb
132 lines (110 loc) · 2.95 KB
/
compose.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
126
127
128
129
130
131
132
require 'docker'
require_relative 'compose_port'
require_relative '../utils/compose_utils'
class Compose
attr_reader :containers
def initialize
@containers = {}
end
public
#
# Add a new container to compose
#
def add_container(container)
# Avoid duplicated labels on compose
while @containers.has_key?(container.attributes[:label]) do
container.attributes[:label].succ!
end
@containers[container.attributes[:label]] = container
true
end
#
# Select containers based on attributes given by "params"
#
def get_containers_by(params)
@containers.values.select do |container|
(params.to_a - container.attributes.to_a).empty?
end
end
#
# Select containers based on its given name
# (ignore basename)
#
def get_containers_by_given_name(given_name)
@containers.select { |label, container|
container.attributes[:name].match(/#{ComposeUtils.dir_name}_#{given_name}_\d+/)
}.values
end
#
# Create link relations among containers
#
def link_containers
@containers.each_value do |container|
links = container.attributes[:links]
next if (container.loaded_from_environment? or links.nil?)
links.each do |service, label|
dependency_container = @containers[service]
container.add_dependency(dependency_container)
end
end
end
#
# Start a container
#
# This method accepts an array of labels.
# If labels is informed, only those containers with label present in array will be started.
# Otherwise, all containers are started
#
def start(labels = [])
call_container_method(:start, labels)
end
#
# Stop a container
#
# This method accepts an array of labels.
# If labels is informed, only those containers with label present in array will be stopped.
# Otherwise, all containers are stopped
#
def stop(labels = [])
call_container_method(:stop, labels)
end
#
# Kill a container
#
# This method accepts an array of labels.
# If labels is informed, only those containers with label present in array will be killed.
# Otherwise, all containers are killed
#
def kill(labels = [])
call_container_method(:kill, labels)
end
#
# Delete a container
#
# This method accepts an array of labels.
# If labels is informed, only those containers with label present in array will be deleted.
# Otherwise, all containers are deleted
#
def delete(labels = [])
call_container_method(:delete, labels)
delete_containers_entries(labels)
end
private
def call_container_method(method, labels = [])
labels = @containers.keys if labels.empty?
containers = @containers.select { |key, value|
labels.include?(key)
}
containers.values.each do |entry|
entry.send(method)
end
true
end
def delete_containers_entries(labels = [])
labels = @containers.keys if labels.empty?
labels.each do |label|
@containers.delete(label)
end
true
end
end