diff options
-rw-r--r-- | api/actions/env.py | 200 | ||||
-rwxr-xr-x | api/api-prepare.sh | 24 | ||||
-rw-r--r-- | dashboard/ping_dashboard.json | 1 | ||||
-rw-r--r-- | docker/Dockerfile | 2 | ||||
-rwxr-xr-x | install.sh | 1 | ||||
-rwxr-xr-x | setup.py | 3 | ||||
-rwxr-xr-x | tests/ci/yardstick-verify | 2 | ||||
-rw-r--r-- | tests/unit/test_ssh.py | 60 | ||||
-rw-r--r-- | yardstick/cmd/commands/env.py | 24 | ||||
-rw-r--r-- | yardstick/cmd/commands/task.py | 16 | ||||
-rw-r--r-- | yardstick/common/constants.py | 34 | ||||
-rw-r--r-- | yardstick/common/httpClient.py | 3 | ||||
-rw-r--r-- | yardstick/common/utils.py | 41 | ||||
-rw-r--r-- | yardstick/ssh.py | 55 |
14 files changed, 429 insertions, 37 deletions
diff --git a/api/actions/env.py b/api/actions/env.py index 321649940..9e53dde4d 100644 --- a/api/actions/env.py +++ b/api/actions/env.py @@ -8,49 +8,132 @@ ############################################################################## import logging import threading +import subprocess import time +import json +import os +import errno from docker import Client from yardstick.common import constants as config from yardstick.common import utils as yardstick_utils +from yardstick.common.httpClient import HttpClient from api import conf as api_conf -from api.utils import common as common_utils from api.utils import influx +from api.utils.common import result_handler logger = logging.getLogger(__name__) +def createGrafanaContainer(args): + thread = threading.Thread(target=_create_grafana) + thread.start() + return result_handler('success', []) + + +def _create_grafana(): + client = Client(base_url=config.DOCKER_URL) + + try: + if not _check_image_exist(client, '%s:%s' % (config.GRAFANA_IMAGE, + config.GRAFANA_TAGS)): + client.pull(config.GRAFANA_IMAGE, config.GRAFANA_TAGS) + + _create_grafana_container(client) + + time.sleep(5) + + _create_data_source() + + _create_dashboard() + except Exception as e: + logger.debug('Error: %s', e) + + +def _create_dashboard(): + url = 'http://admin:admin@%s:3000/api/dashboards/db' % api_conf.GATEWAY_IP + data = json.load(file('../dashboard/ping_dashboard.json')) + HttpClient().post(url, data) + + +def _create_data_source(): + url = 'http://admin:admin@%s:3000/api/datasources' % api_conf.GATEWAY_IP + data = { + "name": "yardstick", + "type": "influxdb", + "access": "proxy", + "url": "http://%s:8086" % api_conf.GATEWAY_IP, + "password": "root", + "user": "root", + "database": "yardstick", + "basicAuth": True, + "basicAuthUser": "admin", + "basicAuthPassword": "admin", + "isDefault": False, + } + HttpClient().post(url, data) + + +def _create_grafana_container(client): + ports = [3000] + port_bindings = {k: k for k in ports} + host_config = client.create_host_config(port_bindings=port_bindings) + + container = client.create_container(image='%s:%s' % (config.GRAFANA_IMAGE, + config.GRAFANA_TAGS), + ports=ports, + detach=True, + tty=True, + host_config=host_config) + client.start(container) + + +def _check_image_exist(client, t): + return any(t in a['RepoTags'][0] for a in client.images() if a['RepoTags']) + + def createInfluxDBContainer(args): + thread = threading.Thread(target=_create_influxdb) + thread.start() + return result_handler('success', []) + + +def _create_influxdb(): + client = Client(base_url=config.DOCKER_URL) + try: - container = _create_influxdb_container() _config_output_file() - thread = threading.Thread(target=_config_influxdb) - thread.start() - return common_utils.result_handler('success', container) + + if not _check_image_exist(client, '%s:%s' % (config.INFLUXDB_IMAGE, + config.INFLUXDB_TAG)): + client.pull(config.INFLUXDB_IMAGE, tag=config.INFLUXDB_TAG) + + _create_influxdb_container(client) + + time.sleep(5) + + _config_influxdb() except Exception as e: - message = 'Failed to create influxdb container: %s' % e - return common_utils.error_handler(message) + logger.debug('Error: %s', e) -def _create_influxdb_container(): - client = Client(base_url=config.DOCKER_URL) +def _create_influxdb_container(client): ports = [8083, 8086] port_bindings = {k: k for k in ports} host_config = client.create_host_config(port_bindings=port_bindings) - container = client.create_container(image='tutum/influxdb', + container = client.create_container(image='%s:%s' % (config.INFLUXDB_IMAGE, + config.INFLUXDB_TAG), ports=ports, detach=True, tty=True, host_config=host_config) client.start(container) - return container def _config_influxdb(): - time.sleep(20) try: client = influx.get_data_db_client() client.create_user(config.USER, config.PASSWORD, config.DATABASE) @@ -61,11 +144,11 @@ def _config_influxdb(): def _config_output_file(): - yardstick_utils.makedirs('/etc/yardstick') - with open('/etc/yardstick/yardstick.conf', 'w') as f: + yardstick_utils.makedirs(config.YARDSTICK_CONFIG_DIR) + with open(config.YARDSTICK_CONFIG_FILE, 'w') as f: f.write("""\ [DEFAULT] -debug = True +debug = False dispatcher = influxdb [dispatcher_file] @@ -83,3 +166,90 @@ username = root password = root """ % api_conf.GATEWAY_IP) + + +def prepareYardstickEnv(args): + thread = threading.Thread(target=_prepare_env_daemon) + thread.start() + return result_handler('success', []) + + +def _prepare_env_daemon(): + + installer_ip = os.environ.get('INSTALLER_IP', 'undefined') + installer_type = os.environ.get('INSTALLER_TYPE', 'undefined') + + _check_variables(installer_ip, installer_type) + + _create_directories() + + rc_file = config.OPENSTACK_RC_FILE + + _get_remote_rc_file(rc_file, installer_ip, installer_type) + + _source_file(rc_file) + + _append_external_network(rc_file) + + _load_images() + + +def _check_variables(installer_ip, installer_type): + + if installer_ip == 'undefined': + raise SystemExit('Missing INSTALLER_IP') + + if installer_type == 'undefined': + raise SystemExit('Missing INSTALLER_TYPE') + elif installer_type not in config.INSTALLERS: + raise SystemExit('INSTALLER_TYPE is not correct') + + +def _create_directories(): + yardstick_utils.makedirs(config.YARDSTICK_CONFIG_DIR) + + +def _source_file(rc_file): + yardstick_utils.source_env(rc_file) + + +def _get_remote_rc_file(rc_file, installer_ip, installer_type): + + os_fetch_script = os.path.join(config.RELENG_DIR, config.OS_FETCH_SCRIPT) + + try: + cmd = [os_fetch_script, '-d', rc_file, '-i', installer_type, + '-a', installer_ip] + p = subprocess.Popen(cmd, stdout=subprocess.PIPE) + p.communicate()[0] + + if p.returncode != 0: + logger.debug('Failed to fetch credentials from installer') + except OSError as e: + if e.errno != errno.EEXIST: + raise + + +def _append_external_network(rc_file): + neutron_client = yardstick_utils.get_neutron_client() + networks = neutron_client.list_networks()['networks'] + try: + ext_network = next(n['name'] for n in networks if n['router:external']) + except StopIteration: + logger.warning("Can't find external network") + else: + cmd = 'export EXTERNAL_NETWORK=%s' % ext_network + try: + with open(rc_file, 'a') as f: + f.write(cmd + '\n') + except OSError as e: + if e.errno != errno.EEXIST: + raise + + +def _load_images(): + cmd = [config.LOAD_IMAGES_SCRIPT] + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, + cwd=config.YARDSTICK_REPOS_DIR) + output = p.communicate()[0] + logger.debug('The result is: %s', output) diff --git a/api/api-prepare.sh b/api/api-prepare.sh index c05dbb5ff..fade8ccc6 100755 --- a/api/api-prepare.sh +++ b/api/api-prepare.sh @@ -24,14 +24,26 @@ server { } } EOF +echo "daemon off;" >> /etc/nginx/nginx.conf fi # nginx service start when boot -cat << EOF >> /root/.bashrc +supervisor_config='/etc/supervisor/conf.d/yardstick.conf' -nginx_status=\$(service nginx status | grep not) -if [ -n "\${nginx_status}" ];then - service nginx restart - uwsgi -i /home/opnfv/repos/yardstick/api/yardstick.ini -fi +if [[ ! -e "${supervisor_config}" ]];then + cat << EOF > "${supervisor_config}" +[supervisord] +nodaemon = true + +[program:yardstick_nginx] +user = root +command = service nginx restart +autorestart = true + +[program:yardstick_uwsgi] +user = root +directory = /home/opnfv/repos/yardstick/api +command = uwsgi -i yardstick.ini +autorestart = true EOF +fi diff --git a/dashboard/ping_dashboard.json b/dashboard/ping_dashboard.json new file mode 100644 index 000000000..cbc4f67df --- /dev/null +++ b/dashboard/ping_dashboard.json @@ -0,0 +1 @@ +{"meta":{"type":"db","canSave":true,"canEdit":true,"canStar":true,"slug":null,"expires":"0001-01-01T00:00:00Z","created":"2016-10-09T00:45:46Z","updated":"2016-10-09T03:12:01Z","updatedBy":"admin","createdBy":"admin","version":7},"dashboard":{"annotations":{"list":[]},"editable":true,"gnetId":null,"hideControls":false,"id":null,"links":[],"refresh":false,"rows":[{"collapse":false,"editable":true,"height":"250px","panels":[{"aliasColors":{},"bars":false,"datasource":"yardstick","editable":true,"error":false,"fill":1,"grid":{"threshold1":1,"threshold1Color":"rgba(216, 200, 27, 0.27)","threshold2":0.5,"threshold2Color":"rgba(234, 112, 112, 0.22)","thresholdLine":false},"id":1,"isNew":true,"legend":{"alignAsTable":false,"avg":false,"current":false,"max":true,"min":true,"rightSide":false,"show":false,"total":false,"values":true},"lines":true,"linewidth":2,"links":[],"nullPointMode":"connected","percentage":false,"pointradius":5,"points":false,"renderer":"flot","seriesOverrides":[],"span":12,"stack":false,"steppedLine":false,"targets":[{"dsType":"influxdb","groupBy":[{"params":["$interval"],"type":"time"},{"params":["null"],"type":"fill"}],"measurement":"ping","policy":"default","refId":"A","resultFormat":"time_series","select":[[{"params":["rtt.ares"],"type":"field"},{"params":[],"type":"mean"}]],"tags":[]}],"timeFrom":null,"timeShift":null,"title":"Ping","tooltip":{"msResolution":true,"shared":true,"sort":0,"value_type":"cumulative"},"type":"graph","xaxis":{"show":true},"yaxes":[{"format":"short","label":null,"logBase":1,"max":null,"min":null,"show":true},{"format":"short","label":null,"logBase":1,"max":null,"min":null,"show":true}]}],"title":"Row"}],"schemaVersion":12,"sharedCrosshair":false,"style":"dark","tags":[],"templating":{"list":[]},"time":{"from":"now-24h","to":"now"},"timepicker":{"refresh_intervals":["5s","10s","30s","1m","5m","15m","30m","1h","2h","1d"],"time_options":["5m","15m","1h","6h","12h","24h","2d","7d","30d"]},"timezone":"browser","title":"Ping_Sample","version":7}} diff --git a/docker/Dockerfile b/docker/Dockerfile index 048804dc5..3dd94019a 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -48,6 +48,7 @@ RUN apt-get update && apt-get install -y \ nginx \ uwsgi \ uwsgi-plugin-python \ + supervisor \ python-setuptools && \ easy_install -U setuptools @@ -73,3 +74,4 @@ ADD http://download.cirros-cloud.net/0.3.3/cirros-0.3.3-x86_64-disk.img /home/op ADD http://cloud-images.ubuntu.com/trusty/current/trusty-server-cloudimg-amd64-disk1.img /home/opnfv/images/ COPY ./exec_tests.sh /usr/local/bin/ +CMD ["/usr/bin/supervisord"] diff --git a/install.sh b/install.sh index afb735195..e9b6035d9 100755 --- a/install.sh +++ b/install.sh @@ -16,6 +16,7 @@ apt-get update && apt-get install -y \ nginx \ uwsgi \ uwsgi-plugin-python \ + supervisor \ python-setuptools && \ easy_install -U setuptools @@ -28,7 +28,8 @@ setup( 'yardstick/nodes/*/*.yaml' ], 'tests': [ - 'opnfv/*/*.yaml' + 'opnfv/*/*.yaml', + 'ci/*.sh' ] }, url="https://www.opnfv.org", diff --git a/tests/ci/yardstick-verify b/tests/ci/yardstick-verify index 1a6682f85..7644c96c4 100755 --- a/tests/ci/yardstick-verify +++ b/tests/ci/yardstick-verify @@ -162,7 +162,7 @@ run_test() cat << EOF > /etc/yardstick/yardstick.conf [DEFAULT] -debug = True +debug = False dispatcher = ${DISPATCHER_TYPE} [dispatcher_file] diff --git a/tests/unit/test_ssh.py b/tests/unit/test_ssh.py index 1e021a051..88638a0a8 100644 --- a/tests/unit/test_ssh.py +++ b/tests/unit/test_ssh.py @@ -17,6 +17,7 @@ # rally/tests/unit/common/test_sshutils.py import os +import socket import unittest from cStringIO import StringIO @@ -164,10 +165,10 @@ class SSHTestCase(unittest.TestCase): def test_send_command(self, mock_paramiko): paramiko_sshclient = self.test_client._get_client() with mock.patch.object(paramiko_sshclient, "exec_command") \ - as mock_paramiko_exec_command: + as mock_paramiko_exec_command: self.test_client.send_command('cmd') mock_paramiko_exec_command.assert_called_once_with('cmd', - get_pty=True) + get_pty=True) class SSHRunTestCase(unittest.TestCase): @@ -307,6 +308,61 @@ class SSHRunTestCase(unittest.TestCase): self.fake_session.exit_status_ready.return_value = False self.assertRaises(ssh.SSHTimeout, self.test_client.run, "cmd") + @mock.patch("yardstick.ssh.open", create=True) + def test__put_file_shell(self, mock_open): + self.test_client.run = mock.Mock() + self.test_client._put_file_shell("localfile", "remotefile", 0o42) + + self.test_client.run.assert_called_once_with( + 'cat > "remotefile"&& chmod -- 042 "remotefile"', + stdin=mock_open.return_value.__enter__.return_value) + + @mock.patch("yardstick.ssh.os.stat") + def test__put_file_sftp(self, mock_stat): + sftp = self.fake_client.open_sftp.return_value = mock.MagicMock() + sftp.__enter__.return_value = sftp + + mock_stat.return_value = os.stat_result([0o753] + [0] * 9) + + self.test_client._put_file_sftp("localfile", "remotefile") + + sftp.put.assert_called_once_with("localfile", "remotefile") + mock_stat.assert_called_once_with("localfile") + sftp.chmod.assert_called_once_with("remotefile", 0o753) + sftp.__exit__.assert_called_once_with(None, None, None) + + def test__put_file_sftp_mode(self): + sftp = self.fake_client.open_sftp.return_value = mock.MagicMock() + sftp.__enter__.return_value = sftp + + self.test_client._put_file_sftp("localfile", "remotefile", mode=0o753) + + sftp.put.assert_called_once_with("localfile", "remotefile") + sftp.chmod.assert_called_once_with("remotefile", 0o753) + sftp.__exit__.assert_called_once_with(None, None, None) + + def test_put_file_SSHException(self): + exc = ssh.paramiko.SSHException + self.test_client._put_file_sftp = mock.Mock(side_effect=exc()) + self.test_client._put_file_shell = mock.Mock() + + self.test_client.put_file("foo", "bar", 42) + self.test_client._put_file_sftp.assert_called_once_with("foo", "bar", + mode=42) + self.test_client._put_file_shell.assert_called_once_with("foo", "bar", + mode=42) + + def test_put_file_socket_error(self): + exc = socket.error + self.test_client._put_file_sftp = mock.Mock(side_effect=exc()) + self.test_client._put_file_shell = mock.Mock() + + self.test_client.put_file("foo", "bar", 42) + self.test_client._put_file_sftp.assert_called_once_with("foo", "bar", + mode=42) + self.test_client._put_file_shell.assert_called_once_with("foo", "bar", + mode=42) + def main(): unittest.main() diff --git a/yardstick/cmd/commands/env.py b/yardstick/cmd/commands/env.py index d9c0c0a3f..098379ae1 100644 --- a/yardstick/cmd/commands/env.py +++ b/yardstick/cmd/commands/env.py @@ -6,12 +6,34 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## +import logging + from yardstick.common.httpClient import HttpClient +from yardstick.common import constants + +logger = logging.getLogger(__name__) +logger.setLevel(logging.DEBUG) class EnvCommand(object): + ''' + Set of commands to prepare environment + ''' def do_influxdb(self, args): - url = 'http://localhost:5000/yardstick/env/action' + url = constants.YARDSTICK_ENV_ACTION_API data = {'action': 'createInfluxDBContainer'} HttpClient().post(url, data) + logger.debug('Now creating and configing influxdb') + + def do_grafana(self, args): + url = constants.YARDSTICK_ENV_ACTION_API + data = {'action': 'createGrafanaContainer'} + HttpClient().post(url, data) + logger.debug('Now creating and configing grafana') + + def do_prepare(self, args): + url = constants.YARDSTICK_ENV_ACTION_API + data = {'action': 'prepareYardstickEnv'} + HttpClient().post(url, data) + logger.debug('Now preparing environment') diff --git a/yardstick/cmd/commands/task.py b/yardstick/cmd/commands/task.py index 47fb2ee60..9524778ba 100644 --- a/yardstick/cmd/commands/task.py +++ b/yardstick/cmd/commands/task.py @@ -17,12 +17,15 @@ import ipaddress import time import logging import uuid +import errno from itertools import ifilter from yardstick.benchmark.contexts.base import Context from yardstick.benchmark.runners import base as base_runner from yardstick.common.task_template import TaskTemplate from yardstick.common.utils import cliargs +from yardstick.common.utils import source_env +from yardstick.common import constants output_file_default = "/tmp/yardstick.out" test_cases_dir_default = "tests/opnfv/test_cases/" @@ -58,6 +61,8 @@ class TaskCommands(object): self.task_id = kwargs.get('task_id', str(uuid.uuid4())) + check_environment() + total_start_time = time.time() parser = TaskParser(args.inputfile[0]) @@ -483,3 +488,14 @@ def parse_task_args(src_name, args): % {"src": src_name, "src_type": type(kw)}) raise TypeError() return kw + + +def check_environment(): + auth_url = os.environ.get('OS_AUTH_URL', None) + if not auth_url: + try: + source_env(constants.OPENSTACK_RC_FILE) + except IOError as e: + if e.errno != errno.EEXIST: + raise + LOG.debug('OPENRC file not found') diff --git a/yardstick/common/constants.py b/yardstick/common/constants.py index 8fbc82f0e..d541ead15 100644 --- a/yardstick/common/constants.py +++ b/yardstick/common/constants.py @@ -1,6 +1,4 @@ -CONFIG_SAMPLE = '/etc/yardstick/config.yaml' - -RELENG_DIR = 'releng.dir' +import os DOCKER_URL = 'unix://var/run/docker.sock' @@ -8,3 +6,33 @@ DOCKER_URL = 'unix://var/run/docker.sock' USER = 'root' PASSWORD = 'root' DATABASE = 'yardstick' + +INFLUXDB_IMAGE = 'tutum/influxdb' +INFLUXDB_TAG = '0.13' + +GRAFANA_IMAGE = 'grafana/grafana' +GRAFANA_TAGS = '3.1.1' + +dirname = os.path.dirname +abspath = os.path.abspath +sep = os.path.sep + +INSTALLERS = ['apex', 'compass', 'fuel', 'joid'] + +YARDSTICK_ROOT_PATH = dirname(dirname(dirname(abspath(__file__)))) + sep + +YARDSTICK_REPOS_DIR = '/home/opnfv/repos/yardstick' + +YARDSTICK_CONFIG_DIR = '/etc/yardstick/' + +YARDSTICK_CONFIG_FILE = os.path.join(YARDSTICK_CONFIG_DIR, 'config.yaml') + +RELENG_DIR = '/home/opnfv/repos/releng' + +OS_FETCH_SCRIPT = 'utils/fetch_os_creds.sh' + +LOAD_IMAGES_SCRIPT = 'tests/ci/load_images.sh' + +OPENSTACK_RC_FILE = os.path.join(YARDSTICK_CONFIG_DIR, 'openstack.creds') + +YARDSTICK_ENV_ACTION_API = 'http://localhost:5000/yardstick/env/action' diff --git a/yardstick/common/httpClient.py b/yardstick/common/httpClient.py index b6959b400..ab2e9a379 100644 --- a/yardstick/common/httpClient.py +++ b/yardstick/common/httpClient.py @@ -23,5 +23,8 @@ class HttpClient(object): response = requests.post(url, data=data, headers=headers) result = response.json() logger.debug('The result is: %s', result) + + return result except Exception as e: logger.debug('Failed: %s', e) + raise diff --git a/yardstick/common/utils.py b/yardstick/common/utils.py index afbe4e8ec..3ecb0ae20 100644 --- a/yardstick/common/utils.py +++ b/yardstick/common/utils.py @@ -19,10 +19,19 @@ import os import sys import yaml import errno +import subprocess +import logging + from oslo_utils import importutils +from keystoneauth1 import identity +from keystoneauth1 import session +from neutronclient.v2_0 import client import yardstick +logger = logging.getLogger(__name__) +logger.setLevel(logging.DEBUG) + # Decorator for cli-args def cliargs(*args, **kwargs): @@ -100,3 +109,35 @@ def makedirs(d): except OSError as e: if e.errno != errno.EEXIST: raise + + +def execute_command(cmd): + exec_msg = "Executing command: '%s'" % cmd + logger.debug(exec_msg) + + output = subprocess.check_output(cmd.split()).split(os.linesep) + + return output + + +def source_env(env_file): + p = subprocess.Popen(". %s; env" % env_file, stdout=subprocess.PIPE, + shell=True) + output = p.communicate()[0] + env = dict((line.split('=', 1) for line in output.splitlines())) + os.environ.update(env) + return env + + +def get_openstack_session(): + auth = identity.Password(auth_url=os.environ.get('OS_AUTH_URL'), + username=os.environ.get('OS_USERNAME'), + password=os.environ.get('OS_PASSWORD'), + tenant_name=os.environ.get('OS_TENANT_NAME')) + return session.Session(auth=auth) + + +def get_neutron_client(): + sess = get_openstack_session() + neutron_client = client.Client(session=sess) + return neutron_client diff --git a/yardstick/ssh.py b/yardstick/ssh.py index 8485dccd0..46d53b7d2 100644 --- a/yardstick/ssh.py +++ b/yardstick/ssh.py @@ -29,24 +29,29 @@ Execute command and get output: Execute command with huge output: - class PseudoFile(object): + class PseudoFile(io.RawIOBase): def write(chunk): if "error" in chunk: email_admin(chunk) - ssh = sshclient.SSH("root", "example.com") - ssh.run("tail -f /var/log/syslog", stdout=PseudoFile(), timeout=False) + ssh = SSH("root", "example.com") + with PseudoFile() as p: + ssh.run("tail -f /var/log/syslog", stdout=p, timeout=False) Execute local script on remote side: ssh = sshclient.SSH("user", "example.com") - status, out, err = ssh.execute("/bin/sh -s arg1 arg2", - stdin=open("~/myscript.sh", "r")) + + with open("~/myscript.sh", "r") as stdin_file: + status, out, err = ssh.execute('/bin/sh -s "arg1" "arg2"', + stdin=stdin_file) Upload file: - ssh = sshclient.SSH("user", "example.com") - ssh.run("cat > ~/upload/file.gz", stdin=open("/store/file.gz", "rb")) + ssh = SSH("user", "example.com") + # use rb for binary files + with open("/store/file.gz", "rb") as stdin_file: + ssh.run("cat > ~/upload/file.gz", stdin=stdin_file) Eventlet: @@ -54,7 +59,7 @@ Eventlet: or eventlet.monkey_patch() or - sshclient = eventlet.import_patched("opentstack.common.sshclient") + sshclient = eventlet.import_patched("yardstick.ssh") """ import os @@ -290,3 +295,37 @@ class SSH(object): def send_command(self, command): client = self._get_client() client.exec_command(command, get_pty=True) + + def _put_file_sftp(self, localpath, remotepath, mode=None): + client = self._get_client() + + with client.open_sftp() as sftp: + sftp.put(localpath, remotepath) + if mode is None: + mode = 0o777 & os.stat(localpath).st_mode + sftp.chmod(remotepath, mode) + + def _put_file_shell(self, localpath, remotepath, mode=None): + # quote to stop wordpslit + cmd = ['cat > "%s"' % remotepath] + if mode is not None: + # use -- so no options + cmd.append('chmod -- 0%o "%s"' % (mode, remotepath)) + + with open(localpath, "rb") as localfile: + # only chmod on successful cat + cmd = "&& ".join(cmd) + self.run(cmd, stdin=localfile) + + def put_file(self, localpath, remotepath, mode=None): + """Copy specified local file to the server. + + :param localpath: Local filename. + :param remotepath: Remote filename. + :param mode: Permissions to set after upload + """ + import socket + try: + self._put_file_sftp(localpath, remotepath, mode=mode) + except (paramiko.SSHException, socket.error): + self._put_file_shell(localpath, remotepath, mode=mode) |