plugins.bash 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/bin/bash
  2. # This file contains some utilities to test the elasticsearch
  3. # plugin installation and uninstallation process.
  4. # WARNING: This testing file must be executed as root and can
  5. # dramatically change your system. It should only be executed
  6. # in a throw-away VM like those made by the Vagrantfile at
  7. # the root of the Elasticsearch source code. This should
  8. # cause the script to fail if it is executed any other way:
  9. [ -f /etc/is_vagrant_vm ] || {
  10. >&2 echo "must be run on a vagrant VM"
  11. exit 1
  12. }
  13. # Licensed to Elasticsearch under one or more contributor
  14. # license agreements. See the NOTICE file distributed with
  15. # this work for additional information regarding copyright
  16. # ownership. Elasticsearch licenses this file to you under
  17. # the Apache License, Version 2.0 (the "License"); you may
  18. # not use this file except in compliance with the License.
  19. # You may obtain a copy of the License at
  20. #
  21. # http://www.apache.org/licenses/LICENSE-2.0
  22. #
  23. # Unless required by applicable law or agreed to in writing,
  24. # software distributed under the License is distributed on an
  25. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  26. # KIND, either express or implied. See the License for the
  27. # specific language governing permissions and limitations
  28. # under the License.
  29. # Install a plugin
  30. install_plugin() {
  31. local name=$1
  32. local path="$2"
  33. local umask="$3"
  34. assert_file_exist "$path"
  35. if [ ! -z "$ES_PATH_CONF" ] ; then
  36. if is_dpkg; then
  37. echo "ES_PATH_CONF=$ES_PATH_CONF" >> /etc/default/elasticsearch;
  38. elif is_rpm; then
  39. echo "ES_PATH_CONF=$ES_PATH_CONF" >> /etc/sysconfig/elasticsearch;
  40. fi
  41. fi
  42. if [ -z "$umask" ]; then
  43. sudo -E -u $ESPLUGIN_COMMAND_USER "$ESHOME/bin/elasticsearch-plugin" install --batch "file://$path"
  44. else
  45. sudo -E -u $ESPLUGIN_COMMAND_USER bash -c "umask $umask && \"$ESHOME/bin/elasticsearch-plugin\" install --batch \"file://$path\""
  46. fi
  47. #check we did not accidentially create a log file as root as /usr/share/elasticsearch
  48. assert_file_not_exist "/usr/share/elasticsearch/logs"
  49. # At some point installing or removing plugins caused elasticsearch's logs
  50. # to be owned by root. This is bad so we want to make sure it doesn't
  51. # happen.
  52. if [ -e "$ESLOG" ] && [ $(stat "$ESLOG" --format "%U") == "root" ]; then
  53. echo "$ESLOG is now owned by root! That'll break logging when elasticsearch tries to start."
  54. false
  55. fi
  56. }
  57. # Remove a plugin and make sure its plugin directory is removed.
  58. remove_plugin() {
  59. local name=$1
  60. echo "Removing $name...."
  61. sudo -E -u $ESPLUGIN_COMMAND_USER "$ESHOME/bin/elasticsearch-plugin" remove $name
  62. assert_file_not_exist "$ESPLUGINS/$name"
  63. # At some point installing or removing plugins caused elasticsearch's logs
  64. # to be owned by root. This is bad so we want to make sure it doesn't
  65. # happen.
  66. if [ -e "$ESLOG" ] && [ $(stat "$ESLOG" --format "%U") == "root" ]; then
  67. echo "$ESLOG is now owned by root! That'll break logging when elasticsearch tries to start."
  68. false
  69. fi
  70. }
  71. # Install a sample plugin which fully exercises the special case file placements.
  72. install_plugin_example() {
  73. local relativePath=${1:-$(readlink -m $BATS_PLUGINS/custom-settings-*.zip)}
  74. install_plugin custom-settings "$relativePath" $2
  75. bin_user=$(find "$ESHOME/bin" -maxdepth 0 -printf "%u")
  76. bin_owner=$(find "$ESHOME/bin" -maxdepth 0 -printf "%g")
  77. assert_file "$ESHOME/plugins/custom-settings" d $bin_user $bin_owner 755
  78. assert_file "$ESHOME/plugins/custom-settings/custom-settings-$(cat version).jar" f $bin_user $bin_owner 644
  79. #owner group and permissions vary depending on how es was installed
  80. #just make sure that everything is the same as the parent bin dir, which was properly set up during install
  81. assert_file "$ESHOME/bin/custom-settings" d $bin_user $bin_owner 755
  82. assert_file "$ESHOME/bin/custom-settings/test" f $bin_user $bin_owner 755
  83. #owner group and permissions vary depending on how es was installed
  84. #just make sure that everything is the same as $CONFIG_DIR, which was properly set up during install
  85. config_user=$(find "$ESCONFIG" -maxdepth 0 -printf "%u")
  86. config_owner=$(find "$ESCONFIG" -maxdepth 0 -printf "%g")
  87. # directories should user the user file-creation mask
  88. assert_file "$ESCONFIG/custom-settings" d $config_user $config_owner 750
  89. assert_file "$ESCONFIG/custom-settings/custom.yml" f $config_user $config_owner 660
  90. run sudo -E -u vagrant LANG="en_US.UTF-8" cat "$ESCONFIG/custom-settings/custom.yml"
  91. [ $status = 1 ]
  92. [[ "$output" == *"Permission denied"* ]] || {
  93. echo "Expected permission denied but found $output:"
  94. false
  95. }
  96. echo "Running sample plugin bin script...."
  97. "$ESHOME/bin/custom-settings/test" | grep test
  98. }
  99. # Remove the sample plugin which fully exercises the special cases of
  100. # removing bin and not removing config.
  101. remove_plugin_example() {
  102. remove_plugin custom-settings
  103. assert_file_not_exist "$ESHOME/bin/custom-settings"
  104. assert_file_exist "$ESCONFIG/custom-settings"
  105. assert_file_exist "$ESCONFIG/custom-settings/custom.yml"
  106. }
  107. # Install a plugin with a special prefix. For the most part prefixes are just
  108. # useful for grouping but the "analysis" prefix is special because all
  109. # analysis plugins come with a corresponding lucene-analyzers jar.
  110. # $1 - the prefix
  111. # $2 - the plugin name
  112. # $@ - all remaining arguments are jars that must exist in the plugin's
  113. # installation directory
  114. install_and_check_plugin() {
  115. local prefix=$1
  116. shift
  117. local name=$1
  118. shift
  119. if [ "$prefix" == "-" ]; then
  120. local full_name="$name"
  121. else
  122. local full_name="$prefix-$name"
  123. fi
  124. install_plugin $full_name "$(readlink -m $BATS_PLUGINS/$full_name-*.zip)"
  125. assert_module_or_plugin_directory "$ESPLUGINS/$full_name"
  126. assert_file_exist "$ESPLUGINS/$full_name/plugin-descriptor.properties"
  127. assert_file_exist "$ESPLUGINS/$full_name/$full_name"*".jar"
  128. # analysis plugins have a corresponding analyzers jar
  129. if [ $prefix == 'analysis' ]; then
  130. local analyzer_jar_suffix=$name
  131. # the name of the analyzer jar for the ukrainian plugin does
  132. # not match the name of the plugin, so we have to make an
  133. # exception
  134. if [ $name == 'ukrainian' ]; then
  135. analyzer_jar_suffix='morfologik'
  136. fi
  137. assert_module_or_plugin_file "$ESPLUGINS/$full_name/lucene-analyzers-$analyzer_jar_suffix-*.jar"
  138. fi
  139. for file in "$@"; do
  140. assert_module_or_plugin_file "$ESPLUGINS/$full_name/$file"
  141. done
  142. }
  143. # Install a meta plugin
  144. # $1 - the plugin name
  145. # $@ - all remaining arguments are jars that must exist in the plugin's
  146. # installation directory
  147. install_meta_plugin() {
  148. local name=$1
  149. install_plugin $name "$(readlink -m $BATS_PLUGINS/$name-*.zip)"
  150. assert_module_or_plugin_directory "$ESPLUGINS/$name"
  151. }
  152. # Compare a list of plugin names to the plugins in the plugins pom and see if they are the same
  153. # $1 the file containing the list of plugins we want to compare to
  154. # $2 description of the source of the plugin list
  155. compare_plugins_list() {
  156. cat $1 | sort > /tmp/plugins
  157. echo "Checking plugins from $2 (<) against expected plugins (>):"
  158. # bats tests are run under build/packaging/distributions, and expected file is in build/plugins/expected
  159. # this can't be an absolute path since it differs whether running in vagrant or GCP
  160. diff -w ../../plugins/expected /tmp/plugins
  161. }