diff --git a/TODO.md b/TODO.md index f68c806..8026f7e 100644 --- a/TODO.md +++ b/TODO.md @@ -35,6 +35,7 @@ * ✅ Implement zero-downtime rollout (`faaso deploy`) * ✅ Cleanup `tmp/whatever` after use * `faaso scale` remote is broken +* ✅ Setup linters/pre-commit/etc ## Things to do but not before release diff --git a/src/commands/build.cr b/src/commands/build.cr index b34f9b0..42748b1 100644 --- a/src/commands/build.cr +++ b/src/commands/build.cr @@ -58,10 +58,7 @@ module Faaso {"funko.tgz" => File.open(tmp), "name" => "funko.tgz"}, user: user, password: password ) do |response| - loop do - Log.info { response.body_io.gets } - break if response.body_io.closed? - end + IO.copy(response.body_io, STDOUT) end Log.info { "Build finished successfully." } rescue ex : Crest::InternalServerError diff --git a/src/commands/deploy.cr b/src/commands/deploy.cr index aafe262..f2bb2e8 100644 --- a/src/commands/deploy.cr +++ b/src/commands/deploy.cr @@ -41,10 +41,7 @@ module Faaso Crest.get( "#{Config.server}funkos/#{funko_name}/deploy/", \ user: user, password: password) do |response| - loop do - Log.info { response.body_io.gets } - break if response.body_io.closed? - end + IO.copy(response.body_io, STDOUT) end 0 end diff --git a/src/commands/scale.cr b/src/commands/scale.cr index df962fe..e83ed5e 100644 --- a/src/commands/scale.cr +++ b/src/commands/scale.cr @@ -10,14 +10,14 @@ module Faaso # In both cases stopped instances after the required # scale is reached are deleted. 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] # Asked about scale if funko.image_history.empty? Log.error { "Unknown funko #{funko.name}" } return 1 end - if !scale + if scale.nil? Log.info { "Funko #{name} has a scale of #{funko.scale}" } return 0 end @@ -26,19 +26,21 @@ module Faaso 0 end - def remote(options, name : String, scale : Int) : Int32 + def remote(options, name : String, scale : Int | Nil) : Int32 user, password = Config.auth Faaso.check_version - if !scale - response = Crest.get( + if scale.nil? + Crest.get( "#{Config.server}funkos/#{name}/scale/", \ - user: user, password: password) - Log.info { " => " + response.body } - else - response = Crest.post( - "#{Config.server}funkos/#{name}/scale/", - {"scale" => scale}, user: user, password: password) - Log.info { " => " + response.body } + user: user, password: password) do |response| + IO.copy(response.body_io, STDOUT) + end + return 0 + end + Crest.post( + "#{Config.server}funkos/#{name}/scale/", + {"scale" => scale}, user: user, password: password) do |response| + IO.copy(response.body_io, STDOUT) end 0 rescue ex : Crest::InternalServerError @@ -46,7 +48,8 @@ module Faaso 1 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"] return local(options, name, scale) end diff --git a/src/commands/status.cr b/src/commands/status.cr index 768382d..ed4ea84 100644 --- a/src/commands/status.cr +++ b/src/commands/status.cr @@ -31,10 +31,7 @@ module Faaso Crest.get( "#{Config.server}funkos/#{name}/status/", \ user: user, password: password) do |response| - loop do - Log.info { response.body_io.gets } - break if response.body_io.closed? - end + IO.copy(response.body_io, STDOUT) end 0 rescue ex : Crest::InternalServerError diff --git a/src/daemon/funko.cr b/src/daemon/funko.cr index 380b2ec..1a61f84 100644 --- a/src/daemon/funko.cr +++ b/src/daemon/funko.cr @@ -134,16 +134,18 @@ module Funko # Helper to run faaso locally and respond via env def run_faaso(args : Array(String), env) : Bool - Log.info { "Running faaso [#{args.join(", ")}, -l, 2>&1]" } + args << "-l" # Always local in the server + Log.info { "Running faaso [#{args}" } Process.run( command: "faaso", - args: args + ["-l", "2>&1"], # Always local in the server - shell: true, + args: args, + env: {"FAASO_SERVER_SIDE" => "true"}, ) do |process| loop do - env.response.print process.output.gets(chomp: false) + data = process.output.gets(chomp: false) + env.response.print data env.response.flush - Fiber.yield + Fiber.yield # Without this the process never ends break if process.terminated? end true diff --git a/src/funko.cr b/src/funko.cr index 4a10f77..71e443c 100644 --- a/src/funko.cr +++ b/src/funko.cr @@ -90,7 +90,11 @@ module Funko docker_api = Docr::API.new(Docr::Client.new) current_scale = self.scale 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}" } if new_scale > current_scale diff --git a/src/main.cr b/src/main.cr index ae9756a..8283e76 100644 --- a/src/main.cr +++ b/src/main.cr @@ -35,29 +35,28 @@ Options: DOC 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 } -status : Int32 = 0 case ans 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) - 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) - 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) - status = Faaso::Commands::Login.new.run(ans) + exit Faaso::Commands::Login.new.run(ans) 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) - 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) - 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) - 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) Log.info { "#{version}" } end -exit(status) +exit 0