forked from CenturyLinkLabs/docker-image-graph
-
Notifications
You must be signed in to change notification settings - Fork 2
/
image-graph-web.rb
68 lines (51 loc) · 1.08 KB
/
image-graph-web.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
63
64
65
66
67
68
require 'sinatra'
require 'docker'
set :port, ENV['PORT']
set :bind, '0.0.0.0'
get '/' do
File.read(File.join('public', 'index.html'))
end
get '/images.json' do
Docker::Image.all(all: 1).map do |image|
label = "#{image.short_id} — #{image.size} MB<span class=\"tags\">#{image.tags}</span>"
[ { v: image.id, f: label }, image.parent_id, image.cmd, ]
end.to_json
end
delete '/images/:image_id.json' do
image = Docker::Image.get(params['image_id'])
begin
image.remove()
"true"
rescue => ex
"false"
end
end
class Docker::Image
NOP_PREFIX = '#(nop) '
def short_id
id[0..11]
end
def parent_id
info['ParentId']
end
def size
info['VirtualSize'] / 1024 / 1024
end
def tags
info['RepoTags'].reject { |t| t == '<none>:<none>' }.join(', ')
end
def cmd
cmd = json['ContainerConfig']['Cmd']
if cmd && cmd.size == 3
cmd = cmd.last
if cmd.start_with?(NOP_PREFIX)
cmd = cmd.split(NOP_PREFIX).last
else
cmd = "RUN #{cmd}".squeeze(' ')
end
end
cmd
rescue => ex
''
end
end