Skip to content

Commit

Permalink
Merge pull request #121 from litmus/dependabot/npm_and_yarn/sparticuz…
Browse files Browse the repository at this point in the history
…/chromium-119.0.2

Bump @sparticuz/chromium from 119.0.0 to 119.0.2
  • Loading branch information
brianfeucht authored Jan 19, 2024
2 parents 4d3dfd1 + 1bd24a9 commit 787c471
Show file tree
Hide file tree
Showing 20 changed files with 224 additions and 10 deletions.
25 changes: 19 additions & 6 deletions .github/workflows/sample-app-docker-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,36 @@ name: Validate Nuget Package Docker Build
on: [pull_request]

jobs:
build:

lambda-docker-build-dotnet:
strategy:
fail-fast: false
matrix:
version: [6, 7, 8]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.x
dotnet-version: ${{ matrix.version }}.x
- run: dotnet tool install -g Amazon.Lambda.Tools
- name: Build and package nuget
uses: cake-build/cake-action@v1
with:
cake-version: 1.3.0
- name: Build and package lambda
working-directory: ./sample/SampleLambda
working-directory: ./sample/SampleLambda-dotnet${{ matrix.version }}
run: dotnet lambda package SampleLambda --profile default --region us-west-1
- name: Run docker
run: docker run --detach -p 9000:8080 samplelambda:latest
run: docker run --detach -p 9000:8080 samplelambda-dotnet${{ matrix.version }}:latest
- name: Test function
run: curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
run: |
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}' -o /tmp/lambda_result.txt
if $(grep -q iVB /tmp/lambda_result.txt); then
echo "SUCCESS: The lambda rendered a PNG"
exit 0
else
echo "FAILURE: The lambda did not return expected png value starting with iVB"
cat /tmp/lambda_result.txt
exit 1
fi
shell: bash
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "HeadlessChromium.Puppeteer.Lambda.Dotnet",
"version": "0.0.1-dev",
"dependencies": {
"@sparticuz/chromium": "119.0.0"
"@sparticuz/chromium": "119.0.2"
}
}
10 changes: 10 additions & 0 deletions sample/SampleLambda-dotnet6/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM public.ecr.aws/lambda/dotnet:6

# Set the image's internal work directory
WORKDIR /var/task

# Copy function code
COPY "bin/Release/lambda-publish" .

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "SampleLambda::SampleLambda.HelloWorldHandler::Handle" ]
File renamed without changes.
27 changes: 27 additions & 0 deletions sample/SampleLambda-dotnet6/SampleLambda.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<None Remove="aws-lambda-tools-defaults.json" />
</ItemGroup>

<ItemGroup>
<Content Include="aws-lambda-tools-defaults.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="2.1.1" />
<PackageReference Include="HeadlessChromium.Puppeteer.Lambda.Dotnet" Version="1.1.0-dev" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="PuppeteerSharp" Version="13.0.1" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions sample/SampleLambda-dotnet6/aws-lambda-tools-defaults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"configuration": "Release",
"framework": "net6.0",
"function-runtime": "dotnet6",
"function-memory-size": 1536,
"function-timeout": 60,
"function-handler": "SampleLambda::SampleLambda.HelloWorldHandler::Handle",
"package-type": "image",
"docker-host-build-output-dir": "./bin/Release/lambda-publish"
}
File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions sample/SampleLambda-dotnet7/HelloWorldHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Amazon.Lambda.Core;
using HeadlessChromium.Puppeteer.Lambda.Dotnet;
using Microsoft.Extensions.Logging;

namespace SampleLambda
{
public class HelloWorldHandler
{
[LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
public async Task<byte[]> Handle(ILambdaContext context)
{
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
var browserLauncher = new HeadlessChromiumPuppeteerLauncher(loggerFactory);

await using (var browser = await browserLauncher.LaunchAsync())
await using (var page = await browser.NewPageAsync())
{
await page.GoToAsync("https://www.google.com");
await page.ScreenshotAsync("./google.png");
}

return await File.ReadAllBytesAsync("./google.png");
}
}
}
File renamed without changes.
6 changes: 6 additions & 0 deletions sample/SampleLambda-dotnet7/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="MyLocalSharedSource" value="..\..\artifacts\" />
</packageSources>
</configuration>
10 changes: 10 additions & 0 deletions sample/SampleLambda-dotnet8/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM public.ecr.aws/lambda/dotnet:8-preview

# Set the image's internal work directory
WORKDIR /var/task

# Copy function code
COPY "bin/Release/lambda-publish" .

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "SampleLambda::SampleLambda.HelloWorldHandler::Handle" ]
25 changes: 25 additions & 0 deletions sample/SampleLambda-dotnet8/HelloWorldHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Amazon.Lambda.Core;
using HeadlessChromium.Puppeteer.Lambda.Dotnet;
using Microsoft.Extensions.Logging;

