forked from metabrainz/musicbrainz-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-amqp-triggers
executable file
·135 lines (110 loc) · 3.73 KB
/
setup-amqp-triggers
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
#!/usr/bin/env bash
set -e -u
# shellcheck source=admin/lib/common.inc.bash
source "$(dirname "${BASH_SOURCE[0]}")/lib/common.inc.bash"
HELP=$(cat <<EOH
Usage: $SCRIPT_NAME <clean|install|uninstall|help>
Commands:
clean Remove temporary install scripts and uninstall scripts.
install Create database triggers and add uninstall scripts.
uninstall Drop database triggers and remove uninstall scripts.
help Print this help message
EOH
)
if [ $# -ne 1 ]
then
echo >&2 "$SCRIPT_NAME: wrong number of arguments"
echo >&2 "Try '$SCRIPT_NAME help' for usage."
exit 64 # EX_USAGE
fi
LOCAL_TRIGGERS_DIR="$(dirname "${BASH_SOURCE[0]}")/.setup-amqp-triggers-sql"
REMOTE_TRIGGERS_DIR=/tmp/indexer-sql
generate_sql_scripts() {
$DOCKER_COMPOSE_CMD exec indexer python -m sir triggers
$DOCKER_COMPOSE_CMD exec indexer ./GenerateDropSql.pl
}
copy_sql_scripts() {
remove_temp_sql_scripts_from_host
remove_temp_sql_scripts_from_container
indexer_container_id="$($DOCKER_COMPOSE_CMD ps -q indexer)"
$DOCKER_CMD cp "$indexer_container_id":/code/sql "$LOCAL_TRIGGERS_DIR"
if [ "$(stat -c %U "$LOCAL_TRIGGERS_DIR")" != "$(id -un)" ]
then
sudo chown -R "$(id -un):$(id -gn)" "$LOCAL_TRIGGERS_DIR"
fi
musicbrainz_container_id="$($DOCKER_COMPOSE_CMD ps -q musicbrainz)"
$DOCKER_CMD cp "$LOCAL_TRIGGERS_DIR" "$musicbrainz_container_id":"$REMOTE_TRIGGERS_DIR"
remove_temp_sql_scripts_from_host
}
remove_temp_sql_scripts_from_host() {
if [ -e "$LOCAL_TRIGGERS_DIR" ]
then
if [ "$(stat -c %U "$LOCAL_TRIGGERS_DIR")" != "$(id -un)" ]
then
sudo rm -fr"${1:+v}" "$LOCAL_TRIGGERS_DIR"
else
rm -fr"${1:+v}" "$LOCAL_TRIGGERS_DIR"
fi
fi
}
remove_temp_sql_scripts_from_container() {
$DOCKER_COMPOSE_CMD exec musicbrainz rm -fr"${1:+v}" "$REMOTE_TRIGGERS_DIR"
}
case "$1" in
clean )
if ! $DOCKER_COMPOSE_CMD ps musicbrainz | grep -qw 'running\|Up'
then
echo >&2 "$SCRIPT_NAME: cannot clean: " \
"the Docker Compose service 'musicbrainz' is not up"
echo >&2 "Try '$DOCKER_COMPOSE_CMD up -d musicbrainz' from '$MB_DOCKER_ROOT'"
exit 69 # EX_UNAVAILABLE
fi
echo "Cleaning indexer triggers install/uninstall scripts up ..."
remove_temp_sql_scripts_from_host -v
remove_temp_sql_scripts_from_container -v
;;
install )
for service in indexer musicbrainz
do
if ! $DOCKER_COMPOSE_CMD ps "$service" | grep -qw 'running\|Up'
then
echo >&2 "$SCRIPT_NAME: cannot install: " \
"the Docker Compose service '$service' is not up"
echo >&2 "Try '$DOCKER_COMPOSE_CMD up -d $service' from '$MB_DOCKER_ROOT'"
exit 69 # EX_UNAVAILABLE
fi
done
generate_sql_scripts
copy_sql_scripts
echo "Installing indexer triggers ..."
$DOCKER_COMPOSE_CMD exec musicbrainz indexer-triggers.sh "$REMOTE_TRIGGERS_DIR" create
;;
uninstall )
for service in indexer musicbrainz
do
if ! $DOCKER_COMPOSE_CMD ps "$service" | grep -qw 'running\|Up'
then
echo >&2 "$SCRIPT_NAME: cannot uninstall: " \
"the Docker Compose service '$service' is not up"
echo >&2 "Try '$DOCKER_COMPOSE_CMD up -d $service' from '$MB_DOCKER_ROOT'"
exit 69 # EX_UNAVAILABLE
fi
done
generate_sql_scripts
copy_sql_scripts
echo "Uninstalling indexer triggers ..."
$DOCKER_COMPOSE_CMD exec musicbrainz indexer-triggers.sh "$REMOTE_TRIGGERS_DIR" drop
remove_temp_sql_scripts_from_container
;;
help|-h|--help)
echo "$HELP"
exit 0 # EX_OK
;;
* )
echo >&2 "$SCRIPT_NAME: unrecognized command '$1'"
echo >&2 "Try '$SCRIPT_NAME help' for usage."
exit 64 # EX_USAGE
;;
esac
echo "Done."
# vi: set et sts=2 sw=2 ts=2 :