-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuild.hs
More file actions
executable file
·381 lines (341 loc) · 9.61 KB
/
Copy pathBuild.hs
File metadata and controls
executable file
·381 lines (341 loc) · 9.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env stack
{- stack
--nix
--no-nix-pure
--resolver=lts-12.26
runghc
--package filemanip
--package HStringTemplate
--package regex-compat
--package shake
--package split
-}
import Control.Monad
import Data.Monoid
import Data.List
import Data.List.Split
import Development.Shake
import Development.Shake.Command
import Development.Shake.FilePath
import Development.Shake.Util
import qualified System.Directory as Dir
import System.Environment (getEnvironment)
import System.Exit
import System.Info
import System.Process
import System.Process.Internals
import Text.Regex
import Text.StringTemplate
clojureVersion = "1.9.0-beta1"
appVersion = "0.0.1"
maintainer = "Drew Raines <web@trinitynashville.org>"
heapSize = "1g"
gcrRoot = "gcr.io"
gcrProject = "trinitynashville-188115"
gcrTagName = "media"
gcrDeploymentName = "triweb-media"
appDir = "app"
projectClj = appDir </> "project.clj"
appUberWar = appDir </> "target" </> "app.war"
appUberJar = appDir </> "target" </> "app.jar"
versionTxtApp = appDir </> "resources" </> "version.txt"
dockerDir = "docker"
dockerFileTmpl = "Dockerfile.app.st"
dockerFile = dockerDir </> "Dockerfile"
appDockerWar = dockerDir </> "app.war"
appDockerJar = dockerDir </> "app.jar"
sermonJson = dockerDir </> "sermons.json.gz"
sermonJsonStaging = appDir </> "target" </> "sermons.json.gz"
mediaIndex = "triweb-media"
kubeContext = "gke_trinitynashville-188115_us-central1-a_triweb"
shakeOpts = shakeOptions { shakeFiles="_build"
, shakeTimings=True
, shakeVerbosity=Loud
}
-- http://stackoverflow.com/a/27388709/3227
getPid :: ProcessHandle -> IO (Maybe PHANDLE)
getPid ph = withProcessHandle ph go
where
go ph_ = case ph_ of
OpenHandle x -> return $ Just x
ClosedHandle _ -> return Nothing
cleanUpProcess :: ProcessHandle -> String -> IO ()
cleanUpProcess hdl desc = do
putStrLn $ ">>>>> terminating " <> desc
terminateProcess $ hdl
withProcess :: String -> CreateProcess -> Action () -> Action ()
withProcess name p action = do
(_, _, _, handle) <- liftIO . createProcess $ p
(Just pid) <- liftIO . Main.getPid $ handle
let desc = name <> " (" <> show pid <> ")"
putNormal $ ">>>>> starting " <> desc
flip actionFinally (cleanUpProcess handle desc) action
dirtyGit :: IO Bool
dirtyGit = do
(code, _, _) <- readProcessWithExitCode
"git" ["diff-index"
, "--quiet"
, "HEAD"
] ""
pure $ case code of
ExitSuccess -> False
(ExitFailure _) -> True
gitVersion :: IO String
gitVersion = do
dirt <- dirtyGit
date <- readCreateProcess
(proc "git"
[ "log"
, "--pretty=format:%cI"
, "-1"
]) ""
sha <- readCreateProcess
(proc "git"
[ "log"
, "--pretty=format:%h"
, "-1"
]) ""
pure $ (dropLast4 . justNums $ date) <> "-" <> sha <> if dirt then "-dev" else ""
where
justNums s = subRegex (mkRegex "[^0-9]+") s ""
dropLast4 s = reverse . (drop 4) . reverse $ s
makeGcrImageName :: String -> String
makeGcrImageName ver = gcrRoot <> "/" <> gcrProject <> "/" <> gcrTagName <> ":" <> ver
logCmd :: String -> [String] -> Action ()
logCmd dir c = do
putNormal $ unwords $ [">>>"] ++ c
cmd [Cwd dir] c
main :: IO ()
main = shakeArgs shakeOpts $ do
want [ "info"
, projectClj
, appDockerWar
]
phony "clean" $ do
putNormal "cleaning..."
projectCljExists <- doesFileExist $ appDir </> "project.clj"
when projectCljExists $ do
unit $ cmd (Cwd appDir) "rm -rf target"
removeFilesAfter "_build" ["//*"]
removeFilesAfter "docker" ["//*"]
removeFilesAfter "lib" ["//*.jar", "//*.war"]
removeFilesAfter appDir ["project.clj"]
removeFilesAfter appDir ["resources/version.txt"]
phony "info" $ do
ver <- liftIO gitVersion
unit $ cmd "which lein"
unit $ cmd "which stack"
putNormal $ "os: " <> os
putNormal $ "arch: " <> arch
putNormal $ "gitVersion: " <> ver
phony "test-jetty" $ do
env' <- liftIO getEnvironment
need [ appUberJar ]
let
-- This wasn't ideal because the compilations from the `lein
-- run` and the `lein test` would actually happen concurrently.
-- However, I forgot the trampoline before (to properly replace
-- the parent process), and want to remember it in case I move
-- back.
-- p = (proc "lein" [ "trampoline"
-- , "run"
-- , "-m"
-- , "triweb.boot" ])
-- { cwd = Just appDir }
p = (proc "java" [ "-cp"
, appUberJar
, "triweb.boot"
])
{ env = Just ([ ("DEV", "true") ] <> env') }
withProcess "jetty" p $ do
unit $ cmd [ Cwd appDir
]
"lein test"
phony "run-jetty" $ do
need [ projectClj ]
unit $ cmd [ AddEnv "DEV" "true"
, Cwd appDir
]
"lein" "ring" "server-headless"
phony "docker-build" $ do
ver <- liftIO gitVersion
need [ appDockerJar
, dockerFile
, sermonJson
]
logCmd
dockerDir
[ "docker"
, "build"
, "-t"
, "trinitynashville/media:" <> ver
, "."
]
phony "docker-tag-gcr" $ do
ver <- liftIO gitVersion
need [ "docker-build"
]
cmd
[ "docker"
, "tag"
, "trinitynashville/media:" <> ver
, makeGcrImageName ver
]
phony "docker-push-gcr" $ do
ver <- liftIO gitVersion
need [ "docker-tag-gcr"
]
logCmd
"."
[ "docker"
, "push"
, makeGcrImageName ver
]
phony "update-gcr" $ do
ver <- liftIO gitVersion
need [ "docker-push-gcr"
]
cmd
[ "kubectl"
, "set"
, "image"
, "--context", kubeContext
, "deployment/" <> gcrDeploymentName
, gcrDeploymentName <> "=" <> makeGcrImageName ver
]
phony "kube-shell" $ do
ver <- liftIO gitVersion
need [ "update-gcr"
]
logCmd
"."
[ "kubectl"
, "run"
, "--context", kubeContext
, "shell"
, "--rm", "-i"
, "--image", makeGcrImageName ver
, "--restart", "Never"
, "--env=SERMONS_FILE=/sermons.json.gz"
, "--env=ES_URL=http://elasticsearch:9200"
, "--env=ES_INDEX=" <> mediaIndex
, "/bin/bash"
]
phony "load-media" $ do
ver <- liftIO gitVersion
need [ "update-gcr"
]
logCmd
"."
[ "kubectl"
, "run"
, "--context", kubeContext
, "load-data"
, "--rm", "-i"
, "--image", makeGcrImageName ver
, "--restart", "Never"
, "--env=SERMONS_FILE=/sermons.json.gz"
, "--env=ES_URL=http://elasticsearch:9200"
, "--env=ES_INDEX=" <> mediaIndex
, "--"
, "java", "-cp", "/app/app.jar", "triweb.load"
]
phony "load-media-local" $ do
ver <- liftIO gitVersion
need [ "docker-build"
]
cmd [ AddEnv "ES_INDEX" mediaIndex
, AddEnv "SERMONS_FILE" "/sermons.json.gz"
]
[ "docker"
, "run"
, "-t"
, "-i"
, "--network"
, "host"
, "-e"
, "ES_URL"
, "-e"
, "ES_INDEX"
, "-e"
, "SERMONS_FILE"
, "-p"
, "9000:9000"
, "trinitynashville/media:" <> ver
, "java", "-cp", "/app/app.jar", "triweb.load"
]
phony "docker-run" $ do
ver <- liftIO gitVersion
need [ "docker-build"
]
cmd [ AddEnv "ES_INDEX" mediaIndex
]
[ "docker"
, "run"
, "-t"
, "-i"
, "--network"
, "host"
, "-e"
, "ES_URL"
, "-e"
, "ES_INDEX"
, "-p"
, "9000:9000"
, "trinitynashville/media:" <> ver
]
dockerFile %> \out -> do
need [ dockerFileTmpl ]
confText <- liftIO . readFile $ dockerFileTmpl
let tmpl = newSTMP confText :: StringTemplate String
rendered = render $
setAttribute "maintainer" maintainer tmpl
liftIO $ writeFile out rendered
projectClj %> \out -> do
let cljTmpl = projectClj <> ".st"
need [ cljTmpl ]
confText <- liftIO . readFile $ cljTmpl
cwd <- liftIO Dir.getCurrentDirectory
let tmpl = newSTMP confText :: StringTemplate String
rendered = render $
setAttribute "appVersion" appVersion $
setAttribute "clojureVersion" clojureVersion tmpl
liftIO $ writeFile out rendered
versionTxtApp %> \out -> do
ver <- liftIO gitVersion
liftIO $ writeFile out ver
appUberWar %> \out -> do
need [ projectClj
, versionTxtApp
]
unit $ cmd [ Cwd appDir
, AddEnv "LEIN_SNAPSHOTS_IN_RELEASE" "true" ]
"lein ring uberwar"
appUberJar %> \out -> do
need [ projectClj
, versionTxtApp
]
unit $ cmd [ Cwd appDir
, AddEnv "LEIN_SNAPSHOTS_IN_RELEASE" "true" ]
"lein with-profile package uberjar"
sermonJsonStaging %> \out -> do
need [ projectClj
, versionTxtApp
]
unit $ cmd [ Cwd appDir
, AddEnv "LEIN_SNAPSHOTS_IN_RELEASE" "true" ]
"lein with-profile sermon-compile run -m triweb.media/compile-sermons -- search/"
sermonJson %> \out -> do
need [ sermonJsonStaging
]
copyFile' sermonJsonStaging out
appDockerWar %> \out -> do
let archive = appUberWar
need [ archive
]
copyFile' archive out
appDockerJar %> \out -> do
let archive = appUberJar
need [ archive
]
copyFile' archive out