summaryrefslogtreecommitdiff
path: root/src/Propellor/Property/Reboot.hs
diff options
context:
space:
mode:
authorSean Whitton2016-06-12 09:41:23 +0900
committerSean Whitton2016-06-12 09:41:23 +0900
commit5423d7a5e25907d126944921fff0fba2451cf11b (patch)
tree178c0d2d6d5d566e40c2c030a2dbf6088aca32e6 /src/Propellor/Property/Reboot.hs
parent397204fb1910fad31ead2ed1ba1e226f088856c5 (diff)
factor out reboot code from DigitalOcean.hs
Diffstat (limited to 'src/Propellor/Property/Reboot.hs')
-rw-r--r--src/Propellor/Property/Reboot.hs38
1 files changed, 37 insertions, 1 deletions
diff --git a/src/Propellor/Property/Reboot.hs b/src/Propellor/Property/Reboot.hs
index 5b854fa3..b1d76613 100644
--- a/src/Propellor/Property/Reboot.hs
+++ b/src/Propellor/Property/Reboot.hs
@@ -1,7 +1,13 @@
-module Propellor.Property.Reboot where
+module Propellor.Property.Reboot (
+ now,
+ atEnd,
+ toDistroKernel,
+) where
import Propellor.Base
+import Data.List
+
now :: Property Linux
now = tightenTargets $ cmdProperty "reboot" []
`assume` MadeChange
@@ -28,3 +34,33 @@ atEnd force resultok = property "scheduled reboot at end of propellor run" $ do
rebootparams
| force = [Param "--force"]
| otherwise = []
+
+-- | Reboots immediately if a kernel other than the distro-installed kernel is
+-- running.
+--
+-- This will only work if you have taken measures to ensure that the other
+-- kernel won't just get booted again. See 'Propellor.Property.DigitalOcean'
+-- for an example.
+toDistroKernel :: Property DebianLike
+toDistroKernel = check (not <$> runningInstalledKernel) now
+ `describe` "running installed kernel"
+
+runningInstalledKernel :: IO Bool
+runningInstalledKernel = do
+ kernelver <- takeWhile (/= '\n') <$> readProcess "uname" ["-r"]
+ when (null kernelver) $
+ error "failed to read uname -r"
+ kernelimages <- concat <$> mapM kernelsIn ["/", "/boot/"]
+ when (null kernelimages) $
+ error "failed to find any installed kernel images"
+ findVersion kernelver <$>
+ readProcess "file" ("-L" : kernelimages)
+
+-- | File output looks something like this, we want to unambiguously
+-- match the running kernel version:
+-- Linux kernel x86 boot executable bzImage, version 3.16-3-amd64 (debian-kernel@lists.debian.org) #1 SMP Debian 3.1, RO-rootFS, swap_dev 0x2, Normal VGA
+findVersion :: String -> String -> Bool
+findVersion ver s = (" version " ++ ver ++ " ") `isInfixOf` s
+
+kernelsIn :: FilePath -> IO [FilePath]
+kernelsIn d = filter ("vmlinu" `isInfixOf`) <$> dirContents d