<commit_before>#!/bin/bash

set -o nounset
set -o errexit

STARTUP_DIR="$( cd "$( dirname "$0" )" && pwd )"

SELDON_HOME=${STARTUP_DIR}/../..
SELDON_WITH_SPARK=${SELDON_WITH_SPARK:-true}
SELDON_WITH_GLUSTERFS=${SELDON_WITH_GLUSTERFS:-false}
KCMD="kubectl exec seldon-control -i bash"

function normalize_version {
    echo "$@" | awk -F. '{ printf("%03d%03d%03d\n", $1,$2,$3); }';
}

function check_zookeeper {
    kubectl exec -i zookeeper-3 -- bash -c "echo srvr | nc localhost  2181 | grep Mode | cut -d' ' -f2"
}

function start_core_services {
    echo "Starting core servces"
    kubectl create -f ${STARTUP_DIR}/../conf/mysql.json
    kubectl create -f ${STARTUP_DIR}/../conf/memcache.json
    kubectl create -f ${STARTUP_DIR}/../conf/zookeeper.json
    kubectl create -f ${STARTUP_DIR}/../conf/control.json
    kubectl create -f ${STARTUP_DIR}/../conf/influxdb-grafana.json

    while true; do
        non_running_states=$(get_non_running_states)
        if [[ "$non_running_states" == "0" ]]; then
            break
        else
	    kubectl get pods
            echo "Waiting for pods to be running as found $non_running_states in non-running state"
            echo "Sleeping for 5 seconds..."
            sleep 5
        fi
    done
    while true; do
        zookeeper_mode=$(check_zookeeper)
        if [[ "$zookeeper_mode" == "leader" || "$zookeeper_mode" == "follower" ]]; then
            break
        else
            echo "Waiting for zookeeper to be ready as status check returned $zookeeper_mode"
            sleep 1
        fi
    done

    kubectl create -f ${STARTUP_DIR}/../conf/kafka.json
    kubectl create -f ${STARTUP_DIR}/../conf/td-agent-server.json
    while true; do
        non_running_states=$(get_non_running_states)
        if [[ "$non_running_states" == "0" ]]; then
            break
        else
	    kubectl get pods
            echo "Waiting for kafka and td-agent-server to be running as found $non_running_states in non-running state"
            echo "Sleeping for 5 seconds..."
            sleep 5
        fi
    done

}


function start_api_server {
    echo "Starting Seldon API server"
    kubectl create -f ${STARTUP_DIR}/../conf/server.json
}

function setup_basic_conf {

    #push any existing conf to zookeeper
    ${STARTUP_DIR}/seldon-cli client --action zk_push
    # Setup memcached
    ${STARTUP_DIR}/seldon-cli memcached --action setup --servers memcached1:11211,memcached2:11211
    ${STARTUP_DIR}/seldon-cli memcached --action commit
    # Setup DB
    ${STARTUP_DIR}/seldon-cli db --action setup --db-name ClientDB --db-jdbc 'jdbc:mysql:replication://mysql:3306,mysql:3306/?characterEncoding=utf8&useServerPrepStmts=true&logger=com.mysql.jdbc.log.StandardLogger&roundRobinLoadBalance=true&transformedBitIsBoolean=true&rewriteBatchedStatements=true'
    echo "seldon-cli db --action commit" | ${KCMD}
    # Setup Test client
    ${STARTUP_DIR}/seldon-cli client --action setup --db-name ClientDB --client-name test

}

function start_spark {
    if $SELDON_WITH_SPARK ; then
        echo 'Creating Spark Cluster'
        kubectl create -f ${STARTUP_DIR}/../conf/spark-master.json
        while true; do
            non_running_states=$(get_non_running_states)
            if [[ "$non_running_states" == "0" ]]; then
                break
            else
		kubectl get pods
                echo "Waiting for pods to be running as found $non_running_states in non-running state"
                echo "Sleeping for 3 seconds..."
                sleep 3
            fi
        done
        kubectl create -f ${STARTUP_DIR}/../conf/spark-workers.json
    fi
}

