Build from the published sources, not from git
[citadel-docker.git] / run-citadel.sh
1 #!/bin/bash
2
3 # This is a simple startup script to run Citadel in a Docker container.
4 # If you know your way around Docker, you don't have to use it; feel free to do whatever you want.
5 # The container expects a persistent volume called "citadel-data" in which it will keep everything.
6 # The remainder of the container is ephermal and can be deleted at any time.
7
8 migrate_mode=false
9 database_cleanup_mode=false
10 volume_mode=false
11 bind_mode=false
12
13 # Parse the command line arguments
14 while getopts ":mdv:h:" o
15 do
16         case "${o}" in
17                 m)
18                         echo "migrate mode"
19                         migrate_mode=true
20                         ;;
21                 d)
22                         echo "database cleanup mode";
23                         database_cleanup_mode=true
24                         ;;
25                 v)
26                         volume_mode=true
27                         CTDL_DIR=${OPTARG}
28                         ;;
29                 h)
30                         bind_mode=true
31                         CTDL_DIR=${OPTARG}
32                         ;;
33                 *)
34                         echo
35                         echo "$0: usage: $0 [-v volume_name] [-h home_directory] [-m] [-d] container_image_name"
36                         echo "  -v volume_name          Use or create a Citadel database stored in a Docker volume called 'volume_name'"
37                         echo "  -h home_directory       Use or create a Citadel database stored in 'home_directory' on the host system"
38                         echo "  -m                      Enter migrate mode, to copy a remote Citadel installation to this host"
39                         echo "  -d                      Attempt to recover a corrupt database"
40                         exit 1
41                         ;;
42         esac
43 done
44 shift $((OPTIND-1))
45
46 if ( ${volume_mode} && ${bind_mode} ) ; then
47         echo "$0: -v and -h cannot both be specified."
48         exit 2
49 fi
50
51 if ${volume_mode} ; then
52         CTDL_VOL=volume
53 elif ${bind_mode} ; then
54         CTDL_VOL=bind
55 else
56         volume_mode=true
57         CTDL_VOL=bind
58         CTDL_DIR=/usr/local/citadel
59         mkdir /usr/local/citadel >/dev/null 2>/dev/null
60 fi
61
62 if ( ${migrate_mode} && ${database_cleanup_mode} ) ; then
63         echo "$0: -m and -d cannot both be specified."
64         exit 3
65 fi
66
67 docker run -i --rm citadel -c || exit 1
68
69 n_args=""
70 c_args=""
71
72 if ${migrate_mode} ; then
73         c_args="-m"                             # Tell ctdlvisor to run ctdlmigrate
74 elif ${database_cleanup_mode} ; then
75         c_args="-d"                             # Tell ctdlvisor to run database_cleanup.sh
76 else
77         n_args="--network=host"                 # Only open ports if we're running in normal mode
78 fi
79
80 exec docker run \
81         --name=citadel \
82         -i \
83         --rm \
84         ${n_args} \
85         --mount type=${CTDL_VOL},source=${CTDL_DIR},target=/citadel-data \
86         citadel \
87         ${c_args}