<commit_before>#!/bin/bash
# vi: fdm=marker

set -e

### Constants ###

SCRIPT_NAME="${0##*/}"
TRUE='true'
FALSE='false'
DFT_DOCKER_PUSH_ENABLED=$TRUE

### Functions ###

function log() {
  echo -e "$@" >&2
}

function error {
	local msg=$1
	local code=$2
	[[ -z $code ]] && code=1
	log "[ERROR] $msg"

	exit $code
}


function debug {
    local msg=$1
    local level=$2
    [[ -n $level ]] || level=1

    [[ $DEBUG -lt $level ]] || log "[DEBUG] $msg"
}

function warning {
    local msg=$1

    log "[WARNING] ##### $msg #####"
}

function print_help {
    (
        echo "Usage: $SCRIPT_NAME [options]"
        echo
        echo "Build Galaxy docker compose images for different purposes."
        echo
        echo "Options:"
        echo "   -g, --debug                    Debug mode. [false]"
        echo "   -h, --help                     Print this help message."
        echo "   -p, --push                     Push docker images. You can also set environment variable DOCKER_PUSH_ENABLED to \"true\". [true]"
        echo "   +p, --no-push                  Do not push dockers. You can also set environment variable DOCKER_PUSH_ENABLED to \"false\"."
        echo "       --push-intermediate        Also push intermediate images [false]"
        echo "       --no-cache                 Tell Docker not to use cached images. [false]"
        echo "       --init-tag       <tag>     Set the tag for the Galaxy Init Flavour container image."
        echo "       --postgres-tag   <tag>     Set the tag for the Galaxy Postgres container image."
        echo "       --proftpd-tag    <tag>     Set the tag for the Galaxy Proftpd container image."
        echo "       --web-tag        <tag>     Set the tag for the Galaxy Web k8s container image."
        echo "       --k8s                      Set settings for Kubernetes usage."
        echo "       --grafana                  Build grafana container."
        echo "       --condor                   Build condor containers."
        echo "       --slurm                    Build slurm containers."
        echo "   -u, --container-user <user>    Set the container user. You can also set the environment variable CONTAINER_USER."
        echo "   -r, --container-registry <reg> Set the container registry. You can also set the environment variable CONTAINER_REGISTRY."
        echo
    ) >&2
}

function read_args {

    local args="$*" # save arguments for debugging purpose

    # Read options
    while [[ $# > 0 ]] ; do
        shift_count=1
        case $1 in
            -g|--debug)              DEBUG=$((DEBUG + 1)) ;;
            -h|--help)               print_help ; exit 0 ;;
            -p|--push)               DOCKER_PUSH_ENABLED=$TRUE ;;
            +p|--no-push)            DOCKER_PUSH_ENABLED=$FALSE ;;
            --k8s)                   BUILD_FOR_K8S=$TRUE ;;
            --condor)                BUILD_FOR_CONDOR=$TRUE ;;
            --slurm)                 BUILD_FOR_SLURM=$TRUE ;;
            --grafana)               BUILD_FOR_GRAFANA=$TRUE ;;
            --push-intermediate)     PUSH_INTERMEDIATE_IMAGES=$TRUE ;;
            --no-cache)              NO_CACHE="--no-cache" ;;
            --postgres-tag)          OVERRIDE_POSTGRES_TAG="$2" ; shift_count=2 ;;
            --proftpd-tag)           OVERRIDE_PROFTPD_TAG="$2" ; shift_count=2 ;;
            --init-tag)              OVERRIDE_GALAXY_INIT_PHENO_FLAVOURED_TAG="$2" ; shift_count=2 ;;
            --web-tag)               OVERRIDE_GALAXY_WEB_TAG="$2" ; shift_count=2 ;;
            -u|--container-user)     CONTAINER_USER="$2" ; shift_count=2 ;;
            -r|--container-registry) CONTAINER_REGISTRY="$2" ; shift_count=2 ;;
            *) error "Illegal option $1." 98 ;;
        esac
        shift $shift_count
    done

    # Debug messages
    debug "Command line arguments: $args"
    debug "Argument DEBUG=$DEBUG"
    debug "Argument DOCKER_PUSH_ENABLED=$DOCKER_PUSH_ENABLED"
    debug "Argument DOCKER_USER=$DOCKER_USER"
    debug "Argument OVERRIDE_GALAXY_WEB_TAG=$OVERRIDE_GALAXY_WEB_TAG"
    debug "Argument OVERRIDE_POSTGRES_TAG=$OVERRIDE_POSTGRES_TAG"
    debug "Argument OVERRIDE_PROFTPD_TAG=$OVERRIDE_PROFTPD_TAG"
    debug "shift_count=$shift_count"
}

