|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (c) 2023 Robert Bosch GmbH and Microsoft Corporation |
| 3 | +# |
| 4 | +# This program and the accompanying materials are made available under the |
| 5 | +# terms of the Apache License, Version 2.0 which is available at |
| 6 | +# https://www.apache.org/licenses/LICENSE-2.0. |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 | +# License for the specific language governing permissions and limitations |
| 12 | +# under the License. |
| 13 | +# |
| 14 | +# SPDX-License-Identifier: Apache-2.0 |
| 15 | + |
| 16 | +echo "#######################################################" |
| 17 | +echo "### Configure-proxies ###" |
| 18 | +echo "#######################################################" |
| 19 | + |
| 20 | +if [ "$HTTP_PROXY" != "" ]; then |
| 21 | + USE_PROXIES="true" |
| 22 | + CONFIGURE_GIT="true" |
| 23 | + CONFIGURE_DOCKER_IN_DOCKER_PROXY="true" |
| 24 | + FTP_PROXY=$HTTP_PROXY |
| 25 | + ALL_PROXY=$HTTP_PROXY |
| 26 | + NO_PROXY="localhost,127.0.0.1,0.0.0.0,10.0.0.0/8,192.168.122.0/24,172.0.0.0/8,cattle-system.svc,.svc,.cluster.local" |
| 27 | +fi |
| 28 | + |
| 29 | +echo "Use proxies: $USE_PROXIES" |
| 30 | +echo "Http-proxy: $HTTP_PROXY" |
| 31 | +echo "Https-proxy: $HTTPS_PROXY" |
| 32 | +echo "Ftp-proxy: $FTP_PROXY" |
| 33 | +echo "All proxy: $ALL_PROXY" |
| 34 | +echo "No proxy: $NO_PROXY" |
| 35 | +echo "Configure git: $CONFIGURE_GIT" |
| 36 | + |
| 37 | +set -e |
| 38 | + |
| 39 | +if [ "$(id -u)" -ne 0 ]; then |
| 40 | + echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' |
| 41 | + exit 1 |
| 42 | +fi |
| 43 | + |
| 44 | +# Determine the appropriate non-root user |
| 45 | +# This recognizes the same possible user names found in Microsoft base Docker images |
| 46 | +# as the scripts in the ../library-scripts directory |
| 47 | +if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then |
| 48 | + USERNAME="" |
| 49 | + POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)") |
| 50 | + for CURRENT_USER in ${POSSIBLE_USERS[@]}; do |
| 51 | + if id -u ${CURRENT_USER} > /dev/null 2>&1; then |
| 52 | + USERNAME=${CURRENT_USER} |
| 53 | + break |
| 54 | + fi |
| 55 | + done |
| 56 | +elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then |
| 57 | + USERNAME=root |
| 58 | +fi |
| 59 | + |
| 60 | +if [ "${USERNAME}" = "" ]; then |
| 61 | + USERNAME=vscode |
| 62 | +fi |
| 63 | +echo "Selected user name is ${USERNAME}" |
| 64 | + |
| 65 | +if [ "${USE_PROXIES}" = "true" ]; then |
| 66 | + echo "Configuring proxies" |
| 67 | + |
| 68 | + mkdir -p /home/${USERNAME} |
| 69 | + echo "export HTTP_PROXY=\"${HTTP_PROXY}\"" >> /home/${USERNAME}/.profile |
| 70 | + echo "export http_proxy=\"${HTTP_PROXY}\"" >> /home/${USERNAME}/.profile |
| 71 | + echo "export HTTPS_PROXY=\"${HTTPS_PROXY}\"" >> /home/${USERNAME}/.profile |
| 72 | + echo "export https_proxy=\"${HTTPS_PROXY}\"" >> /home/${USERNAME}/.profile |
| 73 | + echo "export FTP_PROXY=\"${FTP_PROXY}\"" >> /home/${USERNAME}/.profile |
| 74 | + echo "export ftp_proxy=\"${FTP_PROXY}\"" >> /home/${USERNAME}/.profile |
| 75 | + echo "export ALL_PROXY=\"${ALL_PROXY}\"" >> /home/${USERNAME}/.profile |
| 76 | + echo "export all_proxy=\"${ALL_PROXY}\"" >> /home/${USERNAME}/.profile |
| 77 | + echo "export NO_PROXY=\"${NO_PROXY}\"" >> /home/${USERNAME}/.profile |
| 78 | + echo "export no_proxy=\"${NO_PROXY}\"" >> /home/${USERNAME}/.profile |
| 79 | + |
| 80 | + echo "# Proxy settings" >> /etc/wgetrc |
| 81 | + echo "http_proxy=${HTTP_PROXY}" >> /etc/wgetrc |
| 82 | + echo "https_proxy=${HTTPS_PROXY}" >> /etc/wgetrc |
| 83 | + echo "ftp_proxy=${FTP_PROXY}" >> /etc/wgetrc |
| 84 | + echo "no_proxy=${NO_PROXY}" >> /etc/wgetrc |
| 85 | + echo "use_proxy=on" >> /etc/wgetrc |
| 86 | + |
| 87 | + # enable root user to "apt-get" via proxy |
| 88 | + echo "Acquire::http::proxy \"${HTTP_PROXY}\";" >> /etc/apt/apt.conf |
| 89 | + echo "Acquire::https::proxy \"${HTTPS_PROXY}\";" >> /etc/apt/apt.conf |
| 90 | +fi |
| 91 | + |
| 92 | +exit 0 |
0 commit comments