1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
22
from nova import exception as nova_exception
23
from nova.openstack.common import log as logging
24
from nova.virt.powervm import exception
26
LOG = logging.getLogger(__name__)
29
class Connection(object):
31
def __init__(self, host, username, password, port=22):
33
self.username = username
34
self.password = password
38
def ssh_connect(connection):
39
"""Method to connect to remote system using ssh protocol.
41
:param connection: a Connection object.
42
:returns: paramiko.SSHClient -- an active ssh connection.
43
:raises: PowerVMConnectionFailed
46
ssh = paramiko.SSHClient()
47
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
48
ssh.connect(connection.host,
49
username=connection.username,
50
password=connection.password,
54
LOG.exception(_('Connection error connecting PowerVM manager'))
55
raise exception.PowerVMConnectionFailed()
58
def ssh_command_as_root(ssh_connection, cmd, check_exit_code=True):
59
"""Method to execute remote command as root.
61
:param connection: an active paramiko.SSHClient connection.
62
:param command: string containing the command to run.
63
:returns: Tuple -- a tuple of (stdout, stderr)
64
:raises: nova.exception.ProcessExecutionError
66
chan = ssh_connection._transport.open_session()
67
# This command is required to be executed
68
# in order to become root.
69
chan.exec_command('ioscli oem_setup_env')
71
stdin = chan.makefile('wb', bufsize)
72
stdout = chan.makefile('rb', bufsize)
73
stderr = chan.makefile_stderr('rb', bufsize)
74
# We run the command and then call 'exit' to exit from
75
# super user environment.
76
stdin.write('%s\n%s\n' % (cmd, 'exit'))
78
exit_status = chan.recv_exit_status()
80
# Lets handle the error just like nova.utils.ssh_execute does.
82
LOG.debug(_('Result was %s') % exit_status)
83
if check_exit_code and exit_status != 0:
84
raise nova_exception.ProcessExecutionError(exit_code=exit_status,
89
return (stdout, stderr)
92
def ftp_put_command(connection, local_path, remote_dir):
93
"""Method to transfer a file via ftp.
95
:param connection: a Connection object.
96
:param local_path: path to the local file
97
:param remote_dir: path to remote destination
98
:raises: PowerVMFileTransferFailed
101
ftp = ftplib.FTP(host=connection.host,
102
user=connection.username,
103
passwd=connection.password)
105
name = os.path.split(local_path)[1]
106
f = open(local_path, "rb")
107
ftp.storbinary("STOR " + name, f)
111
LOG.exception(_('File transfer to PowerVM manager failed'))
112
raise exception.PowerVMFileTransferFailed(file_path=local_path)