#!/bin/bash
. gather_service_logs_util

BASE_COLLECTION_PATH="/must-gather"
ROLES=${1:-master}         ### Defaults to only collecting things from Masters

# Service Lists
GENERAL_SERVICES=(kubelet crio machine-config-daemon-firstboot machine-config-daemon-host)
MASTER_SERVICES+=(${2:-"${GENERAL_SERVICES[@]}"})
MASTER_SERVICES+=()        ### Placeholder to extend Master only services
NODE_SERVICES+=(${2:-"${GENERAL_SERVICES[@]}"})
NODE_SERVICES+=()          ### Placeholder to extend Node only services

# Collect System Service Logs
SERVICE_LOG_PATH="${BASE_COLLECTION_PATH}/host_service_logs/"

# collect service logs expects PIDS to be an already defined array
PIDS=()
if [[ $ROLES == "master" ]]; then
    collect_service_logs --role=master ${MASTER_SERVICES[@]}
elif [[ $ROLES == "worker" ]]; then
    collect_service_logs --role=worker ${NODE_SERVICES[@]}
elif [[ $ROLES == "master,worker" ]] || [[ $ROLES == "worker,master" ]]; then
    collect_service_logs --role=master ${MASTER_SERVICES[@]}
    collect_service_logs --role=worker ${NODE_SERVICES[@]}
else
    echo "Error: no selector supplied or an invalid selector was supplied." >&2
    echo "Error: valid selectors are '--role=master', '--role=worker', '--rolemaster,worker', '--role=worker,master' the node name." >&2
fi

echo "INFO: Waiting for worker host service log collection to complete ..."
wait "${PIDS[@]}"
echo "INFO: Worker host service log collection to complete."

# force disk flush to ensure that all data gathered is accessible in the copy container
sync
