protected override async Task <int> ExecuteAsync(CommandContext context, CancellationToken cancellationToken) { var deviceType = await context.FindDeviceTypeAsync(DeviceType, cancellationToken); if (deviceType == null) { return(1); } var agentVersion = await context.FindAgentVersion(deviceType.Id, Version, cancellationToken); if (agentVersion == null) { return(1); } using (IDockerClient sourceDockerClient = new DockerClientConfiguration(new Uri(Source)).CreateClient()) using (IDockerClient targetDockerClient = new DockerClientConfiguration(new Uri(Target)).CreateClient()) { Console.WriteLine("Saving image to target..."); //Copy the image. using (var sourceImageStream = await sourceDockerClient.Images.SaveImageAsync(agentVersion.ImageId, cancellationToken)) { await targetDockerClient.Images.LoadImageAsync(new ImageLoadParameters(), sourceImageStream, new Progress <JSONMessage>(p => Console.WriteLine(p.Status)), cancellationToken); } Console.WriteLine("Removing target agent container(s)..."); //Ditch the containers that might cause a problem. await targetDockerClient.ObliterateContainerAsync(DockerContainerNames.AgentA, cancellationToken : cancellationToken); await targetDockerClient.ObliterateContainerAsync(DockerContainerNames.AgentB, cancellationToken : cancellationToken); Console.WriteLine("Creating agent container..."); //Create the container factory var containerFactory = new AgentDockerContainerFactory(); //Create the container itself var createContainerResponse = await containerFactory.CreateContainerForDirectAsync(targetDockerClient, agentVersion.ImageId, cancellationToken); Console.WriteLine($"Container '{createContainerResponse.ID}' created. Starting container..."); //Start the container. bool started = await targetDockerClient.Containers.StartContainerAsync(createContainerResponse.ID, new ContainerStartParameters(), cancellationToken); if (started) { Console.WriteLine("Agent container started."); return(0); } Console.Error.WriteLine("Agent container failed to start."); return(1); } }
public async Task RunAsync(CancellationToken cancellationToken) { bool done = false; while (!done) { try { if (await HasRunBefore()) { done = true; } else { //Get the version of the agent to install. var deviceConfiguration = await _deviceApiClient.Configuration.GetConfigurationAsync(cancellationToken); //Make sure we have an actual version if (deviceConfiguration.AgentVersion == null) { Logger.Warning("The server isn't giving us a agent version. Can't proceed until that changes."); //Wait a bit before we start over await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken); } else { //Make sure that the image is downloaded if (!await _dockerClient.DoesImageExistAsync(deviceConfiguration.AgentVersion.ImageId, cancellationToken)) { //Download the image await _agentUpdateService.DownloadImageAsync(_dockerClient, deviceConfiguration.AgentVersion, cancellationToken); } await _dockerClient.ObliterateContainersAsync(new string[] { DockerContainerNames.AgentA, DockerContainerNames.AgentB }, Logger, cancellationToken); //Create the new updated container var createContainerResponse = await _agentDockerContainerFactory.CreateContainerForDirectAsync( _dockerClient, deviceConfiguration.AgentVersion.ImageId, cancellationToken); //Show the warnings if (createContainerResponse.Warnings != null && createContainerResponse.Warnings.Any()) { string formattedWarnings = string.Join(",", createContainerResponse.Warnings); Logger.Warning("Warnings during container creation: {Warnings}", formattedWarnings); } Logger.Information("Container {ContainerId} created for agent {ImageId}. Starting new agent...", createContainerResponse.ID, deviceConfiguration.AgentVersion.ImageId); //Attempt to start the container var started = await _dockerClient.Containers.StartContainerAsync( createContainerResponse.ID, new ContainerStartParameters(), cancellationToken); if (!started) { Logger.Fatal("Agent container didn't start. Not sure how to deal with this."); } //We're good! Like totally bootstrapped and stuff. done = true; } } } catch (Exception ex) { Logger.Error(ex, "Error attempting to bootstrap the agent."); //Wait a bit before we start over await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken); } } Logger.Information("We're bootstrapped! Now just wait forever."); //Wait forever await Task.Delay(Timeout.Infinite, cancellationToken); }