@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
137 lines (136 loc) • 5.94 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.startupScripts = void 0;
const chalk_1 = __importDefault(require("chalk"));
const fs = __importStar(require("fs"));
const node_cron_1 = __importDefault(require("node-cron"));
const app_config_1 = require("../../app.config");
const const_1 = require("../../config/const");
const plugins_1 = require("../../plugins");
const seeds_1 = require("../../seeds");
const seed_projects_1 = require("../../seeds/seed-projects");
const seed_system_1 = require("../../seeds/seed-system");
const server_1 = require("../../server");
const services_1 = require("../../services");
const find_and_run_job_1 = require("../cronjob/find-and-run-job");
const mark_long_build_release_as_failed_1 = require("../deploy/mark-long-build-release-as-failed");
/**
* NOTE: BUILD SERVER INITIAL START-UP SCRIPTS:
* - Create config directory in {HOME_DIR}
* - Connect GIT providers (if any)
* - Connect Container Registries (if any)
* - Connect K8S clusters (if any)
* - Start system cronjobs
* - Seed some initial data
*/
async function startupScripts() {
console.log(`---------------------------------`);
console.log(chalk_1.default.green(`[SYSTEM]`), `Server is initializing...`);
console.log(chalk_1.default.green(`[SYSTEM]`), `Is testing:`, (0, app_config_1.IsTest)());
// config dir
if (!fs.existsSync(const_1.CLI_CONFIG_DIR))
fs.mkdirSync(const_1.CLI_CONFIG_DIR);
/**
* System cronjob checking every minute...
* [Skip for unit tests]
*/
if (!(0, app_config_1.IsTest)()) {
(0, find_and_run_job_1.findAndRunCronjob)();
setInterval(find_and_run_job_1.findAndRunCronjob, 15 * 1000);
}
// NOTE: [isDevMode == process.env.DEV_MODE == true] to make sure it won't override your current GIT config when developing Diginext
if (!app_config_1.isDevMode) {
// write initial private SSH keys if any
const result = await (0, plugins_1.execCmd)(`./scripts/custom_rsa.sh`);
console.log(result);
/**
* CAUTION: DO NOT TURN OFF THIS
* ---
* Set default git config
*/
(0, plugins_1.execCmd)(`git init`);
(0, plugins_1.execCmd)(`git config --global user.email server@dxup.dev`);
(0, plugins_1.execCmd)(`git config --global --add user.name Diginext`);
(0, plugins_1.execCmd)(`git config --global http.postBuffer 524288000`); // 524 mb
}
// seed system initial data: Cloud Providers
await (0, seed_system_1.seedSystemInitialData)();
const wsSvc = new services_1.WorkspaceService();
let workspaces = await wsSvc.find({}, { populate: ["owner"] });
if (workspaces.length > 0) {
// seed default roles to workspace if missing:
await Promise.all(workspaces.map((ws) => (0, seeds_1.seedDefaultRoles)(ws, ws.owner)));
// seed default projects to workspace if missing:
await Promise.all(workspaces.map((ws) => (0, seed_projects_1.seedDefaultProjects)(ws, ws.owner)));
}
/**
* CRONJOBS
* ---
* Schedule a clean up task every 7 days at 02:00 AM
* (Skip for test environment)
*/
if (!(0, app_config_1.IsTest)()) {
/**
* System clean up: DOCKER/PODMAN -> Not working (use server crontab currently instead)
*/
// const repeatDays = 7; // every 7 days
// const atHour = 3; // 3AM
// console.log(chalk.green(`[SYSTEM]`), `✓ Cronjob of "System Clean Up" has been scheduled every ${repeatDays} days at ${atHour}:00 AM`);
// cronjob.schedule(`0 ${atHour} */${repeatDays} * *`, () => cleanUp());
/**
* Mark all builds & releases with "in_progress" status longer than 1 hour as "failed".
* Run every hour.
*/
node_cron_1.default.schedule(`0 * * * *`, () => (0, mark_long_build_release_as_failed_1.markLongRunningBuildAndReleaseAsFailed)());
(0, mark_long_build_release_as_failed_1.markLongRunningBuildAndReleaseAsFailed)();
}
/**
* Database migration
*/
// await migrateAllRecords();
// await migrateAllRoles();
// await migrateAllUsers();
// await migrateAllAppEnvironment();
// await migrateAllFrameworks();
// await migrateAllGitProviders();
// await migrateServiceAccountAndApiKey();
// await migrateDefaultServiceAccountAndApiKeyUser();
// await migrateAllClusters();
// await migrateAllReleases();
/**
* Seed or update initial data
*/
// await seedInitialClusters();
// await seedInitialRegistries();
/**
* Mark "healthz" return true & server is ready to receive connections:
*/
(0, server_1.setServerStatus)(true);
}
exports.startupScripts = startupScripts;