diff --git a/app.js b/app.js index 223cabb..977e1c7 100644 --- a/app.js +++ b/app.js @@ -5,6 +5,8 @@ var logger = require('morgan'); var statusRouter = require('./routes/status'); var startRouter = require('./routes/start'); +var restartRouter = require('./routes/restart'); + var app = express(); @@ -17,6 +19,7 @@ app.use(express.static(path.join(__dirname, 'public'))); app.use('/status', statusRouter); app.use('/start', startRouter); +app.use('/restart', restartRouter); module.exports = app; diff --git a/routes/restart.js b/routes/restart.js new file mode 100644 index 0000000..ebd0394 --- /dev/null +++ b/routes/restart.js @@ -0,0 +1,41 @@ +var express = require('express'); +var router = express.Router(); +var docker = require('../bin/dockerode'); + +const registry = 'registry.cliffbreak.de/'; + +router.get('/:id/:image', (req, res, next) => { + if (req.params.image = ! null) { + await pullImage(req.params.image); + restartContainer().then(data => { + res.send(data); + }); + + } + else { + restartContainer(req.params.id).then(data => { + res.send(data); + }); + } +}); + + +function restartContainer(containername) { + return new Promise((resolve, reject) => { + const container = docker.getContainer(containername); + container.restart((err, data) => { + resolve({ err, data }); + }); + }) +} + +function pullImage(image) { + return new Promise((resolve, reject) => { + docker.pull(registry + image, (err, stream) => { + resolve({ err, stream }); + }); + }) +} + + +module.exports = router;