summaryrefslogtreecommitdiffstats
path: root/src/debputy/plugin/debputy/service_management.py
blob: 1ec8c1b4511f606b64b92280cfa994558ace5189 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import collections
import dataclasses
import os
import textwrap
from typing import Dict, List, Literal, Iterable, Sequence

from debputy.packages import BinaryPackage
from debputy.plugin.api.spec import (
    ServiceRegistry,
    VirtualPath,
    PackageProcessingContext,
    BinaryCtrlAccessor,
    ServiceDefinition,
)
from debputy.util import _error, assume_not_none

DPKG_ROOT = '"${DPKG_ROOT}"'
EMPTY_DPKG_ROOT_CONDITION = '[ -z "${DPKG_ROOT}" ]'
SERVICE_MANAGER_IS_SYSTEMD_CONDITION = "[ -d /run/systemd/system ]"


@dataclasses.dataclass(slots=True)
class SystemdServiceContext:
    had_install_section: bool


@dataclasses.dataclass(slots=True)
class SystemdUnit:
    path: VirtualPath
    names: List[str]
    type_of_service: str
    service_scope: str
    enable_by_default: bool
    start_by_default: bool
    had_install_section: bool


def detect_systemd_service_files(
    fs_root: VirtualPath,
    service_registry: ServiceRegistry[SystemdServiceContext],
    context: PackageProcessingContext,
) -> None:
    pkg = context.binary_package
    systemd_units = _find_and_analyze_systemd_service_files(pkg, fs_root, "system")
    for unit in systemd_units:
        service_registry.register_service(
            unit.path,
            unit.names,
            type_of_service=unit.type_of_service,
            service_scope=unit.service_scope,
            enable_by_default=unit.enable_by_default,
            start_by_default=unit.start_by_default,
            default_upgrade_rule="restart" if unit.start_by_default else "do-nothing",
            service_context=SystemdServiceContext(
                unit.had_install_section,
            ),
        )


