Return success/failure in commands uniformly

This commit is contained in:
Roberto Alsina 2024-07-05 00:13:13 -03:00
parent 33cd8be45b
commit 6eb9c49c22
7 changed files with 53 additions and 28 deletions

View File

@ -2,7 +2,7 @@ module Faaso
module Commands module Commands
# Build images for one or more funkos from source # Build images for one or more funkos from source
struct Build struct Build
def run(options, folders : Array(String)) def run(options, folders : Array(String)) : Int32
funkos = Funko::Funko.from_paths(folders) funkos = Funko::Funko.from_paths(folders)
if options["--local"] if options["--local"]
@ -58,10 +58,11 @@ module Faaso
body = JSON.parse(ex.response.body) body = JSON.parse(ex.response.body)
Log.info { body["stdout"] } Log.info { body["stdout"] }
Log.error { body["stderr"] } Log.error { body["stderr"] }
exit 1 return 1
end end
end end
end end
0
end end
end end
end end

View File

@ -1,7 +1,7 @@
module Faaso module Faaso
module Commands module Commands
struct Export struct Export
def run(options, source : String, destination : String) def run(options, source : String, destination : String) : Int32
funko = Funko::Funko.from_paths([source])[0] funko = Funko::Funko.from_paths([source])[0]
# Create temporary build location # Create temporary build location
dst_path = destination dst_path = destination
@ -12,6 +12,7 @@ module Faaso
Log.info { "Exporting #{funko.path} to #{dst_path}" } Log.info { "Exporting #{funko.path} to #{dst_path}" }
Dir.mkdir_p(dst_path) Dir.mkdir_p(dst_path)
funko.prepare_build Path[dst_path] funko.prepare_build Path[dst_path]
0
end end
end end
end end

View File

@ -7,17 +7,31 @@ module Faaso
@@known : Array(String) = {{`find ./runtimes -type d -mindepth 1`.split('\n').reject(&.empty?)}} @@known : Array(String) = {{`find ./runtimes -type d -mindepth 1`.split('\n').reject(&.empty?)}}
@@filelist : Array(String) = {{`find ./runtimes -type f -mindepth 1`.split('\n').reject(&.empty?)}} @@filelist : Array(String) = {{`find ./runtimes -type f -mindepth 1`.split('\n').reject(&.empty?)}}
def run(options, folder) def run(options, folder) : Int32
Log.debug { "@@known: #{@@known}"} Log.debug { "@@known: #{@@known}" }
Log.debug { "@@filelist: #{@@filelist}"} Log.debug { "@@filelist: #{@@filelist}" }
if options["-r"].as(String) == "list"
runtime = options["-r"].as(String)
# Give a list of known runtimes
if runtime == "list"
Log.info { "Crystal has some included runtimes:\n" } Log.info { "Crystal has some included runtimes:\n" }
@@known.each do |runtime| @@known.each do |i|
Log.info { " * #{Path[runtime].basename}" } Log.info { " * #{Path[i].basename}" }
end end
Log.info { "\nOr if you have your own, use a folder name" } Log.info { "\nOr if you have your own, use a folder name" }
return 0 return 0
end end
# Create folder with a preconfigured funko for this runtime
if @@known.includes? "./runtimes/#{runtime}"
Log.info { "Using known runtime #{runtime}" }
elsif File.exists? runtime
Log.info { "Using directory #{runtime} as runtime" }
else
Log.error { "Can't find runtime #{runtime}" }
return 1
end
0
end end
end end
end end

View File

