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',
|
|
|
|
'startNow': '=startNow',
|
|
|
|
'isReady': '=isReady',
|
|
|
|
'uploadFailed': '&uploadFailed',
|
|
|
|
'uploadStarted': '&uploadStarted',
|
|
|
|
'buildStarted': '&buildStarted',
|
|
|
|
'buildFailed': '&buildFailed',
|
|
|
|
'missingFile': '&missingFile',
|
|
|
|
'uploading': '=uploading',
|
|
|
|
'building': '=building'
|
|
|
|
},
|
2015-08-19 20:15:21 +00:00
|
|
|
controller: function($scope, $element, ApiService, DockerfileService, Config) {
|
|
|
|
var MEGABYTE = 1000000;
|
|
|
|
var MAX_FILE_SIZE = 100 * MEGABYTE;
|
|
|
|
|
2015-08-20 23:24:51 +00:00
|
|
|
var resetState = function() {
|
|
|
|
$scope.hasDockerFile = false;
|
|
|
|
$scope.pullEntity = null;
|
|
|
|
$scope.dockerfileState = 'none';
|
|
|
|
$scope.privateBaseRepository = null;
|
|
|
|
$scope.isReady = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
resetState();
|
2015-08-19 20:15:21 +00:00
|
|
|
|
|
|
|
$scope.handleFilesChanged = function(files) {
|
|
|
|
$scope.dockerfileError = '';
|
|
|
|
$scope.privateBaseRepository = null;
|
|
|
|
$scope.pullEntity = null;
|
|
|
|
|
|
|
|
$scope.dockerfileState = 'loading';
|
|
|
|
$scope.hasDockerFile = files.length > 0;
|
|
|
|
|
|
|
|
var checkPrivateImage = function(baseImage) {
|
|
|
|
var params = {
|
|
|
|
'repository': baseImage
|
|
|
|
};
|
|
|
|
|
|
|
|
ApiService.getRepo(null, params).then(function(repository) {
|
|
|
|
$scope.privateBaseRepository = repository.is_public ? null : baseImage;
|
|
|
|
$scope.dockerfileState = 'ready';
|
|
|
|
}, function() {
|
|
|
|
$scope.privateBaseRepository = baseImage;
|
|
|
|
$scope.dockerfileState = 'ready';
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var loadError = function(msg) {
|
|
|
|
$scope.$apply(function() {
|
|
|
|
$scope.dockerfileError = msg || 'Could not read uploaded Dockerfile';
|
|
|
|
$scope.dockerfileState = 'error';
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var gotDockerfile = function(df) {
|
|
|
|
$scope.$apply(function() {
|
|
|
|
var baseImage = df.getRegistryBaseImage();
|
|
|
|
if (baseImage) {
|
|
|
|
checkPrivateImage(baseImage);
|
|
|
|
} else {
|
|
|
|
$scope.dockerfileState = 'ready';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (files.length > 0) {
|
|
|
|
if (files[0].size < MAX_FILE_SIZE) {
|
|
|
|
DockerfileService.getDockerfile(files[0], gotDockerfile, loadError);
|
|
|
|
} else {
|
|
|
|
$scope.dockerfileState = 'ready';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$scope.dockerfileState = 'none';
|
|
|
|
}
|
|
|
|
};
|
2015-02-19 21:21:54 +00:00
|
|
|
|
|
|
|
var handleBuildFailed = function(message) {
|
|
|
|
message = message || 'Dockerfile build failed to start';
|
|
|
|
|
|
|
|
var result = false;
|
|
|
|
if ($scope.buildFailed) {
|
|
|
|
result = $scope.buildFailed({'message': message});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
bootbox.dialog({
|
|
|
|
"message": message,
|
|
|
|
"title": "Cannot start Dockerfile build",
|
|
|
|
"buttons": {
|
|
|
|
"close": {
|
|
|
|
"label": "Close",
|
|
|
|
"className": "btn-primary"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var handleUploadFailed = function(message) {
|
|
|
|
message = message || 'Error with file upload';
|
|
|
|
|
|
|
|
var result = false;
|
|
|
|
if ($scope.uploadFailed) {
|
|
|
|
result = $scope.uploadFailed({'message': message});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
bootbox.dialog({
|
|
|
|
"message": message,
|
|
|
|
"title": "Cannot upload file for Dockerfile build",
|
|
|
|
"buttons": {
|
|
|
|
"close": {
|
|
|
|
"label": "Close",
|
|
|
|
"className": "btn-primary"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var handleMissingFile = function() {
|
|
|
|
var result = false;
|
|
|
|
if ($scope.missingFile) {
|
|
|
|
result = $scope.missingFile({});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
bootbox.dialog({
|
|
|
|
"message": 'A Dockerfile or an archive containing a Dockerfile is required',
|
|
|
|
"title": "Missing Dockerfile",
|
|
|
|
"buttons": {
|
|
|
|
"close": {
|
|
|
|
"label": "Close",
|
|
|
|
"className": "btn-primary"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var startBuild = function(fileId) {
|
|
|
|
$scope.building = true;
|
|
|
|
|
|
|
|
var repo = $scope.repository;
|
|
|
|
var data = {
|
|
|
|
'file_id': fileId
|
|
|
|
};
|
|
|
|
|
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) {
|
|
|
|
$scope.building = false;
|
|
|
|
$scope.uploading = false;
|
|
|
|
|
|
|
|
if ($scope.buildStarted) {
|
|
|
|
$scope.buildStarted({'build': resp});
|
|
|
|
}
|
|
|
|
}, function(resp) {
|
|
|
|
$scope.building = false;
|
|
|
|
$scope.uploading = false;
|
|
|
|
|
|
|
|
handleBuildFailed(resp.message);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var conductUpload = function(file, url, fileId, mimeType) {
|
|
|
|
if ($scope.uploadStarted) {
|
|
|
|
$scope.uploadStarted({});
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
request.open('PUT', url, true);
|
|
|
|
request.setRequestHeader('Content-Type', mimeType);
|
|
|
|
request.onprogress = function(e) {
|
|
|
|
$scope.$apply(function() {
|
|
|
|
var percentLoaded;
|
|
|
|
if (e.lengthComputable) {
|
|
|
|
$scope.upload_progress = (e.loaded / e.total) * 100;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2015-10-16 17:34:10 +00:00
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
request.onerror = function() {
|
|
|
|
$scope.$apply(function() {
|
|
|
|
handleUploadFailed();
|
|
|
|
});
|
|
|
|
};
|
2015-10-16 17:34:10 +00:00
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
request.onreadystatechange = function() {
|
|
|
|
var state = request.readyState;
|
2015-10-16 17:34:10 +00:00
|
|
|
var status = request.status;
|
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
if (state == 4) {
|
2015-10-16 17:34:10 +00:00
|
|
|
if (Math.floor(status / 100) == 2) {
|
|
|
|
$scope.$apply(function() {
|
|
|
|
startBuild(fileId);
|
|
|
|
$scope.uploading = false;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var message = request.statusText;
|
|
|
|
if (status == 413) {
|
|
|
|
message = 'Selected file too large to upload';
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.$apply(function() {
|
|
|
|
handleUploadFailed(message);
|
|
|
|
});
|
|
|
|
}
|
2015-02-19 21:21:54 +00:00
|
|
|
}
|
|
|
|
};
|
2015-10-16 17:34:10 +00:00
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
request.send(file);
|
|
|
|
};
|
|
|
|
|
|
|
|
var startFileUpload = function(repo) {
|
|
|
|
$scope.uploading = true;
|
|
|
|
$scope.uploading_progress = 0;
|
|
|
|
|
2015-04-22 17:22:09 +00:00
|
|
|
var uploader = $element.find('#file-drop')[0];
|
2015-02-19 21:21:54 +00:00
|
|
|
if (uploader.files.length == 0) {
|
|
|
|
handleMissingFile();
|
|
|
|
$scope.uploading = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var file = uploader.files[0];
|
|
|
|
$scope.upload_file = file.name;
|
|
|
|
|
|
|
|
var mimeType = file.type || 'application/octet-stream';
|
|
|
|
var data = {
|
|
|
|
'mimeType': mimeType
|
|
|
|
};
|
|
|
|
|
|
|
|
var getUploadUrl = ApiService.getFiledropUrl(data).then(function(resp) {
|
|
|
|
conductUpload(file, resp.url, resp.file_id, mimeType);
|
|
|
|
}, function() {
|
|
|
|
handleUploadFailed('Could not retrieve upload URL');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-19 20:15:21 +00:00
|
|
|
var checkReady = function() {
|
|
|
|
$scope.isReady = ($scope.dockerfileState == 'ready' &&
|
|
|
|
(!$scope.privateBaseRepository || $scope.pullEntity));
|
|
|
|
};
|
|
|
|
|
|
|
|
var checkEntity = function() {
|
|
|
|
$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;
|
|
|
|
});
|
2015-02-19 21:21:54 +00:00
|
|
|
};
|
|
|
|
|
2015-08-19 20:15:21 +00:00
|
|
|
$scope.$watch('pullEntity', checkEntity);
|
|
|
|
$scope.$watch('pullEntity', checkReady);
|
|
|
|
$scope.$watch('dockerfileState', checkReady);
|
2015-02-19 21:21:54 +00:00
|
|
|
|
2015-08-20 23:24:51 +00:00
|
|
|
$scope.$watch('repository', resetState);
|
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
$scope.$watch('startNow', function() {
|
|
|
|
if ($scope.startNow && $scope.repository && !$scope.uploading && !$scope.building) {
|
|
|
|
startFileUpload();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return directiveDefinitionObject;
|
|
|
|
});
|