Skip to content

Commit

Permalink
Merge pull request #17 from opensight-cv/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
132ikl authored Jan 12, 2020
2 parents 9f3c2df + a14d933 commit 1b1c993
Show file tree
Hide file tree
Showing 41 changed files with 1,601 additions and 421 deletions.
33 changes: 33 additions & 0 deletions opsi/backend/network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os
from pathlib import Path


def set_network_mode(
dhcp: bool, team_number: int, static_ip_extension: int, lifespan: "Lifespan",
):
template_dir = Path(os.path.dirname(__file__), "templates")
if dhcp:
with open(template_dir / "dhcp.conf", "r") as template:
data = template.read()
with open("/etc/dhcpcd.conf", "w") as output:
output.write(data)
else:
# 4 digit team number with leading zeros
team_padded = f"{team_number:04d}"
router_ip = f"10.{team_padded[0:2]}.{team_padded[2:4]}.1"
static_ip = f"10.{team_padded[0:2]}.{team_padded[2:4]}.{static_ip_extension}"
with open(template_dir / "static.conf", "r") as template:
data = template.read()
data = data.replace("STATIC_IP_ADDR", static_ip).replace(
"ROUTER_IP_ADDR", router_ip
)
with open("/etc/dhcpcd.conf", "w") as output:
output.write(data)

lifespan.restart(host=True)


def dhcpcd_writable():
path = "/etc/dhcpcd.conf"
if os.path.exists(path) and os.path.isfile(path):
return os.access(path, os.W_OK)
59 changes: 59 additions & 0 deletions opsi/backend/templates/dhcp.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# A sample configuration for dhcpcd.
# See dhcpcd.conf(5) for details.

# Allow users of this group to interact with dhcpcd via the control socket.
#controlgroup wheel

# Inform the DHCP server of our hostname for DDNS.
hostname

# Use the hardware address of the interface for the Client ID.
clientid
# or
# Use the same DUID + IAID as set in DHCPv6 for DHCPv4 ClientID as per RFC4361.
# Some non-RFC compliant DHCP servers do not reply with this set.
# In this case, comment out duid and enable clientid above.
#duid

# Persist interface configuration when dhcpcd exits.
persistent

# Rapid commit support.
# Safe to enable by default because it requires the equivalent option set
# on the server to actually work.
option rapid_commit

# A list of options to request from the DHCP server.
option domain_name_servers, domain_name, domain_search, host_name
option classless_static_routes
# Respect the network MTU. This is applied to DHCP routes.
option interface_mtu

# Most distributions have NTP support.
#option ntp_servers

# A ServerID is required by RFC2131.
require dhcp_server_identifier

# Generate SLAAC address using the Hardware Address of the interface
#slaac hwaddr
# OR generate Stable Private IPv6 Addresses based from the DUID
slaac private

# Example static IP configuration:
#interface eth0
#static ip_address=192.168.0.10/24
#static ip6_address=fd51:42f8:caae:d92e::ff/64
#static routers=192.168.0.1
#static domain_name_servers=192.168.0.1 8.8.8.8 fd51:42f8:caae:d92e::1

# It is possible to fall back to a static IP if DHCP fails:
# define static profile
#profile static_eth0
#static ip_address=192.168.1.23/24
#static routers=192.168.1.1
#static domain_name_servers=192.168.1.1

# fallback to static profile on eth0
#interface eth0
#fallback static_eth0
58 changes: 58 additions & 0 deletions opsi/backend/templates/static.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# A sample configuration for dhcpcd.
# See dhcpcd.conf(5) for details.

# Allow users of this group to interact with dhcpcd via the control socket.
#controlgroup wheel

# Inform the DHCP server of our hostname for DDNS.
hostname

# Use the hardware address of the interface for the Client ID.
clientid
# or
# Use the same DUID + IAID as set in DHCPv6 for DHCPv4 ClientID as per RFC4361.
# Some non-RFC compliant DHCP servers do not reply with this set.
# In this case, comment out duid and enable clientid above.
#duid

# Persist interface configuration when dhcpcd exits.
persistent

# Rapid commit support.
# Safe to enable by default because it requires the equivalent option set
# on the server to actually work.
option rapid_commit

# A list of options to request from the DHCP server.
option domain_name_servers, domain_name, domain_search, host_name
option classless_static_routes
# Respect the network MTU. This is applied to DHCP routes.
option interface_mtu

# Most distributions have NTP support.
#option ntp_servers

# A ServerID is required by RFC2131.
require dhcp_server_identifier

# Generate SLAAC address using the Hardware Address of the interface
#slaac hwaddr
# OR generate Stable Private IPv6 Addresses based from the DUID
slaac private

# Example static IP configuration:
interface eth0
static ip_address=STATIC_IP_ADDR/24
static routers=ROUTER_IP_ADDR
#static domain_name_servers=192.168.0.1 8.8.8.8 fd51:42f8:caae:d92e::1

# It is possible to fall back to a static IP if DHCP fails:
# define static profile
#profile static_eth0
#static ip_address=192.168.1.23/24
#static routers=192.168.1.1
#static domain_name_servers=192.168.1.1

# fallback to static profile on eth0
#interface eth0
#fallback static_eth0
16 changes: 0 additions & 16 deletions opsi/backend/upgrade.sh

This file was deleted.