def generate_snippets_for_systemd_units(
    services: Sequence[ServiceDefinition[SystemdServiceContext]],
    ctrl: BinaryCtrlAccessor,
    _context: PackageProcessingContext,
) -> None:
    stop_in_prerm: List[str] = []
    stop_then_start_scripts = []
    on_purge = []
    start_on_install = []
    action_on_upgrade = collections.defaultdict(list)
    assert services

    for service_def in services:
        if service_def.auto_enable_on_install:
            template = """\
                if deb-systemd-helper debian-installed {UNITFILE}; then
                    # The following line should be removed in trixie or trixie+1
                    deb-systemd-helper unmask {UNITFILE} >/dev/null || true

                    if deb-systemd-helper --quiet was-enabled {UNITFILE}; then
                        # Create new symlinks, if any.
                        deb-systemd-helper enable {UNITFILE} >/dev/null || true
                    fi
                fi

                # 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 {UNITFILE} >/dev/null || true
            """
        else:
            template = """\
                # The following line should be removed in trixie or trixie+1
                deb-systemd-helper unmask {UNITFILE} >/dev/null || true

                # was-enabled defaults to true, so new installations run enable.
                if deb-systemd-helper --quiet was-enabled {UNITFILE}; then
                    # Enables the unit on first installation, creates new
                    # symlinks on upgrades if the unit file has changed.
                    deb-systemd-helper enable {UNITFILE} >/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 {UNITFILE} >/dev/null || true
                fi
            """
        service_name = service_def.name

        if assume_not_none(service_def.service_context).had_install_section:
            ctrl.maintscript.on_configure(
                template.format(
                    UNITFILE=ctrl.maintscript.escape_shell_words(service_name),
                )
            )
            on_purge.append(service_name)
        elif service_def.auto_enable_on_install:
            _error(
                f'The service "{service_name}" cannot be enabled under "systemd" as'
                f' it has no "[Install]" section. Please correct {service_def.definition_source}'
                f' so that it does not enable the service or does not apply to "systemd"'
            )

        if service_def.auto_start_in_install:
            start_on_install.append(service_name)
        if service_def.on_upgrade == "stop-then-start":
            stop_then_start_scripts.append(service_name)
        elif service_def.on_upgrade in ("restart", "reload"):
            action: str = service_def.on_upgrade
            if not service_def.auto_start_in_install and action != "reload":
                action = f"try-{action}"
            action_on_upgrade[action].append(service_name)
        elif service_def.on_upgrade != "do-nothing":
            raise AssertionError(
                f"Missing support for on_upgrade rule: {service_def.on_upgrade}"
            )

    if start_on_install or action_on_upgrade:
        lines = [
            "if {EMPTY_DPKG_ROOT_CONDITION} && {SERVICE_MANAGER_IS_SYSTEMD_CONDITION}; then".format(
                EMPTY_DPKG_ROOT_CONDITION=EMPTY_DPKG_ROOT_CONDITION,
                SERVICE_MANAGER_IS_SYSTEMD_CONDITION=SERVICE_MANAGER_IS_SYSTEMD_CONDITION,
            ),
            "    systemctl --system daemon-reload >/dev/null || true",
        ]
        if stop_then_start_scripts:
            unit_files = ctrl.maintscript.escape_shell_words(*stop_then_start_scripts)
            lines.append(
                "        deb-systemd-invoke start {UNITFILES} >/dev/null || true".format(
                    UNITFILES=unit_files,
                )
            )
        if start_on_install:
            lines.append('    if [ -z "$2" ]; then')
            lines.append(
                "        deb-systemd-invoke start {UNITFILES} >/dev/null || true".format(
                    UNITFILES=ctrl.maintscript.escape_shell_words(*start_on_install),
                )
            )
            lines.append("    fi")
        if action_on_upgrade:
            lines.append('    if [ -n "$2" ]; then')
            for action, units in action_on_upgrade.items():
                lines.append(
                    "        deb-systemd-invoke {ACTION} {UNITFILES} >/dev/null || true".format(
                        ACTION=action,
                        UNITFILES=ctrl.maintscript.escape_shell_words(*units),
                    )
                )
            lines.append("    fi")
        lines.append("fi")
        combined = "".join(x if x.endswith("\n") else f"{x}\n" for x in lines)
        ctrl.maintscript.on_configure(combined)

    if stop_then_start_scripts:
        ctrl.maintscript.unconditionally_in_script(
            "preinst",
            textwrap.dedent(
                """\
            if {EMPTY_DPKG_ROOT_CONDITION} && [ "$1" = upgrade ] && {SERVICE_MANAGER_IS_SYSTEMD_CONDITION} ; then
                deb-systemd-invoke stop {UNIT_FILES} >/dev/null || true
            fi
            """.format(
                    EMPTY_DPKG_ROOT_CONDITION=EMPTY_DPKG_ROOT_CONDITION,
                    SERVICE_MANAGER_IS_SYSTEMD_CONDITION=SERVICE_MANAGER_IS_SYSTEMD_CONDITION,
                    UNIT_FILES=ctrl.maintscript.escape_shell_words(
                        *stop_then_start_scripts
                    ),
                )
            ),
        )

    if stop_in_prerm:
        ctrl.maintscript.on_before_removal(
            """\
        if {EMPTY_DPKG_ROOT_CONDITION} && {SERVICE_MANAGER_IS_SYSTEMD_CONDITION} ; then
            deb-systemd-invoke stop {UNIT_FILES} >/dev/null || true
        fi
        """.format(
                EMPTY_DPKG_ROOT_CONDITION=EMPTY_DPKG_ROOT_CONDITION,
                SERVICE_MANAGER_IS_SYSTEMD_CONDITION=SERVICE_MANAGER_IS_SYSTEMD_CONDITION,
                UNIT_FILES=ctrl.maintscript.escape_shell_words(*stop_in_prerm),
            )
        )
    if on_purge:
        ctrl.maintscript.on_purge(
            """\
        if [ -x "/usr/bin/deb-systemd-helper" ]; then
            deb-systemd-helper purge {UNITFILES} >/dev/null || true
        fi
        """.format(
                UNITFILES=ctrl.maintscript.escape_shell_words(*stop_in_prerm),
            )
        )
    ctrl.maintscript.on_removed(
        textwrap.dedent(
            """\
        if {SERVICE_MANAGER_IS_SYSTEMD_CONDITION} ; then
            systemctl --system daemon-reload >/dev/null || true
        fi
        """.format(
                SERVICE_MANAGER_IS_SYSTEMD_CONDITION=SERVICE_MANAGER_IS_SYSTEMD_CONDITION
            )
        )
    )


