small-ops 0.0.32 → 0.0.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/docker2etcd +56 -33
- metadata +1 -1
data/bin/docker2etcd
CHANGED
|
@@ -19,43 +19,66 @@ OptionParser.new do |opts|
|
|
|
19
19
|
opts.on("-c","--clear","clear etcd of previous data (only containers data)") do |c|
|
|
20
20
|
@options[:clear]=c
|
|
21
21
|
end
|
|
22
|
+
opts.on("-f","--foreground","Keep running on foregroud. Default is run once and exit.") do |f|
|
|
23
|
+
@options[:foreground]=f
|
|
24
|
+
end
|
|
22
25
|
end.parse!
|
|
23
26
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if n.has_key?("host") && n.has_key?("name") && n.has_key?("port") then
|
|
29
|
-
puts "del #{n["name"]}"
|
|
30
|
-
http_delete("#{@options[:etcd]}/v2/keys/#{n["name"]}?dir=true&recursive=true")
|
|
31
|
-
end
|
|
32
|
-
}
|
|
33
|
-
end
|
|
27
|
+
run = true
|
|
28
|
+
last = []
|
|
29
|
+
|
|
30
|
+
while run do
|
|
34
31
|
|
|
35
|
-
`docker ps | tail -n+2 | awk '{ print $1 }'`.split("\n")
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if @options[:url] then
|
|
47
|
-
container["/#{name}/url"] = "http://#{@options[:host]}/#{name}"
|
|
48
|
-
else
|
|
49
|
-
container["/#{name}/url"] = "http://#{@options[:host]}:#{hport}"
|
|
32
|
+
containers = `docker ps | tail -n+2 | awk '{ print $1 }'`.split("\n")
|
|
33
|
+
|
|
34
|
+
if last != containers then
|
|
35
|
+
|
|
36
|
+
if @options[:clear] then
|
|
37
|
+
data = nodes2obj( http_get("#{@options[:etcd]}/v2/keys/?recursive=true")["node"]["nodes"] ,"/")
|
|
38
|
+
data.keys.each {|k|
|
|
39
|
+
n = data[k]
|
|
40
|
+
if n.has_key?("host") && n.has_key?("name") && n.has_key?("port") then
|
|
41
|
+
puts "delete #{n["name"]}"
|
|
42
|
+
http_delete("#{@options[:etcd]}/v2/keys/#{n["name"]}?dir=true&recursive=true")
|
|
50
43
|
end
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
44
|
+
}
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
containers
|
|
48
|
+
.map { |id| `docker inspect #{id}` }
|
|
49
|
+
.map { |inspect| JSON.parse(inspect)[0]}
|
|
50
|
+
.map { |data| flatten(data,data["Name"]) }
|
|
51
|
+
.each { |container|
|
|
52
|
+
name = container.keys.first.split("/")[1]
|
|
53
|
+
port = `docker inspect #{container["/#{name}/id"]} | grep -o '[0-9]\\+/tcp' | head -n1 | grep -o '[0-9]\\+'`.gsub("\n","")
|
|
54
|
+
hport = container["/#{name}/networksettings/ports/#{port}/tcp/hostport"]
|
|
55
|
+
container["/#{name}/name"] = name
|
|
56
|
+
container["/#{name}/port"] = hport
|
|
57
|
+
container["/#{name}/host"] = @options[:host]
|
|
58
|
+
if @options[:url] then
|
|
59
|
+
container["/#{name}/url"] = "http://#{@options[:host]}/#{name}"
|
|
60
|
+
else
|
|
61
|
+
container["/#{name}/url"] = "http://#{@options[:host]}:#{hport}"
|
|
57
62
|
end
|
|
63
|
+
container.keys.each {|key|
|
|
64
|
+
if container[key] != nil then
|
|
65
|
+
if @options[:verbose] then
|
|
66
|
+
puts "#{key} = #{container[key]}"
|
|
67
|
+
end
|
|
68
|
+
#http_put("#{@options[:etcd]}/v2/keys#{key}","value=#{URI.encode(container[key])}")
|
|
69
|
+
end
|
|
70
|
+
}
|
|
71
|
+
puts "-> #{name} : #{container["/#{name}/url"]}"
|
|
58
72
|
}
|
|
59
|
-
|
|
60
|
-
|
|
73
|
+
last = containers
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
run =false
|
|
77
|
+
|
|
78
|
+
if @options[:foreground] then
|
|
79
|
+
run = true
|
|
80
|
+
sleep 5
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
end
|
|
61
84
|
|