karaf-service.sh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #!/usr/bin/env bash
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one or more
  4. # contributor license agreements. See the NOTICE file distributed with
  5. # this work for additional information regarding copyright ownership.
  6. # The ASF licenses this file to You under the Apache License, Version 2.0
  7. # (the "License"); you may not use this file except in compliance with
  8. # the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. function usage {
  19. cat <<-END >&2
  20. USAGE: $0
  21. -k KARAF_SERVICE_PATH # Karaf installation path
  22. -d KARAF_SERVICE_DATA # Karaf data path (default to \${KARAF_SERVICE_PATH}/data)
  23. -c KARAF_SERVICE_CONF # Karaf configuration file (default to \${KARAF_SERVICE_PATH/etc/\${KARAF_SERVICE_NAME}.conf
  24. -t KARAF_SERVICE_ETC # Karaf etc path (default to \${KARAF_SERVICE_PATH/etc}
  25. -p KARAF_SERVICE_PIDFILE # Karaf pid path (default to \${KARAF_SERVICE_DATA}/\${KARAF_SERVICE_NAME}.pid)
  26. -n KARAF_SERVICE_NAME # Karaf service name (default karaf)
  27. -e KARAF_ENV # Karaf environment variable (can be repeated)
  28. -u KARAF_SERVICE_USER # Karaf user
  29. -g KARAF_SERVICE_GROUP # Karaf group (default \${KARAF_SERVICE_USER)
  30. -l KARAF_SERVICE_LOG # Karaf console log (default to \${KARAF_SERVICE_DATA}/log/\${KARAF_SERVICE_NAME}-console.log)
  31. -f KARAF_SERVICE_TEMPLATE # Template file to use
  32. -x KARAF_SERVICE_EXECUTABLE # Karaf executable name (defaul karaf, should support daemon and stop commands)
  33. -h # this usage message
  34. END
  35. exit
  36. }
  37. CONF_TEMPLATE="karaf-service-template.conf"
  38. SYSTEMD_TEMPLATE="karaf-service-template.systemd"
  39. SYSTEMD_TEMPLATE_INSTANCES="karaf-service-template.systemd-instances"
  40. INIT_TEMPLATE="karaf-service-template.init"
  41. INIT_REDHAT_TEMPLATE="karaf-service-template.init-redhat"
  42. INIT_DEBIAN_TEMPLATE="karaf-service-template.init-debian"
  43. SOLARIS_SMF_TEMPLATE="karaf-service-template.solaris-smf"
  44. ################################################################################
  45. #
  46. ################################################################################
  47. KARAF_ENV=()
  48. while getopts k:d:c:p:n:u:g:l:t:e:f:x:h opt
  49. do
  50. case $opt in
  51. k) export KARAF_SERVICE_PATH="$OPTARG" ;;
  52. d) export KARAF_SERVICE_DATA="$OPTARG" ;;
  53. c) export KARAF_SERVICE_CONF="$OPTARG" ;;
  54. p) export KARAF_SERVICE_PIDFILE="$OPTARG" ;;
  55. n) export KARAF_SERVICE_NAME="$OPTARG" ;;
  56. u) export KARAF_SERVICE_USER="$OPTARG" ;;
  57. g) export KARAF_SERVICE_GROUP="$OPTARG" ;;
  58. l) export KARAF_SERVICE_LOG="$OPTARG" ;;
  59. t) export KARAF_SERVICE_ETC="$OPTARG" ;;
  60. f) export KARAF_SERVICE_TEMPLATE="$OPTARG" ;;
  61. x) export KARAF_SERVICE_EXECUTABLE="$OPTARG" ;;
  62. e) KARAF_ENV+=("$OPTARG") ;;
  63. h|?) usage ;;
  64. esac
  65. done
  66. shift $(( $OPTIND - 1 ))
  67. if [[ ! $KARAF_SERVICE_PATH ]]; then
  68. echo "Warning, KARAF_SERVICE_PATH is required"
  69. usage
  70. fi
  71. if [[ ! $KARAF_SERVICE_DATA ]]; then
  72. export KARAF_SERVICE_DATA="${KARAF_SERVICE_PATH}/data"
  73. fi
  74. if [[ ! $KARAF_SERVICE_ETC ]]; then
  75. export KARAF_SERVICE_ETC="${KARAF_SERVICE_PATH}/etc"
  76. fi
  77. if [[ ! $KARAF_SERVICE_NAME ]]; then
  78. export KARAF_SERVICE_NAME="karaf"
  79. fi
  80. if [[ ! $KARAF_SERVICE_CONF ]]; then
  81. export KARAF_SERVICE_CONF="${KARAF_SERVICE_PATH}/etc/${KARAF_SERVICE_NAME}.conf"
  82. fi
  83. if [[ ! $KARAF_SERVICE_PIDFILE ]]; then
  84. export KARAF_SERVICE_PIDFILE="${KARAF_SERVICE_DATA}/${KARAF_SERVICE_NAME}.pid"
  85. fi
  86. if [[ ! $KARAF_SERVICE_LOG ]]; then
  87. export KARAF_SERVICE_LOG="${KARAF_SERVICE_DATA}/log/${KARAF_SERVICE_NAME}-console.log"
  88. fi
  89. if [[ ! $KARAF_SERVICE_USER ]]; then
  90. export KARAF_SERVICE_USER="root"
  91. fi
  92. if [[ ! $KARAF_SERVICE_GROUP ]]; then
  93. export KARAF_SERVICE_GROUP="${KARAF_SERVICE_USER}"
  94. fi
  95. if [[ ! $KARAF_SERVICE_EXECUTABLE ]]; then
  96. export KARAF_SERVICE_EXECUTABLE="karaf"
  97. fi
  98. ################################################################################
  99. #
  100. ################################################################################
  101. function generate_service_descriptor {
  102. echo "Writing service file \"$2\""
  103. perl -p -e 's/\$\{([^}]+)\}/defined $ENV{$1} ? $ENV{$1} : $&/eg' < "$1" > "$2"
  104. if [ $# -eq 4 ]; then
  105. echo "Writing service configuration file \"$4\""
  106. perl -p -e 's/\$\{([^}]+)\}/defined $ENV{$1} ? $ENV{$1} : $&/eg' < "$3" > "$4"
  107. for var in "${KARAF_ENV[@]}"; do
  108. echo "${var}" >> "$4"
  109. done
  110. fi
  111. }
  112. ################################################################################
  113. #
  114. ################################################################################
  115. if [[ ! $KARAF_SERVICE_TEMPLATE ]]; then
  116. case $(uname | tr [:upper:] [:lower:]) in
  117. sunos)
  118. # add KARAF_ENV vars to envirioment
  119. for var in "${KARAF_ENV[@]}"; do
  120. export $var
  121. done
  122. # Default java path if not set
  123. if [[ ! $JAVA_HOME ]]; then
  124. export JAVA_HOME=/usr/java
  125. fi
  126. # escape spaces in path
  127. export KARAF_SERVICE_PATH="$(echo $KARAF_SERVICE_PATH | sed 's/ /\\ /g')"
  128. export KARAF_SERVICE_DATA="$(echo $KARAF_SERVICE_DATA | sed 's/ /\\ /g')"
  129. export KARAF_SERVICE_CONF="$(echo $KARAF_SERVICE_CONF | sed 's/ /\\ /g')"
  130. export KARAF_SERVICE_PIDFILE="$(echo $KARAF_SERVICE_PIDFILE | sed 's/ /\\ /g')"
  131. generate_service_descriptor \
  132. "$SOLARIS_SMF_TEMPLATE" \
  133. "${PWD}/${KARAF_SERVICE_NAME}.xml"
  134. ;;
  135. linux)
  136. if [ -d /run/systemd/system ]; then
  137. generate_service_descriptor \
  138. "$SYSTEMD_TEMPLATE" \
  139. "${PWD}/${KARAF_SERVICE_NAME}.service" \
  140. "${CONF_TEMPLATE}" \
  141. "${KARAF_SERVICE_CONF}"
  142. generate_service_descriptor \
  143. "$SYSTEMD_TEMPLATE_INSTANCES" \
  144. "${PWD}/${KARAF_SERVICE_NAME}@.service"
  145. elif [ -f /etc/redhat-release ]; then
  146. generate_service_descriptor \
  147. "$INIT_REDHAT_TEMPLATE" \
  148. "${PWD}/${KARAF_SERVICE_NAME}" \
  149. "${CONF_TEMPLATE}" \
  150. "${KARAF_SERVICE_CONF}"
  151. chmod 755 "${PWD}/${KARAF_SERVICE_NAME}"
  152. elif [ -f /etc/debian-release ] || [ -f /etc/debian_version ]; then
  153. generate_service_descriptor \
  154. "$INIT_DEBIAN_TEMPLATE" \
  155. "${PWD}/${KARAF_SERVICE_NAME}" \
  156. "${CONF_TEMPLATE}" \
  157. "${KARAF_SERVICE_CONF}"
  158. chmod 755 "${PWD}/${KARAF_SERVICE_NAME}"
  159. fi
  160. ;;
  161. *)
  162. generate_service_descriptor \
  163. "$INIT_TEMPLATE" \
  164. "${PWD}/${KARAF_SERVICE_NAME}" \
  165. "${CONF_TEMPLATE}" \
  166. "${KARAF_SERVICE_CONF}"
  167. chmod 755 "${PWD}/${KARAF_SERVICE_NAME}"
  168. ;;
  169. esac
  170. else
  171. generate_service_descriptor \
  172. "$KARAF_SERVICE_TEMPLATE" \
  173. "${PWD}/${KARAF_SERVICE_NAME}" \
  174. "${CONF_TEMPLATE}" \
  175. "${KARAF_SERVICE_CONF}"
  176. fi