docker_brick 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 871b125062a6423b2cc1cb71a460fd37c57a61d8
4
- data.tar.gz: a8f96cafd7c996a4511a5109178e1edcb4e51d54
3
+ metadata.gz: 7be165a0c80fa76a8c372fc04ffb9d5599cc9c5a
4
+ data.tar.gz: 636631ac56fe6d09d1afd61b1390f2517b2dbf21
5
5
  SHA512:
6
- metadata.gz: e878625bf45f7adb55cd08ad54a96a133280849dae024fc4ef791181b4be871bea9da50e0d7672fceb7ccbed47cdb6055ee0d3ad8af3f05bf6be5373e51faf83
7
- data.tar.gz: e9e3b8a58c6b966ab1016457dbf62d12fe2966c8e754fcea27a5ea32b474e8a66c685def0c568825a2a7046cd846608c3b31df60a6aba993bda73329352de514
6
+ metadata.gz: 7905112b0ee861638125c5a106b836c0fddf8fdeed9286b86506a9d3f07c47cd7c6bfec6485714db2c1abe39bc291ef6555725e10504ce62dbd1825d581a72f4
7
+ data.tar.gz: 01480b8c8af8814e15c8edf2df0ced8cdf62e948da563d6fea4f49d79604ad23dff5b232b03583ecde6857cbe5f36b3cce4b3f6b7368ab67f47195cebbd95078
@@ -1,276 +1,276 @@
1
- require 'brick/mixin'
2
- require 'colorize'
3
-
4
-
5
- module Brick
6
- module Models
7
- class Service
8
-
9
- include ::Brick::Mixin::Colors
10
-
11
- @@waiting_pool = []
12
-
13
- attr_accessor :client, :name, :links, :service_config_hash, :container, :volumes_from, :image, :image_name
14
-
15
- def self.wait_for_deamon
16
- @@waiting_pool.each{|thr|
17
- thr.join
18
- }
19
- end
20
-
21
-
22
- def initialize(name, config, client)
23
- self.name = name
24
- self.service_config_hash = config
25
- self.client = client
26
- #puts "client=#{client}"
27
-
28
- determine_color
29
-
30
- unless config["links"].nil?
31
- if config["links"].instance_of?(String)
32
- self.links= [config["links"]]
33
- else
34
- self.links= config["links"].dup
35
- end
36
- end
37
-
38
- unless config["volumes_from"].nil?
39
- if config["volumes_from"].instance_of?(String)
40
- self.volumes_from= [config["volumes_from"]]
41
- else
42
- self.volumes_from= config["volumes_from"].dup
43
- end
44
- end
45
-
46
- begin
47
- self.container = ::Docker::Container.get(name)
48
- rescue
49
- self.container = nil
50
- end
51
-
52
- end
53
-
54
- def update_volumes_from services
55
-
56
- new_volumes_from_config = []
57
-
58
- new_volumes_from = []
59
-
60
- unless volumes_from.nil?
61
-
62
- volumes_from.each {|vo|
63
- #new_volumes_from << services[vo]
64
- vo_parts = vo.split(':')
65
-
66
- #only one part
67
- if vo_parts.size == 1
68
- new_vo = "#{services[vo_parts[0]].name}:rw"
69
- else
70
- new_vo= "#{services[vo_parts[0]].name}:#{vo_parts[1]}"
71
- end
72
-
73
- new_volumes_from<< services[vo_parts[0]]
74
-
75
- new_volumes_from_config << new_vo
76
-
77
- }
78
- self.volumes_from = new_volumes_from
79
-
80
- service_config_hash["volumes_from"] = new_volumes_from_config
81
- end
82
- end
83
-
84
-
85
- def update_links services
86
-
87
- new_links_config = []
88
-
89
- new_links =[]
90
-
91
- unless links.nil?
92
- links.each{|link|
93
-
94
- link_array=link.split(':')
95
-
96
- #It's for getting the real service name
97
- service_key = link_array[0]
98
-
99
- alias_name = link_array[-1]
100
-
101
- service_container= services[service_key]
102
-
103
- new_links << service_container
104
-
105
- new_links_config << "#{service_container.name}:#{alias_name}"
106
- }
107
-
108
- self.links=new_links
109
-
110
- service_config_hash["links"] = new_links_config
111
- end
112
-
113
-
114
- end
115
-
116
- def exec cmd_array, options ={}
117
- if self.container.nil?
118
- raise "no container #{name} running, so we can't execute "
119
- end
120
-
121
- self.container.exec(cmd_array, options){|stream, chunk| puts "#{color_generator(name)} | #{chunk}".chomp }
122
-
123
- end
124
-
125
- #equals to "docker run"
126
- def run enable_link=true, recreate=true, detach_mode=false
127
-
128
- if running? and (!recreate or can_be_skipped_this_time?)
129
- Brick::CLI::logger.debug "the service #{name} is already running. exited."
130
- unless detach_mode
131
- attach
132
- end
133
-
134
- return
135
- end
136
-
137
- unless volumes_from.nil?
138
- volumes_from.each{|vo| vo.run enable_link}
139
- end
140
-
141
- config_hash = @service_config_hash.dup
142
-
143
- if enable_link and !links.nil?
144
- links.each{|linked_service|
145
- linked_service.run enable_link
146
-
147
- unless detach_mode
148
- linked_service.attach
149
- else
150
- puts "Service #{linked_service.name} has been started"
151
- end
152
- }
153
- end
154
-
155
- if !enable_link
156
- config_hash.delete("links")
157
- end
158
-
159
- if recreate and !container.nil?
160
- #if recreate is true, it will destory the old container, and create a new one
161
- if running?
162
- container.stop
163
- end
164
- container.delete(:force => true)
165
- self.container=nil
166
- skip_next_time
167
- end
168
-
169
- if container.nil?
170
- self.container = client.run config_hash, name
171
- else
172
- container.start
173
- end
174
-
175
- unless detach_mode
176
- attach
177
- else
178
- puts "Service #{name} has been started"
179
- end
180
-
181
- end
182
-
183
- #Check if the container is running
184
- def running?
185
- is_running = false
186
-
187
- if container.nil?
188
- begin
189
- self.container = ::Docker::Container.get(name)
190
- rescue
191
- self.container = nil
192
- end
193
- end
194
-
195
- unless container.nil?
196
- is_running = container.is_running?
197
- end
198
-
199
- is_running
200
- end
201
-
202
- def container_info
203
- (client.get_container_by_id(container.id)).info rescue {}
204
- end
205
-
206
-
207
- def attach
208
-
209
- thr=Thread.new{
210
- puts "Attaching to service #{name}"
211
- container.attach(:stdin => STDIN, :tty => true){|message|
212
-
213
- if message.length > 0
214
- puts "#{color_generator(name)} | #{message}".chomp
215
- end
216
-
217
- }
218
- }
219
-
220
- #thr.join
221
- @@waiting_pool << thr
222
- end
223
-
224
-
225
- def can_be_built?
226
- !service_config_hash["build"].nil?
227
- end
228
-
229
- def skip_next_time
230
- @skip = true
231
- end
232
-
233
- def can_be_skipped_this_time?
234
- @skip == true
235
- end
236
-
237
- def build name=nil, no_cache=false, project_dir=nil
238
-
239
- if name.nil?
240
- name = self.image_name
241
- end
242
-
243
- puts "Start building #{name}..."
244
-
245
- if can_be_built?
246
- self.image = client.build_from_dir({:image_name => name,
247
- :no_cache => no_cache,
248
- :project_dir=>project_dir,
249
- :build_dir=>service_config_hash["build"]})
250
- else
251
- Brick::CLI::logger.debug "no build defintion for #{image_build},skip it"
252
- end
253
- self.image
254
- end
255
-
256
- def image_exist?
257
- ::Docker::Image.exist?(image_name)
258
- end
259
-
260
- def container_exist?
261
- ::Docker::Container.exist?(name)
262
- end
263
-
264
- #If it's using build tag, will create an actual image name for it.
265
- #For example, if project name is test, service name is web, the image name should
266
- #be test_web
267
- def update_image_for_building_tag name
268
- unless service_config_hash["build"].nil?
269
- service_config_hash["image"]=name
270
- end
271
-
272
- self.image_name = service_config_hash["image"]
273
- end
274
- end
275
- end
276
- end
1
+ require 'brick/mixin'
2
+ require 'colorize'
3
+
4
+
5
+ module Brick
6
+ module Models
7
+ class Service
8
+
9
+ include ::Brick::Mixin::Colors
10
+
11
+ @@waiting_pool = []
12
+
13
+ attr_accessor :client, :name, :links, :service_config_hash, :container, :volumes_from, :image, :image_name
14
+
15
+ def self.wait_for_deamon
16
+ @@waiting_pool.each{|thr|
17
+ thr.join
18
+ }
19
+ end
20
+
21
+
22
+ def initialize(name, config, client)
23
+ self.name = name
24
+ self.service_config_hash = config
25
+ self.client = client
26
+ #puts "client=#{client}"
27
+
28
+ determine_color
29
+
30
+ unless config["links"].nil?
31
+ if config["links"].instance_of?(String)
32
+ self.links= [config["links"]]
33
+ else
34
+ self.links= config["links"].dup
35
+ end
36
+ end
37
+
38
+ unless config["volumes_from"].nil?
39
+ if config["volumes_from"].instance_of?(String)
40
+ self.volumes_from= [config["volumes_from"]]
41
+ else
42
+ self.volumes_from= config["volumes_from"].dup
43
+ end
44
+ end
45
+
46
+ begin
47
+ self.container = ::Docker::Container.get(name)
48
+ rescue
49
+ self.container = nil
50
+ end
51
+
52
+ end
53
+
54
+ def update_volumes_from services
55
+
56
+ new_volumes_from_config = []
57
+
58
+ new_volumes_from = []
59
+
60
+ unless volumes_from.nil?
61
+
62
+ volumes_from.each {|vo|
63
+ #new_volumes_from << services[vo]
64
+ vo_parts = vo.split(':')
65
+
66
+ #only one part
67
+ if vo_parts.size == 1
68
+ new_vo = "#{services[vo_parts[0]].name}:rw"
69
+ else
70
+ new_vo= "#{services[vo_parts[0]].name}:#{vo_parts[1]}"
71
+ end
72
+
73
+ new_volumes_from<< services[vo_parts[0]]
74
+
75
+ new_volumes_from_config << new_vo
76
+
77
+ }
78
+ self.volumes_from = new_volumes_from
79
+
80
+ service_config_hash["volumes_from"] = new_volumes_from_config
81
+ end
82
+ end
83
+
84
+
85
+ def update_links services
86
+
87
+ new_links_config = []
88
+
89
+ new_links =[]
90
+
91
+ unless links.nil?
92
+ links.each{|link|
93
+
94
+ link_array=link.split(':')
95
+
96
+ #It's for getting the real service name
97
+ service_key = link_array[0]
98
+
99
+ alias_name = link_array[-1]
100
+
101
+ service_container= services[service_key]
102
+
103
+ new_links << service_container
104
+
105
+ new_links_config << "#{service_container.name}:#{alias_name}"
106
+ }
107
+
108
+ self.links=new_links
109
+
110
+ service_config_hash["links"] = new_links_config
111
+ end
112
+
113
+
114
+ end
115
+
116
+ def exec cmd_array, options ={}
117
+ if self.container.nil?
118
+ raise "no container #{name} running, so we can't execute "
119
+ end
120
+
121
+ self.container.exec(cmd_array, options){|stream, chunk| puts "#{color_generator(name)} | #{chunk}".chomp }
122
+
123
+ end
124
+
125
+ #equals to "docker run"
126
+ def run enable_link=true, recreate=true, detach_mode=false
127
+
128
+ if running? and (!recreate or can_be_skipped_this_time?)
129
+ Brick::CLI::logger.debug "the service #{name} is already running. exited."
130
+ unless detach_mode
131
+ attach
132
+ end
133
+
134
+ return
135
+ end
136
+
137
+ unless volumes_from.nil?
138
+ volumes_from.each{|vo| vo.run enable_link}
139
+ end
140
+
141
+ config_hash = @service_config_hash.dup
142
+
143
+ if enable_link and !links.nil?
144
+ links.each{|linked_service|
145
+ linked_service.run enable_link, recreate, detach_mode
146
+
147
+ unless detach_mode
148
+ linked_service.attach
149
+ else
150
+ puts "Service #{linked_service.name} has been started"
151
+ end
152
+ }
153
+ end
154
+
155
+ if !enable_link
156
+ config_hash.delete("links")
157
+ end
158
+
159
+ if recreate and !container.nil?
160
+ #if recreate is true, it will destory the old container, and create a new one
161
+ if running?
162
+ container.stop
163
+ end
164
+ container.delete(:force => true)
165
+ self.container=nil
166
+ skip_next_time
167
+ end
168
+
169
+ if container.nil?
170
+ self.container = client.run config_hash, name
171
+ else
172
+ container.start
173
+ end
174
+
175
+ unless detach_mode
176
+ attach
177
+ else
178
+ puts "Service #{name} has been started"
179
+ end
180
+
181
+ end
182
+
183
+ #Check if the container is running
184
+ def running?
185
+ is_running = false
186
+
187
+ if container.nil?
188
+ begin
189
+ self.container = ::Docker::Container.get(name)
190
+ rescue
191
+ self.container = nil
192
+ end
193
+ end
194
+
195
+ unless container.nil?
196
+ is_running = container.is_running?
197
+ end
198
+
199
+ is_running
200
+ end
201
+
202
+ def container_info
203
+ (client.get_container_by_id(container.id)).info rescue {}
204
+ end
205
+
206
+
207
+ def attach
208
+
209
+ thr=Thread.new{
210
+ puts "Attaching to service #{name}"
211
+ container.attach(:stdin => STDIN, :tty => true){|message|
212
+
213
+ if message.length > 0
214
+ puts "#{color_generator(name)} | #{message}".chomp
215
+ end
216
+
217
+ }
218
+ }
219
+
220
+ #thr.join
221
+ @@waiting_pool << thr
222
+ end
223
+
224
+
225
+ def can_be_built?
226
+ !service_config_hash["build"].nil?
227
+ end
228
+
229
+ def skip_next_time
230
+ @skip = true
231
+ end
232
+
233
+ def can_be_skipped_this_time?
234
+ @skip == true
235
+ end
236
+
237
+ def build name=nil, no_cache=false, project_dir=nil
238
+
239
+ if name.nil?
240
+ name = self.image_name
241
+ end
242
+
243
+ puts "Start building #{name}..."
244
+
245
+ if can_be_built?
246
+ self.image = client.build_from_dir({:image_name => name,
247
+ :no_cache => no_cache,
248
+ :project_dir=>project_dir,
249
+ :build_dir=>service_config_hash["build"]})
250
+ else
251
+ Brick::CLI::logger.debug "no build defintion for #{image_build},skip it"
252
+ end
253
+ self.image
254
+ end
255
+
256
+ def image_exist?
257
+ ::Docker::Image.exist?(image_name)
258
+ end
259
+
260
+ def container_exist?
261
+ ::Docker::Container.exist?(name)
262
+ end
263
+
264
+ #If it's using build tag, will create an actual image name for it.
265
+ #For example, if project name is test, service name is web, the image name should
266
+ #be test_web
267
+ def update_image_for_building_tag name
268
+ unless service_config_hash["build"].nil?
269
+ service_config_hash["image"]=name
270
+ end
271
+
272
+ self.image_name = service_config_hash["image"]
273
+ end
274
+ end
275
+ end
276
+ end
data/lib/brick/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Brick
2
2
  BRICK_ROOT = File.dirname(File.expand_path(File.dirname(__FILE__)))
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docker_brick
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - cheyang