Class: Brick::Models::Service

Inherits:
Object
  • Object
show all
Includes:
Brick::Mixin::Colors
Defined in:
lib/brick/models/service.rb

Constant Summary collapse

@@waiting_pool =
[]

Constants included from Brick::Mixin::Colors

Brick::Mixin::Colors::COLORS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#clientObject

Returns the value of attribute client.



13
14
15
# File 'lib/brick/models/service.rb', line 13

def client
  @client
end

#containerObject

Returns the value of attribute container.



13
14
15
# File 'lib/brick/models/service.rb', line 13

def container
  @container
end

#imageObject

Returns the value of attribute image.



13
14
15
# File 'lib/brick/models/service.rb', line 13

def image
  @image
end

#image_nameObject

Returns the value of attribute image_name.



13
14
15
# File 'lib/brick/models/service.rb', line 13

def image_name
  @image_name
end

Returns the value of attribute links.



13
14
15
# File 'lib/brick/models/service.rb', line 13

def links
  @links
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/brick/models/service.rb', line 13

def name
  @name
end

#service_config_hashObject

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_fromObject

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_deamonObject



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

#attachObject



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){|message| 
      
    if message.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

Returns:

  • (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

Returns:

  • (Boolean)


233
234
235
# File 'lib/brick/models/service.rb', line 233

def can_be_skipped_this_time?
  @skip == true
end

#container_exist?Boolean

Returns:

  • (Boolean)


260
261
262
# File 'lib/brick/models/service.rb', line 260

def container_exist?
  ::Docker::Container.exist?(name)
end

#container_infoObject



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, options ={}
  if self.container.nil?
    raise "no container #{name} running, so we can't execute "
  end
  
  self.container.exec(cmd_array, options){|stream, chunk| puts "#{color_generator(name)} | #{chunk}".chomp }
  
end

#image_exist?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


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_timeObject



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


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