Daemon can now build images

This commit is contained in:
Roberto Alsina 2024-06-30 13:11:35 -03:00
parent 35fa820029
commit eb063beb2c
6 changed files with 53 additions and 5 deletions

View File

@ -7,7 +7,7 @@ RUN mkdir src/
COPY src/* src/ COPY src/* src/
RUN shards install RUN shards install
RUN shards build -d --error-trace RUN shards build -d --error-trace
RUN strip bin/faaso-daemon RUN strip bin/*
FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine as ship FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine as ship
RUN apk add tinyproxy multirun openssl zlib yaml pcre2 gc libevent libgcc libxml2 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 WORKDIR /home/app
COPY tinyproxy.conf ./ 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"] CMD ["/usr/bin/multirun", "faaso-daemon", "tinyproxy -d -c tinyproxy.conf"]

View File

@ -1,6 +1,6 @@
build: shard.yml $(wildcard src/**/*cr) build: shard.yml $(wildcard src/**/*cr)
shards build shards build
proxy-image: build proxy: build
docker build . -t faaso-proxy --no-cache docker build . -t faaso-proxy --no-cache
start-proxy: start-proxy:
docker run --network=faaso-net -v /var/run/docker.sock:/var/run/docker.sock -p 8888:8888 faaso-proxy docker run --network=faaso-net -v /var/run/docker.sock:/var/run/docker.sock -p 8888:8888 faaso-proxy

View File

@ -1,3 +1,4 @@
get "/" do get "/" do
"Hello World Crystal!" "Hello World Crystal!"
end end

View File

@ -26,3 +26,5 @@ dependencies:
github: kemalcr/kemal-basic-auth github: kemalcr/kemal-basic-auth
crinja: crinja:
github: straight-shoota/crinja github: straight-shoota/crinja
crystar:
github: naqvis/crystar

View File

@ -1,12 +1,17 @@
require "crystar"
require "compress/gzip"
require "docr" require "docr"
require "kemal"
require "kemal-basic-auth" require "kemal-basic-auth"
require "kemal"
require "uuid"
# FIXME: make configurable # FIXME: make configurable
basic_auth "admin", "admin" basic_auth "admin", "admin"
current_config = "" current_config = ""
# Bump proxy config to current docker state, returns
# new proxy config
get "/" do get "/" do
"Updating routing" "Updating routing"
# Get all the funkos, create routes for them all # Get all the funkos, create routes for them all
@ -43,4 +48,43 @@ ReversePath "/admin/" "http://127.0.0.1:3000/"
proxy_config proxy_config
end 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 Kemal.run

View File

@ -1,3 +1,4 @@
Port 8888 Port 8888
Listen 0.0.0.0 Listen 0.0.0.0
Timeout 600 Timeout 600