working on ctdlmigrate in the appimage
[citadel] / appimage / citadel.AppDir / AppRun
1 #!/bin/bash
2
3 # This is an AppImage control script for the Citadel system.
4 #
5 # Copyright (c) 2021 by the citadel.org team
6 #
7 # This program is open source software.  It runs great on the
8 # Linux operating system (and probably elsewhere).  You can use,
9 # copy, and run it under the terms of the GNU General Public
10 # License version 3.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # Default values
18 HTTP_PORT=80
19 HTTPS_PORT=443
20 CTDL_DIR=/usr/local/citadel
21 export APPDIR
22 ulimit -c unlimited
23
24 usage() {
25         echo ${APPIMAGE}: usage: ${APPIMAGE} '[-h data_directory] [-p http_port] [-s https_port] command'
26         echo 'command must be one of: run, test, install, database_cleanup, migrate'
27         exit 2
28 }
29
30
31 # Permanently install the Citadel AppImage to this system
32 installation() {
33
34         clear
35         echo
36         echo
37
38         # First ... check to make sure Citadel isn't already running.
39         echo Checking to make sure Citadel is not already running...
40         if ps ax | grep citserver | grep -v grep ; then
41                 echo Installation cannot proceed while Citadel is running.
42                 echo Stop your services and try again.
43                 exit 1
44         fi
45         echo OK
46         echo
47
48         # Check compatibility
49         echo Checking this AppImage compatibility with your host system...
50         (
51                 export LD_LIBRARY_PATH=$APPDIR/usr/lib
52                 export PATH=$APPDIR/usr/bin
53                 $APPDIR/usr/local/citadel/citserver -c || {
54                         echo Compatibility failed.
55                         exit 2
56                 }
57         )
58         echo OK
59         echo
60
61         # Locate the Citadel run directory
62         confirmed=0
63         while [ ${confirmed} == 0 ] 
64         do
65                 echo -n In what directory will you run Citadel\?  \[${CTDL_DIR}\]\ 
66                 read dir
67                 if [ "${dir}" == "" ]
68                 then
69                         dir=${CTDL_DIR}
70                 fi
71                 if [ ! -d ${dir} ] 
72                 then
73                         echo -n ${dir} does not exist.  Do you want to create it\?\ 
74                         read yesno
75                         if [ `echo ${yesno} | cut -c1` == 'y' ]
76                         then
77                                 mkdir ${dir}
78                         fi
79                 fi
80                 if [ -d ${dir} ] 
81                 then
82                         confirmed=1
83                 fi
84         done
85         CTDL_DIR=${dir}
86
87         # Check systemd
88         echo Checking this operating system for systemd...
89         systemctl --version >/dev/null 2>&1 || {
90                 echo Automatic installation is only supported with systemd.
91                 echo You can still run Citadel but you will need to start it some other way.
92                 exit 3
93         }
94         echo OK
95         echo
96
97         # Remove old unit files
98         echo Checking for old startup files.
99         ls /etc/systemd/system/citadel* 2>/dev/null && {
100                 rm -i /etc/systemd/system/citadel*
101         }
102         ls /etc/systemd/system/webcit* 2>/dev/null && {
103                 rm -i /etc/systemd/system/webcit*
104         }
105         echo OK
106         echo
107
108         echo Ready to install ${APPIMAGE} in ${CTDL_DIR}
109         echo Copying the AppImage...
110         rm -f ${CTDL_DIR}/citadel.appimage 2>/dev/null
111         cp ${APPIMAGE} ${CTDL_DIR}/citadel.appimage || {
112                 echo Installation has failed with error code $? .
113                 exit 4
114         }
115
116         echo Creating the systemd unit file...
117         (
118                 echo '# This unit file starts all Citadel services via the AppImage distribution.'
119                 echo '# Automatically installed on' `date`
120                 echo ''
121                 echo '[Unit]'
122                 echo 'Description=Citadel'
123                 echo 'After=network.target'
124                 echo ''
125                 echo '[Service]'
126                 echo 'ExecStart='${CTDL_DIR}'/citadel.appimage run -h '${CTDL_DIR}' -p '${HTTP_PORT}' -s '${HTTPS_PORT}
127                 echo 'ExecStop=/bin/kill $MAINPID'
128                 echo 'KillMode=process'
129                 echo 'Restart=on-failure'
130                 echo 'LimitCORE=infinity'
131                 echo ''
132                 echo '[Install]'
133                 echo 'WantedBy=multi-user.target'
134         ) >/etc/systemd/system/citadel.service || {
135                 echo Installation has failed with error code $? .
136                 exit 5
137         }
138         echo OK
139         echo
140
141         echo Enabling the service...
142         systemctl enable citadel || {
143                 echo Installation has failed with error code $? .
144                 exit 6
145         }
146         echo OK
147         echo
148
149         echo Starting the service...
150         systemctl start citadel || {
151                 echo Installation has failed with error code $? .
152                 exit 6
153         }
154         echo OK
155         echo
156
157         echo Installation has completed.
158         echo Please continue by browsing to http://`hostname`:${HTTP_PORT}
159         exit
160 }
161
162
163 PARSED_ARGUMENTS=$(getopt -o h:p:s: -- "$@")
164 VALID_ARGUMENTS=$?
165 if [ "$VALID_ARGUMENTS" != "0" ]; then
166         usage
167 fi
168
169 eval set -- ${PARSED_ARGUMENTS}
170 while :
171 do
172         case ${1} in
173                 -h)     CTDL_DIR=${2}   ; shift 2 ;;
174                 -p)     HTTP_PORT=${2}  ; shift 2 ;;
175                 -s)     HTTPS_PORT=${2} ; shift 2 ;;
176                 --)     shift; break;;
177                 *)      echo Unexpected option: ${1}
178                         usage;
179         esac
180 done
181
182 case ${1} in
183         run)
184                 export APPDIR CTDL_DIR HTTP_PORT HTTPS_PORT
185                 export LD_LIBRARY_PATH=$APPDIR/usr/lib
186                 export PATH=$APPDIR/usr/bin
187                 exec ctdlvisor $*
188                 ;;
189         test)
190                 export LD_LIBRARY_PATH=$APPDIR/usr/lib
191                 export PATH=$APPDIR/usr/bin
192                 exec $APPDIR/usr/local/citadel/citserver -c
193                 ;;
194         install)
195                 installation
196                 ;;
197         database_cleanup)
198                 export LD_LIBRARY_PATH=$APPDIR/usr/lib
199                 export PATH=$APPDIR/usr/bin:$PATH
200                 exec $APPDIR/usr/local/citadel/database_cleanup.sh -h $CTDL_DIR
201                 ;;
202         migrate)
203                 export APPDIR CTDL_DIR HTTP_PORT HTTPS_PORT
204                 export LD_LIBRARY_PATH=$APPDIR/usr/lib
205                 export PATH=$APPDIR/usr/bin
206                 exec ctdlvisor -m -h $CTDL_DIR
207                 ;;
208         *)
209                 echo Unexpected command: ${1} 
210                 usage
211                 exit 1
212                 ;;
213 esac
214 exit 0