Heat templates for deploying 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.

72 lines
1.5KB

  1. #!/bin/bash
  2. {% if debug_enabled|bool -%}
  3. set -x
  4. {% endif -%}
  5. add_date() {
  6. echo "$(date) $@"
  7. }
  8. # Set up script logging for debugging purpose.
  9. # It will be taken care of by logrotate since there is the .log
  10. # suffix.
  11. exec 3>&1 4>&2
  12. trap 'exec 2>&4 1>&3' 0 1 2 3
  13. exec 1>>/var/log/neutron/kill-script.log 2>&1
  14. SIG=$1
  15. PID=$2
  16. NETNS=$(ip netns identify ${PID})
  17. if [ "x${NETNS}" == "x" ]; then
  18. add_date "No network namespace detected, exiting"
  19. exit 1
  20. fi
  21. {% if container_cli == 'podman' %}
  22. CLI="nsenter --net=/run/netns/${NETNS} --preserve-credentials -m -t 1 podman"
  23. {% elif container_cli == 'docker' %}
  24. {% if docker_additional_sockets and docker_additional_sockets|length > 0-%}
  25. export DOCKER_HOST=unix://{{ docker_additional_sockets[0] }}
  26. {% endif -%}
  27. CLI='docker'
  28. {% else %}
  29. CLI='echo noop'
  30. {% endif %}
  31. kill_container() {
  32. add_date "Stopping container $1 ($2)"
  33. $CLI stop $2
  34. add_date "Deleting container $1 ($2)"
  35. $CLI rm $2
  36. }
  37. hup_container() {
  38. add_date "Sending HUP signal to $1 ($2)"
  39. $CLI kill --signal HUP $2
  40. }
  41. {% raw -%}
  42. if [ -f /proc/$PID/cgroup ]; then
  43. # Get container ID based on process cgroups
  44. CT_ID=$(awk 'BEGIN {FS="[-.]"} /name=/{print $3}' /proc/$PID/cgroup)
  45. CT_NAME=$($CLI inspect -f '{{.Name}}' $CT_ID)
  46. case $SIG in
  47. HUP)
  48. hup_container $CT_NAME $CT_ID
  49. ;;
  50. 9)
  51. kill_container $CT_NAME $CT_ID
  52. ;;
  53. *)
  54. add_date "Unknown action ${SIG} for ${$CT_NAME} ${CT_ID}"
  55. exit 1
  56. ;;
  57. esac
  58. else
  59. add_date "No such PID: ${PID}"
  60. exit 1
  61. fi
  62. {% endraw %}