Skip to content

Commit 3d23051

Browse files
authored
usage clean up (#523)
1 parent 1a3c9f1 commit 3d23051

File tree

7 files changed

+86
-123
lines changed

7 files changed

+86
-123
lines changed

src/Agent.Listener/Agent.cs

+25-33
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,13 @@ public async Task<int> ExecuteCommand(CommandSettings command)
7070
return Constants.Agent.ReturnCode.Success;
7171
}
7272

73-
// Unconfigure, remove config files, service and exit
74-
if (command.Unconfigure)
73+
// Configure agent prompt for args if not supplied
74+
// Unattend configure mode will not prompt for args if not supplied and error on any missing or invalid value.
75+
if (command.Configure)
7576
{
7677
try
7778
{
78-
await configManager.UnconfigureAsync(command);
79+
await configManager.ConfigureAsync(command);
7980
return Constants.Agent.ReturnCode.Success;
8081
}
8182
catch (Exception ex)
@@ -86,19 +87,12 @@ public async Task<int> ExecuteCommand(CommandSettings command)
8687
}
8788
}
8889

89-
if (command.Run && !configManager.IsConfigured())
90-
{
91-
_term.WriteError(StringUtil.Loc("AgentIsNotConfigured"));
92-
PrintUsage();
93-
return Constants.Agent.ReturnCode.TerminatedError;
94-
}
95-
96-
// unattend mode will not prompt for args if not supplied. Instead will error.
97-
if (command.Configure)
90+
// Unconfigure, remove config files, service and exit
91+
if (command.Unconfigure)
9892
{
9993
try
10094
{
101-
await configManager.ConfigureAsync(command);
95+
await configManager.UnconfigureAsync(command);
10296
return Constants.Agent.ReturnCode.Success;
10397
}
10498
catch (Exception ex)
@@ -109,44 +103,42 @@ public async Task<int> ExecuteCommand(CommandSettings command)
109103
}
110104
}
111105

112-
Trace.Info("Done evaluating commands");
113-
await configManager.EnsureConfiguredAsync(command);
114-
115106
_inConfigStage = false;
116107

117-
if (command.NoStart)
118-
{
119-
Trace.Info("No start.");
120-
return Constants.Agent.ReturnCode.Success;
121-
}
122-
123108
AgentSettings settings = configManager.LoadSettings();
124109
bool runAsService = configManager.IsServiceConfigured();
125-
if (command.Run || !runAsService)
110+
111+
// Run agent
112+
if (command.Run)
126113
{
127-
// Run the agent interactively
114+
// Error if agent not configured.
115+
if (!configManager.IsConfigured())
116+
{
117+
_term.WriteError(StringUtil.Loc("AgentIsNotConfigured"));
118+
PrintUsage();
119+
return Constants.Agent.ReturnCode.TerminatedError;
120+
}
121+
122+
// Run the agent interactively or as service
128123
Trace.Verbose($"Run as service: '{runAsService}'");
129124
return await RunAsync(TokenSource.Token, settings, runAsService);
130125
}
131126

132127
#if OS_WINDOWS
133-
if (File.Exists(Path.Combine(IOUtil.GetBinPath(), "VsoAgentService.exe")))
128+
// this code is for migrated .net windows agent that running as windows service.
129+
// leave the code as is untill we have a real plan for auto-migration.
130+
if (runAsService && configManager.IsConfigured() && File.Exists(Path.Combine(IOUtil.GetBinPath(), "VsoAgentService.exe")))
134131
{
135132
// The old .net windows servicehost doesn't pass correct args while invoke Agent.Listener.exe
136133
// 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.
137134
Trace.Verbose($"Run the agent for compat reason.");
138135
return await RunAsync(TokenSource.Token, settings, runAsService);
139136
}
140137
#endif
141-
if (runAsService)
142-
{
143-
// This is helpful if the user tries to start the agent.listener which is already configured or running as service
144-
// However user can execute the agent by calling the run command
145-
// TODO: Should we check if the service is running and prompt user to start the service if its not already running?
146-
_term.WriteLine(StringUtil.Loc("ConfiguredAsRunAsService"));
147-
}
148138

