gen-jhipster
Version:
Spring Boot + Angular/React/Vue in one handy generator
194 lines (193 loc) • 8.45 kB
JavaScript
// @ts-nocheck
/**
* Copyright 2013-2024 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.
*/
import fs from 'fs';
import chalk from 'chalk';
import BaseWorkspacesGenerator from '../base-workspaces/index.js';
import prompts from '../kubernetes/prompts.js';
import { checkImages, configureImageNames, generateJwtSecret, loadFromYoRc } from '../base-workspaces/internal/docker-base.js';
import { checkHelm, checkKubernetes, derivedKubernetesPlatformProperties, loadConfig, setupHelmConstants, setupKubernetesConstants, } from '../kubernetes/kubernetes-base.js';
import { messageBrokerTypes } from '../../lib/jhipster/index.js';
import { getJdbcUrl, getR2dbcUrl } from '../spring-data-relational/support/index.js';
import { loadDeploymentConfig, loadDockerDependenciesTask } from '../base-workspaces/internal/index.js';
import { checkDocker } from '../docker/support/index.js';
import { loadDerivedServerConfig } from '../server/support/index.js';
import { loadDerivedAppConfig } from '../app/support/index.js';
import { writeFiles } from './files.js';
const { KAFKA } = messageBrokerTypes;
/**
* @class
* @extends {BaseWorkspacesGenerator}
*/
export default class KubernetesHelmGenerator extends BaseWorkspacesGenerator {
async beforeQueue() {
if (!this.fromBlueprint) {
await this.composeWithBlueprints();
}
}
get initializing() {
return {
sayHello() {
this.log.log(chalk.white(`${chalk.bold('⎈')} Welcome to the JHipster Kubernetes Helm Generator ${chalk.bold('⎈')}`));
this.log.log(chalk.white(`Files will be generated in folder: ${chalk.yellow(this.destinationRoot())}`));
},
existingDeployment() {
this.regenerate = this.regenerate || this.config.existed;
},
loadDockerDependenciesTask,
checkDocker,
checkKubernetes,
checkHelm,
loadConfig,
setupKubernetesConstants,
setupHelmConstants,
};
}
get [BaseWorkspacesGenerator.INITIALIZING]() {
return this.delegateTasksToBlueprint(() => this.initializing);
}
get prompting() {
return {
askForApplicationType: prompts.askForApplicationType,
askForPath: prompts.askForPath,
askForApps: prompts.askForApps,
askForMonitoring: prompts.askForMonitoring,
askForClustersMode: prompts.askForClustersMode,
askForServiceDiscovery: prompts.askForServiceDiscovery,
askForAdminPassword: prompts.askForAdminPassword,
askForKubernetesNamespace: prompts.askForKubernetesNamespace,
askForDockerRepositoryName: prompts.askForDockerRepositoryName,
askForDockerPushCommand: prompts.askForDockerPushCommand,
askForIstioSupport: prompts.askForIstioSupport,
askForKubernetesServiceType: prompts.askForKubernetesServiceType,
askForIngressType: prompts.askForIngressType,
askForIngressDomain: prompts.askForIngressDomain,
};
}
get [BaseWorkspacesGenerator.PROMPTING]() {
return this.delegateTasksToBlueprint(() => this.prompting);
}
get configuring() {
return {
generateJwtSecret,
};
}
get [BaseWorkspacesGenerator.CONFIGURING]() {
return this.delegateTasksToBlueprint(() => this.configuring);
}
get loading() {
return {
loadFromYoRc,
loadSharedConfig() {
for (const app of this.appConfigs) {
loadDerivedAppConfig({ application: app });
loadDerivedServerConfig({ application: app });
}
loadDeploymentConfig.call(this);
derivedKubernetesPlatformProperties(this);
},
};
}
get [BaseWorkspacesGenerator.LOADING]() {
return this.delegateTasksToBlueprint(() => this.loading);
}
get preparing() {
return {
configureImageNames,
setPostPromptProp() {
this.appConfigs.forEach(element => {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
element.clusteredDb ? (element.dbPeerCount = 3) : (element.dbPeerCount = 1);
if (element.messageBroker === KAFKA) {
this.useKafka = true;
}
});
this.useKeycloak = false;
},
};
}
get [BaseWorkspacesGenerator.PREPARING]() {
return this.delegateTasksToBlueprint(() => this.preparing);
}
get writing() {
return writeFiles();
}
get [BaseWorkspacesGenerator.WRITING]() {
return this.delegateTasksToBlueprint(() => this.writing);
}
get end() {
return {
checkImages,
deploy() {
if (this.hasWarning) {
this.log.warn('Helm configuration generated, but no Jib cache found');
this.log.warn('If you forgot to generate the Docker image for this application, please run:');
this.log.warn(this.warningMessage);
}
else {
this.log.verboseInfo(`\n${chalk.bold.green('Helm configuration successfully generated!')}`);
}
this.log.warn('You will need to push your image to a registry. If you have not done so, use the following commands to tag and push the images:');
for (let i = 0; i < this.appsFolders.length; i++) {
const originalImageName = this.appConfigs[i].baseName.toLowerCase();
const targetImageName = this.appConfigs[i].targetImageName;
if (originalImageName !== targetImageName) {
this.log.verboseInfo(` ${chalk.cyan(`docker image tag ${originalImageName} ${targetImageName}`)}`);
}
this.log.verboseInfo(` ${chalk.cyan(`${this.dockerPushCommand} ${targetImageName}`)}`);
}
this.log.log('\nYou can deploy all your apps by running the following script:');
this.log.verboseInfo(` ${chalk.cyan('bash helm-apply.sh')}`);
this.log.log('\nYou can upgrade (after any changes) all your apps by running the following script:');
this.log.verboseInfo(` ${chalk.cyan('bash helm-upgrade.sh')}`);
// Make the apply script executable
try {
fs.chmodSync('helm-apply.sh', '755');
fs.chmodSync('helm-upgrade.sh', '755');
}
catch {
this.log.warn("Failed to make 'helm-apply.sh', 'helm-upgrade.sh' executable, you may need to run 'chmod +x helm-apply.sh helm-upgrade.sh");
}
},
};
}
get [BaseWorkspacesGenerator.END]() {
return this.delegateTasksToBlueprint(() => this.end);
}
/**
* @private
* Returns the JDBC URL for a databaseType
*
* @param {string} databaseType
* @param {*} options: databaseName, and required infos that depends of databaseType (hostname, localDirectory, ...)
*/
getJDBCUrl(databaseType, options = {}) {
return getJdbcUrl(databaseType, options);
}
/**
* @private
* Returns the R2DBC URL for a databaseType
*
* @param {string} databaseType
* @param {*} options: databaseName, and required infos that depends of databaseType (hostname, localDirectory, ...)
*/
getR2DBCUrl(databaseType, options = {}) {
return getR2dbcUrl(databaseType, options);
}
}