Skip to content

Commit

Permalink
Add pr_check.sh script and refactor
Browse files Browse the repository at this point in the history
- Adds the pr_check.sh for running the project required validation checks.
- Adds the test.sh script which contains logic to set up and run integration tests
- Update docker-compose to use Quay repository images
- Updates Dockerfile to add multi-stage builds
- adds build-deploy logic to generate images
- Adds tests to cover #2 and #3
  • Loading branch information
Victoremepunto committed Oct 15, 2021
1 parent 482f71a commit 61c5dc8
Show file tree
Hide file tree
Showing 21 changed files with 641 additions and 144 deletions.
12 changes: 10 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
FROM registry.access.redhat.com/ubi8/python-39
FROM registry.access.redhat.com/ubi8/python-39 as base

# Add application sources to a directory that the assemble script expects them
# and set permissions so that the container runs without root access
USER 0
ADD app-src /tmp/src
ADD setup.py requirements.txt /tmp/src/
ADD src /tmp/src/src

RUN /usr/bin/fix-permissions /tmp/src
USER 1001

Expand All @@ -12,3 +14,9 @@ RUN /usr/libexec/s2i/assemble

# Set the default command for the resulting image
CMD /usr/libexec/s2i/run

FROM base as test

ADD tests/test_* tests/floorplan_* tests/requirements.txt ./tests/

RUN pip install -r tests/requirements.txt
11 changes: 11 additions & 0 deletions build_deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -exv

source 'deployment/build-deploy-common.sh'

BACKWARDS_COMPATIBILITY_TAGS="latest"
IMAGE_NAME="${IMAGE_NAME:-quay.io/cloudservices/floorist}"
BUILD_DEPLOY_BUILD_TARGET="${BUILD_DEPLOY_BUILD_TARGET:-base}"

build_deploy_main || exit 1
254 changes: 254 additions & 0 deletions deployment/build-deploy-common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
#!/usr/bin/env bash

BUILD_DEPLOY_WORKDIR=$(pwd)
BACKWARDS_COMPATIBILITY="${BACKWARDS_COMPATIBILITY:-true}"
BACKWARDS_COMPATIBILITY_TAGS="latest qa"
REQUIRED_REGISTRIES="quay redhat"
REQUIRED_REGISTRIES_LOCAL="redhat"
LOCAL_BUILD="${LOCAL_BUILD:-false}"
DOCKER_CONF="$BUILD_DEPLOY_WORKDIR/.docker"
DOCKERFILE=${DOCKERFILE:="${BUILD_DEPLOY_WORKDIR}/Dockerfile"}
REDHAT_REGISTRY="${REDHAT_REGISTRY:-registry.redhat.io}"
QUAY_REGISTRY="${QUAY_REGISTRY:-quay.io}"
BUILD_DEPLOY_TEMP_IMAGE="${BUILD_DEPLOY_TEMP_IMAGE:-false}"
CONTAINER_ENGINE_CMD=''
BUILD_ARGS=''

local_build() {
[ "$LOCAL_BUILD" = true ]
}

temporary_image() {
[ "$BUILD_DEPLOY_TEMP_IMAGE" = true ]
}

build_target() {
[ -n "$BUILD_DEPLOY_BUILD_TARGET" ]
}

backwards_compatibility_enabled() {
[ "$BACKWARDS_COMPATIBILITY" = true ]
}

is_ci_runner() {
[[ "$(hostname)" == "ci-"* ]]
}

get_7_chars_commit_hash() {
echo "$(git rev-parse --short=7 HEAD)"
}

_check_command_is_present() {
command -v "$1" >/dev/null
}

login_container_registry() {

local USER="$1"
local PASSWORD="$2"
local REGISTRY="$3"

container_engine_cmd login "-u=${USER}" "--password-stdin" "$REGISTRY" <<< "$PASSWORD"
}

login_quay_registry() {
login_container_registry "$QUAY_USER" "$QUAY_TOKEN" "$QUAY_REGISTRY"
}

login_redhat_registry() {
login_container_registry "$RH_REGISTRY_USER" "$RH_REGISTRY_TOKEN" "$REDHAT_REGISTRY"
}

login_container_registry_type() {

local REGISTRY="$1"

if [ "$REGISTRY" = 'quay' ]; then
login_quay_registry || return 1
elif [ "$REGISTRY" = 'redhat' ]; then
login_redhat_registry || return 1
else
echo "unsupported registry '$REGISTRY'"
return 1
fi
}

login_to_required_registries() {

for REGISTRY in $REQUIRED_REGISTRIES_LOCAL; do
if ! login_container_registry_type "$REGISTRY"; then
echo "Error while attempting to log into '${REGISTRY}' registry"
return 1
fi
done

if ! local_build; then
for REGISTRY in $REQUIRED_REGISTRIES; do
if ! login_container_registry_type "$REGISTRY"; then
echo "Error while attempting to log into '${REGISTRY}' registry"
return 1
fi
done
fi
}

check_quay_registry_credentials() {
[ -n "$QUAY_USER" ] && [ -n "$QUAY_TOKEN" ]
}

