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

Support configure runner as ephemeral. #660

Merged
merged 2 commits into from
Sep 13, 2021
Merged
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
3 changes: 3 additions & 0 deletions src/Runner.Common/ConfigurationStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public sealed class RunnerSettings
[DataMember(EmitDefaultValue = false)]
public string PoolName { get; set; }

[DataMember(EmitDefaultValue = false)]
public bool Ephemeral { get; set; }

[DataMember(EmitDefaultValue = false)]
public string ServerUrl { get; set; }

Expand Down
3 changes: 2 additions & 1 deletion src/Runner.Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,10 @@ public static class Flags
{
public static readonly string Check = "check";
public static readonly string Commit = "commit";
public static readonly string Ephemeral = "ephemeral";
public static readonly string Help = "help";
public static readonly string Replace = "replace";
public static readonly string Once = "once";
public static readonly string Once = "once"; // TODO: Remove in 10/2021
public static readonly string RunAsService = "runasservice";
public static readonly string Unattended = "unattended";
public static readonly string Version = "version";
Expand Down
4 changes: 3 additions & 1 deletion src/Runner.Listener/CommandSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public sealed class CommandSettings
{
Constants.Runner.CommandLine.Flags.Check,
Constants.Runner.CommandLine.Flags.Commit,
Constants.Runner.CommandLine.Flags.Ephemeral,
Constants.Runner.CommandLine.Flags.Help,
Constants.Runner.CommandLine.Flags.Replace,
Constants.Runner.CommandLine.Flags.RunAsService,
Constants.Runner.CommandLine.Flags.Once,
Constants.Runner.CommandLine.Flags.Unattended,
Constants.Runner.CommandLine.Flags.Version
};
Expand Down Expand Up @@ -66,7 +66,9 @@ public sealed class CommandSettings
public bool Help => TestFlag(Constants.Runner.CommandLine.Flags.Help);
public bool Unattended => TestFlag(Constants.Runner.CommandLine.Flags.Unattended);
public bool Version => TestFlag(Constants.Runner.CommandLine.Flags.Version);
public bool Ephemeral => TestFlag(Constants.Runner.CommandLine.Flags.Ephemeral);

// TODO: Remove in 10/2021
public bool RunOnce => TestFlag(Constants.Runner.CommandLine.Flags.Once);

// Constructor.
Expand Down
12 changes: 8 additions & 4 deletions src/Runner.Listener/Configuration/ConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public async Task ConfigureAsync(CommandSettings command)
TaskAgent agent;
while (true)
{
runnerSettings.Ephemeral = command.Ephemeral;
runnerSettings.AgentName = command.GetRunnerName();

_term.WriteLine();
Expand All @@ -210,7 +211,7 @@ public async Task ConfigureAsync(CommandSettings command)
if (command.GetReplace())
{
// Update existing agent with new PublicKey, agent version.
agent = UpdateExistingAgent(agent, publicKey, userLabels);
agent = UpdateExistingAgent(agent, publicKey, userLabels, runnerSettings.Ephemeral);

try
{
Expand All @@ -233,7 +234,7 @@ public async Task ConfigureAsync(CommandSettings command)
else
{
// Create a new agent.
agent = CreateNewAgent(runnerSettings.AgentName, publicKey, userLabels);
agent = CreateNewAgent(runnerSettings.AgentName, publicKey, userLabels, runnerSettings.Ephemeral);

try
{
Expand Down Expand Up @@ -458,7 +459,7 @@ private ICredentialProvider GetCredentialProvider(CommandSettings command, strin
}


private TaskAgent UpdateExistingAgent(TaskAgent agent, RSAParameters publicKey, ISet<string> userLabels)
private TaskAgent UpdateExistingAgent(TaskAgent agent, RSAParameters publicKey, ISet<string> userLabels, bool ephemeral)
{
ArgUtil.NotNull(agent, nameof(agent));
agent.Authorization = new TaskAgentAuthorization
Expand All @@ -469,6 +470,8 @@ private TaskAgent UpdateExistingAgent(TaskAgent agent, RSAParameters publicKey,
// update should replace the existing labels
agent.Version = BuildConstants.RunnerPackage.Version;
agent.OSDescription = RuntimeInformation.OSDescription;
agent.Ephemeral = ephemeral;
TingluoHuang marked this conversation as resolved.
Show resolved Hide resolved
agent.MaxParallelism = 1;

agent.Labels.Clear();

Expand All @@ -484,7 +487,7 @@ private TaskAgent UpdateExistingAgent(TaskAgent agent, RSAParameters publicKey,
return agent;
}

private TaskAgent CreateNewAgent(string agentName, RSAParameters publicKey, ISet<string> userLabels)
private TaskAgent CreateNewAgent(string agentName, RSAParameters publicKey, ISet<string> userLabels, bool ephemeral)
{
TaskAgent agent = new TaskAgent(agentName)
{
Expand All @@ -495,6 +498,7 @@ private TaskAgent CreateNewAgent(string agentName, RSAParameters publicKey, ISet
MaxParallelism = 1,
Version = BuildConstants.RunnerPackage.Version,
OSDescription = RuntimeInformation.OSDescription,
Ephemeral = ephemeral,
};

agent.Labels.Add(new AgentLabel("self-hosted", LabelType.System));
Expand Down
18 changes: 14 additions & 4 deletions src/Runner.Listener/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public async Task<int> ExecuteCommand(CommandSettings command)
HostContext.StartupType = startType;

// Run the runner interactively or as service
return await RunAsync(settings, command.RunOnce);
return await RunAsync(settings, command.RunOnce || settings.Ephemeral); // TODO: Remove RunOnce later.
}
else
{
Expand Down Expand Up @@ -466,8 +466,16 @@ private async Task<int> RunAsync(RunnerSettings settings, bool runOnce = false)
await jobDispatcher.ShutdownAsync();
}

//TODO: make sure we don't mask more important exception
await _listener.DeleteSessionAsync();
try
{
await _listener.DeleteSessionAsync();
}
catch (Exception ex) when (runOnce)
{
// ignore exception during delete session for ephemeral runner since the runner might already be deleted from the server side
// and the delete session call will ends up with 401.
Trace.Info($"Ignore any exception during DeleteSession for an ephemeral runner. {ex}");
}

messageQueueLoopTokenSource.Dispose();
}
Expand Down Expand Up @@ -512,7 +520,9 @@ private void PrintUsage(CommandSettings command)
--labels string Extra labels in addition to the default: 'self-hosted,{Constants.Runner.Platform},{Constants.Runner.PlatformArchitecture}'
--work string Relative runner work directory (default {Constants.Path.WorkDirectory})
--replace Replace any existing runner with the same name (default false)
--pat GitHub personal access token used for checking network connectivity when executing `.{separator}run.{ext} --check`");
--pat GitHub personal access token used for checking network connectivity when executing `.{separator}run.{ext} --check`
--ephemeral Configure the runner to only take one job and then let the service un-configure the runner after the job finishes (default false)");

#if OS_WINDOWS
_term.WriteLine($@" --runasservice Run the runner as a service");
_term.WriteLine($@" --windowslogonaccount string Account to run the service as. Requires runasservice");
Expand Down
11 changes: 11 additions & 0 deletions src/Sdk/DTWebApi/WebApi/TaskAgentReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ protected TaskAgentReference(TaskAgentReference referenceToBeCloned)
this.OSDescription = referenceToBeCloned.OSDescription;
this.ProvisioningState = referenceToBeCloned.ProvisioningState;
this.AccessPoint = referenceToBeCloned.AccessPoint;
this.Ephemeral = referenceToBeCloned.Ephemeral;

if (referenceToBeCloned.m_links != null)
{
Expand Down Expand Up @@ -81,6 +82,16 @@ public Boolean? Enabled
set;
}

/// <summary>
/// Signifies that this Agent can only run one job and will be removed by the server after that one job finish.
/// </summary>
[DataMember]
public bool? Ephemeral
{
get;
set;
}

/// <summary>
/// Whether or not the agent is online.
/// </summary>
Expand Down
15 changes: 9 additions & 6 deletions src/Test/L0/Listener/RunnerL0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ public async void TestRunOnce()
runner.Initialize(hc);
var settings = new RunnerSettings
{
PoolId = 43242
PoolId = 43242,
Ephemeral = true
};

var message = new TaskAgentMessage()
Expand Down Expand Up @@ -294,7 +295,7 @@ public async void TestRunOnce()

_configStore.Setup(x => x.IsServiceConfigured()).Returns(false);
//Act
var command = new CommandSettings(hc, new string[] { "run", "--once" });
var command = new CommandSettings(hc, new string[] { "run" });
Task<int> runnerTask = runner.ExecuteCommand(command);

//Assert
Expand Down Expand Up @@ -332,7 +333,8 @@ public async void TestRunOnceOnlyTakeOneJobMessage()
runner.Initialize(hc);
var settings = new RunnerSettings
{
PoolId = 43242
PoolId = 43242,
Ephemeral = true
};

var message1 = new TaskAgentMessage()
Expand Down Expand Up @@ -390,7 +392,7 @@ public async void TestRunOnceOnlyTakeOneJobMessage()

_configStore.Setup(x => x.IsServiceConfigured()).Returns(false);
//Act
var command = new CommandSettings(hc, new string[] { "run", "--once" });
var command = new CommandSettings(hc, new string[] { "run" });
Task<int> runnerTask = runner.ExecuteCommand(command);

//Assert
Expand Down Expand Up @@ -431,7 +433,8 @@ public async void TestRunOnceHandleUpdateMessage()
var settings = new RunnerSettings
{
PoolId = 43242,
AgentId = 5678
AgentId = 5678,
Ephemeral = true
};

var message1 = new TaskAgentMessage()
Expand Down Expand Up @@ -475,7 +478,7 @@ public async void TestRunOnceHandleUpdateMessage()

_configStore.Setup(x => x.IsServiceConfigured()).Returns(false);
//Act
var command = new CommandSettings(hc, new string[] { "run", "--once" });
var command = new CommandSettings(hc, new string[] { "run" });
Task<int> runnerTask = runner.ExecuteCommand(command);

//Assert
Expand Down