-
Notifications
You must be signed in to change notification settings - Fork 0
/
sbml4j.sh
executable file
·203 lines (187 loc) · 9.18 KB
/
sbml4j.sh
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/bash
# Default volume name should be prefixed with the folder name
#default_volume_prefix="$(echo ${PWD##*/} | tr '[A-Z]' '[a-z]')"
default_volume_prefix="sbml4j-compose"
# Default api defintion file name
default_api_def=sbml4j.yaml
function show_usage() {
echo "Usage: "
echo "${0} -h | -b | -i | -a | -r "
echo "Details:"
echo "This script is used to either setup the SBML4J volumes, setup the database from database dumps, or backup the databases into a database dump."
echo "You can either use any one option alone, or use the -i and -r options together."
echo " -h : Print this help"
echo " -b {argument} :"
echo " Backup the current database into the backup files named by the {argument}"
echo " -i :"
echo " Install prerequisits for SBML4J."
echo " This will recreate the volumes used for SBML4J, the (empty) neo4j database and the api documentation using the default api-documentation file ${default_api_doc} found in the api_doc subfolder."
echo " -a : {argument} :"
echo " Used only in conjunction with -i to provide the filename of an alternative api-documentation file, which has to be placed in the api_doc subfolder. "
echo " -r {argument} :"
echo " Restore the neo4j database from the database dumps in the files with prefix given by the {argument}"
# echo " -p {argument} :"
# echo " Use {argument} as prefix for the volumes created, i.e. argument_sbml4j_service_vol"
# echo " Use in conjuction with the -b, -i, -s flags."
echo " "
echo "Examples:"
echo "${0} -i :"
echo " This will (re)-create the volumes for SBML4J and use the default api definition file ${default_api_def} as source for the api page"
echo "${0} -i -a myapidef.yaml :"
echo " This will (re)-create the volumes for SBML4J and use the provided api definition file myapidef.yaml as source for the api page"
echo "${0} -b mydbbackup"
echo " This will create a database dump of the neo4j and system database in the files mydbbackup-neo4j.dump and mydbbackup-system.dump respectively"
echo "${0} -r mydbbackup"
echo " This will load a database dump from the neo4j and system database dump files mydbbackup-neo4j.dump and mydbbackup-system.dump."
echo " WARNING: Any data currently stored in the database will be overwritten"
echo "${0} -i -r mydbbackup"
echo " This will (re-)create the volumes for SBML4J (as described above) and load the database backup from the database dunmp files as described above."
# echo "${0} -i -r mydbbackup -p my-compose"
# echo " This will (re-)create the volumes with names my-compose_sbml4j_neo4j_vol instead of the default name sbml4j-compose_sbml4j_neo4j_vol for SBML4J (as described above) and load the database backup from the database dunmp files as described above."
# echo " This needs to be used when you want to use these volumes in a different compose setup"
}
function install() {
api_def=$1
prefix_name=$2
# Steps to do:
# Start a docker container running a shell
# mount the /vol dir to create all prerequisits for neo4j
# run script inside container that
# creates plugin dir
# downloads apoc plugin
# creates logs dir
# creates data dir
# creates conf dir
# copies conf file in volume
# fixes permissions
echo "Creating volume for neo4j database"
docker run --rm --detach --mount type=bind,src=${PWD}/scripts,dst=/scripts --mount type=bind,src=${PWD}/conf,dst=/conf --mount type=volume,src=${prefix_name}_sbml4j_neo4j_vol,dst=/vol alpine /scripts/setup_neo4j.sh
# Start a docker container running a shell
# mount the api-doc volume to create all prerequisits for the api doc
# copy api definition file in volume
echo "Creating volume for api documentation"
if [ -a ${PWD}/api_doc/$api_def ]
then
echo "Using API definition file $api_def"
docker run --rm --detach --mount type=bind,src=${PWD}/api_doc,dst=/api --mount type=volume,src=${prefix_name}_sbml4j_api_doc_vol,dst=/definition alpine cp /api/$api_def /definition/sbml4j.yaml
else
echo "Could not find provided API definition file $api_def, trying the default file ${default_api_def}"
docker run --rm --detach --mount type=bind,src=${PWD}/api_doc,dst=/api --mount type=volume,src=${prefix_name}_sbml4j_api_doc_vol,dst=/definition alpine cp /api/${default_api_def} /definition/sbml4j.yaml
fi
# Start a docker container running a shell
# mount the /sbml4j dir to create all prerequisits for sbml4j
# basically create the logs folder in the volume
echo "Creating volume for the sbml4j service"
docker run --rm --detach --mount type=volume,src=${prefix_name}_sbml4j_service_vol,dst=/logs alpine touch /logs/root.log
}
function ensure_db_backups_folder_exists() {
if [ ! -d "${PWD}/db_backups" ]
then
echo "Directory for database backups not found. Creating directory with name 'db_backups' in current folder ${PWD}"
mkdir -p ${PWD}/db_backups
fi
}
function setup_db() {
backup_base_name=$1
prefix_name=$2
ensure_db_backups_folder_exists
# Start neo4j for restoring the neo4j backup (twice: one neo4j, one system)
docker run --interactive --tty --rm --publish=7474:7474 --publish=7687:7687 --mount type=volume,src=${prefix_name}_sbml4j_neo4j_vol,dst=/vol --mount type=bind,src=$PWD/db_backups,dst=/backups --user="7474:7474" --env NEO4J_CONF=/vol/conf neo4j:4.1.6 neo4j-admin load --from=/backups/${backup_base_name}-neo4j.dump --database=neo4j --force
#
docker run --interactive --tty --rm --publish=7474:7474 --publish=7687:7687 --mount type=volume,src=${prefix_name}_sbml4j_neo4j_vol,dst=/vol --mount type=bind,src=$PWD/db_backups,dst=/backups --user="7474:7474" --env NEO4J_CONF=/vol/conf neo4j:4.1.6 neo4j-admin load --from=/backups/${backup_base_name}-system.dump --database=system --force
}
function backup_db() {
backup_base_name=$1
prefix_name=$2
ensure_db_backups_folder_exists
# Start neo4j for backing upthe neo4j database (twice: one neo4j, one system)
docker run --interactive --tty --rm --publish=7474:7474 --publish=7687:7687 --mount type=volume,src=${prefix_name}_sbml4j_neo4j_vol,dst=/vol --mount type=bind,src=$PWD/db_backups,dst=/backups --user="7474:7474" --env NEO4J_CONF=/vol/conf neo4j:4.1.6 neo4j-admin dump --database=neo4j --to=/backups/${backup_base_name}-neo4j.dump
docker run --interactive --tty --rm --publish=7474:7474 --publish=7687:7687 --mount type=volume,src=${prefix_name}_sbml4j_neo4j_vol,dst=/vol --mount type=bind,src=$PWD/db_backups,dst=/backups --user="7474:7474" --env NEO4J_CONF=/vol/conf neo4j:4.1.6 neo4j-admin dump --database=system --to=/backups/${backup_base_name}-system.dump
}
declare -i i=0
while getopts a:hb:ir:p: flag
do
case "${flag}" in
a) api_def=${OPTARG}
is_custom_api_def=True
;;
h) show_usage
exit 0
;;
b) backup_name=${OPTARG}
do_backup=True
i=i+100
#echo "Performing database backup into file: $backup_name"
;;
i) do_install=True
i=i+1
;;
r) backup_name=${OPTARG}
do_setup=True
#echo "Performing database setup from file: $backup_name"
i=i+10
;;
# p) prefix_name=${OPTARG}
# is_prefix_set=True
# ;;
esac
done
echo $i
function check_api_def() {
# Do we have a custom api defintion file
if [ "$is_custom_api_def" = True ]
then
echo "Using custom API definition file ${api_def}"
else
echo "Using default API defintion file ${default_api_def}"
api_def=${default_api_def}
fi
}
function check_prefix_name() {
# Do we have a custom prefix set
if [ "$is_prefix_set" = True ]
then
echo "Using custom prefix name ${prefix_name}"
else
echo "Using default prefix name ${default_volume_prefix}"
prefix_name=$default_volume_prefix
fi
}
if [ "$i" -lt "1" ]
then
echo "No argument given. Please give one or two arguments."
show_usage
exit 0
elif [ "$i" -lt "10" ]
then
echo "Performing installation of prerequisits for running sbml4j"
check_api_def
check_prefix_name
install $api_def $prefix_name
echo "Successfully installed prerequisists for running sbml4j."
exit 0
elif [ "$i" -lt "11" ]
then
echo "Restoring database from dumps: $backup_name-neo4j.dump and $backup_name-system.dump."
check_prefix_name
setup_db $backup_name $prefix_name
echo "Successfully restored database."
exit 0
elif [ "$i" -lt "12" ]
then
echo "Performing installation of prerequisits for running sbml4j"
check_api_def
check_prefix_name
install $api_def $prefix_name
echo "Restoring database from dumps: $backup_name-neo4j.dump and $backup_name-system.dump."
setup_db $backup_name $prefix_name
echo "Successfully installed prerequisits and resored database state."
exit 0
elif [ "$i" -lt "101" ]
then
echo "Performing database backup into dump-files $backup_name-neo4j.dump and $backup_name-system.dump."
check_prefix_name
backup_db $backup_name $prefix_name
echo "Successfully created database backup into dumps: $backup_name-neo4j.dump and $backup_name-system.dump."
exit
fi