|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.Threading.Tasks; |
| 5 | + |
| 6 | +using Amazon.Lambda.Core; |
| 7 | +using Amazon.S3; |
| 8 | + |
| 9 | +[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))] |
| 10 | + |
| 11 | +namespace dump_dotnet6 |
| 12 | +{ |
| 13 | + public class Function |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// Lambda function to dump the container directories /var/lang |
| 17 | + /// and /var/runtime and upload the resulting archive to S3 |
| 18 | + /// </summary> |
| 19 | + /// <returns></returns> |
| 20 | + public async Task<string> FunctionHandler(object invokeEvent, ILambdaContext context) |
| 21 | + { |
| 22 | + string filename = "dotnet6.0.tgz"; |
| 23 | + string cmd = $"tar -cpzf /tmp/{filename} --numeric-owner --ignore-failed-read /var/runtime /var/lang"; |
| 24 | + |
| 25 | + Console.WriteLine($"invokeEvent: {invokeEvent}"); |
| 26 | + Console.WriteLine($"context.RemainingTime: {context.RemainingTime}"); |
| 27 | + |
| 28 | + Console.WriteLine("Parent cmdline:"); |
| 29 | + Console.WriteLine(File.ReadAllText("/proc/1/cmdline").Replace("\0", " ")); |
| 30 | + |
| 31 | + Console.WriteLine("Parent env:"); |
| 32 | + RunShell("xargs --null --max-args=1 < /proc/1/environ"); |
| 33 | + |
| 34 | + Console.WriteLine("This cmdline:"); |
| 35 | + Console.WriteLine(File.ReadAllText($"/proc/{Process.GetCurrentProcess().Id}/cmdline").Replace("\0", " ")); |
| 36 | + |
| 37 | + Console.WriteLine("This env:"); |
| 38 | + RunShell($"xargs --null --max-args=1 < /proc/{Process.GetCurrentProcess().Id}/environ"); |
| 39 | + |
| 40 | + Console.WriteLine($"Current working directory: {Directory.GetCurrentDirectory()}"); |
| 41 | + |
| 42 | + RunShell(cmd); |
| 43 | + |
| 44 | + Console.WriteLine("Zipping done! Uploading..."); |
| 45 | + |
| 46 | + var s3Client = new AmazonS3Client(); |
| 47 | + var response = await s3Client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest |
| 48 | + { |
| 49 | + BucketName = "lambci", |
| 50 | + Key = $"fs/{filename}", |
| 51 | + FilePath = $"/tmp/{filename}", |
| 52 | + CannedACL = S3CannedACL.PublicRead, |
| 53 | + }); |
| 54 | + |
| 55 | + Console.WriteLine("Uploading done!"); |
| 56 | + |
| 57 | + return response.HttpStatusCode.ToString(); |
| 58 | + } |
| 59 | + |
| 60 | + private static Process RunShell(string cmd) |
| 61 | + { |
| 62 | + var escapedArgs = cmd.Replace("\"", "\\\""); |
| 63 | + var process = new Process |
| 64 | + { |
| 65 | + StartInfo = new ProcessStartInfo |
| 66 | + { |
| 67 | + FileName = "/bin/sh", |
| 68 | + Arguments = $"-c \"{escapedArgs}\"", |
| 69 | + UseShellExecute = false, |
| 70 | + CreateNoWindow = true, |
| 71 | + } |
| 72 | + }; |
| 73 | + process.Start(); |
| 74 | + process.WaitForExit(); |
| 75 | + return process; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments