Compare commits
No commits in common. "e82ba84ee1357c75fac30e28b6ffc8e05a89c0ae" and "97f50e607391bbfbd28b26bdb9936716ca444706" have entirely different histories.
e82ba84ee1
...
97f50e6073
3 changed files with 25 additions and 70 deletions
1
src/caddy.js
Normal file
1
src/caddy.js
Normal file
|
@ -0,0 +1 @@
|
||||||
|
// Communication with caddy and config generation
|
|
@ -4,7 +4,6 @@ import Dockerode from "dockerode";
|
||||||
|
|
||||||
export class DockerClient {
|
export class DockerClient {
|
||||||
#dockerConnection;
|
#dockerConnection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {Dockerode} dockerConnection
|
* @param {Dockerode} dockerConnection
|
||||||
|
@ -12,9 +11,8 @@ export class DockerClient {
|
||||||
constructor(dockerConnection) {
|
constructor(dockerConnection) {
|
||||||
this.#dockerConnection = dockerConnection;
|
this.#dockerConnection = dockerConnection;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start a new deployment if it not already exist.
|
*
|
||||||
* @param {string} userId
|
* @param {string} userId
|
||||||
* @param {string} productId
|
* @param {string} productId
|
||||||
* @param {string} orderId
|
* @param {string} orderId
|
||||||
|
@ -32,39 +30,15 @@ export class DockerClient {
|
||||||
[dockerLabels.appContainerLabelKeyOrderId]: orderId
|
[dockerLabels.appContainerLabelKeyOrderId]: orderId
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
await container.start();
|
console.log("created");
|
||||||
}
|
container.start();
|
||||||
|
console.log("started");
|
||||||
/**
|
|
||||||
* 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) {
|
async #existContainer(userId, productId, orderId) {
|
||||||
const foundContainers = await this.#dockerConnection.listContainers({
|
const foundContainers = await this.#dockerConnection.listContainers({
|
||||||
filters: {
|
filters: {
|
||||||
label: [
|
label: [
|
||||||
dockerLabels.appContainerLabelKeyProject,
|
|
||||||
`${dockerLabels.appContainerLabelKeyUserId}=${userId}`,
|
`${dockerLabels.appContainerLabelKeyUserId}=${userId}`,
|
||||||
`${dockerLabels.appContainerLabelKeyProductId}=${productId}`,
|
`${dockerLabels.appContainerLabelKeyProductId}=${productId}`,
|
||||||
`${dockerLabels.appContainerLabelKeyOrderId}=${orderId}`
|
`${dockerLabels.appContainerLabelKeyOrderId}=${orderId}`
|
||||||
|
|
42
src/index.js
42
src/index.js
|
@ -2,7 +2,10 @@ import express from "express";
|
||||||
import { Docker } from "./docker-helper.cjs";
|
import { Docker } from "./docker-helper.cjs";
|
||||||
import { DockerClient } from "./docker.js";
|
import { DockerClient } from "./docker.js";
|
||||||
|
|
||||||
const { dockerClient, app } = init();
|
const docker = Docker({ socketPath: '/var/run/docker.sock' });
|
||||||
|
const dockerClient = new DockerClient(docker);
|
||||||
|
|
||||||
|
const app = init(docker);
|
||||||
const port = 3000;
|
const port = 3000;
|
||||||
|
|
||||||
app.use(express.text({ type: '*/*' }))
|
app.use(express.text({ type: '*/*' }))
|
||||||
|
@ -20,30 +23,15 @@ app.post('/', async (req, res) => {
|
||||||
res.send('Hello World!');
|
res.send('Hello World!');
|
||||||
})
|
})
|
||||||
|
|
||||||
app.route("/api/v1")
|
app.post('/new', async (req, res) => {
|
||||||
// create a new deployment
|
|
||||||
.post('/order/:id', async (req, res) => {
|
|
||||||
try {
|
try {
|
||||||
const queryParams = req.query;
|
const queryParams = req.query;
|
||||||
if (queryParams.userId == null || queryParams.orderId == null) {
|
if (queryParams.userId == null || queryParams.productId == null || queryParams.orderId == null) {
|
||||||
res.status(400).statusMessage("Missing query params. Required: userId, productId")
|
throw new Error("Missing query params. Required: userId, productId, orderId")
|
||||||
}
|
}
|
||||||
const params = req.params;
|
await dockerClient.start(queryParams.userId, queryParams.productId, queryParams.orderId);
|
||||||
await dockerClient.start(queryParams.userId, queryParams.productId, params.id);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
res.status(500).send(e.message);
|
res.status(400).send(e.message);
|
||||||
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;
|
return;
|
||||||
}
|
}
|
||||||
res.status(201).end();
|
res.status(201).end();
|
||||||
|
@ -57,20 +45,12 @@ app.listen(port, () => {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains the initialization logic.
|
*
|
||||||
* @param {Dockerode} dockerClient
|
* @param {Dockerode} dockerClient
|
||||||
* @returns {Express} express session
|
* @returns {Express} express session
|
||||||
*/
|
*/
|
||||||
function init(dockerClient) {
|
function init(dockerClient) {
|
||||||
var socket = process.env.DOCKER_SOCKET || '/var/run/docker.sock';
|
|
||||||
var stats = fs.statSync(socket);
|
|
||||||
|
|
||||||
if (!stats.isSocket()) {
|
return express();
|
||||||
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 };
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue