Class: EzPaaS::Helpers::ContainerManager

Inherits:
Object
  • Object
show all
Defined in:
lib/ezpaas/helpers/container_manager.rb

Instance Method Summary collapse

Constructor Details

#initializeContainerManager

Returns a new instance of ContainerManager.



18
19
20
# File 'lib/ezpaas/helpers/container_manager.rb', line 18

def initialize
  ensure_paths
end

Instance Method Details

#create_slug(src_tar_stream, emitter = nil) ⇒ Object



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
# File 'lib/ezpaas/helpers/container_manager.rb', line 22

def create_slug(src_tar_stream, emitter = nil)

temp_tar = Tempfile.new('ezpaas') # Temporary place to copy the slug tarball

emitter.emit :message, '-----> Creating slug compilation container' if emitter
container = Docker::Container.create({'Image' => 'deis/slugbuilder', 'OpenStdin' => true, 'StdinOnce' => true}, connection)

begin
  # Start the container
  emitter.emit :message, '-----> Starting slug compilation container' if emitter
  container.start

  # Redirect container output to HTTP stream
  container.attach(stdin: src_tar_stream) do |_fd, chunk|
    emitter.emit :message, chunk if emitter
  end

  # Wait for container to finish compiling the slug
  container.wait

  # Kill the container
  emitter.emit :message, '-----> Stopping slug compilation container' if emitter
  begin
    container.stop
  end

  # Copy the tarred slug out of the container
  emitter.emit :message, "-----> Temporarily copying slug from container to #{temp_tar.path}" if emitter
  container.copy('/tmp/slug.tgz') { |chunk| temp_tar.write(chunk) }

  temp_tar.rewind # rewind the temp file for immediate reading

  # Decompress the tarred slug to our final destination
  slug_name = SecureRandom.uuid
  slug_destination = slug_path(slug_name)
  emitter.emit :message, "-----> Untarring slug to #{slug_destination}" if emitter
  tar_extract = Gem::Package::TarReader.new(temp_tar)
  slug_entry = tar_extract.find { |e| e.full_name == 'slug.tgz' }
  File.open(slug_destination, 'wb') do |file|
    File.ez_cp(slug_entry, file)
  end
  temp_tar.close
rescue Exception => ex
  raise
else
  slug_name
  ensure
    emitter.emit :message, "-----> Removing container" if emitter
    # Try to remove the container
    begin
      container.remove
    rescue
      raise
    end
    emitter.emit :message, "-----> Deleting temporary files" if emitter
    temp_tar.close
    temp_tar.unlink
  end

end

#deploy_app(name, slug, config, emitter = nil) ⇒ Object



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
# File 'lib/ezpaas/helpers/container_manager.rb', line 83

def deploy_app(name, slug, config, emitter = nil)

  emitter.emit :message, "-----> Creating temporary slug container to snapshot" if emitter

  # Load the slug into a temporary container to produce an image
  slug_labels = {
    'com.tendigi.appname' => name,
  }

  slug_file = File.open(slug_path(slug), 'rb')
  slug_container = Docker::Container.create({'Image' => 'deis/slugrunner', 'OpenStdin' => true, 'StdinOnce' => true, 'Labels' => slug_labels}, connection)
  slug_container.start
  slug_container.attach(stdin: slug_file) do |_fd, chunk|
    # puts chunk
  end

  slug_image_name = "ezpass/#{name}"

  emitter.emit :message, "-----> Imaging container #{slug_container.id}" if emitter
  slug_image = slug_container.commit(repo: slug_image_name, 'Labels' => slug_labels)
  emitter.emit :message, "-----> Created slug image #{slug_image.id}" if emitter

  slug_container.remove(force: true)
  emitter.emit :message, "-----> Removing temporary slug container #{slug_container.id}" if emitter

  emitter.emit :message, "-----> Starting deployment..." if emitter

  # Start up instances using that image
  config.each do |process, count|

    # iterate over the desired number of instances
    for i in 0 ... count
      container_name = "#{name}-#{process}-#{i}"
      labels = {
        'com.tendigi.appname' => name,
        'com.tendigi.instance' => container_name,
        'com.tendigi.slug' => slug,
        'com.tendigi.process' => process
      }
      container = slug_image.run("start #{process}", { name: container_name, 'Labels' => labels, 'PublishAllPorts' => true })
      emitter.emit :message, "-----> Deployed container #{i + 1} for process type `#{process}`: #{container.id}" if emitter
    end
  end

end

#http_destinations(name) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ezpaas/helpers/container_manager.rb', line 151

def http_destinations(name)
  containers = Docker::Container.all(filters: { label: [ "com.tendigi.appname=#{name}", "com.tendigi.process=web"]  }.to_json)

  ports = {}

  for container in containers
    for port_info in (container.info['Ports'] || [])
      if port_info['PrivatePort'] == 5000
        ports[container.id] = port_info['PublicPort']
      end
    end
  end

  ports
end

#pull_imagesObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/ezpaas/helpers/container_manager.rb', line 179

def pull_images

  images = [
    'deis/slugbuilder',
    'deis/slugrunner'
  ]

  puts

  images.each do |name|

  	pastel = Pastel.new
  	puts 'Downloading latest image: ' + pastel.blue(name)

    Docker::Image.create({'fromImage' => name, 'tag' => 'latest'}, connection) do |chunk|
      fragments = chunk.split "\r\n"
      fragments.each do |fragment|
      	data = JSON.parse(fragment)
      	if id = data['id']
      		# puts "#{id}: #{data['status']}" #off for now
      	else
      		puts pastel.green(data['status'].rstrip)
      	end
      	
      end
    end

    puts
  end

end

#read_procfile(slug_name) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/ezpaas/helpers/container_manager.rb', line 167

def read_procfile(slug_name)
  tar_extract = Gem::Package::TarReader.new(Zlib::GzipReader.open(slug_path(slug_name)))
  tar_extract.rewind # The extract has to be rewinded after every iteration
  tar_extract.each do |entry|
    if entry.full_name == './Procfile' && entry.file?
      return YAML.load(entry.read)
    end
  end
ensure
  tar_extract.close
end

#undeploy_app(name, emitter = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ezpaas/helpers/container_manager.rb', line 129

def undeploy_app(name, emitter = nil)

  emitter.emit :message, "-----> Un-deploying #{name}" if emitter

  containers = Docker::Container.all(all: true, filters: { label: [ "com.tendigi.appname=#{name}"  ]  }.to_json)

  containers.each do |c|
    emitter.emit :message, "-----> Removing container #{c.id}" if emitter
    c.remove(force: true)
  end

  emitter.emit :message, "-----> Deleting slug images for #{name}" if emitter

  images = Docker::Image.all(all: true, filters: { label: [ "com.tendigi.appname=#{name}"  ]  }.to_json)

  images.each do |i|
    emitter.emit :message, "-----> Removing image #{i.id}" if emitter
    i.remove
  end

end