diff --git a/Dockerfile b/Dockerfile index 2638cab..baf499a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,7 @@ RUN mkdir src/ COPY src/* src/ RUN shards install RUN shards build -d --error-trace -RUN strip bin/faaso-daemon +RUN strip bin/* FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine as ship RUN apk add tinyproxy multirun openssl zlib yaml pcre2 gc libevent libgcc libxml2 @@ -17,6 +17,6 @@ RUN addgroup -S app && adduser app -S -G app WORKDIR /home/app COPY tinyproxy.conf ./ -COPY --from=build /home/app/bin/faaso-daemon ./ +COPY --from=build /home/app/bin/faaso-daemon /home/app/bin/faaso /usr/bin/ -CMD ["/usr/bin/multirun", "./faaso-daemon", "tinyproxy -d -c tinyproxy.conf"] \ No newline at end of file +CMD ["/usr/bin/multirun", "faaso-daemon", "tinyproxy -d -c tinyproxy.conf"] \ No newline at end of file diff --git a/Makefile b/Makefile index fbcae11..e692733 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ build: shard.yml $(wildcard src/**/*cr) shards build -proxy-image: build +proxy: build docker build . -t faaso-proxy --no-cache start-proxy: docker run --network=faaso-net -v /var/run/docker.sock:/var/run/docker.sock -p 8888:8888 faaso-proxy diff --git a/examples/hello_crystal/funko.cr b/examples/hello_crystal/funko.cr index 88ae85f..174454b 100644 --- a/examples/hello_crystal/funko.cr +++ b/examples/hello_crystal/funko.cr @@ -1,3 +1,4 @@ get "/" do "Hello World Crystal!" end + diff --git a/shard.yml b/shard.yml index 13c2689..7e5b33d 100644 --- a/shard.yml +++ b/shard.yml @@ -26,3 +26,5 @@ dependencies: github: kemalcr/kemal-basic-auth crinja: github: straight-shoota/crinja + crystar: + github: naqvis/crystar \ No newline at end of file diff --git a/src/daemon.cr b/src/daemon.cr index 5ea5a5b..ac2501a 100644 --- a/src/daemon.cr +++ b/src/daemon.cr @@ -1,12 +1,17 @@ +require "crystar" +require "compress/gzip" require "docr" -require "kemal" require "kemal-basic-auth" +require "kemal" +require "uuid" # FIXME: make configurable basic_auth "admin", "admin" current_config = "" +# Bump proxy config to current docker state, returns +# new proxy config get "/" do "Updating routing" # Get all the funkos, create routes for them all @@ -43,4 +48,43 @@ ReversePath "/admin/" "http://127.0.0.1:3000/" proxy_config end +# Build image for funko received as "funko.tgz" +# TODO: This may take a while, consider using something like +# mosquito-cr/mosquito to make it a job queue +post "/funko/build/" do |env| + # Create place to build funko + tmp_dir = Path.new("tmp", UUID.random.to_s) + Dir.mkdir_p(tmp_dir) unless File.exists? tmp_dir + + # Expand tarball in there + file = env.params.files["funko.tgz"].tempfile + Compress::Gzip::Reader.open(file) do |gzip| + Crystar::Reader.open(gzip) do |tar| + tar.each_entry do |entry| + File.open(Path.new(tmp_dir, entry.name), "w") do |dst| + IO.copy entry.io, dst + end + end + end + end + + # Build the thing + stderr = IO::Memory.new + stdout = IO::Memory.new + status = Process.run( + command: "faaso", + args: ["build", tmp_dir.to_s], + output: stdout, + error: stderr, + ) + response = { + "exit_code" => status.exit_code, + "stdout" => stdout.to_s, + "stderr" => stderr.to_s, + }.to_json + + halt env, status_code: 500, response: response if status.exit_code != 0 + response +end + Kemal.run diff --git a/tinyproxy.conf b/tinyproxy.conf index 7fd78fd..8ab98e1 100644 --- a/tinyproxy.conf +++ b/tinyproxy.conf @@ -1,3 +1,4 @@ + Port 8888 Listen 0.0.0.0 Timeout 600