-
Notifications
You must be signed in to change notification settings - Fork 5
/
rocket.service.js
138 lines (111 loc) · 2.84 KB
/
rocket.service.js
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
//rocket.service.js
var Service = require('node-windows').Service;
var EventLogger = require('node-windows').EventLogger;
var logger = new EventLogger('Rocket.Chat');
// Create a new service object
var svc = new Service({
name:'Rocket.Chat',
description: 'Rocket.Chat Service',
script: process.env["SCRIPT_PATH"],
env: [{
name: "PORT",
value: process.env["PORT"]
},
{
name: "ROOT_URL",
value: process.env["ROOT_URL"]
},
{
name: "MONGO_URL",
value: process.env["MONGO_URL"]
},
{
name: "MONGO_OPLOG_URL",
value: process.env["MONGO_OPLOG_URL"]
}]
});
var log = function(message){
console.log(message);
logger.info(message);
};
var warn = function(message){
console.warn(message);
logger.warn(message);
};
var error = function(message){
console.error(message);
logger.error(message);
};
svc.on('install',function(){
log('Install complete!');
svc.start();
});
svc.on('uninstall',function(){
log((svc.exists)
? error("Error. The Rocket.Chat service still exists.")
: log('Uninstall complete.'));
});
svc.on('start',function(){
log('The Rocket.Chat service is starting...');
});
svc.on('stop',function(){
warn('The Rocket.Chat service has stopped.');
});
svc.on('error',function(){
error('Error. Something went wrong...');
});
svc.on('alreadyinstalled',function(){
log('The Rocket.Chat service has already been installed.');
});
svc.on('invalidinstallation',function(){
error('Unable to install the Rocket.Chat service. Something is wrong with the data provided.');
});
var command = (process.argv.length > 2) ? process.argv[2] : null;
var doCommand = function()
{
switch(command)
{
case 'install':
log('Attempting to create the Rocket.Chat service...');
svc.install();
break;
case 'uninstall':
log('Attempting to remove the Rocket.Chat service...');
svc.uninstall();
break;
case 'start':
log('Attempting to start the Rocket.Chat service...');
svc.start();
case 'stop':
log('Attempting to stop the Rocket.Chat service...');
svc.stop();
break;
default:
console.error('Error. Unknown command!');
break;
}
}
var varsAreSpecified = function(command)
{
var result = true;
var requiredVars = [ 'SCRIPT_PATH' ];
if(command == 'install')
{
requiredVars = [ 'PORT', 'ROOT_URL', 'MONGO_URL', 'MONGO_OPLOG_URL', 'SCRIPT_PATH' ];
}
for(var x =0; x < requiredVars.length; x++)
{
var required = requiredVars[x];
if(process.env[required] == null)
{
error('Error. '+ required +' was not defined!');
result = false;
x = requiredVars.length;
}
}
return result;
}
if(varsAreSpecified(command))
{
doCommand();
}