2021-11-17 21:58:38 +00:00
|
|
|
import { PorImageRegistryModel } from 'Docker/models/porImageRegistry';
|
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
angular.module('portainer.docker').controller('ImportImageController', [
|
|
|
|
'$scope',
|
|
|
|
'$state',
|
|
|
|
'ImageService',
|
|
|
|
'Notifications',
|
|
|
|
'HttpRequestHelper',
|
2021-11-17 21:58:38 +00:00
|
|
|
'Authentication',
|
|
|
|
'ImageHelper',
|
|
|
|
'endpoint',
|
|
|
|
function ($scope, $state, ImageService, Notifications, HttpRequestHelper, Authentication, ImageHelper, endpoint) {
|
2020-04-10 21:54:53 +00:00
|
|
|
$scope.state = {
|
|
|
|
actionInProgress: false,
|
|
|
|
};
|
2018-07-26 13:09:48 +00:00
|
|
|
|
2021-11-17 21:58:38 +00:00
|
|
|
$scope.endpoint = endpoint;
|
|
|
|
|
|
|
|
$scope.isAdmin = Authentication.isAdmin();
|
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
$scope.formValues = {
|
|
|
|
UploadFile: null,
|
|
|
|
NodeName: null,
|
2021-11-17 21:58:38 +00:00
|
|
|
RegistryModel: new PorImageRegistryModel(),
|
2020-04-10 21:54:53 +00:00
|
|
|
};
|
2018-07-26 13:09:48 +00:00
|
|
|
|
2021-11-17 21:58:38 +00:00
|
|
|
$scope.setPullImageValidity = setPullImageValidity;
|
|
|
|
function setPullImageValidity(validity) {
|
|
|
|
$scope.state.pullImageValidity = validity;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function tagImage(id) {
|
|
|
|
const registryModel = $scope.formValues.RegistryModel;
|
|
|
|
if (registryModel.Image) {
|
|
|
|
const image = ImageHelper.createImageConfigForContainer(registryModel);
|
|
|
|
try {
|
|
|
|
await ImageService.tagImage(id, image.fromImage);
|
|
|
|
} catch (err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to tag image');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.uploadImage = async function () {
|
2020-04-10 21:54:53 +00:00
|
|
|
$scope.state.actionInProgress = true;
|
2018-07-26 13:09:48 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
var nodeName = $scope.formValues.NodeName;
|
|
|
|
HttpRequestHelper.setPortainerAgentTargetHeader(nodeName);
|
|
|
|
var file = $scope.formValues.UploadFile;
|
2021-11-17 21:58:38 +00:00
|
|
|
try {
|
|
|
|
const { data } = await ImageService.uploadImage(file);
|
|
|
|
if (data.error) {
|
|
|
|
Notifications.error('Failure', data.error, 'Unable to upload image');
|
|
|
|
} else if (data.stream) {
|
|
|
|
var regex = /Loaded.*?: (.*?)\n$/g;
|
|
|
|
var imageIds = regex.exec(data.stream);
|
|
|
|
if (imageIds && imageIds.length == 2) {
|
|
|
|
await tagImage(imageIds[1]);
|
|
|
|
$state.go('docker.images.image', { id: imageIds[1] }, { reload: true });
|
|
|
|
}
|
2022-08-10 05:07:35 +00:00
|
|
|
Notifications.success('Success', 'Images successfully uploaded');
|
2021-11-17 21:58:38 +00:00
|
|
|
} else {
|
2022-08-10 05:07:35 +00:00
|
|
|
Notifications.success('Success', 'The uploaded tar file contained multiple images. The provided tag therefore has been ignored.');
|
2021-11-17 21:58:38 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to upload image');
|
|
|
|
} finally {
|
|
|
|
$scope.state.actionInProgress = false;
|
|
|
|
}
|
2020-04-10 21:54:53 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
]);
|