From 1c381c5246b2836ca0f535b9ac65eddcaa000024 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 3 Apr 2014 21:22:37 -0400 Subject: library shimming for docker (untested) --- Propellor/Property/Docker.hs | 38 +++++++++++++++------------- Propellor/Property/Docker/Shim.hs | 52 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 17 deletions(-) create mode 100644 Propellor/Property/Docker/Shim.hs (limited to 'Propellor/Property') diff --git a/Propellor/Property/Docker.hs b/Propellor/Property/Docker.hs index 5f819f26..88adb06d 100644 --- a/Propellor/Property/Docker.hs +++ b/Propellor/Property/Docker.hs @@ -4,13 +4,6 @@ -- -- The existance of a docker container is just another Property of a system, -- which propellor can set up. See config.hs for an example. --- --- Note that propellor provisions a container by running itself, inside the --- container. Currently, to avoid the overhead of building propellor --- inside the container, the binary from outside is reused inside. --- So, the libraries that propellor is linked against need to be available --- in the container with compatable versions. This can cause a problem --- if eg, mixing Debian stable and unstable. module Propellor.Property.Docker where @@ -18,6 +11,7 @@ import Propellor import Propellor.SimpleSh import qualified Propellor.Property.File as File import qualified Propellor.Property.Apt as Apt +import qualified Propellor.Property.Docker.Shim as Shim import Utility.SafeCommand import Utility.Path @@ -256,15 +250,14 @@ runningContainer cid@(ContainerId hn cn) image containerprops = containerDesc ci , name (fromContainerId cid) ] - chaincmd = [localdir "propellor", "--docker", fromContainerId cid] - go img = do clearProvisionedFlag cid createDirectoryIfMissing True (takeDirectory $ identFile cid) + shim <- Shim.setup "./propellor" (localdir shimdir cid) writeFile (identFile cid) (show ident) ensureProperty $ boolProperty "run" $ runContainer img (runps ++ ["-i", "-d", "-t"]) - chaincmd + [shim, "--docker", fromContainerId cid] -- | Called when propellor is running inside a docker container. -- The string should be the container's ContainerId. @@ -290,8 +283,9 @@ chain s = case toContainerId s of writeFile propellorIdent . show =<< readIdentFile cid -- Run boot provisioning before starting simpleSh, -- to avoid ever provisioning twice at the same time. - whenM (checkProvisionedFlag cid) $ - unlessM (boolSystem "./propellor" [Param "--continue", Param $ show $ Chain $ fromContainerId cid]) $ + whenM (checkProvisionedFlag cid) $ do + let shim = Shim.file "./propellor" (localdir shimdir cid) + unlessM (boolSystem shim [Param "--continue", Param $ show $ Chain $ fromContainerId cid]) $ warningMessage "Boot provision failed!" void $ async $ simpleSh $ namedPipe cid forever $ do @@ -310,7 +304,8 @@ chain s = case toContainerId s of -- 1 minute. provisionContainer :: ContainerId -> Property provisionContainer cid = containerDesc cid $ Property "provision" $ do - r <- simpleShClientRetry 60 (namedPipe cid) "./propellor" params (go Nothing) + let shim = Shim.file "./propellor" (localdir shimdir cid) + r <- simpleShClientRetry 60 (namedPipe cid) shim params (go Nothing) when (r /= FailedChange) $ setProvisionedFlag cid return r @@ -342,11 +337,17 @@ stopContainer cid = boolSystem dockercmd [Param "stop", Param $ fromContainerId stoppedContainer :: ContainerId -> Property stoppedContainer cid = containerDesc cid $ Property desc $ ifM (elem cid <$> listContainers RunningContainers) - ( ensureProperty $ boolProperty desc $ stopContainer cid + ( cleanup `after` ensureProperty + (boolProperty desc $ stopContainer cid) , return NoChange ) where desc = "stopped" + cleanup = do + nukeFile $ namedPipe cid + nukeFile $ identFile cid + removeDirectoryRecursive $ shimdir cid + clearProvisionedFlag cid removeContainer :: ContainerId -> IO Bool removeContainer cid = catchBoolIO $ @@ -396,10 +397,10 @@ propellorIdent = "/.propellor-ident" -- | Named pipe used for communication with the container. namedPipe :: ContainerId -> FilePath -namedPipe cid = "docker/" ++ fromContainerId cid +namedPipe cid = "docker" fromContainerId cid provisionedFlag :: ContainerId -> FilePath -provisionedFlag cid = "docker/" ++ fromContainerId cid ++ ".provisioned" +provisionedFlag cid = "docker" fromContainerId cid ++ ".provisioned" clearProvisionedFlag :: ContainerId -> IO () clearProvisionedFlag = nukeFile . provisionedFlag @@ -412,8 +413,11 @@ setProvisionedFlag cid = do checkProvisionedFlag :: ContainerId -> IO Bool checkProvisionedFlag = doesFileExist . provisionedFlag +shimdir :: ContainerId -> FilePath +shimdir cid = "docker" fromContainerId cid ++ ".shim" + identFile :: ContainerId -> FilePath -identFile cid = "docker/" ++ fromContainerId cid ++ ".ident" +identFile cid = "docker" fromContainerId cid ++ ".ident" readIdentFile :: ContainerId -> IO ContainerIdent readIdentFile cid = fromMaybe (error "bad ident in identFile") diff --git a/Propellor/Property/Docker/Shim.hs b/Propellor/Property/Docker/Shim.hs new file mode 100644 index 00000000..0e0f55e7 --- /dev/null +++ b/Propellor/Property/Docker/Shim.hs @@ -0,0 +1,52 @@ +-- | Support for running propellor, as built outside a docker container, +-- inside the container. +-- +-- Note: This is currently Debian specific, due to glibcLibs. + +module Propellor.Property.Docker.Shim (setup, file) where + +import Propellor +import Utility.LinuxMkLibs +import Utility.SafeCommand +import Utility.Path +import Utility.FileMode + +import Data.List +import System.Posix.Files + +-- | Sets up a shimmed version of the program, in a directory, and +-- returns its path. +setup :: FilePath -> FilePath -> IO FilePath +setup propellorbin dest = do + createDirectoryIfMissing True dest + + libs <- parseLdd <$> readProcess "ldd" [propellorbin] + glibclibs <- glibcLibs + let libs' = nub $ libs ++ glibclibs + libdirs <- map (dest ++) . nub . catMaybes + <$> mapM (installLib installFile dest) libs' + + let linker = (dest ++) $ + fromMaybe (error "cannot find ld-linux linker") $ + headMaybe $ filter ("ld-linux" `isInfixOf`) libs' + let linkerparams = ["--library-path", intercalate ":" libdirs ] + let shim = file propellorbin dest + writeFile shim $ unlines + [ "#!/bin/sh" + , "exec " ++ unwords (map shellEscape $ linker : linkerparams) ++ + " " ++ shellEscape propellorbin ++ " \"$@\"" + ] + modifyFileMode shim (addModes executeModes) + return shim + +file :: FilePath -> FilePath -> FilePath +file propellorbin dest = dest propellorbin + +installFile :: FilePath -> FilePath -> IO () +installFile top f = do + createDirectoryIfMissing True destdir + createLink f dest `catchIO` (const copy) + where + copy = void $ boolSystem "cp" [Param "-a", Param f, Param dest] + destdir = inTop top $ parentDir f + dest = inTop top f -- cgit v1.2.3 From 5499b2a612d9379fc8a3ed3ea2e70165e0bdefad Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 3 Apr 2014 23:16:34 -0400 Subject: propellor spin --- Propellor/Property/Docker/Shim.hs | 1 + 1 file changed, 1 insertion(+) (limited to 'Propellor/Property') diff --git a/Propellor/Property/Docker/Shim.hs b/Propellor/Property/Docker/Shim.hs index 0e0f55e7..402f1c12 100644 --- a/Propellor/Property/Docker/Shim.hs +++ b/Propellor/Property/Docker/Shim.hs @@ -45,6 +45,7 @@ file propellorbin dest = dest propellorbin installFile :: FilePath -> FilePath -> IO () installFile top f = do createDirectoryIfMissing True destdir + nukeFile dest createLink f dest `catchIO` (const copy) where copy = void $ boolSystem "cp" [Param "-a", Param f, Param dest] -- cgit v1.2.3 From b254cbbab194c948b270cc6456fc6020ed0c0f49 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 3 Apr 2014 23:30:23 -0400 Subject: propellor spin --- Propellor/Property/Docker.hs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Propellor/Property') diff --git a/Propellor/Property/Docker.hs b/Propellor/Property/Docker.hs index 88adb06d..888e76c6 100644 --- a/Propellor/Property/Docker.hs +++ b/Propellor/Property/Docker.hs @@ -253,7 +253,7 @@ runningContainer cid@(ContainerId hn cn) image containerprops = containerDesc ci go img = do clearProvisionedFlag cid createDirectoryIfMissing True (takeDirectory $ identFile cid) - shim <- Shim.setup "./propellor" (localdir shimdir cid) + shim <- Shim.setup (localdir "propellor") (localdir shimdir cid) writeFile (identFile cid) (show ident) ensureProperty $ boolProperty "run" $ runContainer img (runps ++ ["-i", "-d", "-t"]) @@ -284,7 +284,7 @@ chain s = case toContainerId s of -- Run boot provisioning before starting simpleSh, -- to avoid ever provisioning twice at the same time. whenM (checkProvisionedFlag cid) $ do - let shim = Shim.file "./propellor" (localdir shimdir cid) + let shim = Shim.file (localdir "propellor") (localdir shimdir cid) unlessM (boolSystem shim [Param "--continue", Param $ show $ Chain $ fromContainerId cid]) $ warningMessage "Boot provision failed!" void $ async $ simpleSh $ namedPipe cid @@ -304,7 +304,7 @@ chain s = case toContainerId s of -- 1 minute. provisionContainer :: ContainerId -> Property provisionContainer cid = containerDesc cid $ Property "provision" $ do - let shim = Shim.file "./propellor" (localdir shimdir cid) + let shim = Shim.file (localdir "propellor") (localdir shimdir cid) r <- simpleShClientRetry 60 (namedPipe cid) shim params (go Nothing) when (r /= FailedChange) $ setProvisionedFlag cid -- cgit v1.2.3 From ff49cf562c3761993a22ea6fe6904325a1e53d33 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 3 Apr 2014 23:32:00 -0400 Subject: propellor spin --- Propellor/Property/Hostname.hs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'Propellor/Property') diff --git a/Propellor/Property/Hostname.hs b/Propellor/Property/Hostname.hs index 25f0e1b2..f5aa5da7 100644 --- a/Propellor/Property/Hostname.hs +++ b/Propellor/Property/Hostname.hs @@ -3,6 +3,9 @@ module Propellor.Property.Hostname where import Propellor import qualified Propellor.Property.File as File +-- | Sets the hostname. Should be provided with a FQDN, and will configure +-- both /etc/hostname (with the base hostname) and /etc/hosts (with the +-- full hostname). Also sets the current hostname. set :: HostName -> Property set hostname = "/etc/hostname" `File.hasContent` [hostname] `onChange` cmdProperty "hostname" [hostname] -- cgit v1.2.3 From fbc57d684509180f518c84469c45e2d85bb20708 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 3 Apr 2014 23:35:36 -0400 Subject: propellor spin --- Propellor/Property/Docker/Shim.hs | 2 +- Propellor/Property/Hostname.hs | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'Propellor/Property') diff --git a/Propellor/Property/Docker/Shim.hs b/Propellor/Property/Docker/Shim.hs index 402f1c12..a210e162 100644 --- a/Propellor/Property/Docker/Shim.hs +++ b/Propellor/Property/Docker/Shim.hs @@ -40,7 +40,7 @@ setup propellorbin dest = do return shim file :: FilePath -> FilePath -> FilePath -file propellorbin dest = dest propellorbin +file propellorbin dest = dest takeFileName propellorbin installFile :: FilePath -> FilePath -> IO () installFile top f = do diff --git a/Propellor/Property/Hostname.hs b/Propellor/Property/Hostname.hs index f5aa5da7..a2e3c7c6 100644 --- a/Propellor/Property/Hostname.hs +++ b/Propellor/Property/Hostname.hs @@ -10,3 +10,5 @@ set :: HostName -> Property set hostname = "/etc/hostname" `File.hasContent` [hostname] `onChange` cmdProperty "hostname" [hostname] `describe` ("hostname " ++ hostname) + where + (host, domain) = separate (== '.') hostname -- cgit v1.2.3 From 7932c5abec925bfd0240b62290e00977141419e7 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 3 Apr 2014 23:57:36 -0400 Subject: propellor spin --- Propellor/Property/Hostname.hs | 30 ++++++++++++++++++++++++------ config-joeyh.hs | 2 +- 2 files changed, 25 insertions(+), 7 deletions(-) (limited to 'Propellor/Property') diff --git a/Propellor/Property/Hostname.hs b/Propellor/Property/Hostname.hs index a2e3c7c6..44a3b111 100644 --- a/Propellor/Property/Hostname.hs +++ b/Propellor/Property/Hostname.hs @@ -3,12 +3,30 @@ module Propellor.Property.Hostname where import Propellor import qualified Propellor.Property.File as File --- | Sets the hostname. Should be provided with a FQDN, and will configure --- both /etc/hostname (with the base hostname) and /etc/hosts (with the --- full hostname). Also sets the current hostname. +-- | Sets the hostname. Configures both /etc/hostname and the current +-- hostname. +-- +-- When provided with a FQDN, also configures /etc/hosts, +-- with an entry for 127.0.1.1, which is standard at least on Debian +-- to set the FDQN (127.0.0.1 is localhost). set :: HostName -> Property -set hostname = "/etc/hostname" `File.hasContent` [hostname] - `onChange` cmdProperty "hostname" [hostname] - `describe` ("hostname " ++ hostname) +set hostname = propertyList desc go + `onChange` cmdProperty "hostname" [host] + `describe` desc where + desc = "hostname " ++ hostname (host, domain) = separate (== '.') hostname + + go = catMaybes + [ Just $ "/etc/hostname" `File.hasContent` [host] + , if null domain + then Nothing + else Just $ File.fileProperty desc + addhostline "/etc/hosts" + ] + + hostip = "127.0.1.1" + hostline = hostip ++ "\t" ++ hostname ++ " " ++ host + + addhostline ls = hostline : filter (not . hashostip) ls + hashostip l = headMaybe (words l) == Just hostip diff --git a/config-joeyh.hs b/config-joeyh.hs index aa0669c7..6f8b75c2 100644 --- a/config-joeyh.hs +++ b/config-joeyh.hs @@ -10,7 +10,7 @@ import qualified Propellor.Property.Cron as Cron import qualified Propellor.Property.Sudo as Sudo import qualified Propellor.Property.User as User import qualified Propellor.Property.Hostname as Hostname -import qualified Propellor.Property.Reboot as Reboot +--import qualified Propellor.Property.Reboot as Reboot import qualified Propellor.Property.Tor as Tor import qualified Propellor.Property.Docker as Docker import qualified Propellor.Property.SiteSpecific.GitHome as GitHome -- cgit v1.2.3 From d19171ed53884c0422b27ccc6aac4960c96ccc7d Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 3 Apr 2014 23:59:32 -0400 Subject: propellor spin --- Propellor/Property/Hostname.hs | 1 - 1 file changed, 1 deletion(-) (limited to 'Propellor/Property') diff --git a/Propellor/Property/Hostname.hs b/Propellor/Property/Hostname.hs index 44a3b111..f710df70 100644 --- a/Propellor/Property/Hostname.hs +++ b/Propellor/Property/Hostname.hs @@ -12,7 +12,6 @@ import qualified Propellor.Property.File as File set :: HostName -> Property set hostname = propertyList desc go `onChange` cmdProperty "hostname" [host] - `describe` desc where desc = "hostname " ++ hostname (host, domain) = separate (== '.') hostname -- cgit v1.2.3 From cf3fc9b8e9a9e93c6784f78e0ba48f518d3003f7 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 4 Apr 2014 00:08:30 -0400 Subject: fix file mode bug --- Propellor/Property/File.hs | 12 ++++++++++-- debian/changelog | 9 +++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) (limited to 'Propellor/Property') diff --git a/Propellor/Property/File.hs b/Propellor/Property/File.hs index af4f554f..80c69d9b 100644 --- a/Propellor/Property/File.hs +++ b/Propellor/Property/File.hs @@ -2,6 +2,8 @@ module Propellor.Property.File where import Propellor +import System.Posix.Files + type Line = String -- | Replaces all the content of a file. @@ -32,13 +34,19 @@ fileProperty :: Desc -> ([Line] -> [Line]) -> FilePath -> Property fileProperty desc a f = Property desc $ go =<< doesFileExist f where go True = do - ls <- lines <$> catchDefaultIO [] (readFile f) + ls <- lines <$> readFile f let ls' = a ls if ls' == ls then noChange - else makeChange $ viaTmp writeFile f (unlines ls') + else makeChange $ viaTmp updatefile f (unlines ls') go False = makeChange $ writeFile f (unlines $ a []) + -- viaTmp makes the temp file mode 600. + -- Replicate the original file mode before moving it into place. + updatefile f' content = do + writeFile f' content + getFileStatus f >>= setFileMode f' . fileMode + -- | Ensures a directory exists. dirExists :: FilePath -> Property dirExists d = check (not <$> doesDirectoryExist d) $ Property (d ++ " exists") $ diff --git a/debian/changelog b/debian/changelog index 09d8492d..a126e8fb 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,12 @@ +propellor (0.2.2) UNRELEASED; urgency=medium + + * Now supports provisioning docker containers with architecture/libraries + that do not match the outside host. + * Fixed a bug that caused file modes to be set to 600 when propellor + modified the file. + + -- Joey Hess Fri, 04 Apr 2014 00:06:26 -0400 + propellor (0.2.1) unstable; urgency=medium * First release with Debian package. -- cgit v1.2.3 From 3aff9915532ae7256deb982e9c8d05e174249a32 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 4 Apr 2014 00:11:24 -0400 Subject: propellor spin --- Propellor/Property/Hostname.hs | 2 +- config-joeyh.hs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'Propellor/Property') diff --git a/Propellor/Property/Hostname.hs b/Propellor/Property/Hostname.hs index f710df70..26635374 100644 --- a/Propellor/Property/Hostname.hs +++ b/Propellor/Property/Hostname.hs @@ -10,7 +10,7 @@ import qualified Propellor.Property.File as File -- with an entry for 127.0.1.1, which is standard at least on Debian -- to set the FDQN (127.0.0.1 is localhost). set :: HostName -> Property -set hostname = propertyList desc go +set hostname = combineProperties desc go `onChange` cmdProperty "hostname" [host] where desc = "hostname " ++ hostname diff --git a/config-joeyh.hs b/config-joeyh.hs index 6f8b75c2..e4a9dcac 100644 --- a/config-joeyh.hs +++ b/config-joeyh.hs @@ -45,7 +45,7 @@ host hostname@"orca.kitenet.net" = standardSystem Unstable $ props & Docker.configured & Apt.buildDep ["git-annex"] & Docker.docked container hostname "amd64-git-annex-builder" - ! Docker.docked container hostname "i386-git-annex-builder" + & Docker.docked container hostname "i386-git-annex-builder" & Docker.garbageCollected -- My laptop host _hostname@"darkstar.kitenet.net" = Just $ props -- cgit v1.2.3 From ba8a259f24e15c1e1005bed628ceed8e374e963a Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 4 Apr 2014 00:29:19 -0400 Subject: propellor spin --- Propellor/Property/Docker/Shim.hs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'Propellor/Property') diff --git a/Propellor/Property/Docker/Shim.hs b/Propellor/Property/Docker/Shim.hs index a210e162..7d4f56f6 100644 --- a/Propellor/Property/Docker/Shim.hs +++ b/Propellor/Property/Docker/Shim.hs @@ -29,10 +29,15 @@ setup propellorbin dest = do let linker = (dest ++) $ fromMaybe (error "cannot find ld-linux linker") $ headMaybe $ filter ("ld-linux" `isInfixOf`) libs' + let gconvdir = (dest ++) $ parentDir $ + fromMaybe (error "cannot find gconv directory") $ + headMaybe $ filter ("/gconv/" `isInfixOf`) glibclibs let linkerparams = ["--library-path", intercalate ":" libdirs ] let shim = file propellorbin dest writeFile shim $ unlines [ "#!/bin/sh" + , "set GCONV_PATH=" ++ shellEscape gconvdir + , "export GCONV_PATH" , "exec " ++ unwords (map shellEscape $ linker : linkerparams) ++ " " ++ shellEscape propellorbin ++ " \"$@\"" ] -- cgit v1.2.3 From eb8dcfd99513131f95e067d0480164684793b1e9 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 4 Apr 2014 00:44:29 -0400 Subject: propellor spin --- Propellor/Property/Docker/Shim.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Propellor/Property') diff --git a/Propellor/Property/Docker/Shim.hs b/Propellor/Property/Docker/Shim.hs index 7d4f56f6..01c2b22f 100644 --- a/Propellor/Property/Docker/Shim.hs +++ b/Propellor/Property/Docker/Shim.hs @@ -36,7 +36,7 @@ setup propellorbin dest = do let shim = file propellorbin dest writeFile shim $ unlines [ "#!/bin/sh" - , "set GCONV_PATH=" ++ shellEscape gconvdir + , "GCONV_PATH=" ++ shellEscape gconvdir , "export GCONV_PATH" , "exec " ++ unwords (map shellEscape $ linker : linkerparams) ++ " " ++ shellEscape propellorbin ++ " \"$@\"" -- cgit v1.2.3