Skip to content

Commit

Permalink
Added deployment time tracking, memory, and disk space usage monitori…
Browse files Browse the repository at this point in the history
…ng in mojafos bash scripts
  • Loading branch information
Abhinavcode13 committed Jul 10, 2024
1 parent 8f942ef commit 23390a1
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 38 deletions.
35 changes: 30 additions & 5 deletions src/mojafos/commandline/commandline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ function getoptions {
}

# this function is called when Ctrl-C is sent
function cleanUp ()
{
function cleanUp () {
# perform cleanup here
echo -e "${RED}Performing graceful clean up${RESET}"

Expand All @@ -104,6 +103,30 @@ function trapCtrlc {
# when signal 2 (SIGINT) is received
trap "trapCtrlc" 2

function getMemoryUsage() {
kubectl top pod --all-namespaces | grep -E 'mifos|mojaloop|phee'
}

function getDiskUsage() {
du -sh /var/lib/kubelet/pods
}

function measureTime() {
local start_time=$(date +%s)
$@
local end_time=$(date +%s)
local elapsed=$(( end_time - start_time ))
echo "Time taken: $(($elapsed / 60)) minutes and $(($elapsed % 60)) seconds."
}

function monitorResources() {
echo "Memory usage by components:"
getMemoryUsage

echo "Disk usage:"
getDiskUsage
}

###########################################################################
# MAIN
###########################################################################
Expand All @@ -116,11 +139,13 @@ function main {
echo -e "The deployment made by this script is meant for demo purposes and not for production"
echo -e "===================================================================================="
echo -e "${RESET}"
envSetupMain "$mode" "k3s" "1.26" "$environment"
deployApps "$fineract_instansces" "$apps"
measureTime envSetupMain "$mode" "k3s" "1.26" "$environment"
measureTime deployApps "$fineract_instansces" "$apps"
monitorResources
elif [ $mode == "cleanup" ]; then
logWithVerboseCheck $debug info "Cleaning up all traces of Mojafos"
envSetupMain "$mode" "k3s" "1.26" "$environment"
measureTime envSetupMain "$mode" "k3s" "1.26" "$environment"
monitorResources
else
showUsage
fi
Expand Down
116 changes: 83 additions & 33 deletions src/mojafos/configurationManager/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ FIN_NAMESPACE="fineract"
FIN_RELEASE_NAME="fineract"
FIN_VALUES_FILE="$BASE_DIR/src/mojafos/deployer/fin_values.yaml"

# Timers
START_TIME=$(date +%s)

########################################################################
# FUNCTIONS FOR CONFIGURATION MANAGEMENT
Expand All @@ -70,40 +72,27 @@ function replaceValuesInFiles() {
return 1
fi

# Read the JSON file and create an associative array
declare -A replacements
while IFS= read -r json_object; do
local old_value new_value
old_value=$(echo "$json_object" | jq -r '.old_value')
new_value=$(echo "$json_object" | jq -r '.new_value')
replacements["$old_value"]="$new_value"
done < <(jq -c '.[]' "$json_file")

# Loop through the directories and process each file
for dir in "${directories[@]}"; do
if [ -d "$dir" ]; then
find "$dir" -type f | while read -r file; do
local changed=false
for old_value in "${!replacements[@]}"; do
if grep -q "$old_value" "$file"; then
sed -i "s|$old_value|${replacements[$old_value]}|g" "$file"
changed=true
fi
done
if $changed; then
echo "Updated: $file"
fi
done
else
echo "Directory $dir does not exist."
fi
done
}
# Loop over JSON objects in the file and call the process_json_object function
jq -c '.[]' "$json_file" | while read -r json_object; do
local file_name
local old_value
local new_value

function configureMojaloop() {
replaceValuesInFiles "${MOJALOOP_LAYER_DIRS[0]}" "${MOJALOOP_LAYER_DIRS[2]}" "${MOJALOOP_LAYER_DIRS[3]}"
}
# Extract attributes from the JSON object
file_name=$(echo "$json_object" | jq -r '.file_name')
old_value=$(echo "$json_object" | jq -r '.old_value')
new_value=$(echo "$json_object" | jq -r ".new_value")

# Call the function with the extracted attributes
replaceValuesInFile "$file_name" "$old_value" "$new_value"
done

if [ $? -eq 0 ]; then
echo -e "\n==> Mojaloop Manifests edited successfully"
else
echo -e "${RED}Mojaloop Manifests were not edited successfully${RESET}"
fi
}

function createSecret(){
local namespace="$1"
Expand Down Expand Up @@ -158,4 +147,65 @@ function configurePH() {

function configureFineract(){
echo -e "${BLUE}Configuring fineract ${RESET}"
}
}

########################################################################
# FUNCTION TO MONITOR DEPLOYMENT
########################################################################
function monitorDeployment() {
echo -e "${BLUE}Monitoring Deployment ${RESET}"

echo -e "Waiting for all pods to be in 'Running' state..."
start_time=$(date +%s)
while true; do
not_running_pods=$(kubectl get pods --all-namespaces --field-selector=status.phase!=Running | wc -l)
if [ "$not_running_pods" -eq 0 ]; then
echo "All pods are in 'Running' state."
break
fi
sleep 5
done
end_time=$(date +%s)
total_time=$((end_time - start_time))
echo -e "${GREEN}All pods are in 'Running' state. Total deploy time: ${total_time}s${RESET}"
}

########################################################################
# FUNCTION TO CHECK MEMORY USAGE
########################################################################
function checkMemoryUsage() {
echo -e "${BLUE}Checking Memory Usage ${RESET}"
kubectl top pods --all-namespaces | awk '
NR==1 {print $0}
/mojaloop|paymenthub|fineract/ {print $0}'
}

########################################################################
# FUNCTION TO CHECK DISK SPACE USAGE
########################################################################
function checkDiskSpaceUsage() {
echo -e "${BLUE}Checking Disk Space Usage ${RESET}"
df -h | awk 'NR==1 || /\/$/ {print $0}'
}

########################################################################
# MAIN EXECUTION
########################################################################

# Run configuration functions
configureMojaloop
configurePH "$PHREPO_DIR"
configureFineract

# Monitor deployment
monitorDeployment

# Check memory and disk space usage
checkMemoryUsage
checkDiskSpaceUsage

END_TIME=$(date +%s)
TOTAL_DEPLOY_TIME=$((END_TIME - START_TIME))
echo -e "${GREEN}Total deployment time: ${TOTAL_DEPLOY_TIME}s${RESET}"

echo -e "${GREEN}Deployment and configuration complete.${RESET}"

0 comments on commit 23390a1

Please sign in to comment.