function start_analytics {
    kubectl create -f ${STARTUP_DIR}/../conf/analytics/impressions-kafka-stream.json
    kubectl create -f ${STARTUP_DIR}/../conf/analytics/predictions-kafka-stream.json
}

function start_glusterfs_service {
    if $SELDON_WITH_GLUSTERFS ; then
	echo 'Creating Glusterfs service'
	kubectl create -f ${STARTUP_DIR}/../conf/glusterfs.json
    fi
}


function setup_influxdb {

    INFLUXDB_POD=`kubectl get pods -l name=influxGrafana | sed 1d | cut -d' ' -f1 |sed -e 's/^[ \t]*//'`
    kubectl exec ${INFLUXDB_POD} -- influx --execute 'create database IF NOT EXISTS seldon'

}

function startup_msg {
    local SELDON_VERSION_INSTALLED=$(cat ${SELDON_HOME}/pom.xml|grep -A 1 '<artifactId>seldon-parent</artifactId>'|sed -n -e "s/<version>\(.*\)<\/version>/\1/p")
    local SELDON_VERSION_INSTALLED=$(echo -n "${SELDON_VERSION_INSTALLED}" | sed "s/^[ \t]*//")
    local SELDON_VERSION_URL="http://static.seldon.io/seldon-version/seldon-version.txt"
    local SELDON_VERSION_RELEASED=$(curl -fsL --max-time 5 ${SELDON_VERSION_URL} || echo '0.0.0')
    local UPDATE_MSG=
    if [ $(normalize_version ${SELDON_VERSION_RELEASED}) -gt $(normalize_version ${SELDON_VERSION_INSTALLED}) ]; then
        UPDATE_MSG=", latest released version is [${SELDON_VERSION_RELEASED}]"
    fi

    local MSG="Starting seldon version [${SELDON_VERSION_INSTALLED}]${UPDATE_MSG}"
    echo "${MSG}"
}