@ -10,7 +10,7 @@ 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, scale) def local(options, name, scale) : Int32
funko = Funko::Funko.from_names([name])[0] funko = Funko::Funko.from_names([name])[0]
# Asked about scale # Asked about scale
if !scale if !scale
@ -20,12 +20,13 @@ module Faaso
# Asked to set scale # Asked to set scale
if funko.image_history.empty? if funko.image_history.empty?
Log.error { "Error: no images available for #{funko.name}:latest" } Log.error { "Error: no images available for #{funko.name}:latest" }
exit 1 return 1
end end
funko.scale(scale.as(String).to_i) funko.scale(scale.as(String).to_i)
0
end end
def remote(options, name, scale) def remote(options, name, scale) : Int32
if !scale if !scale
response = Crest.get( response = Crest.get(
"#{FAASO_SERVER}funkos/#{name}/scale/", \ "#{FAASO_SERVER}funkos/#{name}/scale/", \
@ -37,14 +38,15 @@ module Faaso
end end
body = JSON.parse(response.body) body = JSON.parse(response.body)
Log.info { body["output"] } Log.info { body["output"] }
0
rescue ex : Crest::InternalServerError rescue ex : Crest::InternalServerError
Log.error { "Error scaling funko #{name}" } Log.error { "Error scaling funko #{name}" }
body = JSON.parse(ex.response.body) body = JSON.parse(ex.response.body)
Log.info { body["output"] } Log.info { body["output"] }
exit 1 1
end end
def run(options, name, scale) def run(options, name, scale) : Int32
if options["--local"] if options["--local"]
return local(options, name, scale) return local(options, name, scale)
end end

View File

@ -1,7 +1,7 @@
module Faaso module Faaso
module Commands module Commands
struct Secret struct Secret
def local(options, funko, name, secret) def local(options, funko, name, secret) : Int32
if options["--add"] if options["--add"]
dst_dir = "secrets/#{funko}" dst_dir = "secrets/#{funko}"
Dir.mkdir_p(dst_dir) unless Dir.exists?(dst_dir) Dir.mkdir_p(dst_dir) unless Dir.exists?(dst_dir)
@ -9,9 +9,10 @@ module Faaso
elsif options["--delete"] elsif options["--delete"]
File.delete("secrets/#{funko}/#{name}") File.delete("secrets/#{funko}/#{name}")
end end
0
end end
def remote(options, funko, name, secret) def remote(options, funko, name, secret) : Int32
if options["--add"] if options["--add"]
Crest.post( Crest.post(
"#{FAASO_SERVER}secrets/", "#{FAASO_SERVER}secrets/",
@ -26,12 +27,13 @@ module Faaso
"#{FAASO_SERVER}secrets/#{funko}/#{name}", "#{FAASO_SERVER}secrets/#{funko}/#{name}",
user: "admin", password: "admin") user: "admin", password: "admin")
end end
0
rescue ex : Crest::RequestFailed rescue ex : Crest::RequestFailed
Log.error { "Error #{ex.response.status_code}" } Log.error { "Error #{ex.response.status_code}" }
exit 1 1
end end
def run(options, funko, name) def run(options, funko, name) : Int32
if options["--add"] if options["--add"]
Log.info { "Enter the secret, end with Ctrl-D" } if STDIN.tty? Log.info { "Enter the secret, end with Ctrl-D" } if STDIN.tty?
secret = STDIN.gets_to_end secret = STDIN.gets_to_end

View File

@ -1,7 +1,7 @@
module Faaso module Faaso
module Commands module Commands
struct Status struct Status
def local(options, name) def local(options, name) : Int32
funko = Funko::Funko.from_names([name])[0] funko = Funko::Funko.from_names([name])[0]
status = funko.docker_status status = funko.docker_status
@ -17,22 +17,24 @@ module Faaso
status.images.each do |image| status.images.each do |image|
Log.info { " #{image.repo_tags} #{Time.unix(image.created)}" } Log.info { " #{image.repo_tags} #{Time.unix(image.created)}" }
end end
0
end end
def remote(options, name) def remote(options, name) : Int32
response = Crest.get( response = Crest.get(
"#{FAASO_SERVER}funkos/#{name}/status/", \ "#{FAASO_SERVER}funkos/#{name}/status/", \
user: "admin", password: "admin") user: "admin", password: "admin")
body = JSON.parse(response.body) body = JSON.parse(response.body)
Log.info { body["output"] } Log.info { body["output"] }
0
rescue ex : Crest::InternalServerError rescue ex : Crest::InternalServerError
Log.error { "Error scaling funko #{name}" } Log.error { "Error scaling funko #{name}" }
body = JSON.parse(ex.response.body) body = JSON.parse(ex.response.body)
Log.info { body["output"] } Log.info { body["output"] }
exit 1 1
end end
def run(options, name) def run(options, name) : Int32
if options["--local"] if options["--local"]
return local(options, name) return local(options, name)
end end

View File

@ -67,17 +67,20 @@ ans = Docopt.docopt(doc, ARGV)
LogFormat.setup(ans["-v"].to_s.to_i) LogFormat.setup(ans["-v"].to_s.to_i)
Log.debug { ans } Log.debug { ans }
status : Int32 = 0
case ans case ans
when .fetch("build", false) when .fetch("build", false)
Faaso::Commands::Build.new.run(ans, ans["FOLDER"].as(Array(String))) status = Faaso::Commands::Build.new.run(ans, ans["FOLDER"].as(Array(String)))
when .fetch("export", false) when .fetch("export", false)
Faaso::Commands::Export.new.run(ans, ans["SOURCE"].as(String), ans["DESTINATION"].as(String)) status = Faaso::Commands::Export.new.run(ans, ans["SOURCE"].as(String), ans["DESTINATION"].as(String))
when .fetch("new", false) when .fetch("new", false)
Faaso::Commands::New.new.run(ans, ans["FOLDER"].as(Array(String))[0]) status = Faaso::Commands::New.new.run(ans, ans["FOLDER"].as(Array(String))[0])
when .fetch("scale", false) when .fetch("scale", false)
Faaso::Commands::Scale.new.run(ans, ans["FUNKO"].as(String), ans["SCALE"]) status = Faaso::Commands::Scale.new.run(ans, ans["FUNKO"].as(String), ans["SCALE"])
when .fetch("secret", false) when .fetch("secret", false)
Faaso::Commands::Secret.new.run(ans, ans["FUNKO"].as(String), ans["SECRET"].as(String)) status = Faaso::Commands::Secret.new.run(ans, ans["FUNKO"].as(String), ans["SECRET"].as(String))
when .fetch("status", false) when .fetch("status", false)
Faaso::Commands::Status.new.run(ans, ans["FUNKO"].as(String)) status = Faaso::Commands::Status.new.run(ans, ans["FUNKO"].as(String))
end end
exit(status)