Skip to content

Commit

Permalink
Merge pull request #16 from haidousm/fix/internal-name
Browse files Browse the repository at this point in the history
Fix -  Service Ordering & Internal Name
  • Loading branch information
haidousm authored May 15, 2022
2 parents ef6dcea + 8a57027 commit b8db25a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 31 deletions.
29 changes: 12 additions & 17 deletions client/src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,18 @@ const deleteService = async (service: Service) => {
};

const updateServiceOrdering = async (services: Service[]) => {
// const token = document.cookie.replace(
// /(?:(?:^|.*;\s*)token\s*\=\s*([^;]*).*$)|^.*$/,
// "$1"
// );

// return await authorizedAxios().put(
// `/services/order`,
// {
// services,
// },
// {
// headers: {
// Authorization: token,
// },
// }
// );
console.log("temporarily disabled");
const updateOrderRequest: {
name: string;
order: number;
}[] = services.map((service, index) => {
return {
name: service.name,
order: service.order,
};
});
return await authorizedAxios().put(`/services/order`, {
services: updateOrderRequest,
});
};

const login = async (username: string, password: string) => {
Expand Down
11 changes: 6 additions & 5 deletions server/libs/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,13 @@ const getMiddlewareLabel = (
};
};
const getAllLabels = (service: Service) => {
const SSLLabels = getSSLLabels(service.name);
const hostLabels = transformHostsToLabel(service.name, service.hosts);
const internalName = `traefiker_${service.name}`;
const SSLLabels = getSSLLabels(internalName);
const hostLabels = transformHostsToLabel(internalName, service.hosts);
const hostPathPrefixMiddlewareLabels =
transformHostsToPathPrefixMiddlewareLabels(service.name, service.hosts);
transformHostsToPathPrefixMiddlewareLabels(internalName, service.hosts);
const redirectMiddlewareLabels = transformRedirectsToMiddlewareLabels(
service.name,
internalName,
service.redirects
);
const labels = {
Expand All @@ -198,6 +199,6 @@ const getAllLabels = (service: Service) => {
...hostPathPrefixMiddlewareLabels,
...redirectMiddlewareLabels,
};
const middlewareLabel = getMiddlewareLabel(service.name, labels);
const middlewareLabel = getMiddlewareLabel(internalName, labels);
return { ...labels, ...middlewareLabel };
};
10 changes: 10 additions & 0 deletions server/src/controllers/services.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ export const updateServiceHandler = async (req: Request, res: Response) => {
logger.info(`Service ${service.name} updated`);
return res.json(service);
} catch (e) {
if (e instanceof Error) {
logger.error(e.message);
}
return res.status(500).json({
error: e,
});
Expand Down Expand Up @@ -152,6 +155,9 @@ export const startServiceHandler = async (req: Request, res: Response) => {
logger.info(`Service ${updatedService.name} started`);
return res.json(updatedService);
} catch (e) {
if (e instanceof Error) {
logger.error(e.message);
}
return res.status(500).json({
error: e,
});
Expand Down Expand Up @@ -197,6 +203,9 @@ export const stopServiceHandler = async (req: Request, res: Response) => {
logger.info(`Service ${updatedService.name} stopped`);
return res.json(updatedService);
} catch (e) {
if (e instanceof Error) {
logger.error(e.message);
}
return res.status(500).json({
error: e,
});
Expand Down Expand Up @@ -327,6 +336,7 @@ const attachContainerToService = async (
});
service.containerId = container.id;
service.status = ServiceStatus.CREATED;
service.internalName = `traefiker_${service.name}`;
await saveService(service);
// istanbul ignore next
logger.info(
Expand Down
2 changes: 1 addition & 1 deletion server/src/routes/services.route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import express from "express";
import validateResource from "../middleware/validateResource";
import { UpdateServicesOrderRequestSchema } from "../schemas/services.schema";
import {
CreateServiceRequestSchema,
UpdateServiceRequestSchema,
UpdateServicesOrderRequestSchema,
} from "../schemas/services.schema";
import {
createServiceHandler,
Expand Down
18 changes: 10 additions & 8 deletions server/src/utils/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,25 @@ export const useDockerAsSourceOfTruth = async () => {
await deleteAllContainers();
await deleteAllImages();

const containers = (await getAllContainers()).filter((container) =>
container.Names[0].includes("traefiker_")
const containers = (await getAllContainers()).filter(
(container) =>
container.Names[0].includes("traefiker_") &&
!container.Names[0].includes("traefiker-client") &&
!container.Names[0].includes("traefiker-server")
);
for (const container of containers) {
const serviceName = container.Names[0]
.replace("/", "")
.replace("traefiker_", "");
const serviceInternalName = container.Names[0].replace("/", "");

const image = await getOrCreateImageByImageIdentifier(
container.Image
);
const hosts: string[] = transformLabelsToHosts(
serviceName,
serviceInternalName,
container.Labels
);

const redirects: Redirect[] = transformLabelsToRedirects(
serviceName,
serviceInternalName,
container.Labels
);

Expand All @@ -52,7 +53,7 @@ export const useDockerAsSourceOfTruth = async () => {
? ServiceStatus.CREATED
: ServiceStatus.STOPPED;
await saveService({
name: serviceName,
name: serviceInternalName.replace("traefiker_", ""),
status: status, // TODO: use state from container
image: image,
hosts: hosts,
Expand All @@ -61,6 +62,7 @@ export const useDockerAsSourceOfTruth = async () => {
order: (await findLastUsedOrder()) + 1, // TODO: operation is not atomic ?? might(is) a problem if multiple requests are made at the same time
network: inspectData.HostConfig.NetworkMode,
containerId: container.Id,
internalName: serviceInternalName,
});
}
} catch (e) {
Expand Down

0 comments on commit b8db25a

Please sign in to comment.