+55
-15
@@ -1,12 +1,34 @@
|
||||
#! /usr/bin/env Python3
|
||||
|
||||
import os
|
||||
import os
|
||||
from posix import stat
|
||||
from jinja2 import Template
|
||||
import subprocess
|
||||
|
||||
|
||||
def is_root():
|
||||
if os.geteuid() == 0:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
class create_unit_service():
|
||||
def __init__(self, UnitName = str,systemd_sysfolder = "/etc/systemd/system", execstart = str, systemd_type = "simple") -> None:
|
||||
def __init__(self,
|
||||
UnitName: str,
|
||||
systemd_sysfolder: str = "/etc/systemd/system",
|
||||
UnitExecStart = str,
|
||||
systemd_type: str = "simple",
|
||||
UnitDescription: str = "Meine Unit",
|
||||
UnitUser = str,
|
||||
UnitWorkDir: str = "/home/jonnybravo/"
|
||||
) -> None:
|
||||
|
||||
self.UnitName = UnitName
|
||||
self.systemd_systemfolder = systemd_sysfolder
|
||||
self.execstart = execstart
|
||||
self.UnitExecStart = UnitExecStart
|
||||
self.UnitDescription = UnitDescription
|
||||
self.UnitWorkDir = UnitWorkDir
|
||||
self.UnitUser = UnitUser
|
||||
if systemd_type == "simple" or systemd_type == "oneshot":
|
||||
self.systemd_type = systemd_type
|
||||
else:
|
||||
@@ -20,20 +42,38 @@ class create_unit_service():
|
||||
print("Gibt es bereits")
|
||||
else:
|
||||
print("Wird angelegt !!")
|
||||
with open(UnitFile, "w") as ufile:
|
||||
print("[Unit]",
|
||||
"[Service]",
|
||||
"Type=oneshot",
|
||||
"PrivateTmp=yes",
|
||||
"DynamicUser=yes",
|
||||
"ExecStart={my_execStart}".format(my_execStart=self.execstart), file=ufile, sep="\n")
|
||||
os.popen("systemctl daemon-reload")
|
||||
with open('template/LinuxUnit.j2') as f:
|
||||
tmpl = Template(f.read())
|
||||
with open(UnitFile, "w") as uf:
|
||||
print(tmpl.render(
|
||||
description = self.UnitDescription,
|
||||
working_dir = self.UnitWorkDir,
|
||||
exec_command = self.UnitExecStart,
|
||||
user = self.UnitUser,
|
||||
env_vars = {"PYTHONPATH": "."},
|
||||
service_type = self.systemd_type
|
||||
),file=uf)
|
||||
|
||||
def unit_start_and_load(self):
|
||||
UnitFile = self.systemd_systemfolder + os.sep + self.UnitName + ".service"
|
||||
status_daemon_reload = subprocess.run(["systemctl", "daemon-reload"])
|
||||
if status_daemon_reload.returncode != 0:
|
||||
raise Exception( f'Systemd reload Invalid result: { status_daemon_reload.returncode }' )
|
||||
else:
|
||||
print(f"{self.UnitName} wurde angelegt und kann genutzt werden !!! Ein starten oder Enable wurde bewusst nicht eingebaut.")
|
||||
|
||||
def delete_unit(self):
|
||||
pass
|
||||
|
||||
if __name__ == "__main__":
|
||||
new_unit = create_unit_service(UnitName="TestUnit",
|
||||
execstart="""/usr/bin/echo "Ich bin eine TestUnit %p" """,
|
||||
systemd_type="oneshot")
|
||||
new_unit.create_unit()
|
||||
if is_root():
|
||||
new_unit = create_unit_service(UnitName="TestUnit",
|
||||
UnitExecStart="""/usr/bin/echo "Ich bin eine TestUnit %p" """,
|
||||
UnitUser="jonnybravo",
|
||||
systemd_type="oneshot")
|
||||
new_unit.create_unit()
|
||||
new_unit.unit_start_and_load()
|
||||
else:
|
||||
print("Du bist nicht root führe das Skript mit sudo aus")
|
||||
print('sudo python <skript>')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user