jenkins-support 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/bin/bash -eu
  2. # compare if version1 < version2
  3. versionLT() {
  4. local v1; v1=$(echo "$1" | cut -d '-' -f 1 )
  5. local q1; q1=$(echo "$1" | cut -s -d '-' -f 2- )
  6. local v2; v2=$(echo "$2" | cut -d '-' -f 1 )
  7. local q2; q2=$(echo "$2" | cut -s -d '-' -f 2- )
  8. if [ "$v1" = "$v2" ]; then
  9. if [ "$q1" = "$q2" ]; then
  10. return 1
  11. else
  12. if [ -z "$q1" ]; then
  13. return 1
  14. else
  15. if [ -z "$q2" ]; then
  16. return 0
  17. else
  18. [ "$q1" = "$(echo -e "$q1\n$q2" | sort -V | head -n1)" ]
  19. fi
  20. fi
  21. fi
  22. else
  23. [ "$v1" = "$(echo -e "$v1\n$v2" | sort -V | head -n1)" ]
  24. fi
  25. }
  26. # returns a plugin version from a plugin archive
  27. get_plugin_version() {
  28. local archive; archive=$1
  29. local version; version=$(unzip -p "$archive" META-INF/MANIFEST.MF | grep "^Plugin-Version: " | sed -e 's#^Plugin-Version: ##')
  30. version=${version%%[[:space:]]}
  31. echo "$version"
  32. }
  33. # Copy files from /usr/share/jenkins/ref into $JENKINS_HOME
  34. # So the initial JENKINS-HOME is set with expected content.
  35. # Don't override, as this is just a reference setup, and use from UI
  36. # can then change this, upgrade plugins, etc.
  37. copy_reference_file() {
  38. f="${1%/}"
  39. b="${f%.override}"
  40. rel="${b:23}"
  41. version_marker="${rel}.version_from_image"
  42. dir=$(dirname "${b}")
  43. local action;
  44. local reason;
  45. local container_version;
  46. local image_version;
  47. local marker_version;
  48. local log; log=false
  49. if [[ ${rel} == plugins/*.jpi ]]; then
  50. container_version=$(get_plugin_version "$JENKINS_HOME/${rel}")
  51. image_version=$(get_plugin_version "${f}")
  52. if [[ -e $JENKINS_HOME/${version_marker} ]]; then
  53. marker_version=$(cat "$JENKINS_HOME/${version_marker}")
  54. if versionLT "$marker_version" "$container_version"; then
  55. action="SKIPPED"
  56. reason="Installed version ($container_version) has been manually upgraded from initial version ($marker_version)"
  57. log=true
  58. else
  59. if [[ "$image_version" == "$container_version" ]]; then
  60. action="SKIPPED"
  61. reason="Version from image is the same as the installed version $image_version"
  62. else
  63. if versionLT "$image_version" "$container_version"; then
  64. action="SKIPPED"
  65. log=true
  66. reason="Image version ($image_version) is older than installed version ($container_version)"
  67. else
  68. action="UPGRADED"
  69. log=true
  70. reason="Image version ($image_version) is newer than installed version ($container_version)"
  71. fi
  72. fi
  73. fi
  74. else
  75. if [[ -n "$TRY_UPGRADE_IF_NO_MARKER" ]]; then
  76. if [[ "$image_version" == "$container_version" ]]; then
  77. action="SKIPPED"
  78. reason="Version from image is the same as the installed version $image_version (no marker found)"
  79. # Add marker for next time
  80. echo "$image_version" > "$JENKINS_HOME/${version_marker}"
  81. else
  82. if versionLT "$image_version" "$container_version"; then
  83. action="SKIPPED"
  84. log=true
  85. reason="Image version ($image_version) is older than installed version ($container_version) (no marker found)"
  86. else
  87. action="UPGRADED"
  88. log=true
  89. reason="Image version ($image_version) is newer than installed version ($container_version) (no marker found)"
  90. fi
  91. fi
  92. fi
  93. fi
  94. if [[ ! -e $JENKINS_HOME/${rel} || "$action" == "UPGRADED" || $f = *.override ]]; then
  95. action=${action:-"INSTALLED"}
  96. log=true
  97. mkdir -p "$JENKINS_HOME/${dir:23}"
  98. cp -r "${f}" "$JENKINS_HOME/${rel}";
  99. # pin plugins on initial copy
  100. touch "$JENKINS_HOME/${rel}.pinned"
  101. echo "$image_version" > "$JENKINS_HOME/${version_marker}"
  102. reason=${reason:-$image_version}
  103. else
  104. action=${action:-"SKIPPED"}
  105. fi
  106. else
  107. if [[ ! -e $JENKINS_HOME/${rel} || $f = *.override ]]
  108. then
  109. action="INSTALLED"
  110. log=true
  111. mkdir -p "$JENKINS_HOME/${dir:23}"
  112. cp -r "${f}" "$JENKINS_HOME/${rel}";
  113. else
  114. action="SKIPPED"
  115. fi
  116. fi
  117. if [[ -n "$VERBOSE" || "$log" == "true" ]]; then
  118. if [ -z "$reason" ]; then
  119. echo "$action $rel" >> "$COPY_REFERENCE_FILE_LOG"
  120. else
  121. echo "$action $rel : $reason" >> "$COPY_REFERENCE_FILE_LOG"
  122. fi
  123. fi
  124. }