Skip to content

Commit 156f217

Browse files
author
Scott Smereka
committed
Added install events to avoid sequential installs or uninstalls.
1 parent 847d2a6 commit 156f217

File tree

3 files changed

+242
-29
lines changed

3 files changed

+242
-29
lines changed

server/app/event/serverEvent_model.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// ~> Model
2+
// ~A Scott Smereka
3+
4+
/* Event
5+
*
6+
*/
7+
8+
/* ************************************************** *
9+
* ******************** Load Libraries
10+
* ************************************************** */
11+
12+
var fox = require("foxjs"),
13+
model = fox.model;
14+
15+
16+
module.exports = function(app, db, config) {
17+
18+
19+
/* ************************************************** *
20+
* ******************** Module Variables
21+
* ************************************************** */
22+
23+
var Schema = db.Schema, // Mongoose schema object for MongoDB documents.
24+
ObjectId = Schema.ObjectId; // Object ID used in mongoose schemas
25+
26+
27+
/* ************************************************** *
28+
* ******************** Module Config Schema
29+
* ************************************************** */
30+
31+
/**
32+
* Describes an event that took place.
33+
*/
34+
var ServerEvent = new Schema({
35+
how: String,
36+
what: String,
37+
why: String,
38+
when: { type: Date, default: Date.now },
39+
who: { type: ObjectId, ref: "User" }
40+
});
41+
42+
43+
/* ************************************************** *
44+
* ******************** Methods
45+
* ************************************************** */
46+
47+
48+
/* ************************************************** *
49+
* ********************
50+
* ************************************************** */
51+
52+
//Phone.pre('save', function(next) {
53+
// return next();
54+
//});
55+
56+
57+
/* ************************************************** *
58+
* ******************** CRUD Override Methods
59+
* ************************************************** */
60+
61+
/* Enabling CRUD will automatically take care of
62+
* update, and delete methods for the object. However
63+
* you can still add your own custom functionality
64+
* here, by overriding the default methods.
65+
*
66+
* In addition to overriding you can add more methods
67+
* that CRUD will automatically use such as sanitize.
68+
*/
69+
70+
/**
71+
* Strip out secret information that should not be seen
72+
* outside of this server.
73+
*/
74+
ServerEvent.methods.sanitize = function() {
75+
return this;
76+
};
77+
78+
/* ************************************************** *
79+
* ******************** Plugins
80+
* ************************************************** */
81+
82+
// Enable additional functionality through plugins
83+
// you have written or 3rd party plugins.
84+
85+
// Add addition fields and methods to this schema to
86+
// create, read, update, and delete schema objects.
87+
ServerEvent.plugin(model.crudPlugin);
88+
89+
/* ************************************************** *
90+
* ******************** Export Schema(s)
91+
* ************************************************** */
92+
93+
db.model('ServerEvent', ServerEvent);
94+
95+
};

server/app/index.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,36 @@ var server = {
3030
return log.e(err);
3131
}
3232

33-
log.s("Server Installed Successfully!");
33+
if(results) {
34+
log.s("Server installed successfully!");
35+
}
36+
});
37+
},
38+
39+
uninstall: function(app, db, config, log) {
40+
var installer;
41+
42+
if (config.paths["serverInstallerLib"]) {
43+
var Installer = require(config.paths.serverInstallerLib);
44+
installer = new Installer(db, config, log);
45+
} else {
46+
return log.e("Invalid path or no path specified for paths.serverInstallerLib in the configuration object.");
47+
}
48+
49+
installer.uninstall(function (err, results) {
50+
if (err) {
51+
return log.e(err);
52+
}
53+
54+
if(results) {
55+
log.s("Server uninstalled successfully!");
56+
}
3457
});
3558
},
3659

