forked from veelkoov/fuzzrake
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoolbox
executable file
·240 lines (181 loc) · 7.4 KB
/
toolbox
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env bash
set -euo pipefail
PROJECT_NAME="${FUZZRAKE_DEV_PROJECT_NAME:-fuzzrake}"
IMPORT_DIR_PATH='var/iuFormData/' # Trailing slash required
DB_PATH='var/db.sqlite'
DB_TMP_PATH="$DB_PATH.tmp"
DB_DUMP_DIR_PATH='db_dump'
DB_DUMP_TMP_PATH="$DB_DUMP_DIR_PATH/fuzzrake.tmp.sql"
DB_DUMP_PRV_COPY_PATH="$DB_DUMP_DIR_PATH/artisans_private_data-$(date -u '+%Y-%m-%d_%H-%M-%S').sql"
# Volatile information, easily reproducible
DB_IGNORED_TABLES=(artisans_commissions_statuses artisans_volatile_data artisans_urls_states tracker_settings submissions)
function run_command() {
echo "Executing: $*"
"$@"
}
function run_docker_compose() {
run_command docker compose --project-directory docker --project-name "$PROJECT_NAME" "$@"
}
function run_docker_compose_exec() {
run_docker_compose exec --user "$(echo -n "$(id -u):$(id -g)")" -ti php "$@"
}
function run_composer() {
run_docker_compose_exec composer "$@"
}
function run_console() {
run_docker_compose_exec ./bin/console "$@"
}
function assure_line_in_file() {
local filepath="$1"
local pattern="$2"
local default="$3"
grep -q "$pattern" "$filepath" || {
echo "Appending '$default' to '$filepath'"
echo "$default" >> "$filepath"
}
}
function action_run_setup() {
# FIXME: https://github.com/veelkoov/fuzzrake/issues/168
run_command sudo mkdir -p ./var/cache
run_command sudo mkdir -p ./var/log
run_command sudo chmod -R a+w ./var
run_command touch ./var/db.sqlite
assure_line_in_file ./.env.local '^GOOGLE_RECAPTCHA_SITE_KEY=' 'GOOGLE_RECAPTCHA_SITE_KEY=__TODO_PROVIDE_THIS__'
assure_line_in_file ./.env.local '^GOOGLE_RECAPTCHA_SECRET=' 'GOOGLE_RECAPTCHA_SECRET=__TODO_PROVIDE_THIS__'
assure_line_in_file ./.env.test.local '^GOOGLE_RECAPTCHA_SITE_KEY=' 'GOOGLE_RECAPTCHA_SITE_KEY=__TODO_PROVIDE_THIS__'
assure_line_in_file ./.env.test.local '^GOOGLE_RECAPTCHA_SECRET=' 'GOOGLE_RECAPTCHA_SECRET=__TODO_PROVIDE_THIS__'
}
function backup_private_data() {
run_command sqlite3 "$DB_PATH" ".output $DB_DUMP_PRV_COPY_PATH" '.dump artisans_private_data'
}
function action_release_prod() {
run_command git checkout main
run_command git merge --no-edit develop
run_command git push
run_command git checkout develop
run_command git merge main
run_command git push
run_command ansible/setup_envs.yaml --limit prod_env
}
function action_release_beta() {
run_command git branch -D beta
run_command git checkout -b beta
run_command git push --force origin beta
run_command ansible/setup_envs.yaml --limit beta_env
echo 'Make sure to return to the previous branch' # FIXME: This stupid limitation
}
function action_get_snapshots() {
run_command rsync --recursive --progress --human-readable --compress --checksum \
getfursu.it:/var/www/prod/var/snapshots/ var/snapshots/
}
function action_get_submissions() {
. .env.local
run_command rsync --recursive --progress --human-readable --compress --checksum \
getfursu.it:/var/www/prod/var/iuFormData/ "$IMPORT_DIR_PATH"
run_command aws s3 sync --size-only "${S3_COPIES_BUCKET_URL%/}/" "$IMPORT_DIR_PATH"
}
function action_dbcommit() {
pushd "$DB_DUMP_DIR_PATH"
run_command git reset HEAD
run_command git commit -m 'Updated DB dump' -p
run_command git push
run_command git show -q
popd
}
function action_dbpull() {
run_command scp -p "getfursu.it:/var/www/prod/$DB_PATH" "$DB_TMP_PATH"
backup_private_data
run_command sqlite3 "$DB_TMP_PATH" 'DROP TABLE artisans_private_data;'
run_command sqlite3 "$DB_TMP_PATH" ".read $DB_DUMP_PRV_COPY_PATH"
run_command chmod a+w "$DB_TMP_PATH"
run_command mv "$DB_TMP_PATH" "$DB_PATH"
}
function action_dbpush() {
run_command cp "$DB_PATH" "$DB_TMP_PATH"
run_command sqlite3 "$DB_TMP_PATH" "UPDATE artisans_private_data SET original_contact_info = '', contact_address = '';"
run_command scp -p "$DB_TMP_PATH" "getfursu.it:/var/www/prod/$DB_PATH"
run_command scp -p "$DB_TMP_PATH" "getfursu.it:/var/www/beta/$DB_PATH"
run_command rm "$DB_TMP_PATH"
}
function action_dbdump() {
# shellcheck disable=SC2207 # Yes, split by whitespace
TABLE_NAMES=($(sqlite3 "$DB_PATH" .tables))
for TABLE_NAME in "${DB_IGNORED_TABLES[@]}"; do # Sanity check
if ! printf '%s\0' "${DB_IGNORED_TABLES[@]}" | grep -Fxqz -- "$TABLE_NAME"; then
error "$TABLE_NAME does not exist in the DB $DB_PATH"
fi
done
backup_private_data
for TABLE_NAME in "${TABLE_NAMES[@]}"; do
if ! printf '%s\0' "${DB_IGNORED_TABLES[@]}" | grep -Fxqz -- "$TABLE_NAME"; then
run_command sqlite3 "$DB_PATH" ".output $DB_DUMP_DIR_PATH/$TABLE_NAME.sql" ".dump $TABLE_NAME"
fi
done
}
function error() {
local message="$1"
echo "ERROR: $message" >&2
echo ''
usage
exit 1
}
function usage() {
cat << EOF
Usage:
$0 ACTION [arguments ...]
Available actions:
setup setup and/or fix required filesystem items and/or settings
docker-up "ups" the Docker Compose project
docker-down "downs" the Docker Compose project
yep execute 'yarn encore production'
composer run Composer
console run Symfony console command
cc clear cache
pu run PHPUnit tests
pus run PHPUnit tests, "small" group
pum run PHPUnit tests, "medium" group
pul run PHPUnit tests, "large" group
pcf run PHP CS Fixer
ps run PHPStan
rector run Rector
EOF
}
function action() {
[[ $# -ge 1 ]] || error 'Not enough arguments'
local action="$1"
shift
case $action in
'setup') action_run_setup ;;
'docker-up') run_docker_compose up --detach --build ;;
'docker-down') run_docker_compose down ;;
'yep') yarn encore production ;;
'composer') run_composer "$@" ;;
'console') run_console "$@" ;;
# FIXME: https://github.com/veelkoov/fuzzrake/issues/168
'cc') run_command sudo rm -rf ./var/cache/* ;;
'cc-prod') run_command ssh getfursu.it sudo rm -rf /var/www/prod/var/cache/prod ;;
'pu') run_docker_compose_exec ./bin/phpunit --testdox "$@" ;;
'pus') action pu --group small "$@" ;;
'pum') action pu --group medium "$@" ;;
'pul') action pu --group large "$@" ;;
'pcf') run_docker_compose_exec ./vendor/bin/php-cs-fixer fix "$@" ;;
'ps') run_docker_compose_exec ./vendor/bin/phpstan analyse -c phpstan.neon "$@" ;;
'rector') run_docker_compose_exec ./vendor/bin/rector process "$@" ;;
'cst') run_console app:status-tracker:run "$@" ;;
'cstc') action cst --commit "$@" ;;
'cstr') action cst --refetch "$@" ;;
'tidy') run_console app:data:tidy "$@" ;;
'tidyc') action tidy --commit "$@" ;;
'dbcommit') action_dbcommit ;;
'dbpull') action_dbpull ;;
'dbpush') action_dbpush ;;
'dbdump') action_dbdump ;;
'release-beta') action_release_beta ;;
'release-prod') action_release_prod ;;
'get-snapshots') action_get_snapshots ;;
'get-submissions') action_get_submissions ;;
*) error "Unknown action: '$action'" ;;
esac
}
pushd "$(dirname "$(realpath "$0")")"
action "$@"