From 249d11ccee0bccbd4785794913c44c2eafc2c460 Mon Sep 17 00:00:00 2001
From: xengineering <mail2xengineering@protonmail.com>
Date: Sun, 7 Mar 2021 11:10:38 +0100
Subject: Implement Deployment

---
 Makefile                            |  9 ++++++---
 scripts/install                     | 27 +++++++++++++++++++++++++++
 scripts/uninstall                   | 19 +++++++++++++++++++
 src/Makefile                        |  3 ---
 systemd/web_template.service.jinja2 | 14 ++++++++++++++
 5 files changed, 66 insertions(+), 6 deletions(-)
 create mode 100755 scripts/install
 create mode 100755 scripts/uninstall
 create mode 100644 systemd/web_template.service.jinja2

diff --git a/Makefile b/Makefile
index 8814205..66854cc 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,11 @@
 # vim: shiftwidth=4 tabstop=4 noexpandtab
 
-all:
-	echo "make all - nothing to do in this pure python project ;)"
-
 debug:
 	make -C src/ debug
 
+install:
+	./scripts/install
+
+uninstall:
+	./scripts/uninstall
+
diff --git a/scripts/install b/scripts/install
new file mode 100755
index 0000000..bbba17c
--- /dev/null
+++ b/scripts/install
@@ -0,0 +1,27 @@
+#!/usr/bin/python3
+# vim: shiftwidth=4 tabstop=4 expandtab
+
+
+import configparser
+import jinja2
+import subprocess
+
+
+# read config
+config = configparser.ConfigParser()
+config.read("./src/config.ini")
+cfg = {}
+for option in config.options("web-template"):
+    cfg[option] = config.get("web-template", option)
+
+# generate systemd unit file
+with open("./systemd/web_template.service.jinja2", "r") as f:
+    template = jinja2.Template(f.read())
+unit_file = template.render(cfg)
+
+# install the project
+subprocess.run("mkdir -p /opt/{}".format(cfg["project_name"]), shell=True)
+subprocess.run("rsync --delete -vrL ./src/ /opt/{}/".format(cfg["project_name"]), shell=True)
+with open("/etc/systemd/system/{}.service".format(cfg["project_name"]), "w") as f:
+    f.write(unit_file)
+
diff --git a/scripts/uninstall b/scripts/uninstall
new file mode 100755
index 0000000..9b1b8e7
--- /dev/null
+++ b/scripts/uninstall
@@ -0,0 +1,19 @@
+#!/usr/bin/python3
+# vim: shiftwidth=4 tabstop=4 expandtab
+
+
+import configparser
+import subprocess
+
+
+# read config
+config = configparser.ConfigParser()
+config.read("./src/config.ini")
+cfg = {}
+for option in config.options("web-template"):
+    cfg[option] = config.get("web-template", option)
+
+# uninstall the project
+subprocess.run("rm -rf /opt/{}/".format(cfg["project_name"]), shell=True)
+subprocess.run("rm /etc/systemd/system/{}.service".format(cfg["project_name"]), shell=True)
+
diff --git a/src/Makefile b/src/Makefile
index 43ea379..b8e4285 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,8 +1,5 @@
 # vim: shiftwidth=4 tabstop=4 noexpandtab
 
-all:
-	echo "make all - nothing to do in this pure python project ;)"
-
 debug:
 	./server.py
 
diff --git a/systemd/web_template.service.jinja2 b/systemd/web_template.service.jinja2
new file mode 100644
index 0000000..86b3c3d
--- /dev/null
+++ b/systemd/web_template.service.jinja2
@@ -0,0 +1,14 @@
+
+[Unit]
+Description={{ project_description }}
+After=network.target
+
+[Service]
+User={{ user }}
+Group={{ group }}
+WorkingDirectory=/opt/{{ project_name }}/
+ExecStart=python3 /opt/{{ project_name }}/server.py
+
+[Install]
+WantedBy=multi-user.target
+
-- 
cgit v1.2.3-70-g09d2