149-
return Constants.Agent.ReturnCode.Success;
139+
Trace.Info("Doesn't match any existing command option, print usage.");
140+
PrintUsage();
141+
return Constants.Agent.ReturnCode.TerminatedError;
150142
}
151143
finally
152144
{

src/Agent.Listener/CommandSettings.cs

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public sealed class CommandSettings
2020
// Flags.
2121
public bool Commit => TestFlag(Constants.Agent.CommandLine.Flags.Commit);
2222
public bool Help => TestFlag(Constants.Agent.CommandLine.Flags.Help);
23-
public bool NoStart => TestFlag(Constants.Agent.CommandLine.Flags.NoStart);
2423
public bool Unattended => TestFlag(Constants.Agent.CommandLine.Flags.Unattended);
2524
public bool Version => TestFlag(Constants.Agent.CommandLine.Flags.Version);
2625

src/Agent.Listener/Configuration/ConfigurationManager.cs

-10
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public interface IConfigurationManager : IAgentService
2020
{
2121
bool IsConfigured();
2222
bool IsServiceConfigured();
23-
Task EnsureConfiguredAsync(CommandSettings command);
2423
Task ConfigureAsync(CommandSettings command);
2524
Task UnconfigureAsync(CommandSettings command);
2625
AgentSettings LoadSettings();
@@ -55,15 +54,6 @@ public bool IsConfigured()
5554
return result;
5655
}
5756

58-
public async Task EnsureConfiguredAsync(CommandSettings command)
59-
{
60-
Trace.Info(nameof(EnsureConfiguredAsync));
61-
if (!IsConfigured())
62-
{
63-
await ConfigureAsync(command);
64-
}
65-
}
66-
6757
public AgentSettings LoadSettings()
6858
{
6959
Trace.Info(nameof(LoadSettings));

src/Microsoft.VisualStudio.Services.Agent/Constants.cs

-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ public static class Flags
8686
public static readonly string AcceptTeeEula = "acceptteeeula";
8787
public static readonly string Commit = "commit";
8888
public static readonly string Help = "help";
89-
public static readonly string NoStart = "nostart";
9089
public static readonly string Replace = "replace";
9190
public static readonly string RunAsService = "runasservice";
9291
public static readonly string Unattended = "unattended";

src/Misc/layoutbin/en-US/strings.json

+59-53
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
"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)",
5353
"CommandNotFound": "Can't find command extension for ##vso[{0}.command]. Please reference documentaion (http://go.microsoft.com/fwlink/?LinkId=817296)",
5454
"CommandProcessFailed": "Unable to process command '{0}' successfully. Please reference documentaion (http://go.microsoft.com/fwlink/?LinkId=817296)",
55-
"ConfiguredAsRunAsService": "Agent is configured to run as service.",
5655
"ConnectingToServer": "Connecting to server ...",
5756
"ConnectSectionHeader": "Connect",
5857
"ConnectToServer": "Connecting to the server.",
@@ -125,69 +124,58 @@
125124
"Visual Studio Team Services Agent",
126125
"Copyright (c) Microsoft Corporation",
127126
"",
128-
"Run from the directory above the bin agent folder to allow for updates",
127+
"Run the agent using the convenience cmd or shell script wrappers.",
129128
"",
130-
"Alternatively, you can run the convenience cmd or shell script wrappers.",
131-
"Arguments will be passed along",
129+
"Print agent version information:",
130+
"Win : .\\config.cmd --version and config.cmd --commit",
131+
"Unix : ./config.sh --version and ./config.sh --commit",
132132
"",
133-
"Win : run.cmd [command(s)] [arguments] [options]",
134-
"Unix : ./run.sh [command(s)] [arguments] [options]",
133+
"Display command line help:",
134+
"Win : .\\config.cmd --help",
135+
"Unix : ./config.sh --help",
135136
"",
136-
"Command to configure:",
137-
"Win : config.cmd [command(s)] [arguments] [options]",
138-
"Unix : ./config.sh [command(s)] [arguments] [options]",
137+
"Configure the agent and exit:",
138+
"Win : .\\config.cmd [arguments] [options]",
139+
"Unix : ./config.sh [arguments] [options]",
139140
"",
140-
"Command to uninstall:",
141-
"Win : config.cmd remove",
141+
"Unconfigure the agent:",
142+
"Win : .\\config.cmd remove",
142143
"Unix : ./config.sh remove",
143144
"",
144-
"usage:",
145-
"bin/Agent.Listener [command(s)] [arguments] [options]",
146-
"",
147-
"It is common to just run Agent or Agent.Listener with no arguments for an interactive configuration.",
148-
"You will be prompted and walked through all options.",
149-
"",
150-
"",
151-
"Commands:",
152-
"-----------------------------------------------------------------------------",
153-
"(none) Interactively configure and then run the agent.",
154-
" You will be prompted for data.",
155-
"configure Configure the agent and exit.",
156-
"remove Unconfigure the agent.",
157-
"run Runs the agent interactively. must be configured.",
145+
"Run agent interactively (must be configured):",
146+
"Win : .\\run.cmd [arguments] [options]",
147+
"Unix : ./run.sh [arguments] [options]",
158148
"",
159149
"",
160150
"Configure Arguments:",
161151
"-----------------------------------------------------------------------------",
162152
"--url URL of the server. Examples:",
163153
" https://myaccount.visualstudio.com or http://onprem:8080/tfs",
164-
"--agent Agent name",
165-
"--pool Pool name for agent to join",
166-
"--windowslogonaccount Windows logon account name if runasservice option is chosen",
167-
"--windowslogonpassword Windows logon account password if runasservice option is chosen",
168154
"--auth Authentication type. Valid options are PAT (Personal Access Token),",
169-
" Negotiate (Kerberos or NTLM), Integrated (Windows default credentials) and",
170-
" ALT (Alternate Credentials)",
155+
" Negotiate (Kerberos or NTLM) and Integrated (Windows default credentials)",
156+
"--pool Pool name for agent to join",
157+
"--agent Agent name",
171158
"--work Work directory where job data is stored.",
172159
" Defaults to _work under the root of the agent directory.",
173160
" Work directory is owned by a given agent and should not share between multiple agents.",
161+
"--windowslogonaccount Windows logon account name if runasservice option is chosen (Only on Windows)",
162+
"--windowslogonpassword Windows logon account password if runasservice option is chosen (Only on Windows)",
163+
"",
174164
"",
175165
"Remove Arguments:",
176166
"-----------------------------------------------------------------------------",
177167
"--auth Authentication type. Valid options are PAT (Personal Access Token),",
178-
" Negotiate (Kerberos or NTLM), Integrated (Windows default credentials) and",
179-
" ALT (Alternate Credentials)",
168+
" Negotiate (Kerberos or NTLM) and Integrated (Windows default credentials)",
169+
"",
180170
"",
181171
"Options:",
182172
"-----------------------------------------------------------------------------",
183-
"--version Print the version",
184-
"--help Display command line help",
185-
"--unattended Unattended config. You will not be prompted.",
186-
" All answers must be supplied to the command line.",
187-
"--nostart Do not start the agent after interactive configuration.",
188-
"--runasservice Configure the agent as service",
189-
"--replace Replace the agent in a pool. If another agent is listening",
190-
" by that name, it will start failing with a conflict.",
173+
"--unattended Unattended config. You will not be prompted.",
174+
" All answers must be supplied to the command line.",
175+
"--acceptteeeula Accept TEE end user license agreement. (Only on Linux and OSX)",
176+
"--replace Replace the agent in a pool. If another agent is listening",
177+
" by that name, or it will start failing with a conflict.",
178+
"--runasservice Configure the agent as service. (Only on Windows, need administrator permission to configure windows service)",
191179
"",
192180
"",
193181
"Arguments by Auth Type:",
@@ -201,22 +189,40 @@
201189
"",
202190
"Integrated: does not take any additional arguments",
203191
"",
204-
"ALT:",
205-
"--username alternate username",
206-
"--password alternate password",
207192
"",
208-
"Examples:",
193+
"Unattended configure and unconfigure examples:",
209194
"-----------------------------------------------------------------------------",
210-
"Run with no arguments to start. will configure if not configured yet.",
211-
"$ bin/Agent.Listener",
212-
"",
213-
"Unattend configuration. Remember to check return codes",
214-
"bin/Agent.Listener configure --unattended --url https://myaccount.visualstudio.com --agent myagent --pool default --nostart --acceptteeeula --auth PAT --token o4u5... --work D:\\agent_work",
215-
"bin/Agent.Listener run",
195+
"Remember to check return code when using unattended configuration",
196+
"Unattend configuration for VSTS with PAT authentication",
197+
"Win : .\\config.cmd --unattended --url https://myaccount.visualstudio.com --auth PAT --token <token> --pool default --agent myagent",
198+
"Unix : ./config.sh --unattended --acceptteeeula --url https://myaccount.visualstudio.com --auth PAT --token <token> --pool default --agent myagent",
216199
"",
217200
"Unattend configuration for on-premise TFS with integrated (windows) and (Linux/OSX) negotiate authentication",
218-
"bin/Agent.Listener configure --unattended --url http://mylocaltfsserver:8080/tfs --agent myagent --pool default --nostart --acceptteeeula --auth integrated",
219-
"bin/Agent.Listener configure --unattended --url http://mylocaltfsserver:8080/tfs --agent myagent --pool default --nostart --acceptteeeula --auth negotiate --username DEV-BOX\\developer --password MyPassword"
201+
"Win : .\\config.cmd --unattended --url http://mylocaltfsserver:8080/tfs --auth Integrated --pool default --agent myagent",
202+
"Unix : ./config.sh --unattended --acceptteeeula --url http://mylocaltfsserver:8080/tfs --auth Negotiate --username DOMAIN\\USER_NAME --password MyPassword --pool default --agent myagent",
203+
"",
204+
"Unattend configuration for VSTS with PAT authentication and replace existing agent with same agent name",
205+
"Win : .\\config.cmd --unattended --url https://myaccount.visualstudio.com --auth PAT --token <token> --pool default --agent myagent --replace",
206+
"Unix : ./config.sh --unattended --acceptteeeula --url https://myaccount.visualstudio.com --auth PAT --token <token> --pool default --agent myagent --replace",
207+
"",
208+
"Unattend configuration for VSTS with PAT authentication and specify agent work directory",
209+
"Win : .\\config.cmd --unattended --url https://myaccount.visualstudio.com --auth PAT --token <token> --pool default --agent myagent --work D:\\agent_work",
210+
"Unix : ./config.sh --unattended --acceptteeeula --url https://myaccount.visualstudio.com --auth PAT --token <token> --pool default --agent myagent --work usr/local/agent_work",
211+
"",
212+
"Unattend configuration for on-premise TFS with integrated authentication and configure agent as Windows service run as NetworkService",
213+
"Win : .\\config.cmd --unattended --url http://mylocaltfsserver:8080/tfs --auth Integrated --pool default --agent myagent --runasservice",
214+
"",
215+
"Unattend configuration for on-premise TFS with integrated authentication and configure agent as Windows service run as a domain account",
216+
"Win : .\\config.cmd --unattended --url http://mylocaltfsserver:8080/tfs --auth Integrated --pool default --agent myagent --runasservice --windowslogonaccount DOMAIN\\USER_NAME --windowslogonpassword MyPassword",
217+
"",
218+
"Unattend unconfiguration for VSTS with PAT authentication",
219+
"Win : .\\config.cmd remove --unattended --auth PAT --token <token>",
220+
"Unix : ./config.sh remove --unattended --auth PAT --token <token>",
221+
"",
222+
"Unattend unconfiguration for on-premise TFS with integrated (windows) and (Linux/OSX) negotiate authentication",
223+
"Win : .\\config.cmd remove --unattended --auth Integrated",
224+
"Unix : ./config.sh remove --unattended --auth Negotiate --username DOMAIN\\USER_NAME --password MyPassword",
225+
""
220226
],
221227
"ListenForJobs": "{0:u}: Listening for Jobs",
222228
"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.",

src/Test/L0/Listener/AgentL0.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Moq;
55
using System;
66
using System.Collections.Generic;
7-
using System.Reflection;
87
using System.Threading;
98
using System.Threading.Tasks;
109
using Xunit;
@@ -89,8 +88,6 @@ public async void TestRunAsync()
8988
.Returns(settings);
9089
_configurationManager.Setup(x => x.IsConfigured())
9190
.Returns(true);
92-
_configurationManager.Setup(x => x.EnsureConfiguredAsync(It.IsAny<CommandSettings>()))
93-
.Returns(Task.CompletedTask);
9491
_messageListener.Setup(x => x.CreateSessionAsync(It.IsAny<CancellationToken>()))
9592
.Returns(Task.FromResult<bool>(true));
9693
_messageListener.Setup(x => x.GetNextMessageAsync(It.IsAny<CancellationToken>()))
@@ -122,7 +119,7 @@ public async void TestRunAsync()
122119
hc.EnqueueInstance<IJobDispatcher>(_jobDispatcher.Object);
123120

124121
//Act
125-
var command = new CommandSettings(hc, new string[0]);
122+
var command = new CommandSettings(hc, new string[] { "run" });
126123
Task agentTask = agent.ExecuteCommand(command);
127124

128125
//Assert
@@ -159,10 +156,8 @@ public async void TestRunAsync()
159156
{
160157
// staring with run command, configured as run as service, should start the agent
161158
{ new [] { "run" }, true, Times.Once() },
162-
// starting with no argument, configured as run as service, should not start agent
163-
{ new string[] { }, true, Times.Never() },
164159
// starting with no argument, configured not to run as service, should start agent interactively
165-
{ new string[] { }, false, Times.Once() }
160+
{ new [] { "run" }, false, Times.Once() }
166161
};
167162
[Theory]
168163
[MemberData("RunAsServiceTestData")]

0 commit comments

Comments
 (0)