Merge branch 'nginx' of ssh://git.karchnu.fr:2202/WeirdOS/service into nginx

master
Philippe PITTOLI 2019-10-28 13:39:22 +01:00
commit 221206ec84
9 changed files with 88 additions and 59 deletions

View File

@ -1,5 +1,6 @@
command: gitea -C . -w . -c ./custom/conf/app.ini
consumes: postgresql
requires-domain: true
%directory ${SERVICE_ROOT}/custom/conf
name: working directory

View File

@ -1,5 +1,6 @@
command: echo "coucou"
consumes: www
requires-domain: true
%directory ${SERVICE_ROOT}/
name: data directory

View File

@ -7,6 +7,18 @@ def sanitize_path(path)
path.gsub /\/\/+/, "/"
end
class Service
def to_genconfig
Hash(String, String?).new.tap do |entry|
entry["name"] = name
entry["id"] = full_id
entry["environment"] = environment.name
entry["root"] = root
entry["domain"] = domain
end
end
end
module GenConfig
# FIXME: The fuck has this become.
alias Variables =
@ -14,6 +26,7 @@ module GenConfig
Array(String) | Array(Variables) |
Hash(String, String) | Hash(String, Variables) |
Array(Hash(String, String?)) |
Hash(String, Array(Hash(String, String?))) |
Hash(String, String?) |
Crinja::Callable::Instance
@ -78,57 +91,26 @@ class GenConfig::Context
raise Exception.new "Could not find template to generate file ('#{target}')."
end
pp ENV["SERVICE_ID"]?
if service_id = ENV["SERVICE_ID"]?
if service = Service.get_by_id service_id
environment = service.environment
options["id"] = service.full_id
options["name"] = service.name
options["service_root"] = service.root
options["service"] = service.to_genconfig
options["environment"] = environment.name
options["environment_root"] = environment.root
providers = Hash(String, Variables).new
service.providers.each do |token, provider|
options["providers"] = service.providers.compact_map do |token, provider|
provider = Service.get_by_id provider
next unless provider
# FIXME: deduplicate
providers[token] = Hash(String, String?).new.tap do |entry|
entry["name"] = provider.name
entry["id"] = provider.full_id
entry["environment"] = provider.environment.name
entry["root"] = provider.root
entry["domain"] = provider.domain
provider.to_genconfig
end
end
options["providers"] = providers
consumers = Hash(String, Variables).new
service.provides.each do |provider_data|
token = provider_data.token
# FIXME: definitely unreadable
consumers[token] = Service.all
.select(
&.providers.select do |t, provider|
t == token && service.is_id?(provider)
end
.size.>(0))
.map do |consumer|
Hash(String, String?).new.tap do |entry|
entry["name"] = consumer.name
entry["id"] = consumer.id
entry["environment"] = consumer.environment.name
entry["root"] = consumer.root
entry["domain"] = consumer.domain
end
end
end
options["consumers"] = consumers
options["consumers"] = service.provides
.map(&.token)
.map{ |token|
{token, service.get_consumers(token).map &.to_genconfig}
}
.to_h
end
end

View File

@ -44,6 +44,7 @@ commands = CommandsList.new
commands.push "add", "Adds a service to an environment." do |args|
providers = Hash(String, String).new
domain = nil
environment, service = Service.parse_id args[0]
@ -52,15 +53,25 @@ commands.push "add", "Adds a service to an environment." do |args|
match = arg.match /(.*)=(.*)/
if match.nil?
if match.nil? || match.size < 2
raise ::Service::Exception.new "usage: service add <service> <token=provider>"
next
end
providers[match[1]] = match[2]
_, key, value = match
if key == "domain"
domain = value
else
providers[key] = value
end
end
Service.new(service, environment).tap do |service|
if domain
service.domain = domain
end
service.consumes.each do |token|
provider = providers[token.token]?
@ -76,7 +87,10 @@ commands.push "add", "Adds a service to an environment." do |args|
service.providers[token.token] = provider
end
pp! service.providers
if service.requires_domain && !service.domain
raise Service::Exception.new "'#{service.name}' requires a domain= parameter to be provided!"
end
end.write RC_DIRECTORY
end

View File

