Use new connection script to be the connection adapter to run onos
[functest.git] / testcases / Controllers / ONOS / Teston / CI / adapters / connection.py
1 """
2 Description:
3     This file is used to make connections
4     Include ssh & exchange public-key to each other so that
5     it can run without password
6
7     lanqinglong@huawei.com
8 """
9 import os
10 import time
11 import pexpect
12 import re
13 import sys
14 from foundation import foundation
15
16 class connection:
17
18     def __init__( self ):
19         self.loginfo = foundation()
20
21     def AddKnownHost( self, ipaddr, username, password ):
22         """
23         Add an user to known host,so that onos can login in with onos $ipaddr.
24         parameters:
25         ipaddr:   ip address
26         username: login user name
27         password: login password
28         """
29         print( "Now Adding an user to known hosts " + ipaddr )
30         login = pexpect.spawn( "ssh -l %s -p 8101 %s"%( username, ipaddr ) )
31         index = 0
32         while index != 2:
33             index = login.expect( ['assword:', 'yes/no', pexpect.EOF, \
34                                    pexpect.TIMEOUT] )
35             if index == 0:
36                 login.sendline( password )
37                 login.sendline( "logout" )
38                 index = login.expect( ["closed", pexpect.EOF] )
39                 if index == 0:
40                     self.loginfo.log( "Add SSH Known Host Success!" )
41                 else:
42                     self.loginfo.log( "Add SSH Known Host Failed! Please Check!" )
43                 #login.interact()
44
45             if index == 1:
46                 login.sendline('yes')
47
48     def Gensshkey( self ):
49         """
50         Generate ssh keys, used for some server have no sshkey.
51         """
52         print "Now Generating SSH keys..."
53         #Here file name may be id_rsa or id_ecdsa or others
54         #So here will have a judgement
55         filelist = os.listdir( '~/.ssh' )
56         for item in filelist:
57             if 'id' in item:
58                 self.loginfo.log("SSH keys are exsit in ssh directory.")
59                 return True
60         keysub = pexpect.spawn("ssh-keygen -t rsa")
61         Result = 0
62         while Result != 2:
63             Result = keysub.expect( ["Overwrite", "Enter", pexpect.EOF, \
64                                      pexpect.TIMEOUT])
65             if Result == 0:
66                 keysub.sendline("y")
67             if Result == 1:
68                 keysub.sendline("\n")
69             if Result == 3:
70                 self.loginfo.log("Generate SSH key failed.")
71
72         self.loginfo.log( "Generate SSH key success." )
73
74     def GetRootAuth( self, password ):
75         """
76         Get root user
77         parameters:
78         password: root login password
79         """
80         print( "Now changing to user root" )
81         login = pexpect.spawn( "su - root" )
82         index = 0
83         while index != 2:
84             index = login.expect( ['assword:', "failure", \
85                                    pexpect.EOF, pexpect.TIMEOUT] )
86             if index == 0:
87                 login.sendline( password )
88             if index == 1:
89                 self.loginfo.log("Change user to root failed.")
90
91         login.interact()
92
93     def ReleaseRootAuth( self ):
94         """
95         Exit root user.
96         """
97         print( "Now Release user root" )
98         login = pexpect.spawn( "exit" )
99         index = login.expect( ['logout', \
100                                 pexpect.EOF, pexpect.TIMEOUT] )
101         if index == 0:
102             self.loginfo.log("Release root user success.")
103         if index == 1:
104             self.loginfo.log("Release root user failed.")
105
106         login.interact()
107
108     def AddEnvIntoBashrc( self, envalue ):
109         """
110         Add Env var into /etc/profile.
111         parameters:
112         envalue: environment value to add
113         """
114         print "Now Adding bash environment"
115         fileopen = open( "/etc/profile", 'r' )
116         findContext = 1
117         while findContext:
118             findContext = fileopen.readline( )
119             result = findContext.find( envalue )
120             if result != -1:
121                 break
122         fileopen.close
123         if result == -1:
124             envAdd = open( "/etc/profile", 'a+' )
125             envAdd.writelines( "\n" + envalue )
126             envAdd.close( )
127         self.loginfo.log( "Add env to bashrc success!" )
128
129     def OnosConnectionSet (self):
130         """
131         Intergrate for ONOS connection setup
132         """
133         self.Gensshkey()
134         self.AddKnownHost( self.OC1, "karaf", "karaf" )
135         self.AddKnownHost( self.OC2, "karaf", "karaf" )
136         self.AddKnownHost( self.OC3, "karaf", "karaf" )
137         currentpath = os.getcwd()
138         filepath = os.path.join( currentpath, "onos/tools/dev/bash_profile" )
139         self.AddEnvIntoBashrc("source " + filepath + "\n")
140         self.AddEnvIntoBashrc("export OCT=" + self.OCT)
141         self.AddEnvIntoBashrc("export OC1=" + self.OC1)
142         self.AddEnvIntoBashrc("export OC2=" + self.OC2)
143         self.AddEnvIntoBashrc("export OC3=" + self.OC3)
144         self.AddEnvIntoBashrc("export OCN=" + self.OCN)
145         self.AddEnvIntoBashrc("export OCN2=" + self.OCN2)
146         self.AddEnvIntoBashrc("export localhost=" + self.localhost)