__systemd_unit improvements (#606)
* __systemd_unit: Move systemctl detection to manifest * __systemd_unit: Restart the unit if inactive Until now, the --restart parameter caused the unit to be restarted only when the unit file has changed. This commit modifies --restart behavior so that the unit is also restarted when the unit is inactive. * __systemd_unit: Do not create unit file when source is empty
This commit is contained in:
		
					parent
					
						
							
								b4e98d4ab4
							
						
					
				
			
			
				commit
				
					
						3397bcbf9b
					
				
			
		
					 4 changed files with 43 additions and 19 deletions
				
			
		
							
								
								
									
										21
									
								
								cdist/conf/type/__systemd_unit/explorer/unit-status
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								cdist/conf/type/__systemd_unit/explorer/unit-status
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,21 @@
 | 
				
			||||||
 | 
					#!/bin/sh
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# 2017 Ľubomír Kučera <lubomir.kucera.jr at gmail.com>
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# This file is part of cdist.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# cdist is free software: you can redistribute it and/or modify
 | 
				
			||||||
 | 
					# it under the terms of the GNU General Public License as published by
 | 
				
			||||||
 | 
					# the Free Software Foundation, either version 3 of the License, or
 | 
				
			||||||
 | 
					# (at your option) any later version.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# cdist is distributed in the hope that it will be useful,
 | 
				
			||||||
 | 
					# but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
				
			||||||
 | 
					# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
				
			||||||
 | 
					# GNU General Public License for more details.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# You should have received a copy of the GNU General Public License
 | 
				
			||||||
 | 
					# along with cdist. If not, see <http://www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					systemctl is-active "${__object_id}" || true
 | 
				
			||||||
| 
						 | 
					@ -18,14 +18,6 @@
 | 
				
			||||||
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
 | 
					# along with cdist. If not, see <http://www.gnu.org/licenses/>.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
 | 
					
 | 
				
			||||||
systemctl_present=$(cat "${__object}/explorer/systemctl-present")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
if [ "${systemctl_present}" -ne 0 ]; then
 | 
					 | 
				
			||||||
    echo "systemctl does not seem to be present on this system" >&2
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    exit 1
 | 
					 | 
				
			||||||
fi
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
name="${__object_id}"
 | 
					name="${__object_id}"
 | 
				
			||||||
state=$(cat "${__object}/parameter/state")
 | 
					state=$(cat "${__object}/parameter/state")
 | 
				
			||||||
current_enablement_state=$(cat "${__object}/explorer/enablement-state")
 | 
					current_enablement_state=$(cat "${__object}/explorer/enablement-state")
 | 
				
			||||||
| 
						 | 
					@ -38,6 +30,15 @@ if [ "${state}" = "absent" ]; then
 | 
				
			||||||
    exit 0
 | 
					    exit 0
 | 
				
			||||||
fi
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					unit_status=$(cat "${__object}/explorer/unit-status")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if [ -f "${__object}/parameter/restart" ]; then
 | 
				
			||||||
 | 
					    if grep -q "^__file/etc/systemd/system/${name}" "${__messages_in}" || \
 | 
				
			||||||
 | 
					        [ "${unit_status}" != "active" ]; then
 | 
				
			||||||
 | 
					        echo "systemctl restart ${name} || true"
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
desired_enablement_state=$(cat "${__object}/parameter/enablement-state")
 | 
					desired_enablement_state=$(cat "${__object}/parameter/enablement-state")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if [ "${current_enablement_state}" = "${desired_enablement_state}" ]; then
 | 
					if [ "${current_enablement_state}" = "${desired_enablement_state}" ]; then
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -45,7 +45,7 @@ BOOLEAN PARAMETERS
 | 
				
			||||||
------------------
 | 
					------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
restart
 | 
					restart
 | 
				
			||||||
    Restart the unit on change.
 | 
					    Restart the unit on unit file change or when the unit is inactive.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
MESSAGES
 | 
					MESSAGES
 | 
				
			||||||
--------
 | 
					--------
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -18,22 +18,24 @@
 | 
				
			||||||
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
 | 
					# along with cdist. If not, see <http://www.gnu.org/licenses/>.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					systemctl_present=$(cat "${__object}/explorer/systemctl-present")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if [ "${systemctl_present}" -ne 0 ]; then
 | 
				
			||||||
 | 
					    echo "systemctl does not seem to be present on this system" >&2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    exit 1
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
name="${__object_id}"
 | 
					name="${__object_id}"
 | 
				
			||||||
source=$(cat "${__object}/parameter/source")
 | 
					source=$(cat "${__object}/parameter/source")
 | 
				
			||||||
state=$(cat "${__object}/parameter/state")
 | 
					state=$(cat "${__object}/parameter/state")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
onchange() {
 | 
					if [ -z "${source}" ] && [ "${state}" != "absent" ]; then
 | 
				
			||||||
    echo -n "systemctl daemon-reload"
 | 
					    exit 0
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
    if [ -f "${__object}/parameter/restart" ]; then
 | 
					 | 
				
			||||||
        echo -n " && (systemctl restart ${name} || true)"
 | 
					 | 
				
			||||||
    fi
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    echo
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
__config_file "/etc/systemd/system/${name}" \
 | 
					__config_file "/etc/systemd/system/${name}" \
 | 
				
			||||||
    --mode 644 \
 | 
					    --mode 644 \
 | 
				
			||||||
    --onchange "$(onchange)" \
 | 
					    --onchange "systemctl daemon-reload" \
 | 
				
			||||||
    --source "${source}" \
 | 
					    --source "${source}" \
 | 
				
			||||||
    --state "${state}"
 | 
					    --state "${state}"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue