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

Torben Hohn torben.hohn at linutronix.de
Wed Jun 3 11:01:14 CEST 2020


On Mon, Jun 01, 2020 at 12:12:09PM -0400, Olivier Dion wrote:
> 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>

Reviewed-by: Torben Hohn <torben.hohn 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
> 
> 
> _______________________________________________
> elbe-devel mailing list
> elbe-devel at linutronix.de
> https://lists.linutronix.de/mailman/listinfo/elbe-devel

-- 
Torben Hohn
Linutronix GmbH | Bahnhofstrasse 3 | D-88690 Uhldingen-Mühlhofen
Phone: +49 7556 25 999 18; Fax.: +49 7556 25 999 99

Hinweise zum Datenschutz finden Sie hier (Informations on data privacy 
can be found here): https://linutronix.de/kontakt/Datenschutz.php

Linutronix GmbH | Firmensitz (Registered Office): Uhldingen-Mühlhofen | 
Registergericht (Registration Court): Amtsgericht Freiburg i.Br., HRB700 
806 | Geschäftsführer (Managing Directors): Heinz Egger, Thomas Gleixner



More information about the elbe-devel mailing list