forked from cronicle-edge/cronicle-edge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle
executable file
·406 lines (332 loc) · 13.9 KB
/
bundle
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/bin/bash
cd "$(dirname "$0")"
usage() {
echo "Usage: $0 /path/to/dist [--s3 | --lmdb | --level | --sftp | --sql | --dev | --verbose]"
exit 1
}
writehead() {
printf "\n ---- $1 [$(date +"%T")]\n"
}
# -------------------- ARG CHECK
x=0
dist="dist" # default
while (( "$#" )); do
case $1 in
--s3 | --S3 ) s3=1 ;;
--sftp ) sftp=1 ;;
--lmdb ) lmdb=1 ;;
--level ) level=1 ;;
--sql ) sql=1 ;;
--dev | -d ) dev=1 ;; # avoid minification, add verbosity
--verbose | -v ) verbose=1 ;;
--force | -f ) force=1 ;; # reinstall npm packages
--tools | -t ) tools=1 ;; # bundle up repair and migration tools
--all | -a ) all=1 ;; # install all engines
--restart | -r ) restart=1 ;; # start cronicle upon completion
-*) echo "invalid parameter: $1"; usage ;;
* ) dist=$1; x=$(($x + 1))
esac
shift
done
if [ "$x" -gt 1 ]; then
echo "Too many arguments"; usage;
fi
if ! command -v npm &>/dev/null ; then
echo "npm is not installed"
exit 1
fi
if ! command -v node &>/dev/null ; then
echo "node is not installed"
exit 1
fi
# ---------------------------------------------------------------------
echo "------------------------------------------------------"
echo " Installing cronicle bundle into $(readlink -f $dist)"
echo "------------------------------------------------------"
# debug settings
minify="--minify=true"
ESBuildLogLevel="warning"
npmLogLevel="warn"
do_restart="false"
if [ "$restart" = 1 ]; then
do_restart="true"
fi
# check if cronicle is running and stop
if [ -e $dist/logs/cronicled.pid ]; then
if ps -p $(cat $dist/logs/cronicled.pid) > /dev/null; then
writehead "Stopping cronicle [auto restart: $do_restart]"
kill -15 $(cat $dist/logs/cronicled.pid) # sigterm
fi
fi
if [ "$dev" = 1 ]; then
minify="--minify=false"
ESBuildLogLevel="info"
npmLogLevel="info"
fi
if [ "$restart" = 1 ]; then
minify="--minify=false"
fi
if [ "$verbose" = 1 ]; then
ESBuildLogLevel="info"
npmLogLevel="info"
fi
if [ "$all" = 1 ]; then
s3=1
level=1
lmdb=1
sql=1
sftp=1
tools=1
fi
# ---------------------------------------------------------
# install esbuild if needed
if ! command -v esbuild &>/dev/null; then
writehead "Installing esbuild"
npm i esbuild -g --loglevel $npmLogLevel
fi
# skip "npm install" if node_modules exist already
if [ ! -d "node_modules" ] || [ "$force" = 1 ]; then
writehead "Installing npm packages"
npm install --loglevel $npmLogLevel
fi
# ----------------
mkdir -p $dist
cp -r htdocs $dist/
cp -r bin $dist/
cp package.json $dist/bin/
# -------------------------------
writehead "Building frontend"
mkdir -p $dist/htdocs/js/external && cp \
node_modules/jquery/dist/jquery.min.js \
node_modules/moment/min/moment.min.js \
node_modules/moment-timezone/builds/moment-timezone-with-data.min.js \
node_modules/chart.js/dist/Chart.min.js \
node_modules/jstimezonedetect/dist/jstz.min.js \
node_modules/socket.io/client-dist/socket.io.min.js \
node_modules/ansi_up/ansi_up.js \
node_modules/jquery-ui-dist/jquery-ui.min.js \
node_modules/graphlib/dist/graphlib.min.js \
node_modules/vis-network/dist/vis-network.min.js \
node_modules/xss/dist/xss.min.js \
node_modules/jquery-datetimepicker/build/jquery.datetimepicker.full.min.js \
node_modules/diff/dist/diff.min.js \
$dist/htdocs/js/external/
mkdir -p $dist/htdocs/css && cp \
node_modules/font-awesome/css/font-awesome.min.css \
node_modules/@mdi/font/css/materialdesignicons.min.css \
node_modules/jquery-ui-dist/jquery-ui.min.css \
node_modules/jquery-datetimepicker/build/jquery.datetimepicker.min.css \
node_modules/pixl-webapp/css/base.css \
$dist/htdocs/css/
mkdir -p $dist/htdocs/fonts && cp \
node_modules/font-awesome/fonts/*.woff2 \
node_modules/@mdi/font/fonts/*.woff2 \
node_modules/pixl-webapp/fonts/*.woff2 \
$dist/htdocs/fonts/
# code mirror css
cat \
node_modules/codemirror/lib/codemirror.css \
node_modules/codemirror/theme/darcula.css \
node_modules/codemirror/theme/solarized.css \
node_modules/codemirror/theme/gruvbox-dark.css \
node_modules/codemirror/addon/scroll/simplescrollbars.css \
node_modules/codemirror/addon/display/fullscreen.css \
node_modules/codemirror/addon/lint/lint.css \
node_modules/codemirror/addon/fold/foldgutter.css \
> $dist/htdocs/css/codemirror.css
# codemirror js
cat \
node_modules/codemirror/lib/codemirror.js \
node_modules/codemirror/addon/scroll/simplescrollbars.js \
node_modules/codemirror/addon/edit/matchbrackets.js \
node_modules/codemirror/addon/selection/active-line.js \
node_modules/codemirror/addon/fold/foldgutter.js \
node_modules/codemirror/addon/fold/foldcode.js \
node_modules/codemirror/addon/fold/brace-fold.js \
node_modules/codemirror/addon/fold/indent-fold.js \
node_modules/codemirror/mode/powershell/powershell.js \
node_modules/codemirror/mode/javascript/javascript.js \
node_modules/codemirror/mode/python/python.js \
node_modules/codemirror/mode/perl/perl.js \
node_modules/codemirror/mode/shell/shell.js \
node_modules/codemirror/mode/groovy/groovy.js \
node_modules/codemirror/mode/clike/clike.js \
node_modules/codemirror/mode/properties/properties.js \
node_modules/codemirror/addon/display/fullscreen.js \
node_modules/codemirror/mode/xml/xml.js \
node_modules/codemirror/mode/sql/sql.js \
node_modules/js-yaml/dist/js-yaml.js \
node_modules/codemirror/addon/lint/lint.js \
node_modules/codemirror/addon/lint/json-lint.js \
node_modules/codemirror/addon/lint/yaml-lint.js \
node_modules/codemirror/addon/mode/simple.js \
node_modules/codemirror/mode/dockerfile/dockerfile.js \
node_modules/codemirror/mode/toml/toml.js \
node_modules/codemirror/mode/yaml/yaml.js \
node_modules/codemirror/addon/comment/comment.js \
node_modules/jsonlint-mod/lib/jsonlint.js \
| esbuild --log-level=$ESBuildLogLevel $minify > $dist/htdocs/js/codemirror.min.js
cat \
node_modules/pixl-webapp/js/md5.js \
node_modules/pixl-webapp/js/oop.js \
node_modules/pixl-webapp/js/xml.js \
node_modules/pixl-webapp/js/tools.js \
node_modules/pixl-webapp/js/datetime.js \
node_modules/pixl-webapp/js/page.js \
node_modules/pixl-webapp/js/dialog.js \
node_modules/pixl-webapp/js/base.js \
| esbuild --log-level=$ESBuildLogLevel $minify --keep-names > $dist/htdocs/js/common.min.js
cat htdocs/js/app.js \
htdocs/js/pages/Base.class.js \
htdocs/js/pages/Home.class.js \
htdocs/js/pages/Login.class.js \
htdocs/js/pages/Schedule.class.js \
htdocs/js/pages/History.class.js \
htdocs/js/pages/JobDetails.class.js \
htdocs/js/pages/MyAccount.class.js \
htdocs/js/pages/Admin.class.js \
htdocs/js/pages/admin/Categories.js \
htdocs/js/pages/admin/Servers.js \
htdocs/js/pages/admin/Users.js \
htdocs/js/pages/admin/Plugins.js \
htdocs/js/pages/admin/Activity.js \
htdocs/js/pages/admin/APIKeys.js \
htdocs/js/pages/admin/ConfigKeys.js \
htdocs/js/pages/admin/Secrets.js \
| esbuild --log-level=$ESBuildLogLevel $minify --keep-names > $dist/htdocs/js/combo.min.js
cp htdocs/index-bundle.html $dist/htdocs/index.html
writehead "Bundle storage-cli and event plugins"
esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$dist/bin/ \
--external:../conf/config.json --external:../conf/storage.json --external:../conf/setup.json \
bin/storage-cli.js
esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$dist/bin/ bin/shell-plugin.js
esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$dist/bin/ bin/test-plugin.js
esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$dist/bin/ bin/url-plugin.js
esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$dist/bin/ --loader:.node=file bin/ssh-plugin.js
esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$dist/bin/ bin/workflow.js
esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$dist/bin/ bin/run-detached.js
# ----------- Tools (repair/migrate)
if [ "$tools" = 1 ]; then
writehead "Bundling cronicle tools"
printf " - storage-repair.js \n"
esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$dist/bin/ --external:../conf/config.json bin/storage-repair.js
printf " - storage-migrate.js \n"
esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$dist/bin/ --external:../conf/config.json bin/storage-migrate.js
fi
writehead "Building Storage Engines"
printf " - bundling FS Engine\n"
esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$dist/bin/engines engines/Filesystem.js
if [ "$s3" = 1 ]; then
printf " - bundling S3 Engine\n"
npm i @aws-sdk/client-s3 @aws-sdk/lib-storage --no-save --loglevel silent
esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$dist/bin/engines engines/S3.js
fi
if [ "$level" = 1 ]; then
printf " - bundling Level Engine\n"
npm i level --no-save --loglevel silent
esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$dist/bin/engines engines/Level.js
mkdir -p $dist/bin/engines/prebuilds/
cp -r node_modules/classic-level/prebuilds/linux-x64 $dist/bin/engines/prebuilds/
fi
if [ "$lmdb" = 1 ]; then
printf " - bundling Lmdb Engine*\n"
esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$dist/bin/engines --external:lmdb engines/Lmdb.js
fi
if [ "$sftp" = 1 ]; then
printf " - bundling Sftp Engine\n"
npm i ssh2-sftp-client --no-save --loglevel silent
esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --loader:.node=file --outdir=$dist/bin/engines engines/Sftp.js
fi
if [ "$sql" = 1 ]; then
printf " - bundling SQL Engine [mysql/postgres]\n"
npm i knex pg pg-query-stream mysql2 --no-save --loglevel silent
esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --external:oracledb --external:sqlite3 \
--external:mysql --external:tedious --external:pg-native --external:better-sqlite3 \
--outdir=$dist/bin/engines engines/SQL.js
fi
if [ "$redis" = 1 ]; then
printf " - bundling Redis Engine \n"
npm i redis@3.1.2 --no-save --loglevel silent
esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$dist/bin/engines engines/Redis.js
fi
# --- CRONICLE.JS
writehead "Bundling cronicle.js"
esbuild --bundle --log-level=$ESBuildLogLevel $minify --keep-names --platform=node --outfile=$dist/bin/cronicle.js lib/main.js
writehead "Applyig some patches"
# fix formidable
esbuild --bundle --log-level=$ESBuildLogLevel $minify --keep-names --platform=node --outdir=$dist/bin/plugins node_modules/formidable/src/plugins/*.js
# --- setup configs on the initial run
if [ ! -d $dist/conf ]; then
writehead "Setting up initial configs"
mkdir -p $dist/conf
cp sample_conf/config.json sample_conf/setup.json $dist/conf/
cp -r sample_conf/emails $dist/conf/emails
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1 > $dist/conf/secret_key
chmod 400 $dist/conf/secret_key
fi
# ---- set up npm pac in dist if not yet, add some deps if needed
cd $dist
if [ ! -e 'package.json' ]; then
writehead "Setting up npm package in $dist"
npm init -y &>/dev/null
npm pkg set name="croniclex"
npm pkg set bin="bin/control.sh"
npm pkg set main="bin/cronicle.js"
npm pkg set scripts.start="bin/cronicle.js --foreground --echo --manager --color"
fi
if [ "$lmdb" = 1 ]; then
npm i lmdb --loglevel silent
fi
cd - &>/dev/null
# ------ clean up
writehead "Final cleanup"
rm -rf $dist/bin/cronicled.init $dist/bin/debug.sh $dist/bin/install.js $dist/bin/build.js $dist/bin/build-tools.js \
$dist/bin/manager.bat $dist/bin/worker.bat $dist/bin/win-* $dist/bin/*.ps1
chmod -R 755 $dist/bin
# -------------------- we are done
# if in dev mode, start cronicle on background in manager mode
if [ "$restart" = 1 ]; then
writehead "Starting cronicle..."
export CRONICLE_dev_version="1.x.dev-$(date '+%Y-%m-%d %H:%M:%S')"
$dist/bin/cronicle.js --manager
# echo " - new process: $(cat $dist/logs/cronicled.pid)"
echo " - dev version: $CRONICLE_dev_version"
exit 0
fi
echo ""
echo "---------------------------------------------------------------------------------------------------------------------------------------"
echo ""
echo "Bundle is ready: $(readlink -f $dist)"
if [ "$sql" = 1 ]; then
echo " - SQL Engine is bundled for mysql and postgress only. SQLite, MSSQL Oracle need to be installed separetly in $dist"
fi
if [ "$lmdb" = 1 ]; then
echo " - Lmdb cannot be fully bundeled. Lmdb package was installed in $dist"
fi
if [ "$level" = 1 ]; then
echo " - If not running this on linux, copy OS proper binary from node_modules/classic-level/prebuilds to $dist/bin/engines/prebuilds/"
fi
if [ "$restart" = 1 ]; then
echo "Running in dev mode. Version: $CRONICLE_dev_version"
exit 0
fi
cat << EOF
Before you begin:
- Configure you storage engine setting in conf/config.json || conf/storage.json || CRONICLE_storage_config=/path/to/custom/storage.json
- Set you Secret key in conf/config.json || conf/secret_key file || CRONICLE_secret_key_file || CRONICLE_secret_key env variables
To setup cronicle storage (on the first run):
node $dist/bin/storage-cli.js setup
Start as manager in foreground:
node $dist/bin/cronicle.js --echo --foreground --manager --color
Or use $dist/bin/manager script to setup storage + start cronicle in single manager mode
To reinstall/upgrade run: ./bundle.sh $dist -f
Please back up $(readlink -f $dist) first for prod deployments
To setup as systemd service (! make sure node version for sudo user is 16 or higher):
cd $(readlink -f $dist)
npm i pixl-boot
sudo node node_modules/pixl-boot/cli.js install
### systemctl status croniclex
To remove service:
sudo node node_modules/pixl-boot/cli.js uninstall
---------------------------------------------------------------------------------------------------------------------------------------
EOF