<commit_before>#!/bin/bash
# ------------------------------------------------------------------------
#
# Copyright 2005-2015 WSO2, Inc. (http://wso2.com)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License

# ------------------------------------------------------------------------

########################### Simple Test Script ##############################
#                                                                           #
#   This script will build the default version of specified docker images,  #
#   scp them to the Kubernetes nodes and deploy the Kubernetes artifacts.   #
#                                                                           #
#   Tested with the Kubernetes setup at:                                    #
#   https://github.com/pires/kubernetes-vagrant-coreos-cluster              #
#                                                                           #
#   Usage:                                                                  #
#   1. set PUPPET_HOME, KUBERNETES_NODE_USER and KUBERNETES_NODE            #
#      in environment.bash                                                  #
#   2. add the products and version that need to be tested to the array     #
#      'products' as comma separated tuples.                                #
#       ex.: products=(wso2am,1.9.1 wso2is,5.0.0)                           #
#   3. run the script                                                       #
#                                                                           #
#                                                                           #
#############################################################################

set -e

kubernetes_artifact_version=1.0.0
default_profile=default
source environment.bash
source ~/.bash_profile

prgdir=`dirname "$0"`
current_path=`cd "$prgdir"; pwd`
root_dir=${current_path}/..
common_dir=${current_path}/../common
base_image_dir=`cd "${common_dir}/docker/base-image"; pwd`

user=`whoami`
echo "user executing the script $user"

# products to be tested
products=(wso2am,1.9.1 wso2is,5.0.0)
declare -A results

function echoError {
    echo $'\e[1;31m'"${1}"$'\e[0m'
}

function echoSuccess {
    echo $'\e[1;32m'"${1}"$'\e[0m'
}

function echoBold {
    echo $'\e[1m'"${1}"$'\e[0m'
}

# build base image
function build_base_image {
    # navigate to base image dir
    pushd ${base_image_dir} > /dev/null
    # build
    # sudo bash build.sh ${kubernetes_artifact_version}
    # go back to previous directory location
    popd > /dev/null
}

function build_docker_image_and_scp {
    # switch to IS 5 docker directory and build
    pushd ${root_dir}/$1/docker > /dev/null
    echo pwd=`pwd`
    sudo --preserve-env bash build.sh $2 ${kubernetes_artifact_version} $3 || print_results
    # save the image as a tar file
    sudo bash save.sh $2 ${kubernetes_artifact_version} $3 || print_results
    sudo chown ${user}:${user} -R ~/docker/
    bash scp.sh ${KUBERNETES_NODE_USER}@${KUBERNETES_NODE} $2 ${kubernetes_artifact_version} $3 || print_results
    popd > /dev/null
}

function undeploy_kubernetes_artifacts {
    # switch to IS 5 kubernetes directory deploy kubernetes artifacts
    pushd ${root_dir}/$1/kubernetes > /dev/null
    bash undeploy.sh
    popd > /dev/null
}

function deploy_kubernetes_artifacts {
    # switch to IS 5 kubernetes directory deploy kubernetes artifacts
    pushd ${root_dir}/$1/kubernetes > /dev/null
    echo "Deploying $1 $2 service..."
    kubectl create -f "$1"-"$2"-service.yaml || print_results
    echo "Deploying $1 $2 controller..."
    kubectl create -f "$1"-"$2"-controller.yaml || print_results
    popd > /dev/null
}


