-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrun-task.rb
executable file
·45 lines (35 loc) · 1.09 KB
/
run-task.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
#!/usr/bin/env ruby
service_name = ARGV[0]
unless service_name
$stderr.puts "USAGE: ./run-task.rb <service-name>"
exit 1
end
require "docker"
class Docker::Service
include Docker::Base
def self.all(opts = {}, conn = Docker.connection)
hashes = Docker::Util.parse_json(conn.get('/services', opts)) || []
hashes.map { |hash| new(conn, hash) }
end
def version
self.info["Version"]["Index"]
end
def update(opts)
connection.post("/services/#{self.id}/update", {version: version}, body: opts.to_json)
end
def scale(replicas)
spec = self.info["Spec"]
spec["Mode"]["Replicated"]["Replicas"] = replicas
# by setting ForceUpdate to version, we are sure
# it is changed every time, effectively forcing an update
spec["TaskTemplate"]["ForceUpdate"] = version
update(spec)
end
private_class_method :new
end
# do what the following command is doing:
# docker service update --force --replicas 1 service1
# because we don't have docker client installed
Docker.validate_version!
Docker::Service.all(filters: {name: [service_name]}.to_json)[0].scale 1
puts "OK"