From 740740d21553e45447259c3a21e95e94b1e59a42 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Sun, 1 Jun 2014 16:58:05 -0400 Subject: Added --dump to dump out a field of a host's privdata. Useful for editing it. --- src/Propellor/CmdLine.hs | 11 ++++++++--- src/Propellor/PrivData.hs | 12 +++++++++++- src/Propellor/Types.hs | 1 + 3 files changed, 20 insertions(+), 4 deletions(-) (limited to 'src/Propellor') diff --git a/src/Propellor/CmdLine.hs b/src/Propellor/CmdLine.hs index a7b7ef96..06a5921d 100644 --- a/src/Propellor/CmdLine.hs +++ b/src/Propellor/CmdLine.hs @@ -27,6 +27,7 @@ usage = do , " propellor hostname" , " propellor --spin hostname" , " propellor --set hostname field" + , " propellor --dump hostname field" , " propellor --add-key keyid" ] exitFailure @@ -38,9 +39,8 @@ processCmdLine = go =<< getArgs go ("--spin":h:[]) = return $ Spin h go ("--boot":h:[]) = return $ Boot h go ("--add-key":k:[]) = return $ AddKey k - go ("--set":h:f:[]) = case readish f of - Just pf -> return $ Set h pf - Nothing -> errorMessage $ "Unknown privdata field " ++ f + go ("--set":h:f:[]) = withprivfield f (return . Set h) + go ("--dump":h:f:[]) = withprivfield f (return . Dump h) go ("--continue":s:[]) = case readish s of Just cmdline -> return $ Continue cmdline Nothing -> errorMessage "--continue serialization failure" @@ -56,6 +56,10 @@ processCmdLine = go =<< getArgs else return $ Run s go _ = usage + withprivfield s f = case readish s of + Just pf -> f pf + Nothing -> errorMessage $ "Unknown privdata field " ++ s + defaultMain :: [Host] -> IO () defaultMain hostlist = do DockerShim.cleanEnv @@ -66,6 +70,7 @@ defaultMain hostlist = do where go _ (Continue cmdline) = go False cmdline go _ (Set hn field) = setPrivData hn field + go _ (Dump hn field) = dumpPrivData hn field go _ (AddKey keyid) = addKey keyid go _ (Chain hn) = withhost hn $ \h -> do r <- runPropellor h $ ensureProperties $ hostProperties h diff --git a/src/Propellor/PrivData.hs b/src/Propellor/PrivData.hs index 54f67d73..5ddbdcff 100644 --- a/src/Propellor/PrivData.hs +++ b/src/Propellor/PrivData.hs @@ -49,7 +49,7 @@ setPrivData host field = do value <- chomp <$> hGetContentsStrict stdin makePrivDataDir let f = privDataFile host - m <- fromMaybe M.empty . readish <$> gpgDecrypt f + m <- decryptPrivData host let m' = M.insert field value m gpgEncrypt f (show m') putStrLn "Private data set." @@ -59,6 +59,16 @@ setPrivData host field = do | end s == "\n" = chomp (beginning s) | otherwise = s +dumpPrivData :: HostName -> PrivDataField -> IO () +dumpPrivData host field = go . M.lookup field =<< decryptPrivData host + where + go Nothing = error "Requested privdata is not set." + go (Just s) = putStrLn s + +decryptPrivData :: HostName -> IO (M.Map PrivDataField String) +decryptPrivData host = fromMaybe M.empty . readish + <$> gpgDecrypt (privDataFile host) + makePrivDataDir :: IO () makePrivDataDir = createDirectoryIfMissing False privDataDir diff --git a/src/Propellor/Types.hs b/src/Propellor/Types.hs index 4ea97bce..0f96bbbc 100644 --- a/src/Propellor/Types.hs +++ b/src/Propellor/Types.hs @@ -132,6 +132,7 @@ data CmdLine | Spin HostName | Boot HostName | Set HostName PrivDataField + | Dump HostName PrivDataField | AddKey String | Continue CmdLine | Chain HostName -- cgit v1.2.3