这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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();
}