<commit_before>#!/bin/bash

# MAINTAINER <NAME> <<EMAIL>>

set -o errexit

#############################################
# Usage
#############################################
usage () {
    cat <<EOF

$0 launches a sequence that:
- builds Asqatasun from sources,
- builds a Docker image
- runs a container based the freshly built image
- execute functional tests

usage: $0 [OPTIONS]

  -s | --source-dir <directory>     MANDATORY Absolute path to Asqatasun sources directory 
  -d | --docker-dir <directory>     MANDATORY Path to directory containing the Dockerfile. Path must be relative to SOURCE_DIR
  -t | --functional-tests           also execute functional tests (please check pre-requisites on http://doc.asqatasun.org/en/30_Contributor_doc/Testing/Functional_tests.html)
  -h | --help                       Show this help

EOF
    exit 2
}

#############################################
# Manage options and usage
#############################################
TEMP=`getopt -o s:d:ht --long source-dir:,docker-dir:,help,functional-tests -- "$@"`

if [[ $? != 0 ]] ; then
    echo "Terminating..." >&2 ;
    exit 1 ;
fi

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

declare SOURCE_DIR
declare DOCKER_DIR
declare HELP=false
declare FTESTS=false

while true; do
  case "$1" in
    -s | --source-dir ) SOURCE_DIR="$2"; shift 2 ;; 
    -d | --docker-dir ) DOCKER_DIR="$2"; shift 2 ;;
    -h | --help )       HELP=true; shift ;;
    -t | --functional-tests )   FTESTS=true; shift ;;
    * ) break ;;
  esac
done

if [[ -z "$SOURCE_DIR" || -z "$DOCKER_DIR" || "$HELP" == "true" ]]; then
    usage
fi

#############################################
# Functions
#############################################

fail() {
        echo ""
	echo "FAILURE : $*"
        echo ""
	exit -1
}

#############################################
# Variables
#############################################

TIMESTAMP=$(date +%Y-%m-%d) # format 2015-11-25, cf man date
#DOCKER_DIR="docker/single-container"
TGZ_BASENAME="web-app/asqatasun-web-app/target/asqatasun-"
TGZ_EXT=".tar.gz"

#############################################
# Do the actual job
#############################################

# clean and build
(cd "$SOURCE_DIR" ; mvn clean install) ||
    fail "Error at build"
 
# copy TAR.GZ to docker dir
cp "${SOURCE_DIR}/${TGZ_BASENAME}"*"${TGZ_EXT}" "${SOURCE_DIR}/${DOCKER_DIR}/" ||
    fail "Error copying ${SOURCE_DIR}/${TGZ_BASENAME}"

# build Docker container
(cd "${SOURCE_DIR}/${DOCKER_DIR}" ; \
    docker build -t asqatasun/asqatasun:${TIMESTAMP} "${SOURCE_DIR}/${DOCKER_DIR}" ) ||
    fail "Error building container"

# Run container freshly build
# @@@TODO find a way to test whether a container named "asqa" is already running, and if so, stop it and delete it
# Container is destroyed if exiting gracefully (--rm option)
docker run --rm -p 8085:8080 --name asqa asqatasun/asqatasun:${TIMESTAMP}

# functional testing
if [ $FTESTS ]; then
    (cd "${SOURCE_DIR}/testing-tools/tgol-test-scenario"; \
        mvn test \
        -Dadmin.user=<EMAIL> \
        -Dadmin.password=<PASSWORD> \
        -Dhost.location=http://localhost:8085/asqatasun/ \
        -Dfirefox.path=/opt/firefox/firefox
    )
fi<commit_msg>DOCKER: enhanced "meta-script"<commit_after>#!/bin/bash

# MAINTAINER <NAME> <<EMAIL>>

set -o errexit

