|
import ftplib |
|
import os |
|
from dotenv import load_dotenv |
|
import time |
|
import io |
|
import requests |
|
|
|
load_dotenv() |
|
MODE = os.getenv('FTP_MODE') |
|
HOST = os.getenv('FTP_HOST') |
|
USER = os.getenv('FTP_USER') |
|
PASS = os.getenv('FTP_PASS') |
|
|
|
class Overlord(): |
|
rootPath="/public_html" |
|
|
|
systemPath="/_overlord" |
|
systemDirtree=[ |
|
systemPath, |
|
systemPath+"/to_unpack", |
|
systemPath+"/update_backup", |
|
systemPath+"/snet_logs" |
|
] |
|
#webappList=[ |
|
# "ssubwebapp_com", |
|
# "beboki_pl" |
|
#] |
|
|
|
def config(self, host, username, password): |
|
self.ftpHost=host |
|
self.ftpUser=username |
|
self.ftpPass=password |
|
|
|
def connect(self): |
|
self.ftpHook=ftplib.FTP_TLS(self.ftpHost) |
|
self.ftpHook.login(self.ftpUser, self.ftpPass) |
|
|
|
def end(self): |
|
self.ftpHook.close() |
|
|
|
def delDirTree(self, path, withThisDir=True): #idea from https://stackoverflow.com/questions/10042838/delete-all-files-and-folders-after-connecting-to-ftp |
|
self.ftpHook.cwd(path) |
|
for item in self.ftpHook.nlst(): |
|
try: |
|
self.ftpHook.delete(item) |
|
except Exception: |
|
self.delDirTree(item) |
|
self.ftpHook.cwd("..") |
|
if withThisDir: |
|
self.ftpHook.rmd(path) |
|
|
|
def clearWebapp(self): |
|
self.delDirTree(Overlord.rootPath, False) |
|
|
|
def purgeFromServer(self): |
|
#self.delDirTree(Overlord.rootPath, False) |
|
self.delDirTree(Overlord.systemPath) |
|
#self.ftpHook.mkd(Overlord.rootPath) |
|
|
|
def installOnServer(self): |
|
for internalDir in Overlord.systemDirtree: |
|
self.ftpHook.mkd(internalDir) |
|
|
|
phpFilesDir=os.path.dirname(__file__)#+"/server-internal" |
|
self.ftpHook.cwd(Overlord.systemDirtree[0]) |
|
|
|
for phpFile in os.listdir(phpFilesDir): |
|
with open(phpFilesDir+"/"+phpFile, 'rb') as file: |
|
self.ftpHook.storbinary('STOR '+Overlord.systemDirtree[0]+"/"+phpFile, file) |
|
|
|
#for appDir in Overlord.webappList: |
|
# appDirPath=Overlord.rootPath+"/"+appDir |
|
# self.ftpHook.mkd(appDirPath) |
|
|
|
self.ftpHook.cwd(Overlord.rootPath) |
|
#self.ftpHook.dir() |
|
|
|
def updateApp(self, appname, zipFilePath): |
|
if "_" in [appname[0], appname[-1]] or "." in appname: |
|
raise NameError('forbidden appname first char') |
|
#if not appname in Overlord.webappList: |
|
# raise NameError('webapp not exists') |
|
appPath=Overlord.rootPath |
|
appRemoteZip=appname+"."+str(int(time.time()))+".zip" |
|
with open(zipFilePath, 'rb') as file: |
|
self.ftpHook.storbinary('STOR '+Overlord.systemDirtree[1]+"/"+appRemoteZip, file) |
|
indexInstallScript=io.BytesIO() |
|
with open("./packed-installer.php", 'r') as template: |
|
for line in template.readlines(): |
|
replacer=line |
|
replacer=replacer.replace("<<<APPNAME>>>", appname) |
|
replacer=replacer.replace("<<<ZIPNAME>>>", appRemoteZip) |
|
replacer=replacer.replace("<<<SYSTEMDIR>>>", Overlord.systemDirtree[0]) |
|
indexInstallScript.write(bytes(replacer, "utf-8")) |
|
indexInstallScript.seek(0) |
|
#if appname in self.ftpHook.nlst(Overlord.rootPath): |
|
# self.delDirTree(appPath) |
|
#self.ftpHook.mkd(appPath) |
|
self.clearWebapp() |
|
self.ftpHook.storbinary('STOR '+appPath+"/meowmeowmeow.php", indexInstallScript) |
|
with open("./reloc-index.php", 'rb') as template: |
|
self.ftpHook.storbinary('STOR '+appPath+"/index.php", template) |
|
|
|
#self.end() |
|
|
|
def lounchServerSideAct(self, appname): |
|
appDomain=".".join(appname.split("_")) |
|
result=requests.get("http://"+appDomain+"/") |
|
return (result.text, result) |
|
|
|
ovrlrdClient=Overlord() |
|
ovrlrdClient.config(HOST, USER, PASS) |
|
ovrlrdClient.connect() |
|
#ovrlrdClient.purgeFromServer() |
|
#ovrlrdClient.installOnServer() |
|
ovrlrdClient.updateApp("subwebapp_com", "/media/asterisk/snet-A0010/__WORKSHOP/forge/examplescope/webapp.com/subwebapp_com-1692723639.zip") |
|
ovrlrdClient.end() |
|
time.sleep(3) |
|
print(ovrlrdClient.lounchServerSideAct("subwebapp_com")) |
|
|
|
|
|
#def updateApp(appname, zipFilePath): |
|
# global MODE, HOST, USER, PASS |
|
# ftpHost=None |
|
# appPath="/main_webapps/"+appname |
|
# |
|
# if "_" in [appname[0], appname[-1]] or "." in appname: |
|
# raise NameError('forbidden appname first char') |
|
# appDomain=".".join(appname.split("_")) |
|
|
|
|
|
# if MODE.lower()=="ftp": |
|
# ftpHost=ftplib.FTP(HOST) |
|
# elif MODE.lower()=="ftps": |
|
# ftpHost=ftplib.FTP_TLS(HOST) |
|
# else: |
|
# raise RuntimeError("unknown mode of FTP") |
|
|
|
# ftpsHost.login(USER, PASS) |
|
|
|
# if appname in ftpHook.nlst(appPath): |
|
# delDirTree(ftpsHost, appPath) |
|
# ftpsHost.mkd(appPath) |
|
|
|
# ftpsHost.close() |