2014-11-19 03:50:38 +00:00
module Propellor.CmdLine (
defaultMain ,
processCmdLine ,
) where
2014-03-30 23:10:32 +00:00
2014-04-01 15:59:48 +00:00
import System.Environment ( getArgs )
2014-03-30 23:10:32 +00:00
import Data.List
import System.Exit
2014-04-03 17:49:26 +00:00
import System.PosixCompat
2014-04-04 20:20:20 +00:00
import Control.Exception ( bracket )
import System.Posix.IO
2014-03-30 23:10:32 +00:00
2014-03-31 03:55:59 +00:00
import Propellor
2014-11-18 17:29:50 +00:00
import Propellor.Protocol
2014-11-11 16:58:53 +00:00
import Propellor.Gpg
2014-11-18 22:39:10 +00:00
import Propellor.Git
2014-11-18 22:42:36 +00:00
import Propellor.Ssh
2014-11-19 02:10:50 +00:00
import Propellor.Server
2014-04-01 17:51:58 +00:00
import qualified Propellor.Property.Docker as Docker
2014-04-04 06:06:19 +00:00
import qualified Propellor.Property.Docker.Shim as DockerShim
2014-03-31 03:55:59 +00:00
import Utility.SafeCommand
2014-03-30 23:10:32 +00:00
2014-11-19 00:33:25 +00:00
usage :: Handle -> IO ()
usage h = hPutStrLn h $ unlines
[ " Usage: "
, " propellor "
, " propellor hostname "
, " propellor --spin hostname "
, " propellor --add-key keyid "
, " propellor --set field context "
, " propellor --dump field context "
, " propellor --edit field context "
, " propellor --list-fields "
]
usageError :: [ String ] -> IO a
usageError ps = do
usage stderr
error ( " (Unexpected: " ++ show ps )
2014-03-30 23:10:32 +00:00
processCmdLine :: IO CmdLine
processCmdLine = go =<< getArgs
where
2014-11-18 05:25:54 +00:00
go ( " --run " : h : [] ) = return $ Run h
2014-11-18 23:49:34 +00:00
go ( " --spin " : h : [] ) = return $ Spin h
2014-03-31 16:06:04 +00:00
go ( " --add-key " : k : [] ) = return $ AddKey k
2014-07-06 19:56:56 +00:00
go ( " --set " : f : c : [] ) = withprivfield f c Set
go ( " --dump " : f : c : [] ) = withprivfield f c Dump
go ( " --edit " : f : c : [] ) = withprivfield f c Edit
go ( " --list-fields " : [] ) = return ListFields
2014-11-19 00:33:25 +00:00
go ( " --help " : _ ) = do
usage stdout
exitFailure
2014-11-18 23:49:34 +00:00
go ( " --update " : h : [] ) = return $ Update h
go ( " --boot " : h : [] ) = return $ Update h -- for back-compat
2014-04-01 06:37:48 +00:00
go ( " --continue " : s : [] ) = case readish s of
2014-03-31 20:37:19 +00:00
Just cmdline -> return $ Continue cmdline
2014-11-19 00:28:16 +00:00
Nothing -> errorMessage $ " --continue serialization failure ( " ++ s ++ " ) "
2014-11-19 00:38:11 +00:00
go ( " --gitpush " : fin : fout : _ ) = return $ GitPush ( Prelude . read fin ) ( Prelude . read fout )
2014-04-01 15:59:48 +00:00
go ( h : [] )
2014-11-19 00:33:25 +00:00
| " -- " ` isPrefixOf ` h = usageError [ h ]
2014-04-01 15:59:48 +00:00
| otherwise = return $ Run h
2014-03-30 23:10:32 +00:00
go [] = do
s <- takeWhile ( /= '\ n' ) <$> readProcess " hostname " [ " -f " ]
if null s
2014-03-31 22:31:08 +00:00
then errorMessage " Cannot determine hostname! Pass it on the command line. "
2014-03-30 23:10:32 +00:00
else return $ Run s
2014-11-19 00:33:25 +00:00
go v = usageError v
2014-03-30 23:10:32 +00:00
2014-07-06 19:56:56 +00:00
withprivfield s c f = case readish s of
Just pf -> return $ f pf ( Context c )
2014-06-01 20:58:05 +00:00
Nothing -> errorMessage $ " Unknown privdata field " ++ s
2014-11-19 03:50:38 +00:00
-- | Runs propellor on hosts, as controlled by command-line options.
2014-04-11 01:09:20 +00:00
defaultMain :: [ Host ] -> IO ()
defaultMain hostlist = do
2014-04-04 06:06:19 +00:00
DockerShim . cleanEnv
2014-04-01 15:59:48 +00:00
checkDebugMode
cmdline <- processCmdLine
debug [ " command line: " , show cmdline ]
go True cmdline
2014-03-30 23:10:32 +00:00
where
2014-03-31 20:37:19 +00:00
go _ ( Continue cmdline ) = go False cmdline
2014-07-06 19:56:56 +00:00
go _ ( Set field context ) = setPrivData field context
go _ ( Dump field context ) = dumpPrivData field context
go _ ( Edit field context ) = editPrivData field context
2014-07-06 20:44:13 +00:00
go _ ListFields = listPrivDataFields hostlist
2014-03-31 20:37:19 +00:00
go _ ( AddKey keyid ) = addKey keyid
2014-11-19 04:30:06 +00:00
go _ ( Chain hn ) = withhost hn $ \ h -> do
2014-05-31 22:02:56 +00:00
r <- runPropellor h $ ensureProperties $ hostProperties h
2014-04-01 18:47:30 +00:00
putStrLn $ " \ n " ++ show r
2014-04-11 01:09:20 +00:00
go _ ( Docker hn ) = Docker . chain hn
2014-11-19 02:10:50 +00:00
go _ ( GitPush fin fout ) = gitPushHelper fin fout
2014-03-31 22:53:27 +00:00
go True cmdline @ ( Spin _ ) = buildFirst cmdline $ go False cmdline
2014-03-31 21:57:12 +00:00
go True cmdline = updateFirst cmdline $ go False cmdline
2014-07-06 21:37:10 +00:00
go False ( Spin hn ) = withhost hn $ spin hn
2014-11-18 22:26:15 +00:00
go False cmdline @ ( SimpleRun hn ) = buildFirst cmdline $
go False ( Run hn )
2014-04-11 01:09:20 +00:00
go False ( Run hn ) = ifM ( ( == ) 0 <$> getRealUserID )
2014-05-31 22:02:56 +00:00
( onlyProcess $ withhost hn mainProperties
2014-04-11 01:09:20 +00:00
, go True ( Spin hn )
2014-04-03 17:58:21 +00:00
)
2014-11-18 23:36:30 +00:00
go False ( Update _ ) = do
2014-11-18 22:13:42 +00:00
forceConsole
2014-11-18 23:36:30 +00:00
onlyProcess update
2014-03-31 20:37:19 +00:00
2014-05-31 22:02:56 +00:00
withhost :: HostName -> ( Host -> IO () ) -> IO ()
2014-07-07 05:57:59 +00:00
withhost hn a = maybe ( unknownhost hn hostlist ) a ( findHost hostlist hn )
2014-03-30 23:10:32 +00:00
2014-04-04 20:20:20 +00:00
onlyProcess :: IO a -> IO a
onlyProcess a = bracket lock unlock ( const a )
where
lock = do
2014-04-04 22:34:03 +00:00
l <- createFile lockfile stdFileMode
2014-04-04 20:20:20 +00:00
setLock l ( WriteLock , AbsoluteSeek , 0 , 0 )
2014-04-04 22:34:03 +00:00
` catchIO ` const alreadyrunning
2014-04-04 20:20:20 +00:00
return l
unlock = closeFd
alreadyrunning = error " Propellor is already running on this host! "
lockfile = localdir </> " .lock "
2014-07-07 05:57:59 +00:00
unknownhost :: HostName -> [ Host ] -> IO a
unknownhost h hosts = errorMessage $ unlines
2014-04-08 22:42:54 +00:00
[ " Propellor does not know about host: " ++ h
2014-04-03 04:59:26 +00:00
, " (Perhaps you should specify the real hostname on the command line?) "
, " (Or, edit propellor's config.hs to configure this host) "
2014-07-07 05:57:59 +00:00
, " Known hosts: " ++ unwords ( map hostName hosts )
2014-03-31 03:02:10 +00:00
]
2014-03-31 22:53:27 +00:00
buildFirst :: CmdLine -> IO () -> IO ()
buildFirst cmdline next = do
oldtime <- getmtime
2014-03-31 23:31:35 +00:00
ifM ( actionMessage " Propellor build " $ boolSystem " make " [ Param " build " ] )
2014-03-31 22:53:27 +00:00
( do
newtime <- getmtime
if newtime == oldtime
then next
2014-07-07 06:01:57 +00:00
else void $ boolSystem " ./propellor " [ Param " --continue " , Param ( show cmdline ) ]
2014-03-31 22:53:27 +00:00
, errorMessage " Propellor build failed! "
)
where
getmtime = catchMaybeIO $ getModificationTime " propellor "
2014-03-31 21:57:12 +00:00
updateFirst :: CmdLine -> IO () -> IO ()
2014-11-18 19:43:00 +00:00
updateFirst cmdline next = ifM hasOrigin ( updateFirst' cmdline next , next )
updateFirst' :: CmdLine -> IO () -> IO ()
updateFirst' cmdline next = do
2014-04-03 16:33:36 +00:00
branchref <- getCurrentBranch
let originbranch = " origin " </> branchref
2014-03-31 22:39:39 +00:00
2014-03-31 23:31:35 +00:00
void $ actionMessage " Git fetch " $ boolSystem " git " [ Param " fetch " ]
2014-03-31 20:20:38 +00:00
2014-06-10 19:49:17 +00:00
oldsha <- getCurrentGitSha1 branchref
2014-11-18 23:43:53 +00:00
whenM ( doesFileExist keyring ) $
ifM ( verifyOriginBranch originbranch )
( do
2014-03-31 20:42:25 +00:00
putStrLn $ " git branch " ++ originbranch ++ " gpg signature verified; merging "
hFlush stdout
2014-06-10 19:49:17 +00:00
void $ boolSystem " git " [ Param " merge " , Param originbranch ]
2014-11-18 23:43:53 +00:00
, warningMessage $ " git branch " ++ originbranch ++ " is not signed with a trusted gpg key; refusing to deploy it! (Running with previous configuration instead.) "
)
2014-03-31 20:20:38 +00:00
2014-03-31 20:40:03 +00:00
newsha <- getCurrentGitSha1 branchref
2014-03-31 20:37:19 +00:00
if oldsha == newsha
2014-03-31 20:50:30 +00:00
then next
2014-03-31 23:31:35 +00:00
else ifM ( actionMessage " Propellor build " $ boolSystem " make " [ Param " build " ] )
2014-03-31 22:31:08 +00:00
( void $ boolSystem " ./propellor " [ Param " --continue " , Param ( show cmdline ) ]
, errorMessage " Propellor build failed! "
)
2014-03-31 20:20:38 +00:00
2014-07-06 21:37:10 +00:00
spin :: HostName -> Host -> IO ()
spin hn hst = do
2014-11-18 22:13:42 +00:00
void $ actionMessage " Git commit (signed) " $
2014-11-18 22:07:26 +00:00
gitCommit [ Param " --allow-empty " , Param " -a " , Param " -m " , Param " propellor spin " ]
2014-11-18 21:10:10 +00:00
-- Push to central origin repo first, if possible.
-- The remote propellor will pull from there, which avoids
-- us needing to send stuff directly to the remote host.
whenM hasOrigin $
2014-11-18 22:13:42 +00:00
void $ actionMessage " Push to central git repository " $
2014-11-18 22:07:26 +00:00
boolSystem " git " [ Param " push " ]
2014-11-18 21:10:10 +00:00
2014-04-11 01:09:20 +00:00
cacheparams <- toCommand <$> sshCachingParams hn
2014-11-19 02:10:50 +00:00
-- Install, or update the remote propellor.
updateServer hn hst $ withBothHandles createProcessSuccess
( proc " ssh " $ cacheparams ++ [ user , updatecmd ] )
-- And now we can run it.
2014-11-19 00:19:10 +00:00
unlessM ( boolSystem " ssh " ( map Param $ cacheparams ++ [ " -t " , user , runcmd ] ) ) $
2014-11-18 05:25:54 +00:00
error $ " remote propellor failed (running: " ++ runcmd ++ " ) "
2014-03-31 19:40:16 +00:00
where
2014-04-11 01:09:20 +00:00
user = " root@ " ++ hn
2014-03-31 19:40:16 +00:00
2014-11-18 05:01:50 +00:00
mkcmd = shellWrap . intercalate " ; "
2014-11-19 02:10:50 +00:00
updatecmd = mkcmd
2014-03-31 20:45:32 +00:00
[ " if [ ! -d " ++ localdir ++ " ] "
, " then " ++ intercalate " && "
2014-07-07 06:04:21 +00:00
[ " apt-get update "
, " apt-get --no-install-recommends --no-upgrade -y install git make "
2014-03-31 20:45:32 +00:00
, " echo " ++ toMarked statusMarker ( show NeedGitClone )
]
, " else " ++ intercalate " && "
[ " cd " ++ localdir
2014-04-10 04:40:38 +00:00
, " if ! test -x ./propellor; then make deps build; fi "
2014-11-18 05:04:41 +00:00
, " ./propellor --boot " ++ hn
2014-03-30 23:10:32 +00:00
]
2014-03-31 20:52:58 +00:00
, " fi "
2014-03-30 23:10:32 +00:00
]
2014-03-31 19:40:16 +00:00
2014-11-18 05:25:54 +00:00
runcmd = mkcmd
2014-11-18 22:26:15 +00:00
[ " cd " ++ localdir ++ " && ./propellor --continue " ++ shellEscape ( show ( SimpleRun hn ) ) ]