-
Notifications
You must be signed in to change notification settings - Fork 867
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
FEATURE 2019014 - Gather telemetry on Agent Azure & Docker Container usage #4166
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
483e01d
FEATURE 2019014 - Gather telemetry on Agent Azure & Docker Container …
LiliaSabitova 2ec7027
Merge branch 'master' into users/LiliaSabitova/ftr2019014
LiliaSabitova 24daa61
Re-write try-catch
LiliaSabitova 4222c1f
Update TaskRunner.cs
LiliaSabitova 9318d99
cleanup
LiliaSabitova ef4da4d
Distinguish AzureInstanceMetadata detection to a separate class
LiliaSabitova b71128d
Merge branch 'master' into users/LiliaSabitova/ftr2019014
kirill-ivlev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Specialized; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Web; | ||
|
||
namespace Agent.Sdk.Util | ||
{ | ||
class AzureInstanceMetadataProvider : IDisposable | ||
{ | ||
private HttpClient _client; | ||
private const string _version = "2021-02-01"; | ||
private const string _azureMetadataEndpoint = "http://169.254.169.254/metadata"; | ||
|
||
public AzureInstanceMetadataProvider() | ||
{ | ||
_client = new HttpClient(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_client?.Dispose(); | ||
_client = null; | ||
} | ||
|
||
private HttpRequestMessage BuildRequest(string url, Dictionary<string, string> parameters) | ||
{ | ||
UriBuilder builder = new UriBuilder(url); | ||
|
||
NameValueCollection queryParameters = HttpUtility.ParseQueryString(builder.Query); | ||
|
||
if (!parameters.ContainsKey("api-version")) | ||
{ | ||
parameters.Add("api-version", _version); | ||
} | ||
|
||
foreach (KeyValuePair<string, string> entry in parameters) | ||
{ | ||
queryParameters[entry.Key] = entry.Value; | ||
} | ||
|
||
builder.Query = queryParameters.ToString(); | ||
|
||
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, builder.Uri); | ||
request.Headers.Add("Metadata", "true"); | ||
|
||
return request; | ||
} | ||
|
||
public string GetMetadata(string category, Dictionary<string, string> parameters) | ||
{ | ||
if (_client == null) | ||
{ | ||
throw new ObjectDisposedException(nameof(AzureInstanceMetadataProvider)); | ||
} | ||
|
||
HttpRequestMessage request = BuildRequest($"{_azureMetadataEndpoint}/{category}", parameters); | ||
HttpResponseMessage response = _client.SendAsync(request).Result; | ||
|
||
if (!response.IsSuccessStatusCode) | ||
{ | ||
string errorText = response.Content.ReadAsStringAsync().Result; | ||
throw new Exception($"Error retrieving metadata category { category }. Received status {response.StatusCode}: {errorText}"); | ||
} | ||
|
||
return response.Content.ReadAsStringAsync().Result; | ||
} | ||
|
||
public bool HasMetadata() | ||
{ | ||
try | ||
{ | ||
return GetMetadata("instance", new Dictionary<string, string> { ["format"] = "text" }) != null; | ||
} | ||
catch (Exception) | ||
{ | ||
return false; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
|
@@ -15,6 +16,8 @@ | |
using Microsoft.VisualStudio.Services.Agent.Util; | ||
using Microsoft.Win32; | ||
using Newtonsoft.Json; | ||
using System.ServiceProcess; | ||
using Agent.Sdk.Util; | ||
|
||
namespace Agent.Sdk | ||
{ | ||
|
@@ -355,6 +358,59 @@ public async static Task<bool> DoesSystemPersistsInNet6Whitelist() | |
|
||
return net6SupportedSystems.Any((s) => s.Equals(systemId)); | ||
} | ||
public static bool DetectDockerContainer() | ||
{ | ||
bool isDockerContainer = false; | ||
|
||
try | ||
{ | ||
if (PlatformUtil.RunningOnWindows) | ||
{ | ||
// For Windows we check Container Execution Agent Service (cexecsvc) existence | ||
var serviceName = "cexecsvc"; | ||
ServiceController[] scServices = ServiceController.GetServices(); | ||
if (scServices.Any(x => String.Equals(x.ServiceName, serviceName, StringComparison.OrdinalIgnoreCase) && x.Status == ServiceControllerStatus.Running)) | ||
{ | ||
isDockerContainer = true; | ||
} | ||
} | ||
else | ||
{ | ||
// In Unix in control group v1, we can identify if a process is running in a Docker | ||
var initProcessCgroup = File.ReadLines("/proc/1/cgroup"); | ||
if (initProcessCgroup.Any(x => x.IndexOf(":/docker/", StringComparison.OrdinalIgnoreCase) >= 0)) | ||
{ | ||
isDockerContainer = true; | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
// Logging exception will be handled by JobRunner | ||
throw; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very low priority: try {} catch {} doesn't have any impact since the exception is just rethrown and "throw;" preserves the callstack. My suggestion is just remove the try / catch |
||
return isDockerContainer; | ||
} | ||
|
||
public static bool DetectAzureVM() | ||
{ | ||
bool isAzureVM = false; | ||
|
||
try | ||
{ | ||
// Metadata information endpoint can be used to check whether we're in Azure VM | ||
// Additional info: https://learn.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service?tabs=linux | ||
using var metadataProvider = new AzureInstanceMetadataProvider(); | ||
if (metadataProvider.HasMetadata()) | ||
isAzureVM = true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
// Logging exception will be handled by JobRunner | ||
throw; | ||
} | ||
return isAzureVM; | ||
} | ||
} | ||
|
||
#pragma warning disable CS0659 // Type overrides Object.Equals(object o) but does not override Object.GetHashCode() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No tracing, no filter on the exception, will make issues difficult to debug