-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.rb
executable file
·62 lines (54 loc) · 1.54 KB
/
deploy.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
#!/usr/bin/env ruby
require_relative 'util/registry'
require_relative 'util/commands'
require 'json'
require 'sshkit/dsl'
fail "Arguments: <server list> <image_id>" unless ARGV.length == 2
servers = ARGV[0].split(/[ ,;]+/)
image_id = ARGV[1]
image_name, image_version = image_id.match(/^(.*):(.*)$/).captures
class Container
attr_reader :id, :image_id, :image_name, :image_tag
end
def get_container_ids(image_name)
cmd = "docker ps -a | grep '#{image_name}:' | awk '{ print $1 }'"
capture(cmd).split
end
def get_containers(image_name)
return [] unless (container_ids = get_container_ids(image_name)).any?
cmd = "docker inspect #{container_ids.join ' '}"
puts cmd
JSON.parse capture(cmd)
end
def start_container(image_id)
cmd = "docker run -d #{image_id}"
puts cmd
capture(cmd)
end
def stop_containers(containers)
running_containers = containers.select do |container|
container['State']['Running']
end
return if running_containers.none?
running_container_ids = running_containers.map do |container|
container['ID']
end
cmd = "docker stop #{running_container_ids.join ' '}"
execute cmd
end
def remove_containers(containers)
return if containers.none?
container_ids = containers.map do |container|
container['ID']
end
cmd = "docker rm #{container_ids.join ' '}"
execute cmd
end
on servers, in: :parallel do
old_containers = get_containers(image_name)
new_container = start_container image_id
puts new_container
# notify load balancers?
stop_containers old_containers
remove_containers old_containers
end