[elbe-devel] [PATCH 1/3] xmlpreprocess: Add proxy preprocess directive

Olivier Dion dion at linutronix.de
Mon Jun 1 18:12:09 CEST 2020


A proxy can be added to mirrors if they don't have one.  Using the
'-p PROXY' or '--proxy=PROXY' options or by the 'http_proxy'
environment variable.

In case that a mirror has already a proxy, a warning is emitted and no
override is done.

Signed-off-by: Olivier Dion <dion at linutronix.de>
---
 elbepack/xmlpreprocess.py | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)

diff --git a/elbepack/xmlpreprocess.py b/elbepack/xmlpreprocess.py
index d0714489..d5c7152d 100644
--- a/elbepack/xmlpreprocess.py
+++ b/elbepack/xmlpreprocess.py
@@ -7,6 +7,7 @@
 
 from __future__ import print_function
 
+import os
 import sys
 try:
     from urllib.request import urlopen
@@ -19,7 +20,7 @@ from optparse import OptionGroup
 from itertools import islice
 
 from lxml import etree
-from lxml.etree import XMLParser, parse
+from lxml.etree import XMLParser, parse, Element
 
 from elbepack.archivedir import ArchivedirError, combinearchivedir
 from elbepack.config import cfg
@@ -98,6 +99,33 @@ def preprocess_initvm_ports(xml):
                 host.text == cfg['soapport'] and benv.text == '7588'):
             forward.getparent().remove(forward)
 
+def preprocess_proxy_add(xml, opt_proxy=None):
+    """Add proxy to mirrors from CLI arguments or environment variable"""
+
+    # Add proxy from CLI or env?
+    set_proxy = opt_proxy or os.getenv("http_proxy")
+
+    if set_proxy is None:
+        return
+
+    proxy_tag = "primary_proxy"
+
+    # For all mirrors
+    for mirror in xml.iterfind(".//mirror"):
+
+        current_proxy = mirror.find(proxy_tag)
+
+        # If there's already a proxy and we're trying to override it
+        if current_proxy is not None:
+            print('[WARN] Trying to override proxy "%s"!' %
+                  current_proxy.text)
+            continue
+
+        # Add proxy to mirror
+        proxy_e      = Element(proxy_tag)
+        proxy_e.text = set_proxy
+
+        mirror.append(proxy_e)
 
 def preprocess_mirror_replacement(xml):
     """Do search and replace on mirror urls
@@ -130,7 +158,7 @@ def preprocess_mirror_replacement(xml):
             u.attrib['value'] = u.attrib['value'].replace(r[0], r[1])
 
 
-def xmlpreprocess(fname, output, variants=None):
+def xmlpreprocess(fname, output, variants=None, proxy=None):
 
     # pylint: disable=too-many-locals
     # pylint: disable=too-many-branches
@@ -199,6 +227,8 @@ def xmlpreprocess(fname, output, variants=None):
 
         preprocess_mirror_replacement(xml)
 
+        preprocess_proxy_add(xml, proxy)
+
         # Change public PGP url key to raw key
         preprocess_pgp_key(xml)
 
-- 
2.26.2




More information about the elbe-devel mailing list