Skip to content
Permalink
Browse files

WIP - add clone and deploy with nixops

  • Loading branch information...
shmish111 committed Jun 11, 2019
1 parent ced56f1 commit fd5600054ebaf8f13acf7ee8ef65a19b31833eab
@@ -1,9 +1,25 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where

import Lib (app)
import Control.Concurrent (forkIO)
import Control.Concurrent.Chan (newChan)
import Deploy.Server (app)
import Deploy.Worker (runWorker)
import GHC.Generics (Generic)
import Network.Wai.Handler.Warp (run)
import Options.Generic (ParseRecord, getRecord)
import Servant.GitHub.Webhook (gitHubKey)

data Options = Options { port :: Int, configDir :: FilePath }
deriving (Generic, Show, ParseRecord)

main :: IO ()
main = run 8080 $ app (gitHubKey . pure $ "key")
main = do
Options { port, configDir } <- getRecord "Plutus CD Server"
chan <- newChan
forkIO $ runWorker chan configDir
run port $ app chan (gitHubKey . pure $ "key")

@@ -17,9 +17,9 @@ source-repository head

library
exposed-modules:
Lib
Deploy.Server
Deploy.Worker
other-modules:
Paths_deployment_server
hs-source-dirs:
src
build-depends:
@@ -29,6 +29,8 @@ library
, servant-server
, github-webhooks
, wai
, typed-process
, temporary
default-language: Haskell2010
ghc-options: -fprint-potential-instances

@@ -44,6 +46,7 @@ executable deployment-server-exe
, deployment-server
, servant-github-webhook
, warp
, optparse-generic
default-language: Haskell2010

test-suite deployment-server-test
@@ -37,7 +37,7 @@
"created_at": "2019-05-15T15:20:33Z",
"updated_at": "2019-05-15T15:20:33Z",
"closed_at": null,
"merged_at": null,
"merged_at": "2019-05-15T15:20:33Z",
"merge_commit_sha": null,
"assignee": null,
"assignees": [
@@ -1,10 +1,11 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}

module Lib
module Deploy.Server
( app
) where

import Control.Concurrent.Chan (Chan, writeChan)
import Control.Monad.IO.Class (liftIO)
import Data.Maybe (isJust)
import Data.Proxy (Proxy (Proxy))
@@ -23,11 +24,11 @@ type Api
isMergePullRequestEvent :: PullRequestEvent -> Bool
isMergePullRequestEvent = isJust . whPullReqMergedAt . evPullReqPayload

prEventAction :: RepoWebhookEvent -> ((), PullRequestEvent) -> Handler ()
prEventAction _ (_, event)
| isMergePullRequestEvent event = liftIO $ putStrLn "merged"
prEventAction _ _ = liftIO $ putStrLn "non-merge PullRequestEvent"
prEventAction :: Chan PullRequestEvent -> RepoWebhookEvent -> ((), PullRequestEvent) -> Handler ()
prEventAction chan _ (_, event)
| isMergePullRequestEvent event = liftIO $ writeChan chan event
prEventAction _ _ _ = liftIO $ putStrLn "non-merge PullRequestEvent"

app :: GitHubKey PullRequestEvent -> Application
app key =
serveWithContext (Proxy :: Proxy Api) (key :. EmptyContext) prEventAction
app :: Chan PullRequestEvent -> GitHubKey PullRequestEvent -> Application
app chan key =
serveWithContext (Proxy :: Proxy Api) (key :. EmptyContext) $ prEventAction chan
@@ -0,0 +1,28 @@
{-# LANGUAGE OverloadedStrings #-}
module Deploy.Worker where

import Control.Concurrent.Chan (Chan, readChan)
import Control.Monad.IO.Class (liftIO)
import GitHub.Data.Webhooks.Events (PullRequestEvent)
import System.IO.Temp (withSystemTempDirectory)
import System.Process.Typed (runProcess, setWorkingDir, shell)

runWorker :: Chan PullRequestEvent -> FilePath -> IO ()
runWorker chan configDir = do
event <- readChan chan
deploy event configDir

deploy :: PullRequestEvent -> FilePath -> IO ()
deploy event configDir = withSystemTempDirectory "deployment" $ \tempDir -> liftIO $ do
let plutusDir = tempDir <> "/plutus"
nixopsDir = plutusDir <> "/deployment/nixops"
putStrLn "Deploy origin/master"
runIn tempDir "git clone https://github.com/input-output-hk/plutus.git"
runIn plutusDir "git checkout origin/master"
runIn nixopsDir $ shell $ "cp " <> configDir <> "/*.json ."
runIn nixopsDir "nixops create ./default.nix ./network.nix"
runIn nixopsDir "nixops deploy"
pure ()
where
runIn dir = runProcess . setWorkingDir dir

@@ -42,14 +42,10 @@ packages:
git: https://github.com/bitnomial/prometheus.git
commit: 69e4cefeb7d04d61a54cb0ae9fd57e2de134badb
extra-dep: true
# Need https://github.com/phadej/github/pull/386 and a relevant update for servant-github-webhook
# Need https://github.com/phadej/github/pull/387 and to be added back in to stackage
- location:
git: https://github.com/shmish111/github.git
commit: d55fddef18a816f090f733b3c28260a214f29f08
extra-dep: true
- location:
git: https://github.com/shmish111/servant-github-webhook.git
commit: fad4657ffba82a01a892385d60ec79cf2ad34104
commit: cc27b9de4d5d0939235fa9a8b418de3ea4807bab
extra-dep: true

extra-deps:
@@ -74,6 +70,10 @@ extra-deps:
- time-interval-0.1.1@sha256:7bfd3601853d1af7caa18248ec10b01701d035ac274a93bb4670fea52a14d4e8
- time-units-1.0.0@sha256:27cf54091c4a0ca73d504fc11d5c31ab4041d17404fe3499945e2055697746c1
- wl-pprint-1.2.1@sha256:aea676cff4a062d7d912149d270e33f5bb0c01b68a9db46ff13b438141ff4b7c
- servant-github-webhook-0.4.1.0@sha256:6ac456ccc6a2a96b30a7b80cd91b121f1b7e9bd33635641a6afbd6137700a753
- binary-instances-1@sha256:e7768b92f34bc40cc5cabecc5c143dee6ab4bcb5eb441d58e15a0b000d64940b
- binary-orphans-1.0.1@sha256:74d9a8e2c8c4dc8e11c9028ef103b930fc62f3943e45b1629f39114f2bfb5abb
- time-compat-1.9.2.2@sha256:9998dc1b77b5067572ab708e94750f1061152f342e92ad1aba38aae63581174d
flags:
language-plutus-core:
development: true

0 comments on commit fd56000

Please sign in to comment.
You can’t perform that action at this time.