aboutsummaryrefslogtreecommitdiff
path: root/network/seafile-server/seafile
blob: fd80c1d68b4bfef1b340cf97b9b9ff1b131b175a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/sh

# Simple `seafile-admin` & `seafserv-gc` wrapper script.

# Copyright 2015 Marcel Saegebarth <marc@mos6581.de>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

THIS_NAME=$(basename ${0})
WORKING_DIR=${WORKING_DIR:-/opt/haiwen}
PYTHONPATH=$WORKING_DIR/seafile-server/seahub/thirdpart:$PYTHONPATH
PATH=$PYTHONPATH:$PATH
SEAFILEUSER=seafile
SEAFILEDATA=${SEAFILEDATA:-""}
OPTHELP=${OPTHELP:-no}
ARGUMENTS=${ARGUMENTS:-$1}

set -o pipefail
set -o nounset

alias printf=$(command -v printf)

getopt -T > /dev/null
if [ $? -eq 4 ]; then
  ARGS=$(getopt -a -n "$THIS_NAME" -l data-directory:,help -o d:h -- "$@")
    if [ $? -ne 0 ]; then
      printf '\n' && exit 1
    fi
  eval set -- $ARGS
else
  printf '%s\n' "Failed: Require GNU enhanced version. Exit." && exit 1
fi

while [ $# -gt 0 ]; do
  case "$1" in
    -d | --data-directory) SEAFILEDATA=$2 ;;
    -h | --help)           OPTHELP=yes    ;;
    --) shift; break;;
  esac
  shift
done

if [ "$OPTHELP" = "yes" ]; then
  cat << EOF
${THIS_NAME}

Usage: seafile start|stop|restart|setup|create-admin|reset-admin|cleanup [-d <seafile-data-directory>]

Options
  -d, --data-directory Specify seafile data directory for garbage collector or
                       alternatively set the SEAFILEDATA environment variable.
                       if not used it will ask for the directory.
  -h, --help           Displays this help message.
EOF
  exit 0
fi

execute () {
  su -s /bin/sh -l $SEAFILEUSER -c "export PYTHONPATH=$PYTHONPATH PATH=$PATH && cd $WORKING_DIR && seafile-admin $1"
}

garbage_collector () {
  if [ -z "$SEAFILEDATA" ]; then
    read -p 'Enter seafile data directory: ' SEAFILEDATA
  fi

  # check if directory
  if [ ! -d "$SEAFILEDATA" ]; then
    printf '\n%s\n' "Couldn't find directory '$SEAFILEDATA'" && exit 1
  fi

  # check if correct directory by checking for 'seafile.db'
  if [ ! -f "$SEAFILEDATA/seafile.db" ]; then
    printf '\n%s\n' "Couldn't find 'seafile.db' in directory '$SEAFILEDATA'" && exit 1
  fi

  # as from the manual, stop seafile first and start again after the garbage
  # collection
  execute stop && \
  su -s /bin/sh -l $SEAFILEUSER -c "seafserv-gc -c \"$WORKING_DIR/ccnet\" -d \"$SEAFILEDATA\"" && \
  execute start
}

case "$ARGUMENTS" in
  start)
    execute start
  ;;
  stop)
    execute stop
  ;;
  setup)
    execute setup
  ;;
  restart)
    execute stop && execute start
  ;;
  reset-admin)
    execute reset-admin
  ;;
  create-admin)
    execute create-admin
  ;;
  cleanup)
    garbage_collector
  ;;
  *)
  cat << EOF
Usage: seafile start|stop|restart|setup|create-admin|reset-admin|cleanup [-d <seafile-data-directory>]
EOF
  ;;
esac