summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-dockerman/luasrc/model/cbi/dockerman/volumes.lua
blob: 8dae4a020d7fa176f6fbaffea1d98a8065a0a56a (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
--[[
LuCI - Lua Configuration Interface
Copyright 2019 lisaac <https://github.com/lisaac/luci-app-dockerman>
]]--

local docker = require "luci.model.docker"
local dk = docker.new()

local m, s, o

local res, containers, volumes

function get_volumes()
	local data = {}
	for i, v in ipairs(volumes) do
		local index = v.Name
		data[index]={}
		data[index]["_selected"] = 0
		data[index]["_nameraw"] = v.Name
		data[index]["_name"] = v.Name:sub(1,12)

		for ci,cv in ipairs(containers) do
			if cv.Mounts and type(cv.Mounts) ~= "table" then
				break
			end
			for vi, vv in ipairs(cv.Mounts) do
				if v.Name == vv.Name then
					data[index]["_containers"] = (data[index]["_containers"] and (data[index]["_containers"] .. " | ") or "")..
					'<a href='..luci.dispatcher.build_url("admin/docker/container/"..cv.Id)..' class="dockerman_link" title="'..translate("Container detail")..'">'.. cv.Names[1]:sub(2)..'</a>'
				end
			end
		end
		data[index]["_driver"] = v.Driver
		data[index]["_mountpoint"] = nil

		for v1 in v.Mountpoint:gmatch('[^/]+') do
			if v1 == index then 
				data[index]["_mountpoint"] = data[index]["_mountpoint"] .."/" .. v1:sub(1,12) .. "..."
			else
				data[index]["_mountpoint"] = (data[index]["_mountpoint"] and data[index]["_mountpoint"] or "").."/".. v1
			end
		end
		data[index]["_created"] = v.CreatedAt
	end

	return data
end

res = dk.volumes:list()
if res.code <300 then
	volumes = res.body.Volumes
else
	return
end

res = dk.containers:list({
	query = {
		all=true
	}
})
if res.code <300 then
	containers = res.body
else
	return
end

local volume_list = get_volumes()

m = SimpleForm("docker", translate("Docker"))
m.submit=false
m.reset=false

s = m:section(Table, volume_list, translate("Volumes"))

o = s:option(Flag, "_selected","")
o.disabled = 0
o.enabled = 1
o.default = 0
o.write = function(self, section, value)
	volume_list[section]._selected = value
end

o = s:option(DummyValue, "_name", translate("Name"))

o = s:option(DummyValue, "_driver", translate("Driver"))

o = s:option(DummyValue, "_containers", translate("Containers"))
o.rawhtml = true

o = s:option(DummyValue, "_mountpoint", translate("Mount Point"))

o = s:option(DummyValue, "_created", translate("Created"))

s = m:section(SimpleSection)
s.template = "dockerman/apply_widget"
s.err=docker:read_status()
s.err=s.err and s.err:gsub("\n","<br>"):gsub(" ","&nbsp;")
if s.err then
	docker:clear_status()
end

s = m:section(Table,{{}})
s.notitle=true
s.rowcolors=false
s.template="cbi/nullsection"

o = s:option(Button, "remove")
o.inputtitle= translate("Remove")
o.template = "dockerman/cbi/inlinebutton"
o.inputstyle = "remove"
o.forcewrite = true
o.write = function(self, section)
	local volume_selected = {}

	for k in pairs(volume_list) do
		if volume_list[k]._selected == 1 then
			volume_selected[#volume_selected+1] = k
		end
	end

	if next(volume_selected) ~= nil then
		local success = true
		docker:clear_status()
		for _,vol in ipairs(volume_selected) do
			docker:append_status("Volumes: " .. "remove" .. " " .. vol .. "...")
			local msg = dk.volumes["remove"](dk, {id = vol})
			if msg.code ~= 204 then
				docker:append_status("code:" .. msg.code.." ".. (msg.body.message and msg.body.message or msg.message).. "\n")
				success = false
			else
				docker:append_status("done\n")
			end
		end

		if success then
			docker:clear_status()
		end
		luci.http.redirect(luci.dispatcher.build_url("admin/docker/volumes"))
	end
end

return m