Compare commits

...

4 Commits

Author SHA1 Message Date
3b2297e954 Housekeeping 2024-07-09 13:23:21 -03:00
4f4daf5943 Types tweak 2024-07-09 13:23:16 -03:00
2d333c3df1 * More robust stderr/stdout mixing when
running commandsserver side
* More robust streaming responses client side
2024-07-09 13:01:37 -03:00
f015afe7f0 Ensure network exists for start-proxy 2024-07-09 10:57:36 -03:00
9 changed files with 51 additions and 50 deletions

View File

@ -8,6 +8,7 @@ proxy:
all: build proxy all: build proxy
start-proxy: start-proxy:
docker network create faaso-net || true
docker run --name faaso-proxy-one \ docker run --name faaso-proxy-one \
--rm --network=faaso-net \ --rm --network=faaso-net \
-e FAASO_SECRET_PATH=${PWD}/secrets \ -e FAASO_SECRET_PATH=${PWD}/secrets \

14
TODO.md
View File

@ -12,11 +12,12 @@
* ✅ Crystal + Kemal * ✅ Crystal + Kemal
* ✅ Python + Flask * ✅ Python + Flask
* ✅ Nodejs + Express * ✅ Nodejs + Express
* Document * Create a site
* How to create a runtime * Document
* How to create a funko * How to create a runtime
* How to setup the proxy * How to create a funko
* APIs * How to setup the proxy
* APIs
* Sanitize all inputs * Sanitize all inputs
* ✅ Streaming responses in slow operations like scaling down * ✅ Streaming responses in slow operations like scaling down
or building or building
@ -34,7 +35,8 @@
* ✅ Fix `export examples/hello_crystal` it has a `template/` * ✅ Fix `export examples/hello_crystal` it has a `template/`
* ✅ Implement zero-downtime rollout (`faaso deploy`) * ✅ Implement zero-downtime rollout (`faaso deploy`)
* ✅ Cleanup `tmp/whatever` after use * ✅ Cleanup `tmp/whatever` after use
* `faaso scale` remote is broken * ✅ `faaso scale` remote is broken
* ✅ Setup linters/pre-commit/etc
## Things to do but not before release ## Things to do but not before release

View File

@ -58,10 +58,7 @@ module Faaso
{"funko.tgz" => File.open(tmp), "name" => "funko.tgz"}, {"funko.tgz" => File.open(tmp), "name" => "funko.tgz"},
user: user, password: password user: user, password: password
) do |response| ) do |response|
loop do IO.copy(response.body_io, STDOUT)
Log.info { response.body_io.gets }
break if response.body_io.closed?
end
end end
Log.info { "Build finished successfully." } Log.info { "Build finished successfully." }
rescue ex : Crest::InternalServerError rescue ex : Crest::InternalServerError

View File

@ -41,10 +41,7 @@ module Faaso
Crest.get( Crest.get(
"#{Config.server}funkos/#{funko_name}/deploy/", \ "#{Config.server}funkos/#{funko_name}/deploy/", \
user: user, password: password) do |response| user: user, password: password) do |response|
loop do IO.copy(response.body_io, STDOUT)
Log.info { response.body_io.gets }
break if response.body_io.closed?
end
end end
0 0
end end

View File

@ -10,14 +10,14 @@ module Faaso
# In both cases stopped instances after the required # In both cases stopped instances after the required
# scale is reached are deleted. # scale is reached are deleted.
struct Scale struct Scale
def local(options, name : String, scale : Int) : Int32 def local(options, name : String, scale : Int | Nil) : Int32
funko = Funko::Funko.from_names([name])[0] funko = Funko::Funko.from_names([name])[0]
# Asked about scale # Asked about scale
if funko.image_history.empty? if funko.image_history.empty?
Log.error { "Unknown funko #{funko.name}" } Log.error { "Unknown funko #{funko.name}" }
return 1 return 1
end end
if !scale if scale.nil?
Log.info { "Funko #{name} has a scale of #{funko.scale}" } Log.info { "Funko #{name} has a scale of #{funko.scale}" }
return 0 return 0
end end
@ -26,19 +26,21 @@ module Faaso
0 0
end end
def remote(options, name : String, scale : Int) : Int32 def remote(options, name : String, scale : Int | Nil) : Int32
user, password = Config.auth user, password = Config.auth
Faaso.check_version Faaso.check_version
if !scale if scale.nil?
response = Crest.get( Crest.get(
"#{Config.server}funkos/#{name}/scale/", \ "#{Config.server}funkos/#{name}/scale/", \
user: user, password: password) user: user, password: password) do |response|
Log.info { " => " + response.body } IO.copy(response.body_io, STDOUT)
else end
response = Crest.post( return 0
"#{Config.server}funkos/#{name}/scale/", end
{"scale" => scale}, user: user, password: password) Crest.post(
Log.info { " => " + response.body } "#{Config.server}funkos/#{name}/scale/",
{"scale" => scale}, user: user, password: password) do |response|
IO.copy(response.body_io, STDOUT)
end end
0 0
rescue ex : Crest::InternalServerError rescue ex : Crest::InternalServerError
@ -46,7 +48,8 @@ module Faaso
1 1
end end
def run(options, name : String, scale : Int) : Int32 def run(options, name : String, scale) : Int32
scale = scale.try &.to_s.to_i
if options["--local"] if options["--local"]
return local(options, name, scale) return local(options, name, scale)
end end

