1#!/bin/bash -xe
2
3# This script is for running rootfs_size.py in Jenkins using docker.
4#
5# This script will build a docker container which will then be used to build
6# and run the rootfs_size.py script.
7#
8#   WORKSPACE:       Required, location of unit test scripts and repository
9#                    code to test
10#   SQUASHFS_FILE:   Required, The squashfs file name to run rootfs_size
11#                    against
12#   DISTRO:          Optional, docker base image (ubuntu or fedora)
13#   DOCKER_IMG_NAME: Optional, default is openbmc/ubuntu-rootfs-size
14
15# Trace bash processing. Set -e so when a step fails, we fail the build
16set -uo pipefail
17
18# Default variables
19DOCKER_IMG_NAME=${DOCKER_IMG_NAME:-"openbmc/ubuntu-rootfs-size"}
20DISTRO=${DISTRO:-ubuntu:bionic}
21OBMC_BUILD_SCRIPTS="openbmc-build-scripts"
22OBMC_TOOLS="openbmc-tools"
23ROOTFS_SIZE_PY_DIR="rootfs_size"
24ROOTFS_SIZE_PY="rootfs_size.py"
25
26# Timestamp for job
27echo "rootfs_size build started, $(date)"
28
29if [[ "${DISTRO}" == "fedora" ]]; then
30    echo "Distro (${DISTRO}) not supported, running as ubuntu"
31    DISTRO="ubuntu:bionic"
32fi
33
34# Check workspace, build scripts exist
35if [ ! -d "${WORKSPACE}" ]; then
36    echo "Workspace(${WORKSPACE}) doesn't exist, exiting..."
37    exit 1
38fi
39
40if [ ! -e "${WORKSPACE}/${SQUASHFS_FILE}" ]; then
41    echo "${WORKSPACE}/${SQUASHFS_FILE} doesn't exist, exiting..."
42    exit 1
43fi
44
45if [ ! -d "${WORKSPACE}/${OBMC_BUILD_SCRIPTS}" ]; then
46    echo "Clone (${OBMC_BUILD_SCRIPTS}) in ${WORKSPACE}..."
47    git clone https://gerrit.openbmc.org/openbmc/${OBMC_BUILD_SCRIPTS} "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}
48fi
49
50if [ ! -d "${WORKSPACE}/${OBMC_TOOLS}" ]; then
51    echo "Clone (${OBMC_TOOLS}) in ${WORKSPACE}..."
52    git clone https://gerrit.openbmc.org/openbmc/${OBMC_TOOLS} "${WORKSPACE}"/${OBMC_TOOLS}
53fi
54
55# Copy rootfs_size.py script into workspace
56cp "${WORKSPACE}"/${OBMC_TOOLS}/${ROOTFS_SIZE_PY_DIR}/${ROOTFS_SIZE_PY} \
57    "${WORKSPACE}"/${ROOTFS_SIZE_PY}
58chmod a+x "${WORKSPACE}"/${ROOTFS_SIZE_PY}
59
60# Configure docker build
61cd "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}
62echo "Building docker image with build-rootfs-size-docker.sh"
63
64# Export input env variables
65export DOCKER_IMG_NAME
66export DISTRO
67./build-rootfs-size-docker.sh
68
69# Run the docker container with the rootfs_size execution script
70echo "Executing docker image"
71docker run --cap-add=sys_admin --rm=true \
72    --network host \
73    --privileged=true \
74    -u "$USER" \
75    -w "${WORKSPACE}" -v "${WORKSPACE}":"${WORKSPACE}" \
76    -t "${DOCKER_IMG_NAME}" \
77    "${WORKSPACE}"/${ROOTFS_SIZE_PY} --build_dir "${WORKSPACE}"/ \
78    --squashfs_file "${SQUASHFS_FILE}"
79
80# Timestamp for build
81echo "rootfs_size build completed, $(date)"
82