class Installer(object): """ This class is responsable for sending the install commands to the child server. It uses the SSH class to send them. """ def __init__(self, hostip, user, password, port=None, use_log=None): """ Constructs the Installer. hostip -- the ip address of the host. user -- the username of the user (Usually it will be host). password -- the password. port -- the port to connect to. (Default is None) use_log -- True for logging the installation. (Default is None) """ # self.ip = hostip # self.user = user # self.password = password if (use_log is None): self.ssh = SSH(hostip, user, password, port) else: self.ssh = SSH(hostip, user, password, port, use_log) def install(self, otherBranch = None): """ Makes the class install itself through SSH. It also starts running the class. """ print('Start installing.') (_, out0, err0) = self.ssh.run('apt-get update') self.ssh._checkStreams(out0, err0, 'apt update failed', 'apt updated.') (_, out0, err0) = self.ssh.run('apt-get install -y --force-yes git') self.ssh._checkStreams(out0, err0, 'git install failed', 'git installed.') command = 'git clone --recursive https://github.com/Skynet2-0/Skynet2.0.git' (_, out0, err0) = self.ssh.run(command) self.ssh._checkStreams(out0, err0, 'git clone failed', 'project cloned.') if isinstance(otherBranch, basestring): command = 'cd ~/Skynet2.0 && git checkout '+otherBranch (_, out0, err0) = self.ssh.run(command) self.ssh._checkStreams(out0, err0, 'git checkout failed', 'git checkout succeeeded.') command = """cd ~/Skynet2.0 && sh build.sh >> build.out""" print("starting the actual installing of programs on the child, this can take up to 15 minutes.") (_, out0, err0) = self.ssh.run(command) self.ssh._checkStreams_until_done(out0, err0, 'Preqrequisite installation failed', 'Preqrequisite installation succesfull.') print('Installation finished.') def finish(self): "Does some clean up." self.ssh.close_connection()
def test_SSH(self, popenMock): params = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", "1.2.1", "8440") host_log_mock = MagicMock() log = {'text': ""} def write_side_effect(text): log['text'] = log['text'] + text host_log_mock.write.side_effect = write_side_effect ssh = SSH(params.user, params.sshkey_file, "dummy-host", "dummy-command", params.bootdir, host_log_mock) log_sample = "log_sample" error_sample = "error_sample" # Successful run process = MagicMock() popenMock.return_value = process process.communicate.return_value = (log_sample, error_sample) process.returncode = 0 retcode = ssh.run() self.assertTrue(popenMock.called) self.assertTrue(log_sample in log['text']) self.assertTrue(error_sample in log['text']) command_str = str(popenMock.call_args[0][0]) self.assertEquals(command_str, "['ssh', '-o', 'ConnectTimeOut=60', '-o', " "'StrictHostKeyChecking=no', '-o', 'BatchMode=yes', '-tt', '-i', " "'sshkey_file', 'root@dummy-host', 'dummy-command']") self.assertEqual(retcode, 0) log['text'] = "" #unsuccessfull run process.returncode = 1 retcode = ssh.run() self.assertTrue(log_sample in log['text']) self.assertTrue(error_sample in log['text']) self.assertEqual(retcode, 1) log['text'] = "" # unsuccessful run with error message process.returncode = 1 dummy_error_message = "dummy_error_message" ssh = SSH(params.user, params.sshkey_file, "dummy-host", "dummy-command", params.bootdir, host_log_mock, errorMessage= dummy_error_message) retcode = ssh.run() self.assertTrue(log_sample in log['text']) self.assertTrue(error_sample in log['text']) self.assertTrue(dummy_error_message in log['text']) self.assertEqual(retcode, 1)
def copyNeededFiles(self): # Copying the files fileToCopy = self.getRepoFile() target = self.getRemoteName(self.AMBARI_REPO_FILENAME) self.host_log.write("Copying repo file to 'tmp' folder...") params = self.shared_state scp = SCP(params.user, params.sshkey_file, self.host, fileToCopy, target, params.bootdir, self.host_log) retcode1 = scp.run() # Move file to repo dir self.host_log.write("Moving file to repo dir...") targetDir = self.getRepoDir() command = self.getMoveRepoFileCommand(targetDir) ssh = SSH(params.user, params.sshkey_file, self.host, command, params.bootdir, self.host_log) retcode2 = ssh.run() self.host_log.write("Copying setup script file...") fileToCopy = params.action_agent_file target = self.getRemoteName(self.ACTION_SCRIPT_FILENAME) scp = SCP(params.user, params.sshkey_file, self.host, fileToCopy, target, params.bootdir, self.host_log) retcode3 = scp.run() self.host_log.write("Copying files finished") return max(retcode1, retcode2, retcode3)
class FileCreator(object): """ This class is responsable for creating files on the child server. It uses the SSH class to send them. """ def __init__(self, hostip, user, password, port=None, use_log=None): """ Constructs the FileCreator which starts the program. hostip -- the ip address of the host. user -- the username of the user (Usually it will be host). password -- the password. port -- the port to connect to. (Default is None) use_log -- True for logging the starting. (Default is None) """ use_log = True if (use_log is None): self.ssh = SSH(hostip, user, password, port) else: self.ssh = SSH(hostip, user, password, port, use_log) def create(self, path, text): """ creates a file at path with contents text. example: createFile("~/file.txt", "test") will create a file named file.txt with contents "test" """ (_, out0, err0) = self.ssh.run('echo \"'+str(text)+'\" > '+str(path))
def copyNeededFiles(self): # Copying the files fileToCopy = self.getRepoFile() target = self.getRemoteName(self.AMBARI_REPO_FILENAME) self.host_log.write("Copying repo file to 'tmp' folder...") params = self.shared_state scp = SCP(params.user, params.sshkey_file, self.host, fileToCopy, target, params.bootdir, self.host_log) retcode1 = scp.run() # Move file to repo dir self.host_log.write("Moving file to repo dir...") targetDir = self.getRepoDir() command = self.getMoveRepoFileCommand(targetDir) ssh = SSH(params.user, params.sshkey_file, self.host, command, params.bootdir, self.host_log) retcode2 = ssh.run() self.host_log.write("Copying setup script file...") fileToCopy = params.action_agent_file target = self.getRemoteName(self.ACTION_SCRIPT_FILENAME) scp = SCP(params.user, params.sshkey_file, self.host, fileToCopy, target, params.bootdir, self.host_log) retcode3 = scp.run() self.host_log.write("Copying files finished") return max(retcode1, retcode2, retcode3)
def runActionAgent(self): params = self.shared_state self.host_log.write("Running agent action...") command = self.getRunActionCommand(self.host) ssh = SSH(params.user, params.sshkey_file, self.host, command, params.bootdir, self.host_log) retcode = ssh.run() self.host_log.write("Setting up agent finished") return retcode
def runActionAgent(self): params = self.shared_state self.host_log.write("Running agent action...") command = self.getRunActionCommand(self.host) ssh = SSH(params.user, params.sshkey_file, self.host, command, params.bootdir, self.host_log) retcode = ssh.run() self.host_log.write("Setting up agent finished") return retcode
def deletePasswordFile(self): # Deleting the password file self.host_log.write("Deleting password file...") params = self.shared_state command = "rm " + self.getPasswordFile() ssh = SSH(params.user, params.sshkey_file, self.host, command, params.bootdir, self.host_log) retcode = ssh.run() self.host_log.write("Deleting password file finished") return retcode
def deletePasswordFile(self): # Deleting the password file self.host_log.write("Deleting password file...") params = self.shared_state command = "rm " + self.getPasswordFile() ssh = SSH(params.user, params.sshkey_file, self.host, command, params.bootdir, self.host_log) retcode = ssh.run() self.host_log.write("Deleting password file finished") return retcode
def changePasswordFileModeOnHost(self): # Change password file mode to 600 self.host_log.write("Changing password file mode...") params = self.shared_state command = "chmod 600 " + self.getPasswordFile() ssh = SSH(params.user, params.sshkey_file, self.host, command, params.bootdir, self.host_log) retcode = ssh.run() self.host_log.write("Change password file mode on host finished") return retcode
def changePasswordFileModeOnHost(self): # Change password file mode to 600 self.host_log.write("Changing password file mode...") params = self.shared_state command = "chmod 600 " + self.getPasswordFile() ssh = SSH(params.user, params.sshkey_file, self.host, command, params.bootdir, self.host_log) retcode = ssh.run() self.host_log.write("Change password file mode on host finished") return retcode
def checkSudoPackage(self): """ Checking 'sudo' package on remote host """ params = self.shared_state command = "rpm -qa | grep sudo" ssh = SSH(params.user, params.sshkey_file, self.host, command, params.bootdir, self.host_log, errorMessage="Error: Sudo command is not available. " \ "Please install the sudo command.") retcode = ssh.run() self.host_log.write("Checking 'sudo' package finished") return retcode
def checkSudoPackage(self): """ Checking 'sudo' package on remote host """ params = self.shared_state command = "rpm -qa | grep sudo" ssh = SSH(params.user, params.sshkey_file, self.host, command, params.bootdir, self.host_log, errorMessage="Error: Sudo command is not available. " \ "Please install the sudo command.") retcode = ssh.run() self.host_log.write("Checking 'sudo' package finished") return retcode
def runOsCheckScript(self): params = self.shared_state self.host_log.write("Running os type check...") command = "chmod a+x %s && %s %s" % \ (self.getOsCheckScriptRemoteLocation(), self.getOsCheckScriptRemoteLocation(), params.cluster_os_type) ssh = SSH(params.user, params.sshkey_file, self.host, command, params.bootdir, self.host_log) retcode = ssh.run() self.host_log.write("Running os type check finished") return retcode
def runOsCheckScript(self): params = self.shared_state self.host_log.write("Running os type check...") command = "chmod a+x %s && %s %s" % \ (self.getOsCheckScriptRemoteLocation(), self.getOsCheckScriptRemoteLocation(), params.cluster_os_type) ssh = SSH(params.user, params.sshkey_file, self.host, command, params.bootdir, self.host_log) retcode = ssh.run() self.host_log.write("Running os type check finished") return retcode
def copyPasswordFile(self): # Copy the password file self.host_log.write("Copying password file to 'tmp' folder...") params = self.shared_state scp = SCP(params.user, params.sshkey_file, self.host, params.password_file, self.getPasswordFile(), params.bootdir, self.host_log) retcode1 = scp.run() self.copied_password_file = True # Change password file mode to 600 self.host_log.write("Changing password file mode...") command = "chmod 600 " + self.getPasswordFile() ssh = SSH(params.user, params.sshkey_file, self.host, command, params.bootdir, self.host_log) retcode2 = ssh.run() self.host_log.write("Copying password file finished") return max(retcode1, retcode2)
def copyPasswordFile(self): # Copy the password file self.host_log.write("Copying password file to 'tmp' folder...") params = self.shared_state scp = SCP(params.user, params.sshkey_file, self.host, params.password_file, self.getPasswordFile(), params.bootdir, self.host_log) retcode1 = scp.run() self.copied_password_file = True # Change password file mode to 600 self.host_log.write("Changing password file mode...") command = "chmod 600 " + self.getPasswordFile() ssh = SSH(params.user, params.sshkey_file, self.host, command, params.bootdir, self.host_log) retcode2 = ssh.run() self.host_log.write("Copying password file finished") return max(retcode1, retcode2)
class Starter(object): """ This class is responsable for sending the startup commands to the child server. It uses the SSH class to send them. """ def __init__(self, hostip, user, password, port=None, use_log=None): """ Constructs the Starter which starts the program. hostip -- the ip address of the host. user -- the username of the user (Usually it will be host). password -- the password. port -- the port to connect to. (Default is None) use_log -- True for logging the starting. (Default is None) """ if (use_log is None): self.ssh = SSH(hostip, user, password, port) else: self.ssh = SSH(hostip, user, password, port, use_log) def start(self, otherCore = None): """ Starts the program. """ self.start_other_requirements() if otherCore: self.start_agent("agent/"+otherCore+" -x True -t False") else: self.start_agent("agent/agentCore.py -x True -t False") def start_other_requirements(self): self.ssh.run('''(Xvfb :99 -ac &> /dev/null &)''') self.ssh.run('''(cd ~/Skynet2.0 && export DISPLAY=:99 && java -jar selenium-server-standalone-2.53.0.jar &>log.out &)''') def start_agent(self, relAgentPath): """ given the path to the agent program starting from ~/Skynet2.0 runs this agent """ self.ssh.run('(cd ~/Skynet2.0 && PYTHONPATH=${PYTHONPATH}:. python ' + relAgentPath + ' &>>agentCore.out &)')
def test_SSH(self, popenMock): params = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", "1.2.1", "8440") host_log_mock = MagicMock() log = {'text': ""} def write_side_effect(text): log['text'] = log['text'] + text host_log_mock.write.side_effect = write_side_effect ssh = SSH(params.user, params.sshkey_file, "dummy-host", "dummy-command", params.bootdir, host_log_mock) log_sample = "log_sample" error_sample = "error_sample" # Successful run process = MagicMock() popenMock.return_value = process process.communicate.return_value = (log_sample, error_sample) process.returncode = 0 retcode = ssh.run() self.assertTrue(popenMock.called) self.assertTrue(log_sample in log['text']) self.assertTrue(error_sample in log['text']) command_str = str(popenMock.call_args[0][0]) self.assertEquals( command_str, "['ssh', '-o', 'ConnectTimeOut=60', '-o', " "'StrictHostKeyChecking=no', '-o', 'BatchMode=yes', '-tt', '-i', " "'sshkey_file', 'root@dummy-host', 'dummy-command']") self.assertEqual(retcode, 0) log['text'] = "" #unsuccessfull run process.returncode = 1 retcode = ssh.run() self.assertTrue(log_sample in log['text']) self.assertTrue(error_sample in log['text']) self.assertEqual(retcode, 1) log['text'] = "" # unsuccessful run with error message process.returncode = 1 dummy_error_message = "dummy_error_message" ssh = SSH(params.user, params.sshkey_file, "dummy-host", "dummy-command", params.bootdir, host_log_mock, errorMessage=dummy_error_message) retcode = ssh.run() self.assertTrue(log_sample in log['text']) self.assertTrue(error_sample in log['text']) self.assertTrue(dummy_error_message in log['text']) self.assertEqual(retcode, 1)