Last active
April 18, 2024 18:39
-
-
Save reubenmiller/06343423009c960473ce24d2e3a3edce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# 1. Pipe a list of ESNs from a file and create a c8y_Command (shell) operation for each device (using go-c8y-cli piping) | |
# A unique batch marker is used to group the operations together (to make querying for the operation easier) | |
# 2. Poll the created operations () | |
# 3. Print out a summary of how many operations failed and how man were successful | |
# | |
# Input file: | |
# * esn.txt - Newline delimited file containing one ESN per line (file must have an empty line at the end, otherwise the last ESN might be skipped) | |
# | |
# | |
# Output files: | |
# * operations.jsonl - List of all created operations (in their original state when they were created) | |
# * operations.jsonl.failed = List of operations which are in the FAILED status | |
# | |
set -e | |
# go-c8y-cli global settings | |
# Uncomment to disable prompts | |
#export CI=true | |
# Use caching to make these action idempotent | |
export C8Y_SETTINGS_DEFAULTS_CACHETTL='30d' | |
# Inputs | |
FILENAME_TDT="esn.txt" | |
# Output | |
OPERATIONS_LIST="operations.jsonl" | |
info () { | |
echo "INFO $*" >&2 | |
} | |
batch_marker="action_20240417_enable_voip_run01" | |
# | |
# Creating the operations (this is idempotent due to the local disk caching) | |
# | |
cat "$FILENAME_TDT" \ | |
| c8y devices list --queryTemplate "c8y_Hardware.serialNumber eq '%s'" --pageSize 1 \ | |
| c8y operations create \ | |
--description "Canary: Enable VoIP" \ | |
--cache \ | |
--template "{ | |
'$batch_marker':{}, | |
c8y_Command: { | |
'text': 'ta-auto-configuration ENABLE_VOIP' | |
} | |
}" > "$OPERATIONS_LIST" | |
TOTAL_OPERATIONS=$(wc -l < "$OPERATIONS_LIST" | xargs) | |
# | |
# Wait for all operations to finish | |
# | |
ITERATIONS=1 | |
MAX_ITERATIONS=60 | |
count_operations() { | |
c8y operations list \ | |
--fragmentType "$batch_marker" \ | |
--status "$1" \ | |
--dateFrom -2d \ | |
--pageSize 1 \ | |
--withTotalPages \ | |
--select statistics.totalPages -o csv | |
} | |
while :; do | |
SUCCESSFUL_COUNT=$(count_operations SUCCESSFUL) | |
FAILED_COUNT=$(count_operations FAILED) | |
REMAINING_COUNT=$((TOTAL_OPERATIONS - SUCCESSFUL_COUNT - FAILED_COUNT)) | |
if [ "$REMAINING_COUNT" -le 0 ] || [ "$ITERATIONS" -gt "$MAX_ITERATIONS" ]; then | |
info "No more operation to process" | |
break | |
fi | |
info "[iteration=$ITERATIONS] Current progress. IN_PROGRESS=$REMAINING_COUNT, SUCCESSFUL=$SUCCESSFUL_COUNT, FAILED=$FAILED_COUNT" | |
# Tune to your liking...it does not have to be done super fast | |
sleep 5 | |
ITERATIONS=$((ITERATIONS + 1)) | |
done | |
# | |
# Print out the summary of the operations | |
# | |
echo "--------------------------------------------------------------------" | |
echo " Summary" | |
echo "--------------------------------------------------------------------" | |
FAILED_COUNT=$(count_operations FAILED) | |
echo "PENDING: $(count_operations PENDING)" | |
echo "EXECUTING: $(count_operations EXECUTING)" | |
echo "SUCCESSFUL: $(count_operations SUCCESSFUL)" | |
echo "FAILED: $FAILED_COUNT" | |
if [ "$FAILED_COUNT" -gt 0 ]; then | |
# List failed devices only | |
c8y operations list \ | |
--fragmentType "$batch_marker" \ | |
--status "FAILED" \ | |
--includeAll > "${OPERATIONS_LIST}.failed" | |
echo | |
echo "Review the list of devices where the command failed" | |
echo | |
echo " ${OPERATIONS_LIST}.failed" | |
echo | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment