/// <summary> /// Expected return error code: /// 200 no error /// 404 No such exec instance /// 409 Container is stopped or paused /// </summary> internal async Task StartContainerExecAsync( string containerExecId, TimeSpan timeout, CancellationToken cancellationToken = default(CancellationToken)) { var requestPath = $"exec/{containerExecId}/start"; var containerExecConfig = new ContainerExecConfig() { Detach = true }; var requestContent = JsonRequestContent.GetContent(containerExecConfig); var response = await this.client.MakeRequestAsync( HttpMethod.Post, requestPath, null, requestContent, timeout).ConfigureAwait(false); if (response.StatusCode != HttpStatusCode.OK) { throw new ContainerApiException(response.StatusCode, response.Body); } }
private async Task ExecuteUpdateRouteCommandWithRetriesAsync(ContainerExecConfig execConfig) { try { var response = await Utility.ExecuteWithRetryOnTimeoutAsync( (operationTimeout) => { return(this.activator.Client.ExecOperation.CreateContainerExecAsync( this.containerId, execConfig, operationTimeout)); }, $"ExecuteUpdateRouteCommandAsync_CreateContainerExecAsync_{this.containerName}", traceType, HostingConfig.Config.DockerRequestTimeout, this.timeoutHelper.RemainingTime); HostingTrace.Source.WriteNoise( traceType, "Setup exec command to update route for Container={0}, AppId={1} succeeded.", this.containerName, this.applicationId); await Utility.ExecuteWithRetryOnTimeoutAsync( (operationTimeout) => { return(this.activator.Client.ExecOperation.StartContainerExecAsync( response.ID, operationTimeout)); }, $"ExecuteUpdateRouteCommandAsync_StartContainerExecAsync_{this.containerName}", traceType, HostingConfig.Config.DockerRequestTimeout, this.timeoutHelper.RemainingTime); HostingTrace.Source.WriteNoise( traceType, "Command execution to update route for Container={0}, AppId={1} succeeded.", this.containerName, this.applicationId); } catch (Exception ex) { var errorMessage = string.Format( "Set up exec command to update route failed for {0}.", Utility.BuildErrorMessage( containerName, applicationId, applicationName, ex)); HostingTrace.Source.WriteWarning(traceType, errorMessage); throw new FabricException(errorMessage, FabricErrorCode.InvalidOperation); } }
private async Task ExecuteUpdateRouteCommandAsync() { var cmd = string.Format( "route delete 0.0.0.0 {0}&route add 0.0.0.0 MASK 0.0.0.0 {1} METRIC 6000", this.activationArgs.GatewayIpAddress, this.activationArgs.GatewayIpAddress); // Route changes on RS3 builds need to be executed with higher privilege. ContainerExecConfig execConfig = new ContainerExecConfig() { AttachStdin = true, AttachStdout = true, AttachStderr = true, Privileged = false, Tty = true, Cmd = new List <string>() { "cmd.exe", "/c", cmd }, User = "******" }; FabricException opEx = null; opEx = await ExecuteUpdateRouteCommandAsync(execConfig); if (opEx != null) { // reset opEx so that we can deactivate container if second try fails. opEx = null; // Execute without higher privilege. execConfig = new ContainerExecConfig() { AttachStdin = true, AttachStdout = true, AttachStderr = true, Privileged = false, Tty = true, Cmd = new List <string>() { "cmd.exe", "/c", cmd } }; opEx = await ExecuteUpdateRouteCommandAsync(execConfig); if (opEx != null) { await this.CleanupOnErrorAsync(opEx); } } }
private async Task <FabricException> ExecuteUpdateRouteCommandAsync(ContainerExecConfig execConfig) { FabricException opEx = null; try { await ExecuteUpdateRouteCommandWithRetriesAsync(execConfig); } catch (FabricException ex) { opEx = ex; } return(opEx); }
/// <summary> /// Expected return error code: /// 201 no error /// 404 no such container /// 409 container is paused /// 500 server error /// </summary> internal async Task <CreateContainerExecResponse> CreateContainerExecAsync( string containerIdOrName, ContainerExecConfig containerExecConfig, TimeSpan timeout, CancellationToken cancellationToken = default(CancellationToken)) { var requestPath = $"containers/{containerIdOrName}/exec"; var requestContent = JsonRequestContent.GetContent(containerExecConfig); var response = await this.client.MakeRequestAsync( HttpMethod.Post, requestPath, null, requestContent, timeout).ConfigureAwait(false); if (response.StatusCode != HttpStatusCode.Created) { throw new ContainerApiException(response.StatusCode, response.Body); } return(ContainerServiceClient.JsonSerializer.DeserializeObject <CreateContainerExecResponse>(response.Body)); }
internal async Task ExecuteUpdateRouteCommandAsync() { var strb = new StringBuilder(); foreach (var gatewayIpAddress in this.gatewayIpAddresses) { var command = string.Format(UpdateRoutesCommand, gatewayIpAddress, gatewayIpAddress); if (strb.Length > 0) { strb.AppendFormat("&{0}", command); } else { strb.Append(command); } } // Route changes on RS3 builds need to be executed with higher privilege. ContainerExecConfig execConfig = new ContainerExecConfig() { AttachStdin = true, AttachStdout = true, AttachStderr = true, Privileged = false, Tty = true, Cmd = new List <string>() { "cmd.exe", "/c", strb.ToString() }, User = "******" }; FabricException opEx = null; opEx = await ExecuteUpdateRouteCommandAsync(execConfig); if (opEx != null) { // reset opEx so that we can deactivate container if second try fails. opEx = null; // Execute without higher privilege. execConfig = new ContainerExecConfig() { AttachStdin = true, AttachStdout = true, AttachStderr = true, Privileged = false, Tty = true, Cmd = new List <string>() { "cmd.exe", "/c", strb.ToString() } }; opEx = await ExecuteUpdateRouteCommandAsync(execConfig); if (opEx != null) { await Utility.CleanupOnErrorAsync( traceType, autoRemove, isContainerRoot, cgroupName, containerName, applicationId, activator, timeoutHelper, opEx); } } }