Skip to content

Commit

Permalink
usage clean up (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
TingluoHuang authored Aug 30, 2016
1 parent 1a3c9f1 commit 3d23051
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 123 deletions.
58 changes: 25 additions & 33 deletions src/Agent.Listener/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ public async Task<int> ExecuteCommand(CommandSettings command)
return Constants.Agent.ReturnCode.Success;
}

// Unconfigure, remove config files, service and exit
if (command.Unconfigure)
// Configure agent prompt for args if not supplied
// Unattend configure mode will not prompt for args if not supplied and error on any missing or invalid value.
if (command.Configure)
{
try
{
await configManager.UnconfigureAsync(command);
await configManager.ConfigureAsync(command);
return Constants.Agent.ReturnCode.Success;
}
catch (Exception ex)
Expand All @@ -86,19 +87,12 @@ public async Task<int> ExecuteCommand(CommandSettings command)
}
}

if (command.Run && !configManager.IsConfigured())
{
_term.WriteError(StringUtil.Loc("AgentIsNotConfigured"));
PrintUsage();
return Constants.Agent.ReturnCode.TerminatedError;
}

// unattend mode will not prompt for args if not supplied. Instead will error.
if (command.Configure)
// Unconfigure, remove config files, service and exit
if (command.Unconfigure)
{
try
{
await configManager.ConfigureAsync(command);
await configManager.UnconfigureAsync(command);
return Constants.Agent.ReturnCode.Success;
}
catch (Exception ex)
Expand All @@ -109,44 +103,42 @@ public async Task<int> ExecuteCommand(CommandSettings command)
}
}

Trace.Info("Done evaluating commands");
await configManager.EnsureConfiguredAsync(command);

_inConfigStage = false;

if (command.NoStart)
{
Trace.Info("No start.");
return Constants.Agent.ReturnCode.Success;
}

AgentSettings settings = configManager.LoadSettings();
bool runAsService = configManager.IsServiceConfigured();
if (command.Run || !runAsService)

// Run agent
if (command.Run)
{
// Run the agent interactively
// Error if agent not configured.
if (!configManager.IsConfigured())
{
_term.WriteError(StringUtil.Loc("AgentIsNotConfigured"));
PrintUsage();
return Constants.Agent.ReturnCode.TerminatedError;
}

// Run the agent interactively or as service
Trace.Verbose($"Run as service: '{runAsService}'");
return await RunAsync(TokenSource.Token, settings, runAsService);
}

#if OS_WINDOWS
if (File.Exists(Path.Combine(IOUtil.GetBinPath(), "VsoAgentService.exe")))
// this code is for migrated .net windows agent that running as windows service.
// leave the code as is untill we have a real plan for auto-migration.
if (runAsService && configManager.IsConfigured() && File.Exists(Path.Combine(IOUtil.GetBinPath(), "VsoAgentService.exe")))
{
// The old .net windows servicehost doesn't pass correct args while invoke Agent.Listener.exe
// When we detect the agent is a migrated .net windows agent, we will just run the agent.listener.exe even the servicehost doesn't pass correct args.
Trace.Verbose($"Run the agent for compat reason.");
return await RunAsync(TokenSource.Token, settings, runAsService);
}
#endif
if (runAsService)
{
// This is helpful if the user tries to start the agent.listener which is already configured or running as service
// However user can execute the agent by calling the run command
// TODO: Should we check if the service is running and prompt user to start the service if its not already running?
_term.WriteLine(StringUtil.Loc("ConfiguredAsRunAsService"));
}

