2021-03-12 00:39:39 +00:00
|
|
|
import sys
|
2021-03-30 21:55:04 +00:00
|
|
|
import os
|
2021-03-12 00:39:39 +00:00
|
|
|
import pysftp
|
|
|
|
import paramiko
|
2021-05-30 16:52:35 +00:00
|
|
|
import stat
|
|
|
|
import re
|
2021-03-12 00:39:39 +00:00
|
|
|
|
|
|
|
from utils.consoleoutput import oColors
|
2021-03-30 21:55:04 +00:00
|
|
|
from handlers.handle_config import configurationValues
|
2021-03-12 00:39:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def createSFTPConnection():
|
2021-03-30 21:55:04 +00:00
|
|
|
configValues = configurationValues()
|
2021-03-12 00:39:39 +00:00
|
|
|
cnopts = pysftp.CnOpts()
|
|
|
|
cnopts.hostkeys = None # TODO fix this
|
|
|
|
try:
|
2021-03-30 21:55:04 +00:00
|
|
|
sftp = pysftp.Connection(configValues.sftp_server, username=configValues.sftp_user, \
|
|
|
|
password=configValues.sftp_password, port=configValues.sftp_port, cnopts=cnopts)
|
2021-03-12 00:39:39 +00:00
|
|
|
except paramiko.ssh_exception.AuthenticationException:
|
|
|
|
print(oColors.brightRed + "[SFTP]: Wrong Username/Password" + oColors.standardWhite)
|
|
|
|
except paramiko.ssh_exception.SSHException:
|
|
|
|
print(oColors.brightRed + "[SFTP]: The SFTP server isn't available." + oColors.standardWhite)
|
|
|
|
try:
|
|
|
|
return sftp
|
|
|
|
except UnboundLocalError:
|
|
|
|
print(oColors.brightRed + "[SFTP]: Check your config.ini!" + oColors.standardWhite)
|
|
|
|
print(oColors.brightRed + "Exiting program..." + oColors.standardWhite)
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
|
def sftp_showPlugins(sftp):
|
2021-05-30 16:52:35 +00:00
|
|
|
configValues = configurationValues()
|
|
|
|
sftp.cd(configValues.sftp_folderPath)
|
2021-03-12 00:39:39 +00:00
|
|
|
for attr in sftp.listdir_attr():
|
|
|
|
print(attr.filename, attr)
|
|
|
|
|
|
|
|
|
|
|
|
def sftp_upload_file(sftp, itemPath):
|
2021-05-30 16:52:35 +00:00
|
|
|
configValues = configurationValues()
|
2021-03-12 00:39:39 +00:00
|
|
|
try:
|
2021-05-30 16:52:35 +00:00
|
|
|
sftp.chdir(configValues.sftp_folderPath)
|
2021-03-12 00:39:39 +00:00
|
|
|
sftp.put(itemPath)
|
|
|
|
|
|
|
|
except FileNotFoundError:
|
2021-03-31 16:11:41 +00:00
|
|
|
print(oColors.brightRed + "[SFTP]: The 'plugins' folder couldn*t be found on the remote host!" + oColors.standardWhite)
|
|
|
|
print(oColors.brightRed + "[SFTP]: Aborting uploading." + oColors.standardWhite)
|
2021-03-12 00:39:39 +00:00
|
|
|
sftp.close()
|
|
|
|
|
|
|
|
|
2021-03-15 15:26:00 +00:00
|
|
|
def sftp_upload_server_jar(sftp, itemPath):
|
|
|
|
try:
|
|
|
|
sftp.chdir('.')
|
|
|
|
sftp.put(itemPath)
|
|
|
|
except FileNotFoundError:
|
2021-03-31 16:11:41 +00:00
|
|
|
print(oColors.brightRed + "[SFTP]: The 'root' folder couldn*t be found on the remote host!" + oColors.standardWhite)
|
|
|
|
print(oColors.brightRed + "[SFTP]: Aborting uploading." + oColors.standardWhite)
|
2021-03-15 15:26:00 +00:00
|
|
|
sftp.close()
|
|
|
|
|
|
|
|
|
2021-03-12 00:39:39 +00:00
|
|
|
def sftp_listAll(sftp):
|
2021-05-30 16:52:35 +00:00
|
|
|
configValues = configurationValues()
|
2021-03-12 00:39:39 +00:00
|
|
|
try:
|
2021-05-30 16:52:35 +00:00
|
|
|
sftp.chdir(configValues.sftp_folderPath)
|
2021-03-12 00:39:39 +00:00
|
|
|
installedPlugins = sftp.listdir()
|
|
|
|
except FileNotFoundError:
|
2021-03-31 16:11:41 +00:00
|
|
|
print(oColors.brightRed + "[SFTP]: The 'plugins' folder couldn*t be found on the remote host!" + oColors.standardWhite)
|
2021-03-12 00:39:39 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
return installedPlugins
|
|
|
|
except UnboundLocalError:
|
2021-03-31 16:11:41 +00:00
|
|
|
print(oColors.brightRed + "[SFTP]: No plugins were found." + oColors.standardWhite)
|
2021-03-15 15:26:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def sftp_listFilesInServerRoot(sftp):
|
|
|
|
try:
|
|
|
|
filesInServerRoot = sftp.listdir()
|
|
|
|
except FileNotFoundError:
|
2021-03-31 16:11:41 +00:00
|
|
|
print(oColors.brightRed + "[SFTP]: The 'root' folder couldn*t be found on the remote host!" + oColors.standardWhite)
|
2021-03-15 15:26:00 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
return filesInServerRoot
|
|
|
|
except UnboundLocalError:
|
2021-03-31 16:11:41 +00:00
|
|
|
print(oColors.brightRed + "[SFTP]: No Serverjar was found." + oColors.standardWhite)
|
2021-03-30 21:55:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def sftp_downloadFile(sftp, downloadPath, fileToDownload):
|
|
|
|
configValues = configurationValues()
|
|
|
|
sftp.cwd(configValues.sftp_folderPath)
|
|
|
|
currentDirectory = os.getcwd()
|
|
|
|
os.chdir('TempSFTPFolder')
|
|
|
|
sftp.get(fileToDownload)
|
|
|
|
sftp.close()
|
|
|
|
os.chdir(currentDirectory)
|
2021-05-30 16:52:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def sftp_validateFileAttributes(sftp, pluginPath):
|
|
|
|
pluginSFTPAttribute = sftp.lstat(pluginPath)
|
|
|
|
if stat.S_ISDIR(pluginSFTPAttribute.st_mode):
|
|
|
|
return False
|
|
|
|
if stat.S_ISDIR(pluginSFTPAttribute.st_mode):
|
|
|
|
if re.search(r'.jar$', pluginSFTPAttribute.filename):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|