1
0
Fork 0

caddy: add config and scripts

This commit is contained in:
Lukáš Kucharczyk 2023-12-01 21:34:12 +01:00
parent 4c5105c73f
commit 6b8701ca14
4 changed files with 201 additions and 0 deletions

9
scripts/README.md Normal file
View File

@ -0,0 +1,9 @@
This folder contains the configuration file, template, and script to generate a Caddyfile for all the services in main repository.
# Usage
1. Run the script:
```bash
./add --config sites-config.yaml --template template.j2 > sites-enabled/generated.caddy
```
2. Reload Caddy with `caddy reload -c /etc/caddy/Caddyfile`

61
scripts/add Executable file
View File

@ -0,0 +1,61 @@
#!/usr/bin/python3
import argparse
import yaml
from jinja2 import Template
def render_template(template_path, **kwargs):
with open(template_path, "r") as file:
template = Template(file.read())
output = template.render(**kwargs)
return output
def process_sites_config(config_path, template_path, check_mode):
with open(config_path, "r") as file:
sites_config = yaml.safe_load(file)
total_sites = len(sites_config["sites"])
enabled_sites = 0
disabled_sites = 0
for site in sites_config["sites"]:
# Check if site is enabled
if site.get("enabled", True): # Default to True if 'enabled' key is not present
enabled_sites += 1
if not check_mode:
rendered_content = render_template(template_path, **site)
print(f"{rendered_content}\n")
else:
disabled_sites += 1
if check_mode:
print(f"Total sites: {total_sites}")
print(f"Enabled sites: {enabled_sites}")
print(f"Disabled sites: {disabled_sites}")
def main():
parser = argparse.ArgumentParser(
description="Process a sites configuration file for Caddyfiles"
)
parser.add_argument(
"--config", required=True, help="Path to the YAML configuration file"
)
parser.add_argument(
"--check",
action="store_true",
help="Only check statistics, do not output templates",
)
parser.add_argument("--template", help="Path to the Jinja2 template file")
args = parser.parse_args()
if args.template is None and args.check is False:
parser.error("--template argument is required if not using --check")
template_path = args.template # Replace with the actual path to your template file
process_sites_config(args.config, template_path, args.check)
if __name__ == "__main__":
main()

114
scripts/sites-config.yaml Normal file
View File

@ -0,0 +1,114 @@
sites:
- hostname: gitea
subdomain: git
port: 3000
- hostname: rtorrent
subdomain: torrent
port: 9080
- subdomain: portainer
# fixme: move portainer to docker-compose.yml
# hostname: portainer
hostname: 192.168.0.106
port: 9000
- subdomain: radarr
hostname: radarr
port: 7878
- subdomain: sonarr-tv
hostname: sonarr_tv
port: 8989
- subdomain: sonarr-anime
hostname: sonarr_anime
port: 8989
- subdomain: notify
hostname: ntfy
port: 80
additional_config: |
@httpget {
protocol http
method GET
path_regexp ^/([-_a-z0-9]{0,64}$|docs/|static/)
}
redir @httpget https://{host}{uri}
- subdomain: recipes
hostname: mealie
port: 80
- subdomain: music
hostname: navidrome
port: 4533
- subdomain: paperless
hostname: paperless-ngx
port: 8000
- subdomain: photos
hostname: photoprism
port: 2342
- subdomain: bookmarks
hostname: linkace
port: 80
- subdomain: bw
hostname: vaultwarden
port: 80
- subdomain: drone
# fixme: move to docker compose & change hostname AND PORT!! (80)
# hostname: drone
hostname: 192.168.0.106
port: 580
- subdomain: jellyfin
hostname: jellyfin
port: 8096
- subdomain: comic
hostname: komga
port: 25600
- subdomain: miniflux
hostname: miniflux
port: 8080
- subdomain: netboot
# fixme: move to compose
# hostname: netbootxyz
hostname: 192.168.0.106
port: 3001
- subdomain: cloud
# fixme: move to compose
# hostname: nextcloud
hostname: 192.168.0.106
port: 8484
additional_config: |
redir /.well-known/carddav /remote.php/dav 301
redir /.well-known/caldav /remote.php/dav 301
header Strict-Transport-Security "max-age=15552000; includeSubDomains"
- subdomain: registry
# fixme: move to compose
# hostname: registry
hostname: 192.168.0.106
port: 5000
- subdomain: tracker
hostname: timetracker
port: 8001
additional_config: |
handle_path /static/* {
root * /srv/timetracker
file_server
}
handle /robots.txt {
root * /srv/timetracker
file_server
}
- subdomain: notes-old
additional_config: |
root * /srv/notes
file_server
- subdomain: notes
additional_config: |
reverse_proxy https://publish.obsidian.md {
header_up Host {upstream_hostport}
}
rewrite * /serve?url=notes.kucharczyk.xyz{path}
server_config: |
encode zstd gzip
- subdomain: wiki
hostname: mediawiki
port: 80
- subdomain: baserow
hostname: baserow
port: 80

17
scripts/template.j2 Normal file
View File

@ -0,0 +1,17 @@
{{ subdomain }}.kucharczyk.xyz {
handle {
{% if reverse_proxy_config %}
reverse_proxy {{ hostname }}:{{ port }} {
{{ reverse_proxy_config }}
}
{% else %}
{% if hostname and port %}
reverse_proxy {{ hostname }}:{{ port }}
{% endif %}
{% endif %}
{% if additional_config %}
{{ additional_config }}
{% endif %}
}
{{ server_config }}
}