Skip to content

Commit

Permalink
fix: select the right image from sourcetype (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
Siumauricio committed May 27, 2024
1 parent 4d3e342 commit aa094e8
Showing 1 changed file with 39 additions and 21 deletions.
60 changes: 39 additions & 21 deletions server/utils/builders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ export const mechanizeDockerContainer = async (
appName,
env,
mounts,
sourceType,
dockerImage,
cpuLimit,
memoryLimit,
memoryReservation,
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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;
};

0 comments on commit aa094e8

Please sign in to comment.