github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/contrib/cirrus/lib.sh (about)

     1  
     2  
     3  # Library of common, shared utility functions.  This file is intended
     4  # to be sourced by other scripts, not called directly.
     5  
     6  # BEGIN Global export of all variables
     7  set -a
     8  
     9  # Due to differences across platforms and runtime execution environments,
    10  # handling of the (otherwise) default shell setup is non-uniform.  Rather
    11  # than attempt to workaround differences, simply force-load/set required
    12  # items every time this library is utilized.
    13  _waserrexit=0
    14  if [[ "$SHELLOPTS" =~ errexit ]]; then _waserrexit=1; fi
    15  set +e  # Assumed in F33 for setting global vars
    16  source /etc/profile
    17  source /etc/environment
    18  if [[ -r "/etc/ci_environment" ]]; then source /etc/ci_environment; fi
    19  USER="$(whoami)"
    20  HOME="$(getent passwd $USER | cut -d : -f 6)"
    21  # Some platforms set and make this read-only
    22  [[ -n "$UID" ]] || \
    23      UID=$(getent passwd $USER | cut -d : -f 3)
    24  if ((_waserrexit)); then set -e; fi
    25  
    26  # During VM Image build, the 'containers/automation' installation
    27  # was performed.  The final step of installation sets the library
    28  # location $AUTOMATION_LIB_PATH in /etc/environment or in the
    29  # default shell profile depending on distribution.
    30  # shellcheck disable=SC2154
    31  if [[ -n "$AUTOMATION_LIB_PATH" ]]; then
    32          # shellcheck source=/usr/share/automation/lib/common_lib.sh
    33          source $AUTOMATION_LIB_PATH/common_lib.sh
    34  else
    35      (
    36      echo "WARNING: It does not appear that containers/automation was installed."
    37      echo "         Functionality of most of this library will be negatively impacted"
    38      echo "         This ${BASH_SOURCE[0]} was loaded by ${BASH_SOURCE[1]}"
    39      ) > /dev/stderr
    40  fi
    41  
    42  OS_RELEASE_ID="$(source /etc/os-release; echo $ID)"
    43  # GCE image-name compatible string representation of distribution _major_ version
    44  OS_RELEASE_VER="$(source /etc/os-release; echo $VERSION_ID | cut -d '.' -f 1)"
    45  # Combined to ease soe usage
    46  OS_REL_VER="${OS_RELEASE_ID}-${OS_RELEASE_VER}"
    47  # This is normally set from .cirrus.yml but default is necessary when
    48  # running under hack/get_ci_vm.sh since it cannot infer the value.
    49  DISTRO_NV="${DISTRO_NV:-$OS_REL_VER}"
    50  
    51  # Essential default paths, many are overridden when executing under Cirrus-CI
    52  GOPATH="${GOPATH:-/var/tmp/go}"
    53  if type -P go &> /dev/null
    54  then
    55      # Cirrus-CI caches $GOPATH contents
    56      export GOCACHE="${GOCACHE:-$GOPATH/cache/go-build}"
    57      # called processes like `make` and other tools need these vars.
    58      eval "export $(go env)"
    59  
    60      # Ensure compiled tooling is reachable
    61      PATH="$PATH:$GOPATH/bin:$HOME/.local/bin"
    62  fi
    63  CIRRUS_WORKING_DIR="${CIRRUS_WORKING_DIR:-$(realpath $(dirname ${BASH_SOURCE[0]})/../../)}"
    64  GOSRC="${GOSRC:-$CIRRUS_WORKING_DIR}"
    65  PATH="$HOME/bin:/usr/local/bin:$PATH"
    66  LD_LIBRARY_PATH="/usr/local/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
    67  
    68  # Saves typing / in case location ever moves
    69  SCRIPT_BASE=${SCRIPT_BASE:-./contrib/cirrus}
    70  
    71  # Downloaded, but not installed packages.
    72  PACKAGE_DOWNLOAD_DIR=/var/cache/download
    73  
    74  # Log remote-client system test varlink output here
    75  PODMAN_SERVER_LOG=$CIRRUS_WORKING_DIR/varlink.log
    76  
    77  # Defaults when not running under CI
    78  export CI="${CI:-false}"
    79  CIRRUS_CI="${CIRRUS_CI:-false}"
    80  DEST_BRANCH="${DEST_BRANCH:-master}"
    81  CONTINUOUS_INTEGRATION="${CONTINUOUS_INTEGRATION:-false}"
    82  CIRRUS_REPO_NAME=${CIRRUS_REPO_NAME:-podman}
    83  # Cirrus only sets $CIRRUS_BASE_SHA properly for PRs, but $EPOCH_TEST_COMMIT
    84  # needs to be set from this value in order for `make validate` to run properly.
    85  # When running get_ci_vm.sh, most $CIRRUS_xyz variables are empty. Attempt
    86  # to accomidate both branch and get_ci_vm.sh testing by discovering the base
    87  # branch SHA value.
    88  # shellcheck disable=SC2154
    89  if [[ -z "$CIRRUS_BASE_SHA" ]] && [[ -z "$CIRRUS_TAG" ]]
    90  then  # Operating on a branch, or under `get_ci_vm.sh`
    91      CIRRUS_BASE_SHA=$(git rev-parse ${UPSTREAM_REMOTE:-origin}/$DEST_BRANCH)
    92  elif [[ -z "$CIRRUS_BASE_SHA" ]]
    93  then  # Operating on a tag
    94      CIRRUS_BASE_SHA=$(git rev-parse HEAD)
    95  fi
    96  # The starting place for linting and code validation
    97  EPOCH_TEST_COMMIT="$CIRRUS_BASE_SHA"
    98  
    99  # Regex defining all CI-releated env. vars. necessary for all possible
   100  # testing operations on all platforms and versions.  This is necessary
   101  # to avoid needlessly passing through global/system values across
   102  # contexts, such as host->container or root->rootless user
   103  PASSTHROUGH_ENV_RE='(^CI.*)|(^CIRRUS)|(^DISTRO_NV)|(^GOPATH)|(^GOCACHE)|(^GOSRC)|(^SCRIPT_BASE)|(CGROUP_MANAGER)|(OCI_RUNTIME)|(^TEST.*)|(^PODBIN_NAME)|(^PRIV_NAME)|(^ALT_NAME)|(^ROOTLESS_USER)|(SKIP_USERNS)|(.*_NAME)|(.*_FQIN)'
   104  # Unsafe env. vars for display
   105  SECRET_ENV_RE='(ACCOUNT)|(GC[EP]..+)|(SSH)|(PASSWORD)|(TOKEN)'
   106  
   107  # Type of filesystem used for cgroups
   108  CG_FS_TYPE="$(stat -f -c %T /sys/fs/cgroup)"
   109  
   110  # Set to 1 in all podman container images
   111  CONTAINER="${CONTAINER:-0}"
   112  
   113  # END Global export of all variables
   114  set +a
   115  
   116  lilto() { err_retry 8 1000 "" "$@"; }  # just over 4 minutes max
   117  bigto() { err_retry 7 5670 "" "$@"; }  # 12 minutes max
   118  
   119  # Print shell-escaped variable=value pairs, one per line, based on
   120  # variable name matching a regex.  This is intended to catch
   121  # variables being passed down from higher layers, like Cirrus-CI.
   122  passthrough_envars(){
   123      local xchars
   124      local envname
   125      local envval
   126      # Avoid values containing entirely punctuation|control|whitespace
   127      xchars='[:punct:][:cntrl:][:space:]'
   128      warn "Will pass env. vars. matching the following regex:
   129      $PASSTHROUGH_ENV_RE"
   130      for envname in $(awk 'BEGIN{for(v in ENVIRON) print v}' | \
   131                           grep -Ev "SETUP_ENVIRONMENT" | \
   132                           grep -Ev "$SECRET_ENV_RE" | \
   133                           grep -E "$PASSTHROUGH_ENV_RE"); do
   134  
   135              envval="${!envname}"
   136              [[ -n $(tr -d "$xchars" <<<"$envval") ]] || continue
   137  
   138              # Properly escape values to prevent injection
   139              printf -- "$envname=%q\n" "$envval"
   140      done
   141  }
   142  
   143  setup_rootless() {
   144      req_env_vars ROOTLESS_USER GOPATH GOSRC SECRET_ENV_RE
   145  
   146      local rootless_uid
   147      local rootless_gid
   148      local env_var_val
   149  
   150      # Only do this once; established by setup_environment.sh
   151      # shellcheck disable=SC2154
   152      if passwd --status $ROOTLESS_USER
   153      then
   154          msg "Updating $ROOTLESS_USER user permissions on possibly changed libpod code"
   155          chown -R $ROOTLESS_USER:$ROOTLESS_USER "$GOPATH" "$GOSRC"
   156          return 0
   157      fi
   158      msg "************************************************************"
   159      msg "Setting up rootless user '$ROOTLESS_USER'"
   160      msg "************************************************************"
   161      cd $GOSRC || exit 1
   162      # Guarantee independence from specific values
   163      rootless_uid=$[RANDOM+1000]
   164      rootless_gid=$[RANDOM+1000]
   165      msg "creating $rootless_uid:$rootless_gid $ROOTLESS_USER user"
   166      groupadd -g $rootless_gid $ROOTLESS_USER
   167      useradd -g $rootless_gid -u $rootless_uid --no-user-group --create-home $ROOTLESS_USER
   168      chown -R $ROOTLESS_USER:$ROOTLESS_USER "$GOPATH" "$GOSRC"
   169  
   170      msg "creating ssh key pair for $USER"
   171      [[ -r "$HOME/.ssh/id_rsa" ]] || \
   172          ssh-keygen -P "" -f "$HOME/.ssh/id_rsa"
   173  
   174      msg "Allowing ssh key for $ROOTLESS_USER"
   175      (umask 077 && mkdir "/home/$ROOTLESS_USER/.ssh")
   176      chown -R $ROOTLESS_USER:$ROOTLESS_USER "/home/$ROOTLESS_USER/.ssh"
   177      install -o $ROOTLESS_USER -g $ROOTLESS_USER -m 0600 \
   178          "$HOME/.ssh/id_rsa.pub" "/home/$ROOTLESS_USER/.ssh/authorized_keys"
   179      # Makes debugging easier
   180      cat /root/.ssh/authorized_keys >> "/home/$ROOTLESS_USER/.ssh/authorized_keys"
   181  
   182      msg "Configuring subuid and subgid"
   183      grep -q "${ROOTLESS_USER}" /etc/subuid || \
   184          echo "${ROOTLESS_USER}:$[rootless_uid * 100]:65536" | \
   185              tee -a /etc/subuid >> /etc/subgid
   186  
   187      msg "Ensure the ssh daemon is up and running within 5 minutes"
   188      systemctl start sshd
   189      lilto ssh $ROOTLESS_USER@localhost \
   190             -o UserKnownHostsFile=/dev/null \
   191             -o StrictHostKeyChecking=no \
   192             -o CheckHostIP=no true
   193  }
   194  
   195  install_test_configs() {
   196      echo "Installing cni config, policy and registry config"
   197      req_env_vars GOSRC SCRIPT_BASE
   198      cd $GOSRC || exit 1
   199      install -v -D -m 644 ./cni/87-podman-bridge.conflist /etc/cni/net.d/
   200      # This config must always sort last in the list of networks (podman picks first one
   201      # as the default).  This config prevents allocation of network address space used
   202      # by default in google cloud.  https://cloud.google.com/vpc/docs/vpc#ip-ranges
   203      install -v -D -m 644 $SCRIPT_BASE/99-do-not-use-google-subnets.conflist /etc/cni/net.d/
   204  
   205      install -v -D -m 644 ./test/registries.conf /etc/containers/
   206  }
   207  
   208  # Remove all files provided by the distro version of podman.
   209  # All VM cache-images used for testing include the distro podman because (1) it's
   210  # required for podman-in-podman testing and (2) it somewhat simplifies the task
   211  # of pulling in necessary prerequisites packages as the set can change over time.
   212  # For general CI testing however, calling this function makes sure the system
   213  # can only run the compiled source version.
   214  remove_packaged_podman_files() {
   215      echo "Removing packaged podman files to prevent conflicts with source build and testing."
   216      req_env_vars OS_RELEASE_ID
   217  
   218      # If any binaries are resident they could cause unexpected pollution
   219      for unit in io.podman.service io.podman.socket
   220      do
   221          for state in enabled active
   222          do
   223              if systemctl --quiet is-$state $unit
   224              then
   225                  echo "Warning: $unit found $state prior to packaged-file removal"
   226                  systemctl --quiet disable $unit || true
   227                  systemctl --quiet stop $unit || true
   228              fi
   229          done
   230      done
   231  
   232      if [[ "$OS_RELEASE_ID" =~ "ubuntu" ]]
   233      then
   234          LISTING_CMD="dpkg-query -L podman"
   235      else
   236          LISTING_CMD="rpm -ql podman"
   237      fi
   238  
   239      # yum/dnf/dpkg may list system directories, only remove files
   240      $LISTING_CMD | while read fullpath
   241      do
   242          # Sub-directories may contain unrelated/valuable stuff
   243          if [[ -d "$fullpath" ]]; then continue; fi
   244          ooe.sh rm -vf "$fullpath"
   245      done
   246  
   247      # Be super extra sure and careful vs performant and completely safe
   248      sync && echo 3 > /proc/sys/vm/drop_caches || true
   249  }