Enviroment
This commit is contained in:
+18
-4
@@ -3,7 +3,7 @@
|
||||
import { verifySession } from "./auth/session";
|
||||
import { getNodes } from "./node";
|
||||
import axios from "axios";
|
||||
import { Env, ImageApp } from "@/lib/types";
|
||||
import { Env, EnvVariable, ImageApp } from "@/lib/types";
|
||||
|
||||
export async function deployNewApp(name: string, repo: string, nodeId: string) {
|
||||
if (await verifySession()) {
|
||||
@@ -42,7 +42,7 @@ export async function getApplications(): Promise<ImageApp[]> {
|
||||
).flat();
|
||||
}
|
||||
|
||||
export async function getEnv(): Promise<Record<string, Env[]>> {
|
||||
export async function getEnv(): Promise<Record<string, Env>> {
|
||||
const nodes = await getNodes();
|
||||
|
||||
const envRecords = await Promise.all(
|
||||
@@ -50,11 +50,25 @@ export async function getEnv(): Promise<Record<string, Env[]>> {
|
||||
const response = await axios.get(`http://${n.ip}:3000/api/env`, {
|
||||
headers: { Authorization: `ApiKey ${n.key}` },
|
||||
});
|
||||
return response.data as Record<string, Env[]>;
|
||||
return response.data as Record<string, Env>;
|
||||
})
|
||||
);
|
||||
|
||||
const merged: Record<string, Env[]> = Object.assign({}, ...envRecords);
|
||||
const merged: Record<string, Env> = Object.assign({}, ...envRecords);
|
||||
|
||||
return merged;
|
||||
}
|
||||
|
||||
export async function setEnv(appId: string, variables: EnvVariable[]) {
|
||||
const nodes = await getNodes();
|
||||
|
||||
nodes.forEach(async (n) => {
|
||||
await axios.post(
|
||||
`http://${n.ip}:3000/api/env`,
|
||||
{ appId, variables },
|
||||
{
|
||||
headers: { Authorization: `ApiKey ${n.key}` },
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import fs from "fs";
|
||||
import path from "path";
|
||||
import { exec } from "child_process";
|
||||
import util from "util";
|
||||
import { Env, ImageApp } from "@/lib/types";
|
||||
import { Env, EnvVariable, ImageApp } from "@/lib/types";
|
||||
import { CPUusage } from "@/lib/server/calc";
|
||||
|
||||
const docker = new Docker();
|
||||
@@ -135,7 +135,7 @@ export async function deployNewApp(name: string, repo: string, nodeId: string) {
|
||||
};
|
||||
}
|
||||
|
||||
export async function getEnv(): Promise<Record<string, Env[]>> {
|
||||
export async function getEnv(): Promise<Record<string, Env>> {
|
||||
const apps = fs.readdirSync(DATA_DIR);
|
||||
|
||||
return apps.reduce((acc, appId) => {
|
||||
@@ -144,9 +144,23 @@ export async function getEnv(): Promise<Record<string, Env[]>> {
|
||||
|
||||
const envPath = path.join(DATA_DIR, appId, "env.json");
|
||||
if (fs.existsSync(envPath)) {
|
||||
const envData: Env[] = JSON.parse(fs.readFileSync(envPath, "utf-8"));
|
||||
acc[name] = envData;
|
||||
const envData: EnvVariable[] = JSON.parse(
|
||||
fs.readFileSync(envPath, "utf-8")
|
||||
);
|
||||
acc[appId] = { variables: envData, id: appId, name };
|
||||
}
|
||||
return acc;
|
||||
}, {} as Record<string, Env[]>);
|
||||
}, {} as Record<string, Env>);
|
||||
}
|
||||
|
||||
export async function setEnv(appId: string, variables: EnvVariable[]) {
|
||||
const apps = fs.readdirSync(DATA_DIR);
|
||||
|
||||
apps.forEach((id) => {
|
||||
if (id === appId) {
|
||||
const envPath = path.join(DATA_DIR, appId, "env.json");
|
||||
|
||||
fs.writeFileSync(envPath, JSON.stringify(variables));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user