@ -19,7 +19,7 @@ class Service
getter environment : Environment
getter providers = ProvidersList.new
getter domain : String?
property domain : String?
# The place well store configuration and data.
@root : String?
@ -81,6 +81,10 @@ class Service
file << "root: #{@root}"
end
if @domain
file << "domain: #{@domain}"
end
@providers.each do |token, provider|
file << "%consumes #{token}"
file << " from: #{provider}"
@ -120,6 +124,9 @@ class Service
def consumes
@reference.consumes
end
def requires_domain
@reference.requires_domain
end
def root
@root || "#{@environment.root}/#{name}"
@ -441,6 +448,16 @@ class Service
def get_default_provider(token) : String?
@environment.get_provider(token) || Environment.root.get_provider(token)
end
def consumes?(token, origin)
providers.select do |_token, provider|
token == _token && origin.is_id?(provider)
end.size > 0
end
def get_consumers(token)
Service.all.select(&.consumes?(token, self))
end
end

View File

@ -49,6 +49,8 @@ class ServiceDefinition
getter pre_start_hooks : Array(Hook)
getter provides : Array(Provides)
getter requires_domain = false
def initialize(@name, specs : SpecParser)
sections = specs.sections
specs = specs.assignments
@ -60,6 +62,18 @@ class ServiceDefinition
@consumes = specs["consumes"]?.try &.as_a_or_s.map { |x| Consumes.new x } || Array(Consumes).new
@environment_variables = specs["environment-variables"]?.try &.as_a_or_s || Array(String).new
# FIXME: as_b?
requires_domain = specs["requires-domain"]?.try &.as_s
case requires_domain
when nil
when "true", "yes"
@requires_domain = true
when "false", "no"
@requires_domain = false
else
STDERR.puts "warning: definition '#{@name}' has a 'requires-domain' entry with an invalid value"
end
@pre_start_hooks = Array(Hook).new
sections.each do |section|

View File

@ -16,25 +16,25 @@ SECRET_KEY = vPFgSqRMIe7Dzk4frRM4UA3CETedL8agK7x6IQFQt9YfRPiQGhQbYAGfyan71iU
[database]
DB_TYPE = postgres
HOST = 127.0.0.1:{{ port(providers.postgresql) }}
NAME = {{ id | replace("/", "_") }}_db
USER = {{ id | replace("/", "_") }}
PASSWD = {{ random_password( id ) }}
NAME = {{ service.id | replace("/", "_") }}_db
USER = {{ service.id | replace("/", "_") }}
PASSWD = {{ random_password( service.id ) }}
SSL_MODE = disable
CHARSET = utf8
PATH = /srv/{{ environment }}/gitea/data/gitea.db
PATH = {{ service.root }}/data/gitea.db
[repository]
ROOT = /srv/{{ environment }}/gitea/repositories
ROOT = {{ service.root }}/repositories
[server]
SSH_DOMAIN = localhost
DOMAIN = localhost
HTTP_PORT = {{ port(id) }}
ROOT_URL = http://localhost:{{ port(id) }}/
SSH_DOMAIN = {{ service.domain }}
DOMAIN = {{ service.domain }}
HTTP_PORT = {{ port(service.id) }}
ROOT_URL = http://{{ service.domain }}:{{ port(service.id) }}/
DISABLE_SSH = false
SSH_PORT = 22
LFS_START_SERVER = true
LFS_CONTENT_PATH = /srv/{{ environment }}/gitea/data/lfs
LFS_CONTENT_PATH = {{ service.root }}/data/lfs
LFS_JWT_SECRET = nUIANTsStEelCeOo7SFTcJllpJ6I8DXNr7duUgGMXfI
OFFLINE_MODE = false
@ -67,6 +67,6 @@ PROVIDER = file
[log]
MODE = file
LEVEL = info
ROOT_PATH = /srv/{{ environment }}/gitea/log
ROOT_PATH = {{ service.root }}/log

View File

@ -8,8 +8,8 @@ events {
}
http {
error_log /var/log/{{ id | replace("/", "_") }}_error.log warn;
access_log /var/log/{{ id | replace("/", "_") }}_access.log;
error_log /var/log/{{ service.id | replace("/", "_") }}_error.log warn;
access_log /var/log/{{ service.id | replace("/", "_") }}_access.log;
include /etc/nginx/mime.types;
default_type application/octet-stream;

View File

@ -60,7 +60,7 @@
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = {{port(id)}}
port = {{port(service.id)}}
max_connections = 100 # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart)
#unix_socket_directories = '/run/postgresql' # comma-separated list of directories