#############################################
# Usage
#############################################
usage () {
    cat <<EOF

$0 launches a sequence that:
- builds Asqatasun from sources,
- builds a Docker image
- runs a container based the freshly built image
- execute functional tests

usage: $0 [OPTIONS]

  -s | --source-dir <directory>     MANDATORY Absolute path to Asqatasun sources directory 
  -d | --docker-dir <directory>     MANDATORY Path to directory containing the Dockerfile. Path must be relative to SOURCE_DIR
  -t | --functional-tests           also execute functional tests (please check pre-requisites on http://doc.asqatasun.org/en/30_Contributor_doc/Testing/Functional_tests.html)

  --skip-build                      skip Maven build (relies on previous build, that must exists)
  --skip-copy                       skip copying .tar.gz (relies on previous .tar.gz, that must exist)
  --skip-docker-build               skip docker build
  --skip-docker-run                 skip docker run

  -h | --help                       Show this help

EOF
    exit 2
}

#############################################
# Manage options and usage
#############################################
TEMP=`getopt -o s:d:ht --long source-dir:,docker-dir:,help,functional-tests,skip-build,skip-copy,skip-docker-build,skip-docker-run -- "$@"`

if [[ $? != 0 ]] ; then
    echo "Terminating..." >&2 ;
    exit 1 ;
fi

# Note the quotes around `$TEMP': they are essential!
eval set -- "${TEMP}"

declare SOURCE_DIR
declare DOCKER_DIR
declare HELP=false
declare FTESTS=false
declare SKIP_BUILD=false
declare SKIP_COPY=false
declare SKIP_DOCKER_BUILD=false
declare SKIP_DOCKER_RUN=false

while true; do
  case "$1" in
    -s | --source-dir )         SOURCE_DIR="$2"; shift 2 ;;
    -d | --docker-dir )         DOCKER_DIR="$2"; shift 2 ;;
    -h | --help )               HELP=true; shift ;;
    -t | --functional-tests )   FTESTS=true; shift ;;
    --skip-build )              SKIP_BUILD=true; shift ;;
    --skip-copy )               SKIP_COPY=true; shift ;;
    --skip-docker-build )       SKIP_DOCKER_BUILD=true; shift ;;
    --skip-docker-run )         SKIP_DOCKER_RUN=true; shift ;;
    * ) break ;;
  esac
done

if [[ -z "$SOURCE_DIR" || -z "$DOCKER_DIR" || "$HELP" == "true" ]]; then
    usage
fi

#############################################
# Functions
#############################################

fail() {
    echo ""
	echo "FAILURE : $*"
    echo ""
	exit -1
}

#############################################
# Variables
#############################################

TIMESTAMP=$(date +%Y-%m-%d) # format 2015-11-23, cf man date
TGZ_BASENAME="web-app/asqatasun-web-app/target/asqatasun-"
TGZ_EXT=".tar.gz"
CONTAINER_NAME="asqa"
CONTAINER_EXPOSED_PORT="8085"

#############################################
# Functions
#############################################

function kill_previous_container() {
    set +e
    RUNNING=$(docker inspect --format="{{ .State.Status }}" ${CONTAINER_NAME} 2>/dev/null)
    set -e

    if [ ${RUNNING} == "running" ]; then
        docker stop ${CONTAINER_NAME}
        docker rm ${CONTAINER_NAME}
    elif [ ${RUNNING} == "exited" ]; then
        docker rm ${CONTAINER_NAME}
    fi
}

function do_build() {
    # clean and build
    (cd "$SOURCE_DIR" ; mvn clean install) ||
        fail "Error at build"
}

function do_copy_targz() {
    # copy TAR.GZ to docker dir
    cp "${SOURCE_DIR}/${TGZ_BASENAME}"*"${TGZ_EXT}" "${SOURCE_DIR}/${DOCKER_DIR}/" ||
        fail "Error copying ${SOURCE_DIR}/${TGZ_BASENAME}"
}

function do_docker_build() {
    # build Docker container
    (cd "${SOURCE_DIR}/${DOCKER_DIR}" ; \
        docker build -t asqatasun/asqatasun:${TIMESTAMP} "${SOURCE_DIR}/${DOCKER_DIR}" ) ||
        fail "Error building container"
}

function do_docker_run() {
    kill_previous_container
    docker run -p ${CONTAINER_EXPOSED_PORT}:8080 --name ${CONTAINER_NAME} -d asqatasun/asqatasun:${TIMESTAMP}

    # wait a bit to let container start
    # @@@TODO find a better way to do this (test if URL responds with 200 or anything else)
    sleep 25
}

function do_functional_testing() {
    # functional testing
    (cd "${SOURCE_DIR}/testing-tools/tgol-test-scenario"; \
        mvn test \
        -Dadmin.user=<EMAIL> \
        -Dadmin.password=<PASSWORD> \
        -Dhost.location=http://localhost:${CONTAINER_EXPOSED_PORT}/asqatasun/ \
        -Dfirefox.path=/opt/firefox/firefox
    )
}

#############################################
# Do the actual job
#############################################

if ! ${SKIP_BUILD} ; then            do_build; fi
if ! ${SKIP_COPY} ; then             do_copy_targz; fi
if ! ${SKIP_DOCKER_BUILD} ; then     do_docker_build; fi
if ! ${SKIP_DOCKER_RUN} ; then       do_docker_run; fi
if ${FTESTS} ; then                  do_functional_testing; fi
{'fraction_non_alphanumeric': 0.24279081133919844, 'fraction_numerical': 0.00855327468230694, 'mean_word_length': 3.337572866984632, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 20, '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': '5550586', 'n_tokens_mistral': 2803, 'n_tokens_neox': 2543, 'n_words': 655}