Commit ee6f3995 by Bach Dániel

dashboard: fix get_connect_uri()

parent 3eb0db45
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
</div> </div>
<h3>{% trans "Connection" %}</h3> <h3>{% trans "Connection" %}</h3>
<!-- TODO RDP --> <!-- TODO RDP -->
<input type="text" value="ssh cloud@vm.ik.bme.hu -p22312" class="form-control" readonly /> <input type="text" value="{{ instance.get_connect_command }}" class="form-control" readonly />
<dl class="dl-horizontal vm-details-pw"> <dl class="dl-horizontal vm-details-pw">
<dt>Password:</dt> <dt>Password:</dt>
<dd> <dd>
......
...@@ -650,6 +650,32 @@ class Host(models.Model): ...@@ -650,6 +650,32 @@ class Host(models.Model):
""" """
return self.get_hostname('ipv4', public=False) return self.get_hostname('ipv4', public=False)
def get_public_endpoints(self, port, protocol='tcp'):
"""Get public IPv4 and IPv6 endpoints for local port.
Optionally the required protocol (e.g. TCP, UDP) can be specified.
"""
endpoints = {}
# IPv4
public_ipv4 = self.pub_ipv4 if self.pub_ipv4 else self.ipv4
# try get matching port(s) without NAT
ports = self.incoming_rules.filter(accept=True, dport=port,
nat=False, proto=protocol)
if ports.exists():
public_port = ports[0].dport
else:
# try get matching port(s) with NAT
ports = self.incoming_rules.filter(accept=True, nat_dport=port,
nat=True, proto=protocol)
public_port = ports[0].dport if ports.exists() else None
endpoints['ipv4'] = ((public_ipv4, public_port) if public_port else
None)
# IPv6
blocked = self.incoming_rules.filter(accept=False, dport=port,
proto=protocol).exists()
endpoints['ipv6'] = (self.ipv6, port) if not blocked else None
return endpoints
@models.permalink @models.permalink
def get_absolute_url(self): def get_absolute_url(self):
return ('network.host', None, {'pk': self.pk}) return ('network.host', None, {'pk': self.pk})
......
...@@ -405,6 +405,24 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel): ...@@ -405,6 +405,24 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel):
return self.interface_set.exclude(host=None)[0].host.get_hostname( return self.interface_set.exclude(host=None)[0].host.get_hostname(
proto=proto) proto=proto)
def get_connect_command(self, use_ipv6=False):
try:
port = self.get_connect_port(use_ipv6=use_ipv6)
host = self.get_connect_host(use_ipv6=use_ipv6)
proto = self.access_method
print proto
if proto == 'rdp':
return 'rdesktop %(host)s:%(port)d -u cloud -p %(pw)s' % {
'port': port, 'proto': proto, 'pw': self.pw,
'host': host}
elif proto == 'ssh':
return ('sshpass -p %(pw)s ssh -o StrictHostKeyChecking=n '
'cloud@%(host)s -p %(port)d') % {
'port': port, 'proto': proto, 'pw': self.pw,
'host': host}
except:
return
def get_connect_uri(self, use_ipv6=False): def get_connect_uri(self, use_ipv6=False):
"""Get access parameters in URI format. """Get access parameters in URI format.
""" """
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment