-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-sync-rapid-pro-to-engagement-db.sh
executable file
·98 lines (80 loc) · 3.45 KB
/
docker-sync-rapid-pro-to-engagement-db.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
#!/bin/bash
set -e
IMAGE_NAME="$(<configurations/docker_image_name.txt)"
while [[ $# -gt 0 ]]; do
case "$1" in
--dry-run)
DRY_RUN="--dry-run"
shift;;
--incremental-cache-volume)
INCREMENTAL_ARG="--incremental-cache-path /cache"
INCREMENTAL_CACHE_VOLUME_NAME="$2"
shift 2;;
--local-archive)
LOCAL_ARCHIVE_PATHS+=("$2")
shift 2;;
--)
shift
break;;
*)
break;;
esac
done
# Check that the correct number of arguments were provided.
if [[ $# -ne 5 ]]; then
echo "Usage: $0
[--dry-run] [--incremental-cache-volume <incremental-cache-volume>]
[--local-archive <local_archive>] : set a single option with argument, repeat it multiple times
<user> <google-cloud-credentials-file-path> <configuration-file> <code-schemes-dir> <data-dir>"
exit
fi
# Assign the program arguments to bash variables.
USER=$1
GOOGLE_CLOUD_CREDENTIALS_PATH=$2
CONFIGURATION_FILE=$3
CODE_SCHEMES_DIR=$4
DATA_DIR=$5
# Set local archive arguments if specified
LOCAL_ARCHIVE_ARGS=""
for LOCAL_ARCHIVE_PATH in "${LOCAL_ARCHIVE_PATHS[@]}"; do
IFS="=" # Setting equal sign as delimiter
read -a strarr <<<"$LOCAL_ARCHIVE_PATH" # Reading str as an array of tokens separated by IFS
gs_url=${strarr[0]}; local_archive_dir=$(basename ${strarr[1]})
LOCAL_ARCHIVE_ARGS+=" --local-archive $gs_url=/$local_archive_dir"
done
CMD="pdm run python -u sync_rapid_pro_to_engagement_db.py ${DRY_RUN} ${INCREMENTAL_ARG} ${LOCAL_ARCHIVE_ARGS} \
${USER} /credentials/google-cloud-credentials.json configuration"
if [[ "$INCREMENTAL_ARG" ]]; then
container="$(docker container create -w /app --mount source="$INCREMENTAL_CACHE_VOLUME_NAME",target=/cache "$IMAGE_NAME" /bin/bash -c "$CMD")"
else
container="$(docker container create -w /app "$IMAGE_NAME" /bin/bash -c "$CMD")"
fi
echo "Created container $container"
container_short_id=${container:0:7}
# Copy input data into the container
echo "Copying $GOOGLE_CLOUD_CREDENTIALS_PATH -> $container_short_id:/credentials/google-cloud-credentials.json"
docker cp "$GOOGLE_CLOUD_CREDENTIALS_PATH" "$container:/credentials/google-cloud-credentials.json"
echo "Copying $CODE_SCHEMES_DIR -> $container_short_id:/app/code_schemes"
docker cp "$CODE_SCHEMES_DIR" "$container:/app/code_schemes"
echo "Copying $CONFIGURATION_FILE -> $container_short_id:/app/configuration.py"
docker cp "$CONFIGURATION_FILE" "$container:/app/configuration.py"
for LOCAL_ARCHIVE_PATH in "${LOCAL_ARCHIVE_PATHS[@]}"; do
IFS="=" # Setting equal sign as delimiter
read -a strarr <<<"$LOCAL_ARCHIVE_PATH" # Reading str as an array of tokens separated by IFS
# Expand the tilde character to home directory if available
path=$(pdm run python -c "import os.path; print(os.path.expanduser(\"${strarr[1]}\"))")
local_archive_dir=$(basename $path)
echo "Copying $path -> $container_short_id:/$local_archive_dir"
docker cp "$path" "$container:/$local_archive_dir"
done
# Run the container
echo "Starting container $container_short_id"
docker start -a -i "$container"
# Copy cache data out of the container for backup
if [[ "$INCREMENTAL_ARG" ]]; then
echo "Copying $container_short_id:/cache/. -> $DATA_DIR/Cache"
mkdir -p "$DATA_DIR/Cache"
docker cp "$container:/cache/." "$DATA_DIR/Cache"
fi
# Tear down the container when it has run successfully
docker container rm "$container" >/dev/null