diff --git a/index.js b/index.js index 4a2b060..002af96 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ var basicAuth = require('express-basic-auth') var bodyParser = require('body-parser'); var Docker = require('dockerode'); var fs = require('fs'); +var http = require('http'); var config = require('./default_settings.js'); var docker = false; @@ -451,6 +452,49 @@ app.get('/pull/*', function (req, res) { }); }); +/** + * Pull the latest image for the given repository/tag. + * When the pull is complete, notify the provided callback_uri of success or failure. + */ +app.post('/pull/*', function (req, res) { + var repoTag = req.params[0]; + console.log("Pull" + repoTag + "asynchronously"); + var callback = req.body.callback_uri ? req.body.callback_uri : false; + if (callback == "" || !callback) { + res.send({ + status: false, + error: "No callback_uri specified. Use GET /pull/* to pull without a callback." + }); + res.status(400); + return; + } else { + res.send({ + status: true, + result: "Started pulling " + repoTag + }) + } + docker.pull(repoTag, function (err, stream) { + if (err) { + if (config.get("debug")) { + console.log("Failed to pull docker image " + repoTag); + console.log(err); + } + var data = { status: false, error: `Failed to pull docker image ${repoTag}` }; + postCallbackRequest(callback, data); + return; + } + console.log("Pulling image " + repoTag); + const chunks = []; + stream.on("data", function (chunk) { + chunks.push(chunk.toString()); + }); + + stream.on("end", function () { + var data = { status: true, result: `Finished pulling docker image ${repoTag}` } + postCallbackRequest(callback, data) + }); + }); +}); //Attempt to connect to the Docker daemon switch (config.get("docker_connection.type")) { @@ -554,3 +598,16 @@ function getContainer(name, cb, error) }); }); } + +function postCallbackRequest(url, data) +{ + var reqOpts = { method: 'POST', headers: { 'content-type': 'application/json' } }; + var req = http.request(url, reqOpts, function (res) { + res.on('end', function () { console.log(`Message sent to callback uri: ${data}`) }); + }); + req.on('error', function (err) { + console.error(`Error sending POST request to callback URI: ${err.message}`); + }); + req.write(JSON.stringify(data)); + req.end(); +}