support different users for autostart

This commit is contained in:
avollkopf 2023-10-15 09:59:05 +02:00
parent 0ec02447d5
commit 4721cf3a8c
4 changed files with 29 additions and 15 deletions

View file

@ -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"):

View file

@ -1,9 +0,0 @@
[Unit]
Description=Craftbeer Pi
[Service]
WorkingDirectory=/home/pi
ExecStart=/home/pi/.local/bin/cbpi start
[Install]
WantedBy=multi-user.target

View file

@ -0,0 +1,9 @@
[Unit]
Description=Craftbeer Pi
[Service]
WorkingDirectory=/home/{{ user }}
ExecStart=/home/{{ user }}/.local/bin/cbpi start
[Install]
WantedBy=multi-user.target

View file

@ -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")