koder aka kdanilov | a4a570f | 2015-04-23 22:11:40 +0300 | [diff] [blame] | 1 | import getpass |
| 2 | |
koder aka kdanilov | 6b1341a | 2015-04-21 22:44:21 +0300 | [diff] [blame] | 3 | from wally.ssh_utils import parse_ssh_uri |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 4 | |
| 5 | |
Yulia Portnova | 21289b2 | 2015-03-18 15:21:43 +0200 | [diff] [blame] | 6 | class Node(object): |
| 7 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 8 | def __init__(self, conn_url, roles): |
Yulia Portnova | 21289b2 | 2015-03-18 15:21:43 +0200 | [diff] [blame] | 9 | self.roles = roles |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 10 | self.conn_url = conn_url |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 11 | self.connection = None |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 12 | self.monitor_ip = None |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame^] | 13 | self.os_vm_id = None |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 14 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 15 | def get_ip(self): |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 16 | if self.conn_url == 'local': |
| 17 | return '127.0.0.1' |
koder aka kdanilov | 6b1341a | 2015-04-21 22:44:21 +0300 | [diff] [blame] | 18 | |
| 19 | assert self.conn_url.startswith("ssh://") |
| 20 | return parse_ssh_uri(self.conn_url[6:]).host |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 21 | |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 22 | def get_conn_id(self): |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 23 | if self.conn_url == 'local': |
| 24 | return '127.0.0.1' |
| 25 | |
koder aka kdanilov | 6b1341a | 2015-04-21 22:44:21 +0300 | [diff] [blame] | 26 | assert self.conn_url.startswith("ssh://") |
| 27 | creds = parse_ssh_uri(self.conn_url[6:]) |
| 28 | return "{0.host}:{0.port}".format(creds) |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 29 | |
koder aka kdanilov | a4a570f | 2015-04-23 22:11:40 +0300 | [diff] [blame] | 30 | def get_user(self): |
| 31 | if self.conn_url == 'local': |
| 32 | return getpass.getuser() |
| 33 | |
| 34 | assert self.conn_url.startswith("ssh://") |
| 35 | creds = parse_ssh_uri(self.conn_url[6:]) |
| 36 | return creds.user |
| 37 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 38 | def __str__(self): |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 39 | templ = "<Node: url={conn_url!r} roles={roles}" + \ |
| 40 | " connected={is_connected}>" |
| 41 | return templ.format(conn_url=self.conn_url, |
| 42 | roles=", ".join(self.roles), |
| 43 | is_connected=self.connection is not None) |
Yulia Portnova | 21289b2 | 2015-03-18 15:21:43 +0200 | [diff] [blame] | 44 | |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 45 | def __repr__(self): |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 46 | return str(self) |