sddsfkjfdskajkdfs

This commit is contained in:
Minecon724 2023-11-01 14:46:30 +01:00
commit 4023167b91
8 changed files with 178 additions and 0 deletions

35
install.sh Normal file
View File

@ -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

8
mnt-nginx.mount Normal file
View File

@ -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

1
nman Normal file
View File

@ -0,0 +1 @@
LISTEN_IP=

106
nman.py Normal file
View File

@ -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')

9
nman.service Normal file
View File

@ -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

2
nman.sh Normal file
View File

@ -0,0 +1,2 @@
cd /opt
python3 -c 'import nman; nman.update()'

9
nman.timer Normal file
View File

@ -0,0 +1,9 @@
[Unit]
Description=Run foo weekly and on boot
[Timer]
OnBootSec=1min
OnUnitActiveSec=1h
[Install]
WantedBy=timers.target

8
rclone.conf Normal file
View File

@ -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