public IActionResult Post([FromBody] GetAgentUploadInfoRequest request) { //Make sure we can find the application using (var connection = _connectionFactory.CreateAndOpen()) { var deviceType = connection.Get <DeviceType>(request.DeviceTypeId); if (deviceType == null) { return(NotFound(new Error($"Unable to find device device type '{request.DeviceTypeId}'."))); } 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.IsAgentVersionNameInUse(request.DeviceTypeId, request.Name)) { return(Ok(new GetUploadInfoResponse { Reason = $"Name '{request.Name}' is already in use for device architecture '{deviceType.Name}'. Specify a new name." })); } //Craft the response var response = new GetUploadInfoResponse { CanUpload = true, RegistryHost = _registryConfig.RegistryHost, Repository = _repositoryNameFactory.Agent }; return(Ok(response)); } }
public Task <GetUploadInfoResponse> GetAgentUploadInfo(GetAgentUploadInfoRequest request, CancellationToken cancellationToken = new CancellationToken()) { return(_client.MakeJsonRequestAsync <GetUploadInfoResponse>(cancellationToken, HttpMethod.Post, ResourceUrls.AgentUploadInfo, request: request)); }
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); }
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); }