Commit 80723214 authored by Valentin Reis's avatar Valentin Reis
Browse files

Externalizes copying argo_nodeos_config from argotk.

parent 6d7d84a7
Pipeline #5327 passed with stage
in 35 seconds
......@@ -18,6 +18,7 @@ executable argonix
unix,
text,
neat-interpolation,
foldl,
system-filepath
default-language: Haskell2010
ghc-options:
......
......@@ -24,9 +24,39 @@ import Prelude hiding ( FilePath )
import Data.Text ( pack
, unpack
)
import Turtle.Shell
import Control.Foldl
import System.Posix.Process
checkFsAttributes :: FilePath -> Shell ()
checkFsAttributes workingDirectory = do
let dir = case toText workingDirectory of
Left di -> di
Right di -> di
let findmnt = inproc "findmnt" ["-T", dir, "-o", "OPTIONS"] empty
b <- liftIO $ Turtle.Shell.fold (grep (has "nosuid") findmnt) Control.Foldl.length
when (b > 0) $ die $ format
("The output directory, " % fp % ", must not mounted with \"nosuid\"")
workingDirectory
main :: IO ()
main = join $ execParser (info (opts <**> helper) idm)
where
opts :: Parser (IO ())
opts = hsubparser
( command
"build"
(info (nixbuild <$> targetParser <*> executorParser)
(progDesc "Run an argo-compatible nix-build.")
)
<> command
"stack-shell"
(info (pure nixshell) (progDesc "Enter an argo-compatable nix-shell")
)
<> help "Type of operation to run."
)
data StackArgs = StackArgs
{
argopkgs :: Text
......@@ -54,6 +84,10 @@ instance Default StackArgs where
overrides = []
}
targetParser :: Parser String
targetParser =
strArgument (metavar "TARGET" <> showDefault <> help "The build target.")
executorParser :: Parser StackArgs
executorParser = do
argopkgs <- strOption
......@@ -124,8 +158,70 @@ executorParser = do
Nothing -> Nothing
Just p -> Just (longform, p)
main :: IO ()
main = sh $ do
envSetup :: Shell FilePath
envSetup = do
sudo <- which "sudo" >>= \case
(Just sudo) -> printf ("Found sudo at " % fp % "\n") sudo >> return sudo
Nothing -> die "sudo not in $PATH."
export "SUDO" $ pack $ encodeString sudo
cachedir <- single $ inproc
"mktemp"
["-d", "--suffix=nixcache", "/tmp/deletable-nix-cache-XXXX"]
empty
export "XDG_CACHE_HOME" $ lineToText cachedir
return $ directory sudo
setupNodeOS :: StackArgs -> Shell FilePath
setupNodeOS sa = do
sudo <- which "sudo" >>= \case
(Just sudo) -> printf ("Found sudo at " % fp % "\n") sudo >> return sudo
Nothing -> die "sudo not in $PATH."
export "SUDO" $ pack $ encodeString sudo
cachedir <- single $ inproc
"mktemp"
["-d", "--suffix=nixcache", "/tmp/deletable-nix-cache-XXXX"]
empty
export "XDG_CACHE_HOME" $ lineToText cachedir
nodeos_config <- single $ inproc "nix-build" (fmap pack (nixArguments "containers" sa)) empty
printf s "Will copy built argo_nodeos_config to /tmp/. Checking filesystem attributes."
checkFsAttributes "/tmp"
printf s "Copying argo_nodeos_config."
shell (format ("cp "%s%" /tmp/argo_nodeos_config") (lineToText nodeos_config)) empty
printf s "Trying to sudo chown and chmod argo_nodeos_config"
shell "sudo chown root:root /tmp/argo_nodeos_config" empty
>>= \case
ExitSuccess -> printf s "Chowned argo_nodeos_config to root:root.\n"
ExitFailure n ->
die ("Failed to set argo_nodeos_config permissions " <> repr n)
shell "sudo chmod u+sw /tmp/argo_nodeos_config" empty
>>= \case
ExitSuccess -> printf s "Set the suid bit.\n"
ExitFailure n ->
die ("Setting suid bit failed with exit code " <> repr n)
return $ directory sudo
nixArguments :: String -> StackArgs -> [String]
nixArguments target StackArgs {..} = [unpack argopkgs, "-A", target] ++ concat
[ ["--arg", longform <> "-src", encodeString p] | (longform, p) <- overrides ]
nixbuild :: String -> StackArgs -> IO ()
nixbuild target sa@StackArgs {..} = sh $ do
view $ single $ inshell "echo $SUDO" empty
let arglist =
( (nixArguments target sa)
++ [ "--pure"
, "--allow-new-privileges"
, "--option"
, "extra-sandbox-paths"
, "/tmp/argo_nodeos_config"
, "-K"
]
)
liftIO $ print arglist
liftIO $ executeFile "nix-build" True arglist Nothing
nixshell :: IO ()
nixshell = sh $ do
StackArgs {..} <- liftIO $ execParser opts
-- building nixArguments (pure stuff) and shellArguments (impure stuff)
......@@ -169,7 +265,7 @@ main = sh $ do
drv <- single $ inproc
"nix-instantiate"
(["--quiet", argopkgs, "-A", "test"] ++ map pack nixArguments)
(["--quiet", argopkgs, "-A", "test"] ++ fmap pack nixArguments)
empty
printf (s % "\n") $ lineToText drv
......@@ -205,7 +301,7 @@ main = sh $ do
<> "\""
]
empty
mapM_
Prelude.mapM_
(\r -> proc "scp"
["-r", host <> ":" <> r, fromMaybe "." retreiveAs]
empty
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment