<commit_before>#!/usr/bin/env bash

set -e

export STORE_ENDPOINTS=$NODE_NAME:2379
export STSENTINEL_STORE_ENDPOINTS=$STORE_ENDPOINTS
export STKEEPER_STORE_ENDPOINTS=$STORE_ENDPOINTS
export STPROXY_STORE_ENDPOINTS=$STORE_ENDPOINTS
export STOLONCTL_STORE_ENDPOINTS=$STORE_ENDPOINTS

function die() {
    echo "ERROR: $*" >&2
    exit 1
}

function announce_step() {
    echo
    echo "===> $*"
    echo
}

function setup_cluster_ca() {
	announce_step 'Setup cluster CA'

	if [ -f /usr/local/bin/kubectl ]; then
		mkdir -p /usr/share/ca-certificates/extra
		kubectl get secret cluster-ca
		if [ $? -eq 0 ]; then
			kubectl get secret cluster-ca -o yaml|grep ca.pem|awk '{print $2}'|base64 -d > /usr/local/share/ca-certificates/cluster.crt
			update-ca-certificates
		fi
	fi
}

function chmod_keys() {
	cp -R /etc/ssl/cluster-default /etc/ssl/cluster-default-postgres
	chown -R stolon /etc/ssl/cluster-default-postgres
	chmod 0600 /etc/ssl/cluster-default-postgres/default-server-key.pem
}


# TODO: find how to map ENV vars which is populated by k8s services for discovery to vars, which utilities uses
function setup_stolonctl() {
	announce_step 'Setup stolon CLI'

	_create_pg_pass "$STOLON_POSTGRES_SERVICE_HOST" \
				   "$STOLON_POSTGRES_SERVICE_PORT" \
				   "*" \
				   "$STOLONCTL_DB_USERNAME" \
				   "$STOLON_DB_PASSWORD"

	cat > /usr/local/bin/stolonctl-cluster <<'EOF'
	export STOLONCTL_DB_HOST=$STOLON_POSTGRES_SERVICE_HOST
	export STOLONCTL_DB_PORT=$STOLON_POSTGRES_SERVICE_PORT
	stolonctl "$@"
EOF
	chmod +x /usr/local/bin/stolonctl-cluster
}

function setup_stolonrpc() {
	announce_step 'Setup stolon RPC'

	_create_pg_pass "$STOLON_POSTGRES_SERVICE_HOST" \
				   "$STOLON_POSTGRES_SERVICE_PORT" \
				   "*" \
				   "$STOLONRPC_DB_USERNAME" \
				   "$STOLON_DB_PASSWORD"

	cat > /usr/local/bin/stolonrpc-cluster <<'EOF'
	export STOLONRPC_DB_HOST=$STOLON_POSTGRES_SERVICE_HOST
	export STOLONRPC_DB_PORT=$STOLON_POSTGRES_SERVICE_PORT
	stolonrpc "$@"
EOF
	chmod +x /usr/local/bin/stolonrpc-cluster
}

function _create_pg_pass() {
	local host=${1:-$STOLON_POSTGRES_SERVICE_HOST}
	local port=${2:-$STOLON_POSTGRES_SERVICE_PORT}
	local database=${3:-"postgres"}
	local username=${4:-"stolon"}
	local password=${5}

	echo "$host:$port:$database:$username:$password" > ~/.pgpass
	chmod 0600 ~/.pgpass
}

function launch_keeper() {
	announce_step 'Launching stolon keeper'

	if [[ ! -e /stolon-data ]]; then
		echo "stolon data doesn't exist, data won't be persistent!"
		mkdir /stolon-data
	fi
	chown stolon:stolon /stolon-data

	export STKEEPER_LISTEN_ADDRESS=$POD_IP
	export STKEEPER_PG_LISTEN_ADDRESS=$POD_IP

	su stolon -c "stolon-keeper --data-dir /stolon-data"
}

