Commit f8478254 by Szabolcs Gelencsér

Add connection details if floating ip is available

parent 4cc50dd2
......@@ -114,12 +114,10 @@
<dd>{{ access_method|upper }}</dd>
<dt>{% trans "Host" %}</dt>
<dd>
{% if instance.get_connect_port %}
{{ instance.get_connect_host }}:<strong>{{ instance.get_connect_port }}</strong>
{% elif instance.interface_set.count < 1%}
<strong>{% trans "The VM doesn't have any network interface." %}</strong>
{% if connection_port %}
{{ connection_host }}:<strong>{{ connection_port }}</strong>
{% else %}
<strong>{% trans "The required port for this protocol is not forwarded." %}</strong>
<strong>{% trans "The VM doesn't have any public network interface." %}</strong>
{% endif %}
</dd>
......
......@@ -100,6 +100,17 @@ logger = logging.getLogger(__name__)
class VmDetailView(LoginRequiredMixin, GraphMixin, DetailView):
template_name = "dashboard/vm-detail.html"
def get_instance_connection(self):
all_floating_ips = openstack_api.neutron.tenant_floating_ip_list(self.request)
instance_floating_ips = [ip for ip in all_floating_ips if ip.instance_id == self.object.id]
if len(instance_floating_ips) > 0:
return instance_floating_ips[0].ip, 22
all_ports = openstack_api.neutron.port_list(self.request)
instance_ports = [p for p in all_ports if p.instance_id == self.object.id]
return None, None
def get(self, *args, **kwargs):
if self.request.is_ajax():
return JsonResponse(self.get_json_data())
......@@ -108,9 +119,14 @@ class VmDetailView(LoginRequiredMixin, GraphMixin, DetailView):
def get_json_data(self):
instance = self.get_object()
connenction_host, connection_port = self.get_instance_connection()
try:
self.object.pw = ServerPassword.objects.get(os_server_id=self.object.id).password
except:
pass
return {"status": instance.status,
"host": instance.get_connect_host(),
"port": instance.get_connect_port(),
"host": connenction_host,
"port": connection_port,
"password": instance.pw}
def get_object(self, queryset=None):
......@@ -135,15 +151,28 @@ class VmDetailView(LoginRequiredMixin, GraphMixin, DetailView):
except:
pass
try:
self.object.pw = ServerPassword.objects.get(os_server_id=self.object.id).password
except:
pass
connection_host, connection_port = self.get_instance_connection()
connection_cmd = {
"cmd": "sshpass -p %s ssh -o StrictHostKeyChecking=no cloud@%s -p %s" % (
self.object.pw, connection_host, connection_port)
}
context.update({
'graphite_enabled': settings.GRAPHITE_URL is not None,
'vnc_url': vnc_console.url if vnc_console else None,
'ops': ops,
'op': {i.op: i for i in ops},
# 'connect_commands': user.profile.get_connect_commands(instance),
'connect_commands': [connection_cmd,],
'hide_tutorial': hide_tutorial,
'fav': Favourite.objects.filter(user=user.id, instance=instance.id).exists(),
'instance': self.object,
"connection_host": connection_host,
"connection_port": connection_port,
'access_method': 'ssh'
})
......
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