namespace SampleLambda
{
public class HelloWorldHandler
{
[LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
public async Task<byte[]> Handle(ILambdaContext context)
{
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
var browserLauncher = new HeadlessChromiumPuppeteerLauncher(loggerFactory);

await using (var browser = await browserLauncher.LaunchAsync())
await using (var page = await browser.NewPageAsync())
{
await page.GoToAsync("https://www.google.com");
await page.ScreenshotAsync("./google.png");
}

return await File.ReadAllBytesAsync("./google.png");
}
}
}
27 changes: 27 additions & 0 deletions sample/SampleLambda-dotnet8/SampleLambda.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<None Remove="aws-lambda-tools-defaults.json" />
</ItemGroup>

<ItemGroup>
<Content Include="aws-lambda-tools-defaults.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="2.1.1" />
<PackageReference Include="HeadlessChromium.Puppeteer.Lambda.Dotnet" Version="1.1.0-dev" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="PuppeteerSharp" Version="13.0.1" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions sample/SampleLambda-dotnet8/aws-lambda-tools-defaults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"configuration": "Release",
"framework": "net8.0",
"function-runtime": "dotnet8",
"function-memory-size": 1536,
"function-timeout": 60,
"function-handler": "SampleLambda::SampleLambda.HelloWorldHandler::Handle",
"package-type": "image",
"docker-host-build-output-dir": "./bin/Release/lambda-publish"
}
6 changes: 6 additions & 0 deletions sample/SampleLambda-dotnet8/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="MyLocalSharedSource" value="..\..\artifacts\" />
</packageSources>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.IO.Compression;
using HeadlessChromium.Puppeteer.Lambda.Dotnet.Tar;
using System.Linq;

namespace HeadlessChromium.Puppeteer.Lambda.Dotnet
{
Expand All @@ -20,6 +21,38 @@ public class ChromiumExtractor
private readonly ILogger<ChromiumExtractor> logger;
private readonly ILoggerFactory loggerFactory;

private string awsOperatingSystem;
public string AwsOperatingSystem
{
get
{
if (awsOperatingSystem != null)
{
return awsOperatingSystem;
}

if(File.Exists("/etc/system-release-cpe"))
{
var osDetails = File
.ReadLines("/etc/system-release-cpe")
.FirstOrDefault() ?? string.Empty;

if(osDetails.EndsWith("amazon:amazon_linux:2"))
{
awsOperatingSystem = "al2";
}
else if(osDetails.EndsWith("amazon:amazon_linux:2023"))
{
awsOperatingSystem = "al2023";
}
}

return awsOperatingSystem;
}

set => awsOperatingSystem = value;
}

public ChromiumExtractor(ILoggerFactory loggerFactory)
{
this.loggerFactory = loggerFactory;
Expand Down Expand Up @@ -60,7 +93,15 @@ public string ExtractChromium()
{
if (!File.Exists(ChromiumPath))
{
ExtractDependencies("aws.tar.br", "/tmp");
if (!string.IsNullOrEmpty(AwsOperatingSystem))
{
ExtractDependencies($"{AwsOperatingSystem}.tar.br", "/tmp");
}
else
{
logger.LogWarning("Operating environment unexpected. Unable to extract correct dependencies.");
}

ExtractDependencies("swiftshader.tar.br", "/tmp");

var compressedFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "chromium.br");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
<Pack>true</Pack>
<PackageCopyToOutput>true</PackageCopyToOutput>
</Content>
<Content Include="..\..\chrome-aws-lambda\package\bin\aws.tar.br">
<Content Include="..\..\chrome-aws-lambda\package\bin\al2.tar.br">
<Pack>true</Pack>
<PackageCopyToOutput>true</PackageCopyToOutput>
</Content>
<Content Include="..\..\chrome-aws-lambda\package\bin\al2023.tar.br">
<Pack>true</Pack>
<PackageCopyToOutput>true</PackageCopyToOutput>
</Content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public async Task<IBrowser> LaunchAsync(string[] chromeArgs)
{
ExecutablePath = chromeLocation,
Args = chromeArgs,
Headless = true
Headless = true,
};

return await new Launcher(loggerFactory).LaunchAsync(launchOptions);
Expand Down

0 comments on commit 787c471

Please sign in to comment.