From 49214644ffdfed15f24a0cee5e233dcbd8e22a43 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Fri, 12 Aug 2022 23:32:00 +0100 Subject: [PATCH] Implement migrating networks --- api/views.py | 4 ++++ modules/chankeep.py | 3 ++- modules/network.py | 28 ++++++++++++++++++++++++++++ modules/provision.py | 5 ----- tests/test_chankeep.py | 27 +++++++++++++++++---------- threshold | 4 ++++ 6 files changed, 55 insertions(+), 16 deletions(-) diff --git a/api/views.py b/api/views.py index d5795a2..ce236a4 100644 --- a/api/views.py +++ b/api/views.py @@ -198,6 +198,7 @@ class API(object): def irc_network(self, request, net): if net not in main.network.keys(): return dumps({"success": False, "reason": "no such net."}) + first_relay = helpers.get_first_relay(net) inst = main.network[net] network = {} network["net"] = inst.net @@ -209,6 +210,9 @@ class API(object): network["relays"] = len(inst.relays) network["channels"] = userinfo.getTotalChanNum(net) network["records"] = userinfo.getNumWhoEntries(net) + if first_relay: + network["chanlimit_live"] = first_relay.chanlimit + network["chanlimit_conf"] = inst.chanlimit return dumps(network) @app.route("/irc/network//", methods=["DELETE"]) diff --git a/modules/chankeep.py b/modules/chankeep.py index f91bfcb..5abcb8f 100644 --- a/modules/chankeep.py +++ b/modules/chankeep.py @@ -60,6 +60,7 @@ def getChanFree(net, new): return False return (chanfree, chanlimits.pop()) + def getTotalChans(net): total = 0 for i in getActiveRelays(net): @@ -307,4 +308,4 @@ def chankeep_handler(net, num, listinfo, chanlimit): :param chanlimit: :return: """ - listinfo, mean, sigrelay, relay = _initialList(net, num, listinfo, chanlimit) \ No newline at end of file + listinfo, mean, sigrelay, relay = _initialList(net, num, listinfo, chanlimit) diff --git a/modules/network.py b/modules/network.py index 7aea0b1..758391e 100644 --- a/modules/network.py +++ b/modules/network.py @@ -1,3 +1,5 @@ +from copy import deepcopy + from twisted.internet import reactor from twisted.internet.ssl import DefaultOpenSSLContextFactory @@ -11,6 +13,31 @@ from utils.get import getRelay from utils.logging.log import log +def migrate(): + existing = deepcopy(main.network) + log("Migrating network configuration") + log(f"Existing network configuration: {existing}") + for net, net_inst in existing.items(): + log(f"Migrating network {net}") + net = net_inst.net + host = net_inst.host + port = net_inst.port + security = net_inst.security + auth = net_inst.auth + last = net_inst.last + relays = net_inst.relays + aliases = net_inst.aliases + + new_net = Network(net, host, port, security, auth) + log(f"New network for {net}: {new_net}") + new_net.last = last + new_net.relays = relays + new_net.aliases = aliases + main.network[net] = new_net + main.saveConf("network") + log("Finished migrating network configuration") + + class Network: def __init__(self, net, host, port, security, auth): self.net = net @@ -18,6 +45,7 @@ class Network: self.port = port self.security = security self.auth = auth + self.chanlimit = None self.last = 1 self.relays = {} diff --git a/modules/provision.py b/modules/provision.py index 01d371f..d5a0417 100644 --- a/modules/provision.py +++ b/modules/provision.py @@ -9,11 +9,6 @@ from utils.logging.log import warn def provisionUserNetworkData( num, nick, altnick, ident, realname, emails, network, host, port, security, auth, password ): - print("nick", nick) - print("altnick", altnick) - print("emails", emails) - print("ident", ident) - print("realname", realname) commands = {} stage2commands = {} stage2commands["status"] = [] diff --git a/tests/test_chankeep.py b/tests/test_chankeep.py index f35c653..13c8918 100644 --- a/tests/test_chankeep.py +++ b/tests/test_chankeep.py @@ -1,10 +1,10 @@ -import logging +from math import ceil +from random import randint from unittest import TestCase from unittest.mock import MagicMock, patch -from random import randint + from modules import chankeep -from math import ceil -import heapq + class TestChanKeep(TestCase): def setUp(self): @@ -48,7 +48,7 @@ class TestChanKeep(TestCase): chanlimit = 5 max_chans = instances * chanlimit listinfo = self.generate_listinfo(ranges=[[1000, 1, 2], [200, 400, 800], [10, 1000, 2000]]) - listinfo_num = [x[1] for x in listinfo] + # listinfo_num = [x[1] for x in listinfo] listlength = len(listinfo) cumul = 0 @@ -71,15 +71,22 @@ class TestChanKeep(TestCase): sigrelay = ceil(siglength / chanlimit) relay = ceil(listlength / chanlimit) - print(f"len:{listlength} cumul:{cumul} mean:{mean} siglength:{siglength} insiglength:{insiglength} sigrelay:{sigrelay} relay:{relay} sigcumul:{sigcumul} insigcumul:{insigcumul}") + print( + ( + f"len:{listlength} cumul:{cumul} mean:{mean} " + f"siglength:{siglength} insiglength:{insiglength} " + f"sigrelay:{sigrelay} relay:{relay} sigcumul:{sigcumul} " + f"insigcumul:{insigcumul}" + ) + ) # We want a return between 1000 and 1100 - list_insig = [x for x in listinfo_num if x < mean] + # list_insig = [x for x in listinfo_num if x < mean] list_sig = [x for x in listinfo if x[1] > mean] chosen = sorted(list_sig, reverse=True, key=lambda x: x[1])[:max_chans] print("CHOSEN", chosen) self.assertEqual(len(chosen), 5) - + @patch("modules.chankeep.keepChannels") def test__initialList(self, keepchannels): chankeep._initialList(self.net, self.num, self.listinfo, self.chanlimit) @@ -91,7 +98,7 @@ class TestChanKeep(TestCase): @patch("modules.chankeep.getChanFree") def test_empty_chan_allocate(self, getchanfree): - getchanfree.return_value = ({1: 600}, 600) # pretend we have 600 channels free + getchanfree.return_value = ({1: 600}, 600) # pretend we have 600 channels free eca = chankeep.emptyChanAllocate(self.net, self.chan_name_list, []) self.assertEqual(len(eca), 1) num = list(eca.keys())[0] @@ -106,4 +113,4 @@ class TestChanKeep(TestCase): chans = eca[list(eca.keys())[0]] self.assertEqual(num, self.num) self.assertEqual(len(chans), 100) - #self.assertCountEqual(chans, self.chan_name_list) \ No newline at end of file + # self.assertCountEqual(chans, self.chan_name_list) diff --git a/threshold b/threshold index c728709..3b4bbbf 100755 --- a/threshold +++ b/threshold @@ -29,7 +29,11 @@ if "--debug" in sys.argv: # yes really main.config["Debug"] = True if "--trace" in sys.argv: main.config["Trace"] = True +if "--migrate" in sys.argv: + from modules.network import migrate + migrate() + exit() loadCommands()