cleanup
[citadel.git] / 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 the screen and draw the banner
35         echo -e '\033[2J\033[H\033[44m\033[37m\033[K\033[0m'
36         echo -e '\033[44m\033[37m\033[1m  Citadel installation\033[K\033[0m'
37         echo -e '\033[44m\033[37m \033[K\033[0m'
38         echo
39
40         # First ... check to make sure Citadel isn't already running.
41         echo Checking to make sure Citadel is not already running...
42         if ps ax | grep citserver | grep -v grep ; then
43                 echo
44                 echo -e '\033[31m\033[1mInstallation cannot proceed while Citadel is running.\033[0m'
45                 echo Stop your services and try again.
46                 echo
47                 exit 1
48         fi
49         echo OK
50         echo
51
52         # Check compatibility
53         echo Checking this AppImage compatibility with your host system...
54         (
55                 export LD_LIBRARY_PATH=$APPDIR/usr/lib
56                 export PATH=$APPDIR/usr/bin
57                 $APPDIR/usr/local/citadel/citserver -c || {
58                         echo
59                         echo -e '\033[31m\033[1mCompatibility failed.\033[0m'
60                         echo 'This AppImage is not compatible with your system.'
61                         echo 'If you are attempting to run it on a compatible platform, please make sure you'
62                         echo 'have all of the latest updates and patches applied.  Otherwise, you may have'
63                         echo 'better luck with the Easy Install method.'
64                         echo ''
65                         exit 2
66                 }
67         ) || exit 2
68         echo OK
69         echo
70
71         # Clear the screen and draw the banner
72         echo -e '\033[2J\033[H\033[44m\033[37m\033[K\033[0m'
73         echo -e '\033[44m\033[37m\033[1m  Citadel installation\033[K\033[0m'
74         echo -e '\033[44m\033[37m \033[K\033[0m'
75         echo
76
77         # Locate the Citadel run directory
78         confirmed=0
79         while [ ${confirmed} == 0 ] 
80         do
81                 echo -n In what directory will you run Citadel\?  \[${CTDL_DIR}\]\ 
82                 read dir
83                 if [ "${dir}" == "" ]
84                 then
85                         dir=${CTDL_DIR}
86                 fi
87                 if [ ! -d ${dir} ] 
88                 then
89                         echo -n ${dir} does not exist.  Do you want to create it\?\ 
90                         read yesno
91                         if [ `echo ${yesno} | cut -c1` == 'y' ]
92                         then
93                                 mkdir ${dir}
94                         fi
95                 fi
96                 if [ -d ${dir} ] 
97                 then
98                         confirmed=1
99                 fi
100         done
101         CTDL_DIR=${dir}
102
103         # Check systemd
104         echo Checking this operating system for systemd...
105         systemctl --version >/dev/null 2>&1 || {
106                 echo Automatic installation is only supported with systemd.
107                 echo You can still run Citadel but you will need to start it some other way.
108                 exit 3
109         } | exit 3
110         echo OK
111         echo
112
113         # Remove old unit files
114         echo Checking for old startup files.
115         ls /etc/systemd/system/citadel* 2>/dev/null && {
116                 rm -i /etc/systemd/system/citadel*
117         }
118         ls /etc/systemd/system/webcit* 2>/dev/null && {
119                 rm -i /etc/systemd/system/webcit*
120         }
121         echo OK
122         echo
123
124         # Clear the screen and draw the banner
125         echo -e '\033[2J\033[H\033[44m\033[37m\033[K\033[0m'
126         echo -e '\033[44m\033[37m\033[1m  Citadel installation\033[K\033[0m'
127         echo -e '\033[44m\033[37m \033[K\033[0m'
128         echo
129
130         echo Ready to install ${APPIMAGE} in ${CTDL_DIR}
131         echo Copying the AppImage...
132         rm -f ${CTDL_DIR}/citadel.appimage 2>/dev/null
133         cp ${APPIMAGE} ${CTDL_DIR}/citadel.appimage || {
134                 echo Installation has failed with error code $? .
135                 exit 4
136         } | exit 4
137
138         echo Creating the systemd unit file...
139         (
140                 echo '# This unit file starts all Citadel services via the AppImage distribution.'
141                 echo '# Automatically installed on' `date`
142                 echo ''
143                 echo '[Unit]'
144                 echo 'Description=Citadel'
145                 echo 'After=network.target'
146                 echo ''
147                 echo '[Service]'
148                 echo 'ExecStart='${CTDL_DIR}'/citadel.appimage run -h '${CTDL_DIR}' -p '${HTTP_PORT}' -s '${HTTPS_PORT}
149                 echo 'ExecStop=/bin/kill $MAINPID'
150                 echo 'KillMode=process'
151                 echo 'Restart=on-failure'
152                 echo 'LimitCORE=infinity'
153                 echo ''
154                 echo '[Install]'
155                 echo 'WantedBy=multi-user.target'
156         ) >/etc/systemd/system/citadel.service || {
157                 echo Installation has failed with error code $? .
158                 exit 5
159         } | exit 5
160         echo OK
161         echo
162
163         echo Enabling the service...
164         systemctl enable citadel || {
165                 echo Installation has failed with error code $? .
166                 exit 6
167         } | exit 6
168         echo OK
169         echo
170
171         echo Starting the service...
172         systemctl start citadel || {
173                 echo Installation has failed with error code $? .
174                 exit 6
175         } || exit 6
176         echo OK
177         echo
178
179         # Clear the screen and draw the banner
180         echo -e '\033[2J\033[H\033[44m\033[37m\033[K\033[0m'
181         echo -e '\033[44m\033[37m\033[1m  Citadel installation\033[K\033[0m'
182         echo -e '\033[44m\033[37m \033[K\033[0m'
183         echo
184
185         echo -e '\033[32m\033[1mInstallation has completed.\033[0m'
186         echo Please continue by browsing to http://`hostname`:${HTTP_PORT}
187         echo
188         echo -e 'If this is a new installation, the default administrator'
189         echo -e 'will be named \033[7madmin\033[0m with password \033[7mcitadel\033[0m.'
190         exit
191 }
192
193
194 PARSED_ARGUMENTS=$(getopt -o h:p:s: -- "$@")
195 VALID_ARGUMENTS=$?
196 if [ "$VALID_ARGUMENTS" != "0" ]; then
197         usage
198 fi
199
200 eval set -- ${PARSED_ARGUMENTS}
201 while :
202 do
203         case ${1} in
204                 -h)     CTDL_DIR=${2}   ; shift 2 ;;
205                 -p)     HTTP_PORT=${2}  ; shift 2 ;;
206                 -s)     HTTPS_PORT=${2} ; shift 2 ;;
207                 --)     shift; break;;
208                 *)      echo Unexpected option: ${1}
209                         usage;
210         esac
211 done
212
213 case ${1} in
214         run)
215                 export APPDIR CTDL_DIR HTTP_PORT HTTPS_PORT
216                 export LD_LIBRARY_PATH=$APPDIR/usr/lib
217                 export PATH=$APPDIR/usr/bin
218                 exec ctdlvisor $*
219                 ;;
220         test)
221                 export LD_LIBRARY_PATH=$APPDIR/usr/lib
222                 export PATH=$APPDIR/usr/bin
223                 exec $APPDIR/usr/local/citadel/citserver -c
224                 ;;
225         install)
226                 installation
227                 ;;
228         database_cleanup)
229                 export LD_LIBRARY_PATH=$APPDIR/usr/lib
230                 export PATH=$APPDIR/usr/bin:$PATH
231                 exec $APPDIR/usr/local/citadel/database_cleanup.sh -h $CTDL_DIR
232                 ;;
233         migrate)
234                 export APPDIR CTDL_DIR HTTP_PORT HTTPS_PORT
235                 export LD_LIBRARY_PATH=$APPDIR/usr/lib
236                 export PATH=$APPDIR/usr/bin
237                 exec ctdlvisor -m
238                 ;;
239         mount)
240                 echo This mode of operation mounts the AppImage but does not run anything.
241                 echo -n Kill it when you are finished...
242                 while true
243                 do
244                         sleep 1
245                 done
246                 ;;
247         debug)
248                 export LD_LIBRARY_PATH=$APPDIR/usr/lib
249                 export PATH=$APPDIR/usr/bin:$PATH
250                 exec $APPDIR/usr/local/citadel/citserver -x9 -h $CTDL_DIR
251                 ;;
252         *)
253                 echo Unexpected command: ${1} 
254                 usage
255                 exit 1
256                 ;;
257 esac
258 exit 0