#!/bin/bash

# Copyright 2019 Red Hat, Inc.
#
# 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.

#
# Start/stop AFS servers
#
# Usage:
#
#   1) run this authenticated with admin account
#      (mirror-update.opendev.org is one choice with everything setup,
#      but can do locally)
#
#       $ kinit $username/admin
#       $ aklog
#
#   2) afs-service-restart.sh <restart|restart-auditing|start|start-auditing|stop>
#
#   3) If enabled, audit logs will be in a timestampped /opt file
#
#

SERVERS=(afs01.dfw.openstack.org afs02.dfw.openstack.org afs01.ord.openstack.org)

# See AFS system-config documentation for discussion of parameters
DAFILESERVER_OPTS=" -L -p 242 -busyat 600 -rxpck 700 -s 1200 -l 1200 -cb 2000000 -b 240 -vc 1200 -udpsize 131071 -sendsize 131071"

TIMESTAMP=$(date +%s)

function stop_server {
    local afs_host=$1

    echo "Stopping server $afs_host"
    bos stop -server $afs_host -instance dafs -wait
    sleep 5
    bos delete -server $afs_host -instance dafs
    sleep 5
    echo "Done"
}

function start_server {
    local afs_host=$1
    local audit_log=$2
    local opts=$DAFILESERVER_OPTS
    local with_log=""

    if [[ $audit_log == "true" ]]; then
        opts+=" -auditlog /opt/dafileserver.audit.$TIMESTAMP.log"
        with_log=" (with audit logging) "
    fi

    echo "Enable server $afs_host $with_log"

    bos create -server $afs_host -instance dafs -type dafs \
        -cmd "/usr/lib/openafs/dafileserver ${opts}" \
        -cmd /usr/lib/openafs/davolserver \
        -cmd /usr/lib/openafs/salvageserver \
        -cmd /usr/lib/openafs/dasalvager
}

if [[ $1 == "start" ]]; then

    for server in ${SERVERS[@]}; do
        start_server $server false
    done

elif [[ $1 == "start-auditing" ]]; then

    for server in ${SERVERS[@]}; do
        start_server $server true
    done

elif [[ $1 == "restart" ]]; then

    for server in ${SERVERS[@]}; do
        stop_server $server
        sleep 2
        start_server $server false
    done

elif [[ $1 == "restart-auditing" ]]; then

    for server in ${SERVERS[@]}; do
        stop_server $server
        sleep 2
        start_server $server true
    done

elif [[ $1 == "stop" ]]; then

    echo "*** JUST CHECKING YOU REALLY WANT TO STOP ALL SERVERS ***"
    echo "It may be better to restart so the servers aren't all offline"
    echo "I will wait 10 seconds, ctrl-c to cancel now"
    sleep 10

    for server in ${SERVERS[@]}; do
        stop_server $server
    done

else

    echo "*** usage: <restart|restart-auditing|start|start-auditing|stop>"
    exit 1

fi