remote_host.py: re-use ssh session across commands
[integration/test.git] / test / tools / clustering / cluster-deployer / remote_host.py
1 # remote_host.py
2 #
3 #
4 # The RemoteHost class provides methods to do operations on a remote host
5 #
6
7 from SSHLibrary import SSHLibrary
8
9 import os
10
11
12 class RemoteHost:
13     def __init__(self, host, user, password, rootdir):
14         self.host = host
15         self.user = user
16         self.password = password
17         self.rootdir = rootdir
18         self.lib = SSHLibrary()
19         self.lib.open_connection(self.host)
20         self.lib.login(username=self.user, password=self.password)
21
22     def __del__(self):
23         self.lib.close_connection()
24
25     def exec_cmd(self, command):
26         print "Executing command " + command + " on host " + self.host
27         rc = self.lib.execute_command(command, return_rc=True)
28         if rc[1] != 0:
29             raise Exception('remote command failed [{0}] with exit code {1}'
30                             .format(command, rc))
31
32     def mkdir(self, dir_name):
33         self.exec_cmd("mkdir -p " + dir_name)
34
35     def copy_file(self, src, dest):
36         if src is None:
37             print "src is None not copy anything to " + dest
38             return
39
40         if os.path.exists(src) is False:
41             print "Src file " + src + " was not found"
42             return
43
44         print "Copying " + src + " to " + dest + " on " + self.host
45         self.lib.put_file(src, dest)
46
47     def kill_controller(self):
48         self.exec_cmd("sudo ps axf | grep karaf | grep -v grep "
49                       "| awk '{print \"kill -9 \" $1}' | sudo sh")
50
51     def start_controller(self, dir_name):
52         self.exec_cmd(dir_name + "/odl/bin/start")