-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
vbb-restd
executable file
·94 lines (71 loc) · 1.94 KB
/
vbb-restd
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
#!/usr/bin/env coffee
minimist = require 'minimist'
argv = minimist process.argv.slice 2
help = [
'Usage:'
'vbb-restd start -c <cert> -k <key> [-p <port>]'
'vbb-restd stop <id>'
''
'Arguments:'
' -c, --cert The SSL certificate.'
' -k, --key The SSL key.'
' id The server process id.'
''
'Options:'
' -p, --port Where the server will listen. Default: 8000'
].join '\n'
if argv.h is true or argv.help is true or argv._.length is 0
process.stdout.write help
process.exit 0
else if argv._.length is 1
process.stdout.write help
process.exit 1
if argv._[0] is 'start'
fs = require 'fs'
pm2 = require 'pm2'
path = require 'path'
shortid = require 'shortid'
winston = require 'winston' # todo: consider using `bunyan`
cert = null
if argv.c? then cert = argv.c
else if argv.cert? then cert = argv.cert
else throw new Error 'Missing `--cert` argument.'
cert = fs.readFileSync cert
key = null
if argv.k? then key = argv.k
else if argv.key? then key = argv.key
else throw new Error 'Missing `--key` argument.'
key = fs.readFileSync key
port = 8000
if argv.p? then port = argv.p
if argv.port? then port = argv.port
id = shortid.generate()
options =
exec_mode: 'fork_mode'
exec_interpreter: 'coffee'
script: path.join __dirname, 'vbb-rest'
name: "vbb-rest-#{id}"
args: [
'-c', cert
'-k', ley
'-p', port
]
watch: false
pm2.connect ()->
pm2.start options, (err, process) ->
if err then throw err
winston.info "The server #{id} has been started."
pm2.disconnect()
else if argv._[0] is 'stop'
pm2 = require 'pm2'
winston = require 'winston' # todo: consider using `bunyan`
pm2.connect ()->
pm2.stop "vbb-rest-#{argv._[1]}", (err) ->
if err then throw err
pm2.delete "vbb-rest-#{argv._[1]}", (err) ->
if err then throw err
winston.info "The server #{id} has been stopped."
pm2.disconnect()
else
process.stdout.write help
process.exit 1