38 changes: 30 additions & 8 deletions opsi/frontend/templates/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,50 @@ <h2>Import/Export</h2>
<input id="import-button" type="button" value="Import" />
<input id="export-button" type="button" value="Export" />
</div>
<div class="preference">
<div class="preference network-settings">
<h2>Network Config</h2>
<form id="network-form" enctype="multipart/form-data" method="post" name="import-form">
<div id="net-normal-settings">
<h3>General</h3>
<label for="team-number">Team Number:</label>
<input id="team-number" type="number" min="1" max="9999" step="1" required
value="{{persist.network.team | int}}" />
<br>
<label for="nt-enabled">Use Static IP:</label>
<input id="static" type="checkbox" {{"checked" if persist.network.static}} />
{% if nt %}
<label for="dns-mode">DNS Mode:</label>
<select id="dns-mode">
<option value="mDNS" {{"selected" if persist.network.mdns}}>mDNS</option>
<option value="Static" {{"selected" if not persist.network.mdns}}>Static</option>
</select>
<br>
</div>
{% if netconf %}
<div id="net-pi-settings">
<label for="ip-assign">IP Assignment Mode:</label>
<select id="ip-assign">
<option value="DHCP" {{"selected" if not persist.network.ip_assign}}>DHCP</option>
<option value="Static" {{"selected" if persist.network.ip_assign}}>Static</option>
</select>
<br>
<div id="static-ext-div">
<label for="static-ext">Static IP Extension:</label>
<input id="static-ext" type="number" min="2" max="255" step="1" required
value="{{persist.network.static_ext | int}}" />
<br>
</div>
</div>
{% endif %}
{% if nt %}
<div id="net-nt-settings">
<h3>NetworkTables</h3>
<label for="nt-enabled">Enabled:</label>
<input id="nt-enabled" type="checkbox" {{"checked" if persist.network.nt_enabled}} />
<br>
<label for="nt-mode">Mode:</label>
<label for="nt-mode">NetworkTables Mode:</label>
<select id="nt-mode">
<option value="client" {{"selected" if persist.network.nt_client}}>Client</option>
<option value="server" {{"selected" if not persist.network.nt_client}}>Server</option>
</select>
{% endif %}
</form>
</div>
{% endif %}
<input id="network-button" type="button" value="Submit" />
</div>
<div class="preference">
Expand Down
4 changes: 2 additions & 2 deletions opsi/frontend/www/scripts/mikenode.js
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ const functions = function(jsonData) {
]);
}
break;
case "box":
case "tup":
if (typeof this.rawArr[i][2][x].params.default != "undefined") {
this.settings[this.settingsCount].push([
x,
Expand Down Expand Up @@ -1308,7 +1308,7 @@ const importNodeTree = function(nodetree, functions) {
);
// $('#' + nodetree.nodes[i].id + x).maxVal(nodetree.nodes[i].settings[x].max);
break;
case "box":
case "tup":
$("#" + nodetree.nodes[i].id + x).val(
nodetree.nodes[i].settings[x]
);
Expand Down
27 changes: 15 additions & 12 deletions opsi/frontend/www/scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,17 @@ $(document).ready(function() {
});
$(document).on("click", "#network-button", function(event) {
var form = $("#network-form")[0];
if (typeof form[2] === undefined) {
var data = {
team: parseInt(form[0].valueAsNumber),
static: form[1].checked,
};
} else {
var data = {
team: parseInt(form[0].valueAsNumber),
static: form[1].checked,
nt_enabled: form[2].checked,
nt_client: form[3].value == "client"
};
var data = {
team: $("#team-number")[0].valueAsNumber,
mDNS: ($("#dns-mode")[0].value === "mDNS")
};
if ($("#net-pi-settings").length > 0) {
data['dhcp'] = ($("#ip-assign")[0].value === "DHCP")
data['static_ext'] = $("#static-ext")[0].valueAsNumber;
}
if ($("#net-nt-settings").length > 0) {
data['nt_enabled'] = $("#nt-enabled")[0].checked;
data['nt_client'] = ($("#nt-mode")[0].value === "client");
}
setIcons("spinner");
$.ajax({
Expand All @@ -159,5 +158,9 @@ $(document).ready(function() {
}
});
});
$("#static-ext").prop('disabled', ($("#ip-assign").value == 'Static') ? false : true);
$('#ip-assign').on('change', function () {
$("#static-ext").prop('disabled', (this.value == 'Static') ? false : true);
});
});

7 changes: 5 additions & 2 deletions opsi/frontend/www/styles/node.css
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ p {
margin: 5px auto;
}

#network-form input {
.network-settings input {
display: inline;
max-width: 25%;
}
.network-settings input[type="number"] {
max-width: 18%;
}
/* https://stackoverflow.com/a/4298216 */
Expand Down Expand Up @@ -508,7 +511,7 @@ input[type="button"]:active {
}
.sliderOut {
width: 10%;
float: left;
margin-bottom: 0;
}
.sliderOut2 {
width: 10%;
Expand Down
11 changes: 11 additions & 0 deletions opsi/lifespan/lifespan.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from os.path import isdir, isfile, splitext

import opsi
from opsi.backend.network import dhcpcd_writable
from opsi.manager import Program
from opsi.manager.manager_schema import ModulePath
from opsi.util.concurrency import AsyncThread, ShutdownThread
Expand Down Expand Up @@ -72,6 +73,7 @@ def __init__(self, args, *, catch_signal=False, load_persist=True, timeout=10):

self._systemd = None
self._unit = None
self._netconf = None

self.ports = args.port or self.PORTS
self.persist = Persistence(path=args.persist) if load_persist else None
Expand All @@ -98,6 +100,15 @@ def using_systemd(self):
self._systemd = self._unit.Unit.ActiveState == b"active"
return self._systemd

@property
def netconf_writable(self):
if self._netconf is not None:
return self._netconf
self._netconf = False
if self.using_systemd and dhcpcd_writable():
self._netconf = True
return self._netconf

def make_threads(self):
program = Program(self)

Expand Down
Loading

0 comments on commit 1b1c993

Please sign in to comment.