Ejemplo n.º 1
0
        public async Task <IEnumerable <ContainerDto> > Handle(ListContainerRequest request,
                                                               CancellationToken cancellationToken)
        {
            var containerEntities = await _containerRepository.RetrieveAllContainersAsync(request.Skip, request.Take);

            if (containerEntities == null)
            {
                throw new StatusException(StatusCodes.Status500InternalServerError,
                                          "Could not retrieve any containers.");
            }

            var containers = _mapper.Map <IEnumerable <ContainerDto> >(containerEntities)
                             .Where(container => _containerDtoValidator.Validate(container).IsValid);

            return(containers);
        }
Ejemplo n.º 2
0
        protected override async Task Handle(DeleteImageRequest request, CancellationToken cancellationToken)
        {
            var imageEntity = await _imageRepository.RetrieveImageAsync(request.Id);

            if (imageEntity == null)
            {
                throw new StatusException(StatusCodes.Status404NotFound,
                                          $"Image with id '{request.Id}' does not exist.");
            }

            var image = _mapper.Map <ImageDto>(imageEntity);

            var containerEntities = await _containerRepository
                                    .RetrieveAllContainersAsync(0, int.MaxValue);

            if (containerEntities == null)
            {
                throw new StatusException(StatusCodes.Status500InternalServerError,
                                          "Could not retrieve any containers.");
            }

            var containers = _mapper.Map <IEnumerable <ContainerDto> >(containerEntities)
                             .Where(x => x.Image == image.Name).ToList();

            if (containers.Count > 0)
            {
                throw new StatusException(StatusCodes.Status403Forbidden,
                                          $"Image with id '{request.Id}' is still used by '{containers.Count}' container(s).");
            }

            var success = await _imageRepository.DeleteImageAsync(request.Id);

            if (!success)
            {
                throw new StatusException(StatusCodes.Status500InternalServerError,
                                          $"Image with id '{request.Id}' could not be deleted.");
            }
        }
Ejemplo n.º 3
0
        protected override async Task Handle(UpdateImageRequest request, CancellationToken cancellationToken)
        {
            var imageEntity = await _imageRepository.RetrieveImageAsync(request.Id);

            if (imageEntity == null)
            {
                throw new StatusException(StatusCodes.Status404NotFound,
                                          $"Image with id '{request.Id}' does not exist.");
            }

            var image = _mapper.Map <ImageDto>(imageEntity);

            var containerEntities = await _containerRepository
                                    .RetrieveAllContainersAsync(take : int.MaxValue);

            if (containerEntities == null)
            {
                throw new StatusException(StatusCodes.Status500InternalServerError,
                                          "Could not retrieve any containers during update.");
            }

            var containers = _mapper.Map <IEnumerable <ContainerDto> >(containerEntities)
                             .Where(x => x.Image == image.Name && x.State == "running").ToList();

            var networkEntities = await _networkRepository.RetrieveAllNetworksAsync(take : int.MaxValue);

            if (networkEntities == null)
            {
                throw new StatusException(StatusCodes.Status500InternalServerError,
                                          "Could not retrieve any networks during update.");
            }

            var networks             = _mapper.Map <IEnumerable <NetworkDto> >(networkEntities).ToList();
            var disconnectedNetworks = new Dictionary <string, IEnumerable <string> >();

            foreach (var container in containers)
            {
                var containerNetworks = networks.Where(x =>
                                                       x.Containers.Contains(container.Id)).Select(x => x.Id);
                disconnectedNetworks.Add(container.Id, containerNetworks);

                var stoppedContainer = await _containerService.StopContainerAsync(container.Id);

                if (!stoppedContainer)
                {
                    throw new StatusException(StatusCodes.Status500InternalServerError,
                                              $"Could not stop container with id '{container.Id}' during update.");
                }

                var deletedContainer = await _containerRepository.DeleteContainerAsync(container.Id);

                if (!deletedContainer)
                {
                    throw new StatusException(StatusCodes.Status500InternalServerError,
                                              $"Could not delete container with id '{container.Id}' during update.");
                }
            }

            var registry            = image.Name.Split('/').First();
            var registryCredentials = await _authService.RetrieveCredentialsAsync(registry);

            var username = registryCredentials?.Item1;
            var password = registryCredentials?.Item2;

            var imageParts = image.Name.Split(':');
            var imageName  = imageParts.First();
            var imageTag   = imageParts.Last();

            imageEntity = await _imageRepository.CreateImageAsync(imageName, imageTag, username, password);

            if (imageEntity == null)
            {
                throw new StatusException(StatusCodes.Status500InternalServerError,
                                          $"Could not create image '{image.Name}' during update.");
            }

            foreach (var container in containers)
            {
                var deploymentEntity = await _deploymentRepository.RetrieveDeploymentAsync(container.Deployment);

                if (deploymentEntity == null)
                {
                    throw new StatusException(StatusCodes.Status500InternalServerError,
                                              $"Could not retrieve deployment with id '{container.Deployment}' during update.");
                }

                var parameters      = _mapper.Map <CreateContainerParameters>(deploymentEntity);
                var containerEntity = await _containerRepository.CreateContainerAsync(parameters);

                if (containerEntity == null)
                {
                    throw new StatusException(StatusCodes.Status500InternalServerError,
                                              $"Could not create container for deployment with id '{container.Deployment}' during update.");
                }

                var reconnectNetworks = disconnectedNetworks
                                        .FirstOrDefault(x => x.Key == container.Id).Value;
                foreach (var id in reconnectNetworks)
                {
                    await _networkService.ConnectNetworkAsync(id, containerEntity.ID);
                }

                var startedContainer = await _containerService.StartContainerAsync(containerEntity.ID);

                if (!startedContainer)
                {
                    throw new StatusException(StatusCodes.Status500InternalServerError,
                                              $"Could not start container with id '{containerEntity.ID}' during update.");
                }
            }
        }