############################################################################## # Copyright (c) 2017 ZTE Corporation and others. # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Apache License, Version 2.0 # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## import os import paramiko import scp import tempfile import time import yaml from utils import ( WORKSPACE, LD, LI, LW, err_exit, log_bar, path_join, update_config ) TIMEOUT = 300 BLOCK_SIZE = 1024 def log_from_stream(res, data, log_func): lines = data.splitlines() res_data = res if res_data: lines[0] = res_data + lines[0] res_data = None if not data.endswith("\n"): res_data = lines[-1] del (lines[-1]) for string in lines: log_func(string) if res_data and len(res_data) >= BLOCK_SIZE: log_func(res_data) res_data = None return res_data LEN_OF_NAME_PART = 50 LEN_OF_SIZE_PART = 15 def log_scp(filename, size, send): if size != send: return unit = " B" if size > 1024: size /= 1024 unit = " KB" if size > 1024: size /= 1024 unit = " MB" name_part = 'SCP: ' + filename + ' ' size_part = ' ' + str(size) + unit + ' 100%' if len(name_part) <= LEN_OF_NAME_PART: LD(name_part.ljust(LEN_OF_NAME_PART, '.') + size_part.rjust(LEN_OF_SIZE_PART, '.')) else: LD(name_part) LD(" ".ljust(LEN_OF_NAME_PART, '.') + size_part.rjust(LEN_OF_SIZE_PART, '.')) class DaisyServer(object): def __init__(self, name, address, password, remote_dir, bin_file, adapter, scenario, deploy_file_name, net_file_name): self.name = name self.address = address self.password = password self.remote_dir = remote_dir self.bin_file = bin_file self.adapter = adapter self.ssh_client = None self.scenario = scenario self.deploy_file_name = deploy_file_name self.net_file_name = net_file_name def connect(self): LI('Try to connect to Daisy Server ...') self.ssh_client = paramiko.SSHClient() self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) count = 0 MAX_COUNT = 120 while count < MAX_COUNT: try: self.ssh_client.connect(hostname=self.address, username='root', password=self.password, timeout=TIMEOUT) except (paramiko.ssh_exception.SSHException, paramiko.ssh_exception.NoValidConnectionsError): count += 1 LD('Attempted SSH connection %d time(s)' % count) time.sleep(2) else: break if count >= MAX_COUNT: err_exit('SSH connect to Daisy Server failed') LI('SSH connection established') LI('Try ssh_run: ls -al') self.ssh_run('ls -al', check=True) def close(self): self.ssh_client.close() def ssh_exec_cmd(self, cmd): stdin, stdout, stderr = self.ssh_client.exec_command(cmd, timeout=TIMEOUT) response = stdout.read().strip() error = stderr.read().strip() if error: self.close() err_exit('SSH client error occurred') else: return response def ssh_run(self, cmd, check=False, exit_msg='Ssh_run failed'): transport = self.ssh_client.get_transport() transport.set_keepalive(1) session = transport.open_session() res_data = None session.exec_command(cmd) while True: if session.recv_ready(): data = session.recv(BLOCK_SIZE) while data: res_data = log_from_stream(res_data, data, LI) data = session.recv(BLOCK_SIZE) if res_data: LI(res_data) res_data = None if session.recv_stderr_ready(): data = session.recv_stderr(BLOCK_
# Copyright 2015 Intel Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""VNF interface and helpers.
"""

from vnfs