github.com/arduino/arduino-cloud-cli@v0.0.0-20240517070944-e7a449561083/example/tools/ota/ota_mass_upload.sh (about)

     1  #!/bin/bash
     2  
     3  # This script is used to upload the firmware to the device using the OTA service.
     4  
     5  export PATH=$PATH:.
     6  
     7  checkExecutable () {
     8      if ! command -v $1 &> /dev/null
     9      then
    10          echo "$1 could not be found in PATH"
    11          exit 1
    12      fi
    13  }
    14  
    15  printHelp () {
    16      echo ""
    17      echo "Usage: $0 [-t <tag> | -d <device ids>] -f <firmwarefile> [-o <waittime in seconds - default 600>] [-v <new firmware version for tagging updated devices>]"
    18      echo ""
    19      echo "Examples -----------------"
    20      echo "  perform ota on devices with firmware=v1 tag"
    21      echo "    $0 -t firmware=v1 -f myfirmware.bin"
    22      echo "  perform ota on devices with firmware=v1 tag and apply new tag firmware=v2 to updated devices, waiting for 1200 seconds"
    23      echo "    $0 -t firmware=v1 -f myfirmware.bin -v firmware=v2 -o 1200"
    24      echo "  perform ota on two specified devices"
    25      echo "    $0 -d 261ec96a-38ba-4520-96e6-2447c4163e9b,8b10acdb-b722-4068-8e4d-d1c1b7302df4 -f myfirmware.bin"    
    26      echo ""
    27      exit 1
    28  }
    29  
    30  # Check dependencies...
    31  checkExecutable "arduino-cloud-cli"
    32  checkExecutable "jq"
    33  checkExecutable "sort"
    34  checkExecutable "uniq"
    35  checkExecutable "paste"
    36  
    37  # Default wait time for OTA process to complete
    38  waittime=900
    39  newtagversion=""
    40  
    41  while getopts t:v:f:o:d: flag
    42  do
    43      case "${flag}" in
    44          t) tag=${OPTARG};;
    45          v) newtagversion=${OPTARG};;
    46          f) firmwarefile=${OPTARG};;
    47          o) waittime=${OPTARG};;
    48          d) deviceids=${OPTARG};;
    49      esac
    50  done
    51  
    52  if [[ "$firmwarefile" == "" || "$waittime" == "" ]]; then
    53      printHelp    
    54  fi
    55  if [[ "$tag" == "" && "$deviceids" == "" ]]; then
    56      printHelp    
    57  fi
    58  
    59  if [[ "$deviceids" == "" ]]; then
    60      echo "Starting OTA process for devices with tag \"$tag\" using firmware \"$firmwarefile\""
    61      echo ""
    62  
    63      devicelistjson=$(arduino-cloud-cli device list --tags $tag --format json)
    64  else
    65      echo "Starting OTA process for devices \"$deviceids\" using firmware \"$firmwarefile\""
    66      echo ""
    67  
    68      devicelistjson=$(arduino-cloud-cli device list -d $deviceids --format json)
    69  fi
    70  
    71  if [[ "$devicelistjson" == "" || "$devicelistjson" == "null" ]]; then
    72      echo "No device found"
    73      exit 1    
    74  fi
    75  
    76  devicecount=$(echo $devicelistjson | jq '.[] | .id' | wc -l)
    77  
    78  if [ "$devicecount" -gt 0 ]; then
    79      echo "Found $devicecount devices"
    80      echo ""
    81      if [[ "$deviceids" == "" ]]; then
    82          arduino-cloud-cli device list --tags $tag
    83      else
    84          arduino-cloud-cli device list -d $deviceids
    85      fi
    86  else 
    87      echo "No device found"
    88      exit 1    
    89  fi
    90  
    91  fqbncount=$(echo $devicelistjson | jq '.[] | .fqbn' | sort | uniq | wc -l)
    92  
    93  if [ "$fqbncount" -gt 1 ]; then
    94      echo "Mixed FQBNs detected. Please ensure all devices have the same FQBN."
    95      fqbns=$(echo $devicelistjson | jq '.[] | .fqbn' | sort | uniq)
    96      echo "Detected FQBNs:"
    97      echo "$fqbns"
    98      exit 1    
    99  fi
   100  
   101  fqbn=$(echo $devicelistjson | jq -r '.[] | .fqbn' | sort | uniq | head -n 1)
   102  
   103  echo "Sending OTA request to detected boards of type $fqbn..."
   104  if [[ "$deviceids" == "" ]]; then
   105      otastartedout=$(arduino-cloud-cli ota mass-upload --device-tags $tag --file $firmwarefile -b $fqbn --format json)
   106  else
   107      otastartedout=$(arduino-cloud-cli ota mass-upload -d $deviceids --file $firmwarefile -b $fqbn --format json)
   108  fi
   109  if [ $? -ne 0 ]; then
   110      echo "Detected error during OTA process. Exiting..."
   111      exit 1    
   112  fi
   113  
   114  otaids=$(echo $otastartedout | jq -r '.[] | .OtaStatus | .id' | uniq | paste -sd "," -)
   115  
   116  if [ $otaids == "null" ]; then
   117      echo "No OTA processes to monitor. This could be due to an upgrade from previous ArduinoIotLibrary versions. Exiting..."
   118      if [ "$newtagversion" != "" ]; then
   119          otasucceeded=$(echo $devicelistjson | jq -r '.[] | .id' | uniq | paste -sd "," -)
   120          echo ""
   121          echo "Tag updated devices as \"$newtagversion\""
   122          arduino-cloud-cli device create-tags --ids $otasucceeded --tags $newtagversion
   123          echo ""
   124          arduino-cloud-cli device list --tags $newtagversion
   125      fi    
   126      exit 0
   127  fi
   128  
   129  correctlyfinished=0
   130  while [ $waittime -gt 0 ]; do
   131      echo "Waiting for $waittime seconds for OTA process to complete..."
   132      sleep 15
   133      waittime=$((waittime-15))
   134      # Check status of running processess...
   135      otastatuslines=$(arduino-cloud-cli ota status --ota-ids $otaids --format json)
   136      otastatusinpcnt=$(echo $otastatuslines | grep in_progress | wc -l)
   137      otastatuspencnt=$(echo $otastatuslines | grep pending | wc -l)
   138      otasucceeded=$(echo $otastatuslines | jq -r '.[] | select (.status | contains("succeeded")) | .device_id' | uniq | paste -sd "," -)
   139      if [[ $otastatusinpcnt -eq 0 && $otastatuspencnt -eq 0 ]]; then
   140          correctlyfinished=1
   141          break   
   142      fi
   143  done
   144  
   145  echo ""
   146  echo "Status report:"
   147  arduino-cloud-cli ota status --ota-ids $otaids
   148  
   149  exitcode=0
   150  if [ $correctlyfinished -eq 0 ]; then
   151      echo "OTA process did not complete within the specified time for some boards"
   152      exitcode=1
   153  else 
   154      echo "OTA process completed successfully for all boards"
   155  fi
   156  
   157  if [ "$newtagversion" != "" ]; then
   158      echo ""
   159      echo "Tag updated devices as \"$newtagversion\""
   160      arduino-cloud-cli device create-tags --ids $otasucceeded --tags $newtagversion
   161      echo ""
   162      arduino-cloud-cli device list --tags $newtagversion
   163  fi
   164  
   165  exit $exitcode