genezio
Version:
Command line utility to interact with Genezio infrastructure.
82 lines (81 loc) • 3.58 kB
JavaScript
import axios from "./axios.js";
import { BACKEND_ENDPOINT } from "../constants.js";
import { getAuthToken } from "../utils/accounts.js";
import { debugLogger } from "../utils/logging.js";
import { printUninformativeLog, printAdaptiveLog } from "../utils/logging.js";
import { AbortController } from "node-abort-controller";
import version from "../utils/version.js";
import { GENEZIO_NOT_AUTH_ERROR_MSG, UserError } from "../errors.js";
import { GenezioCloudInputType } from "../cloudAdapter/cloudAdapter.js";
export async function deployRequest(projectConfiguration, genezioDeployInput, stage, stack = [], sourceRepository, environmentVariables, prepareOnly = false) {
// auth token
printAdaptiveLog("Checking your credentials", "start");
const authToken = await getAuthToken();
if (!authToken) {
throw new UserError(GENEZIO_NOT_AUTH_ERROR_MSG);
}
printAdaptiveLog("Checking your credentials", "end");
const json = JSON.stringify({
options: projectConfiguration.options,
classes: projectConfiguration.classes.map((genezioClass) => ({
...genezioClass,
entryFile: genezioDeployInput.find((input) => input.name === genezioClass.name)?.entryFile ??
"",
})),
functions: projectConfiguration.functions?.map((func) => {
const input = genezioDeployInput.find((input) => input.name === func.name);
return {
name: func.name,
language: func.language,
metadata: input?.type === GenezioCloudInputType.FUNCTION
? input?.metadata
: undefined,
fileName: input?.archiveName ?? "genezioDeploy.zip",
entryFile: input?.entryFile ?? "",
timeout: input?.timeout,
storageSize: input?.storageSize,
instanceSize: input?.instanceSize,
vcpuCount: input?.vcpuCount,
memoryMb: input?.memoryMb,
maxConcurrentRequestsPerInstance: input?.maxConcurrentRequestsPerInstance,
maxConcurrentInstances: input?.maxConcurrentInstances,
cooldownTime: input?.cooldownTime,
persistent: input?.persistent,
};
}) ?? [],
projectName: projectConfiguration.name,
region: projectConfiguration.region,
cloudProvider: projectConfiguration.cloudProvider,
stage: stage,
stack: stack,
sourceRepository,
environmentVariables,
});
debugLogger.debug("Deploy request sent with body:", json);
const controller = new AbortController();
const messagePromise = printUninformativeLog(controller);
const method = prepareOnly ? "POST" : "PUT";
const url = prepareOnly
? `${BACKEND_ENDPOINT}/core/deployment/prepare`
: `${BACKEND_ENDPOINT}/core/deployment`;
const response = await axios({
method: method,
url: url,
data: json,
headers: {
Authorization: `Bearer ${authToken}`,
"Accept-Version": `genezio-cli/${version}`,
},
maxContentLength: Infinity,
maxBodyLength: Infinity,
}).catch(async (error) => {
controller.abort();
printAdaptiveLog(await messagePromise, "error");
debugLogger.debug("Error received", error);
throw error;
});
controller.abort();
printAdaptiveLog(await messagePromise, "end");
debugLogger.debug("Response received", JSON.stringify(response.data));
return response.data;
}