Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1, change project name, LocustWrapper to Stormer; 2, add sput subcommands, support copying local file/directory to remote host with ssh.
- Loading branch information
Showing
12 changed files
with
236 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,3 +87,5 @@ ENV/ | |
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
- | ||
hostname: '192.168.10.11' | ||
port: 22 | ||
username: "root" | ||
password: "123456" | ||
- | ||
hostname: '192.168.10.12' | ||
port: 22 | ||
username: "root" | ||
password: "123456" | ||
- | ||
hostname: '192.168.10.13' | ||
port: 22 | ||
username: "root" | ||
password: "123456" |
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import gevent.monkey; gevent.monkey.patch_all() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import logging | ||
from locust.log import setup_logging | ||
|
||
# setup logging | ||
loglevel = 'INFO' | ||
logfile = None | ||
|
||
setup_logging(loglevel, logfile) | ||
logger = logging.getLogger(__name__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import os | ||
import time | ||
import paramiko | ||
import threading | ||
from stormer.logger import logger | ||
from stormer import base | ||
|
||
class SSHConnector(paramiko.SSHClient): | ||
|
||
def __init__(self, host): | ||
super(SSHConnector, self).__init__() | ||
self.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | ||
self.connect(**host) | ||
self.sftp = self.open_sftp() | ||
logger.info("Connected to {}:{} with {}".format( | ||
host['hostname'], host['port'], host['username'])) | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_value, exc_traceback): | ||
self.sftp.close() | ||
self.close() | ||
|
||
def exec_cmd(self, cmd): | ||
stdin, stdout, stderr = self.exec_command(cmd) | ||
return stdout.readlines() | ||
|
||
def create_remote_dir(self, remote_dir): | ||
cmd_create_dir = 'mkdir -p {}'.format(remote_dir) | ||
self.exec_cmd(cmd_create_dir) | ||
time.sleep(0.5) | ||
|
||
def get_file(self, remotefile, localfile): | ||
self.sftp.get(remotefile, localfile) | ||
|
||
def get_dir(self, remote_dir, local_dir): | ||
pass | ||
|
||
def put_file(self, localfile, remotefile): | ||
remote_dirpath = os.path.dirname(remotefile) | ||
if remote_dirpath == '': | ||
remotefile = os.path.join('~', remotefile) | ||
else: | ||
cmd_check_dir = 'test -d {} && echo "ISDIR" || echo "NOTDIR"'.format(remote_dirpath) | ||
output = self.exec_cmd(cmd_check_dir) | ||
if output and 'NOTDIR' in output[0]: | ||
self.create_remote_dir(remote_dirpath) | ||
|
||
logger.info('transfer {} to {}'.format(localfile, remotefile)) | ||
self.sftp.put(localfile, remotefile) | ||
|
||
def __get_all_files_in_local_dir(self, local_dir): | ||
all_files = list() | ||
files = os.listdir(local_dir) | ||
for filename in files: | ||
path = os.path.join(local_dir, filename) | ||
if os.path.isdir(path): | ||
all_files.extend(self.__get_all_files_in_local_dir(path)) | ||
else: | ||
all_files.append(path) | ||
return all_files | ||
|
||
def put_dir(self, local_dir, remote_dir): | ||
""" transfer all file in local_dir to remote_dir. | ||
""" | ||
all_files = self.__get_all_files_in_local_dir(local_dir) | ||
for localfile in all_files: | ||
relative_path = localfile[len(local_dir) + 1:] | ||
remotefile = os.path.join(remote_dir, relative_path) | ||
remote_file_dir = os.path.dirname(remotefile) | ||
self.create_remote_dir(remote_file_dir) | ||
self.put_file(localfile, remotefile) | ||
|
||
|
||
def sput(hostsfile, localpath, remotepath): | ||
|
||
def _put_file(host, localfile, remotefile): | ||
with SSHConnector(host) as ssh_client: | ||
ssh_client.put_file(localfile, remotefile) | ||
|
||
def _put_dir(host, local_dir, remote_dir): | ||
with SSHConnector(host) as ssh_client: | ||
ssh_client.put_dir(local_dir, remote_dir) | ||
|
||
host_list = base.parse_yaml(hostsfile) | ||
localpath = os.path.abspath(localpath) | ||
target_func = _put_dir if os.path.isdir(localpath) else _put_file | ||
|
||
for host in host_list: | ||
threading.Thread(target=target_func, args=(host, localpath, remotepath)).start() |