check_rh_registry_credentials() {
[ -n "$RH_REGISTRY_USER" ] && [ -n "$RH_REGISTRY_TOKEN" ]
}

check_registry_credentials() {

local REGISTRY="$1"

if [ "$REGISTRY" = 'quay' ]; then
check_quay_registry_credentials || return 1
elif [ "$REGISTRY" = 'redhat' ]; then
check_rh_registry_credentials || return 1
else
echo "unsupported registry '$REGISTRY'"
return 1
fi
}

check_required_registry_credentials() {

for REGISTRY in $REQUIRED_REGISTRIES_LOCAL; do
if ! check_registry_credentials "$REGISTRY"; then
echo "Error checking environment for ${REGISTRY} registry credentials"
return 1
fi
done

if ! local_build; then
for REGISTRY in $REQUIRED_REGISTRIES; do
if ! check_registry_credentials "$REGISTRY"; then
echo "Error checking environment for ${REGISTRY} registry credentials"
return 1
fi
done
fi
}

container_engine_cmd() {

if [ "$CONTAINER_ENGINE_CMD" = "podman" ]; then
podman "$@"
else
docker "--config=${DOCKER_CONF}" "$@"
fi
}

initialize_container_engine_cmd() {

if _check_command_is_present podman && ! is_ci_runner; then
CONTAINER_ENGINE_CMD='podman'
else
mkdir -p "$DOCKER_CONF"
CONTAINER_ENGINE_CMD='docker'
fi
}

_get_build_args() {

local tmp=''

for BUILD_ARG in $BUILD_ARGS; do
tmp="${tmp} --build-arg $BUILD_ARG"
done

echo "$tmp"
}

build_image() {

local BUILD_ARGS_CMD=''
local IMAGE_LABELS=''
local BUILD_TARGET=''

if temporary_image; then
IMAGE_LABELS='--label quay.expires-after=3d'
fi

if build_target; then
BUILD_TARGET="--target $BUILD_DEPLOY_BUILD_TARGET"
fi

if [ -n "$BUILD_ARGS" ]; then
BUILD_ARGS_CMD=$(_get_build_args)
container_engine_cmd build --pull -f "$DOCKERFILE" $BUILD_ARGS_CMD -t "${IMAGE_NAME}:${IMAGE_TAG}" $BUILD_TARGET $IMAGE_LABELS .
else
container_engine_cmd build --pull -f "$DOCKERFILE" -t "${IMAGE_NAME}:${IMAGE_TAG}" $BUILD_TARGET $IMAGE_LABELS .
fi

}

push_image() {

local IMAGE_TAG="$1"

container_engine_cmd push "${IMAGE_NAME}:${IMAGE_TAG}"
}

tag_image() {

local NEW_IMAGE_TAG="$1"

container_engine_cmd tag "${IMAGE_NAME}:${IMAGE_TAG}" "${IMAGE_NAME}:${NEW_IMAGE_TAG}"
}

tag_and_push_for_backwards_compatibility() {

for TAG in $BACKWARDS_COMPATIBILITY_TAGS; do
tag_image "$TAG"
if ! local_build; then
push_image "$TAG"
fi
done
}

build_deploy_init() {
check_required_registry_credentials || return 1
initialize_container_engine_cmd || return 1
login_to_required_registries || return 1

# TODO - validate some image related variables ? wrap this into function
if [ -z "$IMAGE_NAME" ]; then
echo "you must define IMAGE_NAME"
return 1
fi

if [ ! -r "$DOCKERFILE" ]; then
echo "ERROR: No ${DOCKERFILE} found or not readable"
return 1
fi
}

build_deploy_main() {

if ! build_deploy_init; then
echo "build_deploy init phase failed!"
return 1
fi

if ! build_image; then
echo "failed building image!"
return 1
fi

if ! local_build; then
push_image "$IMAGE_TAG"
fi

# To enable backwards compatibility with ci, qa, and smoke, always push latest and qa tags
if backwards_compatibility_enabled; then
tag_and_push_for_backwards_compatibility
fi
}

IMAGE_TAG=$(get_7_chars_commit_hash)
IMAGE_NAME=''
ADITIONAL_TAGS=''
24 changes: 24 additions & 0 deletions pr_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

# TODO: Decide wether if this is required or not
# Install bonfire repo/initialize
#CICD_URL=https://raw.githubusercontent.com/RedHatInsights/bonfire/master/cicd
#curl -s $CICD_URL/bootstrap.sh > .cicd_bootstrap.sh && source .cicd_bootstrap.sh
APP_ROOT=${APP_ROOT:-`pwd`}
# --------------------------------------------
# Options that must be configured by app owner
# --------------------------------------------

APP_NAME="floorist" # name of app-sre "application" folder this component lives in
COMPONENT_NAME="floorist" # name of app-sre "resourceTemplate" in deploy.yaml for this component

cat /etc/redhat-release

BUILD_DEPLOY_BUILD_TARGET="test"
BUILD_DEPLOY_TEMP_IMAGE=true

source "$APP_ROOT/build_deploy.sh" || exit 1

mkdir -p artifacts

source "$APP_ROOT/run-tests.sh"
File renamed without changes.
Loading

0 comments on commit 61c5dc8

Please sign in to comment.