2015-02-19 21:21:54 +00:00
|
|
|
/**
|
|
|
|
* An element which displays a form for manually starting a dockerfile build.
|
|
|
|
*/
|
|
|
|
angular.module('quay').directive('dockerfileBuildForm', function () {
|
|
|
|
var directiveDefinitionObject = {
|
|
|
|
priority: 0,
|
|
|
|
templateUrl: '/static/directives/dockerfile-build-form.html',
|
|
|
|
replace: false,
|
|
|
|
transclude: false,
|
|
|
|
restrict: 'C',
|
|
|
|
scope: {
|
|
|
|
'repository': '=repository',
|
2016-09-29 13:19:21 +00:00
|
|
|
|
|
|
|
'isReady': '=?isReady',
|
|
|
|
|
|
|
|
'readyForBuild': '&readyForBuild'
|
2015-02-19 21:21:54 +00:00
|
|
|
},
|
2015-08-19 20:15:21 +00:00
|
|
|
controller: function($scope, $element, ApiService, DockerfileService, Config) {
|
2016-09-29 13:19:21 +00:00
|
|
|
$scope.state = 'empty';
|
2015-08-19 20:15:21 +00:00
|
|
|
|
2016-09-29 13:19:21 +00:00
|
|
|
var checkPrivateImage = function(baseImage) {
|
|
|
|
var params = {
|
|
|
|
'repository': baseImage
|
|
|
|
};
|
2015-08-20 23:24:51 +00:00
|
|
|
|
2016-09-29 13:19:21 +00:00
|
|
|
$scope.state = 'checking-image';
|
|
|
|
ApiService.getRepo(null, params).then(function(repository) {
|
|
|
|
$scope.privateBaseRepository = repository.is_public ? null : baseImage;
|
|
|
|
$scope.state = 'awaiting-bot';
|
|
|
|
}, function() {
|
|
|
|
$scope.privateBaseRepository = baseImage;
|
|
|
|
$scope.state = 'awaiting-bot';
|
|
|
|
});
|
|
|
|
};
|
2015-08-19 20:15:21 +00:00
|
|
|
|
2016-09-29 13:19:21 +00:00
|
|
|
$scope.handleFilesSelected = function(files, opt_callback) {
|
2015-08-19 20:15:21 +00:00
|
|
|
$scope.pullEntity = null;
|
2016-09-29 13:19:21 +00:00
|
|
|
$scope.state = 'checking';
|
|
|
|
$scope.selectedFiles = files;
|
2015-08-19 20:15:21 +00:00
|
|
|
|
2016-09-29 13:19:21 +00:00
|
|
|
DockerfileService.getDockerfile(files[0], function(df) {
|
|
|
|
var baseImage = df.getRegistryBaseImage();
|
|
|
|
if (baseImage) {
|
|
|
|
checkPrivateImage(baseImage);
|
|
|
|
} else {
|
|
|
|
$scope.state = 'ready';
|
|
|
|
}
|
2015-08-19 20:15:21 +00:00
|
|
|
|
|
|
|
$scope.$apply(function() {
|
2016-09-29 13:19:21 +00:00
|
|
|
opt_callback && opt_callback(true, 'Dockerfile found and valid')
|
2015-08-19 20:15:21 +00:00
|
|
|
});
|
2016-09-29 13:19:21 +00:00
|
|
|
}, function(msg) {
|
|
|
|
$scope.state = 'empty';
|
|
|
|
$scope.privateBaseRepository = null;
|
2015-08-19 20:15:21 +00:00
|
|
|
|
|
|
|
$scope.$apply(function() {
|
2016-09-29 13:19:21 +00:00
|
|
|
opt_callback && opt_callback(false, msg || 'Could not find valid Dockerfile');
|
2015-08-19 20:15:21 +00:00
|
|
|
});
|
2016-09-29 13:19:21 +00:00
|
|
|
});
|
2015-02-19 21:21:54 +00:00
|
|
|
};
|
|
|
|
|
2016-09-29 13:19:21 +00:00
|
|
|
$scope.handleFilesCleared = function() {
|
|
|
|
$scope.state = 'empty';
|
|
|
|
$scope.pullEntity = null;
|
|
|
|
$scope.privateBaseRepository = null;
|
2015-02-19 21:21:54 +00:00
|
|
|
};
|
|
|
|
|
2016-09-29 13:19:21 +00:00
|
|
|
$scope.handleFilesValidated = function(uploadFiles) {
|
|
|
|
$scope.uploadFilesCallback = uploadFiles;
|
2015-02-19 21:21:54 +00:00
|
|
|
};
|
|
|
|
|
2016-09-29 13:19:21 +00:00
|
|
|
var requestRepoBuild = function(buildPackId, opt_callback) {
|
2015-02-19 21:21:54 +00:00
|
|
|
var repo = $scope.repository;
|
|
|
|
var data = {
|
2016-09-29 13:19:21 +00:00
|
|
|
'file_id': buildPackId
|
2015-02-19 21:21:54 +00:00
|
|
|
};
|
|
|
|
|
2015-08-19 20:15:21 +00:00
|
|
|
if ($scope.pullEntity) {
|
|
|
|
data['pull_robot'] = $scope.pullEntity['name'];
|
2015-02-19 21:21:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var params = {
|
|
|
|
'repository': repo.namespace + '/' + repo.name,
|
|
|
|
};
|
|
|
|
|
|
|
|
ApiService.requestRepoBuild(data, params).then(function(resp) {
|
2016-09-29 13:19:21 +00:00
|
|
|
opt_callback && opt_callback(true, resp);
|
2015-02-19 21:21:54 +00:00
|
|
|
}, function(resp) {
|
2016-09-29 13:19:21 +00:00
|
|
|
opt_callback && opt_callback(false, 'Could not start build');
|
|
|
|
$scope.handleFilesSelected($scope.selectedFiles);
|
2015-02-19 21:21:54 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-09-29 13:19:21 +00:00
|
|
|
var startBuild = function(opt_callback) {
|
|
|
|
$scope.state = 'uploading-files';
|
|
|
|
$scope.uploadFilesCallback(function(status, messageOrIds) {
|
|
|
|
$scope.state = 'starting-build';
|
|
|
|
requestRepoBuild(messageOrIds[0], opt_callback);
|
2015-02-19 21:21:54 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-19 20:15:21 +00:00
|
|
|
var checkEntity = function() {
|
2016-09-29 13:19:21 +00:00
|
|
|
if (!$scope.pullEntity) { return; }
|
|
|
|
|
|
|
|
$scope.state = 'checking-bot';
|
2015-08-19 20:15:21 +00:00
|
|
|
$scope.currentRobotHasPermission = null;
|
|
|
|
if (!$scope.pullEntity) { return; }
|
|
|
|
|
|
|
|
var permParams = {
|
|
|
|
'repository': $scope.privateBaseRepository,
|
|
|
|
'username': $scope.pullEntity.name
|
|
|
|
};
|
|
|
|
|
|
|
|
ApiService.getUserTransitivePermission(null, permParams).then(function(resp) {
|
|
|
|
$scope.currentRobotHasPermission = resp['permissions'].length > 0;
|
2016-09-29 13:19:21 +00:00
|
|
|
$scope.state = $scope.currentRobotHasPermission ? 'ready' : 'perm-error';
|
2015-08-19 20:15:21 +00:00
|
|
|
});
|
2015-02-19 21:21:54 +00:00
|
|
|
};
|
|
|
|
|
2015-08-19 20:15:21 +00:00
|
|
|
$scope.$watch('pullEntity', checkEntity);
|
2016-09-29 13:19:21 +00:00
|
|
|
$scope.$watch('state', function(state) {
|
|
|
|
$scope.isReady = state == 'ready';
|
|
|
|
if ($scope.isReady) {
|
|
|
|
$scope.readyForBuild({
|
|
|
|
'startBuild': startBuild
|
|
|
|
});
|
2015-02-19 21:21:54 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return directiveDefinitionObject;
|
|
|
|
});
|