From 3f33693d7ae1e0c35cdd02bd888c218e3bf1ee34 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Mon, 31 Jul 2023 23:31:52 -0700 Subject: [PATCH] upgrade to "almost 0.11" (no testing) --- build.zig | 88 +++++++++++------------------------------------ src/lambda.zig | 92 ++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 85 insertions(+), 95 deletions(-) diff --git a/build.zig b/build.zig index 3f1b3d2..79cf230 100644 --- a/build.zig +++ b/build.zig @@ -1,34 +1,24 @@ const builtin = @import("builtin"); const std = @import("std"); -const GitRepoStep = @import("GitRepoStep.zig"); -const CopyStep = @import("CopyStep.zig"); pub fn build(b: *std.build.Builder) !void { - const zfetch_repo = GitRepoStep.create(b, .{ - .url = "https://github.com/truemedian/zfetch", - // .branch = "0.1.10", // branch also takes tags. Tag 0.1.10 isn't quite new enough - .sha = "271cab5da4d12c8f08e67aa0cd5268da100e52f1", - }); - - const copy_deps = CopyStep.create( - b, - "zfetch_deps.zig", - "libs/zfetch/deps.zig", - ); - copy_deps.step.dependOn(&zfetch_repo.step); // Standard release options allow the person running `zig build` to select // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. // const mode = b.standardReleaseOptions(); + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); - const exe = b.addExecutable("bootstrap", "src/main.zig"); + const exe = b.addExecutable(.{ + .name = "bootstrap", + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); - exe.step.dependOn(©_deps.step); - exe.addPackage(getZfetchPackage(b, "libs/zfetch") catch unreachable); - - exe.setBuildMode(.ReleaseSafe); + // exe.setBuildMode(.ReleaseSafe); // TODO: ReleaseSmall is stripped. Maybe best to just leave this to user const debug = b.option(bool, "debug", "Debug mode (do not strip executable)") orelse false; exe.strip = !debug; - exe.install(); + b.installArtifact(exe); // TODO: We can cross-compile of course, but stripping and zip commands // may vary @@ -36,16 +26,10 @@ pub fn build(b: *std.build.Builder) !void { // Package step const package_step = b.step("package", "Package the function"); package_step.dependOn(b.getInstallStep()); - // strip may not be installed or work for the target arch - // TODO: make this much less fragile - const strip = if (debug) - try std.fmt.allocPrint(b.allocator, "true", .{}) - else - try std.fmt.allocPrint(b.allocator, "[ -x /usr/aarch64-linux-gnu/bin/strip ] && /usr/aarch64-linux-gnu/bin/strip {s}", .{b.getInstallPath(exe.install_step.?.dest_dir, exe.install_step.?.artifact.out_filename)}); - defer b.allocator.free(strip); - package_step.dependOn(&b.addSystemCommand(&.{ "/bin/sh", "-c", strip }).step); - const function_zip = b.getInstallPath(exe.install_step.?.dest_dir, "function.zip"); - const zip = try std.fmt.allocPrint(b.allocator, "zip -qj9 {s} {s}", .{ function_zip, b.getInstallPath(exe.install_step.?.dest_dir, exe.install_step.?.artifact.out_filename) }); + // const function_zip = b.getInstallPath(exe.installed_path.?, "function.zip"); + const function_zip = b.getInstallPath(.bin, "function.zip"); + // TODO: https://github.com/hdorio/hwzip.zig/blob/master/src/hwzip.zig + const zip = try std.fmt.allocPrint(b.allocator, "zip -qj9 {s} {s}", .{ function_zip, b.getInstallPath(.bin, exe.out_filename) }); defer b.allocator.free(zip); package_step.dependOn(&b.addSystemCommand(&.{ "/bin/sh", "-c", zip }).step); @@ -68,7 +52,7 @@ pub fn build(b: *std.build.Builder) !void { // need to do anything with the iam role. Otherwise, we'll create/ // get the IAM role and stick the name in a file in our destination // directory to be used later - const iam_role_name_file = b.getInstallPath(exe.install_step.?.dest_dir, "iam_role_name"); + const iam_role_name_file = b.getInstallPath(.bin, "iam_role_name"); iam_role = try std.fmt.allocPrint(b.allocator, "--role $(cat {s})", .{iam_role_name_file}); // defer b.allocator.free(iam_role); if (!fileExists(iam_role_name_file)) { @@ -96,7 +80,7 @@ pub fn build(b: *std.build.Builder) !void { } } const function_name = b.option([]const u8, "function-name", "Function name for Lambda [zig-fn]") orelse "zig-fn"; - const function_name_file = b.getInstallPath(exe.install_step.?.dest_dir, function_name); + const function_name_file = b.getInstallPath(.bin, function_name); const ifstatement = "if [ ! -f {s} ] || [ {s} -nt {s} ]; then if aws lambda get-function --function-name {s} 2>&1 |grep -q ResourceNotFoundException; then echo not found > /dev/null; {s}; else echo found > /dev/null; {s}; fi; fi"; // The architectures option was introduced in 2.2.43 released 2021-10-01 // We want to use arm64 here because it is both faster and cheaper for most @@ -118,7 +102,7 @@ pub fn build(b: *std.build.Builder) !void { } const cmd = try std.fmt.allocPrint(b.allocator, ifstatement, .{ function_name_file, - std.fs.path.dirname(exe.root_src.?.path), + std.fs.path.dirname(exe.root_src.?.path).?, function_name_file, function_name, not_found_fmt, @@ -162,6 +146,8 @@ pub fn build(b: *std.build.Builder) !void { const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); + + // TODO: Add test } } fn fileExists(file_name: []const u8) bool { @@ -176,39 +162,3 @@ fn addArgs(allocator: std.mem.Allocator, original: []const u8, args: [][]const u } return rc; } - -fn getDependency(comptime lib_prefix: []const u8, comptime name: []const u8, comptime root: []const u8) !std.build.Pkg { - const path = lib_prefix ++ "/libs/" ++ name ++ "/" ++ root; - - // We don't actually care if the dependency has been checked out, as - // GitRepoStep will handle that for us - // Make sure that the dependency has been checked out. - // std.fs.cwd().access(path, .{}) catch |err| switch (err) { - // error.FileNotFound => { - // std.log.err("zfetch: dependency '{s}' not checked out", .{name}); - // - // return err; - // }, - // else => return err, - // }; - - return std.build.Pkg{ - .name = name, - .path = .{ .path = path }, - }; -} - -pub fn getZfetchPackage(b: *std.build.Builder, comptime lib_prefix: []const u8) !std.build.Pkg { - var dependencies = b.allocator.alloc(std.build.Pkg, 4) catch unreachable; - - dependencies[0] = try getDependency(lib_prefix, "iguanaTLS", "src/main.zig"); - dependencies[1] = try getDependency(lib_prefix, "network", "network.zig"); - dependencies[2] = try getDependency(lib_prefix, "uri", "uri.zig"); - dependencies[3] = try getDependency(lib_prefix, "hzzp", "src/main.zig"); - - return std.build.Pkg{ - .name = "zfetch", - .path = .{ .path = lib_prefix ++ "/src/main.zig" }, - .dependencies = dependencies, - }; -} diff --git a/src/lambda.zig b/src/lambda.zig index a0edf67..5a5e834 100644 --- a/src/lambda.zig +++ b/src/lambda.zig @@ -1,10 +1,12 @@ const std = @import("std"); -const zfetch = @import("zfetch"); -pub fn run(event_handler: fn (std.mem.Allocator, []const u8) anyerror![]const u8) !void { // TODO: remove inferred error set? +const HandlerFn = *const fn (std.mem.Allocator, []const u8) anyerror![]const u8; + +pub fn run(event_handler: HandlerFn) !void { // TODO: remove inferred error set? const prefix = "http://"; const postfix = "/2018-06-01/runtime/invocation"; - const lambda_runtime_uri = std.os.getenv("AWS_LAMBDA_RUNTIME_API"); + const lambda_runtime_uri = std.os.getenv("AWS_LAMBDA_RUNTIME_API").?; + // TODO: If this is null, go into single use command line mode var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); @@ -12,30 +14,45 @@ pub fn run(event_handler: fn (std.mem.Allocator, []const u8) anyerror![]const u8 const url = try std.fmt.allocPrint(allocator, "{s}{s}{s}/next", .{ prefix, lambda_runtime_uri, postfix }); defer allocator.free(url); + const uri = try std.Uri.parse(url); - try zfetch.init(); - defer zfetch.deinit(); + var client: std.http.Client = .{ .allocator = allocator }; + defer client.deinit(); + var empty_headers = std.http.Headers.init(allocator); + defer empty_headers.deinit(); std.log.info("Bootstrap initializing with event url: {s}", .{url}); - var headers = zfetch.Headers.init(allocator); - defer headers.deinit(); while (true) { var req_alloc = std.heap.ArenaAllocator.init(allocator); defer req_alloc.deinit(); const req_allocator = req_alloc.allocator(); - var req = try zfetch.Request.init(req_allocator, url, null); + var req = try client.request(.GET, uri, empty_headers, .{}); defer req.deinit(); + req.start() catch |err| { // Well, at this point all we can do is shout at the void + std.log.err("Get fail (start): {}", .{err}); + std.os.exit(0); + continue; + }; + // Lambda freezes the process at this line of code. During warm start, // the process will unfreeze and data will be sent in response to client.get - req.do(.GET, headers, null) catch |err| { - std.log.err("Get fail: {}", .{err}); + req.wait() catch |err| { // Well, at this point all we can do is shout at the void + std.log.err("Get fail (wait): {}", .{err}); + std.os.exit(0); + continue; + }; + if (req.response.status != .ok) { // Documentation says something about "exit immediately". The // Lambda infrastrucutre restarts, so it's unclear if that's necessary. // It seems as though a continue should be fine, and slightly faster // std.os.exit(1); + std.log.err("Get fail: {} {s}", .{ + @intFromEnum(req.response.status), + req.response.status.phrase() orelse "", + }); continue; - }; + } var request_id: ?[]const u8 = null; var content_length: ?usize = null; @@ -84,48 +101,71 @@ pub fn run(event_handler: fn (std.mem.Allocator, []const u8) anyerror![]const u8 } const event_response = event_handler(req_allocator, resp_payload.items) catch |err| { // Stack trace will return null if stripped - const return_trace = @errorReturnTrace(); - std.log.err("Caught error: {}. Return Trace: {}", .{ err, return_trace }); + const return_trace = @errorReturnTrace() orelse "no return trace available"; + std.log.err("Caught error: {}. Return Trace: {s}", .{ err, return_trace }); const err_url = try std.fmt.allocPrint(req_allocator, "{s}{s}/runtime/invocation/{s}/error", .{ prefix, lambda_runtime_uri, req_id }); defer req_allocator.free(err_url); + const err_uri = try std.Uri.parse(err_url); const content = \\ {s} \\ "errorMessage": "{s}", \\ "errorType": "HandlerReturnedError", - \\ "stackTrace": [ "{}" ] + \\ "stackTrace": [ "{s}" ] \\ {s} ; const content_fmt = try std.fmt.allocPrint(req_allocator, content, .{ "{", @errorName(err), return_trace, "}" }); defer req_allocator.free(content_fmt); std.log.err("Posting to {s}: Data {s}", .{ err_url, content_fmt }); - var err_req = try zfetch.Request.init(req_allocator, err_url, null); - defer err_req.deinit(); - var err_headers = zfetch.Headers.init(req_allocator); + var err_headers = std.http.Headers.init(req_allocator); defer err_headers.deinit(); - err_headers.append(.{ - .name = "Lambda-Runtime-Function-Error-Type", - .value = "HandlerReturned", - }) catch |append_err| { + err_headers.append( + "Lambda-Runtime-Function-Error-Type", + "HandlerReturned", + ) catch |append_err| { std.log.err("Error appending error header to post response for request id {s}: {}", .{ req_id, append_err }); std.os.exit(0); continue; }; - // TODO: Determine why this post is not returning - err_req.do(.POST, err_headers, content_fmt) catch |post_err| { // Well, at this point all we can do is shout at the void + var err_req = try client.request(.POST, err_uri, empty_headers, .{}); + defer err_req.deinit(); + err_req.start() catch |post_err| { // Well, at this point all we can do is shout at the void std.log.err("Error posting response for request id {s}: {}", .{ req_id, post_err }); std.os.exit(0); continue; }; + + err_req.wait() catch |post_err| { // Well, at this point all we can do is shout at the void + std.log.err("Error posting response for request id {s}: {}", .{ req_id, post_err }); + std.os.exit(0); + continue; + }; + // TODO: Determine why this post is not returning + if (err_req.response.status != .ok) { + // Documentation says something about "exit immediately". The + // Lambda infrastrucutre restarts, so it's unclear if that's necessary. + // It seems as though a continue should be fine, and slightly faster + // std.os.exit(1); + std.log.err("Get fail: {} {s}", .{ + @intFromEnum(err_req.response.status), + err_req.response.status.phrase() orelse "", + }); + continue; + } std.log.err("Post complete", .{}); continue; }; - // We should catch these potential alloc errors too + // TODO: We should catch these potential alloc errors too + // TODO: This whole loop should be in another function so we can catch everything at once const response_url = try std.fmt.allocPrint(req_allocator, "{s}{s}{s}/{s}/response", .{ prefix, lambda_runtime_uri, postfix, req_id }); + defer allocator.free(response_url); + const response_uri = try std.Uri.parse(response_url); const response_content = try std.fmt.allocPrint(req_allocator, "{s} \"content\": \"{s}\" {s}", .{ "{", event_response, "}" }); - var resp_req = try zfetch.Request.init(req_allocator, response_url, null); + var resp_req = try client.request(.POST, response_uri, empty_headers, .{}); defer resp_req.deinit(); - resp_req.do(.POST, headers, response_content) catch |err| { + try resp_req.start(); + try resp_req.writeAll(response_content); // TODO: AllocPrint + writeAll makes no sense + resp_req.wait() catch |err| { // TODO: report error std.log.err("Error posting response for request id {s}: {}", .{ req_id, err }); continue;