Compare commits

...

2 Commits

Author SHA1 Message Date
d0e2a1a494 Support load balancing in caddy 2024-07-04 16:39:43 -03:00
83b6615503 Forgotten file 2024-07-04 15:20:07 -03:00
4 changed files with 83 additions and 23 deletions

View File

@ -1,13 +1,15 @@
{
https_port 8888
http_port 8887
http_port 8888
https_port 8887
local_certs
admin off
}
localhost:8888 {
http://localhost:8888 {
handle_path /admin/terminal/* {
reverse_proxy /* http://127.0.0.1:7681
}
handle_path /admin/* {
reverse_proxy /* http://127.0.0.1:3000
}
}
}

View File

@ -23,6 +23,5 @@ COPY Caddyfile ./
COPY --from=build /home/app/bin/faaso-daemon /home/app/bin/faaso /usr/bin/
RUN mkdir /secrets
RUN echo "sarasa" > /secrets/sarlanga
CMD ["/usr/bin/multirun", "-v", "faaso-daemon", "caddy run --config Caddyfile"]

49
src/commands/secret.cr Normal file
View File

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

View File

@ -20,36 +20,46 @@ module Proxy
def self.update_proxy_config
docker_api = Docr::API.new(Docr::Client.new)
containers = docker_api.containers.list(all: true)
funkos = [] of String
containers.each { |container|
names = container.names.select &.starts_with? "/faaso-"
next if names.empty?
funkos << names[0][7..]
}
funkos.sort!
config = %(
config = <<-CONFIG
{
https_port 8888
http_port 8887
http_port 8888
https_port 8887
local_certs
admin off
}
localhost:8888 {
http://localhost:8888 {
handle_path /admin/terminal/* {
reverse_proxy /* http://127.0.0.1:7681
}
handle_path /admin/* {
reverse_proxy /* http://127.0.0.1:3000
}
) + funkos.map { |funko| %(
handle_path /faaso/#{funko.split("-")[0]}/* {
reverse_proxy /* http://#{funko}:3000
}
) }.join("\n") + "}"
CONFIG
funkos = Funko::Funko.from_docker
funkos.each do |funko|
next if funko.name == "proxy"
containers = funko.containers
next if containers.empty?
funko_urls = containers.map { |container|
"http://#{container.names[0].lstrip("/")}:3000"
}
config += %(
handle_path /faaso/#{funko.name}/* {
reverse_proxy /* #{funko_urls.join(" ")} {
health_uri /ping
fail_duration 30s
}
}
)
end
config += "\n}"
if @@current_config != config
Log.info { "Updating proxy config" }
File.open("Caddyfile", "w") do |file|
file << config
end