From f5467551a1f64bc7644e572a940c404e0e983e48 Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Sun, 30 Jun 2024 15:10:49 -0300 Subject: [PATCH] faaso build works both local and remote. Basic auth for faaso API (lame, hardcoded) --- .ameba.yml | 36 +++++++++++++++- Dockerfile | 2 + examples/hello_crystal/funko.cr | 1 - shard.lock | 12 ++++++ shard.yml | 4 +- src/daemon.cr | 2 +- src/faaso.cr | 73 +++++++++++++++++++++++++++++---- src/funko.cr | 2 +- src/main.cr | 9 ++++ 9 files changed, 128 insertions(+), 13 deletions(-) diff --git a/.ameba.yml b/.ameba.yml index 09c71a0..1de02d4 100644 --- a/.ameba.yml +++ b/.ameba.yml @@ -1,9 +1,9 @@ # This configuration file was generated by `ameba --gen-config` -# on 2024-06-30 14:55:35 UTC using Ameba version 1.6.1. +# on 2024-06-30 18:10:12 UTC using Ameba version 1.6.1. # The point is for the user to remove these configuration records # one by one as the reported problems are removed from the code base. -# Problems found: 9 +# Problems found: 10 # Run `ameba --only Documentation/DocumentationAdmonition` for details Documentation/DocumentationAdmonition: Description: Reports documentation admonitions @@ -19,3 +19,35 @@ Documentation/DocumentationAdmonition: - BUG Enabled: true Severity: Warning + +# Problems found: 1 +# Run `ameba --only Naming/BlockParameterName` for details +Naming/BlockParameterName: + Description: Disallows non-descriptive block parameter names + MinNameLength: 3 + AllowNamesEndingInNumbers: true + Excluded: + - src/faaso.cr + AllowedNames: + - _ + - e + - i + - j + - k + - v + - x + - y + - ex + - io + - ws + - op + - tx + - id + - ip + - k1 + - k2 + - v1 + - v2 + ForbiddenNames: [] + Enabled: true + Severity: Convention diff --git a/Dockerfile b/Dockerfile index baf499a..fd8414f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,6 +16,8 @@ RUN apk add tinyproxy multirun openssl zlib yaml pcre2 gc libevent libgcc libxml RUN addgroup -S app && adduser app -S -G app WORKDIR /home/app +RUN mkdir runtimes +COPY runtimes/* ./runtimes/ COPY tinyproxy.conf ./ COPY --from=build /home/app/bin/faaso-daemon /home/app/bin/faaso /usr/bin/ diff --git a/examples/hello_crystal/funko.cr b/examples/hello_crystal/funko.cr index 174454b..88ae85f 100644 --- a/examples/hello_crystal/funko.cr +++ b/examples/hello_crystal/funko.cr @@ -1,4 +1,3 @@ get "/" do "Hello World Crystal!" end - diff --git a/shard.lock b/shard.lock index 61782b3..48481d1 100644 --- a/shard.lock +++ b/shard.lock @@ -8,6 +8,10 @@ shards: git: https://github.com/mrrooijen/commander.git version: 0.4.0 + crest: + git: https://github.com/mamantoha/crest.git + version: 1.3.13 + crinja: git: https://github.com/straight-shoota/crinja.git version: 0.8.1 @@ -24,6 +28,14 @@ shards: git: https://github.com/crystal-loot/exception_page.git version: 0.4.1 + http-client-digest_auth: + git: https://github.com/mamantoha/http-client-digest_auth.git + version: 0.6.0 + + http_proxy: + git: https://github.com/mamantoha/http_proxy.git + version: 0.10.3 + kemal: git: https://github.com/kemalcr/kemal.git version: 1.5.0 diff --git a/shard.yml b/shard.yml index 7e5b33d..eed8c4d 100644 --- a/shard.yml +++ b/shard.yml @@ -27,4 +27,6 @@ dependencies: crinja: github: straight-shoota/crinja crystar: - github: naqvis/crystar \ No newline at end of file + github: naqvis/crystar + crest: + github: mamantoha/crest \ No newline at end of file diff --git a/src/daemon.cr b/src/daemon.cr index ac2501a..aff5481 100644 --- a/src/daemon.cr +++ b/src/daemon.cr @@ -73,7 +73,7 @@ post "/funko/build/" do |env| stdout = IO::Memory.new status = Process.run( command: "faaso", - args: ["build", tmp_dir.to_s], + args: ["build", "-l", tmp_dir.to_s], output: stdout, error: stderr, ) diff --git a/src/faaso.cr b/src/faaso.cr index 63d1a89..ea86f77 100644 --- a/src/faaso.cr +++ b/src/faaso.cr @@ -1,9 +1,17 @@ require "./funko.cr" require "commander" +require "crest" require "docr" require "docr/utils.cr" +require "json" require "uuid" +# API if you just ran faaso-daemon +FAASO_API = "http://localhost:3000/" + +# API if you are running the proxy image locally +# FAASO_API="http://localhost:8888/admin/" + # Functions as a Service, Ops! module Faaso VERSION = "0.1.0" @@ -34,14 +42,65 @@ module Faaso def run funkos = Funko.from_paths(@arguments) - funkos.each do |funko| - # Create temporary build location - tmp_dir = Path.new("tmp", UUID.random.to_s) - Dir.mkdir_p(tmp_dir) unless File.exists? tmp_dir - funko.prepare_build tmp_dir + local = @options.@bool["local"] - puts "Building function... #{funko.name} in #{tmp_dir}" - funko.build tmp_dir + if local + funkos.each do |funko| + # Create temporary build location + tmp_dir = Path.new("tmp", UUID.random.to_s) + Dir.mkdir_p(tmp_dir) unless File.exists? tmp_dir + funko.prepare_build tmp_dir + + puts "Building function... #{funko.name} in #{tmp_dir}" + funko.build tmp_dir + end + else # Running against a server + funkos.each do |funko| + # Create a tarball for the funko + buf = IO::Memory.new + Compress::Gzip::Writer.open(buf) do |gzip| + Crystar::Writer.open(gzip) do |tw| + Dir.glob("#{funko.path}/**/*").each do |path| + next unless File.file? path + rel_path = Path[path].relative_to funko.path + file_info = File.info(path) + hdr = Crystar::Header.new( + name: rel_path.to_s, + mode: file_info.permissions.to_u32, + size: file_info.size, + ) + tw.write_header(hdr) + tw.write(File.read(path).to_slice) + end + end + end + + tmp = File.tempname + File.open(tmp, "w") do |outf| + outf << buf + end + + url = "#{FAASO_API}funko/build/" + + begin + _response = Crest.post( + url, + {"funko.tgz" => File.open(tmp), "name" => "funko.tgz"}, + user: "admin", password: "admin" + ) + puts "Build finished successfully." + # body = JSON.parse(_response.body) + # puts body["stdout"] + # puts body["stderr"] + rescue ex : Crest::InternalServerError + puts "Error building image." + body = JSON.parse(ex.response.body) + puts body["stdout"] + puts body["stderr"] + puts "Error building funko #{funko.name} from #{funko.path}" + exit 1 + end + end end end end diff --git a/src/funko.cr b/src/funko.cr index 0564f05..c43aef7 100644 --- a/src/funko.cr +++ b/src/funko.cr @@ -80,7 +80,7 @@ class Funko docker_api = Docr::API.new(Docr::Client.new) docker_api.images.build( context: path.to_s, - tags: ["#{name}:latest"]) { } + tags: ["#{name}:latest"]) { |x| puts x } end # Return a list of image IDs for this funko, most recent first diff --git a/src/main.cr b/src/main.cr index 9bb3416..982e71e 100644 --- a/src/main.cr +++ b/src/main.cr @@ -5,6 +5,15 @@ cli = Commander::Command.new do |cmd| cmd.use = "faaso" cmd.long = "Functions as a Service, Open" + cmd.flags.add do |flag| + flag.name = "local" + flag.short = "-l" + flag.long = "--local" + flag.description = "Run commands locally instead of against a FaaSO server." + flag.default = false + flag.persistent = true + end + cmd.commands.add do |command| command.use = "build" command.short = "Build a funko"