{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Hercules.Effect.Container where
import Control.Lens
import Data.Aeson (Value (String), eitherDecode, encode, object, toJSON)
import Data.Aeson.Lens
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import qualified Data.Map as M
import qualified Data.UUID.V4 as UUID
import GHC.IO.Exception (IOErrorType (HardwareFault))
import Protolude
import System.Directory (createDirectory)
import System.FilePath ((</>))
import System.IO (hClose)
import System.IO.Error (ioeGetErrorType)
import System.Posix.IO (closeFd, fdToHandle)
import System.Posix.Terminal (openPseudoTerminal)
import System.Process (CreateProcess (..), StdStream (UseHandle), proc, waitForProcess, withCreateProcess)
import System.Process.ByteString (readCreateProcessWithExitCode)
data BindMount = BindMount
{ BindMount -> Text
pathInContainer :: Text,
BindMount -> Text
pathInHost :: Text,
BindMount -> Bool
readOnly :: Bool
}
defaultBindMount :: Text -> BindMount
defaultBindMount :: Text -> BindMount
defaultBindMount Text
path = BindMount :: Text -> Text -> Bool -> BindMount
BindMount {pathInContainer :: Text
pathInContainer = Text
path, pathInHost :: Text
pathInHost = Text
path, readOnly :: Bool
readOnly = Bool
True}
data Config = Config
{ :: [BindMount],
Config -> Text
executable :: Text,
Config -> [Text]
arguments :: [Text],
Config -> Map Text Text
environment :: Map Text Text,
Config -> Text
workingDirectory :: Text,
Config -> Text
hostname :: Text,
Config -> Bool
rootReadOnly :: Bool
}
effectToRuncSpec :: Config -> Value -> Value
effectToRuncSpec :: Config -> Value -> Value
effectToRuncSpec Config
config Value
spec =
let defaultMounts :: [BindMount]
defaultMounts = [Text -> BindMount
defaultBindMount Text
"/nix/store"]
mounts :: Vector Value
mounts =
(BindMount -> Vector Value) -> [BindMount] -> Vector Value
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap
( \BindMount
bindMount ->
Value -> Vector Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Value -> Vector Value) -> Value -> Vector Value
forall a b. (a -> b) -> a -> b
$
[Pair] -> Value
object
[ (Key
"destination", Text -> Value
String (Text -> Value) -> Text -> Value
forall a b. (a -> b) -> a -> b
$ BindMount -> Text
pathInContainer BindMount
bindMount),
(Key
"source", Text -> Value
String (Text -> Value) -> Text -> Value
forall a b. (a -> b) -> a -> b
$ BindMount -> Text
pathInHost BindMount
bindMount),
(Key
"type", Value
"bind"),
( Key
"options",
[Text] -> Value
forall a. ToJSON a => a -> Value
toJSON ([Text] -> Value) -> [Text] -> Value
forall a b. (a -> b) -> a -> b
$
[Text
"bind" :: Text]
[Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [Text
"ro" | BindMount -> Bool
readOnly BindMount
bindMount]
)
]
)
([BindMount]
defaultMounts [BindMount] -> [BindMount] -> [BindMount]
forall a. Semigroup a => a -> a -> a
<> Config -> [BindMount]
extraBindMounts Config
config)
in Value
spec
Value -> (Value -> Value) -> Value
forall a b. a -> (a -> b) -> b
& Text -> Traversal' Value Value
forall t. AsValue t => Text -> Traversal' t Value
key Text
"process" ((Value -> Identity Value) -> Value -> Identity Value)
-> ((Value -> Identity Value) -> Value -> Identity Value)
-> (Value -> Identity Value)
-> Value
-> Identity Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Traversal' Value Value
forall t. AsValue t => Text -> Traversal' t Value
key Text
"args" ((Value -> Identity Value) -> Value -> Identity Value)
-> Value -> Value -> Value
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [Text] -> Value
forall a. ToJSON a => a -> Value
toJSON ([Config -> Text
executable Config
config] [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> Config -> [Text]
arguments Config
config)
Value -> (Value -> Value) -> Value
forall a b. a -> (a -> b) -> b
& Text -> Traversal' Value Value
forall t. AsValue t => Text -> Traversal' t Value
key Text
"mounts" ((Value -> Identity Value) -> Value -> Identity Value)
-> ((Vector Value -> Identity (Vector Value))
-> Value -> Identity Value)
-> (Vector Value -> Identity (Vector Value))
-> Value
-> Identity Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Vector Value -> Identity (Vector Value))
-> Value -> Identity Value
forall t. AsValue t => Prism' t (Vector Value)
_Array ((Vector Value -> Identity (Vector Value))
-> Value -> Identity Value)
-> (Vector Value -> Vector Value) -> Value -> Value
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ (Vector Value -> Vector Value -> Vector Value
forall a. Semigroup a => a -> a -> a
<> Vector Value
mounts)
Value -> (Value -> Value) -> Value
forall a b. a -> (a -> b) -> b
& Text -> Traversal' Value Value
forall t. AsValue t => Text -> Traversal' t Value
key Text
"process" ((Value -> Identity Value) -> Value -> Identity Value)
-> ((Value -> Identity Value) -> Value -> Identity Value)
-> (Value -> Identity Value)
-> Value
-> Identity Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Traversal' Value Value
forall t. AsValue t => Text -> Traversal' t Value
key Text
"terminal" ((Value -> Identity Value) -> Value -> Identity Value)
-> Value -> Value -> Value
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Bool -> Value
forall a. ToJSON a => a -> Value
toJSON Bool
True
Value -> (Value -> Value) -> Value
forall a b. a -> (a -> b) -> b
& Text -> Traversal' Value Value
forall t. AsValue t => Text -> Traversal' t Value
key Text
"process" ((Value -> Identity Value) -> Value -> Identity Value)
-> ((Value -> Identity Value) -> Value -> Identity Value)
-> (Value -> Identity Value)
-> Value
-> Identity Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Traversal' Value Value
forall t. AsValue t => Text -> Traversal' t Value
key Text
"env" ((Value -> Identity Value) -> Value -> Identity Value)
-> Value -> Value -> Value
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [Text] -> Value
forall a. ToJSON a => a -> Value
toJSON (Config
config Config -> (Config -> Map Text Text) -> Map Text Text
forall a b. a -> (a -> b) -> b
& Config -> Map Text Text
environment Map Text Text
-> (Map Text Text -> [(Text, Text)]) -> [(Text, Text)]
forall a b. a -> (a -> b) -> b
& Map Text Text -> [(Text, Text)]
forall k a. Map k a -> [(k, a)]
M.toList [(Text, Text)] -> ((Text, Text) -> Text) -> [Text]
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \(Text
k, Text
v) -> Text
k Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"=" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
v)
Value -> (Value -> Value) -> Value
forall a b. a -> (a -> b) -> b
& Text -> Traversal' Value Value
forall t. AsValue t => Text -> Traversal' t Value
key Text
"process" ((Value -> Identity Value) -> Value -> Identity Value)
-> ((Value -> Identity Value) -> Value -> Identity Value)
-> (Value -> Identity Value)
-> Value
-> Identity Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Traversal' Value Value
forall t. AsValue t => Text -> Traversal' t Value
key Text
"cwd" ((Value -> Identity Value) -> Value -> Identity Value)
-> Value -> Value -> Value
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text -> Value
forall a. ToJSON a => a -> Value
toJSON (Config
config Config -> (Config -> Text) -> Text
forall a b. a -> (a -> b) -> b
& Config -> Text
workingDirectory)
Value -> (Value -> Value) -> Value
forall a b. a -> (a -> b) -> b
& Text -> Traversal' Value Value
forall t. AsValue t => Text -> Traversal' t Value
key Text
"hostname" ((Value -> Identity Value) -> Value -> Identity Value)
-> Value -> Value -> Value
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text -> Value
forall a. ToJSON a => a -> Value
toJSON (Config
config Config -> (Config -> Text) -> Text
forall a b. a -> (a -> b) -> b
& Config -> Text
hostname)
Value -> (Value -> Value) -> Value
forall a b. a -> (a -> b) -> b
& Text -> Traversal' Value Value
forall t. AsValue t => Text -> Traversal' t Value
key Text
"root" ((Value -> Identity Value) -> Value -> Identity Value)
-> ((Value -> Identity Value) -> Value -> Identity Value)
-> (Value -> Identity Value)
-> Value
-> Identity Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Traversal' Value Value
forall t. AsValue t => Text -> Traversal' t Value
key Text
"readonly" ((Value -> Identity Value) -> Value -> Identity Value)
-> Value -> Value -> Value
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Bool -> Value
forall a. ToJSON a => a -> Value
toJSON (Config
config Config -> (Config -> Bool) -> Bool
forall a b. a -> (a -> b) -> b
& Config -> Bool
rootReadOnly)
run :: FilePath -> Config -> IO ExitCode
run :: String -> Config -> IO ExitCode
run String
dir Config
config = do
let runcExe :: String
runcExe = String
"runc"
createConfigJsonSpec :: CreateProcess
createConfigJsonSpec =
(String -> [String] -> CreateProcess
System.Process.proc String
runcExe [String
"spec", String
"--rootless"])
{ cwd :: Maybe String
cwd = String -> Maybe String
forall a. a -> Maybe a
Just String
dir
}
configJsonPath :: String
configJsonPath = String
dir String -> String -> String
</> String
"config.json"
runcRootPath :: String
runcRootPath = String
dir String -> String -> String
</> String
"runc-root"
rootfsPath :: String
rootfsPath = String
dir String -> String -> String
</> String
"rootfs"
(ExitCode
exit, ByteString
_out, ByteString
err) <- CreateProcess
-> ByteString -> IO (ExitCode, ByteString, ByteString)
readCreateProcessWithExitCode CreateProcess
createConfigJsonSpec ByteString
""
case ExitCode
exit of
ExitCode
ExitSuccess -> IO ()
forall (f :: * -> *). Applicative f => f ()
pass
ExitFailure Int
e -> do
Text -> IO ()
forall (m :: * -> *). MonadIO m => Text -> m ()
putErrText (OnDecodeError -> ByteString -> Text
decodeUtf8With OnDecodeError
lenientDecode ByteString
err)
Text -> IO ()
forall a. HasCallStack => Text -> a
panic (Text -> IO ()) -> Text -> IO ()
forall a b. (a -> b) -> a -> b
$ Text
"Could not create container configuration template. runc terminated with exit code " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show Int
e
ByteString
templateBytes <- String -> IO ByteString
BS.readFile String
configJsonPath
Value
template <- case ByteString -> Either String Value
forall a. FromJSON a => ByteString -> Either String a
eitherDecode (ByteString -> ByteString
BL.fromStrict ByteString
templateBytes) of
Right Value
a -> Value -> IO Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
a
Left String
e -> FatalError -> IO Value
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO (Text -> FatalError
FatalError (Text -> FatalError) -> Text -> FatalError
forall a b. (a -> b) -> a -> b
$ Text
"decoding runc config.json template: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show String
e)
let configJson :: Value
configJson = Config -> Value -> Value
effectToRuncSpec Config
config Value
template
String -> ByteString -> IO ()
BS.writeFile String
configJsonPath (ByteString -> ByteString
BL.toStrict (ByteString -> ByteString) -> ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ Value -> ByteString
forall a. ToJSON a => a -> ByteString
encode Value
configJson)
String -> IO ()
createDirectory String
rootfsPath
String -> IO ()
createDirectory String
runcRootPath
String
name <- do
UUID
uuid <- IO UUID
UUID.nextRandom
String -> IO String
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> IO String) -> String -> IO String
forall a b. (a -> b) -> a -> b
$ String
"hercules-ci-" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> UUID -> String
forall a b. (Show a, ConvertText String b) => a -> b
show UUID
uuid
(ExitCode
exitCode, ()
_) <- ((Handle, Handle) -> IO (ExitCode, ())) -> IO (ExitCode, ())
forall a. ((Handle, Handle) -> IO a) -> IO a
withPseudoTerminalHandles (((Handle, Handle) -> IO (ExitCode, ())) -> IO (ExitCode, ()))
-> ((Handle, Handle) -> IO (ExitCode, ())) -> IO (ExitCode, ())
forall a b. (a -> b) -> a -> b
$
\(Handle
master, Handle
terminal) -> do
IO ExitCode -> IO () -> IO (ExitCode, ())
forall a b. IO a -> IO b -> IO (a, b)
concurrently
( do
let createProcSpec :: CreateProcess
createProcSpec =
(String -> [String] -> CreateProcess
System.Process.proc String
runcExe [String
"--root", String
runcRootPath, String
"run", String
name])
{ std_in :: StdStream
std_in = Handle -> StdStream
UseHandle Handle
terminal,
std_out :: StdStream
std_out = Handle -> StdStream
UseHandle Handle
terminal,
std_err :: StdStream
std_err = Handle -> StdStream
UseHandle Handle
terminal,
cwd :: Maybe String
cwd = String -> Maybe String
forall a. a -> Maybe a
Just String
dir
}
CreateProcess
-> (Maybe Handle
-> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO ExitCode)
-> IO ExitCode
forall a.
CreateProcess
-> (Maybe Handle
-> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO a)
-> IO a
withCreateProcess CreateProcess
createProcSpec \Maybe Handle
_subStdin Maybe Handle
_noOut Maybe Handle
_noErr ProcessHandle
processHandle -> do
ProcessHandle -> IO ExitCode
waitForProcess ProcessHandle
processHandle
IO ExitCode -> IO () -> IO ExitCode
forall a b. IO a -> IO b -> IO a
`onException` ( do
Text -> IO ()
forall (m :: * -> *). MonadIO m => Text -> m ()
putErrText Text
"Terminating effect process..."
ExitCode
_ <- CreateProcess
-> (Maybe Handle
-> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO ExitCode)
-> IO ExitCode
forall a.
CreateProcess
-> (Maybe Handle
-> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO a)
-> IO a
System.Process.withCreateProcess (String -> [String] -> CreateProcess
System.Process.proc String
runcExe [String
"kill", String
name]) \Maybe Handle
_ Maybe Handle
_ Maybe Handle
_ ProcessHandle
kh ->
ProcessHandle -> IO ExitCode
waitForProcess ProcessHandle
kh
Int -> IO ()
threadDelay Int
3_000_000
ExitCode
_ <- CreateProcess
-> (Maybe Handle
-> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO ExitCode)
-> IO ExitCode
forall a.
CreateProcess
-> (Maybe Handle
-> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO a)
-> IO a
System.Process.withCreateProcess (String -> [String] -> CreateProcess
System.Process.proc String
runcExe [String
"kill", String
name, String
"KILL"]) \Maybe Handle
_ Maybe Handle
_ Maybe Handle
_ ProcessHandle
kh ->
ProcessHandle -> IO ExitCode
waitForProcess ProcessHandle
kh
Text -> IO ()
forall (m :: * -> *). MonadIO m => Text -> m ()
putErrText Text
"Killed effect process."
)
)
( do
let shovel :: IO ()
shovel =
IO ByteString -> IO ByteString
handleEOF (Handle -> IO ByteString
BS.hGetLine Handle
master) IO ByteString -> (ByteString -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
ByteString
"" -> IO ()
forall (f :: * -> *). Applicative f => f ()
pass
ByteString
someBytes | ByteString
"@nix" ByteString -> ByteString -> Bool
`BS.isPrefixOf` ByteString
someBytes -> do
IO ()
shovel
ByteString
someBytes -> do
Handle -> ByteString -> IO ()
BS.hPut Handle
stderr (ByteString
someBytes ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"\n")
IO ()
shovel
handleEOF :: IO ByteString -> IO ByteString
handleEOF = (IOError -> IO ByteString) -> IO ByteString -> IO ByteString
forall e a. Exception e => (e -> IO a) -> IO a -> IO a
handle \IOError
e -> if IOError -> IOErrorType
ioeGetErrorType IOError
e IOErrorType -> IOErrorType -> Bool
forall a. Eq a => a -> a -> Bool
== IOErrorType
HardwareFault then ByteString -> IO ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure ByteString
"" else IOError -> IO ByteString
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO IOError
e
IO ()
shovel
)
ExitCode -> IO ExitCode
forall (f :: * -> *) a. Applicative f => a -> f a
pure ExitCode
exitCode
withPseudoTerminalHandles :: ((Handle, Handle) -> IO a) -> IO a
withPseudoTerminalHandles :: forall a. ((Handle, Handle) -> IO a) -> IO a
withPseudoTerminalHandles =
IO (Handle, Handle)
-> ((Handle, Handle) -> IO ())
-> ((Handle, Handle) -> IO a)
-> IO a
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket
IO (Handle, Handle)
openPseudoTerminalHandles
( \(Handle
master, Handle
terminal) -> do
Handle -> IO ()
hClose Handle
master IO () -> (SomeException -> IO ()) -> IO ()
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` \(SomeException
_ :: SomeException) -> IO ()
forall (f :: * -> *). Applicative f => f ()
pass
Handle -> IO ()
hClose Handle
terminal IO () -> (SomeException -> IO ()) -> IO ()
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` \(SomeException
_ :: SomeException) -> IO ()
forall (f :: * -> *). Applicative f => f ()
pass
)
openPseudoTerminalHandles :: IO (Handle, Handle)
openPseudoTerminalHandles :: IO (Handle, Handle)
openPseudoTerminalHandles =
IO (Handle, Handle) -> IO (Handle, Handle)
forall a. IO a -> IO a
mask_ do
(Fd
masterFd, Fd
terminalFd) <- IO (Fd, Fd)
openPseudoTerminal
( do
Handle
master <- Fd -> IO Handle
fdToHandle Fd
masterFd
Handle
terminal <- Fd -> IO Handle
fdToHandle Fd
terminalFd
(Handle, Handle) -> IO (Handle, Handle)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Handle
master, Handle
terminal)
)
IO (Handle, Handle) -> IO () -> IO (Handle, Handle)
forall a b. IO a -> IO b -> IO a
`onException` do
Fd -> IO ()
closeFd Fd
masterFd
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Fd
terminalFd Fd -> Fd -> Bool
forall a. Eq a => a -> a -> Bool
/= Fd
masterFd) (Fd -> IO ()
closeFd Fd
terminalFd)