-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerAccessor.py
42 lines (31 loc) · 1.16 KB
/
ServerAccessor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import ConfigProvider
from fabric import Connection
from paramiko import SFTPClient
from ConfigProvider import ConfigProvider
class ServerAccessor:
def __init__(self, server, user, ssl_path):
self.server = server
self.user = user
self.ssl_path = ssl_path
self.connection = Connection(self.server, user=self.user, connect_kwargs={
"key_filename": self.ssl_path
})
self.sftp_client = self.open_sftp()
self.sftp_client.chdir(ConfigProvider.get_server_property("modsdir"))
# run command
def run(self, command: str):
result = self.connection.run(command)
return result
def open_sftp(self) -> SFTPClient:
return self.connection.sftp()
# upload file or directory
def upload(self, local_path: str, remote_path: str, callback):
self.sftp_client.put(local_path, remote_path, callback=callback)
def read_files(self, path: str = '.'):
return self.sftp_client.listdir(path)
# delete file
def delete(self, path: str):
self.sftp_client.remove(path)
def close(self):
self.connection.close()
self.sftp_client.close()