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

View File

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

View File

@ -1,5 +1,41 @@
require "commander"
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|
cmd.use = "faaso"
@ -14,11 +50,30 @@ cli = Commander::Command.new do |cmd|
flag.persistent = true
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|
command.use = "build"
command.short = "Build a funko"
command.long = "Build a funko's Docker image and upload it to registry"
command.run do |options, arguments|
LogFormat.setup(options.@bool["quiet"], options.@int["verbosity"])
Faaso::Commands::Build.new(options, arguments).run
end
end
@ -28,6 +83,7 @@ cli = Commander::Command.new do |cmd|
command.short = "Ensure funkos are running"
command.long = "Start/unpause/create containers for requested funkos and ensure they are up."
command.run do |options, arguments|
LogFormat.setup(options.@bool["quiet"], options.@int["verbosity"])
Faaso::Commands::Up.new(options, arguments).run
end
end
@ -37,6 +93,7 @@ cli = Commander::Command.new do |cmd|
command.short = "Deploy latest images"
command.long = "Update containers for all funkos to latest image."
command.run do |options, arguments|
LogFormat.setup(options.@bool["quiet"], options.@int["verbosity"])
Faaso::Commands::Deploy.new(options, arguments).run
end
end
@ -46,6 +103,7 @@ cli = Commander::Command.new do |cmd|
command.short = "Stop a funko"
command.long = "Stop a funko in a container"
command.run do |options, arguments|
LogFormat.setup(options.@bool["quiet"], options.@int["verbosity"])
Faaso::Commands::Down.new(options, arguments).run
end
end
@ -55,6 +113,7 @@ cli = Commander::Command.new do |cmd|
command.short = "Export a funko to a directory"
command.long = "Exports a funko as a self-contained directory."
command.run do |options, arguments|
LogFormat.setup(options.@bool["quiet"], options.@int["verbosity"])
Faaso::Commands::Export.new(options, arguments).run
end
end