Skip to content

Commit

Permalink
clean up log output
Browse files Browse the repository at this point in the history
  • Loading branch information
Pat-Ayres committed Dec 14, 2023
1 parent 100a266 commit 662e671
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
38 changes: 20 additions & 18 deletions create-k8s-chained-sessions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# Do not run this script more than once without resetting your Leapp
# instance beforehand.

. ./utils.sh

# global variables
declare REGION='us-east-1'

Expand All @@ -16,6 +18,7 @@ declare REGION='us-east-1'
# 1: parent session id name from leapp
# 2: role name to use for the chained session
function createLeappSession {
green_echo "creating chained session for $1 with role $2"
parent_session_name=$1
parent_role_name=$2
chained_role_name=$3
Expand All @@ -24,35 +27,35 @@ function createLeappSession {
# don't want to create a chained session for them.
parent_session_id=$(leappSessionId "$parent_session_name" "$parent_role_name")
if [[ -z "${parent_session_id}" ]]; then
echo "No parent session found for ${parent_session_name} with role ${parent_role_name}"
green_echo " No parent session found for ${parent_session_name} with role ${parent_role_name}"
return
fi

chained_session_name="${parent_session_name}-${chained_role_name}"

echo "looking for existing session ${chained_session_name}"
green_echo " looking for existing session ${chained_session_name}"
chained_session_id=$(leappSessionId "$chained_session_name" "$chained_role_name")

if [[ -z "${chained_session_id}" ]]; then
echo "no existing session found; starting session for ${parent_session_name} to get role arn"
# start leapp session
leapp session start --sessionId "$parent_session_id"
# call to aws to get the role arn for `TerraformRole`
green_echo " no existing session found; starting session for ${parent_session_name} to get role arn"

# use the parent session to get the role arn
# so we don't have to hard-code account ids
leapp session start --sessionId "$parent_session_id" > /dev/null 2> >(logStdErr)
role_arn=$(aws iam get-role --role-name "$chained_role_name" --query Role.Arn | tr -d '"')
# stop the leapp session
leapp session stop --sessionId "$parent_session_id"
# create a named profile per account so they can be used simultaneously
echo "creating new profile"
leapp session stop --sessionId "$parent_session_id" > /dev/null 2> >(logStdErr)

green_echo " creating new profile"
profile_id=$(createLeappProfile "$parent_session_name")
echo "creating new session"
# create new chained leapp session from parent

green_echo " creating new session"
leapp session add --providerType aws --sessionType awsIamRoleChained \
--sessionName "$chained_session_name" --region "$REGION" \
--roleArn "$role_arn" --parentSessionId "$parent_session_id" \
--profileId "$profile_id"
--profileId "$profile_id" > /dev/null 2> >(logStdErr)

else
echo "existing session found"
yellow_echo " existing session found"
fi
}

Expand All @@ -72,18 +75,17 @@ function createLeappProfile {
# match e.g. both `kubectl-access-role-panorama-k8s-playground` and
# `kubectl-access-role-panorama-k8s-playground-2`.
profile_name="kubectl-access-role-${1}"
profile_id=$(leapp profile list -x --output json --filter="Profile Name=^${profile_name}$" | jq -r '.[0].id')
profile_id=$(leapp profile list -x --output json --filter="Profile Name=^${profile_name}$" | jq -r '.[].id')
if [[ -n "${profile_id}" ]]; then
echo "${profile_id}"
return
fi
leapp profile create --profileName "$profile_name"
leapp profile list -x --output json --filter="Profile Name=^${profile_name}$" | jq -r '.[0].id'
leapp profile create --profileName "$profile_name" > /dev/null 2> >(logStdErr)
leapp profile list -x --output json --filter="Profile Name=^${profile_name}$" | jq -r '.[].id'
}
#
###### END FUNCTIONS ######

echo "Creating Leapp Chained k8s sessions for k8s accounts"
# session names from Leapp for each k8s account
PARENT_SESSION_NAMES="panorama-k8s-playground panorama-k8s-playground-2 panorama-k8s-staging panorama-k8s-production"

Expand Down
2 changes: 1 addition & 1 deletion setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
MIN_OS_VERSION="12.4.0"
CURRENT_OS_VERSION=$(sw_vers -productVersion)

red_echo () { echo -ne "\033[1;31m"; echo -n "$@"; echo -e "\033[0m"; }
. ./utils.sh

# use version sorting to check if the current version is less than $MIN_OS_VERSION
if [[ $MIN_OS_VERSION != "$(printf "$MIN_OS_VERSION\n$CURRENT_OS_VERSION" | sort -V | sed -n 1p)" ]]; then
Expand Down
17 changes: 17 additions & 0 deletions utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
# A collection of utility functions to be sourced by other scripts

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

red_echo () { echo -e "${RED}$*${NC}"; }
green_echo () { echo -e "${GREEN}$*${NC}"; }
yellow_echo () { echo -e "${YELLOW}$*${NC}"; }

logStdErr() {
while read -r line; do
red_echo "$line" >&2
done
}

0 comments on commit 662e671

Please sign in to comment.