### MAIN ###
BUILD_FOR_K8S=$FALSE
BUILD_FOR_CONDOR=$FALSE
BUILD_FOR_GRAFANA=$FALSE
BUILD_FOR_SLURM=$FALSE

read_args "$@"

DOCKER_PUSH_ENABLED=${DOCKER_PUSH_ENABLED:-$DFT_DOCKER_PUSH_ENABLED}

DOCKER_REPO=${CONTAINER_REGISTRY:-}
DOCKER_USER=${CONTAINER_USER:-pcm32}

ANSIBLE_REPO=${ANSIBLE_REPO:-galaxyproject/ansible-galaxy-extras}
ANSIBLE_RELEASE=${ANSIBLE_RELEASE:-18.09}

GALAXY_VERSION=${GALAXY_VERSION:-18.09}

# GALAXY_BASE_FROM_TO_REPLACE=${GALAXY_BASE_FROM_TO_REPLACE:-quay.io/bgruening/galaxy-base:$GALAXY_VERSION}
GALAXY_BASE_FROM_TO_REPLACE=$(grep ^FROM galaxy-init/Dockerfile | awk '{ print $2 }') # init starts from base, so we get it from there.
CONDOR_BASE_FROM_TO_REPLACE=quay.io/bgruening/galaxy-htcondor-base:$GALAXY_VERSION

GALAXY_RELEASE=${GALAXY_RELEASE:-release_$GALAXY_VERSION}
GALAXY_REPO=${GALAXY_REPO:-galaxyproject/galaxy}

GALAXY_VER_FOR_POSTGRES=$GALAXY_VERSION
# Uncomment to push intermediate images, otherwise only the images needed for the helm chart are pushed.
#PUSH_INTERMEDIATE_IMAGES=yes

# Set tags
TAG=${GALAXY_TAG:-v$GALAXY_VERSION}

# TODO This is PhenoMeNal Jenkins specific, should be removed at some point.
if [[ -n ${CONTAINER_TAG_PREFIX:-} && -n ${BUILD_NUMBER:-} ]]; then
    TAG=${CONTAINER_TAG_PREFIX}.${BUILD_NUMBER}
fi

if [[ -n "${DOCKER_REPO}" ]]; then
    # Append slash, avoiding double slash
    DOCKER_REPO="${DOCKER_REPO%/}/"
fi

GALAXY_BASE_TAG=$DOCKER_REPO$DOCKER_USER/galaxy-base:$TAG
GALAXY_INIT_TAG=$DOCKER_REPO$DOCKER_USER/galaxy-init:$TAG
GALAXY_WEB_TAG=${OVERRIDE_GALAXY_WEB_TAG:-$DOCKER_REPO$DOCKER_USER/galaxy-web:$TAG}

if [[ -n "${OVERRIDE_POSTGRES_TAG:-}" ]]; then
    POSTGRES_TAG="${OVERRIDE_POSTGRES_TAG}"
else
    PG_Dockerfile="galaxy-postgres/Dockerfile"

    [[ -f "${PG_Dockerfile}" ]] || error "The galaxy-postgres Dockerfile is missing under galaxy-Postgres." 99
    POSTGRES_VERSION=$(grep FROM "${PG_Dockerfile}" | awk -F":" '{ print $2 }')

    POSTGRES_TAG=$DOCKER_REPO$DOCKER_USER/galaxy-postgres:$POSTGRES_VERSION"_for_"$GALAXY_VER_FOR_POSTGRES
fi

if [[ -n "${OVERRIDE_PROFTPD_TAG:-}" ]]; then
    PROFTPD_TAG="${OVERRIDE_PROFTPD_TAG}"
else
    PROFTPD_TAG=$DOCKER_REPO$DOCKER_USER/galaxy-proftpd:for_galaxy_v$GALAXY_VER_FOR_POSTGRES<commit_msg>Remove 'v' from tags for uniformity<commit_after>#!/bin/bash
# vi: fdm=marker

set -e

### Constants ###

