~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/virt/powervm/common.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-08-16 14:04:11 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20120816140411-0mr4n241wmk30t9l
Tags: upstream-2012.2~f3
ImportĀ upstreamĀ versionĀ 2012.2~f3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2012 IBM
 
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
 
8
#
 
9
#         http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
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
 
15
#    under the License.
 
16
 
 
17
import ftplib
 
18
import os
 
19
 
 
20
import paramiko
 
21
 
 
22
from nova import exception as nova_exception
 
23
from nova.openstack.common import log as logging
 
24
from nova.virt.powervm import exception
 
25
 
 
26
LOG = logging.getLogger(__name__)
 
27
 
 
28
 
 
29
class Connection(object):
 
30
 
 
31
    def __init__(self, host, username, password, port=22):
 
32
        self.host = host
 
33
        self.username = username
 
34
        self.password = password
 
35
        self.port = port
 
36
 
 
37
 
 
38
def ssh_connect(connection):
 
39
    """Method to connect to remote system using ssh protocol.
 
40
 
 
41
    :param connection: a Connection object.
 
42
    :returns: paramiko.SSHClient -- an active ssh connection.
 
43
    :raises: PowerVMConnectionFailed
 
44
    """
 
45
    try:
 
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,
 
51
                    port=connection.port)
 
52
        return ssh
 
53
    except Exception:
 
54
        LOG.exception(_('Connection error connecting PowerVM manager'))
 
55
        raise exception.PowerVMConnectionFailed()
 
56
 
 
57
 
 
58
def ssh_command_as_root(ssh_connection, cmd, check_exit_code=True):
 
59
    """Method to execute remote command as root.
 
60
 
 
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
 
65
    """
 
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')
 
70
    bufsize = -1
 
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'))
 
77
    stdin.flush()
 
78
    exit_status = chan.recv_exit_status()
 
79
 
 
80
    # Lets handle the error just like nova.utils.ssh_execute does.
 
81
    if exit_status != -1:
 
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,
 
85
                                                       stdout=stdout,
 
86
                                                       stderr=stderr,
 
87
                                                       cmd=' '.join(cmd))
 
88
 
 
89
    return (stdout, stderr)
 
90
 
 
91
 
 
92
def ftp_put_command(connection, local_path, remote_dir):
 
93
    """Method to transfer a file via ftp.
 
94
 
 
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
 
99
    """
 
100
    try:
 
101
        ftp = ftplib.FTP(host=connection.host,
 
102
                         user=connection.username,
 
103
                         passwd=connection.password)
 
104
        ftp.cwd(remote_dir)
 
105
        name = os.path.split(local_path)[1]
 
106
        f = open(local_path, "rb")
 
107
        ftp.storbinary("STOR " + name, f)
 
108
        f.close()
 
109
        ftp.close()
 
110
    except Exception:
 
111
        LOG.exception(_('File transfer to PowerVM manager failed'))
 
112
        raise exception.PowerVMFileTransferFailed(file_path=local_path)