236 lines
6.4 KiB
Jsonnet
236 lines
6.4 KiB
Jsonnet
# Tag to add a restart policy on all containers
|
|
#
|
|
# Examples:
|
|
# tags:
|
|
# - docker-services-restart:
|
|
# policy: always
|
|
# services:
|
|
# - srv1
|
|
# - mysqldb
|
|
|
|
# Default imports
|
|
# -------------------------------------
|
|
|
|
# Import from external data
|
|
local action = std.parseJson(std.extVar('action'));
|
|
local docker_data = std.parseJson(std.extVar('docker_data'));
|
|
local user_data = std.parseJson(std.extVar('user_data'));
|
|
|
|
|
|
#
|
|
# Default config
|
|
# -------------------------------------
|
|
|
|
# Build default config
|
|
local global_vars_default(user_data) =
|
|
assert std.isString(user_data.paasify_ns);
|
|
|
|
# Init defaults
|
|
local ns = std.get(user_data, 'paasify_ns', '<NS>');
|
|
local stack = std.get(user_data, 'paasify_stack', '<STACK>');
|
|
local sep = std.get(user_data, 'paasify_sep', '_');
|
|
|
|
# Runtime vars
|
|
local default_name = std.get(user_data, 'app_name', stack);
|
|
local default_top_domain = std.get(user_data, 'app_domain', ns);
|
|
|
|
# We replace app name by 'app', just for nice looking
|
|
local traefik_ident = if stack != default_name then default_name else 'app';
|
|
#local traefik_ident = stack;
|
|
|
|
{
|
|
|
|
# Required by API:
|
|
traefik_network_name: ns + sep + 'traefik',
|
|
traefik_net_ident: 'default',
|
|
traefik_net_external: true,
|
|
traefik_svc_ident: stack ,
|
|
traefik_svc_port: user_data.app_port ,
|
|
|
|
traefik_svc_name: null,
|
|
traefik_svc_domain: null,
|
|
traefik_svc_entrypoints: null,
|
|
traefik_svc_auth: null,
|
|
traefik_svc_tls: null,
|
|
traefik_svc_certresolver: null,
|
|
|
|
};
|
|
|
|
local global_vars_override(conf) =
|
|
# Any plugin user configuration can be used here
|
|
# No usage of paasify.X is permitted here at anytime !
|
|
# Forbidden to change existing vars, only new vars with _PREFIX
|
|
# All variables must start with _
|
|
|
|
local default_ident = conf.paasify_ns + conf.paasify_sep + conf.paasify_stack;
|
|
|
|
local default_svc_name = [
|
|
std.get(conf, 'traefik_svc_name'),
|
|
default_ident
|
|
];
|
|
local default_svc_domain = [
|
|
std.get(conf, 'traefik_svc_domain'),
|
|
std.get(conf, 'app_fqdn'),
|
|
std.get(conf, 'app_name', conf.paasify_stack) + '.' + std.get(conf, 'app_domain', conf.paasify_ns),
|
|
];
|
|
|
|
local default_svc_entrypoints = [
|
|
std.get(conf, 'traefik_svc_entrypoints'),
|
|
"web",
|
|
];
|
|
|
|
|
|
{
|
|
_traefik_svc_name: std.prune(default_svc_name)[0],
|
|
_traefik_svc_domain: std.prune(default_svc_domain)[0],
|
|
|
|
_traefik_svc_entrypoints: std.prune(default_svc_entrypoints)[0],
|
|
_traefik_svc_auth: std.get(conf, 'traefik_svc_auth', default=null),
|
|
_traefik_svc_tls: std.get(conf, 'traefik_svc_tls', default=false),
|
|
_traefik_svc_certresolver: std.get(conf, 'traefik_svc_certresolver', default=null),
|
|
#zz_conf: conf ,
|
|
}
|
|
;
|
|
|
|
|
|
# Internal functions
|
|
# -------------------------------------
|
|
|
|
# Base routing
|
|
local LabelsTraefik(svc, domain, entrypoints, port) =
|
|
{
|
|
["traefik.enable"]: "true",
|
|
["traefik.http.routers." + svc + ".rule"]: 'Host(`' + domain + '`)',
|
|
["traefik.http.routers." + svc + ".entrypoints"]: entrypoints,
|
|
["traefik.http.routers." + svc + ".service"]: svc,
|
|
["traefik.http.services." + svc + ".loadbalancer.server.port"]: std.format("%s", port),
|
|
};
|
|
|
|
# Middleware
|
|
local LabelsTraefikAuthelia(svc, authservice) =
|
|
if std.isString(authservice) && std.length(authservice) > 0 then
|
|
{
|
|
["traefik.http.routers." + svc + ".middlewares"]: authservice + '@docker',
|
|
} else {};
|
|
|
|
# TLS management
|
|
local LabelsTraefikTls(svc, status) =
|
|
if status == true then
|
|
{
|
|
["traefik.http.routers." + svc + ".tls"]: "true",
|
|
} else {};
|
|
|
|
local LabelsTraefikCertResolver(svc, name) =
|
|
if std.isString(name) && std.length(name) > 0 then
|
|
LabelsTraefikTls(svc, true) + {
|
|
["traefik.http.routers." + svc + ".tls.certresolver"]: name,
|
|
} else {};
|
|
|
|
# Networking
|
|
local TraefikSvcNetwork(id, name) =
|
|
if std.isString(id) then
|
|
{
|
|
[id]: null,
|
|
} else {};
|
|
|
|
local TraefikPrjNetwork(id, name, external) =
|
|
if std.isString(id) then
|
|
{
|
|
[id]+: {
|
|
name: name
|
|
},
|
|
} +
|
|
if external == true then
|
|
{
|
|
[id]+: {
|
|
external: true,
|
|
},
|
|
} else {}
|
|
else {};
|
|
|
|
|
|
# Provides docker data override
|
|
# -------------------------------------
|
|
local docker_transform(conf_raw, docker_data) =
|
|
assert std.isString(conf_raw.paasify_stack_service);
|
|
local conf = conf_raw + global_vars_override(conf_raw);
|
|
|
|
local service = std.get(conf, 'traefik_svc_ident', conf.paasify_stack_service );
|
|
local services = std.split(std.get(conf, 'paasify_stack_services', service), ',');
|
|
|
|
{
|
|
# Append stack network
|
|
networks+: TraefikPrjNetwork(
|
|
conf.traefik_net_ident,
|
|
conf.traefik_network_name,
|
|
conf.traefik_net_external),
|
|
|
|
# Apply per services labels
|
|
services+: {
|
|
[conf.traefik_svc_ident]+: {
|
|
labels+:
|
|
LabelsTraefik(
|
|
conf.traefik_svc_name,
|
|
conf._traefik_svc_domain,
|
|
conf.traefik_svc_entrypoints,
|
|
conf.traefik_svc_port)
|
|
+ LabelsTraefikAuthelia(
|
|
conf.traefik_svc_name,
|
|
conf.traefik_svc_auth)
|
|
+ LabelsTraefikTls(
|
|
conf.traefik_svc_name,
|
|
conf.traefik_svc_tls)
|
|
+ LabelsTraefikCertResolver(
|
|
conf.traefik_svc_name,
|
|
conf.traefik_svc_certresolver)
|
|
,
|
|
networks+: TraefikSvcNetwork(
|
|
conf.traefik_net_ident,
|
|
conf.traefik_network_name),
|
|
},
|
|
},
|
|
};
|
|
|
|
|
|
|
|
# Return result
|
|
# -------------------------------------
|
|
|
|
|
|
local main() =
|
|
|
|
local getConf(name) = std.parseJson(std.extVar(name));
|
|
local action = getConf('action');
|
|
|
|
if action == 'metadata' then
|
|
{}
|
|
|
|
# TOFIX IN CODE: else if action == 'docker_vars' then
|
|
else if action == 'vars_docker' then
|
|
local user_data = getConf('user_data');
|
|
local default_data = global_vars_default(user_data);
|
|
local common = { [x]: std.get(user_data, std.lstripChars(x, '_'), default_data[x] ) for x in std.objectFields(default_data) };
|
|
|
|
{
|
|
input: user_data,
|
|
|
|
diff: default_data + common + global_vars_override(default_data + user_data ),
|
|
merged: user_data + global_vars_override(default_data + user_data ),
|
|
}
|
|
|
|
else if action == 'docker_transform' then
|
|
local user_data = getConf('user_data');
|
|
local docker_data = getConf('docker_data');
|
|
|
|
{
|
|
input: user_data,
|
|
|
|
#diff: docker_transform(user_data + global_vars_override(user_data), docker_data),
|
|
diff: docker_transform(user_data, docker_data),
|
|
merged: docker_data + self.diff,
|
|
};
|
|
|
|
# Run main script !
|
|
main()
|
|
|