def _remove_quote(v: str) -> str:
    if v and v[0] == v[-1] and v[0] in ('"', "'"):
        return v[1:-1]
    return v


def _find_and_analyze_systemd_service_files(
    pkg: BinaryPackage,
    fs_root: VirtualPath,
    systemd_service_dir: Literal["system", "user"],
) -> Iterable[SystemdUnit]:
    service_dirs = [
        f"./usr/lib/systemd/{systemd_service_dir}",
        f"./lib/systemd/{systemd_service_dir}",
    ]
    had_install_sections = set()
    aliases: Dict[str, List[str]] = collections.defaultdict(list)
    seen = set()
    all_files = []
    expected_units = set()
    expected_units_required_by = collections.defaultdict(list)

    for d in service_dirs:
        system_dir = fs_root.lookup(d)
        if not system_dir:
            continue
        for child in system_dir.iterdir:
            if child.is_symlink:
                dest = os.path.basename(child.readlink())
                aliases[dest].append(child.name)
            elif child.is_file and child.name not in seen:
                seen.add(child.name)
                all_files.append(child)
                if "@" in child.name:
                    # dh_installsystemd does not check the contents of templated services,
                    # and we match that.
                    continue
                with child.open() as fd:
                    for line in fd:
                        line = line.strip()
                        line_lc = line.lower()
                        if line_lc == "[install]":
                            had_install_sections.add(child.name)
                        elif line_lc.startswith("alias="):
                            # This code assumes service names cannot contain spaces (as in
                            # if you copy-paste it for another field it might not work)
                            aliases[child.name].extend(
                                _remove_quote(x) for x in line[6:].split()
                            )
                        elif line_lc.startswith("also="):
                            # This code assumes service names cannot contain spaces (as in
                            # if you copy-paste it for another field it might not work)
                            for unit in (_remove_quote(x) for x in line[5:].split()):
                                expected_units_required_by[unit].append(child.absolute)
                                expected_units.add(unit)
    for path in all_files:
        if "@" in path.name:
            # Match dh_installsystemd, which skips templated services
            continue
        names = aliases[path.name]
        _, type_of_service = path.name.rsplit(".", 1)
        expected_units.difference_update(names)
        expected_units.discard(path.name)
        names.extend(x[:-8] for x in list(names) if x.endswith(".service"))
        names.insert(0, path.name)
        if path.name.endswith(".service"):
            names.insert(1, path.name[:-8])
        yield SystemdUnit(
            path,
            names,
            type_of_service,
            systemd_service_dir,
            # Bug (?) compat with dh_installsystemd. All units are started, but only
            # enable those with an `[Install]` section.
            # Possibly related bug #1055599
            enable_by_default=path.name in had_install_sections,
            start_by_default=True,
            had_install_section=path.name in had_install_sections,
        )

    if expected_units:
        for unit_name in expected_units:
            required_by = expected_units_required_by[unit_name]
            required_names = ", ".join(required_by)
            _error(
                f"The unit {unit_name} was required by {required_names} (via Also=...)"
                f" but was not present in the package {pkg.name}"
            )


