예제 #1
0
 public Task <AgentVersion> CreateAgentVersion(CreateAgentVersionRequest request,
                                               CancellationToken cancellationToken = new CancellationToken())
 {
     return(_client.MakeJsonRequestAsync <AgentVersion>(
                cancellationToken,
                HttpMethod.Post,
                ResourceUrls.AgentVersions,
                request: request));
 }
예제 #2
0
        public AgentVersion Post([FromBody] CreateAgentVersionRequest request)
        {
            using (var connection = _connectionFactory.CreateAndOpen())
                using (var transaction = connection.BeginTransaction())
                {
                    AgentVersion agentVersion = new AgentVersion()
                    {
                        Name         = request.Name,
                        ImageId      = request.ImageId,
                        DeviceTypeId = request.DeviceTypeId,
                        Logs         = request.Logs,
                    }.SetNew();

                    connection.Insert(agentVersion, transaction);

                    transaction.Commit();

                    return(agentVersion);
                }
        }
예제 #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);
            }

            if (string.IsNullOrWhiteSpace(DeviceType))
            {
                Console.Error.WriteLine("No device type was specified.");
                return(1);
            }

            DeviceType deviceType = await context.FindDeviceTypeAsync(DeviceType, cancellationToken);

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

            //Create the request
            var uploadInforRequest = new GetAgentUploadInfoRequest()
            {
                DeviceTypeId = deviceType.Id,
                ImageId      = imageId,
                Name         = tag
            };

            var applicationUploadInfo = await context.Client.AgentUploadInfo.GetAgentUploadInfo(uploadInforRequest, cancellationToken);

            if (applicationUploadInfo.CanUpload)
            {
                Console.WriteLine($"Deploying '{tag}' 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 '{target}:{tag}'...");

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

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

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

            return(0);
        }
예제 #4
0
        protected override async Task <int> ExecuteAsync(CommandContext context, CancellationToken cancellationToken)
        {
            //Look up the device type
            var deviceType = await context.FindDeviceTypeAsync(DeviceType, cancellationToken);

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

            //create the docker instance
            using (var dockerClient = CreateDockerClient())
            {
                string fromImage = $"{FromRepository}:{Tag}";

                Console.WriteLine($"Pulling from '{fromImage}'...");

                var imageCreateParameters = new ImagesCreateParameters
                {
                    FromImage = fromImage,
                };

                var localAuthConfig = new AuthConfig
                {
                };

                //Pull the agent version
                await dockerClient.Images.CreateImageAsync(
                    imageCreateParameters,
                    localAuthConfig,
                    new Progress <JSONMessage>(m => Console.WriteLine(m.ProgressMessage)), cancellationToken);

                var imageInspection = await dockerClient.Images.InspectImageAsync(fromImage, cancellationToken);

                if (imageInspection == null)
                {
                    Console.WriteLine($"Unable to find image '{fromImage}' for inspection.");
                    return(1);
                }

                var getAgentUploadInfoRequest = new GetAgentUploadInfoRequest
                {
                    ImageId      = imageInspection.ID,
                    DeviceTypeId = deviceType.Id,
                    Name         = Tag
                };

                var agentUploadInfo =
                    await context.Client.AgentUploadInfo.GetAgentUploadInfo(getAgentUploadInfoRequest,
                                                                            cancellationToken);

                if (!agentUploadInfo.CanUpload)
                {
                    Console.WriteLine($"Unable to upload: {agentUploadInfo.Reason}");
                    return(1);
                }

                string registryHost = RegistryHost ?? agentUploadInfo.RegistryHost;

                //Create the image tag paramters
                var imageTagParameters = new ImageTagParameters
                {
                    RepositoryName = $"{registryHost}/{agentUploadInfo.Repository}",
                    Tag            = Tag,
                };

                Console.WriteLine($"Tagging the image with '{imageTagParameters}'...");

                //Tag the image
                await dockerClient.Images.TagImageAsync(fromImage, imageTagParameters, cancellationToken);

                string toImage = $"{registryHost}/{agentUploadInfo.Repository}:{Tag}";

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

                //Push to our registry
                await dockerClient.Images.PushImageAsync(
                    toImage,
                    new ImagePushParameters(),
                    localAuthConfig,
                    new Progress <JSONMessage>(m => Console.WriteLine(m.ProgressMessage)),
                    cancellationToken);

                //TODO: Let the management service know that we uploaded it
                var createAgentVersionRequest = new CreateAgentVersionRequest
                {
                    DeviceTypeId = deviceType.Id,
                    ImageId      = imageInspection.ID,
                    MakeCurrent  = false,
                    Name         = Tag,
                    Logs         = "Imported"
                };

                //Create the version
                AgentVersion agentVersion = await context.Client.AgentVersions.CreateAgentVersion(createAgentVersionRequest, cancellationToken);

                //And we're done
                Console.WriteLine($"Version {agentVersion.Id} created.");
            }

            return(0);
        }