github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/docker/custom-scripts/kubectl-helm-debian.sh (about)

     1  docker/#!/usr/bin/env bash
     2  
     3  # Copyright 2021 The Dapr Authors
     4  # Licensed under the Apache License, Version 2.0 (the "License");
     5  # you may not use this file except in compliance with the License.
     6  # You may obtain a copy of the License at
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  # Unless required by applicable law or agreed to in writing, software
     9  # distributed under the License is distributed on an "AS IS" BASIS,
    10  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  # See the License for the specific language governing permissions and
    12  # limitations under the License.
    13  #
    14  #
    15  # Initializes the devcontainer tasks each time the container starts.
    16  # Users can edit this copy under /usr/local/share in the container to
    17  # customize this as needed for their custom localhost bindings.
    18  
    19  # Source: https://github.com/microsoft/vscode-dev-containers/blob/v0.224.3/script-library/kubectl-helm-debian.sh
    20  
    21  #-------------------------------------------------------------------------------------------------------------
    22  # Copyright (c) Microsoft Corporation. All rights reserved.
    23  # Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
    24  #-------------------------------------------------------------------------------------------------------------
    25  #
    26  # Docs: https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/kubectl-helm.md
    27  # Maintainer: The VS Code and Codespaces Teams
    28  #
    29  # Syntax: ./kubectl-helm-debian.sh [kubectl version] [Helm version] [minikube version] [kubectl SHA256] [Helm SHA256] [minikube SHA256]
    30  
    31  set -e
    32  
    33  KUBECTL_VERSION="${1:-"latest"}"
    34  HELM_VERSION="${2:-"latest"}"
    35  MINIKUBE_VERSION="${3:-"none"}" # latest is also valid
    36  KUBECTL_SHA256="${4:-"automatic"}"
    37  HELM_SHA256="${5:-"automatic"}"
    38  MINIKUBE_SHA256="${6:-"automatic"}"
    39  USERNAME=${7:-"automatic"}
    40  
    41  HELM_GPG_KEYS_URI="https://raw.githubusercontent.com/helm/helm/main/KEYS"
    42  GPG_KEY_SERVERS="keyserver hkp://keyserver.ubuntu.com:80
    43  keyserver hkps://keys.openpgp.org
    44  keyserver hkp://keyserver.pgp.com"
    45  
    46  if [ "$(id -u)" -ne 0 ]; then
    47      echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
    48      exit 1
    49  fi
    50  
    51  # Determine the appropriate non-root user
    52  if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
    53      USERNAME=""
    54      POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
    55      for CURRENT_USER in ${POSSIBLE_USERS[@]}; do
    56          if id -u ${CURRENT_USER} > /dev/null 2>&1; then
    57              USERNAME=${CURRENT_USER}
    58              break
    59          fi
    60      done
    61      if [ "${USERNAME}" = "" ]; then
    62          USERNAME=root
    63      fi
    64  elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then
    65      USERNAME=root
    66  fi
    67  
    68  # Get central common setting
    69  get_common_setting() {
    70      if [ "${common_settings_file_loaded}" != "true" ]; then
    71          curl -sfL "https://aka.ms/vscode-dev-containers/script-library/settings.env" 2>/dev/null -o /tmp/vsdc-settings.env || echo "Could not download settings file. Skipping."
    72          common_settings_file_loaded=true
    73      fi
    74      if [ -f "/tmp/vsdc-settings.env" ]; then
    75          local multi_line=""
    76          if [ "$2" = "true" ]; then multi_line="-z"; fi
    77          local result="$(grep ${multi_line} -oP "$1=\"?\K[^\"]+" /tmp/vsdc-settings.env | tr -d '\0')"
    78          if [ ! -z "${result}" ]; then declare -g $1="${result}"; fi
    79      fi
    80      echo "$1=${!1}"
    81  }
    82  
    83  # Figure out correct version of a three part version number is not passed
    84  find_version_from_git_tags() {
    85      local variable_name=$1
    86      local requested_version=${!variable_name}
    87      if [ "${requested_version}" = "none" ]; then return; fi
    88      local repository=$2
    89      local prefix=${3:-"tags/v"}
    90      local separator=${4:-"."}
    91      local last_part_optional=${5:-"false"}    
    92      if [ "$(echo "${requested_version}" | grep -o "." | wc -l)" != "2" ]; then
    93          local escaped_separator=${separator//./\\.}
    94          local last_part
    95          if [ "${last_part_optional}" = "true" ]; then
    96              last_part="(${escaped_separator}[0-9]+)?"
    97          else
    98              last_part="${escaped_separator}[0-9]+"
    99          fi
   100          local regex="${prefix}\\K[0-9]+${escaped_separator}[0-9]+${last_part}$"
   101          local version_list="$(git ls-remote --tags ${repository} | grep -oP "${regex}" | tr -d ' ' | tr "${separator}" "." | sort -rV)"
   102          if [ "${requested_version}" = "latest" ] || [ "${requested_version}" = "current" ] || [ "${requested_version}" = "lts" ]; then
   103              declare -g ${variable_name}="$(echo "${version_list}" | head -n 1)"
   104          else
   105              set +e
   106              declare -g ${variable_name}="$(echo "${version_list}" | grep -E -m 1 "^${requested_version//./\\.}([\\.\\s]|$)")"
   107              set -e
   108          fi
   109      fi
   110      if [ -z "${!variable_name}" ] || ! echo "${version_list}" | grep "^${!variable_name//./\\.}$" > /dev/null 2>&1; then
   111          echo -e "Invalid ${variable_name} value: ${requested_version}\nValid values:\n${version_list}" >&2
   112          exit 1
   113      fi
   114      echo "${variable_name}=${!variable_name}"
   115  }
   116  
   117  # Function to run apt-get if needed
   118  apt_get_update_if_needed()
   119  {
   120      if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then
   121          echo "Running apt-get update..."
   122          apt-get update
   123      else
   124          echo "Skipping apt-get update."
   125      fi
   126  }
   127  
   128  # Checks if packages are installed and installs them if not
   129  check_packages() {
   130      if ! dpkg -s "$@" > /dev/null 2>&1; then
   131          apt_get_update_if_needed
   132          apt-get -y install --no-install-recommends "$@"
   133      fi
   134  }
   135  
   136  # Ensure apt is in non-interactive to avoid prompts
   137  export DEBIAN_FRONTEND=noninteractive
   138  
   139  # Install dependencies
   140  check_packages curl ca-certificates coreutils gnupg2 dirmngr bash-completion
   141  if ! type git > /dev/null 2>&1; then
   142      apt_get_update_if_needed
   143      apt-get -y install --no-install-recommends git
   144  fi
   145  
   146  architecture="$(uname -m)"
   147  case $architecture in
   148      x86_64) architecture="amd64";;
   149      aarch64 | armv8*) architecture="arm64";;
   150      aarch32 | armv7* | armvhf*) architecture="arm";;
   151      i?86) architecture="386";;
   152      *) echo "(!) Architecture $architecture unsupported"; exit 1 ;;
   153  esac
   154  
   155  # Install the kubectl, verify checksum
   156  echo "Downloading kubectl..."
   157  if [ "${KUBECTL_VERSION}" = "latest" ] || [ "${KUBECTL_VERSION}" = "lts" ] || [ "${KUBECTL_VERSION}" = "current" ] || [ "${KUBECTL_VERSION}" = "stable" ]; then
   158      KUBECTL_VERSION="$(curl -sSL https://dl.k8s.io/release/stable.txt)"
   159  else
   160      find_version_from_git_tags KUBECTL_VERSION ${GITHUB_PROXY}https://github.com/kubernetes/kubernetes
   161  fi
   162  if [ "${KUBECTL_VERSION::1}" != 'v' ]; then
   163      KUBECTL_VERSION="v${KUBECTL_VERSION}"
   164  fi
   165  curl -sSL -o /usr/local/bin/kubectl "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/${architecture}/kubectl"
   166  chmod 0755 /usr/local/bin/kubectl
   167  if [ "$KUBECTL_SHA256" = "automatic" ]; then
   168      KUBECTL_SHA256="$(curl -sSL "https://dl.k8s.io/${KUBECTL_VERSION}/bin/linux/${architecture}/kubectl.sha256")"
   169  fi
   170  ([ "${KUBECTL_SHA256}" = "dev-mode" ] || (echo "${KUBECTL_SHA256} */usr/local/bin/kubectl" | sha256sum -c -))
   171  if ! type kubectl > /dev/null 2>&1; then
   172      echo '(!) kubectl installation failed!'
   173      exit 1
   174  fi
   175  
   176  # kubectl bash completion
   177  kubectl completion bash > /etc/bash_completion.d/kubectl
   178  
   179  # kubectl zsh completion
   180  mkdir -p "/home/${USERNAME}/.oh-my-zsh/completions"
   181  kubectl completion zsh > "/home/${USERNAME}/.oh-my-zsh/completions/_kubectl"
   182  chown -R "${USERNAME}" "/home/${USERNAME}/.oh-my-zsh"
   183  
   184  # Install Helm, verify signature and checksum
   185  echo "Downloading Helm..."
   186  # find_version_from_git_tags HELM_VERSION "${GITHUB_PROXY}https://github.com/helm/helm"
   187  # if [ "${HELM_VERSION::1}" != 'v' ]; then
   188  #     HELM_VERSION="v${HELM_VERSION}"
   189  # fi
   190  # echo curl "${GITHUB_PROXY}https://raw.githubusercontent.com/helm/helm/${HELM_VERSION}/scripts/get-helm-3"
   191  # curl "${GITHUB_PROXY}https://raw.githubusercontent.com/helm/helm/${HELM_VERSION}/scripts/get-helm-3" | bash
   192  
   193  curl https://baltocdn.com/helm/signing.asc | gpg --dearmor | sudo tee /usr/share/keyrings/helm.gpg > /dev/null
   194  apt-get install apt-transport-https --yes
   195  echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/helm.gpg] https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list
   196  apt-get update
   197  apt-get install --no-install-recommends helm
   198  
   199  if ! type helm > /dev/null 2>&1; then
   200      echo '(!) Helm installation failed!'
   201      exit 1
   202  fi
   203  
   204  # Install Minikube, verify checksum
   205  if [ "${MINIKUBE_VERSION}" != "none" ]; then
   206      echo "Downloading minikube..."
   207      if [ "${MINIKUBE_VERSION}" = "latest" ] || [ "${MINIKUBE_VERSION}" = "lts" ] || [ "${MINIKUBE_VERSION}" = "current" ] || [ "${MINIKUBE_VERSION}" = "stable" ]; then
   208          MINIKUBE_VERSION="latest"
   209      else
   210          find_version_from_git_tags MINIKUBE_VERSION ${GITHUB_PROXY}https://github.com/kubernetes/minikube
   211          if [ "${MINIKUBE_VERSION::1}" != "v" ]; then
   212              MINIKUBE_VERSION="v${MINIKUBE_VERSION}"
   213          fi
   214      fi
   215      # latest is also valid in the download URLs 
   216      curl -sSL -o /usr/local/bin/minikube "https://storage.googleapis.com/minikube/releases/${MINIKUBE_VERSION}/minikube-linux-${architecture}"    
   217      chmod 0755 /usr/local/bin/minikube
   218      if [ "$MINIKUBE_SHA256" = "automatic" ]; then
   219          MINIKUBE_SHA256="$(curl -sSL "https://storage.googleapis.com/minikube/releases/${MINIKUBE_VERSION}/minikube-linux-${architecture}.sha256")"
   220      fi
   221      ([ "${MINIKUBE_SHA256}" = "dev-mode" ] || (echo "${MINIKUBE_SHA256} */usr/local/bin/minikube" | sha256sum -c -))
   222      if ! type minikube > /dev/null 2>&1; then
   223          echo '(!) minikube installation failed!'
   224          exit 1
   225      fi
   226  fi
   227  
   228  if ! type docker > /dev/null 2>&1; then
   229      echo -e '\n(*) Warning: The docker command was not found.\n\nYou can use one of the following scripts to install it:\n\nhttps://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/docker-in-docker.md\n\nor\n\nhttps://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/docker.md'
   230  fi
   231  
   232  echo -e "\nDone!"