Streaming responses (WIP)

This commit is contained in:
Roberto Alsina 2024-07-06 20:28:58 -03:00
parent 86e2db39fb
commit b611ed199b
4 changed files with 55 additions and 62 deletions

View File

@ -47,14 +47,18 @@ module Faaso
begin begin
Log.info { "Uploading funko to #{FAASO_SERVER}" } Log.info { "Uploading funko to #{FAASO_SERVER}" }
response = Crest.post( Log.info { "Starting remote build:" }
Crest.post(
url, url,
{"funko.tgz" => File.open(tmp), "name" => "funko.tgz"}, {"funko.tgz" => File.open(tmp), "name" => "funko.tgz"},
user: "admin", password: "admin" user: "admin", password: "admin"
) ) do |response|
loop do
Log.info { response.body_io.gets }
break if response.body_io.closed?
end
end
Log.info { "Build finished successfully." } Log.info { "Build finished successfully." }
body = JSON.parse(response.body)
Log.info { body["output"] }
rescue ex : Crest::InternalServerError rescue ex : Crest::InternalServerError
Log.error(exception: ex) { "Error building funko #{funko.name} from #{funko.path}" } Log.error(exception: ex) { "Error building funko #{funko.name} from #{funko.path}" }
return 1 return 1

View File

@ -28,21 +28,27 @@ module Faaso
def remote(options, name, scale) : Int32 def remote(options, name, scale) : Int32
if !scale if !scale
response = Crest.get( Crest.get(
"#{FAASO_SERVER}funkos/#{name}/scale/", \ "#{FAASO_SERVER}funkos/#{name}/scale/", \
user: "admin", password: "admin") user: "admin", password: "admin") do |response|
loop do
Log.info { response.body_io.gets }
break if response.body_io.closed?
end
end
else else
response = Crest.post( Crest.post(
"#{FAASO_SERVER}funkos/#{name}/scale/", "#{FAASO_SERVER}funkos/#{name}/scale/",
{"scale" => scale}, user: "admin", password: "admin") {"scale" => scale}, user: "admin", password: "admin") do |response|
loop do
Log.info { response.body_io.gets }
break if response.body_io.closed?
end
end
end end
body = JSON.parse(response.body)
Log.info { body["output"] }
0 0
rescue ex : Crest::InternalServerError rescue ex : Crest::InternalServerError
Log.error { "Error scaling funko #{name}" } Log.error(exception: ex) { "Error scaling funko #{name}" }
body = JSON.parse(ex.response.body)
Log.info { body["output"] }
1 1
end end

View File

@ -5,6 +5,11 @@ module Faaso
funko = Funko::Funko.from_names([name])[0] funko = Funko::Funko.from_names([name])[0]
status = funko.docker_status status = funko.docker_status
if status.images.size == 0
Log.error { "Unkown funko: #{name}" }
return 1
end
Log.info { "Name: #{status.@name}" } Log.info { "Name: #{status.@name}" }
Log.info { "Scale: #{status.scale}" } Log.info { "Scale: #{status.scale}" }
@ -21,16 +26,17 @@ module Faaso
end end
def remote(options, name) : Int32 def remote(options, name) : Int32
response = Crest.get( Crest.get(
"#{FAASO_SERVER}funkos/#{name}/status/", \ "#{FAASO_SERVER}funkos/#{name}/status/", \
user: "admin", password: "admin") user: "admin", password: "admin") do |response|
body = JSON.parse(response.body) loop do
Log.info { body["output"] } Log.info { response.body_io.gets }
break if response.body_io.closed?
end
end
0 0
rescue ex : Crest::InternalServerError rescue ex : Crest::InternalServerError
Log.error { "Error scaling funko #{name}" } Log.error(exception: ex) { "Error scaling funko #{name}" }
body = JSON.parse(ex.response.body)
Log.info { body["output"] }
1 1
end end

View File

@ -8,39 +8,20 @@ module Funko
# Get the funko's status # Get the funko's status
get "/funkos/:name/status/" do |env| get "/funkos/:name/status/" do |env|
name = env.params.url["name"] name = env.params.url["name"]
response = run_faaso(["status", name]) run_faaso(["status", name], env)
if response["exit_code"] != 0
halt env, status_code: 500, response: response.to_json
else
response.to_json
end
end end
# Get the funko's scale # Get the funko's scale
get "/funkos/:name/scale/" do |env| get "/funkos/:name/scale/" do |env|
name = env.params.url["name"] name = env.params.url["name"]
response = run_faaso(["scale", name]) run_faaso(["scale", name], env)
if response["exit_code"] != 0
halt env, status_code: 500, response: response.to_json
else
response.to_json
end
end end
# Set the funko's scale # Set the funko's scale
post "/funkos/:name/scale/" do |env| post "/funkos/:name/scale/" do |env|
name = env.params.url["name"] name = env.params.url["name"]
scale = env.params.body["scale"].as(String) scale = env.params.body["scale"].as(String)
response = run_faaso(["scale", name, scale]) run_faaso(["scale", name, scale], env)
if response["exit_code"] != 0
Log.error { response }
halt env, status_code: 500, response: response.to_json
else
Log.info { response }
response.to_json
end
end end
# Build image for funko received as "funko.tgz" # Build image for funko received as "funko.tgz"
@ -66,13 +47,7 @@ module Funko
end end
# Build the thing # Build the thing
response = run_faaso(["build", tmp_dir.to_s, "--no-runtime"]) run_faaso(["build", tmp_dir.to_s, "--no-runtime"], env)
if response["exit_code"] != 0
halt env, status_code: 500, response: response.to_json
else
response.to_json
end
end end
# Endpoints for the web frontend # Endpoints for the web frontend
@ -150,21 +125,23 @@ module Funko
"<iframe src='terminal/' width='100%' height='100%'></iframe>" "<iframe src='terminal/' width='100%' height='100%'></iframe>"
end end
# Helper to run faaso locally and get a response back # Helper to run faaso locally and respond via env
def run_faaso(args : Array(String)) def run_faaso(args : Array(String), env) : Bool
Log.info { "Running faaso [#{args.join(", ")}, -l]" } Log.info { "Running faaso [#{args.join(", ")}, -l]" }
output = IO::Memory.new Process.run(
status = Process.run(
command: "faaso", command: "faaso",
args: args + ["-l"], # Always local in the server args: args + ["-l"], # Always local in the server
output: output, ) do |process|
error: output, loop do
) env.response.print process.output.gets(chomp: false)
Log.debug { "faaso output: #{output}" } env.response.flush
result = { Fiber.yield
"exit_code" => status.exit_code, break if process.terminated?
"output" => output.to_s, end
} p! process.error.peek
result true
end
# FIXME: find a way to raise an exception on failure
# of the faaso process
end end
end end