Skip to content

Commit

Permalink
Add temporary loc function. starting config webapi
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanmacfarlane committed Feb 25, 2016
1 parent 15cf059 commit 7e646a2
Show file tree
Hide file tree
Showing 18 changed files with 304 additions and 87 deletions.
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) Microsoft Corporation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
34 changes: 7 additions & 27 deletions src/Agent.Listener/Configuration/ConfigurationStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.IO;
using Newtonsoft.Json;
using Microsoft.VisualStudio.Services.Agent.Util;

namespace Microsoft.VisualStudio.Services.Agent.Configuration
{
Expand Down Expand Up @@ -83,7 +84,7 @@ public CredentialData GetCredentials()
{
if (_creds == null)
{
_creds = Load<CredentialData>(_credFilePath);
_creds = IOUtil.LoadObject<CredentialData>(_credFilePath);
}

return _creds;
Expand All @@ -93,7 +94,7 @@ public AgentSettings GetSettings()
{
if (_settings == null)
{
_settings = Load<AgentSettings>(_configFilePath);
_settings = IOUtil.LoadObject<AgentSettings>(_configFilePath);
}

return _settings;
Expand All @@ -102,35 +103,14 @@ public AgentSettings GetSettings()
public void SaveCredential(CredentialData credential)
{
Trace.Info("Saving {0} credential @ {1}", credential.Scheme, _credFilePath);
Save(credential, _credFilePath);
Trace.Info("Saved.");
IOUtil.SaveObject(credential, _credFilePath);
Trace.Info("Credentials Saved.");
}

public void SaveSettings(AgentSettings settings)
{
Save(settings, _configFilePath);
}

private void Save(Object obj, string path)
{
Trace.Info("Saving to {0}", path);

string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
File.WriteAllText (path, json);
Trace.Info("Written.");
}

private T Load<T>(string path)
{
// TODO: convert many of these Info statements to Verbose

Trace.Info("Loading config from {0}", path);

string json = File.ReadAllText(path);
Trace.Info("Loaded. Length: {0}", json.Length);
T config = JsonConvert.DeserializeObject<T>(json);
Trace.Info("Loaded.");
return config;
IOUtil.SaveObject(settings, _configFilePath);
Trace.Info("Settings Saved.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.Common;

namespace Microsoft.VisualStudio.Services.Agent.Configuration
{
Expand Down Expand Up @@ -28,15 +30,35 @@ public CredentialProvider(string scheme)

public CredentialData CredentialData { get; set; }

// TODO: (bryanmac) abstract GetVSSCredential which knows how to instantiate based off data

public abstract VssCredentials GetVssCredentials(IHostContext context);
public abstract void ReadCredential(IHostContext context, Dictionary<string, string> args, bool enforceSupplied);
}

public sealed class PersonalAccessToken : CredentialProvider
{
public PersonalAccessToken(): base("PAT") {}

public override VssCredentials GetVssCredentials(IHostContext context)
{
TraceSource trace = context.GetTrace("PersonalAccessToken");
trace.Info("GetVssCredentials()");

if (CredentialData == null || !CredentialData.Data.ContainsKey("token"))
{
throw new InvalidOperationException("Must call ReadCredential first.");
}

string token = CredentialData.Data["token"];
trace.Info("token retrieved: {0} chars", token.Length);

// PAT uses a basic credential
VssBasicCredential loginCred = new VssBasicCredential("VstsAgent", token);
VssCredentials creds = new VssClientCredentials(loginCred);
trace.Verbose("cred created");

return creds;
}

public override void ReadCredential(IHostContext context, Dictionary<string, string> args, bool enforceSupplied)
{
TraceSource trace = context.GetTrace("PersonalAccessToken");
Expand All @@ -59,7 +81,31 @@ public override void ReadCredential(IHostContext context, Dictionary<string, str
public sealed class AlternateCredential : CredentialProvider
{
public AlternateCredential(): base("ALT") {}


public override VssCredentials GetVssCredentials(IHostContext context)
{
TraceSource trace = context.GetTrace("PersonalAccessToken");
trace.Info("GetVssCredentials()");

if (CredentialData == null || !CredentialData.Data.ContainsKey("token"))
{
throw new InvalidOperationException("Must call ReadCredential first.");
}

string username = CredentialData.Data["Username"];
trace.Info("username retrieved: {0} chars", username.Length);

string password = CredentialData.Data["Password"];
trace.Info("password retrieved: {0} chars", password.Length);

// PAT uses a basic credential
VssBasicCredential loginCred = new VssBasicCredential(username, password);
VssCredentials creds = new VssClientCredentials(loginCred);
trace.Verbose("cred created");

return creds;
}

public override void ReadCredential(IHostContext context, Dictionary<string, string> args, bool enforceSupplied)
{
var wizard = context.GetService<IConsoleWizard>();
Expand All @@ -81,5 +127,5 @@ public override void ReadCredential(IHostContext context, Dictionary<string, str
args,
enforceSupplied);
}
}
}
}
38 changes: 3 additions & 35 deletions src/Agent.Listener/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static class Program
{
public static Int32 Main(String[] args)
{

using (HostContext context = new HostContext("Agent"))
{
TraceSource _trace = context.GetTrace("AgentProcess");
Expand Down Expand Up @@ -125,42 +126,9 @@ public static async Task<Int32> RunAsync(IHostContext context)

private static void PrintUsage()
{
string usage = @"
usage:
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.
unconfigure Unconfigure the agent.
run Runs the agent interactively. must be configured.
Options:
-----------------------------------------------------------------------------
--unattend Unattended config. You will not be prompted.
All answers must be supplied on cli.
--nostart Do not start the agent after interactive configuration.
--auth Auth type. Valid options are PAT (Personal Access Token) and
ALT (Alternate Credentials)
Options by Auth Type:
-----------------------------------------------------------------------------
PAT
--token Personal Access Token data. Best to paste value in.
ALT
--username alternate username
--password alternate password
";
string usage = StringUtil.Loc("ListenerHelp");
Console.WriteLine(usage);
Console.WriteLine(StringUtil.Loc("Test", "Hello"));
Environment.Exit(0);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Agent.Listener/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Worker()

public void LaunchProcess(String pipeHandleOut, String pipeHandleIn, string workingFolder)
{
string workerFileName = Path.Combine(AssemblyUtil.AssemblyDirectory, WorkerProcessName);
string workerFileName = Path.Combine(IOUtil.GetBinPath(), WorkerProcessName);
_processInvoker = HostContext.GetService<IProcessInvoker>();
_processInvoker.Exited += _processInvoker_Exited;
State = WorkerState.Starting;
Expand Down
2 changes: 1 addition & 1 deletion src/Agent.Listener/WorkerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task Run(JobRequestMessage jobRequestMessage)
_jobsInProgress[jobRequestMessage.JobId] = worker;
worker.ProcessChannel.StartServer( (pipeHandleOut, pipeHandleIn) =>
{
worker.LaunchProcess(pipeHandleOut, pipeHandleIn, AssemblyUtil.AssemblyDirectory);
worker.LaunchProcess(pipeHandleOut, pipeHandleIn, IOUtil.GetBinPath());
}
);
await worker.ProcessChannel.SendAsync(jobRequestMessage, HostContext.CancellationToken);
Expand Down
2 changes: 2 additions & 0 deletions src/Agent.Listener/_project.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"dependencies": {
"NETStandard.Library": "1.0.0-rc3-23721",
"Microsoft.VisualStudio.Services.Agent": "",
"Microsoft.VisualStudio.Services.Client": "0.5.0",
"Microsoft.VisualStudio.Services.Common": "0.5.0",
"Newtonsoft.Json": "7.0.1",
"System.Diagnostics.TraceSource": "4.0.0-rc3-23721"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface IAgentService

public class AgentService
{
public static string Version { get { return "2.0.0"; }}

protected IHostContext HostContext { get; private set; }
protected TraceSource Trace { get; private set; }

Expand Down
26 changes: 26 additions & 0 deletions src/Microsoft.VisualStudio.Services.Agent/Util/ApiUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net.Http.Headers;
using Microsoft.VisualStudio.Services.Agent;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;

namespace Microsoft.VisualStudio.Services.Agent.Util
{
public static class ApiUtil
{
public static VssConnection CreateConnection(Uri serverUri, VssCredentials credentials)
{
VssClientHttpRequestSettings settings = VssClientHttpRequestSettings.Default.Clone();
settings.MaxRetryRequest = 5;

var headerValues = new List<ProductInfoHeaderValue>();
headerValues.Add(new ProductInfoHeaderValue("VstsAgent", AgentService.Version));
VssConnection connection = new VssConnection(serverUri, credentials, settings);
return connection;
//connection.ConnectAsync().SyncResult();
}
}
}

This file was deleted.

28 changes: 28 additions & 0 deletions src/Microsoft.VisualStudio.Services.Agent/Util/IOUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.IO;
using Newtonsoft.Json;

namespace Microsoft.VisualStudio.Services.Agent.Util
{
public static class IOUtil
{
public static void SaveObject(Object obj, string path)
{
string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
File.WriteAllText (path, json);
}

public static T LoadObject<T>(string path)
{
string json = File.ReadAllText(path);
T obj = JsonConvert.DeserializeObject<T>(json);
return obj;
}

public static string GetBinPath()
{
var currentAssemblyLocation = System.Reflection.Assembly.GetEntryAssembly().Location;
return new DirectoryInfo(currentAssemblyLocation).Parent.FullName.ToString();
}
}
}
Loading

0 comments on commit 7e646a2

Please sign in to comment.