fixed issue related to /home partition having all space and the /var/www folder running out of space

This commit is contained in:
Christian 2021-02-13 21:27:46 +01:00
parent 69eddeb4f4
commit b831446984
9 changed files with 31 additions and 60 deletions

8
demo_mover/README.md Normal file → Executable file
View File

@ -8,10 +8,4 @@
##config file: server specifics ##config file: server specifics
##python3 demo_mover.py config.json ##nohup screen -d -m -S demo_mover python3 demo_mover.py config.json
##systemctl enable demo_mover.timer
##systemctl start demo_mover.timer
##logging: journalctl -f -u demo_mover.service
##logging: systemctl status demo_mover.service
###test commit on new git

2
demo_mover/config.json Normal file → Executable file
View File

@ -15,7 +15,7 @@
"hostname": "163.172.117.46", "hostname": "163.172.117.46",
"username": "gameservers", "username": "gameservers",
"port": "22", "port": "22",
"path": "/var/www/demos/", "path": "/home/gameservers/demos/",
"remote_type": "sftp" "remote_type": "sftp"
} }
}, },

20
demo_mover/demo_mover.py Normal file → Executable file
View File

@ -50,18 +50,14 @@ def main():
config_file = sys.argv[1] config_file = sys.argv[1]
remotes, jobs, settings = load_config(config_file) remotes, jobs, settings = load_config(config_file)
for job in jobs: for job in jobs:
try: src = create_remote(remotes[job["src"]], settings)
src = create_remote(remotes[job["src"]], settings) dest = create_remote(remotes[job["dest"]], settings)
dest = create_remote(remotes[job["dest"]], settings) source_files = src.list_dir()
source_files = src.list_dir() distribute_files(source_files, dest)
distribute_files(source_files, dest) for pathfile in source_files:
except Exception as e: src.delete_local(pathfile)
logging.warning('exception caught: ', exc_info = True) dest.delete_remote(source_files)
finally: logging.info(('finished job: ', job))
for pathfile in source_files:
src.delete_local(pathfile)
dest.delete_remote(source_files)
logging.info('finished jobs')
if __name__ == '__main__': if __name__ == '__main__':
main() main()

5
demo_mover/remote_local_dir.py Normal file → Executable file
View File

@ -1,6 +1,7 @@
from pathlib import Path from pathlib import Path
import contextlib import contextlib
import os import os
from datetime import timedelta, datetime
class local_dir_remote: class local_dir_remote:
def __init__(self, config): def __init__(self, config):
@ -10,12 +11,12 @@ class local_dir_remote:
def list_dir(self): def list_dir(self):
path = Path(self.path) path = Path(self.path)
file_list = [] file_list = []
delta = timedelta(minutes=5) delta = timedelta(minutes=2)
for file_demo in list(path.glob('*')): for file_demo in list(path.glob('*')):
mtime = datetime.fromtimestamp(os.stat(file_demo).st_mtime) mtime = datetime.fromtimestamp(os.stat(file_demo).st_mtime)
if mtime < datetime.now() - delta: if mtime < datetime.now() - delta:
file_list.append(file_demo) file_list.append(file_demo)
return file_list return file_list
def delete_local(self, pathfile): def delete_local(self, pathfile):
with contextlib.suppress(FileNotFoundError): with contextlib.suppress(FileNotFoundError):

49
demo_mover/remote_sftp.py Normal file → Executable file
View File

