-
Notifications
You must be signed in to change notification settings - Fork 0
/
qa.sh
248 lines (204 loc) · 8.71 KB
/
qa.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
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
241
242
243
244
245
246
247
248
#!/usr/bin/env bash
################################################################################
# maintainer: https://github.com/zeetabit #
################################################################################
# Branch will be used as initial.
primaryBranch='development'
command="${1: -none}"
command="${command:1}"
secondOption="${2: -noneSecondOption}"
SPRYKER_HOOK_INSTALL_DEFAULT='vendor/bin/install -r production --no-ansi -vvv'
projectYmlPath='docker/deployment/default/project.yml'
if [[ -f $projectYmlPath ]]; then
pipelineDefinitionPrefix="pipeline: "
pipelineDefinitionPrefixReplace=""
SPRYKER_HOOK_INSTALL_DEFAULT=$(grep "$pipelineDefinitionPrefix" $projectYmlPath)
SPRYKER_HOOK_INSTALL_DEFAULT="${SPRYKER_HOOK_INSTALL_DEFAULT/${pipelineDefinitionPrefix}/${pipelineDefinitionPrefixReplace}}"
SPRYKER_HOOK_INSTALL_DEFAULT="vendor/bin/install -r $SPRYKER_HOOK_INSTALL_DEFAULT --no-ansi -vvv"
fi
SPRYKER_HOOK_INSTALL="${SPRYKER_HOOK_INSTALL:=$SPRYKER_HOOK_INSTALL_DEFAULT}"
RED='\033[0;31m'
NC='\033[0m' # No Color
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
function ctrl_c() {
echo 'I see you pushed ctrl+c, exit...'
exit 1
}
echo "WELCOME to the QA utility."
################################################################################
# Helpers #
################################################################################
Help() {
cat << EOL
QA tool, helps with testing at docker & development process.
Syntax: bash $(dirname "$0")/$(basename "$0") [-i|u|s|r]
options:
i Init project from scratch. If project exists - will delete everything.
Create a snapshot.
Works only for initial branches, for now it: $primaryBranch.
u Update initial branch state with latest code.
Apply normal deploy.
Create a snapshot.
s Switch to tested branch and initiate deploy. Works only when init is done.
Changes branch.
Performs cache cleanup, regenerates dynamic resources.
Apply normal deploy.
r Rollback to initial branch: $primaryBranch.
Switch back to '$primaryBranch' branch.
Restores data snapshot to containers.
Performs cache cleanup, regenerates dynamic resources.
examples:
Initial setup or project reset:
bash qa.sh -i
Update initial setup with latest initial branch [$primaryBranch]:
bash qa.sh -u
Test some branch
bash qa.sh -s <branch_name>
or if need to test current branch
bash qa.sh -s
Move back to initial branch
bash qa.sh -r
EOL
}
declare -A availableModes=( [init]=init [i]=init [update]=update [u]=update [switch]=switch [s]=switch [rollback]=rollback [r]=rollback )
################################################################################
# Main program #
################################################################################
directory=${PWD}
if [[ ! -v availableModes["$command"] ]]; then Help; exit 1; fi
command="${availableModes[$command]}"
printf "Current directory is %s \n" "$directory";
printf "Command '%s' \n" "$command"
echo "oooooooooooooo> start <oooooooooooooo"
dumperDirPath="data/dumps";
dumperPath=$dumperDirPath/dumper.bash
dumperDistPath='./tests/dump_docker_data_to_logs_in_pipeline.sh'
[ ! -f $dumperDistPath ] && echo "[ERROR] dumper dist file is not exists, download from github through wget...." && wget https://raw.githubusercontent.com/zeetabit/dumper/main/dumper.bash -O $dumperDistPath;
[ ! -f $dumperPath ] && echo "[WARNING] dumper is not exists, copying initial one... " && cp $dumperDistPath $dumperPath;
warmup() {
printf "Warmup ... \n"
local currentBranch
currentBranch="$(git branch --show-current)"
echo -e "${RED}===]>${NC} composer install --prefer-dist"
docker/sdk cli "composer install --prefer-dist"
local countFiles
countFiles=$(git diff --name-status "$primaryBranch" | grep "src/" | grep "D \|A " | wc -l | tr -d '[:blank:]')
countFiles=$(( $countFiles+0 ))
if [ "$countFiles" -gt "0" ] || [ "$currentBranch" == "$primaryBranch" ] || [ "$secondOption" == "-f" ]; then
echo -e "${RED}===]>${NC} console cache:class-resolver:build"
docker/sdk cli "console cache:class-resolver:build"
fi
echo -e "${RED}===]>${NC} console transfer:g && console transfer:d:g && console transfer:e:g"
docker/sdk cli "console transfer:g && console transfer:d:g && console transfer:e:g"
echo -e "${RED}===]>${NC} console cache:empty-all"
docker/sdk cli "console cache:empty-all"
echo -e "${RED}===]>${NC} $SPRYKER_HOOK_INSTALL"
SPRYKER_HOOK_INSTALL=$SPRYKER_HOOK_INSTALL docker/sdk cli "$SPRYKER_HOOK_INSTALL"
echo -e "${RED}===]>${NC} console q:w:s [3 workers]... wait"
docker/sdk cli "console q:w:s& console q:w:s& console q:w:s& wait"
}
getCurrentSnapshot () {
local currentBranch
currentBranch="$(git branch --show-current)"
currentBranch="${currentBranch////-}"
echo "$currentBranch"
}
init () {
printf "Starting init...\n"
local currentBranch
currentBranch="$(git branch --show-current)"
local deployPath=docker/deployment/default/deploy
[ $currentBranch != "$primaryBranch" ] && [ "$secondOption" != "-f" ] && echo "[ERROR] I can init project only under primaryBranch: $primaryBranch. To force please add '-f'." && exit 1;
[ ! -f $deployPath ] && echo "$deployPath does not exist. Did you boot project before by 'docker/sdk boot'?" && exit 1;
echo "try to stop instance... wait"
docker/sdk stop &>> /dev/null
start=`date +%s`
git clean -fdX -e \!.idea -e \!qa.sh -e \!data/dumps -e \!.npm -e \!vendor
git clean -fdx -e .idea -e qa.sh -e data/dumps -e .npm -e vendor
git stash
git reset --hard HEAD
took=$((`date +%s`-$start))
echo "took $took sec"
mkdir -p $dumperDirPath
[ ! -f $dumperDistPath ] && echo "[ERROR] dumper dist path not exists, download from github through wget...." && wget $DUMPER_DIST_PATH -O $dumperDistPath;
cp -R "$dumperDistPath" "$dumperPath"
start=`date +%s`
echo -e "${RED}===]>${NC} docker/sdk clean-data"
docker/sdk clean-data
took=$((`date +%s`-$start))
echo "took $took sec"
start=`date +%s`
echo -e "${RED}===]>${NC} docker/sdk up"
docker/sdk up
took=$((`date +%s`-$start))
echo "took $took sec"
start=`date +%s`
echo -e "${RED}===]>${NC} console q:w:s [3 workers]... wait"
docker/sdk cli "console q:w:s& console q:w:s& console q:w:s& wait"
took=$((`date +%s`-$start))
echo "took $took sec"
snapshot;
}
snapshot () {
local currentSnapshot
currentSnapshot="$(getCurrentSnapshot)"
echo "make snapshot $currentSnapshot"
echo -e "${RED}===]>${NC} bash $dumperDirPath/dumper.bash -m export -t $currentSnapshot"
mkdir -p "$dumperDirPath/$currentSnapshot"
rm -f $dumperDirPath/"$currentSnapshot"-mysql.sql.gz
bash $dumperDirPath/dumper.bash -m export -t "$currentSnapshot"
}
snapshotRestore () {
local currentSnapshot
currentSnapshot="$(git branch --show-current)"
echo "restore from snapshot $currentSnapshot"
echo -e "${RED}===]>${NC} bash $dumperDirPath/dumper.bash -m import -t $currentSnapshot"
bash $dumperDirPath/dumper.bash -m import -t "$currentSnapshot"
}
if [ "$command" == "init" ];
then
init;
elif [ "$command" == "update" ];
then
currentBranch="$(git branch --show-current)"
[ $currentBranch != "$primaryBranch" ] && [ "$secondOption" != "-f" ] && echo "[ERROR] I can update project only under primaryBranch: $primaryBranch. Please use additional '-f' to force." && exit 1;
echo -e "${RED}===]>${NC} git pull -f"
git pull -f
warmup;
snapshot;
elif [ "$command" == "switch" ];
then
currentBranch="$(git branch --show-current)"
expectedBranch=$secondOption
if [ "$expectedBranch" == "noneSecondOption" ] || [ "$expectedBranch" == "" ];
then
expectedBranch=$currentBranch;
fi;
if [ "$expectedBranch" != "$currentBranch" ];
then
echo -e "${RED}===]>${NC} git checkout \"${expectedBranch}\" -f"
git checkout "$expectedBranch" -f
fi;
echo -e "${RED}===]>${NC} git pull -f"
git pull -f
warmup;
elif [ "$command" == "rollback" ];
then
currentBranch="$(git branch --show-current)"
expectedBranch=$primaryBranch
if [ "$expectedBranch" != "$currentBranch" ];
then
echo -e "${RED}===]>${NC} git checkout \"${expectedBranch}\" -f"
git checkout "$expectedBranch" -f
fi;
snapshotRestore;
echo -e "${RED}===]>${NC} git pull -f"
git pull -f
warmup;
fi
echo "oooooooooooooo> end <oooooooooooooo"
echo "directory: ${directory}"
echo "dumps directory: ${dumperDirPath}"
echo
echo "See you!!"