Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
drunkbatya committed Jul 24, 2024
0 parents commit ff02db5
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.json
31 changes: 31 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM debian:bullseye

RUN DEBIAN_FRONTEND=noninteractive apt update && apt -y install curl gnupg2 ca-certificates lsb-release debian-archive-keyring

# Zerotier
RUN mkdir -p /usr/share/zerotier
RUN curl -o /usr/share/zerotier/tmp.asc "https://download.zerotier.com/contact%40zerotier.com.gpg"
RUN gpg --no-default-keyring --keyring /usr/share/zerotier/zerotier.gpg --import /usr/share/zerotier/tmp.asc
RUN rm -f /usr/share/zerotier/tmp.asc
RUN echo "deb [signed-by=/usr/share/zerotier/zerotier.gpg] http://download.zerotier.com/debian/bullseye bullseye main" > /etc/apt/sources.list.d/zerotier.list
RUN DEBIAN_FRONTEND=noninteractive apt update && apt -y install zerotier-one=1.14.0 curl iproute2 net-tools iputils-ping openssl libssl1.1
RUN rm -rf /var/lib/zerotier-one

# Nginx
RUN curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
RUN echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/debian `lsb_release -cs` nginx" | tee /etc/apt/sources.list.d/nginx.list
RUN DEBIAN_FRONTEND=noninteractive apt update && apt -y install nginx

# Supervisor
RUN DEBIAN_FRONTEND=noninteractive apt update && apt -y install supervisor
RUN mkdir -p /var/log/supervisor
ADD supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# Main
RUN DEBIAN_FRONTEND=noninteractive apt update && apt -y install python3-pip
RUN mkdir -p /etc/app
ADD requirements.txt /usr/bin/app/
RUN python3 -m pip install -r /usr/bin/app/requirements.txt
ADD templates /usr/bin/app/templates
ADD template_configs.py /usr/bin/app/template_configs.py
CMD python3 /usr/bin/app/template_configs.py && /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
annotated-types==0.7.0
jinja2==3.1.4
MarkupSafe==2.1.5
pydantic==2.8.2
pydantic-core==2.20.1
supervisor==4.2.2
typing-extensions==4.12.2
24 changes: 24 additions & 0 deletions supervisord.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[supervisord]
nodaemon=true
logfile=/dev/null
logfile_maxbytes=0
user=root

[unix_http_server]
file=/var/run/supervisor.sock
chmod=0700

[rpcinterface:supervisor]
supervisor.rpcinterface_factory=supervisor.rpcinterface:make_main_rpcinterface

[program:nginx]
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
redirect_stderr=true
command = nginx -g 'daemon off;'

[program:zerotier]
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
redirect_stderr=true
command = /usr/sbin/zerotier-one
57 changes: 57 additions & 0 deletions template_configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3

import json
from pathlib import Path
from jinja2 import Environment, FileSystemLoader
from pydantic import BaseModel


class User(BaseModel):
hostname: str
ip: str


class ConfigFile(BaseModel):
zerotier_network: str
zerotier_public_key: str
zerotier_private_key: str
hostname_base: str
users: list[User]


class App:
def __init__(self):
with open("/etc/app/config.json") as f:
json_data = json.load(f)
self.config = ConfigFile(**json_data)

def template_zerotier(self):
Path("/var/lib/zerotier-one").mkdir(parents=True, exist_ok=True)
with open("/var/lib/zerotier-one/identity.secret", "w") as text_file:
text_file.write(self.config.zerotier_private_key)
with open("/var/lib/zerotier-one/identity.public", "w") as text_file:
text_file.write(self.config.zerotier_public_key)
Path("/var/lib/zerotier-one/networks.d").mkdir(parents=True, exist_ok=True)
Path(
f"/var/lib/zerotier-one/networks.d/{self.config.zerotier_network}.conf"
).touch()

def template_nginx(self):
environment = Environment(loader=FileSystemLoader("/usr/bin/app/templates"))
template = environment.get_template("nginx.conf.j2")
context = {
"users": self.config.users,
"hostname_base": self.config.hostname_base,
}
content = template.render(context)
with open("/etc/nginx/nginx.conf", "w") as text_file:
text_file.write(content)

def run(self):
self.template_zerotier()
self.template_nginx()


if __name__ == "__main__":
app = App()
app.run()
33 changes: 33 additions & 0 deletions templates/nginx.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
user nginx;
worker_processes auto;
error_log /dev/stderr error;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
underscores_in_headers on;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /dev/stdout main;
sendfile on;
keepalive_timeout 65;
{% for user in users %}
server {
listen 80;
server_name {{ user.hostname }}.{{ hostname_base }};
location / {
return 204;
}
}
server {
listen 80 default_server;
server_name _;
location / {
return 444;
}
}
{% endfor %}
}

0 comments on commit ff02db5

Please sign in to comment.