function check_status {
    error_count=0
    success_count=0
    while true; do
        # check the pod status
        result=`kubectl get pods | grep $1`
        if [[ -z "$result" ]]; then
           echo "no pod found for product $1"
           error_count=$((error_count + 1))
           success_count=0
        fi
        IFS=' ' read -r -a array <<< "$result"
        pod_name=${array[0]}
        ready=${array[1]}
        state=${array[2]}
        restarts=${array[3]}
        if [[ ${ready} != '1/1' ]] || [[ $state != 'Running' ]] || [[ $restarts != '0'  ]]; then
            echo "error condition detected in pod=$pod_name, ready=$ready, status=$state, restarts=$restarts"
            error_count=$((error_count + 1))
            success_count=0
        else
            success_count=$((success_count + 1))
            error_count=0
        fi

        if [[ ${error_count} -gt 5 ]]; then
            echo "error condition threshold reached: $error_count, aborting"
	        #undeploy_kubernetes_artifacts "$1"
            results[$1-$2]="ERROR, last pod name=$pod_name, state=$state, restarts=$restarts"
            break
        elif [[ ${success_count} -gt 5 ]]; then
            echo "success condition threshold reached: $success_count"
            server_started=$(check_carbon_server_has_started "$pod_name")
            if [[ ${server_started} -eq 0 ]]; then
                msg="Carbon Server $1-$2 in pod $pod_name has started successfully"
                echo ${msg}
                results[$1-$2]="SUCCESS, $msg"
                break
            else
                msg="Carbon Server $1-$2 in pod $pod_name has failed to start"
                echo ${msg}
                #undeploy_kubernetes_artifacts "$1"
                results[$1-$2]="ERROR, $msg"
                break
            fi
        else
            sleep 6s
        fi
    done
}

function check_carbon_server_has_started {
    tries=0
    while true; do
        carbon_logs=`kubectl logs $1`
        if [[ ${carbon_logs} =~ "Mgt Console URL" ]]; then
            echo 0
            break
        else
            tries=$((tries + 1))
            if [[ ${tries} -gt 10 ]]; then
                echo 1
                break
            fi
        fi
        sleep 6s
    done
}

function validate_environment {
    if [ -z "$PUPPET_HOME" ]; then
        echo "please set environment variable PUPPET_HOME"
        exit
    fi
    if [ -z "$KUBERNETES_NODE" ]; then
        echo "please set environment variable KUBERNETES_NODE"
        exit
    fi
    if [ -z "$KUBERNETES_NODE_USER" ]; then
        echo "please set environment variable KUBERNETES_NODE_USER"
        exit
    fi
}
<commit_msg>increasing carbon server startup waiting time to 5 mins<commit_after>#!/bin/bash
# ------------------------------------------------------------------------
#
# Copyright 2005-2015 WSO2, Inc. (http://wso2.com)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License

# ------------------------------------------------------------------------

########################### Simple Test Script ##############################
#                                                                           #
#   This script will build the default version of specified docker images,  #
#   scp them to the Kubernetes nodes and deploy the Kubernetes artifacts.   #
#                                                                           #
#   Tested with the Kubernetes setup at:                                    #
#   https://github.com/pires/kubernetes-vagrant-coreos-cluster              #
#                                                                           #
#   Usage:                                                                  #
#   1. set PUPPET_HOME, KUBERNETES_NODE_USER and KUBERNETES_NODE            #
#      in environment.bash                                                  #
#   2. add the products and version that need to be tested to the array     #
#      'products' as comma separated tuples.                                #
#       ex.: products=(wso2am,1.9.1 wso2is,5.0.0)                           #
#   3. run the script                                                       #
#                                                                           #
#                                                                           #
#############################################################################

set -e

kubernetes_artifact_version=1.0.0
default_profile=default
source environment.bash
source ~/.bash_profile

prgdir=`dirname "$0"`
current_path=`cd "$prgdir"; pwd`
root_dir=${current_path}/..
common_dir=${current_path}/../common
base_image_dir=`cd "${common_dir}/docker/base-image"; pwd`

user=`whoami`
echo "user executing the script $user"

# products to be tested
products=(wso2am,1.9.1 wso2is,5.0.0)
declare -A results

function echoError {
    echo $'\e[1;31m'"${1}"$'\e[0m'
}

function echoSuccess {
    echo $'\e[1;32m'"${1}"$'\e[0m'
}

function echoBold {
    echo $'\e[1m'"${1}"$'\e[0m'
}

# build base image
function build_base_image {
    # navigate to base image dir
    pushd ${base_image_dir} > /dev/null
    # build
    # sudo bash build.sh ${kubernetes_artifact_version}
    # go back to previous directory location
    popd > /dev/null
}

