Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using Docker actions when running within a Docker container #383

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Runner.Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public static class Commands
//validFlags array as well present in the CommandSettings.cs
public static class Flags
{
public static readonly string AllowDockerInDocker = "allow-docker-in-docker";
public static readonly string Commit = "commit";
public static readonly string Help = "help";
public static readonly string Replace = "replace";
Expand Down
14 changes: 14 additions & 0 deletions src/Runner.Common/HostContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public interface IHostContext : IDisposable
event EventHandler Unloading;
void ShutdownRunner(ShutdownReason reason);
void WritePerfCounter(string counter);
bool AllowDockerInDocker { get; set; }
}

public enum StartupType
Expand Down Expand Up @@ -65,6 +66,7 @@ public sealed class HostContext : EventListener, IObserver<DiagnosticListener>,
private IDisposable _httpTraceSubscription;
private IDisposable _diagListenerSubscription;
private StartupType _startupType;
private bool _allowDockerInDocker;
private string _perfFile;
private RunnerWebProxy _webProxy = new RunnerWebProxy();

Expand Down Expand Up @@ -440,6 +442,18 @@ public StartupType StartupType
}
}

public bool AllowDockerInDocker
{
get
{
return _allowDockerInDocker;
}
set
{
_allowDockerInDocker = value;
}
}

public void WritePerfCounter(string counter)
{
if (!string.IsNullOrEmpty(_perfFile))
Expand Down
3 changes: 3 additions & 0 deletions src/Runner.Listener/CommandSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public sealed class CommandSettings

private readonly string[] validFlags =
{
Constants.Runner.CommandLine.Flags.AllowDockerInDocker,
Constants.Runner.CommandLine.Flags.Commit,
Constants.Runner.CommandLine.Flags.Help,
Constants.Runner.CommandLine.Flags.Replace,
Expand Down Expand Up @@ -65,6 +66,8 @@ public sealed class CommandSettings

public bool RunOnce => TestFlag(Constants.Runner.CommandLine.Flags.Once);

public bool AllowDockerInDocker => TestFlag(Constants.Runner.CommandLine.Flags.AllowDockerInDocker);

// Constructor.
public CommandSettings(IHostContext context, string[] args)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Runner.Listener/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ public async Task<int> ExecuteCommand(CommandSettings command)
Trace.Info($"Set runner startup type - {startType}");
HostContext.StartupType = startType;

Trace.Info($"Set runner allowDockerInDocker - {command.AllowDockerInDocker}");
HostContext.AllowDockerInDocker = command.AllowDockerInDocker;

// Run the runner interactively or as service
return await RunAsync(settings, command.RunOnce);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Runner.Worker/ContainerOperationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ public interface IContainerOperationProvider : IRunnerService
public class ContainerOperationProvider : RunnerService, IContainerOperationProvider
{
private IDockerCommandManager _dockerManger;
private bool _allowDockerInDocker;

public override void Initialize(IHostContext hostContext)
{
base.Initialize(hostContext);
_allowDockerInDocker = hostContext.AllowDockerInDocker;
_dockerManger = HostContext.GetService<IDockerCommandManager>();
}

Expand Down Expand Up @@ -57,13 +59,13 @@ public async Task StartContainersAsync(IExecutionContext executionContext, objec
#if OS_WINDOWS
// service CExecSvc is Container Execution Agent.
ServiceController[] scServices = ServiceController.GetServices();
if (scServices.Any(x => String.Equals(x.ServiceName, "cexecsvc", StringComparison.OrdinalIgnoreCase) && x.Status == ServiceControllerStatus.Running))
if (!_allowDockerInDocker && scServices.Any(x => String.Equals(x.ServiceName, "cexecsvc", StringComparison.OrdinalIgnoreCase) && x.Status == ServiceControllerStatus.Running))
{
throw new NotSupportedException("Container feature is not supported when runner is already running inside container.");
}
#else
var initProcessCgroup = File.ReadLines("/proc/1/cgroup");
if (initProcessCgroup.Any(x => x.IndexOf(":/docker/", StringComparison.OrdinalIgnoreCase) >= 0))
if (!_allowDockerInDocker && initProcessCgroup.Any(x => x.IndexOf(":/docker/", StringComparison.OrdinalIgnoreCase) >= 0))
{
throw new NotSupportedException("Container feature is not supported when runner is already running inside container.");
}
Expand Down
13 changes: 13 additions & 0 deletions src/Test/L0/TestHostContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public sealed class TestHostContext : IHostContext, IDisposable
private AssemblyLoadContext _loadContext;
private string _tempDirectoryRoot = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("D"));
private StartupType _startupType;
private bool _allowDockerInDocker;
public event EventHandler Unloading;
public CancellationToken RunnerShutdownToken => _runnerShutdownTokenSource.Token;
public ShutdownReason RunnerShutdownReason { get; private set; }
Expand Down Expand Up @@ -86,6 +87,18 @@ public StartupType StartupType
}
}

public bool AllowDockerInDocker
{
get
{
return _allowDockerInDocker;
}
set
{
_allowDockerInDocker = value;
}
}

public ProductInfoHeaderValue UserAgent => new ProductInfoHeaderValue("L0Test", "0.0");

public RunnerWebProxy WebProxy => new RunnerWebProxy();
Expand Down