UNPKG

generator-begcode

Version:

Spring Boot + Angular/React/Vue in one handy generator

182 lines (181 loc) 9.1 kB
import fs from 'fs'; import chalk from 'chalk'; import BaseWorkspacesGenerator from '../base-workspaces/index.js'; import { buildToolTypes, messageBrokerTypes } from '../../lib/jhipster/index.js'; import { checkImages, configureImageNames, generateJwtSecret, loadFromYoRc } from '../base-workspaces/internal/docker-base.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 { checkKubernetes, derivedKubernetesPlatformProperties, loadConfig, setupKubernetesConstants } from './kubernetes-base.js'; import { writeFiles } from './files.js'; import prompts from './prompts.js'; const { KAFKA } = messageBrokerTypes; const { MAVEN } = buildToolTypes; export default class KubernetesGenerator 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 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, loadConfig, setupKubernetesConstants, }; } 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, askForPersistentStorage: prompts.askForPersistentStorage, askForStorageClassName: prompts.askForStorageClassName, }; } 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 => { element.clusteredDb ? (element.dbPeerCount = 3) : (element.dbPeerCount = 1); if (element.messageBroker === KAFKA) { this.useKafka = true; } }); this.usesOauth2 = this.appConfigs.some(appConfig => appConfig.authenticationTypeOauth2); this.usesIngress = this.kubernetesServiceType === 'Ingress'; this.useKeycloak = this.usesOauth2 && this.usesIngress; }, }; } 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(`${chalk.yellow.bold('WARNING!')} Kubernetes 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('Kubernetes configuration successfully generated!')}`); } this.log.warn('\nYou 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}`)}`); } if (this.dockerRepositoryName) { this.log.log('\nAlternatively, you can use Jib to build and push image directly to a remote registry:'); this.appsFolders.forEach((appsFolder, index) => { const appConfig = this.appConfigs[index]; let runCommand = ''; if (appConfig.buildTool === MAVEN) { runCommand = `./mvnw -ntp -Pprod verify jib:build${process.arch === 'arm64' ? ' -Djib-maven-plugin.architecture=arm64' : ''} -Djib.to.image=${appConfig.targetImageName}`; } else { runCommand = `./gradlew bootJar -Pprod jib${process.arch === 'arm64' ? ' -PjibArchitecture=arm64' : ''} -Djib.to.image=${appConfig.targetImageName}`; } this.log.verboseInfo(` ${chalk.cyan(`${runCommand}`)} in ${this.destinationPath(this.directoryPath + appsFolder)}`); }); } this.log.log('\nYou can deploy all your apps by running the following kubectl command:'); this.log.verboseInfo(` ${chalk.cyan('bash kubectl-apply.sh -f')}`); this.log.log('\n[OR]'); this.log.log('\nIf you want to use kustomize configuration, then run the following command:'); this.log.verboseInfo(` ${chalk.cyan('bash kubectl-apply.sh -k')}`); if (this.gatewayNb + this.monolithicNb >= 1) { const namespaceSuffix = this.kubernetesNamespace === 'default' ? '' : ` -n ${this.kubernetesNamespace}`; this.log.verboseInfo("\nUse these commands to find your application's IP addresses:"); for (let i = 0; i < this.appsFolders.length; i++) { if (this.appConfigs[i].applicationType === 'gateway' || this.appConfigs[i].applicationType === 'monolith') { this.log.verboseInfo(` ${chalk.cyan(`kubectl get svc ${this.appConfigs[i].baseName.toLowerCase()}${namespaceSuffix}`)}`); } } this.log.log(); } try { fs.chmodSync('kubectl-apply.sh', '755'); } catch { this.log.warn("Failed to make 'kubectl-apply.sh' executable, you may need to run 'chmod +x kubectl-apply.sh'"); } }, }; } get [BaseWorkspacesGenerator.END]() { return this.delegateTasksToBlueprint(() => this.end); } getJDBCUrl(databaseType, options = {}) { return getJdbcUrl(databaseType, options); } getR2DBCUrl(databaseType, options = {}) { return getR2dbcUrl(databaseType, options); } }