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

3
{-# LANGUAGE OverloadedStrings, RecordWildCards, LambdaCase #-}
4 5 6

import           Argo.Stack
import           Argo.Utils
7
import           Argo.Args
8 9 10 11 12 13 14 15 16 17
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
18 19
import           Data.Either

20 21 22 23 24
opts :: StackArgs -> Parser (IO ())
opts sa = hsubparser
  (  command "clean"
             (info (pure $ runClean sa) (progDesc "Clean sockets, logfiles."))
  <> command
25
       "daemon-only"
26
       (info
27
         (runDaemon <$> parseExtendStackArgs sa)
28 29 30 31 32 33
         (progDesc
           "Set up and launch the daemon in synchronous mode, \
           \with properly cleaned sockets, logfiles."
         )
       )
  <> command
34 35
       "full-stack"
       (info (runStack <$> parseExtendStackArgs sa)
36 37
             (progDesc "Setup stack and run a command in a container.")
       )
38 39 40
  <> command
       "helloworld"
       (info
41 42 43 44 45 46 47 48
         (runStack <$> parseExtendStackArgs
           (let msg = "Hello-Moto"
            in  sa { app           = "echo"
                   , args          = [msg]
                   , messageCmdOut = Just msg
                   , messageCmdErr = Just msg
                   }
           )
49
         )
50
         (progDesc
51
           "Test 1: Setup stack and check that a hello world app sends \
52 53 54
           \message back to cmd."
         )
       )
55 56 57
  <> command
       "perfwrapper"
       (info
58 59 60
         (runStack <$> parseExtendStackArgs
           (sa { manifestName     = "perfwrap.json"
               , app              = "sleep"
61
               , args             = ["15"]
62 63
               , messageDaemonOut = Just "progress"
               , messageDaemonErr = Just "progress"
64 65 66
               }
           )
         )
67
         (progDesc
68 69
           "Test 2: Setup stack and check that argo-perf-wrapper sends \
           \ at least one progress message up."
70 71
         )
       )
72 73 74 75
  <> 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."
76 77 78 79 80
  )

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

84 85 86 87 88 89
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)
90

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

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