From 5638e18196be1fabd9e78d4c506402bf700fe569 Mon Sep 17 00:00:00 2001
From: Frantisek Sumsal <fsumsal@redhat.com>
Date: Mon, 7 Jan 2019 15:49:45 +0100
Subject: [PATCH] travis: enable Travis CI on CentOS 7

(cherry picked from commit 2014cb51b6dfe1f7f0b98e62311398c2bf801c2b)
---
 .travis.yml         | 86 ++++++++++-----------------------------------
 ci/travis-centos.sh | 69 ++++++++++++++++++++++++++++++++++++
 ci/travis_wait.bash | 61 ++++++++++++++++++++++++++++++++
 3 files changed, 149 insertions(+), 67 deletions(-)
 create mode 100755 ci/travis-centos.sh
 create mode 100644 ci/travis_wait.bash

diff --git a/.travis.yml b/.travis.yml
index d980038181..fc63887324 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,77 +1,29 @@
 sudo: required
-
 services:
     - docker
 
-language: c
+env:
+    global:
+        - CI_ROOT="$TRAVIS_BUILD_DIR/ci/"
 
 jobs:
     include:
-        - stage: coverity scan
-          before_script:
-              - sudo apt-get update
+        - stage: Build & test
+          name: CentOS 7
+          language: bash
+          env:
+              - CENTOS_RELEASE="centos7"
+              - CONT_NAME="systemd-centos-$CENTOS_RELEASE"
+              - DOCKER_EXEC="docker exec -ti $CONT_NAME"
+          before_install:
               - sudo apt-get -y -o Dpkg::Options::="--force-confnew" install docker-ce
               - docker --version
-              - env > .env
-          env:
-              - COVERITY_SCAN_PROJECT_NAME="$TRAVIS_REPO_SLUG"
-              - COVERITY_SCAN_NOTIFICATION_EMAIL="${AUTHOR_EMAIL}"
-              - COVERITY_SCAN_BRANCH_PATTERN="$TRAVIS_BRANCH"
-              # Encrypted token for systemd/systemd Coverity Scan Analysis,
-              # generated by "travis encrypt -r systemd/systemd COVERITY_SCAN_TOKEN=<TOKEN>"
-              - secure: "lM0IVP2zOG5Ywk3YCbDCQL4WioyzzwtdtpZ+hKDy4BWCZDBJ/FVwIeBsXdMDvlTa3xi+GQ1b7kS2OmTfmG4aSlhU7isuH8SMq1Y4GR5AxfhkR+irUA1A1fntlvhbjIumDGW5wjs0Dt8KogMWS+ZD4eGE59lrVO/TrhMzIe1eHENVLFQJdNq+ZJXU8wxMfHf8lXk0xA8SJTid0XvZBNc4JN6pjJRA8LaOrMNhQYfygFmVQ598kwlu7gf5vbCKFPnIgJAxdIhz12XS9utGohV28IYj9d1DdUGUT+ar3OfADj3X8KFBP4Ymc02pcln3wVgdPtrDbFZh1R9jbmfdXGAH/6tTOJVn8aFySS2Vq9QiBiprWdPsAOLcWMNhnp0lMkASxs9/W26nU7Czo8VbAVWXM1w35plDpnDGR6lk/06dmOZpqu5p3AYr5xIKACIAdPDn0rNpnSWqC750WZ8ZWbHnKuZC5TWML7scVaPiEi7D7rbwqML2rdwx4ZoTZmCHiGByXCIWTfhf0JNQAix5WW3znl+BmDesumPgPj2mX+y6J1WYJrIz12m7qh7KhV/a1ODKM+I91A9rkOA/bPnmhmSSUR7CwgvZt1fC/VwBnaFFtAz9/70kN9Q8tDBXtXidExZwh1e3t5vDG72k3lXwNqpKRvdW3LOxK6lFvqEdMWVUJls="
+          install:
+              - $CI_ROOT/travis-centos.sh SETUP
           script:
