Skip to content

Instantly share code, notes, and snippets.

@benkolera
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save benkolera/40ace9f83efbc2207ede to your computer and use it in GitHub Desktop.

Select an option

Save benkolera/40ace9f83efbc2207ede to your computer and use it in GitHub Desktop.
Installs the current package into a sandbox with its deps and packages /bin and /share as in rpm using the ruby based fpm tool. Deploys to /opt/haskell/apps/<appname>/{bin,share}
module Main where
import Control.Applicative ((<$>))
import Control.Monad (mfilter)
import Data.Char (toLower,isSpace)
import Data.List (dropWhileEnd,intercalate)
import Data.Time.Clock.POSIX
import Data.Version (versionBranch)
import Distribution.PackageDescription
import Distribution.PackageDescription.Parse
import Distribution.Package
import Distribution.Verbosity
import System.Exit
import System.Process
appName = "iseek-Nagios-Configurator"
revision = "1.el6.iseek"
main = do
td <- tmpDir
e <- show . round <$> getPOSIXTime
cd <- cabalDesc
makeSandbox td
cabalInstall td
buildRpm td cd e >>= putStrLn
makeSandbox td = do
let sandboxDir = appDir td
putStrLn ("Making sandbox: " ++ sandboxDir)
execCabal td ["sandbox","init","--sandbox",sandboxDir]
cabalInstall td = do
putStrLn "Installing into sandbox (includes all deps. This will take a while!"
execCabal td ["install"]
execCabal td = exec "cabal" . (["--sandbox-config",sandboxConf td] ++)
buildRpm td cd e = exec "fpm" fpmArgs
where
fpmArgs =
[ "-n" , appName
, "-t" , "rpm"
, "-s" , "dir"
, "-a" , "native"
, "-C" , buildRoot td
, "--version" , versionText cd
, "--rpm-changelog", "Changelog.fpm"
, "--license", "Non-distributable"
, "--description" , makeDescription cd
, "--iteration" , revision
, "--epoch" , e
, "share"
, "bin"
]
sandboxConf = (++ "/cabal.sandbox.config")
buildRoot = (++ "/BUILDROOT")
appDir = (++ ("/opt/haskell/" ++ appName)) . buildRoot
versionText = intercalate "." . fmap show . versionBranch . pkgVersion . package
makeDescription cd = (maybe "" (++ "\n") s) ++ (description cd)
where
s = mfilter (not . null) . Just . synopsis $ cd
cabalDesc = fmap packageDescription
. readPackageDescription normal
$ (map toLower appName) ++ ".cabal"
tmpDir = exec "mktemp" ["-d","-t","build" ++ "-" ++ appName ++ "-XXXXX"]
exec :: String -> [String] -> IO String
exec cmd args = do
fmap checkExitCode (readProcessWithExitCode cmd args "")
where
checkExitCode (ExitSuccess,out,err) = dropWhileEnd isSpace out
checkExitCode ((ExitFailure code),out,err) = error . concat $
[ cmd ++ " " ++ (intercalate " " args)
, "Output: "
, out
, "\n"
, err
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment