github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/tests/build-and-run-tests.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  set -ex
     4  
     5  # Clean up environment after build. It is flushing every assigned IP address via IPAM, umounting every
     6  # mountpoint and removing unused links
     7  function cleanup {
     8      if [[ "${POSTCLEANUP}" == true ]]; then
     9          if [[ "${CI}" == true || "${FORCE}" == true ]]; then
    10              for mp in $(mount | grep rkt | awk '{print $3}' | tac); do
    11                  sudo umount "${mp}"
    12              done
    13  
    14              for link in $(ip link | grep rkt | cut -d':' -f2 | cut -d'@' -f1); do
    15                  sudo ip link del "${link}"
    16              done
    17              sudo rm -rf /var/lib/cni/networks/*
    18          fi
    19          sudo rm -rf "${BUILD_DIR}"
    20      fi
    21  }
    22  
    23  # Skip build on demand. It requires the `last-commit` file inside the last commit.
    24  function ciSkip {
    25      cat last-commit
    26      echo
    27      echo "Build skipped as requested in the last commit."
    28      exit 0
    29  }
    30  
    31  # Finds the branching point of two commits.
    32  # For example, let B and D be two commits, and their ancestry graph as A -> B, A -> C -> D.
    33  # Given commits B and D, it returns A.
    34  function getBranchingPoint {
    35      diff --old-line-format='' --new-line-format='' \
    36          <(git rev-list --first-parent "${1:-$1}") \
    37              <(git rev-list --first-parent "${2:-$2}") | head -1
    38  }
    39  
    40  # Configure Semaphore CI environment.
    41  function semaphoreCIConfiguration {
    42      # We might not need to run functional tests or process docs.
    43      # This is best-effort; || true ensures this does not affect test outcome
    44      # First, ensure origin is updated - Semaphore can do some weird caching
    45      git fetch || true
    46      BRANCHING_POINT=$(getBranchingPoint HEAD origin/master)
    47      SRC_CHANGES=$(git diff-tree --no-commit-id --name-only -r HEAD..${BRANCHING_POINT} | grep -cEv ${DOC_CHANGE_PATTERN}) || true
    48      DOC_CHANGES=$(git diff-tree --no-commit-id --name-only -r HEAD..${BRANCHING_POINT} | grep -cE ${DOC_CHANGE_PATTERN}) || true
    49  
    50      # Set up go environment on Semaphore
    51      if [ -f /opt/change-go-version.sh ]; then
    52          . /opt/change-go-version.sh
    53          change-go-version 1.7
    54  
    55  	# systemd v229 doesn't build on gcc-4.8, set the compiler to gcc-5
    56          export CC=gcc-5
    57      fi
    58  }
    59  
    60  function checkFlavorValue {
    61      FLAVORS="coreos host kvm none src fly"
    62      if [ -z "${RKT_STAGE1_USR_FROM}" ]; then
    63          set -
    64          echo "Flavor is not set"
    65          exit 1
    66      fi
    67      if ! [[ "${FLAVORS}" =~ "${RKT_STAGE1_USR_FROM}" ]]; then
    68          set -
    69          echo "Unknown flavor: ${RKT_STAGE1_USR_FROM}"
    70          echo "Available flavors: ${FLAVORS}"
    71          exit 1
    72      fi
    73  }
    74  
    75  # Parse user provided parameters
    76  function parseParameters {
    77      while getopts "f:s:r:cxujdv" option; do
    78          case ${option} in
    79          f)
    80              RKT_STAGE1_USR_FROM="${OPTARG}"
    81              ;;
    82          s)
    83              if [[ $RKT_STAGE1_USR_FROM == "src" ]]; then
    84                  RKT_STAGE1_SYSTEMD_VER="${OPTARG}"
    85              else
    86                  echo "Only \`src\` flavor requires systemd version"
    87              fi
    88              ;;
    89          r)
    90              if [[ $RKT_STAGE1_USR_FROM == "src" ]]; then
    91                  RKT_STAGE1_SYSTEMD_REV="${OPTARG}"
    92              else
    93                  echo "Only \`src\` flavor requires systemd revision"
    94              fi
    95              ;;
    96          x)
    97              FORCE=true
    98              ;;
    99          u)
   100              set -
   101              usage
   102              exit 0
   103              ;;
   104          c)
   105              PRECLEANUP=true
   106              POSTCLEANUP=true
   107              ;;
   108          j)
   109              JUSTBUILD=true
   110              ;;
   111          d)
   112              DIRTYBUILD=true
   113              ;;
   114          v)
   115              VERBOSITY="V=3"
   116              ;;
   117          \?)
   118              set -
   119              echo "Invalid parameter -${OPTARG}"
   120              usage
   121              exit 1
   122              ;;
   123          esac
   124      done
   125      checkFlavorValue
   126  }
   127  
   128  # Configure build
   129  function configure {
   130      case "${RKT_STAGE1_USR_FROM}" in
   131          coreos|kvm|fly)
   132          ./configure --with-stage1-flavors="${RKT_STAGE1_USR_FROM}" \
   133                  --with-stage1-default-flavor="${RKT_STAGE1_USR_FROM}" \
   134                  --enable-functional-tests --enable-tpm=auto \
   135                  --enable-insecure-go
   136          ;;
   137          host)
   138          ./configure --with-stage1-flavors=host \
   139                  --with-default-stage1-flavor=host \
   140                  --enable-functional-tests=auto --enable-tpm=auto \
   141                  --enable-insecure-go
   142          ;;
   143          src)
   144          ./configure --with-stage1-flavors="${RKT_STAGE1_USR_FROM}" \
   145                  --with-stage1-default-flavor="${RKT_STAGE1_USR_FROM}" \
   146                  --with-stage1-systemd-version="${RKT_STAGE1_SYSTEMD_VER:-v999}" \
   147                  --with-stage1-systemd-revision="${RKT_STAGE1_SYSTEMD_REV:-master}" \
   148                  --enable-functional-tests --enable-tpm=auto \
   149                  --enable-insecure-go
   150          ;;
   151          none)
   152          # Not a flavor per se, so perform a detailed setup for some
   153          # hypothetical 3rd party stage1 image
   154          ./configure --with-stage1-default-name="example.com/some-stage1-for-rkt" \
   155                  --with-stage1-default-version="0.0.1" --enable-tpm=auto \
   156                  --enable-insecure-go
   157          ;;
   158          *)
   159          echo "Unknown flavor: ${RKT_STAGE1_USR_FROM}"
   160          exit 1
   161          ;;
   162      esac
   163  }
   164  
   165  # Build rkt and run unit & functional tests
   166  function build {
   167      ./autogen.sh
   168  
   169      configure
   170  
   171      CORES=$(grep -c ^processor /proc/cpuinfo)
   172      echo "Running make with ${CORES} threads"
   173      make "-j${CORES}" ${VERBOSITY}
   174      make manpages bash-completion
   175  
   176      if [[ ${PRECLEANUP} == true ]]; then
   177          rm -rf "${BUILD_DIR}/tmp/usr_from_${RKT_STAGE1_USR_FROM}"
   178      fi
   179      if [[ ${JUSTBUILD} != true ]]; then
   180          make ${VERBOSITY} check
   181          make "-j${CORES}" clean
   182      fi
   183  }
   184  
   185  # Prepare build directory name
   186  function buildFolder {
   187      if [[ "${RKT_STAGE1_USR_FROM}" == "src" ]]; then
   188          POSTFIX="-${RKT_STAGE1_SYSTEMD_VER}"
   189      fi
   190      BUILD_DIR="build-rkt-${RKT_STAGE1_USR_FROM}${POSTFIX}"
   191  }
   192  
   193  # Detect changes from last commit. If there is no changes, there is no
   194  # need to run build
   195  function detectChanges {
   196      HEAD=`git rev-parse HEAD`
   197      MASTER=`git rev-parse origin/master`
   198      if [[ ${HEAD} == ${MASTER} ]]; then
   199          SRC_CHANGES=1
   200          DOC_CHANGES=1
   201      elif [[ ${SRC_CHANGES} -eq 0 && ${DOC_CHANGES} -eq 0 ]]; then
   202          echo "No changes detected and HEAD is not origin/master"
   203          exit 0
   204      fi
   205  }
   206  
   207  # Copy source code into build directory
   208  function copyCode {
   209      if [[ $(whereis -b rsync | awk '{print $2}') != "" ]]; then
   210          rsync -aq ../ ${BUILD_DIR} --exclude=".git*" --exclude=builds --exclude-from=../.gitignore
   211      else
   212          echo "Cannot find `rsync`, which is required by this shell script"
   213          exit 1
   214      fi
   215  }
   216  
   217  # Set source code into build directory and enter into it
   218  function setCodeInBuildEnv {
   219      if [[ ${DIRTYBUILD} == '' ]]; then
   220          detectChanges
   221      fi
   222      copyCode
   223      pushd "${BUILD_DIR}"
   224  }
   225  
   226  # Show usage
   227  function usage {
   228      echo "build-and-run-tests.sh usage:"
   229      echo -e "-c\tCleanup"
   230      echo -e "-d\tUse unsaved changes for build"
   231      echo -e "-f\tSelect flavor"
   232      echo -e "-j\tDon't run tests after build"
   233      echo -e "-s\tSystemd version"
   234      echo -e "-r\tSystemd revision"
   235      echo -e "-u\tShow this message"
   236      echo -e "-v\tRun make with verbose flag"
   237      echo -e "-x\tUse with '-c' to force cleanup on non-CI systems"
   238  }
   239  
   240  # Prepare build environment
   241  function prepareBuildEnv {
   242      # In case it wasn't cleaned up
   243      if [ -e "builds/${BUILD_DIR}" ]; then
   244          sudo rm -rf "builds/${BUILD_DIR}"
   245      fi
   246      mkdir -p builds
   247  }
   248  
   249  # Run docs scan
   250  function docsScan {
   251      :
   252      # echo Changes in docs detected, checking docs.
   253      # TODO check for broken links
   254      # TODO check for obvious spelling mistakes:
   255          # coreos -> CoreOS
   256          # More?!
   257  }
   258  
   259  function main {
   260      # Skip build if requested
   261      if test -e ci-skip ; then
   262          ciSkip
   263      fi
   264  
   265      SRC_CHANGES=1 # run functional tests by default
   266      DOC_CHANGES=1 # process docs by default
   267  
   268      parseParameters "${@}"
   269  
   270      DOC_CHANGE_PATTERN="\
   271              -e ^Documentation/ \
   272              -e ^dist/ \
   273              -e ^logos/ \
   274              -e ^(MAINTAINERS|LICENSE|DCO)$ \
   275              -e \.md$\
   276              -e \.(jpeg|jpg|png|svg)$\
   277      "
   278  
   279      buildFolder
   280  
   281      # https://semaphoreci.com/docs/available-environment-variables.html
   282      if [ "${SEMAPHORE-}" == true ] ; then
   283          semaphoreCIConfiguration
   284      fi
   285  
   286      prepareBuildEnv
   287      cd builds
   288      setCodeInBuildEnv
   289  
   290      if [ ${SRC_CHANGES} -gt 0 ]; then
   291          build
   292      fi
   293      if [ ${DOC_CHANGES} -gt 0 ]; then
   294          docsScan
   295      fi
   296      cleanup
   297  }
   298  
   299  main "${@}"