github.com/cptmikhailov/conmon@v2.0.20+incompatible/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 # Under some contexts these values are not set, make sure they are. 7 export USER="$(whoami)" 8 export HOME="$(getent passwd $USER | cut -d : -f 6)" 9 10 # These are normally set by cirrus, but can't be for VMs setup by hack/get_ci_vm.sh 11 # Pick some reasonable defaults 12 ENVLIB=${ENVLIB:-.bash_profile} 13 CIRRUS_WORKING_DIR="${CIRRUS_WORKING_DIR:-/var/tmp/go/src/github.com/containers/libpod}" 14 SCRIPT_BASE=${SCRIPT_BASE:-./contrib/cirrus} 15 PACKER_BASE=${PACKER_BASE:-./contrib/cirrus/packer} 16 CIRRUS_REPO_NAME=${CIRRUS_REPO_NAME-$(dirname $0)} 17 CIRRUS_BUILD_ID=${CIRRUS_BUILD_ID:-DEADBEEF} # a human 18 CIRRUS_BASE_SHA=${CIRRUS_BASE_SHA:-HEAD} 19 CIRRUS_CHANGE_IN_REPO=${CIRRUS_CHANGE_IN_REPO:-FETCH_HEAD} 20 21 if ! [[ "$PATH" =~ "/usr/local/bin" ]] 22 then 23 export PATH="$PATH:/usr/local/bin" 24 fi 25 26 # In ci/testing environment, ensure variables are always loaded 27 if [[ -r "$HOME/$ENVLIB" ]] && [[ -n "$CI" ]] 28 then 29 # Make sure this is always loaded 30 source "$HOME/$ENVLIB" 31 fi 32 33 # Pass in a line delimited list of, space delimited name/value pairs 34 # exit non-zero with helpful error message if any value is empty 35 req_env_var() { 36 echo "$1" | while read NAME VALUE 37 do 38 if [[ -n "$NAME" ]] && [[ -z "$VALUE" ]] 39 then 40 echo "Required env. var. \$$NAME is not set" 41 exit 9 42 fi 43 done 44 } 45 46 # Some env. vars may contain secrets. Display values for known "safe" 47 # and useful variables. 48 # ref: https://cirrus-ci.org/guide/writing-tasks/#environment-variables 49 show_env_vars() { 50 # This is almost always multi-line, print it separately 51 echo "export CIRRUS_CHANGE_MESSAGE=$CIRRUS_CHANGE_MESSAGE" 52 echo " 53 BUILDTAGS $BUILDTAGS 54 BUILT_IMAGE_SUFFIX $BUILT_IMAGE_SUFFIX 55 CI $CI 56 CIRRUS_CI $CIRRUS_CI 57 CI_NODE_INDEX $CI_NODE_INDEX 58 CI_NODE_TOTAL $CI_NODE_TOTAL 59 CONTINUOUS_INTEGRATION $CONTINUOUS_INTEGRATION 60 CIRRUS_BASE_BRANCH $CIRRUS_BASE_BRANCH 61 CIRRUS_BASE_SHA $CIRRUS_BASE_SHA 62 CIRRUS_BRANCH $CIRRUS_BRANCH 63 CIRRUS_BUILD_ID $CIRRUS_BUILD_ID 64 CIRRUS_CHANGE_IN_REPO $CIRRUS_CHANGE_IN_REPO 65 CIRRUS_CLONE_DEPTH $CIRRUS_CLONE_DEPTH 66 CIRRUS_DEFAULT_BRANCH $CIRRUS_DEFAULT_BRANCH 67 CIRRUS_PR $CIRRUS_PR 68 CIRRUS_TAG $CIRRUS_TAG 69 CIRRUS_OS $CIRRUS_OS 70 OS $OS 71 CIRRUS_TASK_NAME $CIRRUS_TASK_NAME 72 CIRRUS_TASK_ID $CIRRUS_TASK_ID 73 CIRRUS_REPO_NAME $CIRRUS_REPO_NAME 74 CIRRUS_REPO_OWNER $CIRRUS_REPO_OWNER 75 CIRRUS_REPO_FULL_NAME $CIRRUS_REPO_FULL_NAME 76 CIRRUS_REPO_CLONE_URL $CIRRUS_REPO_CLONE_URL 77 CIRRUS_SHELL $CIRRUS_SHELL 78 CIRRUS_USER_COLLABORATOR $CIRRUS_USER_COLLABORATOR 79 CIRRUS_USER_PERMISSION $CIRRUS_USER_PERMISSION 80 CIRRUS_WORKING_DIR $CIRRUS_WORKING_DIR 81 CIRRUS_HTTP_CACHE_HOST $CIRRUS_HTTP_CACHE_HOST 82 $(go env) 83 PACKER_BUILDS $PACKER_BUILDS 84 " | while read NAME VALUE 85 do 86 [[ -z "$NAME" ]] || echo "export $NAME=\"$VALUE\"" 87 done 88 } 89 90 # Return a GCE image-name compatible string representation of distribution name 91 os_release_id() { 92 eval "$(egrep -m 1 '^ID=' /etc/os-release | tr -d \' | tr -d \")" 93 echo "$ID" 94 } 95 96 # Return a GCE image-name compatible string representation of distribution major version 97 os_release_ver() { 98 eval "$(egrep -m 1 '^VERSION_ID=' /etc/os-release | tr -d \' | tr -d \")" 99 echo "$VERSION_ID" | cut -d '.' -f 1 100 } 101 102 bad_os_id_ver() { 103 echo "Unknown/Unsupported distro. $OS_RELEASE_ID and/or version $OS_RELEASE_VER for $ARGS" 104 exit 42 105 } 106 107 stub() { 108 echo "STUB: Pretending to do $1" 109 } 110 111 # Helper/wrapper script to only show stderr/stdout on non-zero exit 112 install_ooe() { 113 req_env_var " 114 SRC $SRC 115 SCRIPT_BASE $SCRIPT_BASE 116 " 117 echo "Installing script to mask stdout/stderr unless non-zero exit." 118 sudo install -D -m 755 "$SRC/$SCRIPT_BASE/ooe.sh" /usr/local/bin/ooe.sh 119 } 120 121 # Grab a newer version of git from software collections 122 # https://www.softwarecollections.org/en/ 123 # and use it with a wrapper 124 install_scl_git() { 125 echo "Installing SoftwareCollections updated 'git' version." 126 ooe.sh sudo yum -y install rh-git29 127 cat << "EOF" | sudo tee /usr/bin/git 128 #!/bin/bash 129 130 scl enable rh-git29 -- git $@ 131 EOF 132 sudo chmod 755 /usr/bin/git 133 } 134 135 _finalize(){ 136 set +e # Don't fail at the very end 137 set +e # make errors non-fatal 138 echo "Removing leftover giblets from cloud-init" 139 cd / 140 sudo rm -rf /var/lib/cloud/instance? 141 sudo rm -rf /root/.ssh/* 142 sudo rm -rf /home/* 143 sudo rm -rf /tmp/* 144 sudo rm -rf /tmp/.??* 145 sync 146 sudo fstrim -av 147 } 148 149 rh_finalize(){ 150 set +e # Don't fail at the very end 151 # Allow root ssh-logins 152 if [[ -r /etc/cloud/cloud.cfg ]] 153 then 154 sudo sed -re 's/^disable_root:.*/disable_root: 0/g' -i /etc/cloud/cloud.cfg 155 fi 156 echo "Resetting to fresh-state for usage as cloud-image." 157 PKG=$(type -P dnf || type -P yum || echo "") 158 [[ -z "$PKG" ]] || sudo $PKG clean all # not on atomic 159 sudo rm -rf /var/cache/{yum,dnf} 160 sudo rm -f /etc/udev/rules.d/*-persistent-*.rules 161 sudo touch /.unconfigured # force firstboot to run 162 _finalize 163 } 164 165 setup_gopath() { 166 req_env_var " 167 CRIO_REPO $CRIO_REPO 168 CRIO_SLUG $CRIO_SLUG 169 CONMON_SLUG $CONMON_SLUG 170 " 171 echo "Configuring persistent Go environment for all users" 172 sudo mkdir -p /var/tmp/go/src 173 sudo chown -R $USER:$USER /var/tmp/go 174 sudo chmod g=rws /var/tmp/go 175 ENVLIB=/etc/profile.d/go.sh 176 # configure GOPATH if not set 177 if ! grep -q GOPATH $ENVLIB 178 then 179 sudo tee "$ENVLIB" << EOF 180 export GOPATH=/var/tmp/go 181 export PATH=\$PATH:\$GOPATH/bin 182 EOF 183 source $ENVLIB 184 fi 185 186 # configure CRIO_SRC if not set 187 if ! grep -q CRIO_SRC $ENVLIB 188 then 189 sudo tee "$ENVLIB" << EOF 190 export CRIO_SRC=\$GOPATH/src/$CRIO_SLUG 191 EOF 192 source $ENVLIB 193 fi 194 } 195 196 install_crio_repo() { 197 req_env_var " 198 GOPATH $GOPATH 199 CRIO_SRC $CRIO_SRC 200 CRIO_REPO $CRIO_REPO 201 " 202 echo "Cloning current CRI-O Source for faster access later" 203 sudo rm -rf "$CRIO_SRC" # just in case 204 ooe.sh git clone $CRIO_REPO $CRIO_SRC 205 206 # Install CRI-O 207 cd crio 208 ooe.sh make PREFIX=/usr 209 ooe.sh sudo make install PREFIX=/usr 210 } 211 212 install_testing_deps() { 213 req_env_var " 214 GOPATH $GOPATH 215 CRIO_SRC $CRIO_SRC 216 " 217 218 echo "Installing required go packages into \$GOPATH" 219 for toolpath in \ 220 tools/godep \ 221 onsi/ginkgo \ 222 onsi/gomega \ 223 cloudflare/cfssl/cmd/... \ 224 jteeuwen/go-bindata/go-bindata \ 225 cpuguy83/go-md2man \ 226 urfave/cli \ 227 containers/image/storage 228 do 229 go get -d "github.com/$toolpath" 230 done 231 232 echo "Installing latest upstream version of BATS" 233 ooe.sh git clone https://github.com/bats-core/bats-core.git /tmp/bats 234 cd /tmp/bats 235 ooe.sh ./install.sh /usr 236 rm -rf /tmp/bats 237 238 echo "Installing helper script for CNI plugin test" 239 cd "$CRIO_SRC" 240 sudo mkdir -p /opt/cni/bin/ 241 # Search path for helper is difficult to control 242 ooe.sh sudo install -D -m 0755 test/cni_plugin_helper.bash /usr/libexec/cni/ 243 # Helper hard-codes cni binary path :( 244 ooe.sh sudo ln -fv /usr/libexec/cni/* /opt/cni/bin/ 245 246 echo "Installing registry configuration" 247 sudo mkdir -p /etc/containers/registries.d/ 248 ooe.sh sudo install -D -m 0644 test/redhat_sigstore.yaml \ 249 /etc/containers/registries.d/registry.access.redhat.com.yaml 250 } 251 252 # Needed for e2e tests 253 selinux_permissive(){ 254 echo "Entering SELinux Permissive mode, will switch to enforcing upon shell exit" 255 trap "setenforce 1" EXIT 256 setenforce 0 257 } 258 259 build_and_replace_conmon() { 260 req_env_var " 261 SRC $SRC 262 " 263 264 NEWNAME=.original_packaged_conmon 265 echo "Renaming conmon binaries from RPMs" 266 find /usr -type f -name conmon | 267 while read CONMON_FILEPATH 268 do 269 NEWPATH="$(dirname $CONMON_FILEPATH)/$NEWNAME" 270 [[ -r "$NEWPATH" ]] || sudo mv -v "$CONMON_FILEPATH" "$NEWPATH" 271 done 272 273 echo "Building conmon" 274 cd $SRC 275 276 ooe.sh make 277 echo "Installing conmon" 278 ooe.sh sudo make crio PREFIX=/usr 279 # Use same version for podman in case ever needed 280 ooe.sh sudo ln -fv /usr/libexec/crio/conmon /usr/libexec/podman/conmon 281 ooe.sh sudo restorecon -R /usr/bin 282 }