From d2b414f5c67ad94c8b83e69a2acc3d54f208ea79 Mon Sep 17 00:00:00 2001 From: facetoe Date: Sun, 24 Jul 2016 11:04:44 +0800 Subject: [PATCH] Support toggling connection on click. --- i3pystatus/openvpn.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/i3pystatus/openvpn.py b/i3pystatus/openvpn.py index 48d1792..830ef28 100644 --- a/i3pystatus/openvpn.py +++ b/i3pystatus/openvpn.py @@ -25,6 +25,10 @@ class OpenVPN(IntervalModule): format = "{vpn_name} {status}" status_command = "bash -c 'systemctl show openvpn@%(vpn_name)s | grep ActiveState=active'" + vpn_up_command = "sudo /bin/systemctl start openvpn@%(vpn_name)s.service" + vpn_down_command = "sudo /bin/systemctl stop openvpn@%(vpn_name)s.service" + + connected = False label = '' vpn_name = '' @@ -35,6 +39,8 @@ class OpenVPN(IntervalModule): ("status_down", "Symbol to display when down"), ("status_up", "Symbol to display when up"), ("vpn_name", "Name of VPN"), + ("vpn_up_command", "Command to bring up the VPN - default requires editing /etc/sudoers"), + ("vpn_down_command", "Command to bring up the VPN - default requires editing /etc/sudoers"), ("status_command", "command to find out if the VPN is active"), ) @@ -42,16 +48,24 @@ class OpenVPN(IntervalModule): if not self.vpn_name: raise Exception("vpn_name is required") + def toggle_connection(self): + if self.connected: + command = self.vpn_down_command + else: + command = self.vpn_up_command + run_through_shell(command % {'vpn_name': self.vpn_name}, enable_shell=True) + + def on_click(self, button, **kwargs): + self.toggle_connection() + def run(self): command_result = run_through_shell(self.status_command % {'vpn_name': self.vpn_name}, enable_shell=True) - output = command_result.out.strip() + self.connected = True if command_result.out.strip() else False - if output: - color = self.color_up - status = self.status_up + if self.connected: + color, status = self.color_up, self.status_up else: - color = self.color_down - status = self.status_down + color, status = self.color_down, self.status_down vpn_name = self.vpn_name label = self.label