Skip to content

Commit

Permalink
And make it all work
Browse files Browse the repository at this point in the history
  • Loading branch information
Denys Zhuravel committed Sep 21, 2020
1 parent bd8b906 commit a9192e9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 57 deletions.
5 changes: 2 additions & 3 deletions src/Amazon.Lambda.Tools/LambdaConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ public static class LambdaConstants
// https://github.com/dotnet/corefx/blob/release/1.0.0/pkg/Microsoft.NETCore.Platforms/runtime.json
internal const string RUNTIME_HIERARCHY = "netcore.runtime.hierarchy.json";

// The runtime identifier used for older Lambda runtimes running on Amazon Linux 1.
internal const string LEGACY_RUNTIME_HIERARCHY_STARTING_POINT = "rhel.7.2-x64";

// The runtime identifier provides the most compatibility with Amazon Linux as most of the plain linux-x64 runtimes in reality target ubuntu/debian distros.
internal const string RUNTIME_HIERARCHY_STARTING_POINT = "rhel.7.2-x64";

public const string AWS_LAMBDA_MANAGED_POLICY_PREFIX = "AWSLambda";

Expand Down
4 changes: 2 additions & 2 deletions src/Amazon.Lambda.Tools/LambdaDotNetCLIWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public int Store(LambdaToolsDefaults defaults, string projectLocation, string ou
arguments.Append($" --manifest \"{fullPackageManifest}\"");


arguments.Append($" --runtime {LambdaUtilities.DetermineRuntimeParameter(targetFramework)}");
arguments.Append($" --runtime {LambdaConstants.RUNTIME_HIERARCHY_STARTING_POINT}");

if(!enableOptimization)
{
Expand Down Expand Up @@ -216,7 +216,7 @@ public int Publish(LambdaToolsDefaults defaults, string projectLocation, string
if (msbuildParameters == null ||
msbuildParameters.IndexOf("--runtime", StringComparison.InvariantCultureIgnoreCase) == -1)
{
arguments.Append($" --runtime {LambdaUtilities.DetermineRuntimeParameter(targetFramework)}");
arguments.Append($" --runtime {LambdaConstants.RUNTIME_HIERARCHY_STARTING_POINT}");
}

if (msbuildParameters == null ||
Expand Down
2 changes: 1 addition & 1 deletion src/Amazon.Lambda.Tools/LambdaPackager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ private static IList<string> CalculateRuntimeHierarchy()

// Use a queue to do a breadth first search through the list of runtimes.
var queue = new Queue<string>();
queue.Enqueue(LambdaConstants.LEGACY_RUNTIME_HIERARCHY_STARTING_POINT);
queue.Enqueue(LambdaConstants.RUNTIME_HIERARCHY_STARTING_POINT);

while(queue.Count > 0)
{
Expand Down
51 changes: 15 additions & 36 deletions src/Amazon.Lambda.Tools/LambdaUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ private static List<Tuple<string, bool>> GetYamlTemplateDefinedParameters(string
continue;

var noEcho = false;
if(parameterBody.Children.ContainsKey(noEchoKey))
if (parameterBody.Children.ContainsKey(noEchoKey))
{
noEcho = bool.Parse(parameterBody.Children[noEchoKey].ToString());
}
Expand All @@ -445,7 +445,7 @@ public static async Task<LayerPackageInfo> LoadLayerPackageInfos(IToolLogger log
return info;

logger.WriteLine("Inspecting Lambda layers for runtime package store manifests");
foreach(var arn in layerVersionArns)
foreach (var arn in layerVersionArns)
{
try
{
Expand Down Expand Up @@ -478,7 +478,7 @@ public static async Task<LayerPackageInfo> LoadLayerPackageInfos(IToolLogger log
ManifestPath = filePath
});
}
catch(Exception e)
catch (Exception e)
{
logger.WriteLine($"... {arn}: Skipped, error inspecting layer. {e.Message}");
}
Expand Down Expand Up @@ -532,7 +532,7 @@ internal static ParseLayerVersionArnResult ParseLayerVersionArn(string layerVers
LambdaToolsException.LambdaErrorCode.ParseLayerVersionArnFail);
}
}

public static string DetermineListDisplayLayerDescription(string description, int maxDescriptionLength)
{
if (string.IsNullOrWhiteSpace(description))
Expand All @@ -541,7 +541,7 @@ public static string DetermineListDisplayLayerDescription(string description, in
{
LayerDescriptionManifest manifest;
var parsed = AttemptToParseLayerDescriptionManifest(description, out manifest);
if(parsed)
if (parsed)
{
if (manifest?.Nlt == LayerDescriptionManifest.ManifestType.RuntimePackageStore)
{
Expand All @@ -560,10 +560,10 @@ public static string DetermineListDisplayLayerDescription(string description, in
{
return description;
}

return description.Substring(0, maxDescriptionLength);
}


public class ConvertManifestToSdkManifestResult
{
Expand Down Expand Up @@ -614,7 +614,7 @@ public static ConvertManifestContentToSdkManifestResult ConvertManifestContentTo
if (string.Equals(attr?.Value, "Microsoft.NET.Sdk", StringComparison.OrdinalIgnoreCase))
return new ConvertManifestContentToSdkManifestResult(false, packageManifestContent);


var root = new XElement("Project");
root.SetAttributeValue("Sdk", "Microsoft.NET.Sdk");

Expand All @@ -629,26 +629,26 @@ public static ConvertManifestContentToSdkManifestResult ConvertManifestContentTo
}
catch (Exception e)
{
throw new LambdaToolsException("Error detecting .NET SDK version: \n\t" + e.Message, LambdaToolsException.LambdaErrorCode.FailedToDetectSdkVersion, e );
throw new LambdaToolsException("Error detecting .NET SDK version: \n\t" + e.Message, LambdaToolsException.LambdaErrorCode.FailedToDetectSdkVersion, e);
}

if (dotnetSdkVersion < LambdaConstants.MINIMUM_DOTNET_SDK_VERSION_FOR_ASPNET_LAYERS)
{
throw new LambdaToolsException($"To create a runtime package store layer for an ASP.NET Core project " +
$"version {LambdaConstants.MINIMUM_DOTNET_SDK_VERSION_FOR_ASPNET_LAYERS} " +
$"version {LambdaConstants.MINIMUM_DOTNET_SDK_VERSION_FOR_ASPNET_LAYERS} " +
"or above of the .NET Core SDK must be installed. " +
"If a 2.1.X SDK is used then the \"dotnet store\" command will include all " +
"of the ASP.NET Core dependencies that are already available in Lambda.",
LambdaToolsException.LambdaErrorCode.LayerNetSdkVersionMismatch);
}

// These were added to make sure the ASP.NET Core dependencies are filter if any of the packages
// depend on them.
// See issue for more info: https://github.com/dotnet/cli/issues/10784
var aspNerCorePackageReference = new XElement("PackageReference");
aspNerCorePackageReference.SetAttributeValue("Include", "Microsoft.AspNetCore.App");
itemGroup.Add(aspNerCorePackageReference);

var aspNerCoreUpdatePackageReference = new XElement("PackageReference");
aspNerCoreUpdatePackageReference.SetAttributeValue("Update", "Microsoft.NETCore.App");
aspNerCoreUpdatePackageReference.SetAttributeValue("Publish", "false");
Expand All @@ -662,38 +662,17 @@ public static ConvertManifestContentToSdkManifestResult ConvertManifestContentTo
if (string.Equals(packageName, "Microsoft.AspNetCore.App", StringComparison.OrdinalIgnoreCase) ||
string.Equals(packageName, "Microsoft.AspNetCore.All", StringComparison.OrdinalIgnoreCase))
continue;

var newRef = new XElement("PackageReference");
newRef.SetAttributeValue("Include", packageName);
newRef.SetAttributeValue("Version", version);
itemGroup.Add(newRef);
}

var updatedDoc = new XDocument(root);
var updatedContent = updatedDoc.ToString();

return new ConvertManifestContentToSdkManifestResult(true, updatedContent);
}

/// <summary>
/// Determines what runtime identifier (RID) to use when running a dotnet CLI command. For the
/// older .NET Lambda runtimes that are not running on Amazon Linux 2 keep using the existing rhel.7.2-x64
/// RID. For all other runtimes that are running on Amazon Linux 2 use the newer linux-x64 RID which did
/// not exist when this tool was first created.
/// </summary>
/// <param name="targetFramework"></param>
/// <returns></returns>
public static string DetermineRuntimeParameter(string targetFramework)
{
switch (targetFramework)
{
case "netcoreapp1.0":
case "netcoreapp2.0":
case "netcoreapp2.1":
return LambdaConstants.LEGACY_RUNTIME_HIERARCHY_STARTING_POINT;
default:
return "linux-x64";
}
return new ConvertManifestContentToSdkManifestResult(true, updatedContent);
}
}
}
15 changes: 0 additions & 15 deletions test/Amazon.Lambda.Tools.Test/UtilitiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,5 @@ public void TestGettingLayerDescriptionForNonDotnetLayer(string fullLayer, strin
var value = LambdaUtilities.DetermineListDisplayLayerDescription(fullLayer, maxLength);
Assert.Equal(displayLayer, value);
}

[Theory]
[InlineData("netcoreapp1.0", "rhel.7.2-x64")]
[InlineData("netcoreapp1.1", "linux-x64")]
[InlineData("netcoreapp2.0", "rhel.7.2-x64")]
[InlineData("netcoreapp2.1", "rhel.7.2-x64")]
[InlineData("netcoreapp2.2", "linux-x64")]
[InlineData("netcoreapp3.0", "linux-x64")]
[InlineData("netcoreapp3.1", "linux-x64")]
[InlineData("netcoreapp6.0", "linux-x64")]
public void TestDetermineRuntimeParameter(string targetFramework, string expectedValue)
{
var runtime = LambdaUtilities.DetermineRuntimeParameter(targetFramework);
Assert.Equal(expectedValue, runtime);
}
}
}

0 comments on commit a9192e9

Please sign in to comment.