Skip to content

Commit

Permalink
UPLOAD BUNDLES BundleId refactoringgit diff
Browse files Browse the repository at this point in the history
  • Loading branch information
Yasar icli committed Feb 25, 2016
1 parent 114115d commit e395d92
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 58 deletions.
10 changes: 5 additions & 5 deletions collections/applications.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ Applications.attachSchema(new SimpleSchema({

// ENVIRONMENT VARIABLES
env: { type: Object },
'env.ROOT_URL': { type: String, regEx: SimpleSchema.RegEx.Url },
'env.ROOT_URL': { type: String, regEx: SimpleSchema.RegEx.Url, unique: true },
'env.MONGO_URL': { type: String, optional: true },
'env.MAIL_URL': { type: String, optional: true },
'env.PORT': { type: Number, optional: true },
'env.DISABLE_WEBSOCKETS': { type: Number, optional: true},
'env.DISABLE_WEBSOCKETS': { type: Number, optional: true },
'env.MONGO_OPLOG_URL': { type: String, optional: true },

bundleId: {
Expand Down Expand Up @@ -100,12 +100,12 @@ Applications.helpers({
isServer(() => {
Applications.helpers({
dir() {
return `${process.env.BUNDLE_DIR}/${this._id}`;
return `${process.env.BUNDLE_DIR}/${this.bundleId}`;
},

options(PORT) {
return {
name: this._id,
name: this.bundleId,
script: 'main.js',
cwd: this.dir(),
env: _.extend(this.env, {
Expand All @@ -116,7 +116,7 @@ isServer(() => {
});

Applications.before.insert((userId, doc) => {
doc.env.MONGO_URL = `mongodb://localhost:27017/${doc._id}`;
doc.env.MONGO_URL = `mongodb://localhost:27017/${doc.bundleId}`;
});

Applications.after.remove((userId, doc) => {
Expand Down
82 changes: 35 additions & 47 deletions collections/bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Bundles = new FS.Collection("bundles", {

// New Name file _id.
fileKeyMaker(fileObj) {
return fileObj._id;
return `${fileObj._id}.tar.gz`;
}
})
],
Expand All @@ -18,61 +18,49 @@ Bundles = new FS.Collection("bundles", {
});

isServer(() => {
Bundles.on('uploaded', Meteor.bindEnvironment((file, storeName) => {
Meteor.setTimeout(() => {
const application = Applications.findOne({
bundleId: file._id
});

// IF APPLICATION NOT FOUND THEN NOT RETURN!
if (_.isUndefined(application)) {
// AUTOFORM UPLOAD FIELD UNIQ BUG!
return;
}

// UPDATE STATUS PROGRESS
application.setStatus(1);
Bundles.on('stored', Meteor.bindEnvironment((file, storeName) => {

// CD BUNDLE DIR
shell.cd(process.env.BUNDLE_DIR);
// CD BUNDLES DIR
shell.cd(`${process.env.BUNDLE_DIR}`);

// REMOVE OLD APPLICATION
shell.rm('-rf', application._id);
// REMOVE OLD APPLICATION
shell.rm('-rf', file._id);

// CREATE NEW APPLICATION DIR
shell.mkdir(application._id);
// CREATE NEW APPLICATION DIR
shell.mkdir(file._id);

// EXTRACT
shell.exec(`tar -xvzf ${file._id} -C ${application._id} --strip 1`);
// EXTRACT
shell.exec(`tar -xvzf ${file._id}.tar.gz -C ${file._id} --strip 1`);

// CD SERVER PACKAGES
shell.cd(`${application.dir()}/programs/server`);
// CD SERVER PACKAGES
shell.cd(`${file._id}/programs/server`);

// NPM CORE PACKAGES INSTALL
const npm = shell.exec('npm install', ASYNC_EXEC_OPTIONS);
// NPM CORE PACKAGES INSTALL
const npm = shell.exec('npm install', ASYNC_EXEC_OPTIONS);

npm.stdout.on('end', Meteor.bindEnvironment(() => {

// FIX BCRYPT
if (shell.test('-e', 'npm/npm-bcrypt')) {
shell.exec('npm install bcrypt', SYNC_EXEC_OPTIONS);
shell.rm('-rf', 'npm/npm-bcrypt');
}
npm.stdout.on('end', Meteor.bindEnvironment(() => {
const application = Applications.findOne({
bundleId: file._id
});

// FIX BSON
if (shell.test('-e', 'npm/cfs_gridfs')) {
shell.cd('npm/cfs_gridfs/node_modules/mongodb/node_modules/bson');
shell.exec('make', SYNC_EXEC_OPTIONS);
}
// FIX BCRYPT
if (shell.test('-e', 'npm/npm-bcrypt')) {
shell.exec('npm install bcrypt', SYNC_EXEC_OPTIONS);
shell.rm('-rf', 'npm/npm-bcrypt');
}

// FIX BSON
if (shell.test('-e', 'npm/cfs_gridfs')) {
shell.cd('npm/cfs_gridfs/node_modules/mongodb/node_modules/bson');
shell.exec('make', SYNC_EXEC_OPTIONS);
}

/*
* IMPORTANT!!!!
* After installation is completed it will be updated before
* the start if the application fix begin.
*/
application.setStatus(3);
}));
}, 1000);
/*
* IMPORTANT!!!!
* After installation is completed it will be updated before
* the start if the application fix begin.
*/
application.setStatus(3);
}));
}));
});
14 changes: 11 additions & 3 deletions server/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@ Meteor.methods({
if (application) {
Applications.remove(application._id, () => {
pm2.connect((connect_err) => {
pm2.delete(application._id, (delete_err) => {
pm2.delete(application.bundleId, (delete_err) => {

// CD BUNDLES DIR
shell.cd(`${process.env.BUNDLE_DIR}`);

// REMOVE APPLICATON DIR AND BUNDLE FILE
shell.rm('-rf', [ application._id, application.bundleId ]);
shell.rm('-rf', [

// DIR
application.bundleId,

// TAR.GZ
`${application.bundleId}.tar.gz`
]);

// DISCONNECT
pm2.disconnect();
Expand All @@ -49,7 +57,7 @@ Meteor.methods({
const application = Applications.findOne(_id);

pm2.connect((connect_err) => {
pm2.stop(application._id, (delete_err) => {
pm2.stop(application.bundleId, (delete_err) => {

// DISCONNECT
pm2.disconnect();
Expand Down
7 changes: 4 additions & 3 deletions server/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Meteor.startup(() => {
const { name, monit } = proc;

// UPDATE MONITORING
Applications.update(name, {
Applications.update({ bundleId: name }, {
$set: {
monit
}
Expand All @@ -35,7 +35,7 @@ Meteor.startup(() => {
const { PORT, name } = query.process;

// SET STATUS QUERY EVENT
Applications.update(name, {
Applications.update({ bundleId: name }, {
$set: {
status: STATUS_MAPPER[query.event.toUpperCase()],
'env.PORT': PORT
Expand All @@ -46,10 +46,11 @@ Meteor.startup(() => {
// IF ERROR THEN ON EVENT
bus.on('log:err', Meteor.bindEnvironment((query) => {
const { name } = query.process;
const application = Applications.findOne({ bundleId: name });

// INSERT ERROR LOG
Logs.insert({
applicationId: name,
applicationId: application._id,
type: STATUS_ALLOWED_VALUES[4],
data: query.data
});
Expand Down

0 comments on commit e395d92

Please sign in to comment.