protected override async Task Handle(ConnectNetworkRequest request, CancellationToken cancellationToken) { var networkId = request.Id; var containerId = request.Network.Container; var networkEntity = await _networkRepository.RetrieveNetworkAsync(networkId); if (networkEntity == null) { throw new StatusException(StatusCodes.Status404NotFound, $"Network with id '{request.Id}' does not exist."); } var containerEntity = await _containerRepository.RetrieveContainerAsync(containerId); if (containerEntity == null) { throw new StatusException(StatusCodes.Status404NotFound, $"Container with id '{request.Network.Container}' does not exist."); } var success = await _networkService.ConnectNetworkAsync(networkId, containerId); if (!success) { throw new StatusException(StatusCodes.Status500InternalServerError, $"Could not connect container '{containerId}' to network with id '{networkId}'."); } }
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."); } } }