faaso/src/daemon/main.cr

73 lines
1.7 KiB
Crystal
Raw Normal View History

2024-07-02 14:27:27 +00:00
require "./funkos.cr"
2024-07-01 14:48:52 +00:00
require "./proxyconf.cr"
2024-07-02 14:27:27 +00:00
require "./secrets.cr"
2024-06-30 16:11:35 +00:00
require "compress/gzip"
2024-07-01 13:16:48 +00:00
require "crystar"
require "docr"
2024-06-30 14:55:46 +00:00
require "kemal-basic-auth"
2024-06-30 16:11:35 +00:00
require "kemal"
require "uuid"
2024-06-30 14:55:46 +00:00
# FIXME: make configurable
basic_auth "admin", "admin"
2024-06-30 18:38:51 +00:00
# Bring up the funko
get "/funko/:name/up/" do |env|
name = env.params.url["name"]
response = run_faaso(["up", name])
if response["exit_code"] != 0
halt env, status_code: 500, response: response.to_json
else
response.to_json
end
end
2024-06-30 16:11:35 +00:00
# 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
2024-06-30 18:23:16 +00:00
response = run_faaso(["build", tmp_dir.to_s])
if response["exit_code"] != 0
halt env, status_code: 500, response: response.to_json
else
response.to_json
end
end
def run_faaso(args : Array(String))
2024-06-30 16:11:35 +00:00
stderr = IO::Memory.new
stdout = IO::Memory.new
status = Process.run(
command: "faaso",
2024-06-30 18:23:16 +00:00
args: args + ["-l"], # Always local in the server
2024-06-30 16:11:35 +00:00
output: stdout,
error: stderr,
)
2024-06-30 18:23:16 +00:00
{
2024-06-30 16:11:35 +00:00
"exit_code" => status.exit_code,
"stdout" => stdout.to_s,
"stderr" => stderr.to_s,
2024-06-30 18:23:16 +00:00
}
2024-06-30 16:11:35 +00:00
end
Kemal.run