From aa094e847265d3bec05a57bed496ad389592de8f Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Mon, 27 May 2024 13:57:13 -0600 Subject: [PATCH] fix: select the right image from sourcetype (#109) --- server/utils/builders/index.ts | 60 ++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/server/utils/builders/index.ts b/server/utils/builders/index.ts index 1857a6c..e67ad9b 100644 --- a/server/utils/builders/index.ts +++ b/server/utils/builders/index.ts @@ -65,8 +65,6 @@ export const mechanizeDockerContainer = async ( appName, env, mounts, - sourceType, - dockerImage, cpuLimit, memoryLimit, memoryReservation, @@ -99,27 +97,11 @@ export const mechanizeDockerContainer = async ( const filesMount = generateFileMounts(appName, mounts); const envVariables = prepareEnvironmentVariables(env); - const registry = application.registry; - - let image = - sourceType === "docker" - ? dockerImage || "ERROR-NO-IMAGE-PROVIDED" - : `${appName}:latest`; - - if (registry) { - image = `${registry.registryUrl}/${appName}`; - - if (registry.imagePrefix) { - image = `${registry.registryUrl}/${registry.imagePrefix}/${appName}`; - } - } + const image = getImageName(application); + const authConfig = getAuthConfig(application); const settings: CreateServiceOptions = { - authconfig: { - password: registry?.password || "", - username: registry?.username || "", - serveraddress: registry?.registryUrl || "", - }, + authconfig: authConfig, Name: appName, TaskTemplate: { ContainerSpec: { @@ -170,3 +152,39 @@ export const mechanizeDockerContainer = async ( await docker.createService(settings); } }; + +const getImageName = (application: ApplicationNested) => { + const { appName, sourceType, dockerImage, registry } = application; + + if (sourceType === "docker") { + return dockerImage || "ERROR-NO-IMAGE-PROVIDED"; + } + + const registryUrl = registry?.registryUrl || ""; + const imagePrefix = registry?.imagePrefix ? `${registry.imagePrefix}/` : ""; + return registry + ? `${registryUrl}/${imagePrefix}${appName}` + : `${appName}:latest`; +}; + +const getAuthConfig = (application: ApplicationNested) => { + const { registry, username, password, sourceType } = application; + + if (sourceType === "docker") { + if (username && password) { + return { + password, + username, + serveraddress: "https://index.docker.io/v1/", + }; + } + } else if (registry) { + return { + password: registry.password, + username: registry.username, + serveraddress: registry.registryUrl, + }; + } + + return undefined; +};