# Make a changed sysctl take effect in the run that changed it.
#
# `etc/sysctl.d/60-detc.conf` is read at boot, so without this a value set
# today is a value that arrives tomorrow.  `systemd-sysctl.service` is what
# reads the directory, and restarting it re-applies everything in it.
#
# This is the worked example of the whole mechanism, and the one a fleet copies
# for its own services.  `detc.files` holds the digest of what the file is
# *about to* hold, worked out before any resource is inspected; the `unit`
# provider writes down the digest it last restarted for and reports it back, so
# this is in sync for as long as the file has not moved and asks to be applied
# on the one run that moves it.  `--dry-run` says so before anything happens.
#
# Nothing else is declared.  Whether `systemd-sysctl.service` is enabled is not
# detc's opinion -- it is `sysinit.target` that pulls it in, and a node where
# somebody turned it off meant it.  Only the restart is declared, and only
# because the file this ships is the reason there is anything to restart.
#
# The whole declaration is inside one condition, and renders to nothing unless
# both halves hold.  A declaration that renders nothing asks for nothing, and
# the `unit` provider is not even asked about it.
#
# There has to be a digest.  A node that set no sysctl has no file -- the
# template writes none -- so nothing is published and there is nothing to
# react to.  Neither is there on `detc apply --type resource`, which renders no
# template and so knows nothing about the file.  Declaring `config: ""` there
# instead would be a claim the provider records, and the next full run would
# restart the service for a change that never happened.
#
# And it has to be systemd, which `init.manager` reports.  A system without it
# has no `systemd-sysctl` to restart, and a `unit` provider with no `systemctl`
# to ask refuses rather than inventing a state -- so the core set, installed on
# Alpine, would otherwise fail its first run on a sysctl it cannot apply.
# `init` is read one level at a time because a probe fills it, and a tree with
# no init system reports nothing.
#
# `_order: 70` is the order this provider already declares, written out because
# this file is where the question gets asked: the template it watches is
# rendered at 50, so the restart has to come after it, and a reader should not
# have to go and read the provider's schema to see that this is not an accident.
#
# `_requires` names the same file as `config`, and the two say different things
# about it.  `config` says *restart me when it moves*; `_requires` says *do not
# restart me if it never landed*.  The digest is worked out before a byte is
# written, so a template that renders and then fails to write still publishes
# one -- and without this the service would be restarted for a file that is not
# there.  It is spelled `template/` plus the path as `detc.files` keys it, which
# is the same path a `path` resource would be named by.
#
# Nothing about `apply --type resource` changes: a requirement naming something
# the run was not asked to look at is left alone, exactly as a digest that was
# never published is.
{% set digest = detc.files['etc/sysctl.d/60-detc.conf'] | default('') -%}
{% set init = (init | default({})).manager | default('') -%}
{% if digest and init == 'systemd' -%}
_order: 70
_requires:
  - template/etc/sysctl.d/60-detc.conf
config: "{{ digest }}"
{% endif -%}
