github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/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 USER="$(whoami)" 14 HOME="$(getent passwd $USER | cut -d : -f 6)" 15 # Some platforms set and make this read-only 16 [[ -n "$UID" ]] || \ 17 UID=$(getent passwd $USER | cut -d : -f 3) 18 19 # Automation library installed at image-build time, 20 # defining $AUTOMATION_LIB_PATH in this file. 21 if [[ -r "/etc/automation_environment" ]]; then 22 source /etc/automation_environment 23 fi 24 # shellcheck disable=SC2154 25 if [[ -n "$AUTOMATION_LIB_PATH" ]]; then 26 # shellcheck source=/usr/share/automation/lib/common_lib.sh 27 source $AUTOMATION_LIB_PATH/common_lib.sh 28 else 29 ( 30 echo "WARNING: It does not appear that containers/automation was installed." 31 echo " Functionality of most of this library will be negatively impacted" 32 echo " This ${BASH_SOURCE[0]} was loaded by ${BASH_SOURCE[1]}" 33 ) > /dev/stderr 34 fi 35 36 # Managed by setup_environment.sh; holds task-specific definitions. 37 if [[ -r "/etc/ci_environment" ]]; then source /etc/ci_environment; fi 38 39 # This is normally set from .cirrus.yml but default is necessary when 40 # running under hack/get_ci_vm.sh since it cannot infer the value. 41 DISTRO_NV="${DISTRO_NV:-$OS_REL_VER}" 42 43 # Essential default paths, many are overridden when executing under Cirrus-CI 44 GOPATH="${GOPATH:-/var/tmp/go}" 45 if type -P go &> /dev/null 46 then 47 # Cirrus-CI caches $GOPATH contents 48 export GOCACHE="${GOCACHE:-$GOPATH/cache/go-build}" 49 # called processes like `make` and other tools need these vars. 50 eval "export $(go env)" 51 52 # Ensure compiled tooling is reachable 53 PATH="$PATH:$GOPATH/bin:$HOME/.local/bin" 54 fi 55 CIRRUS_WORKING_DIR="${CIRRUS_WORKING_DIR:-$(realpath $(dirname ${BASH_SOURCE[0]})/../../)}" 56 GOSRC="${GOSRC:-$CIRRUS_WORKING_DIR}" 57 PATH="$HOME/bin:/usr/local/bin:$PATH" 58 LD_LIBRARY_PATH="/usr/local/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}" 59 60 # Saves typing / in case location ever moves 61 SCRIPT_BASE=${SCRIPT_BASE:-./contrib/cirrus} 62 63 # Downloaded, but not installed packages. 64 PACKAGE_DOWNLOAD_DIR=/var/cache/download 65 66 # Log remote-client system test server output here 67 PODMAN_SERVER_LOG=$CIRRUS_WORKING_DIR/server.log 68 69 # Defaults when not running under CI 70 export CI="${CI:-false}" 71 CIRRUS_CI="${CIRRUS_CI:-false}" 72 CONTINUOUS_INTEGRATION="${CONTINUOUS_INTEGRATION:-false}" 73 CIRRUS_REPO_NAME=${CIRRUS_REPO_NAME:-podman} 74 # Cirrus only sets $CIRRUS_BASE_SHA properly for PRs, but $EPOCH_TEST_COMMIT 75 # needs to be set from this value in order for `make validate` to run properly. 76 # When running get_ci_vm.sh, most $CIRRUS_xyz variables are empty. Attempt 77 # to accomidate both branch and get_ci_vm.sh testing by discovering the base 78 # branch SHA value. 79 # shellcheck disable=SC2154 80 if [[ -z "$CIRRUS_BASE_SHA" ]] && [[ -z "$CIRRUS_TAG" ]] 81 then # Operating on a branch, or under `get_ci_vm.sh` 82 CIRRUS_BASE_SHA=$(git rev-parse ${UPSTREAM_REMOTE:-origin}/$DEST_BRANCH) 83 elif [[ -z "$CIRRUS_BASE_SHA" ]] 84 then # Operating on a tag 85 CIRRUS_BASE_SHA=$(git rev-parse HEAD) 86 fi 87 # The starting place for linting and code validation 88 EPOCH_TEST_COMMIT="$CIRRUS_BASE_SHA" 89 90 # Regex defining all CI-related env. vars. necessary for all possible 91 # testing operations on all platforms and versions. This is necessary 92 # to avoid needlessly passing through global/system values across 93 # contexts, such as host->container or root->rootless user 94 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)|(NETWORK_BACKEND)|(DEST_BRANCH)' 95 # Unsafe env. vars for display 96 SECRET_ENV_RE='(ACCOUNT)|(GC[EP]..+)|(SSH)|(PASSWORD)|(TOKEN)' 97 98 # Type of filesystem used for cgroups 99 CG_FS_TYPE="$(stat -f -c %T /sys/fs/cgroup)" 100 101 # Set to 1 in all podman container images 102 CONTAINER="${CONTAINER:-0}" 103 104 # END Global export of all variables 105 set +a 106 107 lilto() { err_retry 8 1000 "" "$@"; } # just over 4 minutes max 108 bigto() { err_retry 7 5670 "" "$@"; } # 12 minutes max 109 110 # Print shell-escaped variable=value pairs, one per line, based on 111 # variable name matching a regex. This is intended to catch 112 # variables being passed down from higher layers, like Cirrus-CI. 113 passthrough_envars(){ 114 local xchars 115 local envname 116 local envval 117 # Avoid values containing entirely punctuation|control|whitespace 118 xchars='[:punct:][:cntrl:][:space:]' 119 warn "Will pass env. vars. matching the following regex: 120 $PASSTHROUGH_ENV_RE" 121 for envname in $(awk 'BEGIN{for(v in ENVIRON) print v}' | \ 122 grep -Ev "SETUP_ENVIRONMENT" | \ 123 grep -Ev "$SECRET_ENV_RE" | \ 124 grep -E "$PASSTHROUGH_ENV_RE"); do 125 126 envval="${!envname}" 127 [[ -n $(tr -d "$xchars" <<<"$envval") ]] || continue 128 129 # Properly escape values to prevent injection 130 printf -- "$envname=%q\n" "$envval" 131 done 132 } 133 134 setup_rootless() { 135 req_env_vars GOPATH GOSRC SECRET_ENV_RE 136 137 ROOTLESS_USER="${ROOTLESS_USER:-some${RANDOM}dude}" 138 139 local rootless_uid 140 local rootless_gid 141 local env_var_val 142 local akfilepath 143 local sshcmd 144 145 # Only do this once; established by setup_environment.sh 146 # shellcheck disable=SC2154 147 if passwd --status $ROOTLESS_USER 148 then 149 if [[ $PRIV_NAME = "rootless" ]]; then 150 msg "Updating $ROOTLESS_USER user permissions on possibly changed libpod code" 151 chown -R $ROOTLESS_USER:$ROOTLESS_USER "$GOPATH" "$GOSRC" 152 return 0 153 fi 154 fi 155 msg "************************************************************" 156 msg "Setting up rootless user '$ROOTLESS_USER'" 157 msg "************************************************************" 158 cd $GOSRC || exit 1 159 # Guarantee independence from specific values 160 rootless_uid=$[RANDOM+1000] 161 rootless_gid=$[RANDOM+1000] 162 msg "creating $rootless_uid:$rootless_gid $ROOTLESS_USER user" 163 groupadd -g $rootless_gid $ROOTLESS_USER 164 useradd -g $rootless_gid -u $rootless_uid --no-user-group --create-home $ROOTLESS_USER 165 166 echo "$ROOTLESS_USER ALL=(root) NOPASSWD: ALL" > /etc/sudoers.d/ci-rootless 167 168 mkdir -p "$HOME/.ssh" "/home/$ROOTLESS_USER/.ssh" 169 170 msg "Creating ssh key pairs" 171 [[ -r "$HOME/.ssh/id_rsa" ]] || \ 172 ssh-keygen -t rsa -P "" -f "$HOME/.ssh/id_rsa" 173 ssh-keygen -t ed25519 -P "" -f "/home/$ROOTLESS_USER/.ssh/id_ed25519" 174 ssh-keygen -t rsa -P "" -f "/home/$ROOTLESS_USER/.ssh/id_rsa" 175 176 msg "Setup authorized_keys" 177 cat $HOME/.ssh/*.pub /home/$ROOTLESS_USER/.ssh/*.pub >> $HOME/.ssh/authorized_keys 178 cat $HOME/.ssh/*.pub /home/$ROOTLESS_USER/.ssh/*.pub >> /home/$ROOTLESS_USER/.ssh/authorized_keys 179 180 msg "Configure ssh file permissions" 181 chmod -R 700 "$HOME/.ssh" 182 chmod -R 700 "/home/$ROOTLESS_USER/.ssh" 183 chown -R $ROOTLESS_USER:$ROOTLESS_USER "/home/$ROOTLESS_USER/.ssh" 184 185 # N/B: We're clobbering the known_hosts here on purpose. There should 186 # never be any non-localhost connections made from tests (using strict-mode). 187 # If there are, it's either a security problem or a broken test, both of which 188 # we want to lead to test failures. 189 msg " setup known_hosts for $USER" 190 ssh-keyscan localhost > /root/.ssh/known_hosts 191 msg " setup known_hosts for $ROOTLESS_USER" 192 # Maintain access-permission consistency with all other .ssh files. 193 install -Z -m 700 -o $ROOTLESS_USER -g $ROOTLESS_USER \ 194 /root/.ssh/known_hosts /home/$ROOTLESS_USER/.ssh/known_hosts 195 } 196 197 install_test_configs() { 198 msg "Installing ./test/registries.conf system-wide." 199 install -v -D -m 644 ./test/registries.conf /etc/containers/ 200 } 201 202 use_cni() { 203 msg "Unsetting NETWORK_BACKEND for all subsequent environments." 204 echo "export -n NETWORK_BACKEND" >> /etc/ci_environment 205 echo "unset NETWORK_BACKEND" >> /etc/ci_environment 206 export -n NETWORK_BACKEND 207 unset NETWORK_BACKEND 208 msg "Installing default CNI configuration" 209 cd $GOSRC || exit 1 210 rm -rvf /etc/cni/net.d 211 mkdir -p /etc/cni/net.d 212 install -v -D -m 644 ./cni/87-podman-bridge.conflist \ 213 /etc/cni/net.d/ 214 # This config must always sort last in the list of networks (podman picks 215 # first one as the default). This config prevents allocation of network 216 # address space used by default in google cloud. 217 # https://cloud.google.com/vpc/docs/vpc#ip-ranges 218 install -v -D -m 644 $SCRIPT_BASE/99-do-not-use-google-subnets.conflist \ 219 /etc/cni/net.d/ 220 } 221 222 use_netavark() { 223 msg "Forcing NETWORK_BACKEND=netavark for all subsequent environments." 224 echo "NETWORK_BACKEND=netavark" >> /etc/ci_environment 225 export NETWORK_BACKEND=netavark # needed for install_test_configs() 226 msg "Removing any/all CNI configuration" 227 rm -rvf /etc/cni/net.d/* 228 } 229 230 # Remove all files provided by the distro version of podman. 231 # All VM cache-images used for testing include the distro podman because (1) it's 232 # required for podman-in-podman testing and (2) it somewhat simplifies the task 233 # of pulling in necessary prerequisites packages as the set can change over time. 234 # For general CI testing however, calling this function makes sure the system 235 # can only run the compiled source version. 236 remove_packaged_podman_files() { 237 echo "Removing packaged podman files to prevent conflicts with source build and testing." 238 req_env_vars OS_RELEASE_ID 239 240 # If any binaries are resident they could cause unexpected pollution 241 for unit in io.podman.service io.podman.socket 242 do 243 for state in enabled active 244 do 245 if systemctl --quiet is-$state $unit 246 then 247 echo "Warning: $unit found $state prior to packaged-file removal" 248 systemctl --quiet disable $unit || true 249 systemctl --quiet stop $unit || true 250 fi 251 done 252 done 253 254 # OS_RELEASE_ID is defined by automation-library 255 # shellcheck disable=SC2154 256 if [[ "$OS_RELEASE_ID" =~ "ubuntu" ]] 257 then 258 LISTING_CMD="dpkg-query -L podman" 259 else 260 LISTING_CMD="rpm -ql podman" 261 fi 262 263 # yum/dnf/dpkg may list system directories, only remove files 264 $LISTING_CMD | while read fullpath 265 do 266 # Sub-directories may contain unrelated/valuable stuff 267 if [[ -d "$fullpath" ]]; then continue; fi 268 ooe.sh rm -vf "$fullpath" 269 done 270 271 # Be super extra sure and careful vs performant and completely safe 272 sync && echo 3 > /proc/sys/vm/drop_caches || true 273 }