module BDCS.Export.Utils(runHacks,
runTmpfiles)
where
import Control.Conditional(whenM)
import Control.Exception(tryJust)
import Control.Monad(guard)
import Data.List(intercalate)
import Data.List.Split(splitOn)
import System.Directory(createDirectoryIfMissing, doesFileExist, listDirectory, removePathForcibly, renameFile)
import System.FilePath((</>))
import System.IO.Error(isDoesNotExistError)
import System.Process(callProcess)
import BDCS.Export.TmpFiles(setupFilesystem)
import Paths_bdcs(getDataFileName)
runHacks :: FilePath -> IO ()
runHacks exportPath = do
shadowRecs <- map (splitOn ":") <$> lines <$> readFile (exportPath </> "etc" </> "shadow")
let newRecs = map (\rec -> case rec of
"root":_:rest -> ["root", "$6$3VLMX3dyCGRa.JX3$RpveyimtrKjqcbZNTanUkjauuTRwqAVzRK8GZFkEinbjzklo7Yj9Z6FqXNlyajpgCdsLf4FEQQKH6tTza35xs/"] ++ rest
_ -> rec)
shadowRecs
writeFile (exportPath </> "etc" </> "shadow.new") (unlines $ map (intercalate ":") newRecs)
renameFile (exportPath </> "etc" </> "shadow.new") (exportPath </> "etc" </> "shadow")
writeFile (exportPath </> "etc" </> "machine-id") ""
let sysusersDir = exportPath </> "usr" </> "lib" </> "sysusers.d"
createDirectoryIfMissing True sysusersDir
getDataFileName "sysusers-default.conf" >>= readFile >>= writeFile (sysusersDir </> "weldr.conf")
callProcess "systemd-sysusers" ["--root", exportPath]
let modDir = exportPath </> "usr" </> "lib" </> "modules"
modVers <- tryJust (guard . isDoesNotExistError) (listDirectory modDir)
mapM_ (\ver -> callProcess "depmod" ["-b", exportPath, "-a", ver]) $ either (const []) id modVers
writeFile (exportPath </> "etc" </> "fstab") "LABEL=composer / ext2 defaults 0 0"
(map ((exportPath </> "run") </>) <$> listDirectory (exportPath </> "run")) >>= mapM_ removePathForcibly
let sslConf = exportPath </> "etc" </> "httpd" </> "conf.d" </> "ssl.conf"
whenM (doesFileExist sslConf)
(renameFile sslConf (sslConf ++ ".off"))
runTmpfiles :: FilePath -> IO ()
runTmpfiles exportPath = do
configPath <- getDataFileName "tmpfiles-default.conf"
setupFilesystem exportPath configPath