diff --git a/Dockerfile b/Dockerfile index a6dd3df..c7ef45f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,15 +7,36 @@ RUN useradd --create-home --groups sudo admin ; echo "admin:admin" | chpasswd ; RUN useradd --create-home znc ; echo "znc:znc" | chpasswd # Install sshd, znc, and znc extras -RUN mkdir /var/run/sshd ; apt-get update ; apt-get install -y supervisor vim openssh-server znc znc-python znc-dev dpkg-dev +RUN mkdir /var/run/sshd ; apt-get update ; apt-get install -y supervisor vim openssh-server znc znc-python znc-dev dpkg-dev pisg nginx-light # Get ZNC source RUN su -c 'cd /home/znc ; apt-get source znc' znc +# Set nginx workers to a low number +RNN sed -i -e"s/^worker_processes\s*4/worker_processes 1/" /etc/nginx/nginx.conf +# Set nginx user to ZNC user +RUN sed -i -e"s/^user\s*www\-data/user znc/" /etc/nginx/nginx.conf +# Set nginx root +RUN sed -i -e"s/^\s*root\s*\/usr\/share\/nginx\/html/ root \/home\/znc\/pisg\/output/" /etc/nginx/sites-enabled/default +# Make pisg root +RUN su -c "mkdir /home/znc/pisg ; mkdir /home/znc/pisg/output ; mkdir /home/znc/pisg/config" znc +# Turn off nginx daemon +RUN echo "daemon off;" >> /etc/nginx/nginx.conf +# Install crontab +COPY crontab /tmp/ +RUN crontab -u znc /tmp/crontab +RUN rm /tmp/crontab +# Install stuff for log generation +RUN mkdir /home/znc/pisg /home/znc/pisg/cache /home/znc/pisg/output +COPY pisg.py /home/znc/pisg/ +RUN chmod +x /home/znc/pisg/pisg.py + # Install startup stuff COPY daemons.conf /etc/supervisor/conf.d/daemons.conf COPY start /start RUN chmod +x /start +# Ports +EXPOSE 80 EXPOSE 22 diff --git a/crontab b/crontab new file mode 100644 index 0000000..1f865be --- /dev/null +++ b/crontab @@ -0,0 +1,2 @@ +0 3 * * * /home/znc/pisg/pisg.py + diff --git a/daemons.conf b/daemons.conf index 83662be..a957428 100644 --- a/daemons.conf +++ b/daemons.conf @@ -9,3 +9,11 @@ autorestart=true command=su -c "/usr/bin/znc --foreground" znc autorestart=true +[program:cron] +command=/usr/sbin/cron -f +autorestart=true + +[program:nginx] +command=/usr/sbin/nginx +autorestart=true + diff --git a/default b/default new file mode 100644 index 0000000..f0e9910 --- /dev/null +++ b/default @@ -0,0 +1,14 @@ +server { + listen 80 default_server; + listen [::]:80 default_server ipv6only=on; + root /home/znc/pisg/output; + index index.html index.htm; + server_name localhost; + location / { + autoindex on; + try_files $uri $uri/ =404; + } + location /gfx/ { + alias /usr/share/pisg/gfx/; + } +} diff --git a/pisg.py b/pisg.py new file mode 100755 index 0000000..3e2807f --- /dev/null +++ b/pisg.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +import subprocess +from os import listdir,unlink,chdir +from os.path import exists +from sys import exit +from random import randint + +class logfile: + def __init__(self, username, network, channel): + self.username = username + self.network = network + self.channel = channel + self.path = "/home/znc/.znc/users/%s/moddata/log/%s_%s" % (self.username, self.network, self.channel) + + def __str__(self): + return "" % (self.username, self.network, self.channel, self.path) + + def generate_config(self): + return """ + + + + + + + + + + + + + + + + + + + Logfile = "%(logdir)s_*.log" + Format = "energymech" + Network = "%(network)s" + OutputFile = "/home/znc/pisg/output/%(username)s_%(network)s_%(channel)s.html" +""" % {"logdir":self.path, "network":self.network, "channel":self.channel, "username":self.username} + + def run_pisg(self): + configname = "config.%s" % str(randint(0,10000)) + open(configname, "w").write(self.generate_config()) + proc = subprocess.Popen(['pisg',"-co", configname], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + print(self.path +": "+str(proc.communicate())) + unlink(configname) + + +if __name__ == "__main__": + chdir("/home/znc/pisg") + logs = [] + for user in listdir("/home/znc/.znc/users/"): + if not exists("/home/znc/.znc/users/%s/moddata/log/" % user): + continue + + networks = {} + + for fname in listdir("/home/znc/.znc/users/%s/moddata/log/" % user): + network, parts = fname.split("_", 1) + if not network in networks: + networks[network]=[] + channel, parts = parts.rsplit("_", 1) + if "#" in channel and not channel in networks[network]: + networks[network].append(channel) + + print(user) + print(networks) + for network in networks: + for channel in networks[network]: + logs.append(logfile(user, network, channel)) + + for log in logs: + log.run_pisg() +