diff options
author | Niko Hermanns <nikolas.hermanns@ericsson.com> | 2016-11-30 11:59:44 +0100 |
---|---|---|
committer | Nikolas Hermanns <nikolas.hermanns@ericsson.com> | 2016-12-13 11:26:00 +0100 |
commit | 416f9cf177801cecfe39f5249d73e3a4a85f0bbd (patch) | |
tree | bea48ec45edc10489aa76d348b0dbfbb1cf4d7ab /odl-pipeline/lib/utils/node.py | |
parent | b9eb7024b014cba0d299b1cf3b01e179c7d0482e (diff) |
adding odl-pipeline
Change-Id: I1c08883f0d68a61ce9e10c5596aec1a259eed71f
Signed-off-by: Nikolas Hermanns <nikolas.hermanns@ericsson.com>
Diffstat (limited to 'odl-pipeline/lib/utils/node.py')
-rwxr-xr-x | odl-pipeline/lib/utils/node.py | 87 |
1 files changed, 87 insertions, 0 deletions
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) |