function build_docker_image_and_scp {
    # switch to IS 5 docker directory and build
    pushd ${root_dir}/$1/docker > /dev/null
    echo pwd=`pwd`
    sudo --preserve-env bash build.sh $2 ${kubernetes_artifact_version} $3 || print_results
    # save the image as a tar file
    sudo bash save.sh $2 ${kubernetes_artifact_version} $3 || print_results
    sudo chown ${user}:${user} -R ~/docker/
    bash scp.sh ${KUBERNETES_NODE_USER}@${KUBERNETES_NODE} $2 ${kubernetes_artifact_version} $3 || print_results
    popd > /dev/null
}

function undeploy_kubernetes_artifacts {
    # switch to IS 5 kubernetes directory deploy kubernetes artifacts
    pushd ${root_dir}/$1/kubernetes > /dev/null
    bash undeploy.sh
    popd > /dev/null
}

function deploy_kubernetes_artifacts {
    # switch to IS 5 kubernetes directory deploy kubernetes artifacts
    pushd ${root_dir}/$1/kubernetes > /dev/null
    echo "Deploying $1 $2 service..."
    kubectl create -f "$1"-"$2"-service.yaml || print_results
    echo "Deploying $1 $2 controller..."
    kubectl create -f "$1"-"$2"-controller.yaml || print_results
    popd > /dev/null
}


function check_status {
    error_count=0
    success_count=0
    while true; do
        # check the pod status
        result=`kubectl get pods | grep $1`
        if [[ -z "$result" ]]; then
           echo "no pod found for product $1"
           error_count=$((error_count + 1))
           success_count=0
        fi
        IFS=' ' read -r -a array <<< "$result"
        pod_name=${array[0]}
        ready=${array[1]}
        state=${array[2]}
        restarts=${array[3]}
        if [[ ${ready} != '1/1' ]] || [[ $state != 'Running' ]] || [[ $restarts != '0'  ]]; then
            echo "error condition detected in pod=$pod_name, ready=$ready, status=$state, restarts=$restarts"
            error_count=$((error_count + 1))
            success_count=0
        else
            success_count=$((success_count + 1))
            error_count=0
        fi

        if [[ ${error_count} -gt 5 ]]; then
            echo "error condition threshold reached: $error_count, aborting"
	        #undeploy_kubernetes_artifacts "$1"
            results[$1-$2]="ERROR, last pod name=$pod_name, state=$state, restarts=$restarts"
            break
        elif [[ ${success_count} -gt 5 ]]; then
            echo "success condition threshold reached: $success_count"
            server_started=$(check_carbon_server_has_started "$pod_name")
            if [[ ${server_started} -eq 0 ]]; then
                msg="Carbon Server $1-$2 in pod $pod_name has started successfully"
                echo ${msg}
                results[$1-$2]="SUCCESS, $msg"
                break
            else
                msg="Carbon Server $1-$2 in pod $pod_name has failed to start"
                echo ${msg}
                #undeploy_kubernetes_artifacts "$1"
                results[$1-$2]="ERROR, $msg"
                break
            fi
        else
            sleep 6s
        fi
    done
}

function check_carbon_server_has_started {
    tries=0
    while true; do
        carbon_logs=`kubectl logs $1`
        if [[ ${carbon_logs} =~ "Mgt Console URL" ]]; then
            echo 0
            break
        else
            tries=$((tries + 1))
            # allow the Carbon server to start for 5 minutes (6s * 50 = 5 mins)
            if [[ ${tries} -gt 50 ]]; then
                echo 1
                break
            fi
        fi
        sleep 6s
    done
}

function validate_environment {
    if [ -z "$PUPPET_HOME" ]; then
        echo "please set environment variable PUPPET_HOME"
        exit
    fi
    if [ -z "$KUBERNETES_NODE" ]; then
        echo "please set environment variable KUBERNETES_NODE"
        exit
    fi
    if [ -z "$KUBERNETES_NODE_USER" ]; then
        echo "please set environment variable KUBERNETES_NODE_USER"
        exit
    fi
}

{'fraction_non_alphanumeric': 0.14100709014614382, 'fraction_numerical': 0.01649544204890754, 'mean_word_length': 1.7541342896991432, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 44, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '708141', 'n_tokens_mistral': 3992, 'n_tokens_neox': 3735, 'n_words': 1107}