diff options
author | Andrew Cady <d@jerkface.net> | 2018-07-16 16:18:33 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2018-07-16 16:33:35 -0400 |
commit | 043c115135e3d72424a649c74fa4d4219b3bda1d (patch) | |
tree | bd9d2eab6eba71f97f2ae547e356e20857bdae1b /fsmgr.hs | |
parent | e91baa702cb70b220116cbdd9bc897cfb3bb3e8e (diff) |
fail early on a few conditions
Diffstat (limited to 'fsmgr.hs')
-rw-r--r-- | fsmgr.hs | 18 |
1 files changed, 12 insertions, 6 deletions
@@ -20,11 +20,13 @@ import Development.Shake.Command () | |||
20 | import Development.Shake.FilePath | 20 | import Development.Shake.FilePath |
21 | 21 | ||
22 | import qualified Options.Applicative as Opt | 22 | import qualified Options.Applicative as Opt |
23 | ;import Options.Applicative hiding (action) | 23 | ;import Options.Applicative hiding (action, command) |
24 | 24 | ||
25 | import ConfigFile | 25 | import ConfigFile |
26 | import System.Directory (createDirectoryIfMissing) | 26 | import System.Directory (createDirectoryIfMissing) |
27 | import System.Posix.Process (getProcessID) | 27 | import System.Posix.Process (getProcessID) |
28 | import System.Posix.Types (CUid (..)) | ||
29 | import System.Posix.User (getEffectiveUserID) | ||
28 | 30 | ||
29 | noParent :: BaseImageSpecification -> Bool | 31 | noParent :: BaseImageSpecification -> Bool |
30 | noParent (EmptyImageOfBytes _) = True | 32 | noParent (EmptyImageOfBytes _) = True |
@@ -121,9 +123,6 @@ buildOpts = Build . BuildOpts <$> argument str idm | |||
121 | chrootOpts :: Parser Command | 123 | chrootOpts :: Parser Command |
122 | chrootOpts = fmap Chroot $ ChrootOpts <$> argument str idm <*> many (argument str idm) | 124 | chrootOpts = fmap Chroot $ ChrootOpts <$> argument str idm <*> many (argument str idm) |
123 | 125 | ||
124 | -- TODO: Fail early on: | ||
125 | -- 1. not running as root | ||
126 | -- 2. no "selfstrap" in PATH | ||
127 | main :: IO () | 126 | main :: IO () |
128 | main = customExecParser (prefs showHelpOnEmpty) (info opts desc) >>= run | 127 | main = customExecParser (prefs showHelpOnEmpty) (info opts desc) >>= run |
129 | where | 128 | where |
@@ -142,8 +141,8 @@ main = customExecParser (prefs showHelpOnEmpty) (info opts desc) >>= run | |||
142 | info' o d = info (helper <*> o) (progDesc $ unwords d) | 141 | info' o d = info (helper <*> o) (progDesc $ unwords d) |
143 | 142 | ||
144 | run :: Options -> IO () | 143 | run :: Options -> IO () |
145 | run (Options (Build (BuildOpts target))) = shakeBuildOneImage target | 144 | run (Options (Build (BuildOpts target))) = earlyFail >> shakeBuildOneImage target |
146 | run (Options (Chroot (ChrootOpts target args))) = chrootImage target args | 145 | run (Options (Chroot (ChrootOpts target args))) = earlyFail >> chrootImage target args |
147 | 146 | ||
148 | useCGroups :: Bool | 147 | useCGroups :: Bool |
149 | useCGroups = True -- TODO: make command-line option | 148 | useCGroups = True -- TODO: make command-line option |
@@ -215,6 +214,13 @@ cgroupChroot groupName mnt args = do | |||
215 | "sh -exc" ["mount -t proc proc /proc; mount -t devpts devpts /dev/pts; exec \"$@\""] | 214 | "sh -exc" ["mount -t proc proc /proc; mount -t devpts devpts /dev/pts; exec \"$@\""] |
216 | "sh" args | 215 | "sh" args |
217 | 216 | ||
217 | earlyFail :: IO () | ||
218 | earlyFail = do | ||
219 | CUid euid <- liftIO getEffectiveUserID | ||
220 | when (euid /= 0) $ fail "you are not root" | ||
221 | Stdout () <- cmd (Traced []) "which selfstrap" | ||
222 | return () | ||
223 | |||
218 | shakeRules :: Rules () | 224 | shakeRules :: Rules () |
219 | shakeRules = do | 225 | shakeRules = do |
220 | "_build/*.yaml.canon" %> \out -> do | 226 | "_build/*.yaml.canon" %> \out -> do |