From e81ae1e7eb8b070f0e3d73f18b8184ed0f920a4d Mon Sep 17 00:00:00 2001
From: Petar Koretic <petar.koretic@sartura.hr>
Date: Thu, 30 Oct 2014 09:46:45 +0100
Subject: [PATCH] luci-app-lxc: add package for luci lxc app

Signed-off-by: Petar Koretic <petar.koretic@sartura.hr>
---
 utils/luci-app-lxc/Makefile                   |  66 +++
 utils/luci-app-lxc/files/controller/lxc.lua   | 182 +++++++
 utils/luci-app-lxc/files/lxc.config           |   6 +
 utils/luci-app-lxc/files/model/cbi/lxc.lua    |  31 ++
 utils/luci-app-lxc/files/view/lxc.htm         | 474 ++++++++++++++++++
 .../www/luci-static/resources/cbi/green.gif   | Bin 0 -> 310 bytes
 .../www/luci-static/resources/cbi/purple.gif  | Bin 0 -> 317 bytes
 .../www/luci-static/resources/cbi/red.gif     | Bin 0 -> 320 bytes
 8 files changed, 759 insertions(+)
 create mode 100644 utils/luci-app-lxc/Makefile
 create mode 100644 utils/luci-app-lxc/files/controller/lxc.lua
 create mode 100644 utils/luci-app-lxc/files/lxc.config
 create mode 100644 utils/luci-app-lxc/files/model/cbi/lxc.lua
 create mode 100644 utils/luci-app-lxc/files/view/lxc.htm
 create mode 100644 utils/luci-app-lxc/files/www/luci-static/resources/cbi/green.gif
 create mode 100644 utils/luci-app-lxc/files/www/luci-static/resources/cbi/purple.gif
 create mode 100644 utils/luci-app-lxc/files/www/luci-static/resources/cbi/red.gif

