added setup services

This commit is contained in:
biersoeckli
2024-11-26 06:59:28 +00:00
parent f15a349a16
commit c0eccd1295
4 changed files with 60 additions and 34 deletions

View File

@@ -70,7 +70,7 @@ async function initializeNextJs() {
});
}
setupQuickStack();
if (process.env.NODE_ENV === 'production' && process.env.START_MODE === 'setup') {
setupQuickStack();
} else {

View File

@@ -1,32 +1,16 @@
import { PodsInfoModel } from "@/model/pods-info.model";
import k3s from "../adapter/kubernetes-api.adapter";
import { ServiceException } from "@/model/service.exception.model";
import setupPodService from "./setup-services/setup-pod.service";
class PodService {
async waitUntilPodIsRunningFailedOrSucceded(projectId: string, podName: string) {
const timeout = 120000;
const interval = 1000;
const maxTries = timeout / interval;
let tries = 0;
while (tries < maxTries) {
const pod = await this.getPodOrUndefined(projectId, podName);
if (pod && ['Running', 'Failed', 'Succeeded'].includes(pod.status?.phase!)) {
return;
}
await new Promise(resolve => setTimeout(resolve, interval));
tries++;
const isPodRunnning = await setupPodService.waitUntilPodIsRunningFailedOrSucceded(projectId, podName);
if (!isPodRunnning) {
throw new ServiceException(`Pod ${podName} did not become ready in time (timeout).`);
}
throw new ServiceException(`Pod ${podName} did not become ready in time (${timeout}ms).`);
}
async getPodOrUndefined(projectId: string, podName: string) {
const res = await k3s.core.readNamespacedPod(podName, projectId);
return res.body;
}
async getPodInfoByName(projectId: string, podName: string) {
@@ -37,12 +21,8 @@ class PodService {
} as PodsInfoModel;
}
async getPodsForApp(projectId: string, appId: string) {
const res = await k3s.core.listNamespacedPod(projectId, undefined, undefined, undefined, undefined, `app=${appId}`);
return res.body.items.map((item) => ({
podName: item.metadata?.name!,
containerName: item.spec?.containers?.[0].name!
})).filter((item) => !!item.podName && !!item.containerName) as PodsInfoModel[];
async getPodsForApp(projectId: string, appId: string): Promise<PodsInfoModel[]> {
return setupPodService.getPodsForApp(projectId, appId);
}
}

View File

@@ -4,6 +4,7 @@ import namespaceService from "./namespace.service";
import { StringUtils } from "../utils/string.utils";
import crypto from "crypto";
import { FancyConsoleUtils } from "../utils/fancy-console.utils";
import setupPodService from "./setup-services/setup-pod.service";
class QuickStackService {
@@ -24,7 +25,7 @@ class QuickStackService {
await this.createOrUpdatePvc();
await this.createOrUpdateDeployment(undefined, nextAuthSecret);
await this.createOrUpdateService(true);
//await this.waitUntilQuickstackIsRunning();
await this.waitUntilQuickstackIsRunning();
console.log('QuickStack successfully initialized');
console.log('');
console.log('------------------------------------------------');
@@ -37,20 +38,23 @@ class QuickStackService {
console.log('------------------------------------------------');
console.log('');
}
/*
TODO enable again: error in setup process because of error: Cannot find module '@/model/service.exception.model'
async waitUntilQuickstackIsRunning() {
console.log('Waiting for QuickStack to be running...');
await new Promise((resolve) => setTimeout(resolve, 5000));
const pods = await podService.getPodsForApp(this.QUICKSTACK_NAMESPACE, this.QUICKSTACK_DEPLOYMENT_NAME);
const pods = await setupPodService.getPodsForApp(this.QUICKSTACK_NAMESPACE, this.QUICKSTACK_DEPLOYMENT_NAME);
const quickStackPod = pods.find(p => p);
if (!quickStackPod) {
console.error('[ERROR] QuickStack pod was not found');
return;
}
await podService.waitUntilPodIsRunningFailedOrSucceded(this.QUICKSTACK_NAMESPACE, quickStackPod.podName);
console.log('QuickStack is now running');
}*/
await setupPodService.waitUntilPodIsRunningFailedOrSucceded(this.QUICKSTACK_NAMESPACE, quickStackPod.podName);
if (setupPodService) {
console.log('QuickStack is now running');
} else {
console.warn('Could not verify if QuickStack is running, please check manually.');
}
}
async createOrUpdateIngress(hostname: string) {
const ingressName = StringUtils.getIngressName(this.QUICKSTACK_NAMESPACE);

View File

@@ -0,0 +1,42 @@
import k3s from "../../adapter/kubernetes-api.adapter";
class SetupPodService {
async waitUntilPodIsRunningFailedOrSucceded(projectId: string, podName: string) {
const timeout = 120000;
const interval = 1000;
const maxTries = timeout / interval;
let tries = 0;
while (tries < maxTries) {
const pod = await this.getPodOrUndefined(projectId, podName);
if (pod && ['Running', 'Failed', 'Succeeded'].includes(pod.status?.phase!)) {
return true;
}
await new Promise(resolve => setTimeout(resolve, interval));
tries++;
}
return false;
}
async getPodOrUndefined(projectId: string, podName: string) {
const res = await k3s.core.listNamespacedPod(projectId);
return res.body.items.find((item) => item.metadata?.name === podName);
}
async getPodsForApp(projectId: string, appId: string): Promise<{
podName: string;
containerName: string;
}[]> {
const res = await k3s.core.listNamespacedPod(projectId, undefined, undefined, undefined, undefined, `app=${appId}`);
return res.body.items.map((item) => ({
podName: item.metadata?.name!,
containerName: item.spec?.containers?.[0].name!
})).filter((item) => !!item.podName && !!item.containerName);
}
}
const setupPodService = new SetupPodService();
export default setupPodService;