File macros.systemd of Package systemd-rpm-macros.6220

#  -*- Mode: makefile; indent-tabs-mode: t -*- */
# RPM macros for packages installing systemd unit files
#
###
#
# When a package install systemd unit files, it should use the following macros:
#
# add %systemd_requires in the specfile
#
# %pre
# %service_add_pre demo.service demo1.service
#
# %post
# %service_add_post demo.service demo1.service
#
# %preun
# %service_del_preun demo.service
#
# %postun
# %service_del_postun demo.service
#
###
#
# When a package install systemd preset files, it should use the following macros:
#
# %pre -p /bin/bash
# %systemd_preset_pre
#
# %posttrans -p /bin/bash
# %systemd_preset_posttrans
#
###

# This is for systemctl
%systemd_requires									\
Requires(pre): systemd									\
Requires(post): systemd									\
Requires(preun): systemd								\
Requires(postun): systemd								\

%_unitdir /usr/lib/systemd/system
%_userunitdir /usr/lib/systemd/user
%_presetdir /usr/lib/systemd/system-preset
%_udevhwdbdir /usr/lib/udev/hwdb.d
%_udevrulesdir /usr/lib/udev/rules.d
%_journalcatalogdir /usr/lib/systemd/catalog
%_tmpfilesdir /usr/lib/tmpfiles.d
%_sysusersdir /usr/lib/sysusers.d
%_sysctldir /usr/lib/sysctl.d
%_ntpunitsdir /usr/lib/systemd/ntp-units.d
%_binfmtdir /usr/lib/binfmt.d

%_restart_on_update_force() (\
			test "$YAST_IS_RUNNING" = instsys && exit 0 \
			%{?*:/usr/bin/systemctl try-restart %{*}} \
		) || : %{nil}