diff --git a/utils/luci-app-lxc/Makefile b/utils/luci-app-lxc/Makefile
new file mode 100644
index 000000000..ffdb95843
--- /dev/null
+++ b/utils/luci-app-lxc/Makefile
@@ -0,0 +1,66 @@
+#
+# Copyright (C) 2014 OpenWrt.org
+#
+# This is free software, licensed under the GNU General Public License v2.
+# See /LICENSE for more information.
+#
+
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=luci-app-lxc
+PKG_RELEASE:=20141012
+
+PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
+
+include $(INCLUDE_DIR)/package.mk
+
+define Package/luci-app-lxc
+  SECTION:=luci
+  CATEGORY:=LuCI
+  SUBMENU:=3. Applications
+  TITLE:=LXC management Web UI
+  DEPENDS:=+luci-mod-admin-full +lxc +liblxc +rpcd-mod-lxc
+  MAINTAINER:=Petar Koretic <petar.koretic@sartura.hr>
+endef
+
+define Package/luci-app-lxc/description
+ This package will install LXC management Web UI.
+endef
+
+define Build/Prepare
+endef
+
+define Build/Configure
+endef
+
+define Build/Compile
+endef
+
+define Package/luci-app-lxc/install
+	$(INSTALL_DIR) $(1)/usr/lib/lua/luci/controller/
+	$(INSTALL_BIN) \
+		./files/controller/lxc.lua \
+		$(1)/usr/lib/lua/luci/controller/
+
+	$(INSTALL_DIR) $(1)/usr/lib/lua/luci/view/
+	$(INSTALL_DATA) \
+		./files/view/lxc.htm \
+		$(1)/usr/lib/lua/luci/view/
+
+	$(INSTALL_DIR) $(1)/usr/lib/lua/luci/model/cbi/
+	$(INSTALL_BIN) \
+		./files/model/cbi/lxc.lua \
+		$(1)/usr/lib/lua/luci/model/cbi/
+
+	$(INSTALL_DIR) $(1)/etc/config/
+	$(INSTALL_DATA) \
+		./files/lxc.config \
+		$(1)/etc/config/lxc
+
+	$(INSTALL_DIR) $(1)/www
+	$(CP) -R \
+		./files/www/* \
+		$(1)/www
+endef
+
+$(eval $(call BuildPackage,luci-app-lxc))
diff --git a/utils/luci-app-lxc/files/controller/lxc.lua b/utils/luci-app-lxc/files/controller/lxc.lua
new file mode 100644
index 000000000..8172fb9c0
--- /dev/null
+++ b/utils/luci-app-lxc/files/controller/lxc.lua
@@ -0,0 +1,182 @@
+--[[
+
+LuCI LXC module
+
+Copyright (C) 2014, Cisco Systems, Inc.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+	http://www.apache.org/licenses/LICENSE-2.0
+
+Author: Petar Koretic <petar.koretic@sartura.hr>
+
+]]--
+
+module("luci.controller.lxc", package.seeall)
+
+function index()
+	page = node("admin", "services", "lxc")
+	page.target = cbi("lxc")
+	page.title = _("LXC Containers")
+	page.order = 70
+
+	page = entry({"admin", "services", "lxc_create"}, call("lxc_create"), nil)
+	page.leaf = true
+
+	page = entry({"admin", "services", "lxc_stop"}, call("lxc_stop"), nil)
+	page.leaf = true
+
+	page = entry({"admin", "services", "lxc_start"}, call("lxc_start"), nil)
+	page.leaf = true
+
+	page = entry({"admin", "services", "lxc_reboot"}, call("lxc_reboot"), nil)
+	page.leaf = true
+
+	page = entry({"admin", "services", "lxc_delete"}, call("lxc_delete"), nil)
+	page.leaf = true
+
+	page = entry({"admin", "services", "lxc_list"}, call("lxc_list"), nil)
+	page.leaf = true
+
+	page = entry({"admin", "services", "lxc_rename"}, call("lxc_rename"), nil)
+	page.leaf = true
+
+	page = entry({"admin", "services", "lxc_clone"}, call("lxc_clone"), nil)
+	page.leaf = true
+
+	page = entry({"admin", "services", "lxc_freeze"}, call("lxc_freeze"), nil)
+	page.leaf = true
+
+	page = entry({"admin", "services", "lxc_unfreeze"}, call("lxc_unfreeze"), nil)
+	page.leaf = true
+
+	page = entry({"admin", "services", "lxc_configuration_get"}, call("lxc_configuration_get"), nil)
+	page.leaf = true
+
+	page = entry({"admin", "services", "lxc_configuration_set"}, call("lxc_configuration_set"), nil)
+	page.leaf = true
+
+end
+
+function lxc_create(lxc_name, lxc_template)
+	luci.http.prepare_content("text/plain")
+
+	local uci = require("uci").cursor()
+
+	local url = uci:get("lxc", "lxc", "url")
+
+	local f = io.popen([[grep DISTRIB_TARGET /etc/openwrt_release | awk -F"[/'']" '{ print $2 }']])
+	if not f then
+		return luci.http.write("1")
+	end
+
+	local target = f:read("*all")
+
+	local res = os.execute("lxc-create -t download -n " .. lxc_name .. " -- --server=" .. url .. " --no-validate --dist openwrt --release bb --arch " .. target)
+
+	luci.http.write(tostring(res))
+end
+
+function lxc_start(lxc_name)
+	luci.http.prepare_content("text/plain")
+
+	local res = os.execute("ubus call lxc start '{\"name\" : \"" .. lxc_name .. "\"}' ")
+
+	luci.http.write(tostring(res))
+end
+
+function lxc_stop(lxc_name)
+	luci.http.prepare_content("text/plain")
+
+	local res = os.execute("ubus call lxc stop '{\"name\" : \"" .. lxc_name .. "\"}' ")
+
+	luci.http.write(tostring(res))
+end
+
+function lxc_delete(lxc_name)
+	luci.http.prepare_content("text/plain")
+
+	os.execute("ubus call lxc stop '{\"name\" : \"" .. lxc_name .. "\"}' ")
+	local res = os.execute("ubus call lxc destroy '{\"name\" : \"" .. lxc_name .. "\"}' ")
+
+	luci.http.write(tostring(res))
+end
+
+function lxc_reboot(lxc_name)
+	luci.http.prepare_content("text/plain")
+
+	local res = os.execute("ubus call lxc reboot '{\"name\" : \"" .. lxc_name .. "\"}' ")
+
+	luci.http.write(tostring(res))
+end
+
+function lxc_rename(lxc_name_cur, lxc_name_new)
+	luci.http.prepare_content("text/plain")
+
+	local res = os.execute("ubus call lxc rename '{\"name\" : \"" .. lxc_name_cur .. "\", \"newname\" : \"" .. lxc_name_new .. "\"}' ")
+
+	luci.http.write(tostring(res))
+end
+
+function lxc_freeze(lxc_name)
+	luci.http.prepare_content("text/plain")
+
+	local res = os.execute("ubus call lxc freeze '{\"name\" : \"" .. lxc_name .. "\"}' ")
+
+	luci.http.write(tostring(res))
+end
+
+function lxc_unfreeze(lxc_name)
+	luci.http.prepare_content("text/plain")
+
+	local res = os.execute("ubus call lxc unfreeze '{\"name\" : \"" .. lxc_name .. "\"}' ")
+
+	luci.http.write(tostring(res))
+end
+
+function lxc_list()
+	luci.http.prepare_content("application/json")
+
+	local cmd = io.popen("ubus call lxc list")
+	if not cmd then
+		return luci.http.write("{}")
+	end
+
+	local res = cmd:read("*all")
+	cmd:close()
+
+	luci.http.write(res)
+end
+
+function lxc_configuration_get(lxc_name)
+	luci.http.prepare_content("text/plain")
+
+	local f = io.open("/lxc/" .. lxc_name .. "/config", "r")
+	local content = f:read("*all")
+	f:close()
+
+	luci.http.write(content)
+end
+
+function lxc_configuration_set(lxc_name)
+	luci.http.prepare_content("text/plain")
+
+	local lxc_configuration = luci.http.formvalue("lxc_configuration")
+
+	if lxc_configuration == nil then
+		return luci.http.write("1")
+	end
+
+	local f, err = io.open("/lxc/" .. lxc_name .. "/config","w+")
+	if not f then
+		return luci.http.write("2")
+	end
+
+	f:write(lxc_configuration)
+	f:close()
+
+	luci.http.write("0")
+end
+
diff --git a/utils/luci-app-lxc/files/lxc.config b/utils/luci-app-lxc/files/lxc.config
new file mode 100644
index 000000000..5572c735f
--- /dev/null
+++ b/utils/luci-app-lxc/files/lxc.config
@@ -0,0 +1,6 @@
+#
+# lxc uci configuration
+#
+
+config lxc 'lxc'
+	option url 'virtualwrt.org/containers/'
diff --git a/utils/luci-app-lxc/files/model/cbi/lxc.lua b/utils/luci-app-lxc/files/model/cbi/lxc.lua
new file mode 100644
index 000000000..ac0fdff33
--- /dev/null
+++ b/utils/luci-app-lxc/files/model/cbi/lxc.lua
@@ -0,0 +1,31 @@
+--[[
+
+LuCI LXC module
+
+Copyright (C) 2014, Cisco Systems, Inc.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+	http://www.apache.org/licenses/LICENSE-2.0
+
+Author: Petar Koretic <petar.koretic@sartura.hr>
+
+]]--
+
+local fs = require "nixio.fs"
+
+m = Map("lxc", translate("LXC Containers"))
+
+if fs.access("/etc/config/lxc") then
+	m:section(SimpleSection).template = "lxc"
+
+	s = m:section(TypedSection, "lxc", translate("Options"))
+	s.anonymous = true
+	s.addremove = false
+
+	s:option(Value, "url", translate("Containers URL"))
+end
+
+return m
diff --git a/utils/luci-app-lxc/files/view/lxc.htm b/utils/luci-app-lxc/files/view/lxc.htm
new file mode 100644
index 000000000..1d38721da
--- /dev/null
+++ b/utils/luci-app-lxc/files/view/lxc.htm
@@ -0,0 +1,474 @@
+<%#
+
+LuCI LXC module
+
+Copyright (C) 2014, Cisco Systems, Inc.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+	http://www.apache.org/licenses/LICENSE-2.0
+
+Author: Petar Koretic <petar.koretic@sartura.hr>
+
+-%>
+
+<fieldset class="cbi-section">
+	<legend><%:Available Containers%></legend>
+	<div class="cbi-section-node">
+		<table id="t_lxc_list" class="cbi-section-table">
+			<tr class="cbi-section-table-titles">
+				<th class="cbi-section-table-cell"><%:Name%></th>
+				<th class="cbi-section-table-cell"><%:Status%></th>
+				<th class="cbi-section-table-cell"><%:Actions%></th>
+			</tr>
+		</table>
+	</div>
+</fieldset>
+
+<fieldset class="cbi-section">
+	<span id="lxc-list-output"></span>
+</fieldset>
+
+<hr/>
+<fieldset class="cbi-section">
+	<legend><%:Create New Container%></legend>
+	<div class="cbi-section-node">
+		<table id="t_lxc_create" class="cbi-section-table">
+			<tr class="cbi-section-table-titles">
+				<th class="cbi-section-table-cell"><%:Name%></th>
+				<th class="cbi-section-table-cell"><%:Template%></th>
+				<th class="cbi-section-table-cell"><%:Actions%></th>
+			</tr>
+			<tr id="tr_holder">
+				<td>
+					<input type="text" id="tx_name" placeholder="<%:Enter new name%>" value='' />
+				</td>
+				<td>
+					<select id="s_template" class="cbi-input-select cbi-button">
+						<option value="openwrt">OpenWrt</option>
+					</select>
+				</td>
+				<td>
+					<input type="button" id="bt_create" value="<%:Create%>" onclick="lxc_create(tr_holder)" class="cbi-button cbi-button-add" />
+					<span id="lxc-add-loader" style="display:inline-block; width:16px; height:16px; margin:0 5px"></span>
+				</td>
+			</tr>
+		</table>
+	</div>
+</fieldset>
+
+<fieldset class="cbi-section">
+	<span id="lxc-add-output"></span>
+</fieldset>
+
+<hr/>
+
+<script type="text/javascript" src="<%=resource%>/cbi.js"></script>
+<script type="text/javascript">//<![CDATA[
+
+	window.img = { "red" : "<%=resource%>/cbi/red.gif", "green" : "<%=resource%>/cbi/green.gif", "purple" : "<%=resource%>/cbi/purple.gif" }
+	window.states = {  "STOPPED" : "red", "RUNNING" : "green", "FROZEN" : "purple"}
+
+	var t_lxc_list = document.getElementById('t_lxc_list');
+	var loader_html = '<img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" width="16" height="16" style="vertical-align:middle" /> ';
+	var timeout_msg = 0
+	var output_list = document.getElementById("lxc-list-output")
+	var output_add = document.getElementById("lxc-add-output")
+	var loader_add = document.getElementById("lxc-add-loader")
+
+	function lxc_create(tr)
+	{
+		var lxc_name = tr.querySelector("#tx_name").value.trim()
+		var lxc_template = tr.querySelector("#s_template").value
+		var bt_create = tr.querySelector("#bt_create")
+
+		if (t_lxc_list.querySelector("[data-id='" + lxc_name + "']") != null)
+			return info_message(output_add, "Container with that name already exists!", 4000)
+
+		bt_create.disabled = true
+		output_add.innerHTML = ''
+
+		if (!lxc_name || !lxc_name.length)
+		{
+			bt_create.disabled = false
+			return info_message(output_add, "Name cannot be empty!", 4000)
+		}
+
+		loading(loader_add)
+
+		new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_create/' + lxc_name + "/" + lxc_template , null,
+		function(x)
+		{
+			bt_create.disabled = false
+			loading(loader_add, 0)
+
+			if (!x)
+				info_message(output_add, "Container creation failed!")
+		})
+	}
+
+	function lxc_create_template(lxc_name, lxc_state)
+	{
+		var info_row = t_lxc_list.querySelector("#empty")
+		if (info_row)
+			t_lxc_list.deleteRow(1)
+
+		var actions = ''
+		actions += '<input type="button" onclick="action_handler(this)" data-action="start" value="<%:Start%>" class="cbi-button cbi-button-apply" />'
+		actions+= '<input type="button" onclick="action_handler(this)" data-action="stop" value="<%:Stop%>" class="cbi-button cbi-button-reset" />'
+		actions+= '<input type="button" onclick="action_handler(this)" data-action="delete" value="<%:Delete%>" class="cbi-button cbi-button-remove" />'
+		actions+= ' <select class="cbi-input-select cbi-button" onchange="action_more_handler(this)">\
+						<option selected disabled>more</option>\
+						<option>configure</option>\
+						<option>freeze</option>\
+						<option>unfreeze</option>\
+						<option>reboot</option>\
+					</select>'
+		actions+= '<span data-loader style="display:inline-block; width:16px; height:16px; margin:0 5px"></span>'
+
+		var row = t_lxc_list.insertRow(-1)
+		var cell = row.insertCell(-1)
+		cell.innerHTML = "<strong>" + lxc_name + "</strong>"
+		cell.width = "30%"
+		cell.setAttribute("data-id", lxc_name)
+
+		cell = row.insertCell(-1)
+		cell.width = "20%"
+		cell.innerHTML = "<img src='"+window.img[lxc_state]+"'/>"
+
+		cell = row.insertCell(-1)
+		cell.width = "50%"
+		cell.innerHTML = actions
+	}
+
+	function action_handler(self)
+	{
+		var action = self.getAttribute("data-action");
+
+		var bt_action = self
+		var lxc_name = self.parentNode.parentNode.children[0].getAttribute('data-id')
+		var status_img = self.parentNode.parentNode.querySelector('img')
+		var loader = self.parentNode.querySelector('[data-loader]')
+
+		bt_action.disabled = true
+
+		if (action == "stop")
+		{
+			loading(loader)
+
+			new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_stop/' + lxc_name, null,
+			function(x)
+			{
+				loading(loader, 0)
+				bt_action.disabled = false
+
+				if (!x || x.responseText != "0")
+					return info_message(output_list,"Invalid response from system!")
+
+				set_status(status_img, "red")
+
+			});
+		}
+
+		else if (action == "start")
+		{
+			loading(loader)
+
+			new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_start/' + lxc_name, null,
+			function(x)
+			{
+				loading(loader, 0)
+				bt_action.disabled = false
+
+				//FIXME: uncomment after fixing 'lxc-start'
+				if (!x /*|| x.responseText != "0"*/)
+					return info_message(output_list,"Invalid response from system!")
+
+				//FIXME: uncomment after fixing 'lxc-start'
+				//set_status(status_img, "green")
+			});
+		}
+
+		else if (action == "delete")
+		{
+			if (!confirm("This will completely remove LXC container from the disk. Are you sure? (container will be stopped if running)"))
+				return
+
+			loading(loader)
+
+			new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_delete/' + lxc_name, null,
+			function(x)
+			{
+				loading(loader, 0)
+				bt_action.disabled = false
+
+				if (!x || x.responseText != "0")
+					return info_message(output_list,"Invalid response from system!")
+
+				var row = self.parentNode.parentNode
+				row.parentNode.removeChild(row)
+
+			});
+		}
+	}
+
+	function lxc_rename_handler(self)
+	{
+		var td = self.parentNode
+		var lxc_name_holder = td.querySelector('[data-id]')
+		var lxc_name_cur = lxc_name_holder.getAttribute('data-id')
+		var lxc_name_new = lxc_name_holder.value
+
+		if (t_lxc_list.querySelector("[data-id='" + lxc_name_new + "']") != null)
+			return info_message(output_list, "Container with new name already exists!", 4000)
+
+		new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_rename/' + lxc_name_cur + '/' + lxc_name_new, null,
+		function(x)
+		{
+			if (!x || x.responseText != "0")
+				return info_message(output_list,"Invalid response from system!")
+
+			info_message(output_list,"LXC renamed")
+		})
+	}
+
+	function lxc_configure_handler(self)
+	{
+		var td = self.parentNode
+		var textarea = td.querySelector('[data-id]')
+		var lxc_name = textarea.getAttribute('data-id')
+		var lxc_configuration = textarea.value
+
+		new XHR().post('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_configuration_set/' + lxc_name, "lxc_configuration=" + encodeURIComponent(lxc_configuration) ,
+		function(x)
+		{
+			if (!x || x.responseText != "0")
+				return info_message(output_list,"Invalid response from system!")
+
+			info_message(output_list,"LXC configuration updated")
+			var row = td.parentNode
+			row.parentNode.removeChild(row)
+		})
+	}
+
+	function lxc_rename_template(lxc_name)
+	{
+		var h = '\
+			<input data-id="'+ lxc_name + '" type="text" placeholder="Enter new name" /> \
+			<input data-id="bt_confirm" onclick="lxc_rename_handler(this)" type="button" class="cbi-button" value="Confirm" />'
+
+		return h
+	}
+
+	function lxc_configure_template(lxc_name, lxc_configuration)
+	{
+		var h = '\
+			<textarea data-id="'+ lxc_name + '" rows="20" style="width:100%">'+ lxc_configuration +'</textarea> \
+			<input data-id="bt_confirm" onclick="lxc_configure_handler(this)" type="button" class="cbi-button" value="Confirm" />'
+
+		return h
+	}
+
+	function action_more_handler(self)
+	{
+		var lxc_name = self.parentNode.parentNode.querySelector('[data-id]').getAttribute('data-id')
+		var loader = self.parentNode.parentNode.querySelector('[data-loader]')
+
+		var option = self.options[self.selectedIndex].text
+
+		self.value = "more"
+
+		switch (option)
+		{
+			case "configure":
+				var tr = document.createElement('tr')
+				var row = self.parentNode.parentNode
+				var next_row = row.nextSibling
+				if (next_row && next_row.getAttribute('data-action') !== null)
+					row.parentNode.removeChild(next_row)
+
+				new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_configuration_get/' + lxc_name, null,
+				function(x)
+				{
+					tr.innerHTML="<td colspan='" + row.cells.length + "'>" + lxc_configure_template(lxc_name, x.responseText) + "</td>"
+					tr.setAttribute('data-action','')
+					row.parentNode.insertBefore(tr, row.nextSibling)
+				})
+
+			break
+
+			case "rename":
+				var tr = document.createElement('tr')
+				var row = self.parentNode.parentNode
+				var next_row = row.nextSibling
+				if (next_row && next_row.getAttribute('data-action') !== null)
+					row.parentNode.removeChild(next_row)
+
+				tr.innerHTML="<td colspan='" + row.cells.length + "'>" + lxc_rename_template(lxc_name) + "</td>"
+				tr.setAttribute('data-action','')
+				row.parentNode.insertBefore(tr, row.nextSibling)
+
+				break
+
+			case "freeze":
+				var tr = self.parentNode.parentNode
+				var img = tr.querySelector('img')
+				if(img.getAttribute('src') != window.img["green"])
+					return info_message(output_list,"Container is not running!")
+
+				loading(loader)
+				new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_freeze/' + lxc_name, null,
+				function(x)
+				{
+					loading(loader, 0)
+					if (!x || x.responseText != "0")
+						return info_message(output_list,"Invalid response from system!")
+
+					set_status(img, "purple")
+				})
+
+			break
+
+			case "unfreeze":
+				var tr = self.parentNode.parentNode
+				var img = tr.querySelector('img')
+
+				if(img.getAttribute('src') != window.img["purple"])
+					return info_message(output_list,"Container is not frozen!")
+
+				loading(loader)
+				new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_unfreeze/' + lxc_name, null,
+				function(x)
+				{
+					loading(loader, 0)
+					if (!x || x.responseText != "0")
+						return info_message(output_list,"Invalid response from system!")
+
+					set_status(img, "green")
+				})
+
+			break
+
+			case "reboot":
+				var tr = self.parentNode.parentNode
+				var img = tr.querySelector('img')
+				if(img.getAttribute('src') != window.img["green"])
+					return info_message(output_list,"Container is not running!")
+
+				if (!confirm("Are you sure?"))
+					return
+
+				loading(loader)
+				new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_reboot/' + lxc_name, null,
+				function(x)
+				{
+					loading(loader, 0)
+					if (!x || x.responseText != "0")
+						return info_message(output_list,"Invalid response from system!")
+
+					info_message(output_list,"LXC rebooted")
+				})
+			break
+		}
+
+	}
+
+	function set_empty(t_lxc_list)
+	{
+		if (document.getElementById('empty') !== null)
+			return
+
+		var row_count = t_lxc_list.rows.length;
+		while(--row_count) t_lxc_list.deleteRow(row_count);
+
+		var row = t_lxc_list.insertRow(-1);
+		row.id = 'empty'
+		var cell = row.insertCell(0);
+		cell.colSpan = 4;
+		cell.innerHTML = '<em><br />There are no containers available yet.</em>';
+	}
+
+	function lxc_list_update()
+	{
+		XHR.poll(4, '<%=luci.dispatcher.build_url("admin", "services")%>/lxc_list', null,
+		function(x, data)
+		{
+			if (!x) return;
+
+			var lxc_count = Object.keys(data).length
+			if (!data || !lxc_count)
+				return set_empty(t_lxc_list)
+
+			if (document.getElementById('empty') !== null)
+				t_lxc_list.deleteRow(1);
+
+			var lxcs = t_lxc_list.querySelectorAll('td[data-id]')
+			var lxc_name_table = {}
+			for (var i = 0, len = lxcs.length; i < len; i++)
+			{
+				var lxc_name = lxcs[i].getAttribute('data-id')
+				if (!(lxc_name in data))
+				{
+					var row = t_lxc_list.querySelector("[data-id='" + lxc_name + "']").parentNode
+					row.parentNode.removeChild(row)
+					continue
+				}
+
+				lxc_name_table[lxc_name] = lxcs[i].parentNode.querySelector('img')
+			}
+
+			for(var key in data)
+			{
+				var lxc_name = key
+				var state = window.states[data[key]]
+
+				if (!(lxc_name in lxc_name_table))
+					lxc_create_template(lxc_name, state)
+
+				else if (state != get_status(lxc_name_table[lxc_name]))
+					set_status(lxc_name_table[lxc_name], state)
+			}
+
+		})
+	}
+
+	function loading(elem, state)
+	{
+		state = (typeof state === 'undefined') ? 1 : state
+
+		if (state === 1)
+			elem.innerHTML = loader_html
+		else
+			setTimeout(function() { elem.innerHTML = ''}, 1000)
+	}
+
+	function set_status(elem, state)
+	{
+		state = (typeof state === 'undefined') ? 1 : state
+
+		setTimeout(function() { elem.setAttribute('src', window.img[state])}, 300)
+	}
+
+	function get_status(elem)
+	{
+		var src = elem.getAttribute('src')
+
+		for (var i in img)
+		{
+			if (img[i] == src)
+				return i
+		}
+	}
+
+	function info_message(output, msg, timeout)
+	{
+		timeout = timeout || 3000
+		output.innerHTML = msg
+		clearTimeout(timeout_msg)
+		timeout_msg = setTimeout(function(){ output.innerHTML=""}, timeout);
+	}
+
+	lxc_list_update()
+//]]></script>
diff --git a/utils/luci-app-lxc/files/www/luci-static/resources/cbi/green.gif b/utils/luci-app-lxc/files/www/luci-static/resources/cbi/green.gif
new file mode 100644
index 0000000000000000000000000000000000000000..d09febf1273b9a20ed89f69b19c9837c325b6db2
GIT binary patch
literal 310
zcmV-60m=SHNk%w1VG;lm0K^{vZ~bkE`i3IOOtAj1%l^tt)@2&AVF&*PSoufY_~E|k
z)JgwHS>#<B{~2}AqJ8O#A^#yh|2{6|G?V|5N&7n`{~R&@B@+J+KH^8f`M^}}P&u`P
zg7SbT=_dgH0RR90A^8LW0018VEC2ui01^Na000HE;3tY1r*)miWn=p#W-uf~sU|KV
ztwisa62d648yJn5<A5W<eI9^-bV9941AxaTg8_KIU@#{0U@RhwbYz`AM-Yrh0a0l{
z?v2aEj&C3e0tg2l9tVblhlB?R0)c~skdcav1sWHUm>w4z1r7`qnUWO@4hjMkkD&(?
z0tz3V7O9d47M&sq1`M&7v<wDL9|{Hq6crbS7Znr*2Cgj$4#OG<2%H8Ez%B|78wLg&
Ir7j@=J5RTB3jhEB

literal 0
HcmV?d00001

diff --git a/utils/luci-app-lxc/files/www/luci-static/resources/cbi/purple.gif b/utils/luci-app-lxc/files/www/luci-static/resources/cbi/purple.gif
new file mode 100644
index 0000000000000000000000000000000000000000..f0d68cc8b2c8e78ee3d8948f67fcb9e1c15f8e94
GIT binary patch
literal 317
zcmV-D0mA-ANk%w1VG;lm0K^{v>e%u0>iE{m+T+XZwTj1<W~juJ*}aU?t%bbFn%-YE
zZOftF+Pvs;MS;$x;iHPH>Er9cj@j0><j|?%#h%s4tkt!ezkODUqkOU4!0GSd^^jtr
zu9LUFiPyb`*8l(iA^8LW0018VEC2ui01^Na000HL;3tY2vqaItXk+^YW(EWSkg0*I
z9g^nL0UzVQ5(NkhfKsDu0vLqcM=}{?2G}h2I+1(;NQs0&E@v2GD4;@_I2;rAVgYx<
zDHX)Rz)$c|eS8KB2n7xu9v&TwiHnGe4h0*BiItX>9S$296cLx79uX8676b;Mmj(nD
z0vijDs2vL%0v{X*39y$P2?!h_5Df&mpS%PO5F-K&7<&<m5rY^FwJi`27o-k*7#9!8
PE+PWh3JTuUBOw4gzwCq@

literal 0
HcmV?d00001

diff --git a/utils/luci-app-lxc/files/www/luci-static/resources/cbi/red.gif b/utils/luci-app-lxc/files/www/luci-static/resources/cbi/red.gif
new file mode 100644
index 0000000000000000000000000000000000000000..c1b39bbedb72c8265531985640df71d761c5f902
GIT binary patch
literal 320
zcmV-G0l)r7Nk%w1VG;lm0K@<Q{Ck!DyU6`GTK8da{VG-Z-s9RqVgD3G$~I5`)7$-p
znES}l{cnoYcZ&a2ckH~z|3z)%T4w)~sQ;a@|FFN?8$<p}aQ`4p!W=sMQ)%czWVIJJ
z-*}Gy7e@aOLjV8&A^8LV00000EC2ui01^Na000HO;3tX!09_XZ1Y`RZfTaXU5hxZ=
zxC;R+i-a%%1P~UZ0zpA(2A+W?MM2no01SzC!tH7$48SL#Q82=QG#blLXi5Wz#X+|~
zAoIFo0MHBwfC7Lhfq(=98WjT_9v%lB2a%5ljU59Oij0k$oRyab0~ej69v1@!91#Mf
zodOXY4+t5WtQ{E$4<D%lw4EKU93ll6s=bXDt{7Gy3&a@$7abkQ84(yrEd>n-5d+>4
S5eN;-E)Uxq8{)YwApkob^ml{+

literal 0
HcmV?d00001