Skip to content

Commit

Permalink
Merge pull request #242 from aws-samples/pcluster-util-fetch-config
Browse files Browse the repository at this point in the history
pcluster: add a small util script to fetch config from a running cluster
  • Loading branch information
verdimrc authored Apr 8, 2024
2 parents b342252 + dfbeeb6 commit c1c6ab5
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions 1.architectures/2.aws-parallelcluster/pcluster-fetch-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/bash

set -eo pipefail

# Dependency: jq (required), bat or yq (optionals)

declare -a HELP=(
"[-h|--help]"
"[-r|--region REGION]"
"CLUSTER_NAME"
"[-s|--syntax-highlighter <cat|bat|jq> (default: auto detect)]"
)

IS_SYNTAX_HIGHLIGHTER_ARGS=0
region=""
cluster_name=""
declare -a pcluster_args=()
declare -a syntax_highlighter=()
parse_args() {
local key
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h|--help)
echo "Get the cluster config YAML of a ParallelCluster cluster."
echo "Usage: $(basename ${BASH_SOURCE[0]}) ${HELP[@]}"
exit 0
;;
-r|--region)
region="$2"
shift 2
;;
-s|--syntax-highlighter)
syntax_highlighter=( "$2" )
shift 2
;;
--)
SYNTAX_HIGHLIGHTER_ARGS=1
shift
;;
*)
if [[ $IS_SYNTAX_HIGHLIGHTER_ARGS == 0 ]]; then
[[ "$cluster_name" == "" ]] \
&& cluster_name="$key" \
|| { echo "Must define one cluster name only" ; exit -1 ; }
else
syntax_highlighter_args+=($key)
fi
shift
;;
esac
done

[[ "$cluster_name" != "" ]] || { echo "Must define a cluster name" ; exit -1 ; }
[[ "${region}" == "" ]] || { pcluster_args+=(--region $region) ; }
}

parse_args $@

cluster_config_url=$(pcluster describe-cluster -n $cluster_name "${pcluster_args[@]}" | jq -r .clusterConfiguration.url)
cluster_config=$(curl --silent "$cluster_config_url")

# By default, auto-detect the syntax highlighter
if [[ ${#syntax_highlighter[@]} -eq 0 ]]; then
if command -v bat &> /dev/null; then
syntax_highlighter=( bat -pp --language yaml )
elif command -v batcat &> /dev/null; then
syntax_highlighter=( batcat -pp --language yaml )
elif command -v yq &> /dev/null; then
syntax_highlighter=( yq )
else
syntax_highlighter=( cat )
fi
fi

echo "$cluster_config" | "${syntax_highlighter[@]}"

0 comments on commit c1c6ab5

Please sign in to comment.