From 416f9cf177801cecfe39f5249d73e3a4a85f0bbd Mon Sep 17 00:00:00 2001 From: Niko Hermanns Date: Wed, 30 Nov 2016 11:59:44 +0100 Subject: adding odl-pipeline Change-Id: I1c08883f0d68a61ce9e10c5596aec1a259eed71f Signed-off-by: Nikolas Hermanns --- odl-pipeline/lib/utils/node.py | 87 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100755 odl-pipeline/lib/utils/node.py (limited to 'odl-pipeline/lib/utils/node.py') diff --git a/odl-pipeline/lib/utils/node.py b/odl-pipeline/lib/utils/node.py new file mode 100755 index 0000000..c3c2005 --- /dev/null +++ b/odl-pipeline/lib/utils/node.py @@ -0,0 +1,87 @@ +# +# Copyright (c) 2015 All rights reserved +# This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# +from ssh_client import SSHClient +from ssh_util import SshUtil +from utils_log import log_enter_exit, for_all_methods + + +@for_all_methods(log_enter_exit) +class Node(object): + + def __init__(self, name, address=None, port=None, + user=None, password=None, jump=None, dict=None): + self.name = name + self.address = address + self.jump = jump + self.user = user + self.port = port + self.password = password + if dict: + self.read_from_dic(dict) + self.sshc = SSHClient(self) + self.has_access = False + self.config = dict + + def read_from_dic(self, dic): + allowed_keys = ['address', 'user', 'jump', 'password', 'port'] + for (key, value) in dic.iteritems(): + if key in allowed_keys: + setattr(self, key, value) + + def ping(self, ip): + self.execute(['ping', '-c', '1', ip]) + + def execute(self, cmd, **kwargs): + return self.sshc.execute(cmd, **kwargs) + + def chown(self, user, path): + self.execute('chown -R %(user)s:%(user)s %(path)s' % {'user': user, + 'path': path}, + as_root=True) + + def is_dir(self, path): + rv, _ = self.execute('test -d %s && echo yes' % path, + check_exit_code=[0, 1]) + if rv == 'yes\n': + return True + else: + return False + + def is_file(self, path): + rv, _ = self.execute('test -f %s && echo yes' % path, + check_exit_code=[0, 1]) + if rv == 'yes\n': + return True + else: + return False + + def reboot(self): + self.execute('reboot', as_root=True, check_exit_code=[255]) + + def create_path_if_not_exsist(self, path, **kwargs): + return self.sshc.execute('mkdir -p %s' % path, **kwargs) + + def copy(self, direction, local_path, remote_path, **kwargs): + return self.sshc.copy(direction, local_path, remote_path, **kwargs) + + def to_ssh_config(self): + config = ["Host %s" % self.name, + " Hostname %s" % + (self.address if self.address else self.name)] + if self.jump: + config.append(" ProxyCommand ssh -F %(config_path)s " + "-W %%h:%%p %(name)s" + % {'config_path': SshUtil.get_config_file_path(), + 'name': self.jump.name}) + if self.user: + config.append(" user %s" % self.user) + if self.port: + config.append(" port %s" % self.port) + return '\n'.join(config) -- cgit 1.2.3-korg