Skip to content

Commit

Permalink
Implement COP rendering, WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
samtay committed Dec 13, 2016
1 parent 7330575 commit 26a254d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# todo

## mvp
1. Use COP to generate docker-compose.yml and pipe to `docker-compose -f -`
1. Fixup haddock docs and add to /docs directory for github.io. Use `stack build --haddock` to check coverage and get output
2. Parse `DOCKMASTER_HOME` from config.yml PATHS into `get_env "DOCKMASTER_HOME" ~> "~/.dockmaster/home" or whatever

## later
0. --local flag to execute on local machine instead of docker-machine targets
1. Use monad transformer to stack Sh on top of Either (error transformer)
2. Read up on exception handling link in haskell-fun-times resources
1 change: 1 addition & 0 deletions dockmaster.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ library
, Dockmaster.Config.Parser
other-modules: Dockmaster.Types
, Dockmaster.Config.Types
, Dockmaster.Compose
, Dockmaster.Utils
build-depends: base >= 4.7 && < 5
, bytestring
Expand Down
7 changes: 2 additions & 5 deletions src/Dockmaster.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Data.Maybe
-- Local modules
import Dockmaster.Parser
import Dockmaster.Config.Parser
import Dockmaster.Compose

-- External modules
import Shelly
Expand Down Expand Up @@ -40,11 +41,7 @@ dm path command args = do
Right dmYml -> do
let targets = map targetName $ dmTargets dmYml -- just grabbing machine name
(flip mapM_) targets $ \m -> dockermachine m $ do
hookWrap' dmYml command $ dockercompose $ command : args

-- | Run docker-compose command
dockercompose :: [T.Text] -> Sh ()
dockercompose = (print_stdout True) . (run_ "docker-compose")
hookWrap' dmYml command $ dockercompose dmYml $ command : args

-- | Takes machine name and Sh action, and wraps Sh action in scope of
-- docker-machine env
Expand Down
47 changes: 47 additions & 0 deletions src/Dockmaster/Compose.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ExtendedDefaultRules #-}
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
module Dockmaster.Compose
( -- * Orchestration for docker-compose.yml
dockercompose
) where

-- Local modules
import Dockmaster.Types
import Dockmaster.Utils (parsePath, toText)

-- External modules
import Control.Monad ((<=<))
import Shelly
import Prelude hiding (FilePath)
import qualified Data.Text as T
default (T.Text)

-- | Runs docker-compose but uses compiled docker-compose.yml file
-- if template/vars are specified in dockmaster.yml
dockercompose :: Dockmaster -> [T.Text] -> Sh ()
dockercompose cfg optargs = print_stdout True $ maybe
(run_ "docker-compose" optargs) -- default run on docker-compose.yml, as usual
(composeViaTemplate optargs)
(dmFile cfg)

-- | Uses 'ComposeFile' argument to build templated docker-compose.yml content
-- and pipes it directly to docker-compose
--
-- TODO make sure the pipe doesnt output garbage
-- If we need shell-level pipe this is possible:
-- https://hackage.haskell.org/package/shelly-1.6.8.1/docs/Shelly.html#v:-45--124--45-
composeViaTemplate :: [T.Text] -> ComposeFile -> Sh ()
composeViaTemplate optargs cf =
compileTemplate cf -|- run_ "docker-compose" (optargs ++ ["-f", "-"])

-- | Leverages @cop@ to build docker-compose.yml content from template/vars
--
-- This function returns the stdout wrapped in 'Sh'. It can then be piped to
-- docker-compose via @docker-compose -f -@ -- see documentation for docker-compose.
compileTemplate :: ComposeFile -> Sh T.Text
compileTemplate cf = do
file <- parse (cfPath cf)
vars <- mapM parse (cfVars cf)
run "cop" $ ["--render-template", file] ++ vars
where parse = toText <=< parsePath <=< toText
11 changes: 7 additions & 4 deletions src/Dockmaster/Types.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{-# LANGUAGE OverloadedStrings #-}
module Dockmaster.Types where

import Dockmaster.Config.Types

import Data.Yaml
import Control.Applicative
import Data.HashMap.Lazy (HashMap, lookup, member)
import Data.Monoid (mempty)
import Prelude hiding (lookup)
import Shelly
import Prelude hiding (lookup, FilePath)
import qualified Data.Text as T

-- | Dockmaster configuration (specified by dockmaster.yml)
Expand All @@ -15,8 +18,8 @@ data Dockmaster = Dockmaster { dmFile :: Maybe ComposeFile
} deriving (Show)

-- | Configuration for docker-compose.yml file template & vars
data ComposeFile = ComposeFile { cfPath :: Maybe T.Text
, cfConfig :: [T.Text]
data ComposeFile = ComposeFile { cfPath :: FilePath
, cfVars :: [FilePath]
} deriving (Show)

-- | Targets are used to identify where compositions are run
Expand All @@ -36,7 +39,7 @@ data CommandConfig = CommandConfig { ccCompose :: Bool

instance FromJSON ComposeFile where
parseJSON (Object v) = ComposeFile
<$> v .:? "path"
<$> v .: "path"
<*> v .:? "config" .!= []

instance FromJSON Target where
Expand Down

0 comments on commit 26a254d

Please sign in to comment.