Proper logging

This commit is contained in:
Roberto Alsina 2024-06-30 16:57:39 -03:00
parent 394a004d8d
commit adef802d44
3 changed files with 85 additions and 27 deletions

View File

@ -51,7 +51,7 @@ module Faaso
Dir.mkdir_p(tmp_dir) unless File.exists? tmp_dir Dir.mkdir_p(tmp_dir) unless File.exists? tmp_dir
funko.prepare_build tmp_dir funko.prepare_build tmp_dir
puts "Building function... #{funko.name} in #{tmp_dir}" Log.info { "Building function... #{funko.name} in #{tmp_dir}" }
funko.build tmp_dir funko.build tmp_dir
end end
else # Running against a server else # Running against a server
@ -88,16 +88,15 @@ module Faaso
{"funko.tgz" => File.open(tmp), "name" => "funko.tgz"}, {"funko.tgz" => File.open(tmp), "name" => "funko.tgz"},
user: "admin", password: "admin" user: "admin", password: "admin"
) )
puts "Build finished successfully." Log.info { "Build finished successfully." }
# body = JSON.parse(_response.body) # body = JSON.parse(_response.body)
# puts body["stdout"] # puts body["stdout"]
# puts body["stderr"] # puts body["stderr"]
rescue ex : Crest::InternalServerError rescue ex : Crest::InternalServerError
puts "Error building image." Log.error { "Error building funko #{funko.name} from #{funko.path}" }
body = JSON.parse(ex.response.body) body = JSON.parse(ex.response.body)
puts body["stdout"] Log.info { body["stdout"] }
puts body["stderr"] Log.error { body["stderr"] }
puts "Error building funko #{funko.name} from #{funko.path}"
exit 1 exit 1
end end
end end
@ -133,39 +132,39 @@ module Faaso
response = Crest.get("#{FAASO_API}funko/#{funko.name}/up/", response = Crest.get("#{FAASO_API}funko/#{funko.name}/up/",
user: "admin", password: "admin") user: "admin", password: "admin")
body = JSON.parse(response.body) body = JSON.parse(response.body)
puts body["stdout"] Log.info { body["stdout"] }
next next
rescue ex : Crest::InternalServerError rescue ex : Crest::InternalServerError
puts "Error bringing up #{funko.name}" Log.error { "Error bringing up #{funko.name}" }
body = JSON.parse(ex.response.body) body = JSON.parse(ex.response.body)
puts body["stdout"] Log.info { body["stdout"] }
puts body["stderr"] Log.error { body["stderr"] }
exit 1 exit 1
end end
end end
if funko.image_history.empty? if funko.image_history.empty?
puts "Error: no images available for #{funko.name}:latest" Log.error { "Error: no images available for #{funko.name}:latest" }
next exit 1
end end
case funko case funko
when .running? when .running?
# If it's already up, do nothing # If it's already up, do nothing
# FIXME: bring back out-of-date warning # FIXME: bring back out-of-date warning
puts "#{funko.name} is already up" Log.info { "#{funko.name} is already up" }
when .paused? when .paused?
# If it is paused, unpause it # If it is paused, unpause it
puts "Resuming existing paused container" Log.info { "Resuming existing paused container" }
funko.unpause funko.unpause
when .exited? when .exited?
puts "Starting function #{funko.name}" Log.info { "Starting function #{funko.name}" }
puts "Restarting existing exited container" Log.info { "Restarting existing exited container" }
funko.start funko.start
else else
# Only have an image, deploy from scratch # Only have an image, deploy from scratch
Faaso.setup_network # We need it Faaso.setup_network # We need it
puts "Creating and starting new container" Log.info { "Creating and starting new container" }
funko.create_container(autostart: true) funko.create_container(autostart: true)
(1..5).each { |_| (1..5).each { |_|
@ -173,10 +172,10 @@ module Faaso
sleep 0.1.seconds sleep 0.1.seconds
} }
if !funko.running? if !funko.running?
puts "Container for #{funko.name} is not running yet" Log.warn { "Container for #{funko.name} is not running yet" }
next next
end end
puts "Container for #{funko.name} is running" Log.info { "Container for #{funko.name} is running" }
end end
end end
end end
@ -197,10 +196,10 @@ module Faaso
# Create temporary build location # Create temporary build location
dst_path = Path.new("export", funko.name) dst_path = Path.new("export", funko.name)
if File.exists? dst_path if File.exists? dst_path
puts "Error: #{dst_path} already exists, not exporting #{funko.path}" Log.error { "#{dst_path} already exists, not exporting #{funko.path}" }
next next
end end
puts "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 dst_path funko.prepare_build dst_path
end end
@ -218,7 +217,7 @@ module Faaso
def run def run
@arguments.each do |arg| @arguments.each do |arg|
puts "Stopping funko... #{arg}" Log.info { "Stopping funko... #{arg}" }
# TODO: check if funko is running # TODO: check if funko is running
# TODO: stop funko container # TODO: stop funko container
# TODO: delete funko container # TODO: delete funko container
@ -238,7 +237,7 @@ module Faaso
def run def run
@arguments.each do |arg| @arguments.each do |arg|
puts "Deploying funko... #{arg}" Log.info { "Deploying funko... #{arg}" }
# TODO: Everything # TODO: Everything
end end
end end

View File

