Added --unset to delete a privdata field.

This commit is contained in:
Joey Hess 2015-06-29 16:40:01 -04:00
parent 37a5c05aba
commit fc04d0d81d
5 changed files with 24 additions and 5 deletions

1
debian/changelog vendored
View File

@ -3,6 +3,7 @@ propellor (2.6.0) UNRELEASED; urgency=medium
* Replace String type synonym Docker.Image by a data type * Replace String type synonym Docker.Image by a data type
which allows to specify an image name and an optional tag. (API change) which allows to specify an image name and an optional tag. (API change)
Thanks, Antoine Eiche. Thanks, Antoine Eiche.
* Added --unset to delete a privdata field.
-- Joey Hess <id@joeyh.name> Tue, 16 Jun 2015 14:49:12 -0400 -- Joey Hess <id@joeyh.name> Tue, 16 Jun 2015 14:49:12 -0400

View File

@ -71,6 +71,10 @@ and configured in haskell.
Sets a field of privdata. The content is read in from stdin. Sets a field of privdata. The content is read in from stdin.
* propellor --unset field context
Removes a value from the privdata store.
* propellor --dump field context * propellor --dump field context
Outputs the privdata value to stdout. Outputs the privdata value to stdout.

View File

@ -51,6 +51,7 @@ processCmdLine = go =<< getArgs
_ -> Spin <$> mapM hostname ps <*> pure Nothing _ -> Spin <$> mapM hostname ps <*> pure Nothing
go ("--add-key":k:[]) = return $ AddKey k go ("--add-key":k:[]) = return $ AddKey k
go ("--set":f:c:[]) = withprivfield f c Set go ("--set":f:c:[]) = withprivfield f c Set
go ("--unset":f:c:[]) = withprivfield f c Unset
go ("--dump":f:c:[]) = withprivfield f c Dump go ("--dump":f:c:[]) = withprivfield f c Dump
go ("--edit":f:c:[]) = withprivfield f c Edit go ("--edit":f:c:[]) = withprivfield f c Edit
go ("--list-fields":[]) = return ListFields go ("--list-fields":[]) = return ListFields
@ -94,6 +95,7 @@ defaultMain hostlist = do
go _ (Continue cmdline) = go False cmdline go _ (Continue cmdline) = go False cmdline
go _ Check = return () go _ Check = return ()
go _ (Set field context) = setPrivData field context go _ (Set field context) = setPrivData field context
go _ (Unset field context) = unsetPrivData field context
go _ (Dump field context) = dumpPrivData field context go _ (Dump field context) = dumpPrivData field context
go _ (Edit field context) = editPrivData field context go _ (Edit field context) = editPrivData field context
go _ ListFields = listPrivDataFields hostlist go _ ListFields = listPrivDataFields hostlist

View File

@ -6,6 +6,7 @@ module Propellor.PrivData (
withSomePrivData, withSomePrivData,
addPrivData, addPrivData,
setPrivData, setPrivData,
unsetPrivData,
dumpPrivData, dumpPrivData,
editPrivData, editPrivData,
filterPrivData, filterPrivData,
@ -143,6 +144,11 @@ setPrivData field context = do
putStrLn "Enter private data on stdin; ctrl-D when done:" putStrLn "Enter private data on stdin; ctrl-D when done:"
setPrivDataTo field context =<< hGetContentsStrict stdin setPrivDataTo field context =<< hGetContentsStrict stdin
unsetPrivData :: PrivDataField -> Context -> IO ()
unsetPrivData field context = do
modifyPrivData $ M.delete (field, context)
putStrLn "Private data unset."
dumpPrivData :: PrivDataField -> Context -> IO () dumpPrivData :: PrivDataField -> Context -> IO ()
dumpPrivData field context = dumpPrivData field context =
maybe (error "Requested privdata is not set.") putStrLn maybe (error "Requested privdata is not set.") putStrLn
@ -192,17 +198,22 @@ listPrivDataFields hosts = do
setPrivDataTo :: PrivDataField -> Context -> PrivData -> IO () setPrivDataTo :: PrivDataField -> Context -> PrivData -> IO ()
setPrivDataTo field context value = do setPrivDataTo field context value = do
makePrivDataDir modifyPrivData set
m <- decryptPrivData
let m' = M.insert (field, context) (chomp value) m
gpgEncrypt privDataFile (show m')
putStrLn "Private data set." putStrLn "Private data set."
void $ boolSystem "git" [Param "add", File privDataFile]
where where
set = M.insert (field, context) (chomp value)
chomp s chomp s
| end s == "\n" = chomp (beginning s) | end s == "\n" = chomp (beginning s)
| otherwise = s | otherwise = s
modifyPrivData :: (PrivMap -> PrivMap) -> IO ()
modifyPrivData f = do
makePrivDataDir
m <- decryptPrivData
let m' = f m
gpgEncrypt privDataFile (show m')
void $ boolSystem "git" [Param "add", File privDataFile]
decryptPrivData :: IO PrivMap decryptPrivData :: IO PrivMap
decryptPrivData = fromMaybe M.empty . readish <$> gpgDecrypt privDataFile decryptPrivData = fromMaybe M.empty . readish <$> gpgDecrypt privDataFile

View File

@ -10,6 +10,7 @@ data CmdLine
| Spin [HostName] (Maybe HostName) | Spin [HostName] (Maybe HostName)
| SimpleRun HostName | SimpleRun HostName
| Set PrivDataField Context | Set PrivDataField Context
| Unset PrivDataField Context
| Dump PrivDataField Context | Dump PrivDataField Context
| Edit PrivDataField Context | Edit PrivDataField Context
| ListFields | ListFields