From f87b5fa3b9a2c1c6826701924712654c8f0c5709 Mon Sep 17 00:00:00 2001 From: hagbard Date: Fri, 18 Oct 2019 12:37:19 -0700 Subject: system-proxy: T1741 - Add system wide proxy setting * removed subprocess as it is not required, script is executed via sudo * pep8 formatted --- src/conf_mode/system-proxy.py | 97 ++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 48 deletions(-) diff --git a/src/conf_mode/system-proxy.py b/src/conf_mode/system-proxy.py index 02e1a2eda..cf72a1f96 100755 --- a/src/conf_mode/system-proxy.py +++ b/src/conf_mode/system-proxy.py @@ -19,65 +19,67 @@ import sys import os import re -import subprocess from vyos import ConfigError from vyos.config import Config -proxy_def=r'/etc/profile.d/vyos-system-proxy.sh' +proxy_def = r'/etc/profile.d/vyos-system-proxy.sh' + def get_config(): - c = Config() - if not c.exists('system proxy'): - return None - - c.set_level('system proxy') - - cnf = { - 'url' : None, - 'port' : None, - 'usr' : None, - 'passwd' : None - } - - if c.exists('url'): - cnf['url'] = c.return_value('url') - if c.exists('port'): - cnf['port'] = c.return_value('port') - if c.exists('username'): - cnf['usr'] = c.return_value('username') - if c.exists('password'): - cnf['passwd'] = c.return_value('password') - - return cnf + c = Config() + if not c.exists('system proxy'): + return None + + c.set_level('system proxy') + + cnf = { + 'url': None, + 'port': None, + 'usr': None, + 'passwd': None + } + + if c.exists('url'): + cnf['url'] = c.return_value('url') + if c.exists('port'): + cnf['port'] = c.return_value('port') + if c.exists('username'): + cnf['usr'] = c.return_value('username') + if c.exists('password'): + cnf['passwd'] = c.return_value('password') + + return cnf + def verify(c): - if not c: - return None - if not c['url'] or not c['port']: - raise ConfigError("proxy url and port requires a value") - elif c['usr'] and not c['passwd']: - raise ConfigError("proxy password requires a value") - elif not c['usr'] and c['passwd']: - raise ConfigError("proxy username requires a value") + if not c: + return None + if not c['url'] or not c['port']: + raise ConfigError("proxy url and port requires a value") + elif c['usr'] and not c['passwd']: + raise ConfigError("proxy password requires a value") + elif not c['usr'] and c['passwd']: + raise ConfigError("proxy username requires a value") + def generate(c): - if not c: - return None - if not c['usr']: - return str("export http_proxy={url}:{port}\nexport https_proxy=$http_proxy\nexport ftp_proxy=$http_proxy"\ - .format(url=c['url'], port=c['port'])) - else: - return str("export http_proxy=http://{usr}:{passwd}@{url}:{port}\nexport https_proxy=$http_proxy\nexport ftp_proxy=$http_proxy"\ - .format(url=re.sub('http://','',c['url']), port=c['port'], usr=c['usr'], passwd=c['passwd'])) + if not c: + return None + if not c['usr']: + return str("export http_proxy={url}:{port}\nexport https_proxy=$http_proxy\nexport ftp_proxy=$http_proxy" + .format(url=c['url'], port=c['port'])) + else: + return str("export http_proxy=http://{usr}:{passwd}@{url}:{port}\nexport https_proxy=$http_proxy\nexport ftp_proxy=$http_proxy" + .format(url=re.sub('http://', '', c['url']), port=c['port'], usr=c['usr'], passwd=c['passwd'])) + def apply(ln): - if not ln and os.path.exists(proxy_def): - subprocess.call(['unset http_proxy https_proxy ftp_proxy'], shell=True) - os.remove(proxy_def) - else: - open(proxy_def,'w').write("# generated by system-proxy.py\n{}\n".format(ln)) - subprocess.call(['. /etc/profile.d/vyos-system-proxy.sh'], shell=True) + if not ln and os.path.exists(proxy_def): + os.remove(proxy_def) + else: + open(proxy_def, 'w').write( + "# generated by system-proxy.py\n{}\n".format(ln)) if __name__ == '__main__': try: @@ -88,4 +90,3 @@ if __name__ == '__main__': except ConfigError as e: print(e) sys.exit(1) - -- cgit v1.2.3