function seldon_up {

    startup_msg<commit_msg>version url updated<commit_after>#!/bin/bash

set -o nounset
set -o errexit

STARTUP_DIR="$( cd "$( dirname "$0" )" && pwd )"

SELDON_HOME=${STARTUP_DIR}/../..
SELDON_WITH_SPARK=${SELDON_WITH_SPARK:-true}
SELDON_WITH_GLUSTERFS=${SELDON_WITH_GLUSTERFS:-false}
KCMD="kubectl exec seldon-control -i bash"

function normalize_version {
    echo "$@" | awk -F. '{ printf("%03d%03d%03d\n", $1,$2,$3); }';
}

function check_zookeeper {
    kubectl exec -i zookeeper-3 -- bash -c "echo srvr | nc localhost  2181 | grep Mode | cut -d' ' -f2"
}

function start_core_services {
    echo "Starting core servces"
    kubectl create -f ${STARTUP_DIR}/../conf/mysql.json
    kubectl create -f ${STARTUP_DIR}/../conf/memcache.json
    kubectl create -f ${STARTUP_DIR}/../conf/zookeeper.json
    kubectl create -f ${STARTUP_DIR}/../conf/control.json
    kubectl create -f ${STARTUP_DIR}/../conf/influxdb-grafana.json

    while true; do
        non_running_states=$(get_non_running_states)
        if [[ "$non_running_states" == "0" ]]; then
            break
        else
	    kubectl get pods
            echo "Waiting for pods to be running as found $non_running_states in non-running state"
            echo "Sleeping for 5 seconds..."
            sleep 5
        fi
    done
    while true; do
        zookeeper_mode=$(check_zookeeper)
        if [[ "$zookeeper_mode" == "leader" || "$zookeeper_mode" == "follower" ]]; then
            break
        else
            echo "Waiting for zookeeper to be ready as status check returned $zookeeper_mode"
            sleep 1
        fi
    done

    kubectl create -f ${STARTUP_DIR}/../conf/kafka.json
    kubectl create -f ${STARTUP_DIR}/../conf/td-agent-server.json
    while true; do
        non_running_states=$(get_non_running_states)
        if [[ "$non_running_states" == "0" ]]; then
            break
        else
	    kubectl get pods
            echo "Waiting for kafka and td-agent-server to be running as found $non_running_states in non-running state"
            echo "Sleeping for 5 seconds..."
            sleep 5
        fi
    done

}


function start_api_server {
    echo "Starting Seldon API server"
    kubectl create -f ${STARTUP_DIR}/../conf/server.json
}

function setup_basic_conf {

    #push any existing conf to zookeeper
    ${STARTUP_DIR}/seldon-cli client --action zk_push
    # Setup memcached
    ${STARTUP_DIR}/seldon-cli memcached --action setup --servers memcached1:11211,memcached2:11211
    ${STARTUP_DIR}/seldon-cli memcached --action commit
    # Setup DB
    ${STARTUP_DIR}/seldon-cli db --action setup --db-name ClientDB --db-jdbc 'jdbc:mysql:replication://mysql:3306,mysql:3306/?characterEncoding=utf8&useServerPrepStmts=true&logger=com.mysql.jdbc.log.StandardLogger&roundRobinLoadBalance=true&transformedBitIsBoolean=true&rewriteBatchedStatements=true'
    echo "seldon-cli db --action commit" | ${KCMD}
    # Setup Test client
    ${STARTUP_DIR}/seldon-cli client --action setup --db-name ClientDB --client-name test

}

function start_spark {
    if $SELDON_WITH_SPARK ; then
        echo 'Creating Spark Cluster'
        kubectl create -f ${STARTUP_DIR}/../conf/spark-master.json
        while true; do
            non_running_states=$(get_non_running_states)
            if [[ "$non_running_states" == "0" ]]; then
                break
            else
		kubectl get pods
                echo "Waiting for pods to be running as found $non_running_states in non-running state"
                echo "Sleeping for 3 seconds..."
                sleep 3
            fi
        done
        kubectl create -f ${STARTUP_DIR}/../conf/spark-workers.json
    fi
}

function start_analytics {
    kubectl create -f ${STARTUP_DIR}/../conf/analytics/impressions-kafka-stream.json
    kubectl create -f ${STARTUP_DIR}/../conf/analytics/predictions-kafka-stream.json
}

function start_glusterfs_service {
    if $SELDON_WITH_GLUSTERFS ; then
	echo 'Creating Glusterfs service'
	kubectl create -f ${STARTUP_DIR}/../conf/glusterfs.json
    fi
}


function setup_influxdb {

    INFLUXDB_POD=`kubectl get pods -l name=influxGrafana | sed 1d | cut -d' ' -f1 |sed -e 's/^[ \t]*//'`
    kubectl exec ${INFLUXDB_POD} -- influx --execute 'create database IF NOT EXISTS seldon'

}

function startup_msg {
    local SELDON_VERSION_INSTALLED=$(cat ${SELDON_HOME}/pom.xml|grep -A 1 '<artifactId>seldon-parent</artifactId>'|sed -n -e "s/<version>\(.*\)<\/version>/\1/p")
    local SELDON_VERSION_INSTALLED=$(echo -n "${SELDON_VERSION_INSTALLED}" | sed "s/^[ \t]*//")
    local SELDON_VERSION_URL="http://version.seldon.io/seldon-server/version.txt"
    local SELDON_VERSION_RELEASED=$(curl -fsL --max-time 5 ${SELDON_VERSION_URL} || echo '0.0.0')
    local UPDATE_MSG=
    if [ $(normalize_version ${SELDON_VERSION_RELEASED}) -gt $(normalize_version ${SELDON_VERSION_INSTALLED}) ]; then
        UPDATE_MSG=", latest released version is [${SELDON_VERSION_RELEASED}]"
    fi

    local MSG="Starting seldon version [${SELDON_VERSION_INSTALLED}]${UPDATE_MSG}"
    echo "${MSG}"
}

function seldon_up {

    startup_msg
{'fraction_non_alphanumeric': 0.12474114978798935, 'fraction_numerical': 0.010847056503303422, 'mean_word_length': 2.8828483920367534, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4901675', 'n_tokens_mistral': 3602, 'n_tokens_neox': 3405, 'n_words': 799}