commit 4023167b91ec0cfeb8f9364bcd191fde97632749 Author: Minecon724 Date: Wed Nov 1 14:46:30 2023 +0100 sddsfkjfdskajkdfs diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..458bbba --- /dev/null +++ b/install.sh @@ -0,0 +1,35 @@ +apt update +apt install -y python3 rclone fuse + +if [ ! -f /root/.ssh/id_rsa.pub ] then + echo no ssh key, generate: rsa without password + exit 1 +fi + +echo +echo Add the following key to your remote sftp: +cat /root/.ssh/id_rsa.pub +echo +echo Press enter to continue +read + +cp -n rclone.conf /root/.config/rclone/rclone.conf +cp nman.service /etc/systemd/system/ +cp nman.timer /etc/systemd/system/ +cp mnt-nginx.mount /etc/systemd/system/ +cp nman /etc/default/ +cp nman.py /opt/ +cp nman.sh /opt/ +chmod +x /opt/nman.sh + +systemctl stop nginx + +rm -rf /etc/nginx +mkdir /etc/nginx + +systemctl enable --now mnt-nginx.mount + +/opt/nman.sh + +systemctl enable nman.timer +systemctl start nginx \ No newline at end of file diff --git a/mnt-nginx.mount b/mnt-nginx.mount new file mode 100644 index 0000000..db39315 --- /dev/null +++ b/mnt-nginx.mount @@ -0,0 +1,8 @@ +[Unit] +Description=nginx mount + +[Mount] +Type=rclone +What=files:nginx +Where=/mnt/nginx +Options=ro,_netdev,allow_other,args2env,config=/root/.config/rclone/rclone.conf,read-only \ No newline at end of file diff --git a/nman b/nman new file mode 100644 index 0000000..1c77eb0 --- /dev/null +++ b/nman @@ -0,0 +1 @@ +LISTEN_IP= \ No newline at end of file diff --git a/nman.py b/nman.py new file mode 100644 index 0000000..241a90e --- /dev/null +++ b/nman.py @@ -0,0 +1,106 @@ +import os +import shutil +from hashlib import sha1 + +REMOTE_PATH=os.getenv("REMOTE_PATH", "/mnt/nginx") +LOCAL_PATH=os.getenv("LOCAL_PATH", "/etc/nginx") + +ENV={} + +def refresh_env(): + global ENV + + lines = [l.strip() for l in open('/etc/default/nman')] + ENV = {k: v for k, v in [l.split('=') for l in lines]} + +def apply_env(data): + for k, v in ENV.items(): + data = data.replace(f'$({k})', v) + return data + +def restart(): + return os.system('systemctl restart nginx') == 0 + +def full_copy(): + shutil.rmtree(LOCAL_PATH) + os.mkdir(LOCAL_PATH) + shutil.copytree(REMOTE_PATH, LOCAL_PATH) + +def tree(path): + if not os.path.exists(path): + raise FileNotFoundError() + for root, dirs, files in os.walk(path): + rl = len(path) + pre = root[rl:] + if pre.startswith('/'): + pre = pre[1:] + print(pre) + + for file in files: + yield (0, os.path.join(pre, file)) + for dir in dirs: + yield (1, os.path.join(pre, dir)) + +def compare(all=False): + refresh_env() + + remote_tree = [i for i in tree(REMOTE_PATH)] + local_tree = [i for i in tree(LOCAL_PATH)] + remote_new = [i for i in remote_tree if not i in local_tree] + local_new = [i for i in local_tree if not i in remote_tree] + + outdated = [] + + for i in [i[1] for i in local_tree if i in remote_tree and i[0] == 0]: + data = open(os.path.join(LOCAL_PATH, i), 'r').read() + data = apply_env(data).encode() + local_hash = sha1(data) + + data = open(os.path.join(REMOTE_PATH, i), 'r').read() + data = apply_env(data).encode() + remote_hash = sha1(data) + + if all or local_hash == remote_hash: + outdated += [(0, i)] + + return {'create': remote_new, 'delete': local_new, 'sync': outdated} + +def update(force=False): + diff = compare(all=force) + changes = len([i for i in [diff[k] for k in diff.keys()]]) + + print(diff) + + print('changes: %d' % changes) + + for t, i in diff['create'] + diff['sync']: + if t == 1: + continue + local = os.path.join(LOCAL_PATH, i) + remote = os.path.join(REMOTE_PATH, i) + + print('syncing %s to %s' % (remote, local)) + + os.makedirs(os.path.dirname(local), exist_ok=True) + + if local.endswith('.conf'): + f = open(local, 'w') + f.write(apply_env(open(remote, 'r').read())) + f.close() + else: + shutil.copy(remote, local) + + for t, i in diff['delete']: + path = os.path.join(LOCAL_PATH, i) + print('deleting %s' % path) + + if t == 0: + os.unlink(path) + else: + shutil.rmtree(path) + + if changes > 0: + if not restart(): + print("error restartyi ng!") + + print('done update') diff --git a/nman.service b/nman.service new file mode 100644 index 0000000..a3355df --- /dev/null +++ b/nman.service @@ -0,0 +1,9 @@ +[Unit] +Description=Run foo weekly and on boot +After=mnt-nginx.mount + +[Service] +ExecStart=/opt/nman.sh + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/nman.sh b/nman.sh new file mode 100644 index 0000000..02f1629 --- /dev/null +++ b/nman.sh @@ -0,0 +1,2 @@ +cd /opt +python3 -c 'import nman; nman.update()' \ No newline at end of file diff --git a/nman.timer b/nman.timer new file mode 100644 index 0000000..f95003e --- /dev/null +++ b/nman.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Run foo weekly and on boot + +[Timer] +OnBootSec=1min +OnUnitActiveSec=1h + +[Install] +WantedBy=timers.target \ No newline at end of file diff --git a/rclone.conf b/rclone.conf new file mode 100644 index 0000000..9480900 --- /dev/null +++ b/rclone.conf @@ -0,0 +1,8 @@ +[files] +type = sftp +host = +user = files +key_file = /root/.ssh/id_rsa +shell_type = unix +md5sum_command = none +sha1sum_command = none \ No newline at end of file