-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup-database-seed
executable file
·153 lines (125 loc) · 3.93 KB
/
setup-database-seed
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
#!/usr/bin/env bash
# TRAP ERRORS
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR
# END TRAP ERRORS
# GLOBALS
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
ganache_seeds_folder="./ganache_seeds"
# END GLOBALS
usage() {
cat << EOF # remove the space between << and EOF, this is due to web plugin issue
Usage: $(basename "${BASH_SOURCE[0]}") [-s] [-a]
Handle ganache seeds
Available options:
-h, --help Print this help and exit
-v, --verbose Print script debug info
-a, --all Copy all seeds
-s, --seed string One of the seeds available. One of the subfolder of ganache_seeds (eg: 'base')
EOF
exit
}
cleanup() {
trap - SIGINT SIGTERM ERR
return 1
}
setup_colors() {
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m'
else
NOFORMAT='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW=''
fi
}
msg() {
echo >&2 -e "${1-}"
}
die() {
local msg=$1
local code=${2-1} # default exit status 1
msg "$msg"
exit "$code"
}
parse_params() {
# default values of variables set from params
seed_name=""
while :; do
case "${1-}" in
-h | --help)
usage
;;
-v | --verbose)
set -x
;;
-a | --all)
seed_name="*"
;;
--no-color)
NO_COLOR=1
;;
-s=*|--seed=*) # seed param
seed_name="${1#*=}"
seed_name=$(echo "$seed_name" | sed 's/\(\"\|\/opt\/\)//g')
;;
-v=*|--verbose=*) # verbose param
set -x
;;
-?*)
die "Unknown option: $1"
;;
*) break ;;
esac
shift
done
args=("$@")
[[ -z "${seed_name-}" ]] && die "You need to specify a seed name using --seed=[seed_name] or use --all"
return 0
}
# use docker busybox to copy/remove files
copy_folders() {
local selected_seeds_folders=$1
local volume_folder=$2
docker run --name docker_cp_helper -v "$volume_folder:/data" busybox true
for pathname in $selected_seeds_folders; do
local seed_name=$(echo "$pathname" | sed "s%$ganache_seeds_folder/%%g")
local destination_folder="/data/${seed_name}"
msg "${BLUE}- Copying seed: ${seed_name}${NOFORMAT}"
if [[ -d "$destination_folder" ]]; then
docker run --rm -v "$volume_folder:/data" busybox rm -i -rf "${destination_folder}/*"
docker cp "$pathname"/* "docker_cp_helper:$destination_folder"
else
docker run --rm -v "$volume_folder:/data" busybox rm -i -rf "$destination_folder"
docker cp "$pathname" "docker_cp_helper:$destination_folder"
fi
done
docker rm docker_cp_helper
return 0
}
copy_seed_folder() {
local ganache_volume_folder
if [[ -z ${GANACHE_VOLUME:-} ]]; then
ganache_volume_folder="$(grep -v '#' < .env | sed -e 's/^GANACHE_VOLUME=\(.*\)$/\1/;t;d' | sed 's/\"//g')"
else
ganache_volume_folder=$GANACHE_VOLUME
fi
local absolute_volume_folder=$(realpath "$ganache_volume_folder")
local ganache_db_folder="${ganache_volume_folder}/${seed_name}"
local ganache_db_file="${ganache_db_folder}/LOG"
if [[ $seed_name == "*" || -f "$ganache_db_file" ]]; then
msg "${CYAN}Copying seed to volume folder:${NOFORMAT}"
local source_folder="$ganache_seeds_folder/$seed_name"
copy_folders "$source_folder" "$absolute_volume_folder"
docker run --rm -v "$absolute_volume_folder:/data" busybox mkdir -p /data/tests
msg "${GREEN}Seed copied successfully [${ganache_seeds_folder}/${seed_name} -> ${ganache_volume_folder}/] ${NOFORMAT}"
return 0
fi
msg "${RED}Seed does not exist within 'ganache_seeds': ${seed_name} ${NOFORMAT}"
return 1
}
# SETUP
parse_params "$@"
setup_colors
# SCRIPT LOGIC
msg "${CYAN}Read parameters:${NOFORMAT}"
msg "${BLUE}- seed_name: ${seed_name}"
cd "${script_dir}/.."
copy_seed_folder