argotk.hs 2.99 KB
Newer Older
1 2
#! /usr/bin/env runhaskell

3
{-# LANGUAGE OverloadedStrings, RecordWildCards, LambdaCase #-}
4 5 6 7 8 9 10 11 12 13 14 15 16

import           Argo.Stack
import           Argo.Utils
import           Turtle
import           Prelude                 hiding ( FilePath )
import           Data.Default
import           Control.Concurrent.Async
import           System.Environment
import           System.Console.ANSI
import           System.Console.ANSI.Types      ( Color )
import           Options.Applicative
import           System.Posix.Signals
import           Control.Monad
17 18
import           Data.Either

19 20 21 22 23
opts :: StackArgs -> Parser (IO ())
opts sa = hsubparser
  (  command "clean"
             (info (pure $ runClean sa) (progDesc "Clean sockets, logfiles."))
  <> command
24
       "daemon-only"
25
       (info
26
         (runDaemon <$> parseExtendStackArgs sa)
27 28 29 30 31 32
         (progDesc
           "Set up and launch the daemon in synchronous mode, \
           \with properly cleaned sockets, logfiles."
         )
       )
  <> command
33 34
       "full-stack"
       (info (runStack <$> parseExtendStackArgs sa)
35 36
             (progDesc "Setup stack and run a command in a container.")
       )
37 38 39
  <> command
       "helloworld"
       (info
40 41 42 43 44 45 46 47
         (runStack <$> parseExtendStackArgs
           (let msg = "Hello-Moto"
            in  sa { app           = "echo"
                   , args          = [msg]
                   , messageCmdOut = Just msg
                   , messageCmdErr = Just msg
                   }
           )
48
         )
49
         (progDesc
50
           "Test 1: Setup stack and check that a hello world app sends \
51 52 53
           \message back to cmd."
         )
       )
54 55 56
  <> command
       "perfwrapper"
       (info
57 58 59 60 61 62
         (runStack <$> parseExtendStackArgs
           (sa { manifestName     = "perfwrap.json"
               , app              = "sleep"
               , args             = ["5"]
               , messageDaemonOut = Just "progress"
               , messageDaemonErr = Just "progress"
63 64 65
               }
           )
         )
66
         (progDesc
67 68
           "Test 2: Setup stack and check that argo-perf-wrapper sends \
           \ at least one progress message up."
69 70
         )
       )
71 72 73 74
  <> help
       "Type of test to run. There are extensive options under each action,\
       \ but be careful, these do not all have the same defaults. The default\
       \ values are printed when you call --help on these actions."
75 76 77 78 79
  )

main :: IO ()
main = do
  manifests <- getEnv "MANIFESTS"
80 81
  let a = def { manifestDir = decodeString manifests }
  join $ execParser (info (opts a <**> helper) idm)
82

83 84 85 86 87 88
runStack :: StackArgs -> IO ()
runStack a@StackArgs {..} = sh $ runSimpleStack a >>= \case
  FoundMessage -> printSuccess "Found message!\n" >> exit ExitSuccess
  DaemonDied ->
    printError "Daemon died unexpectedly.\n" >> exit (ExitFailure 1)
  CmdDied -> printError "Did not find message.\n" >> exit (ExitFailure 1)
89

90
runClean :: StackArgs -> IO ()
91
runClean = sh . cleanLeftovers
92

93
runDaemon :: StackArgs -> IO ()
94
runDaemon a = sh $ cleanLeftovers a >> prepareDaemon a >>= liftIO