SCRIPT_NAME="${0##*/}"
TRUE='true'
FALSE='false'
DFT_DOCKER_PUSH_ENABLED=$TRUE

### Functions ###

function log() {
  echo -e "$@" >&2
}

function error {
	local msg=$1
	local code=$2
	[[ -z $code ]] && code=1
	log "[ERROR] $msg"

	exit $code
}


function debug {
    local msg=$1
    local level=$2
    [[ -n $level ]] || level=1

    [[ $DEBUG -lt $level ]] || log "[DEBUG] $msg"
}

function warning {
    local msg=$1

    log "[WARNING] ##### $msg #####"
}

function print_help {
    (
        echo "Usage: $SCRIPT_NAME [options]"
        echo
        echo "Build Galaxy docker compose images for different purposes."
        echo
        echo "Options:"
        echo "   -g, --debug                    Debug mode. [false]"
        echo "   -h, --help                     Print this help message."
        echo "   -p, --push                     Push docker images. You can also set environment variable DOCKER_PUSH_ENABLED to \"true\". [true]"
        echo "   +p, --no-push                  Do not push dockers. You can also set environment variable DOCKER_PUSH_ENABLED to \"false\"."
        echo "       --push-intermediate        Also push intermediate images [false]"
        echo "       --no-cache                 Tell Docker not to use cached images. [false]"
        echo "       --init-tag       <tag>     Set the tag for the Galaxy Init Flavour container image."
        echo "       --postgres-tag   <tag>     Set the tag for the Galaxy Postgres container image."
        echo "       --proftpd-tag    <tag>     Set the tag for the Galaxy Proftpd container image."
        echo "       --web-tag        <tag>     Set the tag for the Galaxy Web k8s container image."
        echo "       --k8s                      Set settings for Kubernetes usage."
        echo "       --grafana                  Build grafana container."
        echo "       --condor                   Build condor containers."
        echo "       --slurm                    Build slurm containers."
        echo "   -u, --container-user <user>    Set the container user. You can also set the environment variable CONTAINER_USER."
        echo "   -r, --container-registry <reg> Set the container registry. You can also set the environment variable CONTAINER_REGISTRY."
        echo
    ) >&2
}

function read_args {

    local args="$*" # save arguments for debugging purpose

    # Read options
    while [[ $# > 0 ]] ; do
        shift_count=1
        case $1 in
            -g|--debug)              DEBUG=$((DEBUG + 1)) ;;
            -h|--help)               print_help ; exit 0 ;;
            -p|--push)               DOCKER_PUSH_ENABLED=$TRUE ;;
            +p|--no-push)            DOCKER_PUSH_ENABLED=$FALSE ;;
            --k8s)                   BUILD_FOR_K8S=$TRUE ;;
            --condor)                BUILD_FOR_CONDOR=$TRUE ;;
            --slurm)                 BUILD_FOR_SLURM=$TRUE ;;
            --grafana)               BUILD_FOR_GRAFANA=$TRUE ;;
            --push-intermediate)     PUSH_INTERMEDIATE_IMAGES=$TRUE ;;
            --no-cache)              NO_CACHE="--no-cache" ;;
            --postgres-tag)          OVERRIDE_POSTGRES_TAG="$2" ; shift_count=2 ;;
            --proftpd-tag)           OVERRIDE_PROFTPD_TAG="$2" ; shift_count=2 ;;
            --init-tag)              OVERRIDE_GALAXY_INIT_PHENO_FLAVOURED_TAG="$2" ; shift_count=2 ;;
            --web-tag)               OVERRIDE_GALAXY_WEB_TAG="$2" ; shift_count=2 ;;
            -u|--container-user)     CONTAINER_USER="$2" ; shift_count=2 ;;
            -r|--container-registry) CONTAINER_REGISTRY="$2" ; shift_count=2 ;;
            *) error "Illegal option $1." 98 ;;
        esac
        shift $shift_count
    done

    # Debug messages
    debug "Command line arguments: $args"
    debug "Argument DEBUG=$DEBUG"
    debug "Argument DOCKER_PUSH_ENABLED=$DOCKER_PUSH_ENABLED"
    debug "Argument DOCKER_USER=$DOCKER_USER"
    debug "Argument OVERRIDE_GALAXY_WEB_TAG=$OVERRIDE_GALAXY_WEB_TAG"
    debug "Argument OVERRIDE_POSTGRES_TAG=$OVERRIDE_POSTGRES_TAG"
    debug "Argument OVERRIDE_PROFTPD_TAG=$OVERRIDE_PROFTPD_TAG"
    debug "shift_count=$shift_count"
}

