Migrate Get Requests invocations(libraries)
[integration/test.git] / 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 %s on host %s" % (command, self.host))
27         rc = self.lib.execute_command(command, return_rc=True)
28         if rc[1] != 0:
29             raise Exception(
30                 "remote command failed [{0}] with exit code {1}."
31                 "For linux-based vms, Please make sure requiretty is disabled in the /etc/sudoers file".format(
32                     command, rc
33                 )
34             )
35
36     def mkdir(self, dir_name):
37         self.exec_cmd("mkdir -p " + dir_name)
38
39     def copy_file(self, src, dest):
40         if src is None:
41             print("src is None not copy anything to ", dest)
42             return
43
44         if os.path.exists(src) is False:
45             print("Src file " + src + " was not found")
46             return
47
48         print("Copying %s to %s on %s" % (src, dest, self.host))
49         self.lib.put_file(src, dest)
50
51     def kill_controller(self):
52         self.exec_cmd(
53             "sudo ps axf | grep karaf | grep -v grep "
54             "| awk '{print \"kill -9 \" $1}' | sudo sh"
55         )
56
57     def start_controller(self, dir_name):
58         self.exec_cmd(dir_name + "/odl/bin/start")