public DeleteImageHandler(IDockerClient dockerClient,
                           DockerWorkItem actionSpecification,
                           ILogger <DeleteImageHandler> logger) : base(dockerClient, actionSpecification)
 {
     _logger = logger;
     _spec   = actionSpecification.SpecificationsMarker as DeleteImageSpecification;
 }
Esempio n. 2
0
        public CancellationToken Enqueue(DockerWorkItem workItem)
        {
            var token = workItem.CancellationToken == CancellationToken.None ? new CancellationToken() : workItem.CancellationToken;
            var item  = workItem with {
                CancellationToken = token
            };

            _queue.Enqueue(item);

            return(token);
        }
Esempio n. 3
0
        public override IDockerActionHandler Create(DockerWorkItem actionSpecification)
        {
            var logger = _loggerFactory.CreateLogger <FetchImageHandler>();

            return(new FetchImageHandler(DockerClient, actionSpecification, logger));
        }
 internal DockerActionHandlerBase(IDockerClient dockerClient, DockerWorkItem actionSpecification)
 {
     DockerClient        = dockerClient;
     ActionSpecification = actionSpecification;
 }
        public async Task <Result <Empty> > Handle(DeleteImageRequest request, CancellationToken cancellationToken)
        {
            //check image exists
            var image = await _dbContext.Images.FirstOrDefaultAsync(c => c.Id == request.EntityId, cancellationToken);

            var imageExists = image != null;

            if (!imageExists)
            {
                _logger.LogError("Not able to delete non existent image {Id}", request.EntityId);
                return(Result <Empty> .FromError <Empty>("Not able to delete non existent image"));
            }

            //delete image
            var dockerRequest = new DockerWorkItem
            {
                SpecificationsMarker = new DeleteImageSpecification
                {
                    ImageId = image.Id
                },
                CancellationToken = cancellationToken
            };

            //await docker
            var            tries = 0;
            Result <Empty> result;

            while (!((DeleteImageSpecification)(dockerRequest.SpecificationsMarker)).ReturnStore.IsFinished(
                       out result) || cancellationToken.IsCancellationRequested)
            {
                await Task.Delay(1000, cancellationToken);

                tries++;
                if (tries >= 3)
                {
                    break;
                }
            }

            //check docker result
            if (!result.IsSuccessful)
            {
                _logger.LogError("Unable to delete stored image {Id}", image.Id);
                return(Result <Empty> .FromError <Empty>("Unable to delete store image"));
            }


            //delete image entity
            var entityResult = await _imageService.DeleteImage(image, cancellationToken);

            //check cancellation
            if (cancellationToken.IsCancellationRequested)
            {
                _logger.LogError("cancellation requested; status of image {Id} unknown", image.Id);
                return(Result <Empty> .FromError <Empty>("cancellation requested; image status unsure"));
            }

            //verify & return

            if (entityResult)
            {
                _logger.LogInformation("Successfully deleted image {Id}", image.Id);
                return(Result <Empty> .FromResult(new Empty()));
            }

            _logger.LogError("Deletion of image {Id} failed", image.Id);
            return(Result <Empty> .FromError <Empty>("Unable to delete image"));
        }
 public CreateContainerHandler(IDockerClient dockerClient, DockerWorkItem actionSpecification, ILogger <CreateContainerHandler> logger) : base(
         dockerClient, actionSpecification)
 {
     _logger         = logger;
     _specifications = actionSpecification.SpecificationsMarker as CreateContainerSpecification;
 }
        public async Task <Result <Empty> > Handle(FetchImageRequest request, CancellationToken cancellationToken)
        {
            //check allocation is allowed
            //send to mediator to fetch with db context
            //check allocation with extension method

            //gets user
            var user = await _mediator.Send(new UserByIdRequest { UserId = request.OwnerId }, cancellationToken);

            //if !bad cont
            if (!user.IsSuccessful)
            {
                _logger.LogError("Unable to fetch image due to invalid user id {Id}", request.OwnerId);
                return(Result <Empty> .FromError <Empty>("Unable to fetch image due to invalid user id: see log"));
            }

            //does conform
            if (!user.Product.IsConformingToIncrementedImageAllocation())
            {
                _logger.LogError("Unable to fetch image due to user having max allocation of {@Allocation}", user.Product.ImageAllocation);
                return(Result <Empty> .FromError <Empty>("Unable to fetch image due to the user using their max image allocation"));
            }

            //add to db
            //using repo
            var imageEntity = new ContainerImage
            {
                Owner = await _dbContext.Users.FirstOrDefaultAsync(c => c.Id == request.OwnerId,
                                                                   cancellationToken),
                ImageName = request.ImageName,
                ImageTag  = request.ImageTag
            };

            var result = await _imageService.CreateImage(imageEntity, cancellationToken);


            if (!result)
            {
                _logger.LogError("Unable to create db-image {Id}", imageEntity.Id);
                return(Result <Empty> .FromError <Empty>("Unable to create db-item"));
            }

            //add fetch image to queue
            // use _dockerWorkQueue
            var dockerWorkItem = new DockerWorkItem
            {
                SpecificationsMarker = new FetchImageSpecification
                {
                    ImageName = request.ImageName,
                    ImageTag  = request.ImageTag,
                    EntityId  = imageEntity.Id
                },
                CancellationToken = cancellationToken
            };

            _dockerWorkQueue.Enqueue(dockerWorkItem);



            //await fetched from server
            //try and wait

            var            tries = 0;
            Result <Empty> dockerServResult;

            while (!((FetchImageSpecification)dockerWorkItem.SpecificationsMarker).ReturnStore.IsFinished(out dockerServResult) &&
                   !cancellationToken.IsCancellationRequested)
            {
                await Task.Delay(1500, cancellationToken);

                tries++;

                if (tries > 3)
                {
                    break;
                }
            }

            //check if cancelled
            if (cancellationToken.IsCancellationRequested)
            {
                _logger.LogError("cancellation requested; status of image {Id} unknown", imageEntity.Id);
                return(Result <Empty> .FromError <Empty>("cancellation requested; image status unsure"));
            }


            //return result
            if (dockerServResult.IsSuccessful)
            {
                _logger.LogInformation("Successfully fetched image id {Id}", imageEntity.Id);
                return(Result <Empty> .FromResult <Empty>(new Empty()));
            }

            _logger.LogError("Docker Unable to fetch image with name {Name} and tag {Tag}", request.ImageName, request.ImageTag);
            return(dockerServResult);
        }
 public abstract IDockerActionHandler Create(DockerWorkItem actionSpecification);
Esempio n. 9
0
 public bool Dequeue(out DockerWorkItem workItem)
 {
     return(_queue.TryDequeue(out workItem));
 }