Compare commits

...

9 commits

Author SHA1 Message Date
e82ba84ee1 Fix new-line ending in js files 2022-12-22 13:16:24 +01:00
8808f8e65f Remove js file reserved for the interaction with Caddy
We want to use the tls-on-demand feature with labels for the routing.
The plan is to use the container name as hostname for the reverse-proxy.
2022-12-22 13:13:29 +01:00
1ff0b5b7f4 Fix not waiting for the container to start after creating it 2022-12-22 13:12:22 +01:00
081ecd29cf Improve js doc
Some functions needed some discription and the delete method of the
DockerClient had no doc.
2022-12-22 13:09:31 +01:00
b3e5612bf5 Move init docker client initialization to the init function
Including checking if docker is running on the machine.
2022-12-22 13:05:53 +01:00
e5761dc3a2 Add endpoint to delete a deployment 2022-12-22 13:02:01 +01:00
39dfe07d86 Add product key label when searching for containers 2022-12-22 13:01:30 +01:00
3fb1a26e9f Change API call to create a new deployment
Using the router to easier group calls and us the order id as part of
the path. The plan is to use the order id as id for an deployment.
2022-12-22 12:26:48 +01:00
05eb696f95 Remove console logging when starting a new deployment 2022-12-22 12:26:08 +01:00
3 changed files with 70 additions and 25 deletions

View file

@ -1 +0,0 @@
// Communication with caddy and config generation

View file

@ -4,6 +4,7 @@ import Dockerode from "dockerode";
export class DockerClient {
#dockerConnection;
/**
*
* @param {Dockerode} dockerConnection
@ -11,8 +12,9 @@ export class DockerClient {
constructor(dockerConnection) {
this.#dockerConnection = dockerConnection;
}
/**
*
* Start a new deployment if it not already exist.
* @param {string} userId
* @param {string} productId
* @param {string} orderId
@ -30,15 +32,39 @@ export class DockerClient {
[dockerLabels.appContainerLabelKeyOrderId]: orderId
}
});
console.log("created");
container.start();
console.log("started");
await container.start();
}
/**
* Delete an existing deployment.
* @param {string} orderId
*/
async delete(orderId) {
const containers = await this.#dockerConnection.listContainers({
filters: {
label: [
dockerLabels.appContainerLabelKeyProject,
`${dockerLabels.appContainerLabelKeyOrderId}=${orderId}`
]
}
});
if (containers.length != 1) {
if (containers.length < 1) {
throw new Error("No container for the order " + orderId + " found");
}
throw new Error("Too many container for the order " + orderId + " found");
}
const container = containers[0];
await container.stop();
await container.remove();
}
async #existContainer(userId, productId, orderId) {
const foundContainers = await this.#dockerConnection.listContainers({
filters: {
label: [
dockerLabels.appContainerLabelKeyProject,
`${dockerLabels.appContainerLabelKeyUserId}=${userId}`,
`${dockerLabels.appContainerLabelKeyProductId}=${productId}`,
`${dockerLabels.appContainerLabelKeyOrderId}=${orderId}`
@ -57,4 +83,4 @@ export class DockerClient {
if (foundContainers.length == 0) return false;
return true;
}
}
}

View file

@ -2,10 +2,7 @@ import express from "express";
import { Docker } from "./docker-helper.cjs";
import { DockerClient } from "./docker.js";
const docker = Docker({ socketPath: '/var/run/docker.sock' });
const dockerClient = new DockerClient(docker);
const app = init(docker);
const { dockerClient, app } = init();
const port = 3000;
app.use(express.text({ type: '*/*' }))
@ -23,20 +20,35 @@ app.post('/', async (req, res) => {
res.send('Hello World!');
})
app.post('/new', async (req, res) => {
try {
const queryParams = req.query;
if (queryParams.userId == null || queryParams.productId == null || queryParams.orderId == null) {
throw new Error("Missing query params. Required: userId, productId, orderId")
app.route("/api/v1")
// create a new deployment
.post('/order/:id', async (req, res) => {
try {
const queryParams = req.query;
if (queryParams.userId == null || queryParams.orderId == null) {
res.status(400).statusMessage("Missing query params. Required: userId, productId")
}
const params = req.params;
await dockerClient.start(queryParams.userId, queryParams.productId, params.id);
} catch (e) {
res.status(500).send(e.message);
return;
}
await dockerClient.start(queryParams.userId, queryParams.productId, queryParams.orderId);
} catch (e) {
res.status(400).send(e.message);
res.status(201).end();
return;
}
res.status(201).end();
return;
})
})
// delete an order
.delete('/order/:id', async (req, res) => {
try {
const params = req.params;
await dockerClient.delete(params.id);
} catch (e) {
res.status(500).send(e.message);
return;
}
res.status(201).end();
return;
})
app.listen(port, () => {
@ -45,12 +57,20 @@ app.listen(port, () => {
/**
*
* Contains the initialization logic.
* @param {Dockerode} dockerClient
* @returns {Express} express session
*/
function init(dockerClient) {
var socket = process.env.DOCKER_SOCKET || '/var/run/docker.sock';
var stats = fs.statSync(socket);
return express();
if (!stats.isSocket()) {
throw new Error('Are you sure the docker is running?');
}
const docker = Docker({ socketPath: '/var/run/docker.sock' });
const dockerClient = new DockerClient(docker);
const app = express();
return { dockerClient: dockerClient, app: app };
}