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.

114 lines
2.5 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. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  14. DESC="Calico-node Docker"
  15. NAME=calico-node
  16. DAEMON={{ bin_dir }}/calicoctl
  17. DAEMON_ARGS=""
  18. DOCKER=$(which docker)
  19. SCRIPTNAME=/etc/init.d/$NAME
  20. DAEMON_USER=root
  21. # Exit if the binary is not present
  22. [ -x "$DAEMON" ] || exit 0
  23. # Exit if the docker package is not installed
  24. [ -x "$DOCKER" ] || exit 0
  25. # Read configuration variable file if it is present
  26. [ -r /etc/network-environment ] && . /etc/network-environment
  27. # Define LSB log_* functions.
  28. # Depend on lsb-base (>= 3.2-14) to ensure that this file is present
  29. # and status_of_proc is working.
  30. . /lib/lsb/init-functions
  31. do_status()
  32. {
  33. if [ $($DOCKER ps | awk '{ print $2 }' | grep calico/node | wc -l) -eq 1 ]; then
  34. return 0
  35. else
  36. return 1
  37. fi
  38. }
  39. # Function that starts the daemon/service
  40. #
  41. do_start()
  42. {
  43. do_status
  44. retval=$?
  45. if [ $retval -ne 0 ]; then
  46. ${DAEMON} node --ip=${DEFAULT_IPV4} >>/dev/null && return 0 || return 2
  47. else
  48. return 1
  49. fi
  50. }
  51. #
  52. # Function that stops the daemon/service
  53. #
  54. do_stop()
  55. {
  56. ${DAEMON} node stop >> /dev/null || ${DAEMON} node stop --force >> /dev/null
  57. }
  58. case "$1" in
  59. start)
  60. log_daemon_msg "Starting $DESC" "$NAME"
  61. do_start
  62. case "$?" in
  63. 0|1) log_end_msg 0 || exit 0 ;;
  64. 2) log_end_msg 1 || exit 1 ;;
  65. esac
  66. ;;
  67. stop)
  68. log_daemon_msg "Stopping $DESC" "$NAME"
  69. if do_stop; then
  70. log_end_msg 0
  71. else
  72. log_failure_msg "Can't stop calico-node"
  73. log_end_msg 1
  74. fi
  75. ;;
  76. status)
  77. if do_status; then
  78. log_end_msg 0
  79. else
  80. log_failure_msg "Calico-node is not running"
  81. log_end_msg 1
  82. fi
  83. ;;
  84. restart|force-reload)
  85. log_daemon_msg "Restarting $DESC" "$NAME"
  86. if do_stop; then
  87. if do_start; then
  88. log_end_msg 0
  89. exit 0
  90. else
  91. rc="$?"
  92. fi
  93. else
  94. rc="$?"
  95. fi
  96. log_failure_msg "Can't restart Calico-node"
  97. log_end_msg ${rc}
  98. ;;
  99. *)
  100. echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
  101. exit 3
  102. ;;
  103. esac