Skip to content

Commit 9e1e212

Browse files
authored
facts/server: add RebootRequired fact for detecting system reboot needs
1 parent 765ea7c commit 9e1e212

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

pyinfra/facts/server.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,9 @@ def process(self, output) -> LinuxDistributionDict:
661661

662662
for filename, content in parts.items():
663663
with open(
664-
os.path.join(temp_etc_dir, os.path.basename(filename)), "w", encoding="utf-8"
664+
os.path.join(temp_etc_dir, os.path.basename(filename)),
665+
"w",
666+
encoding="utf-8",
665667
) as fp:
666668
fp.write(content)
667669

@@ -901,3 +903,52 @@ def process(self, output):
901903
)
902904

903905
return limits
906+
907+
908+
class RebootRequired(FactBase[bool]):
909+
"""
910+
Returns a boolean indicating whether the system requires a reboot.
911+
912+
On Linux systems:
913+
- Checks /var/run/reboot-required and /var/run/reboot-required.pkgs
914+
- On Alpine Linux, compares installed kernel with running kernel
915+
916+
On FreeBSD systems:
917+
- Compares running kernel version with installed kernel version
918+
"""
919+
920+
@override
921+
def command(self) -> str:
922+
return """
923+
# Get OS type
924+
OS_TYPE=$(uname -s)
925+
if [ "$OS_TYPE" = "Linux" ]; then
926+
# Check if it's Alpine Linux
927+
if [ -f /etc/alpine-release ]; then
928+
RUNNING_KERNEL=$(uname -r)
929+
INSTALLED_KERNEL=$(ls -1 /lib/modules | sort -V | tail -n1)
930+
if [ "$RUNNING_KERNEL" != "$INSTALLED_KERNEL" ]; then
931+
echo "reboot_required"
932+
exit 0
933+
fi
934+
else
935+
# Check standard Linux reboot required files
936+
if [ -f /var/run/reboot-required ] || [ -f /var/run/reboot-required.pkgs ]; then
937+
echo "reboot_required"
938+
exit 0
939+
fi
940+
fi
941+
elif [ "$OS_TYPE" = "FreeBSD" ]; then
942+
RUNNING_VERSION=$(freebsd-version -r)
943+
INSTALLED_VERSION=$(freebsd-version -k)
944+
if [ "$RUNNING_VERSION" != "$INSTALLED_VERSION" ]; then
945+
echo "reboot_required"
946+
exit 0
947+
fi
948+
fi
949+
echo "no_reboot_required"
950+
"""
951+
952+
@override
953+
def process(self, output) -> bool:
954+
return list(output)[0].strip() == "reboot_required"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"command": "\n# Get OS type\nOS_TYPE=$(uname -s)\nif [ \"$OS_TYPE\" = \"Linux\" ]; then\n # Check if it's Alpine Linux\n if [ -f /etc/alpine-release ]; then\n RUNNING_KERNEL=$(uname -r)\n INSTALLED_KERNEL=$(ls -1 /lib/modules | sort -V | tail -n1)\n if [ \"$RUNNING_KERNEL\" != \"$INSTALLED_KERNEL\" ]; then\n echo \"reboot_required\"\n exit 0\n fi\n else\n # Check standard Linux reboot required files\n if [ -f /var/run/reboot-required ] || [ -f /var/run/reboot-required.pkgs ]; then\n echo \"reboot_required\"\n exit 0\n fi\n fi\nelif [ \"$OS_TYPE\" = \"FreeBSD\" ]; then\n RUNNING_VERSION=$(freebsd-version -r)\n INSTALLED_VERSION=$(freebsd-version -k)\n if [ \"$RUNNING_VERSION\" != \"$INSTALLED_VERSION\" ]; then\n echo \"reboot_required\"\n exit 0\n fi\nfi\necho \"no_reboot_required\"\n",
3+
"output": ["reboot_required"],
4+
"fact": true
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"command": "\n# Get OS type\nOS_TYPE=$(uname -s)\nif [ \"$OS_TYPE\" = \"Linux\" ]; then\n # Check if it's Alpine Linux\n if [ -f /etc/alpine-release ]; then\n RUNNING_KERNEL=$(uname -r)\n INSTALLED_KERNEL=$(ls -1 /lib/modules | sort -V | tail -n1)\n if [ \"$RUNNING_KERNEL\" != \"$INSTALLED_KERNEL\" ]; then\n echo \"reboot_required\"\n exit 0\n fi\n else\n # Check standard Linux reboot required files\n if [ -f /var/run/reboot-required ] || [ -f /var/run/reboot-required.pkgs ]; then\n echo \"reboot_required\"\n exit 0\n fi\n fi\nelif [ \"$OS_TYPE\" = \"FreeBSD\" ]; then\n RUNNING_VERSION=$(freebsd-version -r)\n INSTALLED_VERSION=$(freebsd-version -k)\n if [ \"$RUNNING_VERSION\" != \"$INSTALLED_VERSION\" ]; then\n echo \"reboot_required\"\n exit 0\n fi\nfi\necho \"no_reboot_required\"\n", "output": ["reboot_required"],
3+
"fact": true
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"command": "\n# Get OS type\nOS_TYPE=$(uname -s)\nif [ \"$OS_TYPE\" = \"Linux\" ]; then\n # Check if it's Alpine Linux\n if [ -f /etc/alpine-release ]; then\n RUNNING_KERNEL=$(uname -r)\n INSTALLED_KERNEL=$(ls -1 /lib/modules | sort -V | tail -n1)\n if [ \"$RUNNING_KERNEL\" != \"$INSTALLED_KERNEL\" ]; then\n echo \"reboot_required\"\n exit 0\n fi\n else\n # Check standard Linux reboot required files\n if [ -f /var/run/reboot-required ] || [ -f /var/run/reboot-required.pkgs ]; then\n echo \"reboot_required\"\n exit 0\n fi\n fi\nelif [ \"$OS_TYPE\" = \"FreeBSD\" ]; then\n RUNNING_VERSION=$(freebsd-version -r)\n INSTALLED_VERSION=$(freebsd-version -k)\n if [ \"$RUNNING_VERSION\" != \"$INSTALLED_VERSION\" ]; then\n echo \"reboot_required\"\n exit 0\n fi\nfi\necho \"no_reboot_required\"\n",
3+
"fact": false,
4+
"output": ["no_reboot_required"]
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"command": "\n# Get OS type\nOS_TYPE=$(uname -s)\nif [ \"$OS_TYPE\" = \"Linux\" ]; then\n # Check if it's Alpine Linux\n if [ -f /etc/alpine-release ]; then\n RUNNING_KERNEL=$(uname -r)\n INSTALLED_KERNEL=$(ls -1 /lib/modules | sort -V | tail -n1)\n if [ \"$RUNNING_KERNEL\" != \"$INSTALLED_KERNEL\" ]; then\n echo \"reboot_required\"\n exit 0\n fi\n else\n # Check standard Linux reboot required files\n if [ -f /var/run/reboot-required ] || [ -f /var/run/reboot-required.pkgs ]; then\n echo \"reboot_required\"\n exit 0\n fi\n fi\nelif [ \"$OS_TYPE\" = \"FreeBSD\" ]; then\n RUNNING_VERSION=$(freebsd-version -r)\n INSTALLED_VERSION=$(freebsd-version -k)\n if [ \"$RUNNING_VERSION\" != \"$INSTALLED_VERSION\" ]; then\n echo \"reboot_required\"\n exit 0\n fi\nfi\necho \"no_reboot_required\"\n",
3+
"output": ["reboot_required"],
4+
"fact": true
5+
}

0 commit comments

Comments
 (0)