Last active
March 28, 2023 20:43
-
-
Save vernondcole/090048cc24c3e1d84fd2c283347bd6fa to your computer and use it in GitHub Desktop.
SaltStack state to restart a minion (using the minion you are restarting)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
# salt state file for restarting a minion under its own control | |
{% set delay = salt['config.get']('minion_restart_in_seconds', 5) %} | |
restart-the-minion: | |
file.managed: | |
- name: /tmp/run_command_later.py | |
- source: salt://run_command_later.py | |
- mode: 775 | |
cmd.run: | |
- require: | |
- file: restart-the-minion | |
- order: last | |
- name: "/tmp/run_command_later.py {{ delay }} systemctl restart salt-minion" | |
- bg: true {# do not wait for completion of this command #} | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# Execute a shell command after a delay time. | |
# arg[1] is delay time in seconds | |
# | |
import sys, subprocess, time | |
CHEAP_LOG_FILE = '/tmp/' + __file__ + '.log' | |
args = sys.argv | |
if not sys.stdout.isatty(): | |
sys.stdout = open(CHEAP_LOG_FILE, 'w') # cheap log output | |
sys.stderr = sys.stdout | |
cmd = args.pop(0) | |
try: | |
delay = float(args.pop(0)) | |
if not args: # only an empty list remaining? | |
raise ValueError | |
except (ValueError, IndexError): | |
print('Usage: {} seconds_to_delay command and args to run'.format(cmd)) | |
sys.exit(1) | |
print('sleeping {} seconds...'.format(delay)) | |
time.sleep(delay) | |
print(time.asctime()) | |
print('Running command: {}'.format(' '.join(args))) | |
print('- - - - - - - - - - - - - - -') | |
sys.stdout.flush() | |
try: | |
subprocess.run(' '.join(args), shell=True, check=True, stdout=sys.stdout, stderr=subprocess.STDOUT) | |
except subprocess.CalledProcessError as e: | |
print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to tell a remote service to restart itself.
It is a known limitation in SaltStack that in order to update some configuration parameters, you must restart the Salt minion. But -- although you can change those parameters from the Salt master, you cannot then get the minion to read your new configuration because you can't restart it using itself.
This Gist answers that problem by copying as small Python program to the minion which delays for a time, then runs a shell command. That program is then executed using a Salt "cmd.run" state with the "bg: true" option. The Python program then issues a system command to restart the minion.
This version is very simple, and is specific to Linux systems using systemd. Alteration for other systems (like pre 16.04 Ubuntu) is left as an exercise for the reader. Just change the arguments to "run_command_later.py".