1
0

upload.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ##
  2. ## Copyright (c) 2017 Minoca Corp.
  3. ##
  4. ## This file is licensed under the terms of the GNU General Public License
  5. ## version 3. Alternative licensing terms are available. Contact
  6. ## info@minocacorp.com for details. See the LICENSE file at the root of this
  7. ## project for complete licensing information..
  8. ##
  9. ## Script Name:
  10. ##
  11. ## upload.sh
  12. ##
  13. ## Abstract:
  14. ##
  15. ## This script contains helper functions for uploading nightly builds to
  16. ## the production site.
  17. ##
  18. ## Author:
  19. ##
  20. ## Evan Green 19-Jan-2017
  21. ##
  22. ## Environment:
  23. ##
  24. ## Windows Build
  25. ##
  26. ##
  27. ## This code assumes the proper public key is already loaded in the ssh-agent.
  28. ##
  29. UPUSER=upload
  30. UPDEST=www.minocacorp.com
  31. UPPORT=2222
  32. # Define the number of builds to keep, plus one.
  33. KEEPCOUNT=6
  34. SSH="$SRCROOT/git/usr/bin/ssh.exe"
  35. SCP="$SRCROOT/git/usr/bin/scp.exe"
  36. UPLOAD_DATE=
  37. NIGHTLIES=nightlies
  38. ##
  39. ## The ssh-agent on Windows seems to hang frequently. Unset these variables to
  40. ## keep from using it.
  41. ##
  42. unset SSH_AGENT_PID || true
  43. unset SSH_AUTH_SOCK || true
  44. ##
  45. ## It would be great to just use ssh-agent, but that seems to busy-spin hang
  46. ## regularly. This hard-coded path is ugly, but $SRCROOT cannot be used because
  47. ## scp and ssh are msys tools (so they need /c/ instead of C:/).
  48. ##
  49. KEYFILE="/c/autosrc/upload_minocacorp.key"
  50. SSH_CMD="$SSH -p $UPPORT $UPUSER@$UPDEST -i $KEYFILE -- "
  51. run_ssh_cmd() {
  52. echo "Running: $@" >&2
  53. $SSH_CMD $@
  54. }
  55. create_todays_directory() {
  56. [ -z "$UPLOAD_DATE" ] && UPLOAD_DATE=`date +%Y-%m-%d`
  57. $SSH_CMD "mkdir -p $NIGHTLIES/$UPLOAD_DATE/"
  58. }
  59. get_latests() {
  60. latests=
  61. for arch in x86 x86q armv7 armv6; do
  62. latests="$latests `run_ssh_cmd readlink $NIGHTLIES/latest-$arch || true`"
  63. done
  64. echo "$latests"
  65. }
  66. prune_stale_builds() {
  67. # The + number at the end is one greater than the number of builds to keep.
  68. stale=`run_ssh_cmd "ls $NIGHTLIES | grep 201 | sort -r | tail -n+$KEEPCOUNT"`
  69. # Don't blow away the latest build from any particular architecture. ARMv6
  70. # for instance might be way behind.
  71. latests=`get_latests`
  72. # Combine the two lists, sort them, and then ask uniq to print everything
  73. # that's not repeated.
  74. stale=`echo $stale $latests $latests | sed 's/ /\n/g' | sort | uniq -u`
  75. for d in $stale; do
  76. echo "Deleting build $d"
  77. run_ssh_cmd rm -rf $NIGHTLIES/$d/
  78. done
  79. }
  80. prepare_for_upload () {
  81. prune_stale_builds
  82. create_todays_directory
  83. }
  84. mkdir_on_production() {
  85. for d in $@; do
  86. $SSH_CMD mkdir -p "$NIGHTLIES/$UPLOAD_DATE/$d"
  87. done
  88. }
  89. upload_to_production() {
  90. for f in $@; do
  91. echo "Uploading $f to production in $UPLOAD_DATE"
  92. for try in 1 2 3 4 5; do
  93. $SCP -o TCPKeepAlive=yes -i $KEYFILE -P $UPPORT $f \
  94. $UPUSER@$UPDEST:$NIGHTLIES/$UPLOAD_DATE/$f && break
  95. done
  96. done
  97. }
  98. update_production_latest() {
  99. arch="$1"
  100. [ -z "$arch" ] && echo "Error: architecture must be specified" && exit 1
  101. [ -z "$UPLOAD_DATE" ] && echo "Error: UPLOAD_DATE should be set" && exit 1
  102. $SSH_CMD "date > $NIGHTLIES/$UPLOAD_DATE/$UPLOAD_DATE.txt"
  103. $SSH_CMD "ln -nsf $UPLOAD_DATE $NIGHTLIES/latest-$arch"
  104. }