Add CLI management system

This commit is contained in:
Zeva Rose 2021-06-24 19:13:10 -06:00
parent 89ecae3957
commit 1b0b54c5f2
3 changed files with 104 additions and 1 deletions

3
.gitignore vendored
View file

@ -139,3 +139,6 @@ cython_debug/
# Config file
config.yaml
# PID files
*.pid

View file

@ -1,3 +1,4 @@
from pathlib import Path
from discord import Intents
from discord.ext import commands
from discord.utils import find
@ -18,6 +19,8 @@ jarvis_self = Process()
async def on_ready():
print("Logged in as {0.user}".format(jarvis))
print("Connected to {} guild(s)".format(len(jarvis.guilds)))
with jarvis_self.oneshot():
Path(f"jarvis.{jarvis_self.pid}.pid").touch()
@jarvis.event
@ -45,3 +48,4 @@ def run():
+ "{}&permissions=8&scope=bot".format(config.client_id)
)
jarvis.run(config.token, bot=True, reconnect=True)
Path(f"jarvis.{jarvis_self.pid}.pid").unlink()

98
run.py
View file

@ -1,5 +1,101 @@
#!/bin/python3
import jarvis
import os
import signal
import git
from pathlib import Path
from multiprocessing import Process
from importlib import reload as ireload
from time import sleep
def run():
ireload(jarvis)
jarvis.run()
jarvis_process = Process(target=run, name="jarvis")
def start_process():
global jarvis_process
if jarvis_process.is_alive():
jarvis_process.terminate()
jarvis_process.join(10)
jarvis_process = Process(target=run, name="jarvis")
jarvis_process.start()
def update():
repo = git.Repo(".")
dirty = repo.is_dirty()
if dirty:
print(" Local system has uncommitted changes.")
current_hash = repo.head.object.hexsha
origin = repo.remotes.origin
origin.fetch()
if current_hash != origin.refs["main"].object.hexsha:
if dirty:
return 2
origin.pull()
return 0
return 1
def cli():
pid_file = Path(f"jarvis.{jarvis_process.pid}.pid")
while not pid_file.exists():
sleep(0.2)
print(
"""
All systems online.
Command List:
(R)eload
(U)pdate
(Q)uit
"""
)
while True:
cmd = input("> ")
if cmd.lower() in ["q", "quit", "e", "exit"]:
pid_file.unlink()
break
if cmd.lower() in ["u", "update"]:
print("-> Updating core systems...")
status = update()
if status == 0:
start_process()
pid_file = Path(f"jarvis.{jarvis_process.pid}.pid")
while not pid_file.exists():
sleep(0.2)
print(" Core systems successfully updated.")
elif status == 1:
print(" No core updates available.")
elif status == 2:
print(" Core updates available, but not applied.")
if cmd.lower() in ["r", "reload"]:
print(" Reloading core systems...")
pid_file.unlink()
start_process()
pid_file = Path(f"jarvis.{jarvis_process.pid}.pid")
while not pid_file.exists():
sleep(0.2)
print(" All systems reloaded.")
if __name__ == "__main__":
jarvis.run()
print("J.A.R.V.I.S. initializing....")
print(" Updating core systems...")
status = update()
if status == 0:
print(" Core systems successfully updated")
elif status == 1:
print(" No core updates available.")
elif status == 2:
print(" Core updates available, but not applied.")
start_process()
cli()
if jarvis_process.is_alive():
jarvis_process.terminate()
jarvis_process.join(30)