Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/utils/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import _ from "lodash";

// Local imports
import { isCloudMode } from "@ogw_front/utils/stores";
import { useAppStore } from "@ogw_front/stores/app";
import { useInfraStore } from "@ogw_front/stores/infra";

Expand Down Expand Up @@ -105,9 +106,9 @@ function runExtensions() {
const { projectFolderPath } = appStore;
const { PROJECT: projectName } = useRuntimeConfig().public;
const params = { projectFolderPath, projectName };

const endpoint = isCloudMode() ? "cloud" : "local";
const schema = {
$id: "/api/microservice/extensions/run",
$id: `/api/${endpoint}/extensions/run`,
methods: ["POST"],
type: "object",
properties: {
Expand All @@ -130,7 +131,7 @@ function killExtension(extensionId) {
console.log(`[AppStore] Killing extension: ${extensionId}`, { params });

const schema = {
$id: "/api/microservice/extensions/kill",
$id: "/api/local/extensions/kill",
methods: ["POST"],
type: "object",
properties: {
Expand Down
72 changes: 72 additions & 0 deletions server/api/cloud/extensions/run.post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Node imports
import fs from "node:fs";

// Third party imports
import { createError, defineEventHandler, readBody } from "h3";

// Local imports
import {
addMicroserviceMetadatas,
runExtension,
} from "@geode/opengeodeweb-front/server/utils/microservices.js";
import {
extensionBackendPath,
extensionFolderPath,
} from "@geode/opengeodeweb-front/server/utils/path.js";
import {
readExtensionFrontend,
readExtensionMetadata,
} from "@geode/opengeodeweb-front/server/utils/extension.js";
import { extensionsConf } from "@geode/opengeodeweb-front/server/utils/app_config.js";
import { unzipFile } from "@geode/opengeodeweb-front/server/utils/server.js";

export default defineEventHandler(async (event) => {
try {
console.log("NITRO: runExtensions", event);
const { projectFolderPath, projectName } = await readBody(event);
const extensionsConfig = extensionsConf(projectName);
const extensionsArray = await Promise.all(
Object.keys(extensionsConfig).map(async (extensionId) => {
const extensionPath = extensionsConfig[extensionId].path;
const unzippedExtensionPath = await unzipFile(
extensionPath,
extensionFolderPath(projectFolderPath, extensionId),
);
const { id, name, version, backendExecutable, frontendFile } =
await readExtensionMetadata(unzippedExtensionPath);
const frontendContent = await readExtensionFrontend(
unzippedExtensionPath,
frontendFile,
id,
);
fs.chmodSync(extensionBackendPath(unzippedExtensionPath, backendExecutable), "755");
const port = await runExtension(id, backendExecutable, unzippedExtensionPath, {
projectFolderPath,
});
await addMicroserviceMetadatas(projectFolderPath, {
type: "back",
name,
port,
});
return {
id,
name,
version,
frontendContent,
port,
};
}),
);

return {
statusCode: 200,
extensionsArray,
};
} catch (error) {
console.error("Error running extensions:", error);
throw createError({
statusCode: 500,
statusMessage: error.message,
});
}
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Node imports
import fs from "node:fs";
import path from "node:path";

// Third party imports
import { createError, defineEventHandler, readBody } from "h3";
Expand All @@ -11,71 +10,36 @@ import {
runBack,
} from "@geode/opengeodeweb-front/server/utils/microservices.js";
import {
executableName,
extensionBackendPath,
extensionFolderPath,
extensionFrontendPath,
} from "@geode/opengeodeweb-front/server/utils/path.js";
import {
readExtensionFrontend,
readExtensionMetadata,
} from "@geode/opengeodeweb-front/server/utils/extension.js";
import { extensionsConf } from "@geode/opengeodeweb-front/server/utils/app_config.js";
import { unzipFile } from "@geode/opengeodeweb-front/server/utils/server.js";

export default defineEventHandler(async (event) => {
try {
console.log("NITRO: runExtensions", event);
const body = await readBody(event);
const { projectFolderPath, projectName } = body;
const { projectFolderPath, projectName } = await readBody(event);
const extensionsConfig = extensionsConf(projectName);

const extensionsArray = await Promise.all(
Object.keys(extensionsConfig).map(async (extensionId) => {
const extensionPath = extensionsConfig[extensionId].path;
const unzippedExtensionPath = await unzipFile(
extensionPath,
extensionFolderPath(projectFolderPath, extensionId),
);
const metadataPath = path.join(unzippedExtensionPath, "metadata.json");
const metadataContent = await fs.promises.readFile(metadataPath, "utf8");

if (!metadataContent) {
throw createError({
statusCode: 400,
statusMessage: "Invalid extension file: missing metadata.json",
});
}
const metadata = JSON.parse(metadataContent);
console.log("runExtensions", { metadata });

const { id, name, version, backendExecutable, frontendFile } = metadata;
console.log("runExtensions", { id, name, version, backendExecutable });

if (!frontendFile) {
throw createError({
statusCode: 400,
statusMessage: "Invalid extension file: missing frontend JavaScript",
});
}
if (!backendExecutable) {
throw createError({
statusCode: 400,
statusMessage: "Invalid extension file: missing backend executable",
});
}

const frontendFilePath = await extensionFrontendPath(
const { id, name, version, backendExecutable, frontendFile } =
await readExtensionMetadata(unzippedExtensionPath);
const frontendContent = await readExtensionFrontend(
unzippedExtensionPath,
frontendFile,
path.resolve(),
id,
);

console.log("runExtensions", { frontendFilePath });
const frontendContent = await fs.promises.readFile(frontendFilePath, "utf8");

const backendExecutablePath = path.join(
unzippedExtensionPath,
executableName(backendExecutable),
);
console.log("runExtensions", { backendExecutablePath });
fs.chmodSync(backendExecutablePath, "755");
fs.chmodSync(extensionBackendPath(unzippedExtensionPath, backendExecutable), "755");
const port = await runBack(backendExecutable, unzippedExtensionPath, {
projectFolderPath,
});
Expand Down
108 changes: 107 additions & 1 deletion server/utils/cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { google } from "googleapis";

// Local imports

const LOCATIONS_DIR = "/etc/nginx/locations";

async function artifactImage(parent, authClient) {
const projectName = process.env.PROJECT;
const registry = google.artifactregistry({
Expand Down Expand Up @@ -92,4 +94,108 @@ function requestConfig(parent, image, email, projectName) {
};
}

export { artifactImage, requestConfig };
function addSupervisorProgram(name, command, executableArgs) {
const conf = `
[program:${name}]
command=${command} ${executableArgs.join(" ")}
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
`;
const confPath = path.join("/etc/supervisor/conf.d", `${name}.conf`);
fs.writeFileSync(confPath, conf);
execFileSync("supervisorctl", ["reread"]);
execFileSync("supervisorctl", ["update"]);
const stdout = execFileSync("supervisorctl", ["start", name]);
console.log("addSupervisorProgram", stdout);
}

function buildLocationBlock(routePath, port) {
if (!routePath.startsWith("/") || !routePath.endsWith("/")) {
throw new Error(`routePath must start and end with '/', got: ${routePath}`);
}
const methods = "GET, POST, PUT, DELETE, PATCH, OPTIONS";
const limitMethods = methods
.split(",")
.map((method) => method.trim())
.filter((method) => method !== "OPTIONS")
.join(" ");

return `# ====================== ${routePath} location ======================
location ~ "^${routePath}" {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' $allow_origin always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' '${methods}' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-CSRF-Token' always;
add_header 'Access-Control-Max-Age' 1728000 always;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}

limit_except ${limitMethods} { deny all; }

add_header 'Access-Control-Allow-Origin' $allow_origin always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' '${methods}' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-CSRF-Token' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
add_header 'Vary' 'Origin' always;

rewrite "^${routePath}(.*)" /$1 break;
proxy_pass http://localhost:${port};
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
`;
}

function nginxConfigFile(name) {
return path.join(LOCATIONS_DIR, `${name}.conf`);
}

function nginxReload() {
execFileSync("nginx", ["-t"]);
execFileSync("nginx", ["-s", "reload"]);
}

function addNginxLocation(name, port) {
fs.mkdirSync(LOCATIONS_DIR, { recursive: true });
const filePath = nginxConfigFile(name);
if (fs.existsSync(filePath)) {
throw new Error(`Location '${name}' already exists at ${filePath}`);
}
fs.writeFileSync(filePath, buildLocationBlock(`/${name}/`, port));
try {
nginxReload();
} catch (error) {
fs.unlinkSync(filePath);
throw error;
}
return filePath;
}

function removeNginxLocation(name) {
const filePath = filePathFor(name);
if (!fs.existsSync(filePath)) {
return false;
}
fs.unlinkSync(filePath);
nginxReload();
return true;
}

export {
addNginxLocation,
addSupervisorProgram,
artifactImage,
removeNginxLocation,
requestConfig,
};
50 changes: 50 additions & 0 deletions server/utils/extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Node imports
import { promises as fs } from "node:fs";
import path from "node:path";

// Third party imports
import { createError } from "h3";

// Local imports
import { extensionFrontendPath } from "@geode/opengeodeweb-front/server/utils/path.js";

async function readExtensionMetadata(unzippedExtensionPath) {
const metadataPath = path.join(unzippedExtensionPath, "metadata.json");
const metadataContent = await fs.readFile(metadataPath, "utf8");
if (!metadataContent) {
throw createError({
statusCode: 400,
statusMessage: "Invalid extension file: missing metadata.json",
});
}
const metadata = JSON.parse(metadataContent);
console.log("readExtensionMetadata", { metadata });
if (!metadata.frontendFile) {
throw createError({
statusCode: 400,
statusMessage: "Invalid extension file: missing frontend JavaScript",
});
}
if (!metadata.backendExecutable) {
throw createError({
statusCode: 400,
statusMessage: "Invalid extension file: missing backend executable",
});
}

return metadata;
}

async function readExtensionFrontend(unzippedExtensionPath, frontendFile, id) {
console.log("readExtensionFrontend", { id });
const frontendFilePath = await extensionFrontendPath(
unzippedExtensionPath,
frontendFile,
path.resolve(),
id,
);
console.log("readExtensionFrontend", { frontendFilePath });
return fs.readFile(frontendFilePath, "utf8");
}

export { readExtensionFrontend, readExtensionMetadata };
Loading
Loading