3760
start: function(config, next) {
38-
var installServer = this.install;
61+
var installServer = this.install,
62+
uninstallServer = this.uninstall;
3963

4064
// Get the arguments from commandline.
4165
var args = process.argv.slice(2);
@@ -51,6 +75,8 @@ var server = {
5175
// Handle install flag in arguments.
5276
if(args.indexOf('-i') > -1) {
5377
installServer(app, db, config, fox.log);
78+
} else if(args.indexOf('-u') > -1) {
79+
uninstallServer(app, db, config, fox.log);
5480
}
5581

5682
});

server/app/install/install_module.js

Lines changed: 119 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,61 @@ var Installer = function(_db, _config, _log) {
5757
* ******************** Public Methods
5858
* ************************************************** */
5959

60+
/**
61+
* Check if the server's database has already been initialized
62+
* with data on a previous server install.
63+
* @param db is the mongoose database object.
64+
* @param cb is a callback method where the results or errors are returned.
65+
*/
66+
var isInstalled = function(db, cb) {
67+
db.model("ServerEvent").findOne({ "what": "install"}).exec(function(err, event) {
68+
if(err) {
69+
return cb(err);
70+
}
71+
72+
cb(undefined, (event));
73+
});
74+
};
75+
76+
/**
77+
* Save an install event to the server's current database. This should be
78+
* called after a successful install.
79+
* @param db is the mongoose database object.
80+
* @param cb is a callback method where the results or errors are returned.
81+
*/
82+
var saveInstallEvent = function(db, cb) {
83+
var Event = db.model("ServerEvent");
84+
var event = new Event({
85+
"what":"install"
86+
});
87+
88+
event.save(cb);
89+
};
90+
91+
/**
92+
* Remove all install events from the server's current database. This should
93+
* be called after a successful uninstall.
94+
* @param db is the mongoose database object.
95+
* @param cb is a callback method where the results or errors are returned.
96+
*/
97+
var removeInstallEvents = function(db, cb) {
98+
cb = (cb) ? cb : function(err) { if(err) { console.log(err); } };
99+
100+
db.model("ServerEvent").find().exec(function(err, events) {
101+
if(err) {
102+
return cb(err);
103+
}
104+
105+
if(events) {
106+
for (var i = events.length-1; i >= 0; --i) {
107+
events[i].remove();
108+
}
109+
}
110+
111+
cb(undefined, "Removed all saved install events.");
112+
});
113+
};
114+
60115
/**
61116
* Setup the server to run by adding any initalization
62117
* data to the data store, creating files or folder, and/or
@@ -67,22 +122,39 @@ var install = function(cb) {
67122
var db = this.db;
68123
var config = this.config;
69124
var log = this.log;
125+
var saveInstallEvent = this.saveInstallEvent;
70126

71-
async.series([
72-
createDirectories(config, [config.paths.clientAssetsImgUploadsFolder], log),
73-
updateLanguages(db, config, log),
74-
updateCountries(db, config, log),
75-
installData(db, config, "AndroidCategory", "name", undefined, undefined, log),
76-
installData(db, config, "IosCategory", "name", undefined, undefined, log),
77-
installData(db, config, "SdlVersion", "version", undefined, undefined, log),
78-
installData(db, config, "UserRole", "name", undefined, undefined, log),
79-
installData(db, config, "User", "name", undefined, { "password": config.installKey, "securityAnswer": config.installKey }, log),
80-
generateAccessTokens(db, config, log),
81-
installData(db, config, "Category", "name", undefined, undefined, log),
82-
installData(db, config, "HmiLevel", "name", undefined, undefined, log),
83-
installData(db, config, "Language", "language", "languages.js", undefined, log),
84-
installData(db, config, "Country", "country", "countries.js", undefined, log)
85-
], cb);
127+
this.isInstalled(db, function(err, isServerInstalled) {
128+
if (err) {
129+
return cb(err);
130+
}
131+
132+
if (isServerInstalled) {
133+
return cb();
134+
}
135+
136+
async.series([
137+
createDirectories(config, [config.paths.clientAssetsImgUploadsFolder], log),
138+
updateLanguages(db, config, log),
139+
updateCountries(db, config, log),
140+
installData(db, config, "AndroidCategory", "name", undefined, undefined, log),
141+
installData(db, config, "IosCategory", "name", undefined, undefined, log),
142+
installData(db, config, "SdlVersion", "version", undefined, undefined, log),
143+
installData(db, config, "UserRole", "name", undefined, undefined, log),
144+
installData(db, config, "User", "name", undefined, { "password": config.installKey, "securityAnswer": config.installKey }, log),
145+
generateAccessTokens(db, config, log),
146+
installData(db, config, "Category", "name", undefined, undefined, log),
147+
installData(db, config, "HmiLevel", "name", undefined, undefined, log),
148+
installData(db, config, "Language", "language", "languages.js", undefined, log),
149+
installData(db, config, "Country", "country", "countries.js", undefined, log)
150+
], function(err, results) {
151+
cb(err, results);
152+
153+
if( ! err) {
154+
saveInstallEvent(db);
155+
}
156+
});
157+
});
86158
};
87159

88160
/**
@@ -95,19 +167,36 @@ var uninstall = function(cb) {
95167
var db = this.db;
96168
var config = this.config;
97169
var log = this.log;
170+
var removeInstallEvents = this.removeInstallEvents;
98171

99-
async.series([
100-
uninstallData(db, config, "AndroidCategory", "name", undefined, log),
101-
uninstallData(db, config, "IosCategory", "name", undefined, log),
102-
uninstallData(db, config, "SdlVersion", "version", undefined, log),
103-
uninstallData(db, config, "UserRole", "name", undefined, log),
104-
removeAccessTokens(db, config, log),
105-
uninstallData(db, config, "User", "name", undefined, log),
106-
uninstallData(db, config, "Category", "name", undefined, log),
107-
uninstallData(db, config, "HmiLevel", "name", undefined, log),
108-
uninstallData(db, config, "Language", "language", "languages.js", log),
109-
uninstallData(db, config, "Country", "country", "countries.js", log)
110-
], cb);
172+
this.isInstalled(db, function(err, isServerInstalled) {
173+
if (err) {
174+
return cb(err);
175+
}
176+
177+
if ( ! isServerInstalled) {
178+
return cb();
179+
}
180+
181+
async.series([
182+
uninstallData(db, config, "AndroidCategory", "name", undefined, log),
183+
uninstallData(db, config, "IosCategory", "name", undefined, log),
184+
uninstallData(db, config, "SdlVersion", "version", undefined, log),
185+
uninstallData(db, config, "UserRole", "name", undefined, log),
186+
removeAccessTokens(db, config, log),
187+
uninstallData(db, config, "User", "name", undefined, log),
188+
uninstallData(db, config, "Category", "name", undefined, log),
189+
uninstallData(db, config, "HmiLevel", "name", undefined, log),
190+
uninstallData(db, config, "Language", "language", "languages.js", log),
191+
uninstallData(db, config, "Country", "country", "countries.js", log)
192+
], function (err, results) {
193+
cb(err, results);
194+
195+
if (!err) {
196+
removeInstallEvents(db);
197+
}
198+
});
199+
});
111200
};
112201

113202
/**
@@ -1042,6 +1131,9 @@ Installer.prototype.installDemo = installDemo;
10421131
Installer.prototype.uninstallDemo = uninstallDemo;
10431132
Installer.prototype.purgeDemoInstall = purgeDemoInstall;
10441133

1134+
Installer.prototype.isInstalled = isInstalled;
1135+
Installer.prototype.saveInstallEvent = saveInstallEvent;
1136+
Installer.prototype.removeInstallEvents = removeInstallEvents;
10451137

10461138
/* ************************************************** *
10471139
* ******************** Export the Public API

0 commit comments

Comments
 (0)