This commit is contained in:
Jeffrey Morgan 2015-02-15 17:19:30 -08:00
Родитель 02e0cb1dbb
Коммит fa75c1a93c
1 изменённых файлов: 66 добавлений и 24 удалений

Просмотреть файл

@ -11,7 +11,6 @@ var ContainerUtil = require('./ContainerUtil');
var convert = new Convert(); var convert = new Convert();
var _recommended = []; var _recommended = [];
var _placeholders = {};
var _containers = {}; var _containers = {};
var _progress = {}; var _progress = {};
var _logs = {}; var _logs = {};
@ -24,8 +23,32 @@ var ContainerStore = assign(Object.create(EventEmitter.prototype), {
SERVER_CONTAINER_EVENT: 'server_container_event', SERVER_CONTAINER_EVENT: 'server_container_event',
SERVER_PROGRESS_EVENT: 'server_progress_event', SERVER_PROGRESS_EVENT: 'server_progress_event',
SERVER_LOGS_EVENT: 'server_logs_event', SERVER_LOGS_EVENT: 'server_logs_event',
_pullScratchImage: function (callback) {
var image = docker.client().getImage('scratch:latest');
image.inspect(function (err, data) {
if (!data) {
docker.client().pull('scratch:latest', function (err, stream) {
if (err) {
callback(err);
return;
}
stream.setEncoding('utf8');
stream.on('data', function () {});
stream.on('end', function () {
callback();
});
});
} else {
callback();
}
});
},
_pullImage: function (repository, tag, callback, progressCallback) { _pullImage: function (repository, tag, callback, progressCallback) {
registry.layers(repository, tag, function (err, layerSizes) { registry.layers(repository, tag, function (err, layerSizes) {
// TODO: Support v2 registry API
// TODO: clean this up- It's messy to work with pulls from both the v1 and v2 registry APIs
// Use the per-layer pull progress % to update the total progress.
docker.client().listImages({all: 1}, function(err, images) { docker.client().listImages({all: 1}, function(err, images) {
var existingIds = new Set(images.map(function (image) { var existingIds = new Set(images.map(function (image) {
return image.Id.slice(0, 12); return image.Id.slice(0, 12);
@ -139,14 +162,29 @@ var ContainerStore = assign(Object.create(EventEmitter.prototype), {
}); });
}, },
_createPlaceholderContainer: function (imageName, name, callback) { _createPlaceholderContainer: function (imageName, name, callback) {
if (_placeholders[name]) { var self = this;
delete _placeholders[name]; this._pullScratchImage(function (err) {
} if (err) {
_placeholders[name] = { callback(err);
State: { return;
} }
}; docker.client().createContainer({
return _placeholders[name]; Image: 'scratch:latest',
Tty: false,
Env: [
'KITEMATIC_DOWNLOADING=true',
'KITEMATIC_DOWNLOADING_IMAGE=' + imageName
],
Cmd: 'placeholder',
name: name
}, function (err) {
if (err) {
callback(err);
return;
}
self.fetchContainer(name, callback);
});
});
}, },
_generateName: function (repository) { _generateName: function (repository) {
var base = _.last(repository.split('/')); var base = _.last(repository.split('/'));
@ -348,24 +386,28 @@ var ContainerStore = assign(Object.create(EventEmitter.prototype), {
var self = this; var self = this;
var imageName = repository + ':' + tag; var imageName = repository + ':' + tag;
var containerName = this._generateName(repository); var containerName = this._generateName(repository);
// Pull image
// Create placeholder container self._createPlaceholderContainer(imageName, containerName, function (err, container) {
var container = this._createPlaceholderContainer(imageName, containerName); if (err) {
_containers[containerName] = container; callback(err);
self.emit(self.CLIENT_CONTAINER_EVENT, containerName, 'create'); return;
_muted[containerName] = true; }
_progress[containerName] = 0; _containers[containerName] = container;
self._pullImage(repository, tag, function () { self.emit(self.CLIENT_CONTAINER_EVENT, containerName, 'create');
self._createContainer(containerName, {Image: imageName}, function () { _muted[containerName] = true;
delete _progress[containerName]; _progress[containerName] = 0;
_muted[containerName] = false; self._pullImage(repository, tag, function () {
self.emit(self.CLIENT_CONTAINER_EVENT, containerName); self._createContainer(containerName, {Image: imageName}, function () {
delete _progress[containerName];
_muted[containerName] = false;
self.emit(self.CLIENT_CONTAINER_EVENT, containerName);
});
}, function (progress) {
_progress[containerName] = progress;
self.emit(self.SERVER_PROGRESS_EVENT, containerName);
}); });
}, function (progress) { callback(null, containerName);
_progress[containerName] = progress;
self.emit(self.SERVER_PROGRESS_EVENT, containerName);
}); });
callback(null, containerName);
}, },
updateContainer: function (name, data, callback) { updateContainer: function (name, data, callback) {
_muted[name] = true; _muted[name] = true;