-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdeploy.lisp
151 lines (137 loc) · 5.68 KB
/
deploy.lisp
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
(in-package #:org.shirakumo.radiance.core)
(deploy:define-hook (:pre-load set-environment) ()
(deploy:status 0 "Configuring environment")
(setf *deploying-p* T)
(unless *environment*
(let ((*default-environment-config* (asdf:system-relative-pathname :radiance "default-config-deploy.lisp")))
(setf (environment) (if (boundp 'cl-user::environment)
(symbol-value 'cl-user::environment)
"deploy")))))
(deploy:define-hook (:load load-modules) ()
(when (boundp 'cl-user::modules)
(deploy:status 1 "Loading target modules")
(dolist (module (symbol-value 'cl-user::modules))
(asdf:load-system module)))
(deploy:status 1 "Simulating boot to load required interfaces")
(load-implementation 'logger)
(load-implementation 'server)
(dolist (module (config :startup))
#+quicklisp (ql:quickload module)
#-quicklisp (asdf:load-system module))
;; Clear ASDF/QL
(asdf:clear-configuration)
(setf (fdefinition 'asdf:upgrade-asdf) (lambda ()))
#+quicklisp (setf ql:*local-project-directories* ())
(dolist (system (asdf:already-loaded-systems))
(asdf:register-immutable-system system)
(asdf:clear-system system)))
(deploy:define-hook (:deploy copy-resources) (directory)
;; Ensure expected directory structure:
;; bin/
;; modules/
;; config/
;; data/
;; cache/
;; override/
;; template/
;; static/
;; radiance.conf.lisp
;; setup.lisp
;; radiance
(labels ((path (path)
(merge-pathnames path directory)))
(deploy:status 1 "Ensuring directory structure")
(dolist (path '("modules/"
"config/"
"data/"
"cache/asdf/"
"override/template/"
"override/static/"))
(ensure-directories-exist (path path)))
(dolist (part '((:configuration "config/")
(:cache "cache/")
(:data "data/")
(:template "override/template/")
(:static "override/static/")))
(deploy:copy-directory-tree
(environment-directory T (first part))
(path (second part)) :copy-root NIL))
;; Copy config file to root
(uiop:copy-file (mconfig-pathname #.*package*)
(path "radiance.conf.lisp"))
;; Create setup file if necessary
(with-open-file (stream (path "setup.lisp") :direction :output
:if-exists NIL)
(when stream
(format stream ";;;; Radiance startup file. Loaded on init.~%(in-package :rad-user)~%")))
;; Copy module data
(deploy:status 1 "Copying module data")
(dolist (module (list-modules))
(when (and (not (interface-p module)) (resolve-base module))
(deploy:copy-directory-tree
(merge-pathnames "template/" (resolve-base module))
(path (make-pathname :directory `(:relative "modules" ,(module-name module) "template")))
:copy-root NIL)
(deploy:copy-directory-tree
(merge-pathnames "static/" (resolve-base module))
(path (make-pathname :directory `(:relative "modules" ,(module-name module) "static")))
:copy-root NIL)))))
(deploy:define-hook (:boot setup-environment) ()
(flet ((path (path)
(merge-pathnames path (deploy:runtime-directory))))
(deploy:status 0 "Configuring Radiance environment")
(defmethod environment-directory (_ (kind (eql :configuration)))
(path "config/"))
(defmethod environment-directory (_ (kind (eql :cache)))
(path "cache/"))
(defmethod environment-directory (_ (kind (eql :data)))
(path "data/"))
(defmethod environment-directory (_ (kind (eql :template)))
(path "override/template/"))
(defmethod environment-directory (_ (kind (eql :static)))
(path "override/static/"))
(defun resolve-base (thing)
(etypecase thing
(pathname thing)
(null (resolve-base *package*))
((or string symbol package)
(path (make-pathname :directory `(:relative "modules" ,(module-name (modularize:module thing))))))))
(defmethod ubiquitous:designator-pathname ((designator (eql #.*package*)) type)
(make-pathname :name "radiance" :type (format NIL "conf.~(~a~)" type)
:defaults (deploy:runtime-directory)))
(deploy:status 1 "Reloading config file")
(reload-environment)
(deploy:status 1 "Loading setup file")
(setf asdf:*central-registry* (list (path "modules/")))
(setf asdf:*user-cache* (path "cache/asdf/"))
(when (probe-file (path "setup.lisp"))
(load (path "setup.lisp")))))
(defun primitive-repl (&key (out *standard-output*) (in *standard-input*))
(terpri out)
(loop (format out "~%~a> " (package-name *package*))
(finish-output out)
(setf - (read in NIL :quit))
(case -
(:quit
(radiance:shutdown)
(uiop:quit))
(:i
(setf *package* (find-package (string (read)))))
(T
(shiftf /// // / (multiple-value-list (eval -)))
(shiftf +++ ++ + -)
(shiftf *** ** * (first /))
(print *)))))
(defun startup-binary ()
(radiance:startup)
(deploy:status 0 "Server started up at http://~a~@[:~a~]"
(first (config :domains)) (config :port))
;; We are done starting up, now deregister systems so we can load/update later. Maybe.
(asdf/system-registry:clear-registered-systems)
;; Primitive repl
(setf *package* (find-package '#:rad-user))
(if (and (open-stream-p *standard-input*)
(ignore-errors (read-char-no-hang *standard-input*) T))
(primitive-repl)
(loop while (started-p)
do (sleep 60))))