-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.sh
executable file
·314 lines (251 loc) · 10.1 KB
/
build.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/bin/bash
# Sets the number of parallel jobs that can be used on the build process
# Recomended values: 1 job per 1GB of free RAM or 1 job per CPU physical core
CPU_CORES=${CPU_CORES:-""}
# Architectures that can be built:
# linux-x64
# linux-arm64
# macos-x64
# windows-x64
ARCHITECTURE=${ARCHITECTURE:-""}
# Get the origin URL
ORIGIN_URL=$(git config --get remote.origin.url)
# Extract the github username and repository name
if [[ $ORIGIN_URL =~ ^https://github.com/(.+)/(.+)\.git$ ]]; then
GITHUB_USER="${BASH_REMATCH[1]}"
GITHUB_REPO="${BASH_REMATCH[2]}"
elif [[ $ORIGIN_URL =~ ^git@github.com:(.+)/(.+)\.git$ ]]; then
GITHUB_USER="${BASH_REMATCH[1]}"
GITHUB_REPO="${BASH_REMATCH[2]}"
else
echo "Unable to parse origin URL: $ORIGIN_URL"
exit 1
fi
# Sets variables needed for the build
TICKER=${TICKER:-"${GITHUB_REPO}"}
UI_NAME=${UI_NAME:-"Monk"}
BASE_NAME=${BASE_NAME:-"monk"}
# Sets the build environment variable
# 0: The build will use the builder image available on docker hub
# 1: The build will use a locally build image for the builder image
BUILD=${BUILD:-"0"}
# Sets the verify environment variable
# 0: The wallet is only built but not verified
# 1: The wallet is built at the same commit as the published release on GitHub
# and then verified against the published hash files
# 2: The wallet is built at the same commit as the published release on GitHub
# and then all the files are downloaded and checked against the local ones
VERIFY=${VERIFY:-"0"}
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
BLUE="\e[34m"
RESET="\e[0m"
echo -e "${BLUE}
██████╗ ███████╗██╗ ██╗
██╔══██╗██╔════╝██║ ██║
██║ ██║███████╗██║ █╗ ██║
██║ ██║╚════██║██║███╗██║
██████╔╝███████║╚███╔███╔╝
╚═════╝ ╚══════╝ ╚══╝╚══╝
DECENOMY Standard Wallet
Deterministic build script
${RESET}"
error() {
echo -e "${RED}
███████╗██████╗ ██████╗ ██████╗ ██████╗
██╔════╝██╔══██╗██╔══██╗██╔═══██╗██╔══██╗
█████╗ ██████╔╝██████╔╝██║ ██║██████╔╝
██╔══╝ ██╔══██╗██╔══██╗██║ ██║██╔══██╗
███████╗██║ ██║██║ ██║╚██████╔╝██║ ██║
╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝
${YELLOW}$1
${RESET}"
exit 1
}
info() {
echo -e "${GREEN}$1${RESET}"
}
warn() {
echo -e "${YELLOW}$1${RESET}"
}
trace() {
echo -e "${BLUE}$1${RESET}"
}
# Array of commands to test
commands=(
"docker" "wget" "curl" "jq" "lscpu" "grep"
"awk" "sha256sum" "unzip" "basename" "free"
)
# Loop through the array and check each command
for cmd in "${commands[@]}"
do
if ! command -v $cmd &> /dev/null
then
error "${cmd} is not installed, please install before running again."
fi
done
# Check if docker buildx is installed
docker buildx version &> /dev/null
if [ $? -ne 0 ]; then
error "Docker Buildx is not installed, please install before running again."
fi
# Check if the CPU_CORES environment variable needs to be calculated
if [ "$CPU_CORES" = "" ]; then
# Getting the number of sockets
sockets=$(lscpu | grep "Socket(s):" | awk '{print $2}')
# Getting the number of cores per socket
cores_per_socket=$(lscpu | grep "Core(s) per socket:" | awk '{print $4}')
# Calculating total physical cores
total_physical_cores=$((sockets * cores_per_socket))
# Getting the free RAM in gigabytes
free_ram_gb=$(free --giga | awk '/Mem:/ {print $7}')
trace "
Total sockets (physical CPUs): $sockets
Cores per socket: $cores_per_socket
Total physical cores: $total_physical_cores
Free ram (GB): $free_ram_gb"
# Calculating the default number of CPUs to use
CPU_CORES=$(
if [ $total_physical_cores -le $free_ram_gb ]; then
echo "$total_physical_cores"
else
echo "$free_ram_gb"
fi
)
fi
info "Using $CPU_CORES cores"
# Check if the ARCHITECTURE environment variable needs to be chosen
if [ "$ARCHITECTURE" = "" ]; then
warn "Select an architecture from the list:"
# List of architectures
options=("linux-x64" "linux-arm64" "macos-x64" "windows-x64")
# Use select to present a menu and store the selection in a variable
select opt in "${options[@]}"
do
case $opt in
"linux-x64"|"linux-arm64"|"macos-x64"|"windows-x64")
break
;;
*)
error "Invalid option $REPLY"
;;
esac
done
# Store the selection in a variable
ARCHITECTURE=$opt
fi
info "Selected architecture: $ARCHITECTURE"
WALLET_DOCKER_FILE="./contrib/docker/Dockerfile.dsw-$ARCHITECTURE-wallet"
cp $WALLET_DOCKER_FILE $WALLET_DOCKER_FILE.tmp
# Check if the FULL_BUILD environment variable is already set
if [ "$BUILD" = "1" ]; then
# The value of FULL_BUILD is 1, proceed with Docker build for the builder
docker buildx build \
--no-cache \
--build-arg CPU_CORES=$CPU_CORES \
-t decenomy/dsw-$ARCHITECTURE-builder \
-f ./contrib/docker/Dockerfile.dsw-$ARCHITECTURE-builder .
if [ $? -ne 0 ]; then
error "Error: Docker build failed."
fi
sed -i "s|^FROM decenomy/dsw-$ARCHITECTURE-builder.*|FROM decenomy/dsw-$ARCHITECTURE-builder:latest|" "$WALLET_DOCKER_FILE.tmp"
fi
if [ "$VERIFY" = "0" ]; then
TARGET=$(git branch --show-current)
fi
if [ "$VERIFY" -ge 1 ]; then
# Fetch the latest release data using GitHub API
response=$(curl -s "https://api.github.com/repos/$GITHUB_USER/$GITHUB_REPO/releases/latest")
# Parse JSON response to get the browser_download_url of the asset
PACKAGE_URL=$(echo "$response" | jq -r ".assets[] | select(.name | ascii_downcase | test(\"$ARCHITECTURE.zip\")) | .browser_download_url")
# Check if download_url is empty or null
if [ -z "$PACKAGE_URL" ] || [ "$PACKAGE_URL" == "null" ]; then
error "No matching asset found."
fi
trace "Download URL for the package asset is: $PACKAGE_URL"
# Parse JSON response to get the browser_download_url of the hash file asset
PACKAGE_SHA256_URL=$(echo "$response" | jq -r ".assets[] | select(.name | ascii_downcase | test(\"$ARCHITECTURE.asc\")) | .browser_download_url")
# Check if download_url is empty or null
if [ -z "$PACKAGE_SHA256_URL" ] || [ "$PACKAGE_SHA256_URL" == "null" ]; then
error "No matching asset found."
fi
trace "Download URL for the hash file asset is: $PACKAGE_SHA256_URL"
sha256_file_contents=$(curl -sL "$PACKAGE_SHA256_URL")
# Check if the download was successful
if [ -z "$sha256_file_contents" ]; then
error "Failed to download the file or the file is empty."
fi
TARGET=$(echo "$sha256_file_contents" | grep "commit" | awk '{print $1}')
# Check if the target was successfully obtained
if [ -z "$TARGET" ]; then
error "Failed to get the target or the target is empty."
fi
fi
image_tag=${TICKER,,}-${UI_NAME,,}-${TARGET,,}
docker buildx build \
--no-cache \
--build-arg CPU_CORES=$CPU_CORES \
--build-arg TICKER=$TICKER \
--build-arg NAME=$UI_NAME \
--build-arg BASE_NAME=$BASE_NAME \
--build-arg TARGET=$TARGET \
--build-arg GITHUB_USER=$GITHUB_USER \
-f $WALLET_DOCKER_FILE.tmp \
-t $image_tag \
.
# Check if docker build was successful
if [ $? -ne 0 ]; then
error "Error: Docker build failed."
fi
info "Successful build"
# Create a temporary container from the image
container_id=$(docker create $image_tag)
trace "Container ID: $container_id"
# Copy files from the container to the current directory
mkdir -p deploy
rm -rf deploy/$ARCHITECTURE
docker cp "$container_id":/${GITHUB_USER}/${TICKER}/deploy/. ./deploy/
# Main verification process
if [ "$VERIFY" -ge 1 ]; then
files_to_check=("${ARCHITECTURE}.zip" "${BASE_NAME}d" "${BASE_NAME}-cli" "${BASE_NAME}-tx" "${BASE_NAME}-qt")
for file_to_check in "${files_to_check[@]}"; do
remote_hash="$(echo "$sha256_file_contents" | grep -i "${file_to_check}" | awk '{print $1}')"
local_hash="$(sha256sum "$(find "./deploy/$ARCHITECTURE" -type f | grep -i "${file_to_check}")" | awk '{print $1}')"
if [ "$remote_hash" != "$local_hash" ]; then
error "${file_to_check^} file hashes don't match"
fi
done
if [ "$VERIFY" -ge 2 ]; then
rm -rf "deploy/$ARCHITECTURE/download"
mkdir -p "deploy/$ARCHITECTURE/download"
cd "deploy/$ARCHITECTURE/download"
wget "$PACKAGE_URL"
filename=$(basename "$PACKAGE_URL")
unzip "$filename"
for file_to_check in "${files_to_check[@]}"; do
filename="$(find "./" -type f | grep -i "$file_to_check" | xargs -n1 basename)"
download_hash="$(sha256sum "./$filename" | awk '{print $1}')"
local_hash="$(sha256sum "../$filename" | awk '{print $1}')"
if [ "$download_hash" != "$local_hash" ]; then
error "Download $file_to_check file hashes don't match"
fi
done
cd ../../..
fi
info "Binaries verified successfully"
fi
info "
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⣦⣤⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⣿⣿⣿⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⢠⣤⣤⣤⡀⠀⢀⣼⣿⣿⣿⣿⣿⣿⣧⣤⣤⣤⣤⣤⣤⣤⣤⣄⡀⠀
⢸⣿⣿⣿⡇⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡄
⢸⣿⣿⣿⡇⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇
⢸⣿⣿⣿⡇⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠀
⢸⣿⣿⣿⡇⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠁⠀
⢸⣿⣿⣿⡇⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠁⠀⠀
⢸⣿⣿⣿⡇⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠀⠀⠀
⢸⣿⣿⣿⡇⠀⠈⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠀⠀⠀⠀
"