-
Notifications
You must be signed in to change notification settings - Fork 1
/
check
executable file
·226 lines (196 loc) · 7.02 KB
/
check
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
#!/bin/bash
set -e
set -o pipefail
# 0 - stdin
# 1 - stdout
# 2 - stderr
# 3 - for script output
exec 3>&1
exec 1>&2 # send stdout to stderr for logging
echo "check starts..."
script_folder=`dirname "$0"`
source $script_folder/tools/common.sh
source $script_folder/tools/semver.sh
# inputs
request="$TMPDIR/semver-config-request"
cat > "${request}" <&0
destination="$1"
cd $destination
# source items
DRIVER="$(jq -r '.source.driver // empty' < "${request}")"
URI="$(jq -r '.source.uri // empty' < "${request}")"
BRANCH="$(jq -r '.source.branch // "master"' < "${request}")"
PRIVATE_KEY="$(jq -r '.source.private_key // empty' < "${request}")"
USERNAME="$(jq -r '.source.username // empty' < "${request}")"
PASSWORD="$(jq -r '.source.password // empty' < "${request}")"
CONFIG_FILE="$(jq -r '.source.config_file // empty' < "${request}")"
CONFIG_PATH="$(jq -r '.source.config_path // empty' < "${request}")"
INITIAL_VERSION="$(jq -r '.source.initial_version // "0.0.0"' < "${request}")"
VERSION_PATH="$(jq -r '.source.version_path // empty' < "${request}")"
VERSION_PATTERN="$(jq -r '.source.version_pattern // empty' < "${request}")"
# current semver while continously checking
REQUESTING_VERSION="$(jq -r '.version.semver // empty' < "${request}")"
# verify inputs
echo "--------------------"
echo "driver: $DRIVER"
echo "uri: $URI"
echo "branch: $BRANCH"
if [ -z "$PRIVATE_KEY" ]; then
echo "private_key: "
else
echo "private_key: ****"
fi
if [ -z "$USERNAME" ]; then
echo "username: "
else
echo "username: ****"
fi
if [ -z "$PASSWORD" ]; then
echo "password: "
else
echo "password: ****"
fi
echo "config_file: $CONFIG_FILE"
echo "initial_version: $INITIAL_VERSION"
echo "config_path: $CONFIG_PATH"
echo "version_path: $VERSION_PATH"
echo "version_pattern: $VERSION_PATTERN"
echo "requesting version: $REQUESTING_VERSION"
echo "--------------------"
msg=$(verify "driver" "$DRIVER" "git")
msg+=$(verify "uri" "$URI")
msg+=$(verify "config_file" "$CONFIG_FILE")
msg+=$(verify "config_path" "$CONFIG_PATH")
msg+=$(verify "version_path" "$VERSION_PATH")
msg+=$(verify_version_pattern "$VERSION_PATTERN")
if [ ! -z "$msg" ]; then
echo -e $msg
exit 1
fi
# setup auth
# git private key, if any
if [ ! -z "$PRIVATE_KEY" ]; then
git_private_key="$TMPDIR/git_private_key"
echo "${PRIVATE_KEY}" > $git_private_key
#export GIT_SSH_COMMAND="ssh -o StrictHostKeyChecking=no -i \"${git_private_key}\""
setup_ssh_key $git_private_key false
echo "git private key is set"
fi
# username/password
if [ ! -z "$USERNAME" ] && [ ! -z "$PASSWORD" ]; then
cat > ~/.netrc <<EOF
default login ${USERNAME} password ${PASSWORD}
EOF
chmod 0600 ~/.netrc
echo "username/password is set"
fi
# setup repo
REPO_DIR="${TMPDIR}/semver-config-git-repo"
if [ -d "${REPO_DIR}" ]; then
echo "pushd "${REPO_DIR}" && git fetch origin "${BRANCH}" && popd"
pushd "${REPO_DIR}" && git fetch origin "${BRANCH}" && popd
else
echo git clone "${URI}" --branch "${BRANCH}" --depth 1 "${REPO_DIR}"
git clone "${URI}" --branch "${BRANCH}" --depth 1 "${REPO_DIR}"
fi
pushd "${REPO_DIR}" && git reset --hard origin/"${BRANCH}" && popd
# process version & config
config_file="${REPO_DIR}/${CONFIG_FILE}"
if [ ! -f "${config_file}" ]; then
echo "config_file doesn't exist: ${config_file}"
exit 2;
fi
echo "yq r "${config_file}" "${VERSION_PATH}" "
current_version="$(yq r "${config_file}" "${VERSION_PATH}")"
echo "current version: ${current_version}"
if [[ $current_version == "null" ]]; then
echo "version is invalid or version_path is wrong"
exit 3;
fi
# result sample
# [
# { "semver": "1.0.1", "hash": "f60924937312a53779becb6fa634b3e0" }
# ]
first_check=""
md5_cmd="md5sum"
if [[ $(uname -s) == "Darwin" ]]; then
md5_cmd="md5"
fi
previous_config_hash=""
current_config_hash="`echo "$( yq r $config_file $CONFIG_PATH )" | $md5_cmd | awk '{print $1}'`"
version_string=""
config_has_changed=""
echo "current config hash: ${current_config_hash}"
if [[ -z "$REQUESTING_VERSION" || "$REQUESTING_VERSION" == "null" ]] && [[ "$INITIAL_VERSION" == "0.0.0" ]]; then
# first check with no initial_version specified
first_check="true"
else
# use the initial_version as base by default
base_version=$INITIAL_VERSION
if ! [[ -z "$REQUESTING_VERSION" || "$REQUESTING_VERSION" == "null" ]]; then
# use request_version as base instead if any
base_version="$REQUESTING_VERSION"
fi
# if version pattern is "-", which means "please ignore me as a semantic version"
if [[ $VERSION_PATTERN == "-" ]]; then
# check wether the version, as a string, changed
if [[ ! "$base_version" == "$current_version" ]]; then
version_string="true"
echo "version_string=true"
else
# check the hash if needed
if [ -f $TMPDIR/semver-config-hash ]; then
previous_config_hash="`cat $TMPDIR/semver-config-hash`"
fi
echo "previous config hash: ${previous_config_hash}"
if [[ ! "$current_config_hash" == "$previous_config_hash" ]]; then
config_has_changed="true"
echo "config_has_changed=true"
fi
fi
else
major=""
if [[ $VERSION_PATTERN == *"m."* ]]; then
major="$( semverDetectMajorChange "${base_version}" "${current_version}" )"
echo "semverDetectMajorChange "${base_version}" "${current_version}": $major "
fi
minor=""
if [[ $VERSION_PATTERN == *".n."* ]]; then
minor="$( semverDetectMinorChange "${base_version}" "${current_version}" )"
echo "semverDetectMinorChange "${base_version}" "${current_version}": $minor "
fi
patch=""
if [[ $VERSION_PATTERN == *".p"* ]]; then
patch="$( semverDetectPatchChange "${base_version}" "${current_version}" )"
echo "semverDetectPatchChange "${base_version}" "${current_version}": $patch "
fi
# only when same versions need to check config change
if [[ "${base_version}" == "${current_version}" ]]; then
if [ -f $TMPDIR/semver-config-hash ]; then
previous_config_hash="`cat $TMPDIR/semver-config-hash`"
fi
echo "previous config hash: ${previous_config_hash}"
if [[ ! "$current_config_hash" == "$previous_config_hash" ]]; then
config_has_changed="true"
echo "config_has_changed=true"
fi
fi
fi
fi
echo "$current_config_hash" > "$TMPDIR/semver-config-hash"
if [ "${first_check}" == "true" ] \
|| [ "${major}" == "true" ] \
|| [ "${minor}" == "true" ] \
|| [ "${patch}" == "true" ] \
|| [ "${version_string}" == "true" ] \
|| [ "${config_has_changed}" == "true" ]; then
# new version found
echo "new version detected: ${current_version}"
result=$(jq -n "[{semver:\"${current_version}\",hash:\"${current_config_hash}\"}]")
else
echo "no new version detected"
result=$(jq -n "[]")
fi
echo "check completed"
echo "result: ${result}"
echo "$result" | jq -s add >&3