public IActionResult Post([FromBody] GetApplicationUploadInfoRequest request)
        {
            //Make sure we can find the application
            using (var connection = _connectionFactory.CreateAndOpen())
            {
                var application = connection.Get <Application>(request.ApplicationId);

                if (application == null)
                {
                    return(NotFound(new Error($"Unable to find application '{request.ApplicationId}'.")));
                }

                if (string.IsNullOrWhiteSpace(request.Name))
                {
                    return(BadRequest(new Error("No name was specified.")));
                }

                if (string.IsNullOrWhiteSpace(request.ImageId))
                {
                    return(BadRequest(new Error("No image id was specified.")));
                }

                //Check for duplicate name.
                if (connection.IsApplicationVersionNameInUse(request.ApplicationId, request.Name))
                {
                    return(Ok(new GetUploadInfoResponse
                    {
                        Reason = $"Name '{request.Name}' is already in use for application '{application.Name}'. Specify a new name."
                    }));
                }

                //Craft the response
                var response = new GetUploadInfoResponse
                {
                    CanUpload    = true,
                    RegistryHost = _registryConfig.RegistryHost,
                    Repository   = _repositoryNameFactory.FromApplication(request.ApplicationId)
                };

                return(Ok(response));
            }
        }
 public Task <GetUploadInfoResponse> GetApplicationUploadInfo(GetApplicationUploadInfoRequest request, CancellationToken cancellationToken = new CancellationToken())
 {
     return(_client.MakeJsonRequestAsync <GetUploadInfoResponse>(cancellationToken, HttpMethod.Post,
                                                                 ResourceUrls.ApplicationUploadInfo, request: request));
 }
Beispiel #3
0
        private async Task <int> DeployAsync(CommandContext context, IDockerClient dockerClient, BuildResult result,
                                             string tag, CancellationToken cancellationToken)
        {
            //Get the last id
            var imageId = result.Ids.LastOrDefault();

            if (string.IsNullOrWhiteSpace(imageId))
            {
                Console.Error.WriteLine("No image id was found. Unable to deploy.");

                return(1);
            }

            //Make sure we have an application name
            if (string.IsNullOrWhiteSpace(Application))
            {
                Console.Error.WriteLine("No application was specified.");
                return(1);
            }

            var application = await context.FindApplicationAsync(Application, cancellationToken);

            if (application == null)
            {
                return(1);
            }

            //Create the request
            var applicationUploadInfoRequest = new GetApplicationUploadInfoRequest()
            {
                ApplicationId = application.Id,
                ImageId       = imageId,
                Name          = tag
            };

            var applicationUploadInfo = await context.Client.ApplicationUpload.GetApplicationUploadInfo(applicationUploadInfoRequest, cancellationToken);

            if (applicationUploadInfo.CanUpload)
            {
                Console.WriteLine($"Deploying with imageid '{imageId}'...");

                var parameters = new ImagePushParameters
                {
                    ImageID = imageId,
                    Tag     = tag
                };

                string registryHost = RegistryHost ?? applicationUploadInfo.RegistryHost;

                var target = $"{registryHost}/{applicationUploadInfo.Repository}";

                //Tag it!
                await dockerClient.Images.TagImageAsync(imageId, new ImageTagParameters
                {
                    RepositoryName = target,
                    Tag            = tag
                }, cancellationToken);

                //Auth config (will have to include the token)
                var authConfig = new AuthConfig
                {
                    ServerAddress = registryHost
                };

                Console.WriteLine($"Pushing to '{registryHost}'...");

                //Push it to the application registry!
                await dockerClient.Images.PushImageAsync(target, parameters, authConfig,
                                                         new Progress <JSONMessage>(p => Console.WriteLine(p.ProgressMessage)), cancellationToken);

                //Let the service now about the new application version.
                var uploadRequest = new CreateApplicationVersionRequest
                {
                    ApplicationId = application.Id,
                    Name          = tag,
                    ImageId       = imageId,
                    Logs          = result.ToString(),
                    MakeCurrent   = MakeCurrent
                };

                //Upload the application version
                await context.Client.ApplicationVersions.UploadApplicationVersionAsync(uploadRequest, cancellationToken);
            }
            else
            {
                Console.Error.WriteLine($"Warning: Unable to upload image - '{applicationUploadInfo.Reason}'.");
            }

            return(0);
        }