-- | Implementation of an execution environment that uses /docker/.
module B9.Docker
  ( Docker (..),
  )
where

import B9.B9Config
  ( dockerConfigs,
    getB9Config,
  )
import B9.B9Config.Docker as X
import B9.Container
import B9.DiskImages
import B9.ShellScript
import Control.Lens (view)

newtype Docker = Docker DockerConfig

instance Backend Docker where
  getBackendConfig :: proxy Docker -> Eff e (Maybe Docker)
getBackendConfig proxy Docker
_ =
    (DockerConfig -> Docker) -> Maybe DockerConfig -> Maybe Docker
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DockerConfig -> Docker
Docker (Maybe DockerConfig -> Maybe Docker)
-> (B9Config -> Maybe DockerConfig) -> B9Config -> Maybe Docker
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting (Maybe DockerConfig) B9Config (Maybe DockerConfig)
-> B9Config -> Maybe DockerConfig
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting (Maybe DockerConfig) B9Config (Maybe DockerConfig)
Lens' B9Config (Maybe DockerConfig)
dockerConfigs (B9Config -> Maybe Docker)
-> Eff e B9Config -> Eff e (Maybe Docker)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Eff e B9Config
forall (e :: [* -> *]). Member B9ConfigReader e => Eff e B9Config
getB9Config

  -- supportedImageTypes :: proxy config -> [ImageType]
  supportedImageTypes :: proxy Docker -> [ImageType]
supportedImageTypes proxy Docker
_ = [ImageType
Raw]

  -- runInEnvironment ::
  --   forall e.
  --   (Member BuildInfoReader e, CommandIO e) =>
  --   config ->
  --   ExecEnv ->
  --   Script ->
  --   Eff e Bool
  runInEnvironment :: Docker -> ExecEnv -> Script -> Eff e Bool
runInEnvironment (Docker DockerConfig
_dcfg) ExecEnv
_env Script
scriptIn = do
    if Script -> Bool
emptyScript Script
scriptIn
      then Bool -> Eff e Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
      else do [Char] -> Eff e Bool
forall a. HasCallStack => [Char] -> a
error [Char]
"TODO"
-- where
--       setUp = do
--         buildId <- getBuildId
--         buildBaseDir <- getBuildDir
--         uuid <- randomUUID
--         let scriptDirHost = buildDir </> "init-script"
--             scriptDirGuest = "/" ++ buildId
--             domainFile = buildBaseDir </> uuid' <.> domainConfig
--             mkDomain =
--               createDomain cfgIn env buildId uuid' scriptDirHost scriptDirGuest
--             uuid' = printf "%U" uuid
--             setupEnv =
--               Begin
--                 [ Run "export" ["HOME=/root"],
--                   Run "export" ["USER=root"],
--                   Run "source" ["/etc/profile"]
--                 ]
--             script = Begin [setupEnv, scriptIn, successMarkerCmd scriptDirGuest]
--             buildDir = buildBaseDir </> uuid'
--         liftIO $ do
--           createDirectoryIfMissing True scriptDirHost
--           writeSh (scriptDirHost </> initScript) script
--           domain <- mkDomain
--           writeFile domainFile domain
--         return $ Context scriptDirHost uuid domainFile cfgIn