From a0abe1dad7544973f3d1b36f1746e4353bb18e15 Mon Sep 17 00:00:00 2001 From: lixinyu Date: Wed, 3 Jan 2024 11:45:28 +0800 Subject: [PATCH] proxy: add host proxy function * sometimes, we are developping in a closed network environment, but when we build openeuler image in docker container we will need network to download data, usually we set proxy in host to make network open, so when we start docker container we use host proxy setting by default Signed-off-by: lixinyu --- .../app/plugins/bitbake/in_container.py | 10 ++++++- src/oebuild/util.py | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/oebuild/app/plugins/bitbake/in_container.py b/src/oebuild/app/plugins/bitbake/in_container.py index 37f1032..65c452c 100644 --- a/src/oebuild/app/plugins/bitbake/in_container.py +++ b/src/oebuild/app/plugins/bitbake/in_container.py @@ -279,11 +279,19 @@ class InContainer(BaseBuild): # read container default user .bashrc content content = self._get_bashrc_content(container=container) + # get host proxy information and set in container + init_proxy_command = "" + host_proxy = oebuild_util.get_host_proxy(oebuild_util.PROXY_LIST) + for key, value in host_proxy.items(): + key_git = key.replace('_', '.') + command = f'export {key}={value}; git config --global {key_git} {value};' + init_proxy_command = f'{init_proxy_command} {command}' + init_sdk_command = f'. {oebuild_util.NATIVESDK_DIR}/{oebuild_util.get_nativesdk_environment(container=container)}' set_template = f'export TEMPLATECONF="{oebuild_util.CONTAINER_SRC}/yocto-meta-openeuler/.oebuild"' init_oe_comand = f'. {oebuild_util.CONTAINER_SRC}/yocto-poky/oe-init-build-env \ {oebuild_util.CONTAINER_BUILD}/{build_dir_name}' - init_command = [init_sdk_command, set_template, init_oe_comand] + init_command = [init_proxy_command, init_sdk_command, set_template, init_oe_comand] new_content = self._init_bashrc_content(content, init_command) self.update_bashrc(container=container, content=new_content) diff --git a/src/oebuild/util.py b/src/oebuild/util.py index 748860b..27a7d35 100644 --- a/src/oebuild/util.py +++ b/src/oebuild/util.py @@ -17,6 +17,7 @@ import random import getpass import sys import re +import subprocess from ruamel.yaml import YAML from docker.errors import DockerException @@ -36,6 +37,7 @@ DEFAULT_DOCKER = "swr.cn-north-4.myhuaweicloud.com/openeuler-embedded/openeuler- CONTAINER_SRC = '/usr1/openeuler/src' CONTAINER_USER = "openeuler" NATIVESDK_DIR = "/opt/buildtools/nativesdk" +PROXY_LIST = ['http_proxy', 'https_proxy'] def get_nativesdk_environment(nativesdk_dir=NATIVESDK_DIR, container: Container = None): ''' @@ -219,3 +221,27 @@ def add_bashrc(content: str, line: str): content = content + line + BASH_END_FLAG + '\n' return content + +def get_host_proxy(proxy_name): + ''' + get proxy information from host + ''' + host_proxy = {} + if proxy_name is None: + return host_proxy + + for name in proxy_name: + command = "env | grep %s | awk -F'=' '{print$NF}'" % name + res = subprocess.run(command, + shell=True, + capture_output=True, + encoding="utf-8", + check=False) + if res.returncode != 0: + logger.error("get proxy variable failed") + sys.exit(res.returncode) + value = res.stdout.strip() + if value != "": + host_proxy[name] = value + + return host_proxy -- Gitee