2020-04-10 21:54:53 +00:00
|
|
|
angular.module('portainer.docker').controller('BuildImageController', [
|
|
|
|
'$scope',
|
|
|
|
'$state',
|
|
|
|
'BuildService',
|
|
|
|
'Notifications',
|
|
|
|
'HttpRequestHelper',
|
|
|
|
function ($scope, $state, BuildService, Notifications, HttpRequestHelper) {
|
|
|
|
$scope.state = {
|
|
|
|
BuildType: 'editor',
|
|
|
|
actionInProgress: false,
|
|
|
|
activeTab: 0,
|
|
|
|
};
|
2018-02-28 06:19:06 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
$scope.formValues = {
|
|
|
|
ImageNames: [{ Name: '' }],
|
|
|
|
UploadFile: null,
|
|
|
|
DockerFileContent: '',
|
|
|
|
URL: '',
|
|
|
|
Path: 'Dockerfile',
|
|
|
|
NodeName: null,
|
|
|
|
};
|
2018-02-28 06:19:06 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
$scope.addImageName = function () {
|
|
|
|
$scope.formValues.ImageNames.push({ Name: '' });
|
|
|
|
};
|
2018-02-28 06:19:06 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
$scope.removeImageName = function (index) {
|
|
|
|
$scope.formValues.ImageNames.splice(index, 1);
|
|
|
|
};
|
2018-02-28 06:19:06 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function buildImageBasedOnBuildType(method, names) {
|
|
|
|
var buildType = $scope.state.BuildType;
|
|
|
|
var dockerfilePath = $scope.formValues.Path;
|
2018-02-28 06:19:06 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
if (buildType === 'upload') {
|
|
|
|
var file = $scope.formValues.UploadFile;
|
|
|
|
return BuildService.buildImageFromUpload(names, file, dockerfilePath);
|
|
|
|
} else if (buildType === 'url') {
|
|
|
|
var URL = $scope.formValues.URL;
|
|
|
|
return BuildService.buildImageFromURL(names, URL, dockerfilePath);
|
|
|
|
} else {
|
|
|
|
var dockerfileContent = $scope.formValues.DockerFileContent;
|
|
|
|
return BuildService.buildImageFromDockerfileContent(names, dockerfileContent);
|
|
|
|
}
|
|
|
|
}
|
2018-02-28 06:19:06 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
$scope.buildImage = function () {
|
|
|
|
var buildType = $scope.state.BuildType;
|
2018-02-28 06:19:06 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
if (buildType === 'editor' && $scope.formValues.DockerFileContent === '') {
|
|
|
|
$scope.state.formValidationError = 'Dockerfile content must not be empty';
|
|
|
|
return;
|
|
|
|
}
|
2018-02-28 06:19:06 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
$scope.state.actionInProgress = true;
|
2018-02-28 06:19:06 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
var imageNames = $scope.formValues.ImageNames.filter(function filterNull(x) {
|
|
|
|
return x.Name;
|
|
|
|
}).map(function getNames(x) {
|
|
|
|
return x.Name;
|
|
|
|
});
|
2018-02-28 06:19:06 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
var nodeName = $scope.formValues.NodeName;
|
|
|
|
HttpRequestHelper.setPortainerAgentTargetHeader(nodeName);
|
2018-02-28 06:19:06 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
buildImageBasedOnBuildType(buildType, imageNames)
|
|
|
|
.then(function success(data) {
|
|
|
|
$scope.buildLogs = data.buildLogs;
|
|
|
|
$scope.state.activeTab = 1;
|
|
|
|
if (data.hasError) {
|
|
|
|
Notifications.error('An error occured during build', { msg: 'Please check build logs output' });
|
|
|
|
} else {
|
|
|
|
Notifications.success('Image successfully built');
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to build image');
|
|
|
|
})
|
|
|
|
.finally(function final() {
|
|
|
|
$scope.state.actionInProgress = false;
|
|
|
|
});
|
|
|
|
};
|
2018-05-06 07:15:57 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
$scope.validImageNames = function () {
|
|
|
|
for (var i = 0; i < $scope.formValues.ImageNames.length; i++) {
|
|
|
|
var item = $scope.formValues.ImageNames[i];
|
|
|
|
if (item.Name !== '') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2018-02-28 06:19:06 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
$scope.editorUpdate = function (cm) {
|
|
|
|
$scope.formValues.DockerFileContent = cm.getValue();
|
|
|
|
};
|
|
|
|
},
|
|
|
|
]);
|