summaryrefslogtreecommitdiffstats
path: root/SharedModules/Utils/managerutils.py
blob: ca38b3f545a83c830f2205a8bd443f0c3ec746b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""managerutils module. 

Supported Wrappers:-
---------------
*) ssh_connect
*) ssh_connect_allhosts
"""

import re
import ssh
from atfglobals import GlobalObj

def ssh_connect(hostkey):
    """
    """
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    host_connection = cm.getConnection(hostkey)
    if not host_connection:
        host_obj = env.getHost(hostkey)
        if not host_obj:
            print "Invalid Host. %s is not defined in TestEnvironment" % hostkey
            return 1
        else:
            host_connection = ssh.SshConnection()
            if host_connection.connect(host_obj.hostname, host_obj.user,
                                       host_obj.password):
                return 1
            else:
                if re.match("server", hostkey, re.IGNORECASE):
                    cm.addServer(hostkey, host_connection)
                else:
                    cm.addClient(hostkey, host_connection)
                return 0
    else:
        print "Connection to %s already exist" % hostkey

    return 0

def ssh_connect_allhosts():
    """
    """
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    hosts_keys = env.getHostsKeys()
    for hostkey in hosts_keys:
        return_status = ssh_connect(hostkey)
        if return_status:
            return return_status    
    
    return 0

__all__ = ['ssh_connect',
           'ssh_connect_allhosts']