-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject.js
More file actions
234 lines (207 loc) · 7.08 KB
/
Project.js
File metadata and controls
234 lines (207 loc) · 7.08 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
require("./Model");
var mongoose = require('mongoose');
var fs = require("fs");
var async = require("async");
var FILEMODE = 0700;
var config = require("../config");
var workingDir = config.workspaceDir;
var model = mongoose.model("Project");
var Clone = require("../clone");
var loggly = require("loggly");
var global = require("../global");
var logger = loggly.createClient({
subdomain : "stackexpress"
});
var Project = function(args) {
this.clients = [];
if(args.projectId) {
_ExistingProject.apply(this, arguments);
} else {
_NewProject.apply(this, arguments);
}
};
//existing project constructor
var _ExistingProject = function(args) {
var self = this;
async.waterfall([self._projectTasks.projectMustExist.bind(self, args.projectId, null, null)], function(err) {
args.ready(err);
})
};
//new project constructor
var _NewProject = function(args) {
var self = this;
async.waterfall([self._projectTasks.createUniqueProject.bind(self, args.namespace || "", args.framework, args.clone || args.framework), self._projectTasks.cloneProjectFiles.bind(self), self._projectTasks.pushProject.bind(self)], function(err) {
args.ready(err);
})
};
(function() {
this.errorHandler = function(userId, command, msg) {
};
this.successHandler = function(userId, command, payload) {
};
this.addClient = function(client) {
this.clients.push(client);
};
this.removeClient = function(client) {
}
this.getId = function() {
return this.model.projectId;
};
this.getFullName = function() {
return this.model.fullName;
};
this.getTitle = function() {
return this.model.projectTitle || "Untitled-Project";
};
this.getFramework = function() {
return this.model.framework;
};
this.getPath = function() {
return this.model.path;
};
this.getRoot = function() {
return this.model.root;
};
this.getUrl = function() {
return this.model.url;
};
//workspace tasks to be used by async waterfall
this._projectTasks = {
createUniqueProject : function(namespace, framework, root, callback) {
console.log("Creating project");
var _self = this;
var proj = new model({
"namespace" : namespace,
"root" : root,
"framework" : framework
});
proj.save(function(err, doc) {
console.log(err);
if(err) {
callback("Couldn't create unique project", namespace);
} else {
_self.model = doc;
callback(null);
}
})
},
projectMustExist : function(projectId, namespace, projectTitle, callback) {
var _self = this;
var searchParams = {};
if(projectId) {
searchParams = {
"projectId" : projectId
};
} else {
searchParams = {
"namespace" : namespace,
"projectTitle" : projectTitle
};
}
model.findOne(searchParams, function(err, doc) {
if(err || !doc) {
callback("Project doesn't exist", searchParams);
} else {
_self.model = doc;
callback(null);
}
});
},
addWorkspaceFS : function(callback) {
var self = this;
fs.mkdir(self.getPath(), FILEMODE, function(err) {
var res = err ? "I/O error while creating workspace " + self.getId() : null;
callback(res);
});
},
cloneProjectFiles : function(callback) {
var clone = new Clone(config.workspaceDir + this.getRoot() + "", this.getPath());
console.log("cloning to: " + this.getPath());
var self = this;
clone.on("complete", function() {
callback(null);
})
clone.on("error", function(err) {
logger.log(config.logger.errors, JSON.stringify({
action : "cloneProject",
error : err
}));
callback(err);
});
},
startProject : function(callback) {
var self = this;
var pingAttempts = 0;
function waitForStart(err) {
if(err) {
callback(err);
logger.log(config.logger.errors, JSON.stringify({
action : "startApp",
"appUrl" : self.model.url,
error : err
}));
} else {
setTimeout(appPing, 500);
//ping app
}
};
function appPing() {
global.cloudFoundry.getAppStatus(self.getId(), function(obj) {
if(obj.status === "RUNNING") {
callback(null);
//success
} else {
if(pingAttempts > 5) {
callback("Could not start app");
} else {
pingAttempts++;
setTimeout(appPing, 500);
}
}
});
};
global.cloudFoundry.startApp(self.getId(), waitForStart);
},
pushProject : function(callback) {
var self = this;
var pingAttempts = 0;
function waitForStart(err) {
console.log("project started");
console.log(err);
if(err) {
callback(err);
logger.log(config.logger.errors, JSON.stringify({
action : "startApp",
"appUrl" : self.model.url,
error : err
}));
} else {
callback(null);
}
};
function appPing() {
global.cloudFoundry.getAppStatus(self.getId(), function(obj) {
if(obj.status === "RUNNING") {
callback(null);
//success
} else {
if(pingAttempts > 5) {
callback("Could not start app");
} else {
pingAttempts++;
setTimeout(appPing, 500);
}
}
});
};
console.log("pushing app");
global.cloudFoundry.pushNewApp({
"appName" : self.getId(),
"appUrl" : self.getUrl(),
"appDir" : self.getPath(),
"framework" : self.getFramework()
}, waitForStart);
}
}
}).call(Project.prototype);
module.exports = Project;