### MAIN ###
BUILD_FOR_K8S=$FALSE
BUILD_FOR_CONDOR=$FALSE
BUILD_FOR_GRAFANA=$FALSE
BUILD_FOR_SLURM=$FALSE

read_args "$@"

DOCKER_PUSH_ENABLED=${DOCKER_PUSH_ENABLED:-$DFT_DOCKER_PUSH_ENABLED}

DOCKER_REPO=${CONTAINER_REGISTRY:-}
DOCKER_USER=${CONTAINER_USER:-pcm32}

ANSIBLE_REPO=${ANSIBLE_REPO:-galaxyproject/ansible-galaxy-extras}
ANSIBLE_RELEASE=${ANSIBLE_RELEASE:-18.09}

GALAXY_VERSION=${GALAXY_VERSION:-18.09}

# GALAXY_BASE_FROM_TO_REPLACE=${GALAXY_BASE_FROM_TO_REPLACE:-quay.io/bgruening/galaxy-base:$GALAXY_VERSION}
GALAXY_BASE_FROM_TO_REPLACE=$(grep ^FROM galaxy-init/Dockerfile | awk '{ print $2 }') # init starts from base, so we get it from there.
CONDOR_BASE_FROM_TO_REPLACE=quay.io/bgruening/galaxy-htcondor-base:$GALAXY_VERSION

GALAXY_RELEASE=${GALAXY_RELEASE:-release_$GALAXY_VERSION}
GALAXY_REPO=${GALAXY_REPO:-galaxyproject/galaxy}

GALAXY_VER_FOR_POSTGRES=$GALAXY_VERSION
# Uncomment to push intermediate images, otherwise only the images needed for the helm chart are pushed.
#PUSH_INTERMEDIATE_IMAGES=yes

# Set tags
TAG=${GALAXY_TAG:-$GALAXY_VERSION}

# TODO This is PhenoMeNal Jenkins specific, should be removed at some point.
if [[ -n ${CONTAINER_TAG_PREFIX:-} && -n ${BUILD_NUMBER:-} ]]; then
    TAG=${CONTAINER_TAG_PREFIX}.${BUILD_NUMBER}
fi

if [[ -n "${DOCKER_REPO}" ]]; then
    # Append slash, avoiding double slash
    DOCKER_REPO="${DOCKER_REPO%/}/"
fi

GALAXY_BASE_TAG=$DOCKER_REPO$DOCKER_USER/galaxy-base:$TAG
GALAXY_INIT_TAG=$DOCKER_REPO$DOCKER_USER/galaxy-init:$TAG
GALAXY_WEB_TAG=${OVERRIDE_GALAXY_WEB_TAG:-$DOCKER_REPO$DOCKER_USER/galaxy-web:$TAG}

if [[ -n "${OVERRIDE_POSTGRES_TAG:-}" ]]; then
    POSTGRES_TAG="${OVERRIDE_POSTGRES_TAG}"
else
    PG_Dockerfile="galaxy-postgres/Dockerfile"

    [[ -f "${PG_Dockerfile}" ]] || error "The galaxy-postgres Dockerfile is missing under galaxy-Postgres." 99
    POSTGRES_VERSION=$(grep FROM "${PG_Dockerfile}" | awk -F":" '{ print $2 }')

    POSTGRES_TAG=$DOCKER_REPO$DOCKER_USER/galaxy-postgres:$POSTGRES_VERSION"_for_"$GALAXY_VER_FOR_POSTGRES
fi

if [[ -n "${OVERRIDE_PROFTPD_TAG:-}" ]]; then
    PROFTPD_TAG="${OVERRIDE_PROFTPD_TAG}"
else
    PROFTPD_TAG=$DOCKER_REPO$DOCKER_USER/galaxy-proftpd:for_galaxy_$GALAXY_VER_FOR_POSTGRES
{'fraction_non_alphanumeric': 0.12354853692522062, 'fraction_numerical': 0.0075863136708468806, 'mean_word_length': 2.598607242339833, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6799059', 'n_tokens_mistral': 4651, 'n_tokens_neox': 4391, 'n_words': 914}