Skip to content

Commit

Permalink
fix: network issue in buildtime
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
omermecitoglu committed Jan 8, 2025
1 parent 8a462cd commit 8ee42a1
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/actions/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export async function update(id: string, _: unknown, formData: FormData): Promis
export async function destroy(id: string, _: FormData) {
const service = await getService(db, id);
if (service) {
await Promise.all(service.networkIds.map(destroyNetwork));
await Promise.all(service.networkIds.map(networkId => destroyNetwork(networkId)));
}
await deleteService(db, id);
redirect("/services");
Expand Down
16 changes: 13 additions & 3 deletions src/core/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@ import "server-only";
import { kebabCase } from "change-case";
import { after } from "next/server";
import { GitError } from "simple-git";
import { buildImage, createContainer, removeContainer } from "~/core/docker";
import { buildImage, createContainer, createNetwork, destroyNetwork, removeContainer } from "~/core/docker";
import { cloneRepo, deleteRepo } from "~/core/git";
import db from "~/database";
import type { ServiceDTO } from "~/models/service";
import createBuildImage from "~/operations/createBuildImage";
import updateBuildImage from "~/operations/updateBuildImage";
import updateService from "~/operations/updateService";
import { pluck } from "~/utils/object";
import env from "./env";
import { getProviderVariables } from "./provider";

export async function startBuilding(service: ServiceDTO) {
const image = await createBuildImage(db, { serviceId: service.id });
after(async () => {
const providerContainerIds = pluck(service.providers, "containerId").filter(c => c !== null);
const buildNetwork = await createNetwork(`build-${image.id}`, providerContainerIds);
try {
const repoPath = await cloneRepo(service.repo, service.id);
const networkId = service.providers.at(0)?.networkIds.at(0);
const success = await buildImage(image.id, repoPath, service.environmentVariables, networkId, env.DEBUG_MODE === "yes");
const success = await buildImage(
image.id,
repoPath,
service.environmentVariables,
buildNetwork.id,
env.DEBUG_MODE === "yes",
);
await deleteRepo(service.id);
if (success) {
await updateBuildImage(db, image.id, { status: "ready" });
Expand Down Expand Up @@ -49,6 +57,8 @@ export async function startBuilding(service: ServiceDTO) {
} else {
await updateBuildImage(db, image.id, { status: "failed" });
}
} finally {
await destroyNetwork(buildNetwork.id, providerContainerIds);
}
});
return image;
Expand Down
8 changes: 4 additions & 4 deletions src/core/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,21 @@ export async function createVolume(name: string) {
});
}

export async function createNetwork(networkId: string) {
export async function createNetwork(networkId: string, containerIds: string[] = []) {
const network = await docker.createNetwork({
Name: networkId,
Driver: "bridge",
CheckDuplicate: true,
Internal: false,
Attachable: true,
});
await Promise.all(containerIds.map(containerId => connectContainerToNetwork(containerId, network.id)));
return network;
}

export async function destroyNetwork(networkId: string) {
export async function destroyNetwork(networkId: string, containerIds: string[] = []) {
const network = docker.getNetwork(networkId);
await Promise.all(containerIds.map(containerId => disconnectContainerFromNetwork(containerId, network.id)));
await network.remove();
}

Expand All @@ -183,12 +185,10 @@ async function connectContainerToNetwork(containerId: string, networkId: string)
});
}

/*
async function disconnectContainerFromNetwork(containerId: string, networkId: string) {
const network = docker.getNetwork(networkId);
await network.disconnect({
Container: containerId,
// Force: true, // is this necessary?
});
}
*/
1 change: 1 addition & 0 deletions src/models/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const baseSchema = createInsertSchema(services, {
id: z.string(),
name: z.string(),
repo: z.string(),
containerId: z.string().nullable(),
networkIds: z.string().array(),
variables: z.record(z.string(), z.string()),
}).array(),
Expand Down
1 change: 1 addition & 0 deletions src/operations/getService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default async function getService(db: typeof database, serviceId: string)
id: true,
name: true,
repo: true,
containerId: true,
},
with: {
networks: {
Expand Down
1 change: 1 addition & 0 deletions src/operations/getServiceByRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default async function getServiceByRepo(db: typeof database, repo: string
id: true,
name: true,
repo: true,
containerId: true,
},
with: {
networks: {
Expand Down

0 comments on commit 8ee42a1

Please sign in to comment.