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.

124 lines
2.8 KiB

  1. #!/bin/bash
  2. #
  3. ### BEGIN INIT INFO
  4. # Provides: calico-node
  5. # Required-Start: $local_fs $network $syslog
  6. # Required-Stop:
  7. # Default-Start: 2 3 4 5
  8. # Default-Stop: 0 1 6
  9. # Short-Description: Calico docker container
  10. # Description:
  11. # Runs calico as a docker container
  12. ### END INIT INFO
  13. set -a
  14. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  15. DESC="Calico-node Docker"
  16. NAME=calico-node
  17. DAEMON={{ bin_dir }}/calicoctl
  18. DAEMON_ARGS=""
  19. DOCKER=$(which docker)
  20. SCRIPTNAME=/etc/init.d/$NAME
  21. DAEMON_USER=root
  22. # Exit if the binary is not present
  23. [ -x "$DAEMON" ] || exit 0
  24. # Exit if the docker package is not installed
  25. [ -x "$DOCKER" ] || exit 0
  26. # Read configuration variable file if it is present
  27. [ -r /etc/network-environment ] && . /etc/network-environment
  28. # Define LSB log_* functions.
  29. # Depend on lsb-base (>= 3.2-14) to ensure that this file is present
  30. # and status_of_proc is working.
  31. . /lib/lsb/init-functions
  32. do_status()
  33. {
  34. if [ $($DOCKER ps --format "{{.Image}}" | grep -cw 'calico/node') -eq 1 ]; then
  35. return 0
  36. else
  37. return 1
  38. fi
  39. }
  40. # Function that starts the daemon/service
  41. #
  42. do_start()
  43. {
  44. do_status
  45. retval=$?
  46. if [ $retval -ne 0 ]; then
  47. {% if legacy_calicoctl %}
  48. ${DAEMON} node --ip=${DEFAULT_IPV4} >>/dev/null && return 0 || return 2
  49. {% else %}
  50. ${DAEMON} node run --ip=${DEFAULT_IPV4} >>/dev/null && return 0 || return 2
  51. {% endif %}
  52. else
  53. return 1
  54. fi
  55. }
  56. #
  57. # Function that stops the daemon/service
  58. #
  59. do_stop()
  60. {
  61. {% if legacy_calicoctl %}
  62. ${DAEMON} node stop >> /dev/null || ${DAEMON} node stop --force >> /dev/null
  63. {% else %}
  64. echo "Current version of ${DAEMON} doesn't support 'node stop' command!"
  65. return 1
  66. {% endif %}
  67. }
  68. case "$1" in
  69. start)
  70. log_daemon_msg "Starting $DESC" "$NAME"
  71. do_start
  72. case "$?" in
  73. 0|1) log_end_msg 0 || exit 0 ;;
  74. 2) log_end_msg 1 || exit 1 ;;
  75. esac
  76. ;;
  77. stop)
  78. log_daemon_msg "Stopping $DESC" "$NAME"
  79. if do_stop; then
  80. log_end_msg 0
  81. else
  82. log_failure_msg "Can't stop calico-node"
  83. log_end_msg 1
  84. fi
  85. ;;
  86. status)
  87. if do_status; then
  88. log_end_msg 0
  89. else
  90. log_failure_msg "Calico-node is not running"
  91. log_end_msg 1
  92. fi
  93. ;;
  94. restart|force-reload)
  95. log_daemon_msg "Restarting $DESC" "$NAME"
  96. if do_stop; then
  97. if do_start; then
  98. log_end_msg 0
  99. exit 0
  100. else
  101. rc="$?"
  102. fi
  103. else
  104. rc="$?"
  105. fi
  106. log_failure_msg "Can't restart Calico-node"
  107. log_end_msg ${rc}
  108. ;;
  109. *)
  110. echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
  111. exit 3
  112. ;;
  113. esac