2014-11-20 18:06:55 +00:00
|
|
|
module Propellor.Property.Chroot (
|
2014-11-20 21:18:26 +00:00
|
|
|
Chroot(..),
|
2014-11-20 18:06:55 +00:00
|
|
|
chroot,
|
|
|
|
provisioned,
|
2014-11-21 16:17:03 +00:00
|
|
|
-- * Internal use
|
|
|
|
propellChroot,
|
2014-11-20 19:15:28 +00:00
|
|
|
chain,
|
2014-11-20 18:06:55 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Propellor
|
|
|
|
import qualified Propellor.Property.Debootstrap as Debootstrap
|
2014-11-20 19:15:28 +00:00
|
|
|
import qualified Propellor.Shim as Shim
|
|
|
|
import Utility.SafeCommand
|
2014-11-20 18:06:55 +00:00
|
|
|
|
|
|
|
import qualified Data.Map as M
|
2014-11-20 19:15:28 +00:00
|
|
|
import Data.List.Utils
|
|
|
|
import System.Posix.Directory
|
2014-11-20 18:06:55 +00:00
|
|
|
|
|
|
|
data Chroot = Chroot FilePath System Host
|
|
|
|
|
|
|
|
instance Hostlike Chroot where
|
|
|
|
(Chroot l s h) & p = Chroot l s (h & p)
|
|
|
|
(Chroot l s h) &^ p = Chroot l s (h &^ p)
|
|
|
|
getHost (Chroot _ _ h) = h
|
|
|
|
|
|
|
|
-- | Defines a Chroot at the given location, containing the specified
|
|
|
|
-- System. Properties can be added to configure the Chroot.
|
|
|
|
--
|
2014-11-20 21:18:26 +00:00
|
|
|
-- > chroot "/srv/chroot/ghc-dev" (System (Debian Unstable) "amd64")
|
2014-11-20 18:06:55 +00:00
|
|
|
-- > & Apt.installed ["build-essential", "ghc", "haskell-platform"]
|
|
|
|
-- > & ...
|
|
|
|
chroot :: FilePath -> System -> Chroot
|
|
|
|
chroot location system = Chroot location system (Host location [] mempty)
|
|
|
|
|
|
|
|
-- | Ensures that the chroot exists and is provisioned according to its
|
|
|
|
-- properties.
|
|
|
|
--
|
|
|
|
-- Reverting this property removes the chroot. Note that it does not ensure
|
|
|
|
-- that any processes that might be running inside the chroot are stopped.
|
|
|
|
provisioned :: Chroot -> RevertableProperty
|
|
|
|
provisioned c@(Chroot loc system _) = RevertableProperty
|
|
|
|
(propigateChrootInfo c (go "exists" setup))
|
|
|
|
(go "removed" teardown)
|
|
|
|
where
|
2014-11-20 19:15:28 +00:00
|
|
|
go desc a = property (chrootDesc c desc) $ ensureProperties [a]
|
2014-11-20 18:06:55 +00:00
|
|
|
|
2014-11-21 16:17:03 +00:00
|
|
|
setup = propellChroot c (inChrootProcess c) `requires` toProp built
|
2014-11-20 18:06:55 +00:00
|
|
|
|
|
|
|
built = case system of
|
|
|
|
(System (Debian _) _) -> debootstrap
|
|
|
|
(System (Ubuntu _) _) -> debootstrap
|
|
|
|
|
2014-11-21 05:09:15 +00:00
|
|
|
debootstrap = Debootstrap.built loc system []
|
2014-11-20 18:06:55 +00:00
|
|
|
|
2014-11-21 05:09:15 +00:00
|
|
|
teardown = toProp (revert built)
|
2014-11-20 18:06:55 +00:00
|
|
|
|
|
|
|
propigateChrootInfo :: Chroot -> Property -> Property
|
2014-11-20 20:04:48 +00:00
|
|
|
propigateChrootInfo c p = propigateInfo c p (<> chrootInfo c)
|
|
|
|
|
|
|
|
chrootInfo :: Chroot -> Info
|
|
|
|
chrootInfo (Chroot loc _ h) =
|
|
|
|
mempty { _chrootinfo = mempty { _chroots = M.singleton loc h } }
|
2014-11-20 18:06:55 +00:00
|
|
|
|
2014-11-20 19:15:28 +00:00
|
|
|
-- | Propellor is run inside the chroot to provision it.
|
2014-11-21 16:17:03 +00:00
|
|
|
propellChroot :: Chroot -> ([String] -> CreateProcess) -> Property
|
|
|
|
propellChroot c@(Chroot loc _ _) mkproc = property (chrootDesc c "provisioned") $ do
|
2014-11-20 19:15:28 +00:00
|
|
|
let d = localdir </> shimdir c
|
|
|
|
let me = localdir </> "propellor"
|
|
|
|
shim <- liftIO $ ifM (doesDirectoryExist d)
|
|
|
|
( pure (Shim.file me d)
|
|
|
|
, Shim.setup me d
|
|
|
|
)
|
|
|
|
ifM (liftIO $ bindmount shim)
|
|
|
|
( chainprovision shim
|
|
|
|
, return FailedChange
|
|
|
|
)
|
|
|
|
where
|
|
|
|
bindmount shim = ifM (doesFileExist (loc ++ shim))
|
|
|
|
( return True
|
|
|
|
, do
|
|
|
|
let mntpnt = loc ++ localdir
|
|
|
|
createDirectoryIfMissing True mntpnt
|
|
|
|
boolSystem "mount"
|
|
|
|
[ Param "--bind"
|
|
|
|
, File localdir, File mntpnt
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
chainprovision shim = do
|
|
|
|
parenthost <- asks hostName
|
2014-11-21 05:05:51 +00:00
|
|
|
cmd <- liftIO $ toChain parenthost c
|
2014-11-21 16:17:03 +00:00
|
|
|
let p = mkproc
|
2014-11-20 19:15:28 +00:00
|
|
|
[ shim
|
|
|
|
, "--continue"
|
2014-11-21 05:05:51 +00:00
|
|
|
, show cmd
|
2014-11-20 19:15:28 +00:00
|
|
|
]
|
|
|
|
liftIO $ withHandle StdoutHandle createProcessSuccess p
|
|
|
|
processChainOutput
|
|
|
|
|
2014-11-21 05:05:51 +00:00
|
|
|
toChain :: HostName -> Chroot -> IO CmdLine
|
|
|
|
toChain parenthost (Chroot loc _ _) = do
|
|
|
|
onconsole <- isConsole <$> mkMessageHandle
|
|
|
|
return $ ChrootChain parenthost loc onconsole
|
2014-11-20 19:15:28 +00:00
|
|
|
|
2014-11-21 05:05:51 +00:00
|
|
|
chain :: [Host] -> HostName -> FilePath -> Bool -> IO ()
|
|
|
|
chain hostlist hn loc onconsole = case findHostNoAlias hostlist hn of
|
2014-11-20 19:15:28 +00:00
|
|
|
Nothing -> errorMessage ("cannot find host " ++ hn)
|
|
|
|
Just parenthost -> case M.lookup loc (_chroots $ _chrootinfo $ hostInfo parenthost) of
|
|
|
|
Nothing -> errorMessage ("cannot find chroot " ++ loc ++ " on host " ++ hn)
|
|
|
|
Just h -> go h
|
|
|
|
where
|
|
|
|
go h = do
|
|
|
|
changeWorkingDirectory localdir
|
2014-11-21 05:05:51 +00:00
|
|
|
when onconsole forceConsole
|
2014-11-20 19:15:28 +00:00
|
|
|
onlyProcess (provisioningLock loc) $ do
|
|
|
|
r <- runPropellor h $ ensureProperties $ hostProperties h
|
|
|
|
putStrLn $ "\n" ++ show r
|
|
|
|
|
|
|
|
inChrootProcess :: Chroot -> [String] -> CreateProcess
|
|
|
|
inChrootProcess (Chroot loc _ _) cmd = proc "chroot" (loc:cmd)
|
|
|
|
|
|
|
|
provisioningLock :: FilePath -> FilePath
|
|
|
|
provisioningLock containerloc = "chroot" </> mungeloc containerloc ++ ".lock"
|
|
|
|
|
|
|
|
shimdir :: Chroot -> FilePath
|
|
|
|
shimdir (Chroot loc _ _) = "chroot" </> mungeloc loc ++ ".shim"
|
|
|
|
|
|
|
|
mungeloc :: FilePath -> String
|
|
|
|
mungeloc = replace "/" "_"
|
|
|
|
|
|
|
|
chrootDesc :: Chroot -> String -> String
|
|
|
|
chrootDesc (Chroot loc _ _) desc = "chroot " ++ loc ++ " " ++ desc
|