Skip to content

Commit d2faecb

Browse files
committed
Fixed bugs
1 parent 106b8dc commit d2faecb

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

auth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const init = async function (req, res, next) {
2424
const raw = Buffer.from(val, 'base64').toString('utf8');
2525
const credentials = raw.split(':');
2626
const token = await portainer.login(credentials[0] || process.env.PORTAINER_USER, credentials[1] || process.env.PORTAINER_PASS);
27-
req.token = val;
27+
req.token = token;
2828
req.user = jwt.decode(token);
2929
}
3030
} catch (e) {

handlers/default.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
11
/**
22
* Default deploy request handler
33
*/
4+
const Joi = require('@hapi/joi');
5+
const portainer = require('../portainer');
46

7+
const dataSchema = Joi.object().keys({
8+
stack: Joi.string().required(),
9+
env: Joi.object().required(),
10+
}).required();
511
const handle = async (req, res, data) => {
6-
res.send('Hello cow');
12+
const datav = await dataSchema.validateAsync(data);
13+
const stackptr = await portainer.getStack(req.token, datav.stack);
14+
if (!stackptr)
15+
throw new Error('Stack not found');
16+
17+
// Update environment variables (do not create non existing vars)
18+
const keys = Object.keys(datav.env);
19+
for (const key of keys) {
20+
for (const env of stackptr.Env) { // Check if value in list
21+
if (env.name === key)
22+
env.value = datav.env[key]; // Update value
23+
}
24+
}
25+
26+
await portainer.updateStack(req.token, stackptr);
27+
28+
res.status(200).end();
729
}
830

931
module.exports = {

main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const bodyParser = require('body-parser');
5858
const compression = require('compression');
5959
const express = require('express');
6060
const helmet = require('helmet');
61+
const util = require('util');
6162
const validate = require('./validate');
6263

6364
const app = express();
@@ -78,10 +79,9 @@ const deployRequestSchema = Joi.object().keys({
7879
data: Joi.any(),
7980
});
8081
app.post('/', auth.isAuthenticated, validate.validate(deployRequestSchema), as.wrap(async (req, res) => {
81-
console.log('Here');
8282
const handlerName = req.body.handler;
8383
if (handlers[handlerName] && handlers[handlerName].handle) {
84-
if (handlers[handlerName].handle instanceof Promise) {
84+
if (util.types.isAsyncFunction(handlers[handlerName].handle) || util.types.isPromise(handlers[handlerName].handle)) {
8585
await handlers[handlerName].handle(req, res, req.body.data);
8686
} else {
8787
handlers[handlerName].handle(req, res, req.body.data);

portainer.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
const axios = require('axios').default;
55
const fs = require('fs');
6+
const path = require('path');
67

78
/**
89
* Login into portainer
@@ -38,7 +39,7 @@ const getStack = async function (token, name) {
3839
filters: { name: name },
3940
},
4041
});
41-
42+
4243
// React to portainer error
4344
if (response.err)
4445
throw new Error(response.err);
@@ -53,8 +54,9 @@ const getStack = async function (token, name) {
5354
* @param {Stack} stack
5455
*/
5556
const updateStack = async function (token, stack) {
56-
const response = await axios.put(`${process.env.PORTAINER_API}/stacks/${stack.id}`, {
57-
'StackFileContent': await fs.readFile(path.join(process.env.PORTAINER_DIR,`${stack.ProjectPath.replace(/~\/data/, '')}/${stack.EntryPoint}`)),
57+
const stackfile = await fs.promises.readFile(path.join(process.env.PORTAINER_DIR,`${stack.ProjectPath.replace('/data/', '')}/${stack.EntryPoint}`), 'utf8');
58+
const response = await axios.put(`${process.env.PORTAINER_API}/stacks/${stack.Id}`, {
59+
'StackFileContent': stackfile,
5860
'Env': stack.Env,
5961
}, {
6062
headers: {

0 commit comments

Comments
 (0)