@ -88,7 +88,7 @@ class Funko
docker_api = Docr::API.new(Docr::Client.new) docker_api = Docr::API.new(Docr::Client.new)
docker_api.images.build( docker_api.images.build(
context: path.to_s, context: path.to_s,
tags: ["#{name}:latest"]) { |x| puts x } tags: ["#{name}:latest"]) { |x| Log.info { x } }
end end
# Return a list of image IDs for this funko, most recent first # Return a list of image IDs for this funko, most recent first
@ -99,7 +99,7 @@ class Funko
name: name name: name
).sort { |i, j| j.@created <=> i.@created }.map(&.@id) ).sort { |i, j| j.@created <=> i.@created }.map(&.@id)
rescue ex : Docr::Errors::DockerAPIError rescue ex : Docr::Errors::DockerAPIError
puts "Error: #{ex}" Log.error { "#{ex}" }
[] of String [] of String
end end
end end
@ -172,7 +172,7 @@ class Funko
docker_api = Docr::API.new(Docr::Client.new) docker_api = Docr::API.new(Docr::Client.new)
response = docker_api.containers.create(name: "faaso-#{name}", config: conf) response = docker_api.containers.create(name: "faaso-#{name}", config: conf)
response.@warnings.each { |msg| puts "Warning: #{msg}" } response.@warnings.each { |msg| Log.warn { msg } }
docker_api.containers.start(response.@id) if autostart docker_api.containers.start(response.@id) if autostart
response.@id response.@id
end end

View File

@ -1,5 +1,41 @@
require "commander"
require "./faaso.cr" require "./faaso.cr"
require "colorize"
require "commander"
# Log formatter for
struct LogFormat < Log::StaticFormatter
@@colors = {
"FATAL" => :red,
"ERROR" => :red,
"WARN" => :yellow,
"INFO" => :green,
"DEBUG" => :blue,
"TRACE" => :light_blue,
}
def run
string "[#{Time.local}] #{@entry.severity.label}: #{@entry.message}".colorize(@@colors[@entry.severity.label])
end
def self.setup(quiet : Bool, verbosity)
if quiet
_verbosity = Log::Severity::Fatal
else
_verbosity = [
Log::Severity::Fatal,
Log::Severity::Error,
Log::Severity::Warn,
Log::Severity::Info,
Log::Severity::Debug,
Log::Severity::Trace,
][[verbosity, 5].min]
end
Log.setup(
_verbosity,
Log::IOBackend.new(io: STDERR, formatter: LogFormat)
)
end
end
cli = Commander::Command.new do |cmd| cli = Commander::Command.new do |cmd|
cmd.use = "faaso" cmd.use = "faaso"
@ -14,11 +50,30 @@ cli = Commander::Command.new do |cmd|
flag.persistent = true flag.persistent = true
end end
cmd.flags.add do |flag|
flag.name = "quiet"
flag.short = "-q"
flag.long = "--quiet"
flag.description = "Don't log anything"
flag.default = false
flag.persistent = true
end
cmd.flags.add do |flag|
flag.name = "verbosity"
flag.short = "-v"
flag.long = "--verbosity"
flag.description = "Control the logging verbosity, 0 to 5 "
flag.default = 3
flag.persistent = true
end
cmd.commands.add do |command| cmd.commands.add do |command|
command.use = "build" command.use = "build"
command.short = "Build a funko" command.short = "Build a funko"
command.long = "Build a funko's Docker image and upload it to registry" command.long = "Build a funko's Docker image and upload it to registry"
command.run do |options, arguments| command.run do |options, arguments|
LogFormat.setup(options.@bool["quiet"], options.@int["verbosity"])
Faaso::Commands::Build.new(options, arguments).run Faaso::Commands::Build.new(options, arguments).run
end end
end end
@ -28,6 +83,7 @@ cli = Commander::Command.new do |cmd|
command.short = "Ensure funkos are running" command.short = "Ensure funkos are running"
command.long = "Start/unpause/create containers for requested funkos and ensure they are up." command.long = "Start/unpause/create containers for requested funkos and ensure they are up."
command.run do |options, arguments| command.run do |options, arguments|
LogFormat.setup(options.@bool["quiet"], options.@int["verbosity"])
Faaso::Commands::Up.new(options, arguments).run Faaso::Commands::Up.new(options, arguments).run
end end
end end
@ -37,6 +93,7 @@ cli = Commander::Command.new do |cmd|
command.short = "Deploy latest images" command.short = "Deploy latest images"
command.long = "Update containers for all funkos to latest image." command.long = "Update containers for all funkos to latest image."
command.run do |options, arguments| command.run do |options, arguments|
LogFormat.setup(options.@bool["quiet"], options.@int["verbosity"])
Faaso::Commands::Deploy.new(options, arguments).run Faaso::Commands::Deploy.new(options, arguments).run
end end
end end
@ -46,6 +103,7 @@ cli = Commander::Command.new do |cmd|
command.short = "Stop a funko" command.short = "Stop a funko"
command.long = "Stop a funko in a container" command.long = "Stop a funko in a container"
command.run do |options, arguments| command.run do |options, arguments|
LogFormat.setup(options.@bool["quiet"], options.@int["verbosity"])
Faaso::Commands::Down.new(options, arguments).run Faaso::Commands::Down.new(options, arguments).run
end end
end end
@ -55,6 +113,7 @@ cli = Commander::Command.new do |cmd|
command.short = "Export a funko to a directory" command.short = "Export a funko to a directory"
command.long = "Exports a funko as a self-contained directory." command.long = "Exports a funko as a self-contained directory."
command.run do |options, arguments| command.run do |options, arguments|
LogFormat.setup(options.@bool["quiet"], options.@int["verbosity"])
Faaso::Commands::Export.new(options, arguments).run Faaso::Commands::Export.new(options, arguments).run
end end
end end