blob: 7a8956e9397baa71556c3879b41870e45c4891c1 (
plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
PLAYIT_USER=playit
PLAYIT_GROUP=playit
PLAYIT_HOME=/nonexistent
PLAYIT_MANAGER_FILE=/opt/playit/share/init/selected-manager
SYSUSERS_CONFIG=/usr/lib/sysusers.d/playit.conf
SYSTEMD_UNIT=/usr/lib/systemd/system/playit.service
have_command() {
command -v "$1" >/dev/null 2>&1
}
nologin_shell() {
if [ -x /usr/sbin/nologin ]; then
printf '%s\n' /usr/sbin/nologin
elif [ -x /sbin/nologin ]; then
printf '%s\n' /sbin/nologin
else
printf '%s\n' /bin/false
fi
}
group_exists() {
if have_command getent; then
getent group "$PLAYIT_GROUP" >/dev/null 2>&1
else
grep -q "^${PLAYIT_GROUP}:" /etc/group 2>/dev/null
fi
}
user_exists() {
if have_command id; then
id -u "$PLAYIT_USER" >/dev/null 2>&1
else
grep -q "^${PLAYIT_USER}:" /etc/passwd 2>/dev/null
fi
}
ensure_group() {
if group_exists; then
return 0
fi
if have_command groupadd; then
groupadd --system "$PLAYIT_GROUP"
elif have_command addgroup; then
addgroup -S "$PLAYIT_GROUP"
else
echo "Cannot create ${PLAYIT_GROUP} group: groupadd/addgroup not found" >&2
exit 1
fi
}
ensure_user() {
if user_exists; then
return 0
fi
shell="$(nologin_shell)"
if have_command useradd; then
useradd --system --gid "$PLAYIT_GROUP" --home-dir "$PLAYIT_HOME" --no-create-home --shell "$shell" "$PLAYIT_USER"
elif have_command adduser; then
adduser -S -D -H -h "$PLAYIT_HOME" -s "$shell" -G "$PLAYIT_GROUP" "$PLAYIT_USER"
else
echo "Cannot create ${PLAYIT_USER} user: useradd/adduser not found" >&2
exit 1
fi
}
remove_symlink_to_target() {
path="$1"
target="$2"
if [ -L "$path" ] && [ "$(readlink "$path")" = "$target" ]; then
rm -f "$path"
fi
}
handle_legacy_systemd_unit() {
LEGACY_UNIT=/etc/systemd/system/playit.service
if [ -f "$LEGACY_UNIT" ] || [ -L "$LEGACY_UNIT" ]; then
BACKUP_UNIT="${LEGACY_UNIT}.pacsave.$(date -u +%Y%m%d%H%M%S)"
echo "Moving legacy systemd unit ${LEGACY_UNIT} to ${BACKUP_UNIT} because it shadows the packaged unit"
mv "$LEGACY_UNIT" "$BACKUP_UNIT"
elif [ -e "$LEGACY_UNIT" ]; then
echo "Cannot install playit: ${LEGACY_UNIT} exists but is not a file or symlink" >&2
echo "Remove or rename it manually, then reinstall playit." >&2
exit 1
fi
}
remove_legacy_unit_path() {
legacy_unit="$1"
if [ ! -e "$legacy_unit" ] && [ ! -L "$legacy_unit" ]; then
return 0
fi
if [ "$(readlink -f "$legacy_unit" 2>/dev/null || printf '%s\n' "$legacy_unit")" = "$(readlink -f "$SYSTEMD_UNIT" 2>/dev/null || printf '%s\n' "$SYSTEMD_UNIT")" ]; then
return 0
fi
rm -f "$legacy_unit"
}
provision_user() {
if have_command systemd-sysusers; then
systemd-sysusers "$SYSUSERS_CONFIG"
else
ensure_group
ensure_user
fi
}
prepare_filesystem() {
mkdir -p /usr/bin /etc/playit /opt/playit/share/init
ln -sfn /opt/playit/playit /usr/bin/playit
ln -sfn /opt/playit/playitd /usr/bin/playitd
chown "$PLAYIT_USER:$PLAYIT_GROUP" /etc/playit
chmod 0750 /etc/playit
if [ -f /var/log/playit/playit.log ]; then
chmod 0640 /var/log/playit/playit.log
fi
printf '%s\n' systemd > "$PLAYIT_MANAGER_FILE"
chmod 0644 "$PLAYIT_MANAGER_FILE"
if [ -f /etc/playit/playit.toml ]; then
chown "$PLAYIT_USER:$PLAYIT_GROUP" /etc/playit/playit.toml
chmod 0600 /etc/playit/playit.toml
fi
}
configure_service() {
mode="$1"
provision_user
prepare_filesystem
handle_legacy_systemd_unit
remove_legacy_unit_path /lib/systemd/system/playit.service
if have_command systemctl; then
systemctl daemon-reload || true
systemctl enable playit || true
if [ "$mode" = install ]; then
systemctl start playit || true
elif systemctl is-active --quiet playit; then
systemctl restart playit || true
fi
fi
}
post_install() {
configure_service install
}
post_upgrade() {
configure_service upgrade
}
pre_remove() {
if have_command systemctl; then
systemctl stop playit || true
systemctl disable playit || true
fi
remove_legacy_unit_path /lib/systemd/system/playit.service
rm -f /opt/playit/share/init/selected-manager
remove_symlink_to_target /usr/bin/playit /opt/playit/playit
remove_symlink_to_target /usr/bin/playitd /opt/playit/playitd
remove_symlink_to_target /usr/local/bin/playit /opt/playit/playit
remove_symlink_to_target /usr/local/bin/playitd /opt/playit/playitd
}
post_remove() {
if have_command systemctl; then
systemctl daemon-reload || true
fi
}
|