S801gogs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/bin/sh
  2. ### Custom user script for gogs
  3. ### First param is:
  4. ### "start" (call at start entware),
  5. ### "stop" (call before stop entware),
  6. ###
  7. ### Note the additional requirements for gogs on ddwrt: shadow user, group, sudo, daemonize
  8. # pid so we know we're running
  9. PIDFILE="/opt/var/run/gogs.pid"
  10. # will need shadow users and groups, sorry - this adds complexity.
  11. USER="gogs"
  12. # go paths
  13. GOROOT="/opt/bin/go"
  14. GOPATH="/opt/go"
  15. # gogs binary location
  16. GOGSBIN="$GOPATH/src/github.com/gogs/gogs/gogs"
  17. # in case you need to see logs for this daemonized gog
  18. DAEMONIZE_LOG="/tmp/gogs.daemon.log"
  19. # SQL can start up slower than normal on DDWRT, use this string to validate if it's up (/opt/bin/netstat -ln |grep "THE STRING YOU SET FOR BELOW VARIABLE"
  20. SQL_STRING=" 127.0.0.1:3306 "
  21. # items for start
  22. PROC="gogs"
  23. DESC=$PROC
  24. PREARGS="/opt/bin/daemonize -v -o $DAEMONIZE_LOG -u $USER -c $GOPATH -p $PIDFILE -E GOROOT=\"$GOROOT\" -E GOPATH=\"$GOPATH\""
  25. ARGS="web"
  26. # legacy RC stuff
  27. ENABLED=yes
  28. # from rc.func
  29. ansi_red="\033[1;31m";ansi_white="\033[1;37m";ansi_green="\033[1;32m";ansi_yellow="\033[1;33m";ansi_blue="\033[1;34m";
  30. ansi_bell="\007";ansi_blink="\033[5m";ansi_std="\033[m";ansi_rev="\033[7m";ansi_ul="\033[4m";
  31. start() {
  32. # check if we have our user.
  33. grep -q "^$USER" /etc/passwd || {
  34. echo -e -n "$ansi_red User $user doesn't exist in /etc/passwd. Exiting.\n$ansi_std"
  35. exit 1
  36. }
  37. if [ -f "$DAEMONIZE_LOG" ]; then rm $DAEMONIZE_LOG; fi
  38. echo -e -n "$ansi_white Starting $DESC... $ansi_std"
  39. export GOROOT=$GOROOT
  40. export GOPATH=$GOPATH
  41. export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/jffs/sbin:/jffs/bin:/jffs/usr/sbin:/jffs/usr/bin:/mmc/sbin:/mmc/bin:/mmc/usr/sbin:/mmc/usr/bin:/opt/sbin:/opt/bin:/opt/usr/sbin:/ opt/usr/bin:$GOROOT/bin:$GOPATH
  42. # Wait for SQL
  43. for n in `seq 10`;
  44. do
  45. /opt/bin/netstat -ln |grep -v proc |grep -q "$SQL_STRING" && $PREARGS $GOGSBIN $ARGS > /dev/null && break
  46. sleep 1
  47. done
  48. COUNTER=0
  49. LIMIT=10
  50. while [ -z "`pidof $PROC`" -a "$COUNTER" -le "$LIMIT" ]; do
  51. sleep 1;
  52. COUNTER=`expr $COUNTER + 1`
  53. done
  54. if [ -z "`pidof $PROC`" ]
  55. then
  56. echo -e " $ansi_red failed. $ansi_std"
  57. logger "Failed to start $DESC from $CALLER."
  58. return 255
  59. else
  60. echo -e " $ansi_green done. $ansi_std"
  61. logger "Started $DESC from $CALLER."
  62. return 0
  63. fi
  64. }
  65. stop() {
  66. echo -e -n "$ansi_white Shutting down $PROC...\n $ansi_std"
  67. killall $PROC 2>/dev/null
  68. if [ -f "$PIDFILE" ]
  69. then
  70. rm "$PIDFILE"
  71. fi
  72. COUNTER=0
  73. LIMIT=10
  74. while [ -n "`pidof $PROC`" -a "$COUNTER" -le "$LIMIT" ]; do
  75. sleep 1;
  76. COUNTER=`expr $COUNTER + 1`
  77. done
  78. }
  79. status() {
  80. echo -e -n "$ansi_white Checking $DESC... \n"
  81. if [ -n "`pidof $PROC`" ]
  82. then
  83. echo -e " $ansi_green alive. $ansi_std";
  84. return 0
  85. else
  86. echo -e " $ansi_red dead. $ansi_std";
  87. return 1
  88. fi
  89. }
  90. die() {
  91. echo -e -n "$ansi_white Killing $PROC... $ansi_std"
  92. killall -9 $PROC 2>/dev/null
  93. }
  94. case "$1" in
  95. start)
  96. start
  97. ;;
  98. stop)
  99. stop
  100. ;;
  101. kill)
  102. die
  103. ;;
  104. status | check)
  105. status
  106. ;;
  107. restart)
  108. stop
  109. start
  110. ;;
  111. *)
  112. echo "Usage: $0 {start|stop|kill|restart}"
  113. exit 1
  114. ;;
  115. esac