forked from jhipster/generator-jhipster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
317 lines (276 loc) · 12.5 KB
/
index.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
'use strict';
var util = require('util'),
path = require('path'),
yeoman = require('yeoman-generator'),
childProcess = require('child_process'),
chalk = require('chalk'),
_s = require('underscore.string'),
scriptBase = require('../script-base');
var exec = childProcess.exec;
var spawn = childProcess.spawn;
var OpenshiftGenerator = module.exports = function OpenshiftGenerator() {
yeoman.generators.Base.apply(this, arguments);
console.log(chalk.bold('Openshift configuration is starting'));
this.env.options.appPath = this.config.get('appPath') || 'src/main/webapp';
this.baseName = this.config.get('baseName');
this.packageName = this.config.get('packageName');
this.packageFolder = this.config.get('packageFolder');
this.javaVersion = this.config.get('javaVersion');
this.hibernateCache = this.config.get('hibernateCache');
this.databaseType = this.config.get('databaseType');
this.prodDatabaseType = this.config.get('prodDatabaseType');
this.angularAppName = _s.camelize(_s.slugify(this.baseName)) + 'App';
};
util.inherits(OpenshiftGenerator, yeoman.generators.Base);
util.inherits(OpenshiftGenerator, scriptBase);
OpenshiftGenerator.prototype.askForName = function askForName() {
var done = this.async();
var prompts = [{
name: 'openShiftDeployedName',
message: 'Name to deploy as:',
default: this.baseName
}];
this.prompt(prompts, function (props) {
this.openShiftDeployedName = this._.slugify(props.openShiftDeployedName).split('-').join('');
done();
}.bind(this));
};
OpenshiftGenerator.prototype.checkInstallation = function checkInstallation() {
if(this.abort) return;
var done = this.async();
exec('rhc --version', function (err) {
if (err) {
this.log.error('Openshift\'s rhc command line interface is not available. ' +
'You can install it via RubyGems with: gem install rhc');
this.abort = true;
}
done();
}.bind(this));
};
OpenshiftGenerator.prototype.dirInit = function dirInit() {
if(this.abort) return;
var done = this.async();
this.log(chalk.bold('Initializing deployment repo'));
this.mkdir('deploy/openshift');
done();
};
OpenshiftGenerator.prototype.gitRemoteCheck = function gitRemoteCheck() {
this.openshift_remote_exists = false;
if(this.abort || typeof this.dist_repo_url !== 'undefined') return;
var done = this.async();
this.log(chalk.bold("\nChecking for an existing git remote named '"+'openshift'+"'..."));
exec('git remote -v', { cwd: 'deploy/openshift' }, function (err, stdout, stderr) {
var lines = stdout.split('\n');
var dist_repo = '';
if (err && stderr.search('DL is deprecated') === -1) {
this.log.error(err);
} else {
var repo_url_finder = new RegExp('openshift'+"[ ]*");
lines.forEach(function(line) {
if(line.search(repo_url_finder) === 0 && dist_repo === '') {
var dist_repo_detailed = line.slice(line.match(repo_url_finder)[0].length);
dist_repo = dist_repo_detailed.slice(0, dist_repo_detailed.length - 7);
}
});
if (dist_repo !== ''){
console.log("Found an existing git remote for this app: "+dist_repo);
this.dist_repo_url = dist_repo;
this.openshift_remote_exists = true;
} else {
console.log('No existing remote found.');
}
}
done();
}.bind(this));
};
OpenshiftGenerator.prototype.rhcAppShow = function rhcAppShow() {
if(this.abort || typeof this.dist_repo_url !== 'undefined') return;
var done = this.async();
this.log(chalk.bold("\nChecking for an existing Openshift hosting environment..."));
var child = exec('rhc app show '+this.openShiftDeployedName+' --noprompt', { cwd: 'deploy/openshift' }, function (err, stdout, stderr) {
var lines = stdout.split('\n');
var dist_repo = '';
// Unauthenticated
if (stdout.search('Not authenticated') >= 0 || stdout.search('Invalid characters found in login') >= 0) {
this.log.error('Error: Not authenticated. Run "rhc setup" to login to your Openshift account and try again.');
this.abort = true;
}
// No remote found
else if (stderr.search('not found.') < 0) {
console.log('No existing app found.');
}
// Error
else if (err && stderr.search('DL is deprecated') === -1) {
this.log.error(err);
} else { // Remote found
this.log(stdout);
var repo_url_finder = / *Git URL: */;
lines.forEach(function(line) {
if(line.search(repo_url_finder) === 0) {
dist_repo = line.slice(line.match(repo_url_finder)[0].length);
console.log("Found an existing git remote for this app: "+dist_repo);
}
});
if (dist_repo !== '') this.dist_repo_url = dist_repo;
}
done();
}.bind(this));
};
OpenshiftGenerator.prototype.rhcAppCreate = function rhcAppCreate() {
if(this.abort || typeof this.dist_repo_url !== 'undefined') return;
var done = this.async();
this.log(chalk.bold("\nCreating your Openshift hosting environment, this may take a couple minutes..."));
var db = this.prodDatabaseType === 'postgresql' ? 'postgresql-9.2' : 'mysql-5.5';
var child = exec('rhc app create ' + this.openShiftDeployedName + ' diy-0.1 ' + db + ' -r ./', { cwd: 'deploy/openshift' }, function (err, stdout, stderr) {
var lines = stdout.split('\n');
this.log(stdout);
if (stdout.search('Not authenticated') >= 0 || stdout.search('Invalid characters found in login') >= 0) {
this.log.error('Error: Not authenticated. Run "rhc setup" to login to your Openshift account and try again.');
this.abort = true;
} else if (err && stderr.search('DL is deprecated') === -1) {
this.log.error(err);
} else {
var dist_repo = '';
var repo_url_finder = / *Git remote: */;
lines.forEach(function(line) {
if(line.search(repo_url_finder) === 0) {
dist_repo = line.slice(line.match(repo_url_finder)[0].length);
}
});
if (dist_repo !== '') this.dist_repo_url = dist_repo;
if(this.dist_repo_url !== undefined) {
this.log("New remote git repo at: "+this.dist_repo_url);
}
}
done();
}.bind(this));
child.stdout.on('data', function(data) {
this.log(data.toString());
}.bind(this));
};
OpenshiftGenerator.prototype.gitRemoteAdd = function gitRemoteAdd() {
if(this.abort || typeof this.dist_repo_url === 'undefined' || this.openshift_remote_exists) return;
var done = this.async();
this.log(chalk.bold("\nAdding remote repo url: "+this.dist_repo_url));
var child = exec('git remote add '+'openshift'+' '+this.dist_repo_url, { cwd: 'deploy/openshift' }, function (err, stdout, stderr) {
if (err) {
this.log.error(err);
} else {
this.openshift_remote_exists = true;
}
done();
}.bind(this));
child.stdout.on('data', function(data) {
this.log(data.toString());
}.bind(this));
};
OpenshiftGenerator.prototype.copyOpenshiftFiles = function copyOpenshiftFiles() {
if(this.abort || !this.openshift_remote_exists ) return;
var done = this.async();
this.log(chalk.bold('\nCreating Openshift deployment files'));
this.template('openshift/action_hooks/_build', 'deploy/openshift/.openshift/action_hooks/build', null, { 'interpolate': /<%=([\s\S]+?)%>/g });
this.template('openshift/action_hooks/_start', 'deploy/openshift/.openshift/action_hooks/start', null, { 'interpolate': /<%=([\s\S]+?)%>/g });
this.copy('openshift/action_hooks/stop', 'deploy/openshift/.openshift/action_hooks/stop');
if (this.javaVersion === "8") {
this.copy('openshift/action_hooks/pre_build', 'deploy/openshift/.openshift/action_hooks/pre_build');
}
this.conflicter.resolve(function (err) {
done();
});
};
OpenshiftGenerator.prototype.productionBuild = function productionBuild() {
if(this.abort || !this.openshift_remote_exists ) return;
var done = this.async();
this.log(chalk.bold('\nBuilding deploy/openshift folder, please wait...'));
var child = exec('grunt buildOpenshift', function (err, stdout) {
if (err) {
this.log.error(err);
}
done();
}.bind(this));
child.stdout.on('data', function(data) {
this.log(data.toString());
}.bind(this));
};
OpenshiftGenerator.prototype.gitCommit = function gitCommit() {
if(this.abort || !this.openshift_remote_exists ) return;
var done = this.async();
this.log(chalk.bold('\nAdding files for initial commit'));
var child = exec('git add -A && git commit -m "Initial commit"', { cwd: 'deploy/openshift', maxBuffer: 1000*1024 }, function (err, stdout, stderr) {
if (stdout.search('nothing to commit') >= 0) {
this.log('Re-pushing the existing "deploy/openshift" build...');
} else if (err) {
this.log.error(err);
} else {
this.log(chalk.green('Done, without errors.'));
}
done();
}.bind(this));
child.stdout.on('data', function(data) {
this.log(data.toString());
}.bind(this));
};
OpenshiftGenerator.prototype.gitChmod = function gitChmod() {
if(this.abort || !this.openshift_remote_exists ) return;
var done = this.async();
this.log(chalk.bold('\nChmod action hooks'));
var child = exec('git update-index --chmod=+x .openshift/action_hooks/build && '+
'git update-index --chmod=+x .openshift/action_hooks/start && '+
'git update-index --chmod=+x .openshift/action_hooks/stop && ' +
(this.javaVersion === "8" ? 'git update-index --chmod=+x .openshift/action_hooks/pre_build && ' : '') +
'git commit -m "Chmod"', { cwd: 'deploy/openshift' }, function (err, stdout, stderr) {
if (stdout.search('nothing to commit') >= 0) {
this.log('+x already set');
} else if (err) {
this.log.error(err);
} else {
this.log(chalk.green('Done, without errors.'));
}
done();
}.bind(this));
child.stdout.on('data', function(data) {
this.log(data.toString());
}.bind(this));
};
OpenshiftGenerator.prototype.gitForcePush = function gitForcePush() {
if (this.abort || !this.openshift_remote_exists) return;
var done = this.async();
this.log(chalk.bold("\nUploading your initial application code.\n This may take " + chalk.cyan('several minutes') + " depending on your connection speed..."));
var insight = this.insight();
insight.track('generator', 'openshift');
var push = spawn('git', ['push', '-f', 'openshift', 'master'], {cwd: 'deploy/openshift'});
var error = null;
push.stderr.on('data', function (data) {
var output = data.toString();
this.log.error(output);
}.bind(this));
push.stdout.on('data', function (data) {
var output = data.toString();
this.log.stdin(output);
}.bind(this));
push.on('exit', function (code) {
if (code !== 0) {
this.abort = true;
return done();
}
done();
}.bind(this));
};
OpenshiftGenerator.prototype.restartApp = function restartApp() {
if(this.abort || !this.openshift_remote_exists ) return;
this.log(chalk.bold("\nRestarting your Openshift app.\n"));
var child = exec('rhc app restart -a ' + this.openShiftDeployedName, function(err, stdout, stderr) {
var host_url = '';
var hasWarning = false;
var before_hostname = this.dist_repo_url.indexOf('@') + 1;
var after_hostname = this.dist_repo_url.length - ( 'openshift'.length + 12 );
host_url = 'http://' + this.dist_repo_url.slice(before_hostname, after_hostname);
this.log(chalk.green('\nYour app should now be live at \n\t' + chalk.bold(host_url)));
if(hasWarning) {
this.log(chalk.green('\nYou may need to address the issues mentioned above and restart the server for the app to work correctly \n\t' +
'rhc app-restart -a ' + this.openShiftDeployedName));
}
this.log(chalk.yellow('After application modification, re-deploy it with\n\t' + chalk.bold('grunt deployOpenshift') +
' and then do a \n\t' + chalk.bold('git push') + ' on your Openshift repository.'));
}.bind(this));
};