-                # Copy content of CI_DIR into WORKDIR
-              - find $CI_DIR -maxdepth 1 -type f -exec cp -t . {} +
-                # Build container for current user
-              - $CI_SCRIPT_DIR/build-docker-image.sh
-
-                # For kernel version 4.8+
-              - sudo sysctl vsyscall=emulate || true
-                # Prepare environment for Coverity tool
-              - |
-                  PLATFORM=`uname`
-                  export TOOL_BASE="/tmp/coverity-scan-analysis"
-                  export SCAN_URL="https://scan.coverity.com"
-                  export UPLOAD_URL="https://scan.coverity.com/builds"
-                  export TOOL_ARCHIVE="/tmp/cov-analysis-${PLATFORM}.tgz"
-
-                # Get Coverity tool
-              - $CI_TOOL_DIR/get-coverity.sh
-              - TOOL_DIR="$(find $TOOL_BASE -type d -name 'cov-analysis*')"
-
-                # Export env variables for Coverity scan
-              - env | grep -E "TRAVIS|COV|TOOL|URL" > .cov-env
-              - |
-                  docker run -dit --env-file .cov-env \
-                  -v ${TOOL_BASE}:${TOOL_BASE}:ro \
-                  --name travis_coverity_scan coverity-${TRAVIS_COMMIT}:latest bash
-                # Make sure Coverity script is executable
-              - docker cp tools/coverity.sh travis_coverity_scan:/usr/local/bin
-                # Preconfigure with meson to prevent Coverity from capturing meson metadata
-                # Set compiler flag to prevent emit failure
-              - docker exec -it travis_coverity_scan sh -c "CFLAGS='-D_Float128=long\ double -D_Float64=double -D_Float64x=long\ double -D_Float32=float -D_Float32x=double' meson cov-build -Dman=false"
-                # Run Coverity Analysis
-              - docker exec -it travis_coverity_scan coverity.sh build
-              - docker exec -it travis_coverity_scan coverity.sh upload
-
-# Specify the order of stages and conditions
-stages:
-    - name: coverity scan
-      if: type = cron
-
-env:
-    global:
-        - ADMIN_EMAIL=macermak@redhat.com
-
-        - AUTHOR_NAME="$(git log -1 $TRAVIS_COMMIT --pretty=\"%aN\")"
-        - AUTHOR_EMAIL="$(git log -1 $TRAVIS_COMMIT --pretty=\"%aE\")"
-
-        - CI_DIR="$TRAVIS_BUILD_DIR/travis-ci"
-        - CI_TOOL_DIR="$CI_DIR/tools"
-        - CI_SCRIPT_DIR="$CI_DIR/scripts"
-
-notifications:
-  email:
-    recipients:
-      - ${ADMIN_EMAIL}
+              - set -e
+              # Build systemd
+              - $CI_ROOT/travis-centos.sh RUN
+              - set +e
+          after_script:
+              - $CI_ROOT/travis-centos.sh CLEANUP
diff --git a/ci/travis-centos.sh b/ci/travis-centos.sh
new file mode 100755
index 0000000000..60bbdf14c2
--- /dev/null
+++ b/ci/travis-centos.sh
@@ -0,0 +1,69 @@
+#!/bin/bash
+
+# Run this script from the root of the systemd's git repository
+# or set REPO_ROOT to a correct path.
+#
+# Example execution on Fedora:
+# dnf install docker
+# systemctl start docker
+# export CONT_NAME="my-fancy-container"
+# ci/travis-centos.sh SETUP RUN CLEANUP
+
+PHASES=(${@:-SETUP RUN CLEANUP})
+CENTOS_RELEASE="${CENTOS_RELEASE:-latest}"
+CONT_NAME="${CONT_NAME:-centos-$CENTOS_RELEASE-$RANDOM}"
+DOCKER_EXEC="${DOCKER_EXEC:-docker exec -it $CONT_NAME}"
+DOCKER_RUN="${DOCKER_RUN:-docker run}"
+REPO_ROOT="${REPO_ROOT:-$PWD}"
+ADDITIONAL_DEPS=(yum-utils iputils hostname libasan libubsan clang llvm)
+
+function info() {
+    echo -e "\033[33;1m$1\033[0m"
+}
+
+set -e
+
+source "$(dirname $0)/travis_wait.bash"
+
+for phase in "${PHASES[@]}"; do
+    case $phase in
+        SETUP)
+            info "Setup phase"
+            info "Using Travis $CENTOS_RELEASE"
+            # Pull a Docker image and start a new container
+            docker pull centos:$CENTOS_RELEASE
+            info "Starting container $CONT_NAME"
+            $DOCKER_RUN -v $REPO_ROOT:/build:rw \
+                        -w /build --privileged=true --name $CONT_NAME \
+                        -dit --net=host centos:$CENTOS_RELEASE /sbin/init
+            # Beautiful workaround for Fedora's version of Docker
+            sleep 1
+            $DOCKER_EXEC yum makecache
+            # Install necessary build/test requirements
+            $DOCKER_EXEC yum -y --exclude selinux-policy\* upgrade
+            $DOCKER_EXEC yum -y install "${ADDITIONAL_DEPS[@]}"
+            $DOCKER_EXEC yum-builddep -y systemd
+            ;;
+        RUN)
+            info "Run phase"
+            # Build systemd
+            $DOCKER_EXEC ./autogen.sh
+            $DOCKER_EXEC ./configure --disable-timesyncd --disable-kdbus --disable-terminal \
+                                     --enable-gtk-doc --enable-compat-libs --disable-sysusers \
+                                     --disable-ldconfig --enable-lz4 --with-sysvinit-path=/etc/rc.d/init.d
+            $DOCKER_EXEC make
+            if ! $DOCKER_EXEC make check; then
+                $DOCKER_EXEC cat test-suite.log
+                exit 1
+            fi
+            ;;
+        CLEANUP)
+            info "Cleanup phase"
+            docker stop $CONT_NAME
+            docker rm -f $CONT_NAME
+            ;;
+        *)
+            echo >&2 "Unknown phase '$phase'"
+            exit 1
+    esac
+done
diff --git a/ci/travis_wait.bash b/ci/travis_wait.bash
new file mode 100644
index 0000000000..acf6ad15e4
--- /dev/null
+++ b/ci/travis_wait.bash
@@ -0,0 +1,61 @@
+# This was borrowed from https://github.com/travis-ci/travis-build/tree/master/lib/travis/build/bash
+# to get around https://github.com/travis-ci/travis-ci/issues/9979. It should probably be removed
+# as soon as Travis CI has started to provide an easy way to export the functions to bash scripts.
+
+travis_jigger() {
+  local cmd_pid="${1}"
+  shift
+  local timeout="${1}"
+  shift
+  local count=0
+
+  echo -e "\\n"
+
+  while [[ "${count}" -lt "${timeout}" ]]; do
+    count="$((count + 1))"
+    echo -ne "Still running (${count} of ${timeout}): ${*}\\r"
+    sleep 60
+  done
+
+  echo -e "\\n${ANSI_RED}Timeout (${timeout} minutes) reached. Terminating \"${*}\"${ANSI_RESET}\\n"
+  kill -9 "${cmd_pid}"
+}
+
+travis_wait() {
+  local timeout="${1}"
+
+  if [[ "${timeout}" =~ ^[0-9]+$ ]]; then
+    shift
+  else
+    timeout=20
+  fi
+
+  local cmd=("${@}")
+  local log_file="travis_wait_${$}.log"
+
+  "${cmd[@]}" &>"${log_file}" &
+  local cmd_pid="${!}"
+
+  travis_jigger "${!}" "${timeout}" "${cmd[@]}" &
+  local jigger_pid="${!}"
+  local result
+
+  {
+    set +e
+    wait "${cmd_pid}" 2>/dev/null
+    result="${?}"
+    ps -p"${jigger_pid}" &>/dev/null && kill "${jigger_pid}"
+    set -e
+  }
+
+  if [[ "${result}" -eq 0 ]]; then
+    echo -e "\\n${ANSI_GREEN}The command ${cmd[*]} exited with ${result}.${ANSI_RESET}"
+  else
+    echo -e "\\n${ANSI_RED}The command ${cmd[*]} exited with ${result}.${ANSI_RESET}"
+  fi
+
+  echo -e "\\n${ANSI_GREEN}Log:${ANSI_RESET}\\n"
+  cat "${log_file}"
+
+  return "${result}"
+}