return Constants.Agent.ReturnCode.Success;
Trace.Info("Doesn't match any existing command option, print usage.");
PrintUsage();
return Constants.Agent.ReturnCode.TerminatedError;
}
finally
{
Expand Down
1 change: 0 additions & 1 deletion src/Agent.Listener/CommandSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public sealed class CommandSettings
// Flags.
public bool Commit => TestFlag(Constants.Agent.CommandLine.Flags.Commit);
public bool Help => TestFlag(Constants.Agent.CommandLine.Flags.Help);
public bool NoStart => TestFlag(Constants.Agent.CommandLine.Flags.NoStart);
public bool Unattended => TestFlag(Constants.Agent.CommandLine.Flags.Unattended);
public bool Version => TestFlag(Constants.Agent.CommandLine.Flags.Version);

Expand Down
10 changes: 0 additions & 10 deletions src/Agent.Listener/Configuration/ConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public interface IConfigurationManager : IAgentService
{
bool IsConfigured();
bool IsServiceConfigured();
Task EnsureConfiguredAsync(CommandSettings command);
Task ConfigureAsync(CommandSettings command);
Task UnconfigureAsync(CommandSettings command);
AgentSettings LoadSettings();
Expand Down Expand Up @@ -55,15 +54,6 @@ public bool IsConfigured()
return result;
}

public async Task EnsureConfiguredAsync(CommandSettings command)
{
Trace.Info(nameof(EnsureConfiguredAsync));
if (!IsConfigured())
{
await ConfigureAsync(command);
}
}

public AgentSettings LoadSettings()
{
Trace.Info(nameof(LoadSettings));
Expand Down
1 change: 0 additions & 1 deletion src/Microsoft.VisualStudio.Services.Agent/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public static class Flags
public static readonly string AcceptTeeEula = "acceptteeeula";
public static readonly string Commit = "commit";
public static readonly string Help = "help";
public static readonly string NoStart = "nostart";
public static readonly string Replace = "replace";
public static readonly string RunAsService = "runasservice";
public static readonly string Unattended = "unattended";
Expand Down
112 changes: 59 additions & 53 deletions src/Misc/layoutbin/en-US/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"CommandKeywordDetected": "'{0}' contains logging command keyword '##vso', but it's not a legal command. Please reference documentaion to fix any potentially syntax error (http://go.microsoft.com/fwlink/?LinkId=817296)",
"CommandNotFound": "Can't find command extension for ##vso[{0}.command]. Please reference documentaion (http://go.microsoft.com/fwlink/?LinkId=817296)",
"CommandProcessFailed": "Unable to process command '{0}' successfully. Please reference documentaion (http://go.microsoft.com/fwlink/?LinkId=817296)",
"ConfiguredAsRunAsService": "Agent is configured to run as service.",
"ConnectingToServer": "Connecting to server ...",
"ConnectSectionHeader": "Connect",
"ConnectToServer": "Connecting to the server.",
Expand Down Expand Up @@ -125,69 +124,58 @@
"Visual Studio Team Services Agent",
"Copyright (c) Microsoft Corporation",
"",
"Run from the directory above the bin agent folder to allow for updates",
"Run the agent using the convenience cmd or shell script wrappers.",
"",
"Alternatively, you can run the convenience cmd or shell script wrappers.",
"Arguments will be passed along",
"Print agent version information:",
"Win : .\\config.cmd --version and config.cmd --commit",
"Unix : ./config.sh --version and ./config.sh --commit",
"",
"Win : run.cmd [command(s)] [arguments] [options]",
"Unix : ./run.sh [command(s)] [arguments] [options]",
"Display command line help:",
"Win : .\\config.cmd --help",
"Unix : ./config.sh --help",
"",
"Command to configure:",
"Win : config.cmd [command(s)] [arguments] [options]",
"Unix : ./config.sh [command(s)] [arguments] [options]",
"Configure the agent and exit:",
"Win : .\\config.cmd [arguments] [options]",
"Unix : ./config.sh [arguments] [options]",
"",
"Command to uninstall:",
"Win : config.cmd remove",
"Unconfigure the agent:",
"Win : .\\config.cmd remove",
"Unix : ./config.sh remove",
"",
"usage:",
"bin/Agent.Listener [command(s)] [arguments] [options]",
"",
"It is common to just run Agent or Agent.Listener with no arguments for an interactive configuration.",
"You will be prompted and walked through all options.",
"",
"",
"Commands:",
"-----------------------------------------------------------------------------",
"(none) Interactively configure and then run the agent.",
" You will be prompted for data.",
"configure Configure the agent and exit.",
"remove Unconfigure the agent.",
"run Runs the agent interactively. must be configured.",
"Run agent interactively (must be configured):",
"Win : .\\run.cmd [arguments] [options]",
"Unix : ./run.sh [arguments] [options]",
"",
"",
"Configure Arguments:",
"-----------------------------------------------------------------------------",
"--url URL of the server. Examples:",
" https://myaccount.visualstudio.com or http://onprem:8080/tfs",
"--agent Agent name",
"--pool Pool name for agent to join",
"--windowslogonaccount Windows logon account name if runasservice option is chosen",
"--windowslogonpassword Windows logon account password if runasservice option is chosen",
"--auth Authentication type. Valid options are PAT (Personal Access Token),",
" Negotiate (Kerberos or NTLM), Integrated (Windows default credentials) and",
" ALT (Alternate Credentials)",
" Negotiate (Kerberos or NTLM) and Integrated (Windows default credentials)",
"--pool Pool name for agent to join",
"--agent Agent name",
"--work Work directory where job data is stored.",
" Defaults to _work under the root of the agent directory.",
" Work directory is owned by a given agent and should not share between multiple agents.",
"--windowslogonaccount Windows logon account name if runasservice option is chosen (Only on Windows)",
"--windowslogonpassword Windows logon account password if runasservice option is chosen (Only on Windows)",
"",
"",
"Remove Arguments:",
"-----------------------------------------------------------------------------",
"--auth Authentication type. Valid options are PAT (Personal Access Token),",
" Negotiate (Kerberos or NTLM), Integrated (Windows default credentials) and",
" ALT (Alternate Credentials)",
" Negotiate (Kerberos or NTLM) and Integrated (Windows default credentials)",
"",
"",
"Options:",
"-----------------------------------------------------------------------------",
"--version Print the version",
"--help Display command line help",
"--unattended Unattended config. You will not be prompted.",
" All answers must be supplied to the command line.",
"--nostart Do not start the agent after interactive configuration.",
"--runasservice Configure the agent as service",
"--replace Replace the agent in a pool. If another agent is listening",
" by that name, it will start failing with a conflict.",
"--unattended Unattended config. You will not be prompted.",
" All answers must be supplied to the command line.",
"--acceptteeeula Accept TEE end user license agreement. (Only on Linux and OSX)",
"--replace Replace the agent in a pool. If another agent is listening",
" by that name, or it will start failing with a conflict.",
"--runasservice Configure the agent as service. (Only on Windows, need administrator permission to configure windows service)",
"",
"",
"Arguments by Auth Type:",
Expand All @@ -201,22 +189,40 @@
"",
"Integrated: does not take any additional arguments",
"",
"ALT:",
"--username alternate username",
"--password alternate password",
"",
"Examples:",
"Unattended configure and unconfigure examples:",
"-----------------------------------------------------------------------------",
"Run with no arguments to start. will configure if not configured yet.",
"$ bin/Agent.Listener",
"",
"Unattend configuration. Remember to check return codes",
"bin/Agent.Listener configure --unattended --url https://myaccount.visualstudio.com --agent myagent --pool default --nostart --acceptteeeula --auth PAT --token o4u5... --work D:\\agent_work",
"bin/Agent.Listener run",
"Remember to check return code when using unattended configuration",
"Unattend configuration for VSTS with PAT authentication",
"Win : .\\config.cmd --unattended --url https://myaccount.visualstudio.com --auth PAT --token <token> --pool default --agent myagent",
"Unix : ./config.sh --unattended --acceptteeeula --url https://myaccount.visualstudio.com --auth PAT --token <token> --pool default --agent myagent",
"",
"Unattend configuration for on-premise TFS with integrated (windows) and (Linux/OSX) negotiate authentication",
"bin/Agent.Listener configure --unattended --url http://mylocaltfsserver:8080/tfs --agent myagent --pool default --nostart --acceptteeeula --auth integrated",
"bin/Agent.Listener configure --unattended --url http://mylocaltfsserver:8080/tfs --agent myagent --pool default --nostart --acceptteeeula --auth negotiate --username DEV-BOX\\developer --password MyPassword"
"Win : .\\config.cmd --unattended --url http://mylocaltfsserver:8080/tfs --auth Integrated --pool default --agent myagent",
"Unix : ./config.sh --unattended --acceptteeeula --url http://mylocaltfsserver:8080/tfs --auth Negotiate --username DOMAIN\\USER_NAME --password MyPassword --pool default --agent myagent",
"",
"Unattend configuration for VSTS with PAT authentication and replace existing agent with same agent name",
"Win : .\\config.cmd --unattended --url https://myaccount.visualstudio.com --auth PAT --token <token> --pool default --agent myagent --replace",
"Unix : ./config.sh --unattended --acceptteeeula --url https://myaccount.visualstudio.com --auth PAT --token <token> --pool default --agent myagent --replace",
"",
"Unattend configuration for VSTS with PAT authentication and specify agent work directory",
"Win : .\\config.cmd --unattended --url https://myaccount.visualstudio.com --auth PAT --token <token> --pool default --agent myagent --work D:\\agent_work",
"Unix : ./config.sh --unattended --acceptteeeula --url https://myaccount.visualstudio.com --auth PAT --token <token> --pool default --agent myagent --work usr/local/agent_work",
"",
"Unattend configuration for on-premise TFS with integrated authentication and configure agent as Windows service run as NetworkService",
"Win : .\\config.cmd --unattended --url http://mylocaltfsserver:8080/tfs --auth Integrated --pool default --agent myagent --runasservice",
"",
"Unattend configuration for on-premise TFS with integrated authentication and configure agent as Windows service run as a domain account",
"Win : .\\config.cmd --unattended --url http://mylocaltfsserver:8080/tfs --auth Integrated --pool default --agent myagent --runasservice --windowslogonaccount DOMAIN\\USER_NAME --windowslogonpassword MyPassword",
"",
"Unattend unconfiguration for VSTS with PAT authentication",
"Win : .\\config.cmd remove --unattended --auth PAT --token <token>",
"Unix : ./config.sh remove --unattended --auth PAT --token <token>",
"",
"Unattend unconfiguration for on-premise TFS with integrated (windows) and (Linux/OSX) negotiate authentication",
"Win : .\\config.cmd remove --unattended --auth Integrated",
"Unix : ./config.sh remove --unattended --auth Negotiate --username DOMAIN\\USER_NAME --password MyPassword",
""
],
"ListenForJobs": "{0:u}: Listening for Jobs",
"LocalClockSkewed": "The local machine's clock may be out of sync with the server time by more than five minutes. Please sync your clock with your domain or internet time and try again.",
Expand Down
9 changes: 2 additions & 7 deletions src/Test/L0/Listener/AgentL0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Moq;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
Expand Down Expand Up @@ -89,8 +88,6 @@ public async void TestRunAsync()
.Returns(settings);
_configurationManager.Setup(x => x.IsConfigured())
.Returns(true);
_configurationManager.Setup(x => x.EnsureConfiguredAsync(It.IsAny<CommandSettings>()))
.Returns(Task.CompletedTask);
_messageListener.Setup(x => x.CreateSessionAsync(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<bool>(true));
_messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny<CancellationToken>()))
Expand Down Expand Up @@ -122,7 +119,7 @@ public async void TestRunAsync()
hc.EnqueueInstance<IJobDispatcher>(_jobDispatcher.Object);

//Act
var command = new CommandSettings(hc, new string[0]);
var command = new CommandSettings(hc, new string[] { "run" });
Task agentTask = agent.ExecuteCommand(command);

//Assert
Expand Down Expand Up @@ -159,10 +156,8 @@ public async void TestRunAsync()
{
// staring with run command, configured as run as service, should start the agent
{ new [] { "run" }, true, Times.Once() },
// starting with no argument, configured as run as service, should not start agent
{ new string[] { }, true, Times.Never() },
// starting with no argument, configured not to run as service, should start agent interactively
{ new string[] { }, false, Times.Once() }
{ new [] { "run" }, false, Times.Once() }
};
[Theory]
[MemberData("RunAsServiceTestData")]
Expand Down
Loading

0 comments on commit 3d23051

Please sign in to comment.