-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/release-automation' into feature-work/nuget-val…
…idator
- Loading branch information
Showing
9 changed files
with
321 additions
and
0 deletions.
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
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,17 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
namespace S3Validator | ||
{ | ||
public class Configuration | ||
{ | ||
[YamlDotNet.Serialization.YamlMember(Alias = "base-url")] | ||
public string? BaseUrl { get; set; } | ||
|
||
[YamlDotNet.Serialization.YamlMember(Alias = "directory-list")] | ||
public List<string>? DirectoryList { get; set; } | ||
|
||
[YamlDotNet.Serialization.YamlMember(Alias = "file-list")] | ||
public List<FileDetails>? FileList { get; set; } | ||
} | ||
} |
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,13 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
namespace S3Validator | ||
{ | ||
enum ExitCode : int | ||
{ | ||
Success = 0, | ||
Error = 1, | ||
FileNotFound = 2, | ||
BadArguments = 160 | ||
} | ||
} |
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,14 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
namespace S3Validator | ||
{ | ||
public struct FileDetails | ||
{ | ||
[YamlDotNet.Serialization.YamlMember(Alias = "name")] | ||
public string Name { get; set; } | ||
|
||
[YamlDotNet.Serialization.YamlMember(Alias = "size")] | ||
public long Size { get; set; } | ||
} | ||
} |
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,16 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using CommandLine; | ||
|
||
namespace S3Validator | ||
{ | ||
public class Options | ||
{ | ||
[Option('v', "version", Required = true, HelpText = "Version to validate.")] | ||
public required string Version { get; set; } | ||
|
||
[Option('c', "config", Default = "config.yml", Required = false, HelpText = "Path to the configuration file.")] | ||
public required string ConfigurationPath { get; set; } | ||
} | ||
} |
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,126 @@ | ||
// Copyright 2020 New Relic, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using CommandLine; | ||
|
||
namespace S3Validator | ||
{ | ||
internal class Program | ||
{ | ||
private const string VersionToken = @"{version}"; | ||
|
||
private static readonly HttpClient _client = new(); | ||
|
||
static void Main(string[] args) | ||
{ | ||
var result = Parser.Default.ParseArguments<Options>(args) | ||
.WithParsed(ValidateOptions) | ||
.WithNotParsed(HandleParseError); | ||
|
||
var version = result.Value.Version; | ||
var configuration = LoadConfiguration(result.Value.ConfigurationPath); | ||
Validate(version, configuration); | ||
Console.WriteLine("Valid."); | ||
} | ||
|
||
private static void Validate(string version, Configuration configuration) | ||
{ | ||
var tasks = new List<Task<HttpResponseMessage>>(); | ||
foreach (var dir in configuration.DirectoryList!) | ||
{ | ||
foreach (var fileDetail in configuration.FileList!) | ||
{ | ||
var url = $"{configuration.BaseUrl}/{dir.Replace(VersionToken, version)}/{fileDetail.Name.Replace(VersionToken, version)}"; | ||
var request = new HttpRequestMessage(HttpMethod.Head, url); | ||
request.Options.TryAdd("expectedSize", fileDetail.Size); | ||
tasks.Add(_client.SendAsync(request)); | ||
} | ||
} | ||
|
||
if (!tasks.Any()) | ||
{ | ||
ExitWithError(ExitCode.Error, "There was nothing to validate."); | ||
} | ||
|
||
var taskCompleted = Task.WaitAll(tasks.ToArray(), 10000); | ||
if (!taskCompleted) | ||
{ | ||
ExitWithError(ExitCode.Error, "Validation timed out waiting for HttpClient requests to complete."); | ||
} | ||
|
||
var isValid = true; | ||
var results = new List<string>(); | ||
foreach (var task in tasks) | ||
{ | ||
var status = "Valid"; | ||
|
||
if (!task.Result.IsSuccessStatusCode) | ||
{ | ||
isValid = false; | ||
status = task.Result.StatusCode.ToString(); | ||
} | ||
else if (task.Result.Content.Headers.ContentLength < task.Result.RequestMessage?.Options.GetValue<long>("expectedSize")) | ||
{ | ||
isValid = false; | ||
status = "FileSize"; | ||
} | ||
|
||
results.Add($"{status,-12}{task.Result.RequestMessage?.RequestUri}"); | ||
} | ||
|
||
if (!isValid) | ||
{ | ||
ExitWithError(ExitCode.Error, "Validation failed. Results:" + Environment.NewLine + string.Join(Environment.NewLine, results)); | ||
} | ||
} | ||
|
||
private static void ValidateOptions(Options opts) | ||
{ | ||
if (string.IsNullOrWhiteSpace(opts.Version) | ||
|| string.IsNullOrWhiteSpace(opts.ConfigurationPath)) | ||
{ | ||
ExitWithError(ExitCode.BadArguments, "Arguments were empty or whitespace."); | ||
} | ||
|
||
if (!Version.TryParse(opts.Version, out _)) | ||
{ | ||
ExitWithError(ExitCode.Error, $"Version provided, '{opts.Version}', was not a valid version."); | ||
} | ||
|
||
if (!File.Exists(opts.ConfigurationPath)) | ||
{ | ||
ExitWithError(ExitCode.FileNotFound, $"Configuration file did not exist at {opts.ConfigurationPath}."); | ||
} | ||
} | ||
|
||
private static Configuration LoadConfiguration(string path) | ||
{ | ||
var input = File.ReadAllText(path); | ||
var deserializer = new YamlDotNet.Serialization.Deserializer(); | ||
return deserializer.Deserialize<Configuration>(input); | ||
} | ||
|
||
private static void HandleParseError(IEnumerable<Error> errs) | ||
{ | ||
ExitWithError(ExitCode.BadArguments, "Error occurred while parsing command line arguments."); | ||
} | ||
|
||
public static void ExitWithError(ExitCode exitCode, string message) | ||
{ | ||
Console.WriteLine(message); | ||
Environment.Exit((int)exitCode); | ||
} | ||
} | ||
|
||
public static class Helpers | ||
{ | ||
|
||
// Simplfy the TryGetValue into something more usable. | ||
public static T? GetValue<T>(this HttpRequestOptions options, string key) | ||
{ | ||
options.TryGetValue(new HttpRequestOptionsKey<T>(key), out var value); | ||
return value; | ||
} | ||
} | ||
|
||
} |
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,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<LangVersion>11.0</LangVersion> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="config.yml" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="config.yml"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CommandLineParser" Version="2.9.1" /> | ||
<PackageReference Include="YamlDotNet" Version="13.1.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,66 @@ | ||
# Copyright 2020 New Relic, Inc. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# BaseUrl | ||
# Should include the full path the to root of the agent folder - no trailing slash. Example: https://download.newrelic.com/dot_net_agent | ||
base-url: https://download.newrelic.com/dot_net_agent | ||
|
||
# DirectoryList | ||
# A list of sub directories to check - no leading or trailing slash. The FileList will be checked in each directory. | ||
# Use '{version}' in places where the version would be found. This will be replaced with the version supplied to the tool on execution. | ||
# Example: 'previous_releases/10.13.0' becomes 'previous_releases/{version}' | ||
directory-list: | ||
- latest_release | ||
- previous_releases/{version} | ||
|
||
# FileList | ||
# Use the relative path, starting at each directory in DirectoryList, to each file and a minimum acceptable size. | ||
# Do not use the exact size - this file should not need to be update just to change sizes under normal circumstances. | ||
# Use '{version}' in places where the version would be found. This will be replaced with the version supplied to the tool on execution. | ||
# Example: 'NewRelicDotNetAgent_10.13.0_x64.msi' becomes 'NewRelicDotNetAgent_{version}_x64.msi' | ||
file-list: | ||
- name: NewRelicDotNetAgent_{version}_x64.msi | ||
size: 13000000 | ||
- name: NewRelicDotNetAgent_{version}_x64.zip | ||
size: 11500000 | ||
- name: NewRelicDotNetAgent_{version}_x86.msi | ||
size: 12500000 | ||
- name: NewRelicDotNetAgent_{version}_x86.zip | ||
size: 11500000 | ||
- name: NewRelicDotNetAgent_x64.msi | ||
size: 13000000 | ||
- name: NewRelicDotNetAgent_x86.msi | ||
size: 12500000 | ||
- name: Readme.txt | ||
size: 1500 | ||
- name: newrelic-dotnet-agent-{version}-1.x86_64.rpm | ||
size: 3000000 | ||
- name: newrelic-dotnet-agent_{version}_amd64.deb | ||
size: 2500000 | ||
- name: newrelic-dotnet-agent_{version}_amd64.tar.gz | ||
size: 3900000 | ||
- name: newrelic-dotnet-agent_{version}_arm64.deb | ||
size: 2100000 | ||
- name: newrelic-dotnet-agent_{version}_arm64.tar.gz | ||
size: 3700000 | ||
- name: SHA256/NewRelicDotNetAgent_{version}_x64.msi.sha256 | ||
size: 58 | ||
- name: SHA256/NewRelicDotNetAgent_{version}_x64.zip.sha256 | ||
size: 58 | ||
- name: SHA256/NewRelicDotNetAgent_{version}_x86.msi.sha256 | ||
size: 58 | ||
- name: SHA256/NewRelicDotNetAgent_{version}_x86.zip.sha256 | ||
size: 58 | ||
- name: SHA256/checksums.md | ||
size: 800 | ||
- name: SHA256/newrelic-dotnet-agent-{version}-1.x86_64.rpm.sha256 | ||
size: 95 | ||
- name: SHA256/newrelic-dotnet-agent_{version}_amd64.deb.sha256 | ||
size: 95 | ||
- name: SHA256/newrelic-dotnet-agent_{version}_amd64.tar.gz.sha256 | ||
size: 95 | ||
- name: SHA256/newrelic-dotnet-agent_{version}_arm64.deb.sha256 | ||
size: 95 | ||
- name: SHA256/newrelic-dotnet-agent_{version}_arm64.tar.gz.sha256 | ||
size: 95 | ||
|