#!/bin/bash

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.

set -ex

if ! charms.reactive is_state 'giraph.installed'; then
    action-fail 'Giraph is not yet ready'
    exit
fi

# create dir to store results
RUN=`date +%s`
RESULT_DIR=/opt/giraph-smoke-results
RESULT_LOG=${RESULT_DIR}/${RUN}.log
mkdir -p ${RESULT_DIR}
chown -R ubuntu:ubuntu ${RESULT_DIR}

# hdfs dirs
GIRAPH_SMOKE="/tmp/giraph-smoke"
GIRAPH_INPUT="${GIRAPH_SMOKE}/input"
GIRAPH_OUTPUT="${GIRAPH_SMOKE}/output"

# remove any previous smoke test run. must be run as ubuntu, since that user
# owns the hdfs space
su - ubuntu -c "hadoop fs -rm -f -r -skipTrash ${GIRAPH_SMOKE}"

echo 'running giraph smoke-test as the ubuntu user'
# NB: Escaped envars in the block below (e.g., \${GIRAPH_JARS}) come from
# the environment while non-escaped vars (e.g., ${GIRAPH_INPUT}) come from
# this outer scope.
su ubuntu << EOF
set -x
. /etc/default/hadoop
. /etc/environment

# setup our smoke test input
hdfs dfs -mkdir -p ${GIRAPH_INPUT}
hdfs dfs -put \${CHARM_DIR}/resources/tiny_graph.txt ${GIRAPH_INPUT}

hadoop jar /usr/lib/giraph/giraph-core.jar \
  org.apache.giraph.GiraphRunner \
  -libjars \${GIRAPH_JARS} \
  org.apache.giraph.examples.SimplePageRankComputation \
  -vif org.apache.giraph.io.formats.JsonLongDoubleFloatDoubleVertexInputFormat \
  -vip ${GIRAPH_INPUT}/tiny_graph.txt \
  -mc org.apache.giraph.examples.SimplePageRankComputation\\\$SimplePageRankMasterCompute \
  -vof org.apache.giraph.io.formats.IdWithValueTextOutputFormat \
  -op ${GIRAPH_OUTPUT} \
  -w 1 \
  -ca mapred.job.tracker=empty \
  yarn.log-aggregation-enable=true 2>&1 | tee -a ${RESULT_LOG}
EOF
echo 'giraph smoke-test complete'

# Set action output
RAW=`cat ${RESULT_LOG}`
action-set raw="${RAW}"
