Skip to content

Commit

Permalink
otb-luci-swconfig: Add package
Browse files Browse the repository at this point in the history
This package manages the switch configuration
  • Loading branch information
gregdel authored and angt committed Jun 22, 2018
1 parent 6e01052 commit 27449cd
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 2 deletions.
32 changes: 32 additions & 0 deletions otb-luci-swconfig/Makefile
@@ -0,0 +1,32 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=otb-luci-swconfig
PKG_VERSION:=0.1
PKG_RELEASE:=0

PKG_LICENSE:=GPLv3

include $(INCLUDE_DIR)/package.mk

MY_DEPENDS := otb-luci

define Package/$(PKG_NAME)
SECTION:=OVH
CATEGORY:=OverTheBox
DEPENDS:=$(foreach p,$(MY_DEPENDS),+$(p))
TITLE:=$(PKG_NAME)
PKGARCH:=all
endef

define Package/$(PKG_NAME)/description
OverTheBox switch config in LuCI
endef

define Build/Compile
endef

define Package/$(PKG_NAME)/install
$(CP) ./files/* $(1)/
endef

$(eval $(call BuildPackage,$(PKG_NAME)))
@@ -0,0 +1,62 @@
-- vim: set expandtab tabstop=2 shiftwidth=2 softtabstop=2 :
module("luci.controller.otb_swconfig", package.seeall)

function index()
local has_switch = false

local uci = require("luci.model.uci").cursor()
uci:foreach("network", "switch", function(s)
if s.name == 'otbv2sw' then
has_switch = true
return
end
end)

if has_switch then
entry({"admin", "overthebox", "switch"}, template("otb_swconfig"), "Switch", 30)
entry({"admin", "overthebox", "switch_config"}, call("switch_config")).dependent = false
entry({"admin", "overthebox", "switch_reset"}, call("switch_reset")).dependent = false
end
end

function switch_config()
local data = {}
local uci = require("luci.model.uci").cursor()
uci:foreach("network", "switch_vlan", function(s)
if not (s["device"] == 'otbv2sw') then return end

local vlan = tonumber(s.vlan)
local ports
if s.ports then
ports = string.split(s.ports, " ")
end

-- LAN
local portType = "wan"
if vlan == 2 then portType = "lan" end

for i, port in ipairs(ports) do
-- Check if the port is tagged
local taggedValue = false
if string.find(port, "t") then
port = string.match(port,"(%d+)t")
taggedValue = true
end

if not data[port] then
data[port] = { type = portType, tagged = taggedValue }
end
end
end)
luci.http.prepare_content("application/json")
luci.http.write_json(data)
end

function switch_reset()
local wans = luci.http.formvalue("wans")
if os.execute("swconfig-reset "..wans) then
luci.http.status(200, "OK")
else
luci.http.status(500, "ERROR")
end
end
134 changes: 134 additions & 0 deletions otb-luci-swconfig/files/usr/lib/lua/luci/view/otb_swconfig.htm
@@ -0,0 +1,134 @@
<!-- vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 : -->
<%+header%>
<script type="text/javascript">
window.addEventListener('load', function() {
var wans = [];
var otbSwLayout = [
[
{2: '2'}, {4:'4'}, {6:'6'}, {8:'8'}, {10:'10'}, {12:'12'},
null, {14:'WAN 2'}, null, null, null, null,
],
[
{1: '1'}, {3:'3'}, {5:'5'}, {7:'7'}, {9:'9'}, {11:'11'},
null, {13:'WAN 1'}, null, null, {17:'SFP 1'}, {18:'SFP 2'},
],
];

// Handle click on port
var handlePortClick = function(e) {
var $target = $(e.currentTarget);
$target.children().toggleClass("cbi-button-remove");
var value = $target.attr("id").replace("otb-sw-port-", "");

// Add or remove the value from the wan array
var index = wans.indexOf(value);
if (index == -1) {
wans.push(value);
wans.sort(function(a,b) { return a - b });
} else {
wans.splice(index, 1);
}

// Display or hide the helper text
var $helper = $("#otb-sw-wan-port-list");
if (wans.length > 0) {
$helper.removeClass("hidden");
$helper.find("span").html(wans.join(", "));
} else {
$helper.addClass("hidden");
}
}

// Handle submit
$("#otb-sw-form-submit").bind("click", function(e) {
if (wans.length == 0) {
return;
}

$.post('<%=url("admin/overthebox/switch_reset")%>', {
"wans": wans.join(" "),
}, function() { location.reload(); });
});

// Create the table according to the layout
var $otbSwTable = $("#otb-sw-port-table");
for (var i in otbSwLayout) {
// Add the row
$otbSwTable.append("<tr></tr>");
var $row = $otbSwTable.children()
.eq(i)
.addClass("cbi-section-table-row")
.css("background-color", "white")
.attr("id", "otb-sw-table-row-" + i);

for (var j in otbSwLayout[i]) {
var $td = $("<td></td>")
.addClass("cbi-section-table-cell")
.css("height", "70px")
.css("border", "none");

var portObj = otbSwLayout[i][j];
if (portObj !== null) {
var port = Object.keys(portObj)[0];
var name = portObj[port];

$td.attr("id", "otb-sw-port-" + port);
$td.append("<div></div>").click(handlePortClick);
$td.children()
.addClass("cbi-button")
.attr("style", "width:100% !important;")
.css("padding", "5%");
$td.children().append("<div>" + name + "</div>");
$td.children().append('<div><small class="otb-sw-port-type"></small></div>');
}

$row.append($td);
}
};

// Query the current configuration to setup the colors and labels
$.get('<%=url("admin/overthebox/switch_config")%>', null, function(data) {
for (var port in data) {
var $elmt = $("#otb-sw-port-" + port);
if ($elmt == null) {
continue;
}

var portType = data[port]["type"];
var portTagged = data[port]["tagged"];
if (portType == "lan") {
$elmt.children().addClass("cbi-button-reset");
} else {
if (portTagged) {
portType = "tagged"
} else {
$elmt.children().addClass("cbi-button-add");
}
}
$elmt.find(".otb-sw-port-type").text(portType);
}
});
});
</script>

<h2>Switch configuration</h2>

<fieldset class="cbi-section">
<legend>Ports reset</legend>
<div class="cbi-map-descr">
Click on the ports you want to set as WAN, other ports will be set as LAN.
</div>
<br/>
<table style="border: none;">
<tbody id="otb-sw-port-table"></tbody>
</table>
<hr/>
<div id="otb-sw-wan-port-list" class="cbi-map-descr hidden">
This ports will be configured as WAN: <span></span>
</div>
<div class="cbi-section-create cbi-tblsection-create">
<input id="otb-sw-form-submit" class="cbi-button cbi-button-remove pull-right" type="submit" value="Reset switch configuration">
</div>
</fieldset>

<%+footer%>
4 changes: 2 additions & 2 deletions swconfig-otb/Makefile
@@ -1,7 +1,7 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=python-swconfig-otb
PKG_VERSION:=1.2
PKG_VERSION:=1.3
PKG_RELEASE:=2
PKG_LICENSE:=MIT

Expand All @@ -19,7 +19,7 @@ define Package/$(PKG_NAME)
SECTION:=OVH
CATEGORY:=OverTheBox
TITLE:=Tools to manage OVH OverTheBox switches
DEPENDS:=+python-light +python-logging +python-pyserial +lsof
DEPENDS:=+python-light +python-logging +python-pyserial +lsof +otb-luci-swconfig
endef

define Package/$(PKG_NAME)/description
Expand Down

0 comments on commit 27449cd

Please sign in to comment.