forked from jhipster/generator-jhipster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
488 lines (443 loc) · 16.4 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/**
* Copyright 2013-2021 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const fs = require('fs');
const exec = require('child_process').exec;
const chalk = require('chalk');
const BaseBlueprintGenerator = require('../generator-base-blueprint');
const statistics = require('../statistics');
const constants = require('../generator-constants');
const cacheTypes = require('../../jdl/jhipster/cache-types');
const { MEMCACHED } = require('../../jdl/jhipster/cache-types');
const NO_CACHE_PROVIDER = cacheTypes.NO;
const { MAVEN } = require('../../jdl/jhipster/build-tool-types');
const { GENERATOR_AZURE_SPRING_CLOUD } = require('../generator-list');
let useBlueprints;
/* eslint-disable consistent-return */
module.exports = class extends BaseBlueprintGenerator {
constructor(args, opts) {
super(args, opts);
this.option('skip-build', {
desc: 'Skips building the application',
type: Boolean,
defaults: false,
});
this.option('skip-deploy', {
desc: 'Skips deployment to Azure Spring Cloud',
type: Boolean,
defaults: false,
});
this.azureSpringCloudSkipBuild = this.options.skipBuild;
this.azureSpringCloudSkipDeploy = this.options.skipDeploy || this.options.skipBuild;
useBlueprints = !this.fromBlueprint && this.instantiateBlueprints(GENERATOR_AZURE_SPRING_CLOUD);
}
_initializing() {
return {
sayHello() {
if (!this.options.fromCli) {
this.warning(
`Deprecated: JHipster seems to be invoked using Yeoman command. Please use the JHipster CLI. Run ${chalk.red(
'jhipster <command>'
)} instead of ${chalk.red('yo jhipster:<command>')}`
);
}
this.log(chalk.bold('Azure Spring Cloud configuration is starting'));
},
getSharedConfig() {
this.loadAppConfig();
this.loadServerConfig();
this.loadPlatformConfig();
},
getConfig() {
this.env.options.appPath = this.config.get('appPath') || constants.CLIENT_MAIN_SRC_DIR;
this.cacheProvider = this.cacheProvider || NO_CACHE_PROVIDER;
this.enableHibernateCache = this.enableHibernateCache && ![NO_CACHE_PROVIDER, MEMCACHED].includes(this.cacheProvider);
this.frontendAppName = this.getFrontendAppName();
this.azureSpringCloudResourceGroupName = ''; // This is not saved, as it is better to get the Azure default variable
this.azureSpringCloudServiceName = ''; // This is not saved, as it is better to get the Azure default variable
this.azureSpringCloudAppName = this.config.get('azureSpringCloudAppName');
this.azureSpringCloudDeploymentType = this.config.get('azureSpringCloudDeploymentType');
},
};
}
get initializing() {
if (useBlueprints) return;
return this._initializing();
}
_prompting() {
return {
checkBuildTool() {
if (this.abort) return;
const done = this.async();
if (this.buildTool !== MAVEN) {
this.log.error('Sorry, this sub-generator only works with Maven projects for the moment.');
this.abort = true;
}
done();
},
checkInstallation() {
if (this.abort) return;
const done = this.async();
exec('az --version', err => {
if (err) {
this.log.error(
`You don't have the Azure CLI installed.
Download it from:
${chalk.red('https://docs.microsoft.com/en-us/cli/azure/install-azure-cli/?WT.mc_id=generator-jhipster-judubois')}`
);
this.abort = true;
}
done();
});
},
checkExtensionInstallation() {
if (this.abort) return;
const done = this.async();
exec('az extension show --name spring-cloud', err => {
if (err) {
this.log.error(
`You don't have the Azure Spring Cloud extension installed in your Azure CLI.
Install it by running:
${chalk.red('az extension add --name spring-cloud')}`
);
this.abort = true;
}
done();
});
},
checkClusterAvailability() {
if (this.abort) return;
const done = this.async();
exec('az spring-cloud app list', err => {
if (err) {
this.log.error(`${chalk.red('Your Azure Spring Cloud cluster is not available!')}\n ${err}`);
this.abort = true;
}
done();
});
},
getAzureSpringCloudDefaults() {
if (this.abort) return;
const done = this.async();
exec('az configure --list-defaults true', (err, stdout) => {
if (err) {
this.config.set({
azureSpringCloudResourceGroupName: null,
});
this.abort = true;
this.log.error('Could not retrieve your Azure default configuration.');
} else {
const json = JSON.parse(stdout);
Object.keys(json).forEach(key => {
if (json[key].name === 'group') {
this.azureSpringCloudResourceGroupName = json[key].value;
}
if (json[key].name === 'spring-cloud') {
this.azureSpringCloudServiceName = json[key].value;
}
});
if (this.azureSpringCloudResourceGroupName === '') {
this.log.info(
`Your default Azure resource group is not set up. We recommend doing it using the command
'${chalk.yellow('az configure --defaults group=<resource group name>')}`
);
this.azureSpringCloudResourceGroupName = '';
}
if (this.azureSpringCloudServiceName === '') {
this.log.info(
`Your default Azure Spring Cloud service name is not set up. We recommend doing it using the command
'${chalk.yellow('az configure --defaults spring-cloud=<service instance name>')}`
);
this.azureSpringCloudServiceName = '';
}
}
done();
});
},
askForazureSpringCloudVariables() {
if (this.abort) return;
const done = this.async();
const prompts = [
{
type: 'input',
name: 'azureSpringCloudResourceGroupName',
message: 'Azure resource group name:',
default: this.azureSpringCloudResourceGroupName,
},
{
type: 'input',
name: 'azureSpringCloudServiceName',
message: 'Azure Spring Cloud service name (the name of your cluster):',
default: this.azureSpringCloudServiceName,
},
{
type: 'input',
name: 'azureSpringCloudAppName',
message: 'Azure Spring Cloud application name:',
default: this.azureSpringCloudAppName || this.baseName,
},
];
this.prompt(prompts).then(props => {
this.azureSpringCloudResourceGroupName = props.azureSpringCloudResourceGroupName;
this.azureSpringCloudServiceName = props.azureSpringCloudServiceName;
this.azureSpringCloudAppName = props.azureSpringCloudAppName;
done();
});
},
askForAzureDeployType() {
if (this.abort) return;
const done = this.async();
const prompts = [
{
type: 'list',
name: 'azureSpringCloudDeploymentType',
message: 'Which type of deployment do you want ?',
choices: [
{
value: 'local',
name: 'Build and deploy locally',
},
{
value: 'github-action',
name: 'Build and deploy using GitHub Actions',
},
],
default: 0,
},
];
this.prompt(prompts).then(props => {
this.azureSpringCloudDeploymentType = props.azureSpringCloudDeploymentType;
done();
});
},
};
}
get prompting() {
if (useBlueprints) return;
return this._prompting();
}
_configuring() {
return {
saveConfig() {
if (this.abort) return;
this.config.set({
azureSpringCloudAppName: this.azureSpringCloudAppName,
azureSpringCloudDeploymentType: this.azureSpringCloudDeploymentType,
});
},
};
}
get configuring() {
if (useBlueprints) return;
return this._configuring();
}
_default() {
return {
insight() {
statistics.sendSubGenEvent('generator', GENERATOR_AZURE_SPRING_CLOUD);
},
azureSpringCloudAppCreate() {
if (this.abort) return;
const done = this.async();
exec(
`az spring-cloud app show --resource-group ${this.azureSpringCloudResourceGroupName} \
--service ${this.azureSpringCloudServiceName} --name ${this.azureSpringCloudAppName}`,
(err, stdout) => {
if (err) {
this.log(chalk.bold('Application does not exist yet, creating it...'));
exec(
`az spring-cloud app create --resource-group ${this.azureSpringCloudResourceGroupName} \
--service ${this.azureSpringCloudServiceName} --name ${this.azureSpringCloudAppName}`,
(err, stdout) => {
if (err) {
this.abort = true;
this.log.error(`Application creation failed! Here is the error: ${err}`);
} else {
this.log(`${chalk.green(chalk.bold('Success!'))} Your application has been created.`);
}
done();
}
);
} else {
this.log(chalk.bold('Application already exists, using it.'));
done();
}
}
);
},
};
}
get default() {
if (useBlueprints) return;
return this._default();
}
_loading() {
return {
derivedProperties() {
this.isPackageNameJhipsterTech = this.packageName !== 'tech.jhipster';
this.loadDerivedServerConfig();
this.loadDerivedPlatformConfig();
this.loadDerivedAppConfig();
},
};
}
get loading() {
if (useBlueprints) return;
return this._loading();
}
_writing() {
return {
copyAzureSpringCloudFiles() {
if (this.abort) return;
this.log(chalk.bold('\nCreating Azure Spring Cloud deployment files'));
this.template('application-azure.yml.ejs', `${constants.SERVER_MAIN_RES_DIR}/config/application-azure.yml`);
this.template('bootstrap-azure.yml.ejs', `${constants.SERVER_MAIN_RES_DIR}/config/bootstrap-azure.yml`);
if (this.azureSpringCloudDeploymentType === 'github-action') {
this.template('github/workflows/azure-spring-cloud.yml.ejs', '.github/workflows/azure-spring-cloud.yml');
}
},
addAzureSpringCloudMavenProfile() {
if (this.abort) return;
if (this.buildTool === MAVEN) {
this.render('pom-profile.xml.ejs', profile => {
this.addMavenProfile('azure', ` ${profile.toString().trim()}`);
});
}
},
};
}
get writing() {
if (useBlueprints) return;
return this._writing();
}
_end() {
return {
gitHubAction() {
if (this.abort) return;
if (this.azureSpringCloudDeploymentType === 'local') return;
const done = this.async();
try {
this.log('Test if Git is configured on your project...');
fs.lstatSync('.git');
this.log(chalk.bold('\nUsing existing Git repository'));
} catch (e) {
// An exception is thrown if the folder doesn't exist
this.log.error(
`${chalk.red('Git is not set up on your project!')}
You need a GitHub project correctly configured in order to use GitHub Actions.`
);
this.abort = true;
return;
}
const gitAddCmd = 'git add .';
this.log(chalk.bold('\nAdding Azure Spring Cloud files to the Git repository'));
this.log(chalk.cyan(gitAddCmd));
exec(gitAddCmd, (err, stdout, stderr) => {
if (err) {
this.abort = true;
this.log.error(err);
} else {
const line = stderr.toString().trimRight();
if (line.trim().length !== 0) this.log(line);
this.log(chalk.bold('\nCommitting Azure Spring Cloud files'));
const gitCommitCmd = 'git commit -m "Add Azure Spring Cloud files with automated GitHub Action deployment" --allow-empty';
this.log(chalk.cyan(gitCommitCmd));
exec(gitCommitCmd, (err, stdout, stderr) => {
if (err) {
this.abort = true;
this.log.error(err);
} else {
const line = stderr.toString().trimRight();
if (line.trim().length !== 0) this.log(line);
this.log(chalk.bold('\nPushing Azure Spring Cloud files'));
const gitPushCmd = 'git push';
this.log(chalk.cyan(gitPushCmd));
exec(gitPushCmd, (err, stdout, stderr) => {
if (err) {
this.abort = true;
this.log.error(err);
} else {
const line = stderr.toString().trimRight();
if (line.trim().length !== 0) this.log(line);
this.log(chalk.bold(chalk.green('Congratulations, automated deployment with GitHub Action is set up!')));
this.log(
`For the deployment to succeed, you will need to configure a ${chalk.bold('AZURE_CREDENTIALS')} secret in GitHub.
Read the documentation at https://github.com/microsoft/azure-spring-cloud-training/blob/master/11-configure-ci-cd/README.md
for more detailed information.`
);
done();
}
});
}
});
}
});
},
productionBuild() {
if (this.abort) return;
if (this.azureSpringCloudDeploymentType === 'github-action') return;
if (this.azureSpringCloudSkipBuild) return;
const done = this.async();
this.log(chalk.bold('\nBuilding application'));
const child = this.buildApplication(this.buildTool, 'prod,azure', false, err => {
if (err) {
this.abort = true;
this.log.error(err);
}
done();
});
this.buildCmd = child.buildCmd;
child.stdout.on('data', data => {
process.stdout.write(data.toString());
});
},
productionDeploy() {
if (this.abort) return;
if (this.azureSpringCloudDeploymentType === 'github-action') return;
if (this.azureSpringCloudSkipDeploy) return;
const done = this.async();
this.log(chalk.bold('\nDeploying application...'));
let buildDir = 'target';
if (this.buildTool === 'gradle') {
buildDir = 'build/libs';
}
exec(
`az spring-cloud app deploy --resource-group ${this.azureSpringCloudResourceGroupName} \
--service ${this.azureSpringCloudServiceName} --name ${this.azureSpringCloudAppName} \
--jar-path ${buildDir}/*.jar`,
(err, stdout) => {
if (err) {
this.abort = true;
this.log.error(`Deployment failed!\n ${err}`);
} else {
const json = JSON.parse(stdout);
this.log(`${chalk.green(chalk.bold('Success!'))} Your application has been deployed.`);
this.log(`Provisioning state: ${chalk.bold(json.properties.provisioningState)}`);
this.log(`Application status : ${chalk.bold(json.properties.status)}`);
}
done();
}
);
},
};
}
get end() {
if (useBlueprints) return;
return this._end();
}
};