├── NETWORK ├── subcommands └── default ├── .gitignore ├── plugin.toml ├── traefik.flags ├── uninstall ├── commands ├── post-create ├── check-deploy ├── proxy-build-config ├── functions ├── proxy-enable ├── proxy-disable ├── docker-args-deploy ├── traefik.toml ├── post-domains-update ├── README.md ├── install └── traefik.py /NETWORK: -------------------------------------------------------------------------------- 1 | proxy_network 2 | -------------------------------------------------------------------------------- /subcommands/default: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | venv/ 3 | test/ 4 | -------------------------------------------------------------------------------- /plugin.toml: -------------------------------------------------------------------------------- 1 | [plugin] 2 | description = "dokku traefik reverse proxy plugin" 3 | version = "0.0.2" 4 | [plugin.config] 5 | -------------------------------------------------------------------------------- /traefik.flags: -------------------------------------------------------------------------------- 1 | traefik.enable=true 2 | traefik.frontend.rule=Host:traefik.{{DOMAIN}} 3 | traefik.backend.port=8080 4 | traefik.docker.network={{PROXY_NET}} 5 | -------------------------------------------------------------------------------- /uninstall: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Cleanup up extra containers created 3 | set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x 4 | source "$PLUGIN_AVAILABLE_PATH/traefik-proxy/functions" 5 | 6 | docker rm $(docker stop $(cat ${PLUGIN_AVAILABLE_PATH}/traefik-proxy/CONTAINER)) 7 | -------------------------------------------------------------------------------- /commands: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x 3 | 4 | case "$1" in 5 | help | traefik:help) 6 | help_content_func () { 7 | declare desc="return help_content string" 8 | cat< ${PLUGIN_AVAILABLE_PATH}/traefik-proxy/CONTAINER 21 | -------------------------------------------------------------------------------- /traefik.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import json 3 | from pathlib import Path 4 | 5 | 6 | class TraefikPlugin(object): 7 | def __init__(self): 8 | self.app_path = Path() 9 | self.settings = { 10 | "enabled": True, 11 | "name": "", 12 | "port": 5000, 13 | "domains": [] 14 | } 15 | parser = argparse.ArgumentParser(description="Traefik <=> Dokku interfacer") 16 | parser.add_argument("--app_name", required=True) 17 | parser.add_argument("--dokku_root", required=True) 18 | parser.add_argument("--plugin_path") 19 | parser.add_argument("--enable_proxy", help="Enable the proxy for the app", action="store_true") 20 | parser.add_argument("--disable_proxy", help="Disable the proxy for the app", action="store_true") 21 | parser.add_argument("--build_config", help="Build the config for the app", action="store_true") 22 | parser.add_argument("--update_domains", help="Update the list of Domains", action="store_true") 23 | parser.add_argument("--update_domains_action") 24 | parser.add_argument("--get_enabled", action="store_true") 25 | parser.add_argument("--domain", help="The domains to update", action="append") 26 | 27 | args = parser.parse_args() 28 | self.check_config(dokku_root=args.dokku_root, app_name=args.app_name) 29 | if args.enable_proxy: 30 | self.settings["enabled"] = True 31 | elif args.disable_proxy: 32 | self.settings["enabled"] = False 33 | elif args.get_enabled: 34 | print(self.settings["enabled"]) 35 | elif args.build_config: 36 | self.network_name = Path(args.plugin_path).joinpath("NETWORK") 37 | self.build_config() 38 | elif args.update_domains: 39 | self.update_domains(domains=args.domain, action=args.update_domains_action) 40 | self.write_config() 41 | 42 | def check_config(self, dokku_root, app_name): 43 | self.app_path = Path(dokku_root).joinpath(app_name) 44 | config_file = self.app_path.joinpath("traefik.json") 45 | if self.app_path.exists(): 46 | if not config_file.exists(): 47 | config_file.touch() 48 | with config_file.open("w") as f: 49 | self.settings["name"] = app_name 50 | self.settings["domains"].append("") 51 | json.dump(self.settings, f) 52 | else: 53 | with config_file.open() as f: 54 | self.settings = json.load(f) 55 | self.settings["name"] = app_name 56 | else: 57 | print("App does not exist") 58 | 59 | def write_config(self): 60 | config_file = self.app_path.joinpath("traefik.json") 61 | with config_file.open("w") as f: 62 | json.dump(self.settings, f) 63 | 64 | def print_info_1(self, message): 65 | print("-----> {}".format(message)) 66 | 67 | def print_info_2(self, message): 68 | print("=====> {}".format(message)) 69 | 70 | def build_config(self): 71 | if not self.app_path.joinpath("LABELS").exists(): 72 | self.app_path.joinpath("LABELS").touch() 73 | with self.app_path.joinpath("LABELS").open("w") as f: 74 | net_name = "" 75 | with self.network_name.open("r") as nnf: 76 | net_name = nnf.read().rstrip() 77 | domains = ",".join(self.settings["domains"]) 78 | f.writelines([ 79 | "traefik.enable={}\n".format(str(self.settings["enabled"]).lower()), 80 | "traefik.{name}.frontend.rule=Host:{hosts}\n".format( 81 | name=self.settings["name"], 82 | hosts=domains 83 | ), 84 | "traefik.{name}.port={port}\n".format( 85 | name=self.settings["name"], 86 | port=self.settings["port"] 87 | ), 88 | "traefik.docker.network={network_name}\n".format(network_name=net_name) 89 | ]) 90 | 91 | def update_domains(self, domains, action): 92 | if action == "set": 93 | self.settings["domains"] = domains 94 | elif action == "add": 95 | [self.settings["domains"].append(domain) for domain in domains if domain not in self.settings["domains"]] 96 | elif action == "remove": 97 | [self.settings["domains"].remove(domain) for domain in domains if domain in self.settings["domains"]] 98 | elif action == "clear": 99 | self.settings["domains"] = [] 100 | else: 101 | print("Wrong action {}".format(action)) 102 | print(action, domains, ",".join(self.settings["domains"])) 103 | 104 | 105 | if __name__ == '__main__': 106 | TraefikPlugin() 107 | --------------------------------------------------------------------------------