plugins.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #! /bin/bash
  2. # Parse a support-core plugin -style txt file as specification for jenkins plugins to be installed
  3. # in the reference directory, so user can define a derived Docker image with just :
  4. #
  5. # FROM jenkins
  6. # COPY plugins.txt /plugins.txt
  7. # RUN /usr/local/bin/plugins.sh /plugins.txt
  8. #
  9. # Note: Plugins already installed are skipped
  10. #
  11. set -e
  12. echo "WARN: plugins.sh is deprecated, please switch to install-plugins.sh"
  13. if [ -z "$1" ]
  14. then
  15. echo "
  16. USAGE:
  17. Parse a support-core plugin -style txt file as specification for jenkins plugins to be installed
  18. in the reference directory, so user can define a derived Docker image with just :
  19. FROM jenkins
  20. COPY plugins.txt /plugins.txt
  21. RUN /usr/local/bin/plugins.sh /plugins.txt
  22. Note: Plugins already installed are skipped
  23. "
  24. exit 1
  25. else
  26. JENKINS_INPUT_JOB_LIST=$1
  27. if [ ! -f "$JENKINS_INPUT_JOB_LIST" ]
  28. then
  29. echo "ERROR File not found: $JENKINS_INPUT_JOB_LIST"
  30. exit 1
  31. fi
  32. fi
  33. # the war includes a # of plugins, to make the build efficient filter out
  34. # the plugins so we dont install 2x - there about 17!
  35. if [ -d "$JENKINS_HOME" ]
  36. then
  37. TEMP_ALREADY_INSTALLED=$JENKINS_HOME/preinstalled.plugins.$$.txt
  38. else
  39. echo "ERROR $JENKINS_HOME not found"
  40. exit 1
  41. fi
  42. JENKINS_PLUGINS_DIR=/var/jenkins_home/plugins
  43. if [ -d "$JENKINS_PLUGINS_DIR" ]
  44. then
  45. echo "Analyzing: $JENKINS_PLUGINS_DIR"
  46. for i in "$JENKINS_PLUGINS_DIR"/*/; do
  47. JENKINS_PLUGIN=$(basename "$i")
  48. JENKINS_PLUGIN_VER=$(egrep -i Plugin-Version "$i/META-INF/MANIFEST.MF"|cut -d: -f2|sed 's/ //')
  49. echo "$JENKINS_PLUGIN:$JENKINS_PLUGIN_VER"
  50. done >"$TEMP_ALREADY_INSTALLED"
  51. else
  52. JENKINS_WAR=/usr/share/jenkins/jenkins.war
  53. if [ -f "$JENKINS_WAR" ]
  54. then
  55. echo "Analyzing war: $JENKINS_WAR"
  56. TEMP_PLUGIN_DIR=/tmp/plugintemp.$$
  57. while read -r i <&3; do
  58. rm -fr "$TEMP_PLUGIN_DIR"
  59. mkdir -p "$TEMP_PLUGIN_DIR"
  60. PLUGIN=$(basename "$i"|cut -f1 -d'.')
  61. (cd "$TEMP_PLUGIN_DIR" || exit; jar xf "$JENKINS_WAR" "$i"; jar xvf "$TEMP_PLUGIN_DIR/$i" META-INF/MANIFEST.MF >/dev/null 2>&1)
  62. VER=$(egrep -i Plugin-Version "$TEMP_PLUGIN_DIR/META-INF/MANIFEST.MF"|cut -d: -f2|sed 's/ //')
  63. echo "$PLUGIN:$VER"
  64. done 3< <(jar tf "$JENKINS_WAR" | egrep '[^detached-]plugins.*\..pi' | sort) > "$TEMP_ALREADY_INSTALLED"
  65. rm -fr "$TEMP_PLUGIN_DIR"
  66. else
  67. rm -f "$TEMP_ALREADY_INSTALLED"
  68. echo "ERROR file not found: $JENKINS_WAR"
  69. exit 1
  70. fi
  71. fi
  72. REF=/usr/share/jenkins/ref/plugins
  73. mkdir -p $REF
  74. COUNT_PLUGINS_INSTALLED=0
  75. while read -r spec || [ -n "$spec" ]; do
  76. plugin=(${spec//:/ });
  77. [[ ${plugin[0]} =~ ^# ]] && continue
  78. [[ ${plugin[0]} =~ ^[[:space:]]*$ ]] && continue
  79. [[ -z ${plugin[1]} ]] && plugin[1]="latest"
  80. if [ -z "$JENKINS_UC_DOWNLOAD" ]; then
  81. JENKINS_UC_DOWNLOAD=$JENKINS_UC/download
  82. fi
  83. if ! grep -q "${plugin[0]}:${plugin[1]}" "$TEMP_ALREADY_INSTALLED"
  84. then
  85. echo "Downloading ${plugin[0]}:${plugin[1]}"
  86. curl --retry 3 --retry-delay 5 -sSL -f "${JENKINS_UC_DOWNLOAD}/plugins/${plugin[0]}/${plugin[1]}/${plugin[0]}.hpi" -o "$REF/${plugin[0]}.jpi"
  87. unzip -qqt "$REF/${plugin[0]}.jpi"
  88. (( COUNT_PLUGINS_INSTALLED += 1 ))
  89. else
  90. echo " ... skipping already installed: ${plugin[0]}:${plugin[1]}"
  91. fi
  92. done < "$JENKINS_INPUT_JOB_LIST"
  93. echo "---------------------------------------------------"
  94. if (( "$COUNT_PLUGINS_INSTALLED" > 0 ))
  95. then
  96. echo "INFO: Successfully installed $COUNT_PLUGINS_INSTALLED plugins."
  97. if [ -d $JENKINS_PLUGINS_DIR ]
  98. then
  99. echo "INFO: Please restart the container for changes to take effect!"
  100. fi
  101. else
  102. echo "INFO: No changes, all plugins previously installed."
  103. fi
  104. echo "---------------------------------------------------"
  105. #cleanup
  106. rm "$TEMP_ALREADY_INSTALLED"
  107. exit 0