module Debian.Debianize.Goodies
( tightDependencyFixup
, doServer
, doWebsite
, doBackups
, doExecutable
, describe
, watchAtom
, oldClckwrksSiteFlags
, oldClckwrksServerFlags
, siteAtoms
, serverAtoms
, backupAtoms
, execAtoms
) where
import Data.Char (isSpace)
import Data.Lens.Lazy (modL, access)
import Data.List as List (map, intersperse, intercalate, dropWhileEnd)
import Data.Map as Map (insertWith)
import Data.Maybe (fromMaybe)
import Data.Monoid ((<>))
import Data.Set as Set (insert, union, singleton)
import Data.Text as Text (Text, pack, unlines)
import Debian.Debianize.Monad (Atoms, DebT, execDebM)
import Debian.Debianize.Prelude (stripWith, (%=), (+=), (++=), (+++=))
import qualified Debian.Debianize.Types as T
import qualified Debian.Debianize.Types.Atoms as T
import Debian.Orphans ()
import Debian.Pretty (ppDisplay, ppDisplay')
import Debian.Policy (apacheLogDirectory, apacheErrorLog, apacheAccessLog, databaseDirectory, serverAppLog, serverAccessLog)
import Debian.Relation (BinPkgName(BinPkgName), Relation(Rel))
import Distribution.Package (PackageName(PackageName))
import Distribution.PackageDescription as Cabal (PackageDescription(package, synopsis, description))
import Prelude hiding (writeFile, init, unlines, log, map)
import System.FilePath ((</>))
showCommand :: String -> [String] -> String
showCommand cmd args =
unwords (map translate (cmd : args))
translate :: String -> String
translate str =
'"' : foldr escape "\"" str
where
escape '"' = showString "\\\""
escape c = showChar c
tightDependencyFixup :: Monad m => [(BinPkgName, BinPkgName)] -> BinPkgName -> DebT m ()
tightDependencyFixup [] _ = return ()
tightDependencyFixup pairs p =
T.rulesFragments +=
(Text.unlines $
([ "binary-fixup/" <> name <> "::"
, "\techo -n 'haskell:Depends=' >> debian/" <> name <> ".substvars" ] ++
intersperse ("\techo -n ', ' >> debian/" <> name <> ".substvars") (List.map equals pairs) ++
[ "\techo '' >> debian/" <> name <> ".substvars"
, "\techo -n 'haskell:Conflicts=' >> debian/" <> name <> ".substvars" ] ++
intersperse ("\techo -n ', ' >> debian/" <> name <> ".substvars") (List.map newer pairs) ++
[ "\techo '' >> debian/" <> name <> ".substvars" ]))
where
equals (installed, dependent) = "\tdpkg-query -W -f='" <> display' dependent <> " (=$${Version})' " <> display' installed <> " >> debian/" <> name <> ".substvars"
newer (installed, dependent) = "\tdpkg-query -W -f='" <> display' dependent <> " (>>$${Version})' " <> display' installed <> " >> debian/" <> name <> ".substvars"
name = display' p
display' = ppDisplay'
doExecutable :: Monad m => BinPkgName -> T.InstallFile -> DebT m ()
doExecutable p f = T.executable ++= (p, f)
doServer :: Monad m => BinPkgName -> T.Server -> DebT m ()
doServer p s = T.serverInfo ++= (p, s)
doWebsite :: Monad m => BinPkgName -> T.Site -> DebT m ()
doWebsite p w = T.website ++= (p, w)
doBackups :: Monad m => BinPkgName -> String -> DebT m ()
doBackups bin s =
do T.backups ++= (bin, s)
T.depends bin %= (++ [[Rel (BinPkgName "anacron") Nothing Nothing]])
describe :: Monad m => DebT m Text
describe =
do Just p <- access T.packageDescription
return $
debianDescriptionBase p
debianDescriptionBase :: PackageDescription -> Text
debianDescriptionBase p =
pack $ List.intercalate "\n " $ (synop' : desc)
where
synop' = if null synop && length desc /= 1
then "WARNING: No synopsis available for package " ++ ppDisplay (package p)
else synop
synop :: String
synop = intercalate " " $ map (dropWhileEnd isSpace) $ lines $ Cabal.synopsis p
desc :: [String]
desc = List.map addDot . stripWith null $ map (dropWhileEnd isSpace) $ lines $ Cabal.description p
addDot line = if null line then "." else line
oldClckwrksSiteFlags :: T.Site -> [String]
oldClckwrksSiteFlags x =
[
"--base-uri", "http://" ++ T.domain x ++ "/"
, "--http-port", show T.port]
oldClckwrksServerFlags :: T.Server -> [String]
oldClckwrksServerFlags x =
[
"--base-uri", "http://" ++ T.hostname x ++ ":" ++ show (T.port x) ++ "/"
, "--http-port", show T.port]
watchAtom :: PackageName -> Text
watchAtom (PackageName pkgname) =
pack $ "version=3\nhttp://hackage.haskell.org/package/" ++ pkgname ++ "/distro-monitor .*-([0-9\\.]+)\\.(?:zip|tgz|tbz|txz|(?:tar\\.(?:gz|bz2|xz)))\n"
siteAtoms :: BinPkgName -> T.Site -> Atoms -> Atoms
siteAtoms b site =
execDebM
(do T.installDir b "/etc/apache2/sites-available"
T.link b ("/etc/apache2/sites-available/" ++ T.domain site) ("/etc/apache2/sites-enabled/" ++ T.domain site)
T.file b ("/etc/apache2/sites-available" </> T.domain site) apacheConfig
T.installDir b (apacheLogDirectory b)
T.logrotateStanza +++= (b, singleton
(Text.unlines $ [ pack (apacheAccessLog b) <> " {"
, " copytruncate"
, " weekly"
, " rotate 5"
, " compress"
, " missingok"
, "}"]))
T.logrotateStanza +++= (b, singleton
(Text.unlines $ [ pack (apacheErrorLog b) <> " {"
, " copytruncate"
, " weekly"
, " rotate 5"
, " compress"
, " missingok"
, "}" ]))) .
serverAtoms b (T.server site) True
where
apacheConfig =
Text.unlines $
[ "<VirtualHost *:80>"
, " ServerAdmin " <> pack (T.serverAdmin site)
, " ServerName www." <> pack (T.domain site)
, " ServerAlias " <> pack (T.domain site)
, ""
, " ErrorLog " <> pack (apacheErrorLog b)
, " CustomLog " <> pack (apacheAccessLog b) <> " combined"
, ""
, " ProxyRequests Off"
, " AllowEncodedSlashes NoDecode"
, ""
, " <Proxy *>"
, " AddDefaultCharset off"
, " Order deny,allow"
, " #Allow from .example.com"
, " Deny from all"
, " #Allow from all"
, " </Proxy>"
, ""
, " <Proxy http://127.0.0.1:" <> port' <> "/*>"
, " AddDefaultCharset off"
, " Order deny,allow"
, " #Allow from .example.com"
, " #Deny from all"
, " Allow from all"
, " </Proxy>"
, ""
, " SetEnv proxy-sendcl 1"
, ""
, " ProxyPass / http://127.0.0.1:" <> port' <> "/ nocanon"
, " ProxyPassReverse / http://127.0.0.1:" <> port' <> "/"
, "</VirtualHost>" ]
port' = pack (show (T.port (T.server site)))
serverAtoms :: BinPkgName -> T.Server -> Bool -> Atoms -> Atoms
serverAtoms b server' isSite =
modL T.postInst (insertWith (\ old new -> if old /= new then error ("serverAtoms: " ++ show old ++ " -> " ++ show new) else old) b debianPostinst) .
modL T.installInit (Map.insertWith (\ old new -> if old /= new then error ("serverAtoms: " ++ show old ++ " -> " ++ show new) else old) b debianInit) .
serverLogrotate' b .
execAtoms b exec
where
exec = T.installFile server'
debianInit =
Text.unlines $
[ "#! /bin/sh -e"
, ""
, ". /lib/lsb/init-functions"
, "test -f /etc/default/" <> pack (T.destName exec) <> " && . /etc/default/" <> pack (T.destName exec)
, ""
, "case \"$1\" in"
, " start)"
, " test -x /usr/bin/" <> pack (T.destName exec) <> " || exit 0"
, " log_begin_msg \"Starting " <> pack (T.destName exec) <> "...\""
, " mkdir -p " <> pack (databaseDirectory b)
, " " <> startCommand
, " log_end_msg $?"
, " ;;"
, " stop)"
, " log_begin_msg \"Stopping " <> pack (T.destName exec) <> "...\""
, " " <> stopCommand
, " log_end_msg $?"
, " ;;"
, " *)"
, " log_success_msg \"Usage: ${0} {start|stop}\""
, " exit 1"
, "esac"
, ""
, "exit 0" ]
startCommand = pack $ showCommand "start-stop-daemon" (startOptions ++ commonOptions ++ ["--"] ++ T.serverFlags server')
stopCommand = pack $ showCommand "start-stop-daemon" (stopOptions ++ commonOptions)
commonOptions = ["--pidfile", "/var/run/" ++ T.destName exec]
startOptions = ["--start", "-b", "--make-pidfile", "-d", databaseDirectory b, "--exec", "/usr/bin" </> T.destName exec]
stopOptions = ["--stop", "--oknodo"] ++ if T.retry server' /= "" then ["--retry=" ++ T.retry server' ] else []
debianPostinst =
Text.unlines $
([ "#!/bin/sh"
, ""
, "case \"$1\" in"
, " configure)" ] ++
(if isSite
then [ " # Apache won't start if this directory doesn't exist"
, " mkdir -p " <> pack (apacheLogDirectory b)
, " # Restart apache so it sees the new file in /etc/apache2/sites-enabled"
, " /usr/sbin/a2enmod proxy"
, " /usr/sbin/a2enmod proxy_http"
, " service apache2 restart" ]
else []) ++
[
" ;;"
, "esac"
, ""
, "#DEBHELPER#"
, ""
, "exit 0" ])
serverLogrotate' :: BinPkgName -> Atoms -> Atoms
serverLogrotate' b =
modL T.logrotateStanza (insertWith Set.union b (singleton (Text.unlines $ [ pack (serverAccessLog b) <> " {"
, " weekly"
, " rotate 5"
, " compress"
, " missingok"
, "}" ]))) .
modL T.logrotateStanza (insertWith Set.union b (singleton (Text.unlines $ [ pack (serverAppLog b) <> " {"
, " weekly"
, " rotate 5"
, " compress"
, " missingok"
, "}" ])))
backupAtoms :: BinPkgName -> String -> Atoms -> Atoms
backupAtoms b name =
modL T.postInst (insertWith (\ old new -> if old /= new then error $ "backupAtoms: " ++ show old ++ " -> " ++ show new else old) b
(Text.unlines $
[ "#!/bin/sh"
, ""
, "case \"$1\" in"
, " configure)"
, " " <> pack ("/etc/cron.hourly" </> name) <> " --initialize"
, " ;;"
, "esac" ])) .
execAtoms b (T.InstallFile { T.execName = name
, T.destName = name
, T.sourceDir = Nothing
, T.destDir = Just "/etc/cron.hourly" })
execAtoms :: BinPkgName -> T.InstallFile -> Atoms -> Atoms
execAtoms b ifile r =
modL T.rulesFragments (Set.insert (pack ("build" </> ppDisplay b ++ ":: build-ghc-stamp\n"))) .
fileAtoms b ifile $
r
fileAtoms :: BinPkgName -> T.InstallFile -> Atoms -> Atoms
fileAtoms b installFile' r =
fileAtoms' b (T.sourceDir installFile') (T.execName installFile') (T.destDir installFile') (T.destName installFile') r
fileAtoms' :: BinPkgName -> Maybe FilePath -> String -> Maybe FilePath -> String -> Atoms -> Atoms
fileAtoms' b sourceDir' execName' destDir' destName' r =
case (sourceDir', execName' == destName') of
(Nothing, True) -> execDebM (T.installCabalExec b execName' d) r
(Just s, True) -> execDebM (T.install b (s </> execName') d) r
(Nothing, False) -> execDebM (T.installCabalExecTo b execName' (d </> destName')) r
(Just s, False) -> execDebM (T.installTo b (s </> execName') (d </> destName')) r
where
d = fromMaybe "usr/bin" destDir'