%_restart_on_update_never() %{?*:# Restart of %{*} skipped} %{nil}
%_restart_on_update() (\
			test "$YAST_IS_RUNNING" = instsys && exit 0\
			test -f /etc/sysconfig/services -a \\\
			     -z "$DISABLE_RESTART_ON_UPDATE" && . /etc/sysconfig/services\
			test "$DISABLE_RESTART_ON_UPDATE" = yes -o \\\
			     "$DISABLE_RESTART_ON_UPDATE" = 1 && exit 0\
			%{?*:/usr/bin/systemctl try-restart %{*}}\
		) || : %{nil}

%_stop_on_removal_force() ( \
		test "$YAST_IS_RUNNING" = instsys && exit 0\
		%{?*:/usr/bin/systemctl stop %{*}}\
	) || : %{nil}
%_stop_on_removal_never() %{?*:# Stop of %{*} skipped} %{nil}
%_stop_on_removal() (\
		test "$YAST_IS_RUNNING" = instsys && exit 0\
		test -f /etc/sysconfig/services -a \\\
		     -z "$DISABLE_STOP_ON_REMOVAL" && . /etc/sysconfig/services\
		test "$DISABLE_STOP_ON_REMOVAL" = yes -o \\\
		     "$DISABLE_STOP_ON_REMOVAL" = 1 && exit 0\
		%{?*:/usr/bin/systemctl stop %{*}}\
	) || : %{nil}

%service_add_pre()									\
test -n "$FIRST_ARG" || FIRST_ARG="$1"							\
# disable migration if initial install under systemd					\
[ -d /var/lib/systemd/migrated ] || mkdir -p /var/lib/systemd/migrated || :		\
if [ "$FIRST_ARG" -eq 1 ]; then 							\
	for service in %{?*} ; do							\
		sysv_service="${service%%.*}"						\
		touch "/var/lib/systemd/migrated/$sysv_service" || :			\
	done										\
else											\
	if [ "$FIRST_ARG" -gt 1 ]; then 						\
		for service in %{?*} ; do						\
			# The tag file might have been left by a preceding		\
			# update (see bsc#1059627)					\
			rm -f "/run/rpm-%{name}-update-$service-new-in-upgrade"		\
											\
			if [ ! -e "/usr/lib/systemd/system/$service" ]; then		\
				touch "/run/rpm-%{name}-update-$service-new-in-upgrade"	\
			fi								\
		done									\
	fi										\
	for service in %{?*} ; do							\
		sysv_service="${service%%.*}"						\
		if [ ! -e "/var/lib/systemd/migrated/$sysv_service" ]; then		\
			services_to_migrate="$services_to_migrate $sysv_service"	\
		fi									\
	done										\
	if [ -n "$services_to_migrate" -a -x /usr/sbin/systemd-sysv-convert ]; then	\
		/usr/sbin/systemd-sysv-convert --save $services_to_migrate || :		\
	fi										\
fi											\
%{nil}

# On install, tell systemd to reload its unit files
%service_add_post()									\
test -n "$FIRST_ARG" || FIRST_ARG="$1"							\
[ -d /var/lib/systemd/migrated ] || mkdir -p /var/lib/systemd/migrated || :		\
for service in %{?*} ; do								\
	sysv_service="${service%%.*}"							\
	if [ ! -e "/var/lib/systemd/migrated/$sysv_service" ]; then			\
		services_to_migrate="$services_to_migrate $sysv_service"		\
		touch "/var/lib/systemd/migrated/$sysv_service" || :			\
	fi										\
done											\
if [ "$YAST_IS_RUNNING" != "instsys" -a -x /usr/bin/systemctl ]; then							\
	/usr/bin/systemctl daemon-reload || :						\
fi											\
if [ -n "$services_to_migrate" ]; then							\
	if [ -x /usr/sbin/systemd-sysv-convert ]; then					\
		/usr/sbin/systemd-sysv-convert --apply $services_to_migrate || :	\
	fi										\
elif [ "$FIRST_ARG" -eq 1 ]; then							\
	if [ -x /usr/bin/systemctl ]; then						\
		/usr/bin/systemctl preset %{?*} || :					\
	fi										\
elif [ "$FIRST_ARG" -gt 1 ]; then							\
	for service in %{?*} ; do							\
		if [ ! -e "/run/rpm-%{name}-update-$service-new-in-upgrade" ]; then	\
			continue							\
		fi									\
		rm -f "/run/rpm-%{name}-update-$service-new-in-upgrade"			\
		if [ ! -x /usr/bin/systemctl ]; then					\
			continue							\
		fi									\
		/usr/bin/systemctl preset "$service" || :				\
	done										\
fi											\
%{nil}

# On uninstall, disable and stop services
#
# Options used if not in an installation systems
#  -f that is fore service stop in removal
#  -n that do not touch active service
# the default is to check for DISABLE_STOP_ON_REMOVAL environment
# variable if not found use the value read from /etc/sysconfig/services
#
%service_del_preun(fn)									\
test -n "$FIRST_ARG" || FIRST_ARG="$1"							\
if [ "$FIRST_ARG" -eq 0 -a -x /usr/bin/systemctl ]; then				\
	# Package removal, not upgrade							\
	/usr/bin/systemctl --no-reload disable %{?*} || :				\
	%{expand:%%_stop_on_removal%{-f:_force}%{!-f:%{-n:_never}} %{?*}}		\
fi											\
%{nil}

# On uninstall, tell systemd to reload its unit files
#
# Options used if not in an installation systems
#  -f that is fore service restart in removal
#  -n that do not touch active service
# the default is to check for DISABLE_RESTART_ON_UPDATE environment
# variable if not found use the value read from /etc/sysconfig/services
#

%service_del_postun(fn)									\
test -n "$FIRST_ARG" || FIRST_ARG="$1"							\
if [ "$FIRST_ARG" -ge 1 ]; then								\
	# Package upgrade, not uninstall						\
	if [ -x /usr/bin/systemctl ]; then						\
		/usr/bin/systemctl daemon-reload || :					\
		%{expand:%%_restart_on_update%{-f:_force}%{!-f:%{-n:_never}} %{?*}}	\
	fi										\
else # package uninstall								\
	for service in %{?*} ; do							\
		sysv_service="${service%.*}"						\
		rm -f "/var/lib/systemd/migrated/$sysv_service" || :			\
	done										\
	if [ -x /usr/bin/systemctl ]; then						\
		/usr/bin/systemctl daemon-reload || :					\
	fi										\
fi											\
%{nil}

#
# Options used if not in an installation systems
#  -f that is fore service stop in removal
#  -n that do not touch active service
# the default is to check for DISABLE_STOP_ON_REMOVAL environment
# variable if not found use the value read from /etc/sysconfig/services
#
%systemd_post(fn)									\
if [ "$1" -eq 0 -a -x /usr/bin/systemctl ]; then					\
	# Package removal, not upgrade							\
	/usr/bin/systemctl --no-reload disable %{?*} || :				\
	%{expand:%%_stop_on_removal%{-f:_force}%{!-f:%{-n:_never}} %{?*}}		\
fi											\
%{nil}

%systemd_user_post() %systemd_post --user --global %{?*}

#
# Options used if not in an installation systems
#  -f that is fore service stop in removal
#  -n that do not touch active service
# the default is to check for DISABLE_STOP_ON_REMOVAL environment
# variable if not found use the value read from /etc/sysconfig/services
#
%systemd_preun(fn)									\
if [ "$1" -eq 0 -a -x /usr/bin/systemctl ]; then					\
	# Package removal, not upgrade							\
	/usr/bin/systemctl --no-reload disable %{?*} || :				\
	%{expand:%%_stop_on_removal%{-f:_force}%{!-f:%{-n:_never}} %{?*}}		\
fi											\
%{nil}

%systemd_user_preun()									\
if [ "$1" -eq 0 -a -x /usr/bin/systemctl ]; then					\
        # Package removal, not upgrade							\
        /usr/bin/systemctl --no-reload --user --global disable %{?*} || :		\
fi											\
%{nil}

%systemd_postun()									\
if [ -x /usr/bin/systemctl ]; then							\
	/usr/bin/systemctl daemon-reload || :						\
fi											\
%{nil}

%systemd_user_postun() %{nil}
%systemd_user_postun_with_restart() %{nil}

%systemd_postun_with_restart()								\
if [ -x /usr/bin/systemctl ]; then							\
	/usr/bin/systemctl daemon-reload || :						\
fi											\
if [ "$1" -ge 1 -a -x /usr/bin/systemctl ]; then					\
	# Package upgrade, not uninstall						\
	/usr/bin/systemctl try-restart %{?*} || :					\
fi											\
%{nil}

%udev_hwdb_update() \
[ -x /usr/bin/udevadm ] && /usr/bin/udevadm hwdb --update || : \
%{nil}

%udev_rules_update() \
[ -x /usr/bin/udevadm ] && /usr/bin/udevadm control --reload || : \
%{nil}

%journal_catalog_update() \
[ -x /usr/bin/journalctl ] && /usr/bin/journalctl --update-catalog || : \
%{nil}

%tmpfiles_create() \
[ -x /usr/bin/systemd-tmpfiles ] && \
	/usr/bin/systemd-tmpfiles --create %{?*} || : \
%{nil}

%sysusers_create() \
[ -x /usr/bin/systemd-sysusers ] && /usr/bin/systemd-sysusers %{?*} || : \
%{nil}

%sysusers_create_inline() \
if [ -x /usr/bin/systemd-sysusers ]; then \
	echo %{?*} | systemd-sysusers - || : \
fi \
%{nil}

%sysctl_apply() \
[ -x /usr/lib/systemd/systemd-sysctl ] && \
	/usr/lib/systemd/systemd-sysctl %{?*} || : \
%{nil}

%binfmt_apply() \
[ -x /usr/lib/systemd/systemd-binfmt ] && \
	/usr/lib/systemd/systemd-binfmt %{?*} || : \
%{nil}

%systemd_preset_pre() \
if ! test -f /usr/lib/systemd/system-preset/99-default-disable.preset ; then \
	mkdir -p /usr/lib/systemd/system-preset; \
	touch /usr/lib/systemd/system-preset/systemd_preset-skip.rpm-tmp; \
else \
	cd /usr/lib/systemd/system-preset; \
	declare -a PRESET_FILES; \
	declare -A PRESETS; \
	PRESET_FILES=(*.preset); \
	if ! test -f systemd_preset-old.rpm-tmp ; then \
		for ((i=${#PRESET_FILES[@]}-1 ; i>= 0 ; i-- )) ; do \
			FILE=${PRESET_FILES[i]}; \
			exec 3<"$FILE"; \
			while read -u3 ENABLE SERVICE PAD ; do \
				if test -z "$SERVICE" ; then \
					continue; \
				fi; \
				case "$ENABLE" in \
				enable|disable) \
					PRESETS[$SERVICE]=$ENABLE;; \
				esac; \
			done; \
			exec 3<&-; \
		done; \
		exec 3>systemd_preset-old.rpm-tmp; \
		for PRESET in "${!PRESETS[@]}" ; do \
			echo >&3 "${PRESETS[$PRESET]} $PRESET"; \
		done; \
		exec 3>&-; \
	fi; \
fi; \
%{nil}

%systemd_preset_posttrans() \
if test -f /usr/lib/systemd/system-preset/systemd_preset-skip.rpm-tmp ; then \
	rm -f /usr/lib/systemd/system-preset/systemd_preset-{old,skip}.rpm-tmp; \
fi; \
cd /usr/lib/systemd/system-preset; \
if test -f systemd_preset-old.rpm-tmp ; then \
	declare -a PRESET_FILES; \
	declare -A PRESETS_OLD; \
	declare -A PRESETS_OLD_WILDCARD; \
	declare -A PRESETS; \
	declare -A PRESETS_WILDCARD; \
	exec 3<systemd_preset-old.rpm-tmp; \
	while read -u3 ENABLE SERVICE PAD ; do \
		if test -z "$SERVICE" ; then \
			continue; \
		fi; \
		case "$ENABLE" in \
		enable|disable) \
			case $SERVICE in \
			*"*"*|*"?"*) PRESETS_OLD_WILDCARD[$SERVICE]=$ENABLE;; \
			*) PRESETS_OLD[$SERVICE]=$ENABLE;; \
			esac;; \
		esac; \
	done; \
	exec 3<&-; \
	PRESET_FILES=(*.preset); \
	for ((i=${#PRESET_FILES[@]}-1 ; i>= 0 ; i-- )) ; do \
		FILE=${PRESET_FILES[i]}; \
		exec 3<"$FILE"; \
		while read -u3 ENABLE SERVICE PAD ; do \
			if test -z "$SERVICE" ; then \
				continue; \
			fi; \
			case "$ENABLE" in \
			enable|disable) \
				case $SERVICE in \
				*"*"*|*"?"*) PRESETS_WILDCARD[$SERVICE]=$ENABLE;; \
				*) PRESETS[$SERVICE]=$ENABLE;; \
				esac;; \
			esac; \
		done; \
		exec 3<&-; \
	done; \
	if test -x /usr/bin/systemctl ; then \
		/usr/bin/systemctl --type=service,socket list-unit-files; \
	fi >systemd_preset-states.rpm-tmp; \
	exec 3<systemd_preset-states.rpm-tmp; \
	read -u3 PAD; \
	while read -u3 SERVICE ENABLE PAD ; do \
		if test -z "$SERVICE" ; then \
			break; \
		fi; \
		ENABLE_OLD=enable; \
		for PRESET in "${!PRESETS_OLD_WILDCARD[@]}" ; do \
			case "$SERVICE" in \
			$PRESET) ENABLE_OLD=${PRESETS_OLD_WILDCARD[$PRESET]};; \
			esac; \
		done; \
		if test -n "${PRESETS_OLD[$SERVICE]}" ; then \
			ENABLE_OLD="${PRESETS_OLD[$SERVICE]}"; \
		fi; \
		ENABLE_NEW=enable; \
		for PRESET in "${!PRESETS_WILDCARD[@]}" ; do \
			case "$SERVICE" in \
			$PRESET) ENABLE_NEW=${PRESETS_WILDCARD[$PRESET]};; \
			esac; \
		done; \
		if test -n "${PRESETS[$SERVICE]}" ; then \
			ENABLE_NEW="${PRESETS[$SERVICE]}"; \
		fi; \
		if test "$ENABLE_OLD" != "$ENABLE_NEW" ; then \
			echo "Resetting $SERVICE to the new default: $ENABLE_NEW"; \
			/usr/bin/systemctl preset "$SERVICE" || :; \
		fi; \
	done; \
	exec 3<&-; \
	rm -f systemd_preset-old.rpm-tmp systemd_preset-states.rpm-tmp; \
fi; \
%{nil}
openSUSE Build Service is sponsored by