Disk image elements for deployment images of OpenStack
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

os-svc-daemon 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #!/bin/bash
  2. set -eu
  3. DEFAULT_POSTSTART="exec sleep 1"
  4. usage() {
  5. echo "Usage: os-svc-daemon [ -ph ] [ -s POSTSTART ] [ -e ENV ] -n SERVICENAME -u RUNAS [ -c RUNCMD -- [arg [arg...]]]"
  6. echo ""
  7. echo "SERVICENAME, RUNAS, RUNCMD, and POSTSTART can be set via the"
  8. echo "environment as well. Command line arguments will override"
  9. echo "environment variables. By default this will create a python logging"
  10. echo "configuration file in /etc/os-logging/servicename.conf"
  11. echo ""
  12. echo " -a Use alternate svc-map instead of map-services"
  13. echo " -h Show help and exit"
  14. echo " -p Print the job file instead of writing to disk"
  15. echo " -l Create neither a python logging.conf nor pass --log-config-append argument to command."
  16. echo " -d [NAME] Specify the name of the runtime directory, which will be"
  17. echo " /var/run/[NAME]"
  18. echo " -s POSTSTART post_start will be added to the upstart job. Ignored with systemd."
  19. echo " default: $DEFAULT_POSTSTART"
  20. echo " -e ENV Environment name=value entries to set in the service/job"
  21. echo " -n SERVICENAME Name of job/service file."
  22. echo " -i INSTALLDIR Optional: virtualenv installation directory. Defaults to: /opt/stack/venvs/<SERVICENAME>"
  23. echo " -u RUNAS User to run main executable as."
  24. echo " -c RUNCMD Command to execute. Must stay in foreground."
  25. echo " arg... Arguments will be passed to COMMAND"
  26. echo ""
  27. }
  28. # Can be set in environment now
  29. SERVICENAME=${SERVICENAME:-""}
  30. INSTALLDIR=
  31. RUNAS=${RUNAS:-""}
  32. RUNCMD=${RUNCMD:-""}
  33. ENV=${ENV:-""}
  34. DISABLE_LOGGING_CONF=
  35. CREATE_DIR_NAME=${CREATE_DIR_NAME:-""}
  36. # The default helps avoid race with daemon listening. http://pad.lv/1179766
  37. POSTSTART=${POSTSTART:-$DEFAULT_POSTSTART}
  38. MAPPING_COMMAND=map-services
  39. print_only() {
  40. cat
  41. }
  42. print_to_file() {
  43. cat > $1
  44. }
  45. append_to_file() {
  46. cat >> $1
  47. }
  48. OUTPUT=print_to_file
  49. APPEND=append_to_file
  50. nshift=0
  51. while getopts "aplhd:s:n:i:u:c:e:" opt; do
  52. case "$opt" in
  53. n) SERVICENAME=$OPTARG;;
  54. i) INSTALLDIR=$OPTARG;;
  55. u) RUNAS=$OPTARG;;
  56. c) RUNCMD=$OPTARG;;
  57. s) POSTSTART=$OPTARG;;
  58. e) ENV=$OPTARG;;
  59. a) MAPPING_COMMAND=svc-map;;
  60. p) OUTPUT=print_only; APPEND=print_only;;
  61. l) DISABLE_LOGGING_CONF="1";;
  62. d) CREATE_DIR_NAME=$OPTARG;;
  63. h) usage; exit 0;;
  64. \?) usage; exit 1;;
  65. :) usage; exit 1;;
  66. esac
  67. done
  68. shift $(($OPTIND-1))
  69. if [ -z "$SERVICENAME" ] || [ -z "$RUNAS" ] ; then
  70. if [ $# -lt 3 ] ; then
  71. usage
  72. exit 1
  73. fi
  74. fi
  75. function deprecated_posarg_warning {
  76. echo "WARNING: Setting $1 via positional argument is deprecated and will be removed in a future release."
  77. }
  78. # Compatibility with old style passing w/o switches
  79. if [ -z "$SERVICENAME" ]; then
  80. SERVICENAME=$1
  81. shift
  82. deprecated_posarg_warning "SERVICENAME"
  83. fi
  84. if [ -z "$RUNAS" ]; then
  85. RUNAS=$1
  86. shift
  87. deprecated_posarg_warning "RUNAS"
  88. fi
  89. if [ -z "$RUNCMD" ]; then
  90. CHECK=${1:-""}
  91. if [ -n "$CHECK" ]; then
  92. RUNCMD=$1
  93. shift
  94. deprecated_posarg_warning "CHECK"
  95. fi
  96. fi
  97. # if INSTALLDIR isn't set use /opt/stack/venvs/RUNAS
  98. # NOTE: this was our default before adding the -i option
  99. if [ -z "$INSTALLDIR" ]; then
  100. INSTALLDIR="/opt/stack/venvs/$RUNAS"
  101. fi
  102. if [ -z "$DISABLE_LOGGING_CONF" ]; then
  103. # Set up service-specific logging config
  104. LOGGING_CONFIG="/etc/os-logging/$SERVICENAME.config"
  105. mkdir /etc/os-logging || true
  106. $OUTPUT $LOGGING_CONFIG <<EOF
  107. [loggers]
  108. keys=root
  109. [handlers]
  110. keys=syslog
  111. [formatters]
  112. keys=normal
  113. [logger_root]
  114. handlers=syslog
  115. [handler_syslog]
  116. class=handlers.SysLogHandler
  117. args=('/dev/log', handlers.SysLogHandler.LOG_USER)
  118. formatter=normal
  119. [formatter_normal]
  120. format=$SERVICENAME: %(asctime)s %(levelname)s %(message)s
  121. EOF
  122. fi
  123. function install_upstart {
  124. local name=$1
  125. local install_dir=$2
  126. local user=$3
  127. local dirname=${4:-$user}
  128. local cmd=$5
  129. shift; shift; shift; shift; shift
  130. local args=$*
  131. local env_entries=''
  132. if [ -n "$ENV" ]; then
  133. local env_pad=" $ENV"
  134. env_entries=${env_pad// /
  135. env }
  136. fi
  137. local target_file="/etc/init/$name.conf"
  138. if [ -z "$DISABLE_LOGGING_CONF" ]; then
  139. args="--log-config-append $LOGGING_CONFIG $args"
  140. fi
  141. $OUTPUT $target_file <<EOF
  142. start on runlevel [2345]
  143. stop on runlevel [016]
  144. $env_entries
  145. env OS_SVC_ENABLE_CONTROL=1
  146. export OS_SVC_ENABLE_CONTROL
  147. pre-start script
  148. mkdir -p /var/run/$dirname
  149. chown -R $user:$user /var/run/$dirname
  150. end script
  151. EOF
  152. if [ -n "$cmd" ]; then
  153. $APPEND $target_file <<EOF
  154. respawn
  155. # the default post-start of 1 second sleep delays respawning enough to
  156. # not hit the default of 10 times in 5 seconds. Make it 2 times in 10s.
  157. respawn limit 2 10
  158. exec start-stop-daemon --start -c $user --exec $install_dir/bin/$cmd -- $args
  159. post-start $POSTSTART
  160. EOF
  161. fi
  162. }
  163. function install_systemd {
  164. local name=$1
  165. local install_dir=$2
  166. local user=$3
  167. local cmd=$4
  168. shift; shift; shift; shift;
  169. local args=$*
  170. local env_entries=''
  171. if [ -n "$ENV" ]; then
  172. local env_pad=" $ENV"
  173. env_entries=${env_pad// /
  174. Environment=}
  175. fi
  176. if [ -z "$DISABLE_LOGGING_CONF" ]; then
  177. args="--log-config-append $LOGGING_CONFIG $args"
  178. fi
  179. $OUTPUT /lib/systemd/system/$name.service <<EOF
  180. [Unit]
  181. Description=$name Service
  182. After=os-refresh-config.service
  183. Requires=$name-create-dir.service
  184. [Service]
  185. ExecStart=$install_dir/bin/$cmd $args
  186. User=$user
  187. $env_entries
  188. [Install]
  189. WantedBy=multi-user.target
  190. Alias=$name.service
  191. EOF
  192. }
  193. function install_create_dir_systemd {
  194. local name="$($MAPPING_COMMAND "$1")"
  195. local user=$2
  196. local dirname=${3:-$user}
  197. $OUTPUT /lib/systemd/system/$name-create-dir.service <<EOF
  198. [Unit]
  199. Description=Create /var/run/$dirname
  200. [Service]
  201. ExecStartPre=/bin/mkdir -p /var/run/$dirname
  202. ExecStartPre=/usr/local/bin/restore-selinux-file-context /var/run/$dirname
  203. ExecStart=/bin/chown -R $user:$user /var/run/$dirname
  204. [Install]
  205. RequiredBy=$name.service
  206. EOF
  207. }
  208. # TODO: SysV init fallback support
  209. DIB_INIT_SYSTEM=$(dib-init-system)
  210. if [ "$DIB_INIT_SYSTEM" == "upstart" ]; then
  211. install_upstart $SERVICENAME $INSTALLDIR $RUNAS "$CREATE_DIR_NAME" "$RUNCMD" $*
  212. elif [ "$DIB_INIT_SYSTEM" == "systemd" ]; then
  213. if [ "$POSTSTART" != "$DEFAULT_POSTSTART" ] ; then
  214. echo "WARNING: post start is ignored with systemd." >&2
  215. fi
  216. if [ -n "$RUNCMD" ]; then
  217. install_systemd $SERVICENAME $INSTALLDIR $RUNAS $RUNCMD $*
  218. fi
  219. install_create_dir_systemd $SERVICENAME $RUNAS $CREATE_DIR_NAME
  220. fi