@ -22,19 +22,9 @@ class sftp_remote:
self.password = settings[self.remote_type][self.hostname][self.username]['password'] self.password = settings[self.remote_type][self.hostname][self.username]['password']
def connect(self): def connect(self):
total_attempts = int(self.remote_attempts)
self.transport = paramiko.Transport((self.hostname, int(self.port))) self.transport = paramiko.Transport((self.hostname, int(self.port)))
while total_attempts > 0: self.transport.connect(username = self.username, password = self.password)
try: self.sftp = paramiko.SFTPClient.from_transport(self.transport)
self.transport.connect(username = self.username, password = self.password)
self.sftp = paramiko.SFTPClient.from_transport(self.transport)
return True
except NoValidConnectionsError as e:
print('sftp connect failed: ', e)
total_attempts = self.subtract_remote_attempts(total_attempts)
if total_attempts == 0:
self.remote_error = e
return False
def disconnect(self): def disconnect(self):
del (self.sftp) del (self.sftp)
@ -81,29 +71,18 @@ class sftp_remote:
filename = local_path_str.split("/")[-1] filename = local_path_str.split("/")[-1]
remote_path = remote_path + filename remote_path = remote_path + filename
local_temp_folder = local_temp_folder + filename local_temp_folder = local_temp_folder + filename
#print('local_path_str: ', local_path_str, '\n remote_path: ', remote_path, '\n local_temp_folder: ', local_temp_folder)
while total_attempts > 0: while total_attempts > 0:
try: self.connect()
self.connect() self.sftp.put(local_path, remote_path)
self.sftp.put(local_path, remote_path) self.disconnect()
self.disconnect() self.connect()
self.connect() self.sftp.get(remote_path, local_temp_folder)
self.sftp.get(remote_path, local_temp_folder) self.disconnect()
self.disconnect() local_sha256 = self.digest(local_temp_folder)
local_sha256 = self.digest(local_temp_folder) os.remove(local_temp_folder)
os.remove(local_temp_folder) if local_sha256 == sha256:
#print('removing: ', local_temp_folder) #print('sha confirmed')
time.sleep(2) return True
sha256_local_upload_check = self.digest(local_path)
if sha256 != sha256_local_upload_check:
return False #file is currently being uploaded
if local_sha256 == sha256:
#print('sha confirmed')
return True
except SSHException as e:
print(e)
self.remote_error = e
total_attempts = self.subtract_remote_attempts(total_attempts) total_attempts = self.subtract_remote_attempts(total_attempts)
if total_attempts == 0: if total_attempts == 0:
return False return False
@ -119,7 +98,7 @@ class sftp_remote:
pathfile = self.path + filename pathfile = self.path + filename
utime = self.sftp.stat(pathfile).st_mtime utime = self.sftp.stat(pathfile).st_mtime
last_modified = datetime.fromtimestamp(utime) last_modified = datetime.fromtimestamp(utime)
if (datetime.now() - last_modified) > timedelta(days=30 * 3): if (datetime.now() - last_modified) > timedelta(days=30 * 1.5):
print('deleting file remotely: ', pathfile) print('deleting file remotely: ', pathfile)
self.sftp.remove(pathfile) self.sftp.remove(pathfile)
self.disconnect() self.disconnect()

0
demo_mover/requirements.txt Normal file → Executable file
View File

0
demo_mover/run_demo_mover.sh Normal file → Executable file
View File

3
demo_mover/systemd/demo_mover.service Normal file → Executable file
View File

@ -1,8 +1,9 @@
[Unit] [Unit]
Description=Moves demos from the local machine to remote destinations every 20 minutes Description=Moves demos from the local machine to remote destinations every 5 minutes
[Service] [Service]
Type=simple Type=simple
User=gameservers User=gameservers
WorkingDirectory=/home/gameservers/demo_mover WorkingDirectory=/home/gameservers/demo_mover
ExecStart=/home/gameservers/demo_mover/run_demo_mover.sh ExecStart=/home/gameservers/demo_mover/run_demo_mover.sh

4
demo_mover/systemd/demo_mover.timer Normal file → Executable file
View File

@ -1,9 +1,9 @@
[Unit] [Unit]
Description=Moves demos from the local machine to remote destinations every 20 minutes Description=Moves demos from the local machine to remote destinations every 5 minutes
Requires=demo_mover.service Requires=demo_mover.service
[Timer] [Timer]
OnCalendar=*-*-* *:0,20,40 OnCalendar=*-*-* *:0,5,10,15,20,25,30,35,40,45,50,55
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target