Refactored daemon a bit, automatic proxy conf updates
This commit is contained in:
parent
bed7bcf6f3
commit
20c0e0f5de
59
src/daemon-proxyconf.cr
Normal file
59
src/daemon-proxyconf.cr
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
require "kemal"
|
||||||
|
|
||||||
|
module Proxy
|
||||||
|
@@current_config = File.read("tinyproxy.conf")
|
||||||
|
|
||||||
|
# Get current proxy config
|
||||||
|
get "/proxy/" do
|
||||||
|
@@current_config
|
||||||
|
end
|
||||||
|
|
||||||
|
# Bump proxy config to current docker state, returns
|
||||||
|
# new proxy config
|
||||||
|
patch "/proxy/" do
|
||||||
|
Log.info { "Updating routing" }
|
||||||
|
# Get all the funkos, create routes for them all
|
||||||
|
update_proxy_config
|
||||||
|
end
|
||||||
|
|
||||||
|
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 = %(
|
||||||
|
Port 8888
|
||||||
|
Listen 0.0.0.0
|
||||||
|
Timeout 600
|
||||||
|
Allow 0.0.0.0/0
|
||||||
|
ReverseOnly Yes
|
||||||
|
ReverseMagic Yes
|
||||||
|
ReversePath "/admin/" "http://127.0.0.1:3000/"
|
||||||
|
) + funkos.map { |funko| %(ReversePath "/faaso/#{funko}/" "http://#{funko}:3000/") }.join("\n")
|
||||||
|
|
||||||
|
if @@current_config != config
|
||||||
|
File.open("tinyproxy.conf", "w") do |file|
|
||||||
|
file << config
|
||||||
|
end
|
||||||
|
# Reload config
|
||||||
|
Process.run(command: "/usr/bin/killall", args: ["-USR1", "tinyproxy"])
|
||||||
|
@@current_config = config
|
||||||
|
end
|
||||||
|
config
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Update proxy config once a second
|
||||||
|
spawn do
|
||||||
|
loop do
|
||||||
|
Proxy.update_proxy_config
|
||||||
|
sleep 1.second
|
||||||
|
end
|
||||||
|
end
|
@ -1,4 +1,5 @@
|
|||||||
require "./daemon-secrets.cr"
|
require "./daemon-secrets.cr"
|
||||||
|
require "./daemon-proxyconf.cr"
|
||||||
require "compress/gzip"
|
require "compress/gzip"
|
||||||
require "crystar"
|
require "crystar"
|
||||||
require "docr"
|
require "docr"
|
||||||
@ -9,51 +10,6 @@ require "uuid"
|
|||||||
# FIXME: make configurable
|
# FIXME: make configurable
|
||||||
basic_auth "admin", "admin"
|
basic_auth "admin", "admin"
|
||||||
|
|
||||||
current_config = File.read("tinyproxy.conf")
|
|
||||||
|
|
||||||
# Get current proxy config
|
|
||||||
get "/proxy/" do
|
|
||||||
current_config
|
|
||||||
end
|
|
||||||
|
|
||||||
# Bump proxy config to current docker state, returns
|
|
||||||
# new proxy config
|
|
||||||
patch "/proxy/" do
|
|
||||||
Log.info { "Updating routing" }
|
|
||||||
# Get all the funkos, create routes for them all
|
|
||||||
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!
|
|
||||||
|
|
||||||
proxy_config = %(
|
|
||||||
Port 8888
|
|
||||||
Listen 0.0.0.0
|
|
||||||
Timeout 600
|
|
||||||
Allow 0.0.0.0/0
|
|
||||||
ReverseOnly Yes
|
|
||||||
ReverseMagic Yes
|
|
||||||
ReversePath "/admin/" "http://127.0.0.1:3000/"
|
|
||||||
) + funkos.map { |funko| %(ReversePath "/faaso/#{funko}/" "http://#{funko}:3000/") }.join("\n")
|
|
||||||
|
|
||||||
if current_config != proxy_config
|
|
||||||
File.open("tinyproxy.conf", "w") do |file|
|
|
||||||
file << proxy_config
|
|
||||||
end
|
|
||||||
# Reload config
|
|
||||||
Process.run(command: "/usr/bin/killall", args: ["-USR1", "tinyproxy"])
|
|
||||||
current_config = proxy_config
|
|
||||||
end
|
|
||||||
proxy_config
|
|
||||||
end
|
|
||||||
|
|
||||||
# Bring up the funko
|
# Bring up the funko
|
||||||
get "/funko/:name/up/" do |env|
|
get "/funko/:name/up/" do |env|
|
||||||
name = env.params.url["name"]
|
name = env.params.url["name"]
|
||||||
|
@ -6,4 +6,4 @@ Allow 0.0.0.0/0
|
|||||||
ReverseOnly Yes
|
ReverseOnly Yes
|
||||||
ReverseMagic Yes
|
ReverseMagic Yes
|
||||||
ReversePath "/admin/" "http://127.0.0.1:3000/"
|
ReversePath "/admin/" "http://127.0.0.1:3000/"
|
||||||
ReversePath "/faaso/hello/" "http://hello:3000/"
|
|
Loading…
Reference in New Issue
Block a user