From f117835c2c2ffa7192b40f45c8341bc28c1541bc Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Sun, 4 Jul 2021 11:23:19 +0200 Subject: [PATCH] added cbpi service cbpi service can be activated via sudo cbpi add autostart Service can be disabled via sudo cbpi remove autostart sudo cbpi setup has to be triggered once -> this copies the craftbeerpi.service file to the config folder --- cbpi/cli.py | 50 +++++++++++++++++++++++++++++++++ cbpi/config/craftbeerpi.service | 9 ++++++ 2 files changed, 59 insertions(+) create mode 100644 cbpi/config/craftbeerpi.service diff --git a/cbpi/cli.py b/cbpi/cli.py index a63dc09..555c3fc 100644 --- a/cbpi/cli.py +++ b/cbpi/cli.py @@ -54,6 +54,11 @@ def create_config_file(): srcfile = os.path.join(os.path.dirname(__file__), "config", "dashboard", "cbpi_dashboard_1.json") destfile = os.path.join(".", "config", "dashboard") shutil.copy(srcfile, destfile) + + if os.path.exists(os.path.join(".", 'config', "carftbeerpi.service")) is False: + srcfile = os.path.join(os.path.dirname(__file__), "config", "craftbeerpi.service") + destfile = os.path.join(".", 'config') + shutil.copy(srcfile, destfile) print("Config Folder created") @@ -119,6 +124,26 @@ def plugins_add(package_name): if package_name is None: print("Pleaes provide a plugin Name") return + + if package_name == 'autostart': + print("Add cradtbeerpi.service to systemd") + try: + if os.path.exists(os.path.join("/etc/systemd/system","craftbeerpi.service")) is False: + srcfile = os.path.join(".", "config", "craftbeerpi.service") + destfile = os.path.join("/etc/systemd/system") + shutil.copy(srcfile, destfile) + print("Copied craftbeerpi.service to /etc/systemd/system") + os.system('systemctl enable craftbeerpi.service') + print('Enabled craftbeerpi service') + os.system('systemctl start craftbeerpi.service') + print('Started craftbeerpi.service') + else: + print("craftbeerpi.service is already located in /etc/systemd/system") + except Exception as e: + print(e) + return + return + try: with open(os.path.join(".", 'config', "config.yaml"), 'rt') as f: data = yaml.load(f, Loader=yaml.FullLoader) @@ -142,6 +167,31 @@ def plugin_remove(package_name): if package_name is None: print("Pleaes provide a plugin Name") return + + if package_name == 'autostart': + print("Remove cradtbeerpi.service from systemd") + try: + status = os.popen('systemctl list-units --type=service --state=running | grep craftbeerpi.service').read() + if status.find("craftbeerpi.service") != -1: + os.system('systemctl stop craftbeerpi.service') + print('Stopped craftbeerpi service') + os.system('systemctl disable craftbeerpi.service') + print('Removed craftbeerpi.service as service') + else: + print('craftbeerpi.service service is not running') + + if os.path.exists(os.path.join("/etc/systemd/system","craftbeerpi.service")) is True: + os.remove(os.path.join("/etc/systemd/system","craftbeerpi.service")) + print("Deleted craftbeerpi.service from /etc/systemd/system") + else: + print("craftbeerpi.service is not located in /etc/systemd/system") + except Exception as e: + print(e) + return + return + + + try: with open(os.path.join(".", 'config', "config.yaml"), 'rt') as f: data = yaml.load(f, Loader=yaml.FullLoader) diff --git a/cbpi/config/craftbeerpi.service b/cbpi/config/craftbeerpi.service new file mode 100644 index 0000000..cd02dce --- /dev/null +++ b/cbpi/config/craftbeerpi.service @@ -0,0 +1,9 @@ +[Unit] +Description=Craftbeer Pi + +[Service] +WorkingDirectory=/home/pi +ExecStart=/usr/local/bin/cbpi start + +[Install] +WantedBy=multi-user.target