function launch_sentinel() {
	announce_step 'Launching stolon sentinel'

	export STSENTINEL_LISTEN_ADDRESS=$POD_IP<commit_msg>Create dummy file with 300MB size<commit_after>#!/usr/bin/env bash

set -e

export STORE_ENDPOINTS=$NODE_NAME:2379
export STSENTINEL_STORE_ENDPOINTS=$STORE_ENDPOINTS
export STKEEPER_STORE_ENDPOINTS=$STORE_ENDPOINTS
export STPROXY_STORE_ENDPOINTS=$STORE_ENDPOINTS
export STOLONCTL_STORE_ENDPOINTS=$STORE_ENDPOINTS

function die() {
    echo "ERROR: $*" >&2
    exit 1
}

function announce_step() {
    echo
    echo "===> $*"
    echo
}

function setup_cluster_ca() {
	announce_step 'Setup cluster CA'

	if [ -f /usr/local/bin/kubectl ]; then
		mkdir -p /usr/share/ca-certificates/extra
		kubectl get secret cluster-ca
		if [ $? -eq 0 ]; then
			kubectl get secret cluster-ca -o yaml|grep ca.pem|awk '{print $2}'|base64 -d > /usr/local/share/ca-certificates/cluster.crt
			update-ca-certificates
		fi
	fi
}

function chmod_keys() {
	cp -R /etc/ssl/cluster-default /etc/ssl/cluster-default-postgres
	chown -R stolon /etc/ssl/cluster-default-postgres
	chmod 0600 /etc/ssl/cluster-default-postgres/default-server-key.pem
}


# TODO: find how to map ENV vars which is populated by k8s services for discovery to vars, which utilities uses
function setup_stolonctl() {
	announce_step 'Setup stolon CLI'

	_create_pg_pass "$STOLON_POSTGRES_SERVICE_HOST" \
				   "$STOLON_POSTGRES_SERVICE_PORT" \
				   "*" \
				   "$STOLONCTL_DB_USERNAME" \
				   "$STOLON_DB_PASSWORD"

	cat > /usr/local/bin/stolonctl-cluster <<'EOF'
	export STOLONCTL_DB_HOST=$STOLON_POSTGRES_SERVICE_HOST
	export STOLONCTL_DB_PORT=$STOLON_POSTGRES_SERVICE_PORT
	stolonctl "$@"
EOF
	chmod +x /usr/local/bin/stolonctl-cluster
}

function setup_stolonrpc() {
	announce_step 'Setup stolon RPC'

	_create_pg_pass "$STOLON_POSTGRES_SERVICE_HOST" \
				   "$STOLON_POSTGRES_SERVICE_PORT" \
				   "*" \
				   "$STOLONRPC_DB_USERNAME" \
				   "$STOLON_DB_PASSWORD"

	cat > /usr/local/bin/stolonrpc-cluster <<'EOF'
	export STOLONRPC_DB_HOST=$STOLON_POSTGRES_SERVICE_HOST
	export STOLONRPC_DB_PORT=$STOLON_POSTGRES_SERVICE_PORT
	stolonrpc "$@"
EOF
	chmod +x /usr/local/bin/stolonrpc-cluster
}

function _create_pg_pass() {
	local host=${1:-$STOLON_POSTGRES_SERVICE_HOST}
	local port=${2:-$STOLON_POSTGRES_SERVICE_PORT}
	local database=${3:-"postgres"}
	local username=${4:-"stolon"}
	local password=${5}

	echo "$host:$port:$database:$username:$password" > ~/.pgpass
	chmod 0600 ~/.pgpass
}

function launch_keeper() {
	announce_step 'Launching stolon keeper'

	if [[ ! -e /stolon-data ]]; then
		echo "stolon data doesn't exist, data won't be persistent!"
		mkdir /stolon-data
	fi
	chown -R stolon:stolon /stolon-data
    if [[ ! -f /stolon-data/dummy.file ]]; then
        fallocate -l 300MB /stolon.data/dummy.file
    fi

	export STKEEPER_LISTEN_ADDRESS=$POD_IP
	export STKEEPER_PG_LISTEN_ADDRESS=$POD_IP

	su stolon -c "stolon-keeper --data-dir /stolon-data"
}

function launch_sentinel() {
	announce_step 'Launching stolon sentinel'

	export STSENTINEL_LISTEN_ADDRESS=$POD_IP
{'fraction_non_alphanumeric': 0.11571254567600488, 'fraction_numerical': 0.009396206716547765, 'mean_word_length': 4.474285714285714, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '514731', 'n_tokens_mistral': 2426, 'n_tokens_neox': 2303, 'n_words': 416}