def generate_snippets_for_init_scripts(
    services: Sequence[ServiceDefinition[None]],
    ctrl: BinaryCtrlAccessor,
    _context: PackageProcessingContext,
) -> None:
    for service_def in services:
        script_name = service_def.path.name
        script_installed_path = service_def.path.absolute

        update_rcd_params = (
            "defaults" if service_def.auto_enable_on_install else "defaults-disabled"
        )

        ctrl.maintscript.unconditionally_in_script(
            "preinst",
            textwrap.dedent(
                """\
            if [ "$1" = "install" ] && [ -n "$2" ] && [ -x {DPKG_ROOT}{SCRIPT_PATH} ] ; then
                chmod +x {DPKG_ROOT}{SCRIPT_PATH} >/dev/null || true
            fi
               """.format(
                    DPKG_ROOT=DPKG_ROOT,
                    SCRIPT_PATH=ctrl.maintscript.escape_shell_words(
                        script_installed_path
                    ),
                )
            ),
        )

        lines = [
            "if {EMPTY_DPKG_ROOT_CONDITION} && [ -x {SCRIPT_PATH} ]; then",
            "    update-rc.d {SCRIPT_NAME} {UPDATE_RCD_PARAMS} >/dev/null || exit 1",
        ]

        if (
            service_def.auto_start_in_install
            and service_def.on_upgrade != "stop-then-start"
        ):
            lines.append('    if [ -z "$2" ]; then')
            lines.append(
                "        invoke-rc.d --skip-systemd-native {SCRIPT_NAME} start >/dev/null || exit 1".format(
                    SCRIPT_NAME=ctrl.maintscript.escape_shell_words(script_name),
                )
            )
            lines.append("    fi")

        if service_def.on_upgrade in ("restart", "reload"):
            lines.append('    if [ -n "$2" ]; then')
            lines.append(
                "        invoke-rc.d --skip-systemd-native {SCRIPT_NAME} {ACTION} >/dev/null || exit 1".format(
                    SCRIPT_NAME=ctrl.maintscript.escape_shell_words(script_name),
                    ACTION=service_def.on_upgrade,
                )
            )
            lines.append("    fi")
        elif service_def.on_upgrade == "stop-then-start":
            lines.append(
                "    invoke-rc.d --skip-systemd-native {SCRIPT_NAME} start >/dev/null || exit 1".format(
                    SCRIPT_NAME=ctrl.maintscript.escape_shell_words(script_name),
                )
            )
            ctrl.maintscript.unconditionally_in_script(
                "preinst",
                textwrap.dedent(
                    """\
                if {EMPTY_DPKG_ROOT_CONDITION} && [ "$1" = "upgrade" ] && [ -x {SCRIPT_PATH} ]; then
                    invoke-rc.d --skip-systemd-native {SCRIPT_NAME} stop > /dev/null || true
                fi
                """.format(
                        EMPTY_DPKG_ROOT_CONDITION=EMPTY_DPKG_ROOT_CONDITION,
                        SCRIPT_PATH=ctrl.maintscript.escape_shell_words(
                            script_installed_path
                        ),
                        SCRIPT_NAME=ctrl.maintscript.escape_shell_words(script_name),
                    )
                ),
            )
        elif service_def.on_upgrade != "do-nothing":
            raise AssertionError(
                f"Missing support for on_upgrade rule: {service_def.on_upgrade}"
            )

        lines.append("fi")
        combined = "".join(x if x.endswith("\n") else f"{x}\n" for x in lines)
        ctrl.maintscript.on_configure(
            combined.format(
                EMPTY_DPKG_ROOT_CONDITION=EMPTY_DPKG_ROOT_CONDITION,
                DPKG_ROOT=DPKG_ROOT,
                UPDATE_RCD_PARAMS=update_rcd_params,
                SCRIPT_PATH=ctrl.maintscript.escape_shell_words(script_installed_path),
                SCRIPT_NAME=ctrl.maintscript.escape_shell_words(script_name),
            )
        )

        ctrl.maintscript.on_removed(
            textwrap.dedent(
                """\
            if [ -x {DPKG_ROOT}{SCRIPT_PATH} ]; then
                chmod -x {DPKG_ROOT}{SCRIPT_PATH} > /dev/null || true
            fi
            """.format(
                    DPKG_ROOT=DPKG_ROOT,
                    SCRIPT_PATH=ctrl.maintscript.escape_shell_words(
                        script_installed_path
                    ),
                )
            )
        )
        ctrl.maintscript.on_purge(
            textwrap.dedent(
                """\
            if {EMPTY_DPKG_ROOT_CONDITION} ; then
                update-rc.d {SCRIPT_NAME} remove >/dev/null
            fi
            """.format(
                    SCRIPT_NAME=ctrl.maintscript.escape_shell_words(script_name),
                    EMPTY_DPKG_ROOT_CONDITION=EMPTY_DPKG_ROOT_CONDITION,
                )
            )
        )


def detect_sysv_init_service_files(
    fs_root: VirtualPath,
    service_registry: ServiceRegistry[None],
    _context: PackageProcessingContext,
) -> None:
    etc_init = fs_root.lookup("/etc/init.d")
    if not etc_init:
        return
    for path in etc_init.iterdir:
        if path.is_dir or not path.is_executable:
            continue

        service_registry.register_service(
            path,
            path.name,
        )