Skip to content

Commit

Permalink
start using amazonka for AWS interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
abailly committed May 27, 2016
1 parent 45589b1 commit f7fdc90
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 48 deletions.
37 changes: 37 additions & 0 deletions System/Build.hs
@@ -0,0 +1,37 @@
module System.Build where

import Data.ByteString (hGet, hPut, null)
import Data.Functor (void)
import Prelude hiding (null)
import System.Directory
import System.Exit
import System.IO
import System.Process

buildDocker :: IO ()
buildDocker = callProcess "docker" ["build", "-t", "ghc-centos","ghc-centos" ]

copy :: Handle -> Handle -> IO ()
copy hIn hOut = do
bs <- hGet hIn 4096
if not (null bs)
then hPut hOut bs >> copy hIn hOut
else return ()

buildExecutable :: FilePath -> String -> IO FilePath
buildExecutable srcDir targetName = do
absSrcDir <- canonicalizePath srcDir
removeFile ".cidfile"
(_,_,_,hdl) <- createProcess $ proc "docker" ["run", "--cidfile=.cidfile", "-v", absSrcDir ++ ":/build", "-w", "/build" , "ghc-centos","stack", "build","--allow-different-user", targetName ]
exitCode <- waitForProcess hdl
case exitCode of
ExitSuccess -> do
cid <- readFile ".cidfile"
(_, Just hout, _, phdl) <- createProcess $ (proc "docker" ["run", "--volumes-from=" ++ cid, "busybox","dd", "if=/build/.stack-work/install/x86_64-linux/lts-5.17/7.10.3/bin/"++ targetName ]) { std_out = CreatePipe }
withBinaryFile "lambda" WriteMode $ \ hDst -> copy hout hDst
void $ waitForProcess phdl
return "lambda"
ExitFailure code -> fail $ "failed to build correctly " ++ targetName ++ " in directory " ++ srcDir ++ ": " ++ show code

packLambda :: FilePath -> IO ()
packLambda exe = callProcess "zip" [ "lambda.zip", "run.js", exe ]
Empty file added example-src/LICENSE
Empty file.
24 changes: 24 additions & 0 deletions example-src/main.cabal
@@ -0,0 +1,24 @@
-- Initial aws-lambda-haskell.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/

name: main
version: 0.1.0.0
-- synopsis:
-- description:
-- license:
license-file: LICENSE
author: Arnaud Bailly
maintainer: arnaud.oqube@gmail.com
-- copyright:
-- category:
build-type: Simple
extra-source-files: README.md, README.md~
cabal-version: >=1.10

executable main
main-is: main.hs
-- other-modules:
-- other-extensions:
build-depends: base >=4.8 && <4.9
-- hs-source-dirs:
default-language: Haskell2010
9 changes: 9 additions & 0 deletions example-src/main.hs
@@ -0,0 +1,9 @@
module Main where

import System.IO

main :: IO ()
main = do
ln <- getLine
putStrLn $ "Got: " ++ ln

3 changes: 3 additions & 0 deletions example-src/stack.yaml
@@ -0,0 +1,3 @@
resolver: lts-5.17
packages:
- '.'
42 changes: 0 additions & 42 deletions ghc-centos/Dockerfile
Expand Up @@ -29,45 +29,3 @@ RUN mkdir -p /opt/stack/bin
RUN curl -L https://www.stackage.org/stack/linux-x86_64 | tar xz --wildcards --strip-components=1 -C /opt/stack/bin '*/stack'
ENV PATH /opt/stack/bin/:$PATH
RUN stack setup $GHC_VERSION

# ## set ghc and tools versions

# ## Download and install GHC
# RUN curl -s "https://downloads.haskell.org/~ghc/${GHC_VERSION}/ghc-${GHC_VERSION}-x86_64-unknown-linux-centos65.tar.xz" > /tmp/ghc.tar.xz \
# && tar xf /tmp/ghc.tar.xz -C /tmp \
# && cd /tmp/ghc-${GHC_VERSION} && ./configure --prefix /opt/ghc/${GHC_VERSION} \
# && make install \
# && cd /tmp && rm -rf /tmp/* && rm -rf /root/{*,.??*}

