Skip to content

Commit

Permalink
Add unit handler
Browse files Browse the repository at this point in the history
  • Loading branch information
willnode committed Jun 16, 2024
1 parent 11528fa commit 2d57208
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "domcloud-bridge",
"version": "0.50.0",
"version": "0.51.0",
"description": "Deployment runner for DOM Cloud",
"main": "app.js",
"engines": {
Expand Down
22 changes: 22 additions & 0 deletions src/controllers/unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

import express from 'express';
import { unitExec } from '../executor/unit';

export default function () {
var router = express.Router();
router.get('/', async function (req, res, next) {
try {
res.json(unitExec.get(req.path));
} catch (error) {
next(error);
}
});
router.post('/', async function (req, res, next) {
try {
res.json(unitExec.set(req.path, JSON.stringify(req.body)));
} catch (error) {
next(error);
}
});
return router;
}
31 changes: 31 additions & 0 deletions src/executor/unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ShellString } from 'shelljs';
import path from 'path';
import {
executeLock,
spawnSudoUtil,
} from '../util.js';

const tmpFile = path.join(process.cwd(), '/.tmp/unit')

class UnitExecutor {
constructor() {
}
/**
* @param {string} path
*/
async get(path) {
return await spawnSudoUtil("UNIT_GET", [path]);
}
/**
* @param {string} path
* @param {string} body
*/
async set(path, body) {
return await executeLock('unit', async () => {
ShellString(body).to(tmpFile);
await spawnSudoUtil("UNIT_SET", [path]);
});
}
}

export const unitExec = new UnitExecutor();
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import runner from './controllers/runner.js';
import virtualmin from './controllers/virtualmin.js';
import docker from './controllers/docker.js';
import unit from './controllers/unit.js';

const startTime = Date.now();
dotenv.config();
Expand All @@ -32,6 +33,7 @@ app.use('/screend', screend());
app.use('/docker', docker());
app.use('/runner', runner());
app.use('/virtualmin', virtualmin());
app.use('/unit', unit());
app.use(function (err, req, res, next) {
if (err instanceof Error) {
res.status(500);
Expand Down
36 changes: 36 additions & 0 deletions sudoutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import shelljs from 'shelljs'
import cli from 'cli'
import dotenv from 'dotenv'
import path from 'path';
import http from 'http';
import {
chmodSync,
chownSync,
Expand Down Expand Up @@ -47,6 +48,8 @@ const env = Object.assign({}, {
NGINX_BIN: 'nginx',
NGINX_START: 'systemctl start nginx',
NGINX_TMP: path.join(__dirname, '.tmp/nginx'),
UNIT_SOCKET: '/var/run/unit/control.sock',
UNIT_TMP: path.join(__dirname, '.tmp/unit'),
IPTABLES_PATH: '/etc/sysconfig/iptables',
IPTABLES_OUT: '/etc/sysconfig/iptables',
IPTABLES_SAVE: 'iptables-save',
Expand Down Expand Up @@ -109,6 +112,39 @@ switch (cli.args.shift()) {
rm(DEST + '.bak');
exec(`${env.NGINX_BIN} -s reload`);
exit(0);
case 'UNIT_GET':
arg = cli.args.shift();
http.request({
socketPath: env.UNIT_SOCKET,
path: '/config' + arg,
}, res => {
res.setEncoding('utf8');
res.on('data', data => process.stdout.write(data));
res.on('end', () => exit(0))
res.on('error', data => { console.error(data); exit(1); });
});
setTimeout(() => {
// just in case
exit(1);
}, 1000 * 60).unref();
case 'UNIT_SET':
arg = cli.args.shift();
http.request({
socketPath: env.UNIT_SOCKET,
path: '/config' + arg,
method: 'PUT',
}, res => {
res.setEncoding('utf8');
res.push(cat(env.UNIT_TMP).stdout, 'utf-8');
res.on('data', data => process.stdout.write(data));
res.on('end', () => exit(0))
res.on('error', data => { console.error(data); exit(1); });
});
setTimeout(() => {
// just in case
exit(1);
}, 1000 * 60).unref();
exit(0);
case 'VIRTUAL_SERVER_GET':
arg = cli.args.shift();
cat(env.VIRTUAL_SERVER_PATH.replace('$', arg)).to(env.VIRTUAL_SERVER_TMP);
Expand Down

0 comments on commit 2d57208

Please sign in to comment.