View File

@ -31,10 +31,7 @@ module Faaso
Crest.get( Crest.get(
"#{Config.server}funkos/#{name}/status/", \ "#{Config.server}funkos/#{name}/status/", \
user: user, password: password) do |response| user: user, password: password) do |response|
loop do IO.copy(response.body_io, STDOUT)
Log.info { response.body_io.gets }
break if response.body_io.closed?
end
end end
0 0
rescue ex : Crest::InternalServerError rescue ex : Crest::InternalServerError

View File

@ -133,20 +133,21 @@ module Funko
end end
# Helper to run faaso locally and respond via env # Helper to run faaso locally and respond via env
def run_faaso(args : Array(String), env) : Bool def run_faaso(args : Array(String), env)
Log.info { "Running faaso [#{args.join(", ")}, -l, 2>&1]" } args << "-l" # Always local in the server
Log.info { "Running faaso [#{args}" }
Process.run( Process.run(
command: "faaso", command: "faaso",
args: args + ["-l", "2>&1"], # Always local in the server args: args,
shell: true, env: {"FAASO_SERVER_SIDE" => "true"},
) do |process| ) do |process|
loop do loop do
env.response.print process.output.gets(chomp: false) data = process.output.gets(chomp: false)
env.response.print data
env.response.flush env.response.flush
Fiber.yield Fiber.yield # Without this the process never ends
break if process.terminated? break if process.terminated?
end end
true
end end
# FIXME: find a way to raise an exception on failure # FIXME: find a way to raise an exception on failure
# of the faaso process # of the faaso process

View File

@ -90,7 +90,11 @@ module Funko
docker_api = Docr::API.new(Docr::Client.new) docker_api = Docr::API.new(Docr::Client.new)
current_scale = self.scale current_scale = self.scale
result = [] of String result = [] of String
return result if current_scale == new_scale
if current_scale == new_scale
Log.info { "Funko #{name} already at scale #{new_scale}" }
return result
end
Log.info { "Scaling #{name} from #{current_scale} to #{new_scale}" } Log.info { "Scaling #{name} from #{current_scale} to #{new_scale}" }
if new_scale > current_scale if new_scale > current_scale

View File

@ -35,29 +35,28 @@ Options:
DOC DOC
ans = Docopt.docopt(doc, ARGV) ans = Docopt.docopt(doc, ARGV)
Oplog.setup(ans["-v"].to_s.to_i) Oplog.setup(ans["-v"].to_s.to_i) unless ENV.fetch("FAASO_SERVER_SIDE", nil)
Log.debug { ans } Log.debug { ans }
status : Int32 = 0
case ans case ans
when .fetch("build", false) when .fetch("build", false)
status = Faaso::Commands::Build.new.run(ans, ans["FOLDER"].as(Array(String))) exit Faaso::Commands::Build.new.run(ans, ans["FOLDER"].as(Array(String)))
when .fetch("deploy", false) when .fetch("deploy", false)
status = Faaso::Commands::Deploy.new.run(ans, ans["FUNKO"].as(String)) exit Faaso::Commands::Deploy.new.run(ans, ans["FUNKO"].as(String))
when .fetch("export", false) when .fetch("export", false)
status = Faaso::Commands::Export.new.run(ans, ans["SOURCE"].as(String), ans["DESTINATION"].as(String)) exit Faaso::Commands::Export.new.run(ans, ans["SOURCE"].as(String), ans["DESTINATION"].as(String))
when .fetch("login", false) when .fetch("login", false)
status = Faaso::Commands::Login.new.run(ans) exit Faaso::Commands::Login.new.run(ans)
when .fetch("new", false) when .fetch("new", false)
status = Faaso::Commands::New.new.run(ans, ans["FOLDER"].as(Array(String))[0]) exit Faaso::Commands::New.new.run(ans, ans["FOLDER"].as(Array(String))[0])
when .fetch("scale", false) when .fetch("scale", false)
status = Faaso::Commands::Scale.new.run(ans, ans["FUNKO"].as(String), ans["SCALE"].as(String).to_i) exit Faaso::Commands::Scale.new.run(ans, ans["FUNKO"].as(String), ans["SCALE"])
when .fetch("secret", false) when .fetch("secret", false)
status = Faaso::Commands::Secret.new.run(ans, ans["FUNKO"].as(String), ans["SECRET"].as(String)) exit Faaso::Commands::Secret.new.run(ans, ans["FUNKO"].as(String), ans["SECRET"].as(String))
when .fetch("status", false) when .fetch("status", false)
status = Faaso::Commands::Status.new.run(ans, ans["FUNKO"].as(String)) exit Faaso::Commands::Status.new.run(ans, ans["FUNKO"].as(String))
when .fetch("version", false) when .fetch("version", false)
Log.info { "#{version}" } Log.info { "#{version}" }
end end
exit(status) exit 0