Class: Brick::Models::Service
- Inherits:
-
Object
- Object
- Brick::Models::Service
- Includes:
- Brick::Mixin::Colors
- Defined in:
- lib/brick/models/service.rb
Constant Summary collapse
- @@waiting_pool =
[]
Constants included from Brick::Mixin::Colors
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#container ⇒ Object
Returns the value of attribute container.
-
#image ⇒ Object
Returns the value of attribute image.
-
#image_name ⇒ Object
Returns the value of attribute image_name.
-
#links ⇒ Object
Returns the value of attribute links.
-
#name ⇒ Object
Returns the value of attribute name.
-
#service_config_hash ⇒ Object
Returns the value of attribute service_config_hash.
-
#volumes_from ⇒ Object
Returns the value of attribute volumes_from.
Class Method Summary collapse
Instance Method Summary collapse
- #attach ⇒ Object
- #build(name = nil, no_cache = false, project_dir = nil) ⇒ Object
- #can_be_built? ⇒ Boolean
- #can_be_skipped_this_time? ⇒ Boolean
- #container_exist? ⇒ Boolean
- #container_info ⇒ Object
- #exec(cmd_array, options = {}) ⇒ Object
- #image_exist? ⇒ Boolean
-
#initialize(name, config, client) ⇒ Service
constructor
A new instance of Service.
-
#run(enable_link = true, recreate = true, detach_mode = false) ⇒ Object
equals to “docker run”.
-
#running? ⇒ Boolean
Check if the container is running.
- #skip_next_time ⇒ Object
-
#update_image_for_building_tag(name) ⇒ Object
If it’s using build tag, will create an actual image name for it.
- #update_links(services) ⇒ Object
- #update_volumes_from(services) ⇒ Object
Methods included from Brick::Mixin::Colors
#color_generator, #determine_color
Constructor Details
#initialize(name, config, client) ⇒ Service
Returns a new instance of Service.
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 |
# File 'lib/brick/models/service.rb', line 22 def initialize(name, config, client) self.name = name self.service_config_hash = config self.client = client #puts "client=#{client}" determine_color unless config["links"].nil? if config["links"].instance_of?(String) self.links= [config["links"]] else self.links= config["links"].dup end end unless config["volumes_from"].nil? if config["volumes_from"].instance_of?(String) self.volumes_from= [config["volumes_from"]] else self.volumes_from= config["volumes_from"].dup end end begin self.container = ::Docker::Container.get(name) rescue self.container = nil end end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
13 14 15 |
# File 'lib/brick/models/service.rb', line 13 def client @client end |
#container ⇒ Object
Returns the value of attribute container.
13 14 15 |
# File 'lib/brick/models/service.rb', line 13 def container @container end |
#image ⇒ Object
Returns the value of attribute image.
13 14 15 |
# File 'lib/brick/models/service.rb', line 13 def image @image end |
#image_name ⇒ Object
Returns the value of attribute image_name.
13 14 15 |
# File 'lib/brick/models/service.rb', line 13 def image_name @image_name end |
#links ⇒ Object
Returns the value of attribute links.
13 14 15 |
# File 'lib/brick/models/service.rb', line 13 def links @links end |
#name ⇒ Object
Returns the value of attribute name.
13 14 15 |
# File 'lib/brick/models/service.rb', line 13 def name @name end |
#service_config_hash ⇒ Object
Returns the value of attribute service_config_hash.
13 14 15 |
# File 'lib/brick/models/service.rb', line 13 def service_config_hash @service_config_hash end |
#volumes_from ⇒ Object
Returns the value of attribute volumes_from.
13 14 15 |
# File 'lib/brick/models/service.rb', line 13 def volumes_from @volumes_from end |
Class Method Details
.wait_for_deamon ⇒ Object
15 16 17 18 19 |
# File 'lib/brick/models/service.rb', line 15 def self.wait_for_deamon @@waiting_pool.each{|thr| thr.join } end |
Instance Method Details
#attach ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/brick/models/service.rb', line 207 def attach thr=Thread.new{ puts "Attaching to service #{name}" container.attach(:stdin => STDIN, :tty => true){|| if .length > 0 puts "#{color_generator(name)} | #{message}".chomp end } } #thr.join @@waiting_pool << thr end |
#build(name = nil, no_cache = false, project_dir = nil) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/brick/models/service.rb', line 237 def build name=nil, no_cache=false, project_dir=nil if name.nil? name = self.image_name end puts "Start building #{name}..." if can_be_built? self.image = client.build_from_dir({:image_name => name, :no_cache => no_cache, :project_dir=>project_dir, :build_dir=>service_config_hash["build"]}) else Brick::CLI::logger.debug "no build defintion for #{image_build},skip it" end self.image end |
#can_be_built? ⇒ Boolean
225 226 227 |
# File 'lib/brick/models/service.rb', line 225 def can_be_built? !service_config_hash["build"].nil? end |
#can_be_skipped_this_time? ⇒ Boolean
233 234 235 |
# File 'lib/brick/models/service.rb', line 233 def can_be_skipped_this_time? @skip == true end |
#container_exist? ⇒ Boolean
260 261 262 |
# File 'lib/brick/models/service.rb', line 260 def container_exist? ::Docker::Container.exist?(name) end |
#container_info ⇒ Object
202 203 204 |
# File 'lib/brick/models/service.rb', line 202 def container_info (client.get_container_by_id(container.id)).info rescue {} end |
#exec(cmd_array, options = {}) ⇒ Object
116 117 118 119 120 121 122 123 |
# File 'lib/brick/models/service.rb', line 116 def exec cmd_array, ={} if self.container.nil? raise "no container #{name} running, so we can't execute " end self.container.exec(cmd_array, ){|stream, chunk| puts "#{color_generator(name)} | #{chunk}".chomp } end |
#image_exist? ⇒ Boolean
256 257 258 |
# File 'lib/brick/models/service.rb', line 256 def image_exist? ::Docker::Image.exist?(image_name) end |
#run(enable_link = true, recreate = true, detach_mode = false) ⇒ Object
equals to “docker run”
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/brick/models/service.rb', line 126 def run enable_link=true, recreate=true, detach_mode=false if running? and (!recreate or can_be_skipped_this_time?) Brick::CLI::logger.debug "the service #{name} is already running. exited." unless detach_mode attach end return end unless volumes_from.nil? volumes_from.each{|vo| vo.run enable_link} end config_hash = @service_config_hash.dup if enable_link and !links.nil? links.each{|linked_service| linked_service.run enable_link unless detach_mode linked_service.attach else puts "Service #{linked_service.name} has been started" end } end if !enable_link config_hash.delete("links") end if recreate and !container.nil? #if recreate is true, it will destory the old container, and create a new one if running? container.stop end container.delete(:force => true) self.container=nil skip_next_time end if container.nil? self.container = client.run config_hash, name else container.start end unless detach_mode attach else puts "Service #{name} has been started" end end |
#running? ⇒ Boolean
Check if the container is running
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/brick/models/service.rb', line 184 def running? is_running = false if container.nil? begin self.container = ::Docker::Container.get(name) rescue self.container = nil end end unless container.nil? is_running = container.is_running? end is_running end |
#skip_next_time ⇒ Object
229 230 231 |
# File 'lib/brick/models/service.rb', line 229 def skip_next_time @skip = true end |
#update_image_for_building_tag(name) ⇒ Object
If it’s using build tag, will create an actual image name for it. For example, if project name is test, service name is web, the image name should be test_web
267 268 269 270 271 272 273 |
# File 'lib/brick/models/service.rb', line 267 def update_image_for_building_tag name unless service_config_hash["build"].nil? service_config_hash["image"]=name end self.image_name = service_config_hash["image"] end |
#update_links(services) ⇒ Object
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 |
# File 'lib/brick/models/service.rb', line 85 def update_links services new_links_config = [] new_links =[] unless links.nil? links.each{|link| link_array=link.split(':') #It's for getting the real service name service_key = link_array[0] alias_name = link_array[-1] service_container= services[service_key] new_links << service_container new_links_config << "#{service_container.name}:#{alias_name}" } self.links=new_links service_config_hash["links"] = new_links_config end end |
#update_volumes_from(services) ⇒ Object
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 |
# File 'lib/brick/models/service.rb', line 54 def update_volumes_from services new_volumes_from_config = [] new_volumes_from = [] unless volumes_from.nil? volumes_from.each {|vo| #new_volumes_from << services[vo] vo_parts = vo.split(':') #only one part if vo_parts.size == 1 new_vo = "#{services[vo_parts[0]].name}:rw" else new_vo= "#{services[vo_parts[0]].name}:#{vo_parts[1]}" end new_volumes_from<< services[vo_parts[0]] new_volumes_from_config << new_vo } self.volumes_from = new_volumes_from service_config_hash["volumes_from"] = new_volumes_from_config end end |