Implement more automated provisioning of aliases and relays, and remove networks cleanly

This commit is contained in:
Mark Veidemanis 2019-10-02 20:26:05 +01:00
parent 0f31d7f5e2
commit a3b81f8849
6 changed files with 77 additions and 9 deletions

View File

@ -1,5 +1,6 @@
import main
from yaml import dump
from modules import alias
class AliasCommand:
def __init__(self, *args):
@ -7,6 +8,49 @@ class AliasCommand:
def alias(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length):
if authed:
if length == 1:
info(dump(main.alias))
return
elif length == 2:
if spl[1] == "add":
nextNum = max(main.alias.keys())+1
main.alias[nextNum] = alias.generate_alias()
success("Generated new alias: %i" % nextNum)
main.saveConf("alias")
return
else:
incUsage("alias")
return
elif length == 3:
if spl[1] == "add":
if not spl[2].isdigit():
failure("Must be a number, not %s" % spl[2])
return
num = int(spl[2])
for i in range(num):
nextNum = max(main.alias.keys())+1
main.alias[nextNum] = alias.generate_alias()
success("Generated new alias: %i" % nextNum)
main.saveConf("alias")
return
elif spl[1] == "del":
if not spl[2].isdigit():
failure("Must be a number, not %s" % spl[2])
return
num = int(spl[2])
failed = False
for i in main.network.keys():
if num in main.network[i].aliases.keys():
failure("Alias in use by %s" % i)
failed = True
if failed:
return
del main.alias[num]
success("Removed alias: %i" % num)
main.saveConf("alias")
return
else:
incUsage("alias")
return
else:
incUsage(None)

View File

@ -25,13 +25,23 @@ class AutoCommand:
if not spl[1] in main.network.keys():
failure("No such network: %s" % spl[1])
return
num, alias = main.network[spl[1]].add_relay()
for i in main.alias.keys():
print("num", i)
num, alias = main.network[spl[1]].add_relay(i)
success("Successfully created relay %i on network %s with alias %s" % (num, spl[1], alias))
main.saveConf("network")
rtrn = provision.provisionRelay(num, spl[1])
success("Started provisioning network %s on relay %s for alias %s" % (spl[1], num, rtrn))
main.saveConf("network")
return
elif length == 1:
for i in main.network.keys():
for x in main.alias.keys():
num, alias = main.network[i].add_relay(x)
success("Successfully created relay %i on network %s with alias %s" % (num, i, alias))
rtrn = provision.provisionRelay(num, i)
success("Started provisioning network %s on relay %s for alias %s" % (i, num, rtrn))
main.saveConf("network")
return
else:
incUsage("auto")
return

View File

@ -32,7 +32,7 @@ class DisableCommand:
if name in main.FactoryPool.keys():
main.FactoryPool[name].stopTrying()
main.ReactorPool[name].disconnect()
if spl[1] in main.IRCPool.keys():
if name in main.IRCPool.keys():
del main.IRCPool[name]
del main.ReactorPool[name]
del main.FactoryPool[name]

View File

@ -38,6 +38,7 @@ class NetworkCommand:
elif length == 3:
if spl[1] == "del":
if spl[2] in main.network.keys():
main.network[spl[2]].seppuku() # ;(
del main.network[spl[2]]
success("Successfully removed network: %s" % spl[2])
main.saveConf("network")

View File

@ -20,7 +20,7 @@
"users": "users <channel> [<channel> ...]",
"relay": "relay <add|del|list> [<network>] [<num>]",
"network": "network <add|del|list> [<name> <address> <port> <ssl|plain> <sasl|ns|none>]",
"alias": "alias",
"alias": "alias [<add>]",
"auto": "auto <network> <relay>",
"cmd": "cmd <relay> <user> <entity> <text ...>",
"token": "token <add|del|list> [<key>] [<relay>]",

View File

@ -1,7 +1,7 @@
from twisted.internet.ssl import DefaultOpenSSLContextFactory
import json
import modules.alias as alias
from modules import alias
from twisted.internet import reactor
from core.bot import IRCBot, IRCBotFactory
import main
@ -43,6 +43,19 @@ class Network:
del self.aliases[id]
#del main.alias[id] - Aliases are global per num, so don't delete them!
def seppuku(self):
# Removes all bots in preperation for deletion
for i in self.relays.keys():
name = self.net+str(i)
if name in main.ReactorPool.keys():
if name in main.FactoryPool.keys():
main.FactoryPool[name].stopTrying()
main.ReactorPool[name].disconnect()
if name in main.IRCPool.keys():
del main.IRCPool[name]
del main.ReactorPool[name]
del main.FactoryPool[name]
def start_bot(self, num):
# a single name is given to relays in the backend
# e.g. freenode1 for the first relay on freenode network