TorizonCore Builder can't download toolchain because of proxy

We are trying to build and add a external kernel module to a custom image with torizoncore-builder.

torizoncore-builder build command fails because it can’t download the toolchain it needs.

2022-08-03 10:48:59,836 - torizon.tcbuilder.backend.kernel - INFO - A toolchain is required to build the module.
Downloading toolchain from http://sources.toradex.com/tcb/toolchains/gcc-arm-9.2-2019.12-x86_64-arm-none-linux-gnueabihf.tar.xz.
Please wait this could take a while...
2022-08-03 10:49:20,843 - root - ERROR - Error: The requested toolchain could not be downloaded
2022-08-03 10:49:20,845 - root - DEBUG - Traceback (most recent call last):
  File "/usr/lib/python3.9/urllib/request.py", line 1346, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "/usr/lib/python3.9/http/client.py", line 1255, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.9/http/client.py", line 1301, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.9/http/client.py", line 1250, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.9/http/client.py", line 1010, in _send_output
    self.send(msg)
  File "/usr/lib/python3.9/http/client.py", line 950, in send
    self.connect()
  File "/usr/lib/python3.9/http/client.py", line 921, in connect
    self.sock = self._create_connection(
  File "/usr/lib/python3.9/socket.py", line 843, in create_connection
    raise err
  File "/usr/lib/python3.9/socket.py", line 831, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/builder/tcbuilder/backend/kernel.py", line 128, in download_toolchain
    urllib.request.urlretrieve(url, filename=tarball, reporthook=progress)
  File "/usr/lib/python3.9/urllib/request.py", line 239, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "/usr/lib/python3.9/urllib/request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.9/urllib/request.py", line 517, in open
    response = self._open(req, data)
  File "/usr/lib/python3.9/urllib/request.py", line 534, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "/usr/lib/python3.9/urllib/request.py", line 494, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.9/urllib/request.py", line 1375, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "/usr/lib/python3.9/urllib/request.py", line 1349, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 111] Connection refused>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/builder/torizoncore-builder", line 213, in <module>
    mainargs.func(mainargs)
  File "/builder/tcbuilder/cli/build.py", line 492, in do_build
    raise exc
  File "/builder/tcbuilder/cli/build.py", line 469, in do_build
    build(args.config_fname, args.storage_directory,
  File "/builder/tcbuilder/cli/build.py", line 439, in build
    fs_changes = handle_customization_section(
  File "/builder/tcbuilder/cli/build.py", line 173, in handle_customization_section
    handle_kernel_customization(props["kernel"], storage_dir=storage_dir)
  File "/builder/tcbuilder/cli/build.py", line 229, in handle_kernel_customization
    kernel_cli.kernel_build_module(
  File "/builder/tcbuilder/cli/kernel.py", line 94, in kernel_build_module
    kernel.build_module(
  File "/builder/tcbuilder/backend/kernel.py", line 56, in build_module
    download_toolchain(c_c, toolchain_path)
  File "/builder/tcbuilder/backend/kernel.py", line 131, in download_toolchain
    raise TorizonCoreBuilderError("The requested toolchain could not be downloaded")
tcbuilder.errors.TorizonCoreBuilderError: The requested toolchain could not be downloaded

All http (not https) request have to go through our corporate proxy.

How do we configure torizoncore-builder to use a proxy for http requests?

We were able to solve it by adding the proxy configuration environment variable to the “docker run” extra options when sourcing the tcb-env-setup.sh script.

> export tcb-env-setup.sh -- --env http_proxy="http://user:password@proxy.server:port/"

as described at the end of the source script usage information:

> tcb-env-setup.sh -h

Usage: source tcb-env-setup.sh [OPTIONS] [-- <docker_options>]

optional arguments:
  -a <value>: select auto mode
      With this flag enabled the script will automatically run with no need
      for user input. Valid values for <value> are either remote or local.
      When "-a remote" is passed, the script will automatically use the
      latest version of TorizonCore Builder online, with no consideration
      for any local versions that may exist. When "-a local" is passed
      the script will automatically use the latest version of TorizonCore
      Builder found locally, with no consideration to what may be online.
      This flag is mutually exclusive with the -t flag.

  -t <version tag>: select tag mode
      With this flag enabled the script will automatically run with no need
      for user input. Valid values for <version tag> can be found online:
      https://registry.hub.docker.com/r/torizon/torizoncore-builder/tags?page=1&ordering=last_updated.
      Whatever <version tag> is provided will then be pulled from online.
      This flag is mutually exclusive with the -a flag.

  -d: disable volumes
      With this flag enabled the script will setup torizoncore-builder
      without Docker volumes meaning some torizoncore-builder commands will
      require additional directories to be passed as arguments. By default
      with this flag excluded torizoncore-builder is setup with Docker
      volumes.

  -s: select storage directory or Docker volume
      Internal storage directory or Docker volume that TorizonCore Builder
      should use to keep its state information and image customizations.
      It must be an absolute directory or a Docker volume name. If this
      flag is not set, the "storage" Docker volume will be used.

  -n: do not enable "host" network mode.
      Under Linux the tool runs in "host" network mode by default allowing
      it to operate as a server without explicit port publishing. Under
      Windows this mode of operation is always disabled requiring port
      publishing to be set up if the tool is to act as a server. This flag
      disables the default behavior (which is relevant under Linux).

  -- <docker_options>: extra options to be passed to "docker run".
       Parameters after -- are simply forwarded to the "docker run"
       invocation in the alias that the script creates.

  -h: help
       Prints usage information.