From 85c3d110882f0f9d70316235221ba8b20754661f Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Mon, 1 Jun 2015 16:12:21 -0400 Subject: reorganize Port type for systemd can use it --- src/Propellor/Types/OS.hs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/Propellor/Types') diff --git a/src/Propellor/Types/OS.hs b/src/Propellor/Types/OS.hs index 58bd809a..c46d9a28 100644 --- a/src/Propellor/Types/OS.hs +++ b/src/Propellor/Types/OS.hs @@ -10,6 +10,7 @@ module Propellor.Types.OS ( User(..), Group(..), userGroup, + Port(..), ) where import Network.BSD (HostName) @@ -42,3 +43,6 @@ newtype Group = Group String -- | Makes a Group with the same name as the User. userGroup :: User -> Group userGroup (User u) = Group u + +newtype Port = Port Int + deriving (Eq, Show) -- cgit v1.2.3 From 765367dab9b61a512e07268c921f950677af4f27 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Mon, 1 Jun 2015 23:16:25 -0400 Subject: add Bound --- propellor.cabal | 1 + src/Propellor/Property/Systemd.hs | 44 ++++++++++++++++++++------------------- src/Propellor/Types/Container.hs | 30 ++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 21 deletions(-) create mode 100644 src/Propellor/Types/Container.hs (limited to 'src/Propellor/Types') diff --git a/propellor.cabal b/propellor.cabal index 16dffe31..9edc1436 100644 --- a/propellor.cabal +++ b/propellor.cabal @@ -121,6 +121,7 @@ Library Propellor.Exception Propellor.Types Propellor.Types.Chroot + Propellor.Types.Container Propellor.Types.Docker Propellor.Types.Dns Propellor.Types.Empty diff --git a/src/Propellor/Property/Systemd.hs b/src/Propellor/Property/Systemd.hs index 055c02ed..1d03d557 100644 --- a/src/Propellor/Property/Systemd.hs +++ b/src/Propellor/Property/Systemd.hs @@ -1,3 +1,5 @@ +{-# LANGUAGE FlexibleInstances #-} + module Propellor.Property.Systemd ( -- * Services module Propellor.Property.Systemd.Core, @@ -24,17 +26,18 @@ module Propellor.Property.Systemd ( resolvConfed, linkJournal, privateNetwork, - ForwardedPort(..), + module Propellor.Types.Container, Proto(..), - PortSpec(..), Publishable, publish, + Bindable, bind, bindRo, ) where import Propellor import Propellor.Types.Chroot +import Propellor.Types.Container import qualified Propellor.Property.Chroot as Chroot import qualified Propellor.Property.Apt as Apt import qualified Propellor.Property.File as File @@ -308,21 +311,14 @@ class Publishable a where instance Publishable Port where toPublish (Port n) = show n -data ForwardedPort = ForwardedPort - { hostPort :: Port - , containerPort :: Port - } - -instance Publishable ForwardedPort where - toPublish fp = toPublish (hostPort fp) ++ ":" ++ toPublish (containerPort fp) +instance Publishable (Bound Port) where + toPublish v = toPublish (hostSide v) ++ ":" ++ toPublish (containerSide v) data Proto = TCP | UDP -data PortSpec = PortSpec Proto ForwardedPort - -instance Publishable PortSpec where - toPublish (PortSpec TCP fp) = "tcp:" ++ toPublish fp - toPublish (PortSpec UDP fp) = "udp:" ++ toPublish fp +instance Publishable (Proto, Bound Port) where + toPublish (TCP, fp) = "tcp:" ++ toPublish fp + toPublish (UDP, fp) = "udp:" ++ toPublish fp -- | Publish a port from the container on the host. -- @@ -334,13 +330,19 @@ instance Publishable PortSpec where publish :: Publishable p => p -> RevertableProperty publish p = containerCfg $ "--port=" ++ toPublish p +class Bindable a where + toBind :: a -> String + +instance Bindable FilePath where + toBind f = f + +instance Bindable (Bound FilePath) where + toBind v = hostSide v ++ ":" ++ containerSide v + -- | Bind mount a file or directory from the host into the container. --- --- The parameter can be a FilePath, or a colon-separated pair of --- hostpath:containerpath. -bind :: FilePath -> RevertableProperty -bind f = containerCfg $ "--bind=" ++ f +bind :: Bindable p => p -> RevertableProperty +bind p = containerCfg $ "--bind=" ++ toBind p -- | Read-only mind mount. -bindRo :: FilePath -> RevertableProperty -bindRo f = containerCfg $ "--bind-ro=" ++ f +bindRo :: Bindable p => p -> RevertableProperty +bindRo p = containerCfg $ "--bind-ro=" ++ toBind p diff --git a/src/Propellor/Types/Container.hs b/src/Propellor/Types/Container.hs new file mode 100644 index 00000000..d21bada7 --- /dev/null +++ b/src/Propellor/Types/Container.hs @@ -0,0 +1,30 @@ +{-# LANGUAGE TypeFamilies #-} + +module Propellor.Types.Container where + +-- | A value that can be bound between the host and a container. +-- +-- For example, a Bound Port is a Port on the container that is bound to +-- a Port on the host. +data Bound v = Bound + { hostSide :: v + , containerSide :: v + } + +-- | Create a Bound value, from two different values for the host and +-- container. +-- +-- For example, @Port 8080 -<- Port 80@ means that port 8080 on the host +-- is bound to port 80 from the container. +(-<-) :: (hostv ~ v, containerv ~ v) => hostv -> containerv -> Bound v +(-<-) hostv containerv = Bound hostv containerv + +-- | Flipped version of -<- with the container value first and host value +-- second. +(->-) :: (containerv ~ v, hostv ~ v) => hostv -> containerv -> Bound v +(->-) containerv hostv = Bound hostv containerv + +-- | Create a Bound value, that is the same on both the host and container. +same :: v -> Bound v +same v = Bound v v + -- cgit v1.2.3 From fc04d0d81df909904fa655372ee005138f3b6ea7 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Mon, 29 Jun 2015 16:40:01 -0400 Subject: Added --unset to delete a privdata field. --- debian/changelog | 1 + doc/usage.mdwn | 4 ++++ src/Propellor/CmdLine.hs | 2 ++ src/Propellor/PrivData.hs | 21 ++++++++++++++++----- src/Propellor/Types/CmdLine.hs | 1 + 5 files changed, 24 insertions(+), 5 deletions(-) (limited to 'src/Propellor/Types') diff --git a/debian/changelog b/debian/changelog index 079ecf48..90deb80f 100644 --- a/debian/changelog +++ b/debian/changelog @@ -3,6 +3,7 @@ propellor (2.6.0) UNRELEASED; urgency=medium * Replace String type synonym Docker.Image by a data type which allows to specify an image name and an optional tag. (API change) Thanks, Antoine Eiche. + * Added --unset to delete a privdata field. -- Joey Hess Tue, 16 Jun 2015 14:49:12 -0400 diff --git a/doc/usage.mdwn b/doc/usage.mdwn index 4030628f..1c306aa3 100644 --- a/doc/usage.mdwn +++ b/doc/usage.mdwn @@ -71,6 +71,10 @@ and configured in haskell. 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 Outputs the privdata value to stdout. diff --git a/src/Propellor/CmdLine.hs b/src/Propellor/CmdLine.hs index d29ffbb7..95a633ec 100644 --- a/src/Propellor/CmdLine.hs +++ b/src/Propellor/CmdLine.hs @@ -51,6 +51,7 @@ processCmdLine = go =<< getArgs _ -> Spin <$> mapM hostname ps <*> pure Nothing go ("--add-key":k:[]) = return $ AddKey k 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 ("--edit":f:c:[]) = withprivfield f c Edit go ("--list-fields":[]) = return ListFields @@ -94,6 +95,7 @@ defaultMain hostlist = do go _ (Continue cmdline) = go False cmdline go _ Check = return () go _ (Set field context) = setPrivData field context + go _ (Unset field context) = unsetPrivData field context go _ (Dump field context) = dumpPrivData field context go _ (Edit field context) = editPrivData field context go _ ListFields = listPrivDataFields hostlist diff --git a/src/Propellor/PrivData.hs b/src/Propellor/PrivData.hs index 71aa820d..d0426e75 100644 --- a/src/Propellor/PrivData.hs +++ b/src/Propellor/PrivData.hs @@ -6,6 +6,7 @@ module Propellor.PrivData ( withSomePrivData, addPrivData, setPrivData, + unsetPrivData, dumpPrivData, editPrivData, filterPrivData, @@ -143,6 +144,11 @@ setPrivData field context = do putStrLn "Enter private data on stdin; ctrl-D when done:" 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 field context = maybe (error "Requested privdata is not set.") putStrLn @@ -192,17 +198,22 @@ listPrivDataFields hosts = do setPrivDataTo :: PrivDataField -> Context -> PrivData -> IO () setPrivDataTo field context value = do - makePrivDataDir - m <- decryptPrivData - let m' = M.insert (field, context) (chomp value) m - gpgEncrypt privDataFile (show m') + modifyPrivData set putStrLn "Private data set." - void $ boolSystem "git" [Param "add", File privDataFile] where + set = M.insert (field, context) (chomp value) chomp s | end s == "\n" = chomp (beginning 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 = fromMaybe M.empty . readish <$> gpgDecrypt privDataFile diff --git a/src/Propellor/Types/CmdLine.hs b/src/Propellor/Types/CmdLine.hs index bd0cbdfd..96949957 100644 --- a/src/Propellor/Types/CmdLine.hs +++ b/src/Propellor/Types/CmdLine.hs @@ -10,6 +10,7 @@ data CmdLine | Spin [HostName] (Maybe HostName) | SimpleRun HostName | Set PrivDataField Context + | Unset PrivDataField Context | Dump PrivDataField Context | Edit PrivDataField Context | ListFields -- cgit v1.2.3