diff --git a/cbpi/cli.py b/cbpi/cli.py index 2b913c1..64eca20 100644 --- a/cbpi/cli.py +++ b/cbpi/cli.py @@ -150,12 +150,27 @@ class CraftBeerPiCli(): else: print("CraftBeerPi Autostart is {}OFF{}".format(Fore.RED,Style.RESET_ALL)) elif(name == "on"): + user=os.getlogin() print("Add craftbeerpi.service to systemd") try: if os.path.exists(os.path.join("/etc/systemd/system","craftbeerpi.service")) is False: + templatefile=self.config.get_file_path("craftbeerpi.template") + shutil.os.system('cp "{}" "{}"'.format(templatefile,self.config.get_file_path("craftbeerpi.service"))) srcfile = self.config.get_file_path("craftbeerpi.service") + import jinja2 + + templateLoader = jinja2.FileSystemLoader(searchpath=os.path.join(self.config.get_file_path(""))) + templateEnv = jinja2.Environment(loader=templateLoader) + operatingsystem = str(platform.system()).lower() + if operatingsystem.startswith("win"): + srcfile=str(srcfile).replace('\\','/') + + template = templateEnv.get_template("craftbeerpi.service") + outputText = template.render(user=user) + with open(srcfile, "w") as fh: + fh.write(outputText) destfile = os.path.join("/etc/systemd/system") - shutil.os.system('sudo cp "{}" "{}"'.format(srcfile,destfile)) + shutil.os.system('sudo mv "{}" "{}"'.format(srcfile,destfile)) print("Copied craftbeerpi.service to /etc/systemd/system") shutil.os.system('sudo systemctl enable craftbeerpi.service') print('Enabled craftbeerpi service') @@ -188,7 +203,6 @@ class CraftBeerPiCli(): print(e) return return - def chromium(self, name): '''Enable or disable autostart''' if(name == "status"): diff --git a/cbpi/config/craftbeerpi.service b/cbpi/config/craftbeerpi.service deleted file mode 100644 index c8f3d9f..0000000 --- a/cbpi/config/craftbeerpi.service +++ /dev/null @@ -1,9 +0,0 @@ -[Unit] -Description=Craftbeer Pi - -[Service] -WorkingDirectory=/home/pi -ExecStart=/home/pi/.local/bin/cbpi start - -[Install] -WantedBy=multi-user.target diff --git a/cbpi/config/craftbeerpi.template b/cbpi/config/craftbeerpi.template new file mode 100644 index 0000000..1fc26e3 --- /dev/null +++ b/cbpi/config/craftbeerpi.template @@ -0,0 +1,9 @@ +[Unit] +Description=Craftbeer Pi + +[Service] +WorkingDirectory=/home/{{ user }} +ExecStart=/home/{{ user }}/.local/bin/cbpi start + +[Install] +WantedBy=multi-user.target diff --git a/cbpi/configFolder.py b/cbpi/configFolder.py index 807550b..2cffc16 100644 --- a/cbpi/configFolder.py +++ b/cbpi/configFolder.py @@ -94,7 +94,7 @@ class ConfigFolder: #['fermenter_data.json', 'file'], created by fermentation_controller @ start if not available #['step_data.json', 'file'], created by step_controller @ start if not available ['config.json', 'file'], - ['craftbeerpi.service', 'file'], + ['craftbeerpi.template', 'file'], ['chromium.desktop', 'file'], ['dashboard', 'folder'], ['dashboard/widgets', 'folder'], @@ -181,7 +181,7 @@ class ConfigFolder: self.copyDefaultFileIfNotExists("fermenter_data.json") self.copyDefaultFileIfNotExists("step_data.json") self.copyDefaultFileIfNotExists("config.json") - self.copyDefaultFileIfNotExists("craftbeerpi.service") + self.copyDefaultFileIfNotExists("craftbeerpi.template") self.copyDefaultFileIfNotExists("chromium.desktop") print("Config Folder created") @@ -206,5 +206,5 @@ class ConfigFolder: shutil.chown(os.path.join(dirpath, filename), owner, group) except: print("problems assigning file or folder permissions") - print("if this happend on windows its fine") - print("if this happend in the dev container running inside windows its also fine but you might have to rebuild the container if you run into further problems") + print("if this happened on windows its fine") + print("if this happened in the dev container running inside windows its also fine but you might have to rebuild the container if you run into further problems")