From 6263f167e2c4ab2897af888c20e963db48a2427a Mon Sep 17 00:00:00 2001
From: Alexander Lutay <alutay@sipwise.com>
Date: Mon, 19 Jul 2021 09:45:38 +0200
Subject: [PATCH] TT#130659 Stop calling systemd openvpn status twice on every
 HTTP request

The ngcp-panel<=>openvpn functionality has never been finished but on every
HTTP requset ngcp-panel was asking systemd about openvpn status. Twice!

From /var/log/ngcp/panel-debug.log :
> Jul 19 09:36:32 sp1 ngcp-panel: DEBUG: Path: dashboard
> Jul 19 09:36:32 sp1 ngcp-panel: DEBUG: systemctl list-unit-files openvpn.service
> Jul 19 09:36:32 sp1 ngcp-panel: DEBUG: systemctl is-enabled openvpn@ovpn
> ...
> Jul 19 09:36:33 sp1 ngcp-panel: DEBUG: Path: dashboard/ajax/peering_sum
> Jul 19 09:36:33 sp1 ngcp-panel: DEBUG: systemctl list-unit-files openvpn.service
> Jul 19 09:36:33 sp1 ngcp-panel: DEBUG: systemctl is-enabled openvpn@ovpn
> ...
> Jul 19 09:36:33 sp1 ngcp-panel: DEBUG: Path: dashboard/ajax/emergency_mode
> Jul 19 09:36:33 sp1 ngcp-panel: DEBUG: systemctl list-unit-files openvpn.service
> Jul 19 09:36:33 sp1 ngcp-panel: DEBUG: systemctl is-enabled openvpn@ovpn

It is unnecessary load, moreover for not-yet finished feature.
Let's move openvpn WIP functionality into 'NGCP Support Status' page and
finish it there one day. The goal of this commit is to stop DOS'ing systemd.

Change-Id: I6c9ba9e1b218e58b5adffe741f4490b3729d17e7
---
 lib/NGCP/Panel/Controller/Root.pm           |  6 ------
 lib/NGCP/Panel/Controller/Statistics.pm     |  6 ++++++
 share/layout/body.tt                        | 11 -----------
 share/templates/statistics/supportstatus.tt | 10 ++++++++++
 4 files changed, 16 insertions(+), 17 deletions(-)

diff --git a/lib/NGCP/Panel/Controller/Root.pm b/lib/NGCP/Panel/Controller/Root.pm
index 3842654bf0..de06c2ec5e 100644
--- a/lib/NGCP/Panel/Controller/Root.pm
+++ b/lib/NGCP/Panel/Controller/Root.pm
@@ -342,12 +342,6 @@ sub auto :Private {
     # load top menu widgets
     my $topmenu_templates = [];
     $topmenu_templates = ['widgets/'.$c->user->roles.'_topmenu_settings.tt'];
-    if ($c->user->roles eq 'admin') {
-        if (!$c->stash->{openvpn_info}) {
-            my $openvpn_info = NGCP::Panel::Utils::Auth::check_openvpn_status($c);
-            $c->stash(openvpn_info => $openvpn_info);
-        }
-    }
     $c->stash(topmenu => $topmenu_templates);
 
     $self->include_framed($c);
diff --git a/lib/NGCP/Panel/Controller/Statistics.pm b/lib/NGCP/Panel/Controller/Statistics.pm
index e73bb0145a..b4769f8c14 100644
--- a/lib/NGCP/Panel/Controller/Statistics.pm
+++ b/lib/NGCP/Panel/Controller/Statistics.pm
@@ -49,6 +49,12 @@ sub supportstatus :Chained('/') :PathPart('statistics/supportstatus') :Args() {
     $c->stash(support_status_code => $support_status_code,
         #template => 'statistics/supportstatus.tt',
     );
+
+    if (!$c->stash->{openvpn_info}) {
+        my $openvpn_info = NGCP::Panel::Utils::Auth::check_openvpn_status($c);
+        $c->stash(openvpn_info => $openvpn_info);
+    }
+
     return;
 }
 
diff --git a/share/layout/body.tt b/share/layout/body.tt
index 1b09e37095..99273c68fb 100644
--- a/share/layout/body.tt
+++ b/share/layout/body.tt
@@ -62,17 +62,6 @@
                     <li>
                         [% IF c.user && c.session.user_tz_name; '(' _ c.session.user_tz.name _ ' ' _ c.loc('time') _ ')'; END; %]
                     </li>
-[%-   IF ( c.user && ( c.user.roles == 'admin' || c.user.is_superuser ) ) && openvpn_info.allowed -%]
-[%-     IF !openvpn_info.active %]
-                    <li>
-                        <a class="btn btn-small btn-secondary" href="javascript:toggleOpenvpn('confirm');void(0);"><i class="icon-off"></i>Openvpn</a>
-                    </li>
-[%      ELSE -%]
-                    <li>
-                        <a class="btn btn-small btn-success" href="javascript:toggleOpenvpn('confirm');void(0);"><i class="icon-ok"></i>Openvpn</a>
-                    </li>
-[%      END -%]
-[%   END -%]
                     <li class="dropdown">
                     <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                         <i class="icon-globe"></i> [% c.loc('Language') %]
diff --git a/share/templates/statistics/supportstatus.tt b/share/templates/statistics/supportstatus.tt
index d7f9e5a09f..8facd2b6d7 100644
--- a/share/templates/statistics/supportstatus.tt
+++ b/share/templates/statistics/supportstatus.tt
@@ -30,4 +30,14 @@
 
 [% END -%]
 
+<br/>
+
+[% IF openvpn_info.allowed -%]
+[%   IF openvpn_info.active -%]
+<p>[% c.loc('Sipwise VPN service is') %] <a class="btn btn-small btn-success" href="javascript:toggleOpenvpn('confirm');void(0);"><i class="icon-ok"></i>Running</a></p>
+[%   ELSE -%]
+<p>[% c.loc('Sipwise VPN service is') %] <a class="btn btn-small btn-secondary" href="javascript:toggleOpenvpn('confirm');void(0);"><i class="icon-off"></i>Stopped</a></p>
+[%   END -%]
+[% END -%]
+
 [% # vim: set tabstop=4 syntax=html expandtab: -%]