-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservices.py
More file actions
126 lines (105 loc) · 4.1 KB
/
services.py
File metadata and controls
126 lines (105 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
Tooling for systemd services
"""
import os
import shutil
from typing import List
import click
from deb_pack.context import ServiceUnit
_install_path = "lib/systemd/system"
_post_inst = """
# Automatically added by dh_systemd_enable
# This will only remove masks created by d-s-h on package removal.
deb-systemd-helper unmask {0} >/dev/null || true
# was-enabled defaults to true, so new installations run enable.
if deb-systemd-helper --quiet was-enabled {0}; then
# Enables the unit on first installation, creates new
# symlinks on upgrades if the unit file has changed.
deb-systemd-helper enable {0} >/dev/null || true
else
# Update the statefile to add new symlinks (if any), which need to be
# cleaned up on purge. Also remove old symlinks.
deb-systemd-helper update-state {0} >/dev/null || true
fi
# End automatically added section
# Automatically added by dh_systemd_start
if [ -d /run/systemd/system ]; then
systemctl --system daemon-reload >/dev/null || true
deb-systemd-invoke start {0} >/dev/null || true
fi
# End automatically added section
"""
_pre_rm = """
# Automatically added by dh_systemd_start
if [ -d /run/systemd/system ]; then
deb-systemd-invoke stop {0} >/dev/null
fi
"""
_post_rm = """
# Automatically added by dh_systemd_start
if [ -d /run/systemd/system ]; then
systemctl --system daemon-reload >/dev/null || true
fi
# End automatically added section
# Automatically added by dh_installinit
# In case this system is running systemd, we make systemd reload the unit files
# to pick up changes.
if [ -d /run/systemd/system ] ; then
systemctl --system daemon-reload >/dev/null || true
fi
# End automatically added section
# Automatically added by dh_systemd_enable
if [ "$1" = "remove" ]; then
if [ -x "/usr/bin/deb-systemd-helper" ]; then
deb-systemd-helper mask {0} >/dev/null
fi
fi
if [ "$1" = "purge" ]; then
if [ -x "/usr/bin/deb-systemd-helper" ]; then
deb-systemd-helper purge {0} >/dev/null
deb-systemd-helper unmask {0} >/dev/null
fi
fi
# End automatically added section
"""
def _generate(items: List[ServiceUnit], script_body: str) -> str:
output = []
for item in items:
_, item_filename = os.path.split(item.source_path)
output.append(script_body.format(item_filename))
return "\n\n".join(output)
def _build_script(script_name, script_body_template, items: List[ServiceUnit], working_folder: str):
content = _generate(items, script_body_template)
path = os.path.join(working_folder, "DEBIAN", script_name)
if not os.path.exists(path):
click.echo(f" > No {script_name} script, creating it")
with open(path, "w") as handle:
handle.write("#!/bin/sh\nset -e\n")
handle.write(content)
handle.write("\nexit 0\n")
else:
click.echo(f" > Existing {script_name} script, attempting to add service handling to end")
with open(path, "r") as handle:
script_contents = handle.read().strip().strip("exit 0")
script_contents += "\n"
script_contents += content
script_contents += "exit 0\n"
with open(path, "w") as handle:
handle.write(script_contents)
os.chmod(path, 0o755)
def install_services(items: List[ServiceUnit], working_folder: str):
if not items:
click.echo(" > No services")
return
for item in items:
_, item_filename = os.path.split(item.source_path)
dest = os.path.join(working_folder, _install_path, item_filename)
dest_path, _ = os.path.split(dest)
if not os.path.exists(dest_path):
os.makedirs(dest_path)
relative = "/" + os.path.relpath(dest, working_folder)
shutil.copy(item.source_path, dest)
click.echo(f" > Service: {relative}")
_build_script("postinst", _post_inst, items, working_folder)
_build_script("postrm", _post_rm, items, working_folder)
_build_script("prerm", _pre_rm, items, working_folder)