# ## set PATH for GHC
# ENV PATH /opt/ghc/${GHC_VERSION}/bin:$PATH

# ## Donwload and install Cabal/Cabal-Install
# RUN curl -s "https://www.haskell.org/cabal/release/cabal-install-${CABAL_VERSION}/cabal-install-${CABAL_VERSION}.tar.gz" > /tmp/cabal.tar.gz \
# && tar xf /tmp/cabal.tar.gz -C /tmp \
# && cd /tmp/cabal-install-${CABAL_VERSION} \
# && PREFIX=/opt/cabal/${CABAL_VERSION} ./bootstrap.sh \
# && rm -rf /opt/cabal/${CABAL_VERSION}/lib \
# && cd /tmp && rm -rf /tmp/* && rm -rf /root/{*,.??*}

# ## set PATH for cabal
# ENV PATH /opt/cabal/${CABAL_VERSION}/bin:$PATH

# ## Install Cabal to match cabal-install
# ## Install common Haskell build tools
# RUN cabal update \
# && cabal install Cabal-${CABAL_VERSION} \
# --global \
# --enable-library-profiling \
# --prefix=/opt/ghc/${GHC_VERSION} \
# --libsubdir=\$compiler/\$pkgkey \
# --datasubdir=\$compiler/\$pkgkey \
# --docdir=\$datadir/doc/ghc/html/libraries \
# --htmldir=\$datadir/doc/ghc/html/libraries \
# && cabal install alex-${ALEX_VERSION} --prefix=/opt/alex/${ALEX_VERSION} && rm -rf /opt/alex/${ALEX_VERSION}/lib \
# && cabal install happy-${HAPPY_VERSION} --prefix=/opt/happy/${HAPPY_VERSION} && rm -rf /opt/happy/${HAPPY_VERSION}/lib \
# && PATH=/opt/alex/${ALEX_VERSION}/bin/:/opt/happy/${HAPPY_VERSION}/bin/:$PATH cabal install c2hs-${C2HS_VERSION} --prefix=/opt/c2hs/${C2HS_VERSION} && rm -rf /opt/c2hs/${C2HS_VERSION}/lib \
# && rm -rf /root/{*,.??*}

## set PATH for tools

10 changes: 7 additions & 3 deletions main.cabal
@@ -1,7 +1,7 @@
-- Initial aws-lambda-haskell.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/

name: aws-lambda-haskell
name: main
version: 0.1.0.0
-- synopsis:
-- description:
Expand All @@ -19,6 +19,10 @@ executable main
main-is: main.hs
-- other-modules:
-- other-extensions:
build-depends: base >=4.8 && <4.9
build-depends: base >=4.8 && <4.9,
amazonka, amazonka-apigateway, amazonka-lambda, bytestring, directory, exceptions, lens, optparse-applicative
, process, resourcet, transformers, text
-- hs-source-dirs:
default-language: Haskell2010
default-language: Haskell2010
default-extensions: ScopedTypeVariables, OverloadedStrings
ghc-options: -Wall -Werror -threaded
28 changes: 26 additions & 2 deletions main.hs
@@ -1,6 +1,30 @@
{-# LANGUAGE FlexibleContexts #-}
module Main where

import AWS.ApiGateway
import Control.Lens
import Control.Monad.Trans.AWS
import Data.Text (pack)
import Prelude hiding (null)
import System.Build
import System.Environment
import System.IO

main :: IO ()
main = do
input <- getLine
putStrLn $ "got " ++ input
[ apiEndpoint, sourceDirectory, targetName ] <- getArgs
-- build docker container
buildDocker
-- build executable with docker
exe <- buildExecutable sourceDirectory targetName
-- pack executable with js shim in .zip file
_ <- packLambda exe
lgr <- newLogger Trace stdout
env <- newEnv Ireland Discover <&> envLogger .~ lgr
createApiEndpoint env apiEndpoint >>= print

where

createApiEndpoint env api = runResourceT (runAWST env $ createApi $ pack api)


2 changes: 1 addition & 1 deletion stack.yaml
@@ -1,3 +1,3 @@
resolver: ghc-7.10.3
resolver: lts-5.17
packages:
- '.'

0 comments on commit f7fdc90

Please sign in to comment.