dc 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dc.rb +100 -31
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a1778827651668547f6a5ea1e41c8c29ad99f9675e8ecb0982f16e7fe41838e3
4
- data.tar.gz: aaca72cc50e107ebfd0f6ba6517c1562d8b492217dfef2812048aef658e1c12b
3
+ metadata.gz: d5481799a068360610f3762f574898557affbcac0983d4038060cf708a1f1404
4
+ data.tar.gz: 5397e30c5534fa6d867dd46f3809ecb1c8a3aa1b451831fd7393e99c2797207f
5
5
  SHA512:
6
- metadata.gz: b540e943a2b221b098565ab653a9dc98ad240cc31b9076bb4736c4273ecd271dc301c47eb41d315f81a7caa3cacc05fdee5f97220c736b3e0ecc673b83202c0c
7
- data.tar.gz: fc98f1883b3a9daae99d646f75fd593124d547ad024590e1979078b5f0aef241cecac9198855e072239fb6adef2096406a51208b0cffce5ca407368c3da2d377
6
+ metadata.gz: f5a5fdce3cc139c41ad85045f025cbc56303b9303634a95a559412b9c68bc146c67fef38dc5d0a135fe71fadcc2f46496b60da94416be0bb05f2f3e3fd566ed7
7
+ data.tar.gz: 8797f8a5ffacc10cabae32f0fe2eeb79aa45beb74420443b95ca84f11c13151c8e183a015a407c39887d65aa108a83bb112bfe832480b02f8565b63c888b0295
data/lib/dc.rb CHANGED
@@ -3,6 +3,31 @@ module DC
3
3
  require 'yaml'
4
4
  require 'docker'
5
5
 
6
+ module CLI
7
+ def self.run
8
+ project = Project.new
9
+
10
+ if ARGV.empty?
11
+ project.run
12
+ else
13
+ case ARGV.size
14
+ when 1
15
+ case ARGV.first
16
+ when 'rs'
17
+ when 'ri'
18
+ project.rebuild_images
19
+ end
20
+ when 2
21
+ case ARGV
22
+ when ['rebuild', 'services']
23
+ when ['rebuild', 'images']
24
+ project.rebuild_images
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+
6
31
  class Container
7
32
  def initialize name, ports: []
8
33
  @name = name
@@ -57,6 +82,50 @@ module DC
57
82
  end
58
83
  end
59
84
 
85
+ class Group
86
+ def initialize hash, directory:
87
+ @hash = hash
88
+ @images = hash.keys.map do |name|
89
+ Image.new name, directory: "#{directory}/#{name}"
90
+ end
91
+ end
92
+
93
+ attr_reader :images
94
+
95
+ def containers
96
+ @containers ||= create_containers
97
+ end
98
+
99
+ def build_missing_images
100
+ @images.each do |image|
101
+ image.build unless image.exist?
102
+ end
103
+ end
104
+
105
+ def start
106
+ network = Network.new
107
+ containers.each { |container| network.connect container.id }
108
+ containers.each &:start
109
+ end
110
+
111
+ def destroy
112
+ containers.each &:destroy
113
+ end
114
+
115
+ private
116
+ def create_containers
117
+ @hash.map do |name, value|
118
+ ports = if value.is_a?(Hash) && value['ports'].is_a?(Array)
119
+ value['ports']
120
+ else
121
+ []
122
+ end
123
+
124
+ Container.new name, ports: ports
125
+ end
126
+ end
127
+ end
128
+
60
129
  class Image
61
130
  class << self
62
131
  def all
@@ -93,6 +162,10 @@ module DC
93
162
  image
94
163
  end
95
164
 
165
+ def tag tag
166
+ Docker::Image.get(@name).tag repo: @name, tag: tag
167
+ end
168
+
96
169
  private
97
170
  def build_parent_image_if_available
98
171
  dockerfile = IO.read "#{@directory}/Dockerfile"
@@ -137,17 +210,31 @@ module DC
137
210
  check_if_docker_is_available_at docker_socket
138
211
 
139
212
  @directory = File.expand_path directory
213
+
140
214
  @images_directory = "#{@directory}/.docker/images"
215
+ @images = prepare_images
216
+
141
217
  @services_file = "#{@directory}/.docker/services.yml"
142
218
  end
143
219
 
144
- attr_reader :containers
220
+ attr_reader :images, :group
221
+
222
+ def containers
223
+ group.containers
224
+ end
145
225
 
146
226
  def run
147
- prepare_images
227
+ build_images
148
228
  prepare_services
149
229
  end
150
230
 
231
+ def rebuild_images
232
+ images.each do |image|
233
+ image.tag "before_rebuild.#{Time.now.to_i}"
234
+ image.build
235
+ end
236
+ end
237
+
151
238
  private
152
239
  def check_if_docker_is_available_at docker_socket
153
240
  Docker.url = docker_socket
@@ -157,43 +244,25 @@ module DC
157
244
 
158
245
  def prepare_images
159
246
  if Dir.exist? @images_directory
160
- images = (Dir.entries(@images_directory) - ['.', '..']).map do |name|
247
+ (Dir.entries(@images_directory) - ['.', '..']).map do |name|
161
248
  Image.new name, directory: "#{@images_directory}/#{name}"
162
249
  end
250
+ else
251
+ []
252
+ end
253
+ end
163
254
 
164
- images.each do |image|
165
- image.build unless image.exist?
166
- end
255
+ def build_images
256
+ images.each do |image|
257
+ image.build unless image.exist?
167
258
  end
168
259
  end
169
260
 
170
261
  def prepare_services
171
262
  if File.exist? @services_file
172
- hash = YAML.load_file @services_file
173
-
174
- images = hash.keys.map do |name|
175
- Image.new name, directory: "#{@directory}/#{name}"
176
- end
177
-
178
- images.each do |image|
179
- image.build unless image.exist?
180
- end
181
-
182
- @containers = hash.map do |name, value|
183
- ports = if value.is_a?(Hash) && value['ports'].is_a?(Array)
184
- value['ports']
185
- else
186
- []
187
- end
188
-
189
- Container.new name, ports: ports
190
- end
191
-
192
- network = Network.new
193
- @containers.each { |container| network.connect container.id }
194
-
195
- @containers.each &:start
196
- at_exit { @containers.each &:destroy }
263
+ @group = Group.new (YAML.load_file @services_file), directory: @directory
264
+ @group.build_missing_images
265
+ @group.start
197
266
  end
198
267
  end
199
268
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anatoly Chernow