blob: 9f4356f6c451f00b3186b3e65690d505dabbee0e [file] [log] [blame]
koder aka kdanilova4a570f2015-04-23 22:11:40 +03001import getpass
2
koder aka kdanilov6b1341a2015-04-21 22:44:21 +03003from wally.ssh_utils import parse_ssh_uri
koder aka kdanilov4500a5f2015-04-17 16:55:17 +03004
5
Yulia Portnova21289b22015-03-18 15:21:43 +02006class Node(object):
7
koder aka kdanilov2c473092015-03-29 17:12:13 +03008 def __init__(self, conn_url, roles):
Yulia Portnova21289b22015-03-18 15:21:43 +02009 self.roles = roles
koder aka kdanilov2c473092015-03-29 17:12:13 +030010 self.conn_url = conn_url
koder aka kdanilove06762a2015-03-22 23:32:09 +020011 self.connection = None
koder aka kdanilov168f6092015-04-19 02:33:38 +030012 self.monitor_url = None
koder aka kdanilove06762a2015-03-22 23:32:09 +020013
koder aka kdanilov4500a5f2015-04-17 16:55:17 +030014 def get_ip(self):
koder aka kdanilov0c598a12015-04-21 03:01:40 +030015 if self.conn_url == 'local':
16 return '127.0.0.1'
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030017
18 assert self.conn_url.startswith("ssh://")
19 return parse_ssh_uri(self.conn_url[6:]).host
koder aka kdanilov4500a5f2015-04-17 16:55:17 +030020
koder aka kdanilov168f6092015-04-19 02:33:38 +030021 def get_conn_id(self):
koder aka kdanilov0c598a12015-04-21 03:01:40 +030022 if self.conn_url == 'local':
23 return '127.0.0.1'
24
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030025 assert self.conn_url.startswith("ssh://")
26 creds = parse_ssh_uri(self.conn_url[6:])
27 return "{0.host}:{0.port}".format(creds)
koder aka kdanilov168f6092015-04-19 02:33:38 +030028
koder aka kdanilova4a570f2015-04-23 22:11:40 +030029 def get_user(self):
30 if self.conn_url == 'local':
31 return getpass.getuser()
32
33 assert self.conn_url.startswith("ssh://")
34 creds = parse_ssh_uri(self.conn_url[6:])
35 return creds.user
36
koder aka kdanilove06762a2015-03-22 23:32:09 +020037 def __str__(self):
koder aka kdanilov2c473092015-03-29 17:12:13 +030038 templ = "<Node: url={conn_url!r} roles={roles}" + \
39 " connected={is_connected}>"
40 return templ.format(conn_url=self.conn_url,
41 roles=", ".join(self.roles),
42 is_connected=self.connection is not None)
Yulia Portnova21289b22015-03-18 15:21:43 +020043
Yulia Portnova0e64ea22015-03-20 17:27:22 +020044 def __repr__(self):
koder aka kdanilove06762a2015-03-22 23:32:09 +020045 return str(self)