forked from robinpowered/bifrost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbn-pi.js
185 lines (165 loc) · 5.69 KB
/
rbn-pi.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
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
// Generated by CoffeeScript 1.7.1
(function() {
var EventEmitter, NPM, Supervisor, config, forever, semver, supervisor, winston,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
winston = require('winston');
EventEmitter = require('events').EventEmitter;
semver = require('semver');
forever = require('forever-monitor');
NPM = require('npm');
config = require('./config.json');
Supervisor = (function(_super) {
__extends(Supervisor, _super);
function Supervisor() {
this.log.info('Started supervisor!');
this.interval = 300000;
this.updating = false;
this.packages = {};
NPM.load((function(_this) {
return function(err, npm) {
_this.npm = npm;
_this.startRunning();
return _this.watchReleases();
};
})(this));
}
Supervisor.prototype.log = winston;
Supervisor.prototype.getCurrentRelease = function(packageName) {
var err, pjson;
try {
pjson = require("./node_modules/" + packageName + "/package.json");
return this.packages[packageName].current = pjson.version;
} catch (_error) {
err = _error;
this.log.error(err);
this.log.warn("Package " + packageName + " does not exist, so this must be the first run.");
return this.packages[packageName].current = null;
}
};
Supervisor.prototype.watchReleases = function() {
return setInterval((function(_this) {
return function() {
return _this.checkOutdated();
};
})(this), this.interval);
};
Supervisor.prototype.checkOutdated = function() {
this.log.info('checking for outdated packages...');
return this.npm.commands.outdated((function(_this) {
return function(err, outdated) {
var needsUpdate, pack, _i, _len;
needsUpdate = false;
if (!err) {
for (_i = 0, _len = outdated.length; _i < _len; _i++) {
pack = outdated[_i];
if (pack[1].slice(0, 4) === 'rbn-') {
console.log("" + pack[1] + ": " + pack[3] + " > " + pack[2]);
if (semver.valid(pack[2]) && semver.valid(pack[3]) && semver.gt(pack[3], pack[2])) {
needsUpdate = true;
break;
}
}
}
if (needsUpdate) {
if (!_this.updating) {
return _this.update();
}
}
} else {
return _this.log.error(err);
}
};
})(this));
};
Supervisor.prototype.update = function() {
this.log.info('stopping current module...');
return this.stopRunning((function(_this) {
return function() {
_this.updating = true;
return _this.npm.commands.update(function(err) {
console.log(err);
if (!err) {
_this.log.info("Updated successfully!");
_this.updating = false;
return _this.startRunning();
} else {
_this.log.error("Error updating");
console.log(err);
return _this.updating = false;
}
});
};
})(this));
};
Supervisor.prototype.install = function(callback) {
if (callback == null) {
callback = null;
}
this.log.info('installing!');
this.updating = true;
return this.npm.commands.install((function(_this) {
return function(err) {
if (!err) {
_this.log.info("Installed successfully!");
_this.startRunning();
_this.updating = false;
return callback();
} else {
_this.log.error("Error installing");
console.log(err);
return _this.updating = false;
}
};
})(this));
};
Supervisor.prototype.stopRunning = function(callback) {
var emitter;
if (callback == null) {
callback = null;
}
this.log.info('stopping old os...');
if (this.running) {
emitter = this.running.stop();
return emitter.on('stop', (function(_this) {
return function() {
_this.log.info('stopped running successfully');
return callback();
};
})(this));
} else {
if (callback) {
return callback();
}
}
};
Supervisor.prototype.startRunning = function() {
var pjson, startScript;
this.log.info('starting os...');
pjson = require("./node_modules/rbn-base/package.json");
startScript = pjson.main;
if (startScript) {
this.running = new forever.Monitor("./node_modules/rbn-base/" + startScript + ".js", {
silent: false,
command: 'node'
});
this.running.on('start', (function(_this) {
return function(process, data) {
_this.log.info('script started successfully!');
return _this.emit('startedRunning');
};
})(this));
this.running.on('stop', (function(_this) {
return function() {
return _this.emit('stoppedRunning');
};
})(this));
return this.running.start();
} else {
return this.log.error("No start script in package.json - make sure you have a {'scripts':{'start':'someScript.js'}}");
}
};
return Supervisor;
})(EventEmitter);
supervisor = new Supervisor(config.NpmPackageName);
}).call(this);