-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathfablo.sh
More file actions
executable file
·425 lines (337 loc) · 13.8 KB
/
fablo.sh
File metadata and controls
executable file
·425 lines (337 loc) · 13.8 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/usr/bin/env bash
set -e
FABLO_VERSION=2.5.0
FABLO_IMAGE_NAME="ghcr.io/fablo-io/fablo"
FABLO_IMAGE="$FABLO_IMAGE_NAME:$FABLO_VERSION"
COMMAND="$1"
COMMAND_CALL_ROOT="$(pwd)"
FABLO_TARGET="$COMMAND_CALL_ROOT/fablo-target"
# Create temporary directory and remove it after script execution
FABLO_TEMP_DIR="$(mktemp -d -t fablo.XXXXXXXX)"
# shellcheck disable=SC2064
trap "rm -rf \"$FABLO_TEMP_DIR\"" EXIT
getDefaultFabloConfig() {
local fablo_config_json="$COMMAND_CALL_ROOT/fablo-config.json"
local fablo_config_yaml="$COMMAND_CALL_ROOT/fablo-config.yaml"
if [ -f "$fablo_config_json" ]; then
echo "$fablo_config_json"
elif [ -f "$fablo_config_yaml" ]; then
echo "$fablo_config_yaml"
else
echo "$fablo_config_json"
fi
}
snapshotFabloConfigToTarget() {
local fablo_config="$1"
local fablo_target="$2"
if [ -z "$fablo_config" ] || [ ! -f "$fablo_config" ]; then
return
fi
mkdir -p "$fablo_target"
local ext="${fablo_config##*.}"
local preserved_name="fablo-config.$ext"
cp -f "$fablo_config" "$fablo_target/$preserved_name"
}
getStoredConfigSnapshotPath() {
if [ -f "$FABLO_TARGET/fablo-config.json" ]; then
echo "$FABLO_TARGET/fablo-config.json"
elif [ -f "$FABLO_TARGET/fablo-config.yaml" ]; then
echo "$FABLO_TARGET/fablo-config.yaml"
elif [ -f "$FABLO_TARGET/fablo-config.yml" ]; then
echo "$FABLO_TARGET/fablo-config.yml"
else
echo ""
fi
}
verifyConfigMatchesOrFail() {
local current_config=${1:-$(getDefaultFabloConfig)}
if [ -z "$current_config" ] || [ ! -f "$current_config" ]; then
return
fi
local stored_config
stored_config="$(getStoredConfigSnapshotPath)"
if [ -z "$stored_config" ]; then
echo "No stored config snapshot found in '$FABLO_TARGET'. Storing current config snapshot for future comparisons."
local ext="${current_config##*.}"
if [ "$ext" = "json" ] || [ "$ext" = "yaml" ] || [ "$ext" = "yml" ] ; then
snapshotFabloConfigToTarget "$current_config" "$FABLO_TARGET"
else
echo "Error: Unsupported config file extension '.$ext'. Supported extensions are: .json, .yaml"
exit 1
fi
return
fi
if cmp -s "$stored_config" "$current_config"; then
return
fi
echo "Error: Fablo configuration has changed since the network at '$FABLO_TARGET' was generated."
echo "Stored config: $stored_config"
echo "Current config: $current_config"
echo ""
echo "--- Diff (stored vs current) ---"
# Show unified diff
diff -u "$stored_config" "$current_config" || true
echo "--------------------------------"
echo ""
echo "Please run 'fablo prune' to remove the existing network before applying configuration changes,"
echo "or run 'fablo recreate [path/to/fablo-config]' to prune and start with the new configuration."
exit 1
}
getSnapshotPath() {
path="${1:-'snapshot'}"
if echo "$path" | grep -q "tar.gz$"; then
echo "$path"
else
echo "$path.fablo.tar.gz"
fi
}
printSplash() {
darkGray=$'\e[90m'
end=$'\e[0m'
echo ""
if [ "$(date +%m%d)" -gt "1215" ]; then
echo "┌────── * ┌─────. ╷ .────."
echo "│ _/_\_ │ │ │ ╱ ╲ "
echo "├───── _/___\_ ├─────: │ │ │"
echo "│ _/_____\_ │ │ │ ╲ ╱ "
printf "╵ |_| └─────' └────── '────' %24s\n" "v$FABLO_VERSION"
else
echo "┌────── .─. ┌─────. ╷ .────."
echo "│ / \ │ │ │ ╱ ╲ "
echo "├───── / \ ├─────: │ │ │"
echo "│ /───────\ │ │ │ ╲ ╱ "
printf "╵ / \ └─────' └────── '────' %24s\n" "v$FABLO_VERSION"
fi
echo "${darkGray}┌┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┐"
echo "│ https://fablo.io | created at SoftwareMill | backed by Hyperledger Foundation│"
echo "└┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┘${end}"
}
printHelp() {
printSplash
echo "Usage:
fablo init [node] [rest] [dev] [gateway]
Creates simple Fablo config in current directory with optional Node.js, chaincode and REST API and dev mode.
fablo generate [/path/to/fablo-config.json|yaml [/path/to/fablo/target]]
Generates network configuration files in the given directory. Default config file path is '\$(pwd)/fablo-config.json' or '\$(pwd)/fablo-config.yaml', default (and recommended) directory '\$(pwd)/fablo-target'.
fablo up [/path/to/fablo-config.json|yaml]
Starts the Hyperledger Fabric network for given Fablo configuration file, creates channels, installs and instantiates chaincodes. If there is no configuration, it will call 'generate' command for given config file.
fablo <down | start | stop>
Downs, starts or stops the Hyperledger Fabric network for configuration in the current directory. This is similar to down, start and stop commands for Docker Compose.
fablo reset
Downs and ups the network. Network state is lost, but the configuration is kept intact.
fablo prune
Downs the network and removes all generated files.
fablo recreate [/path/to/fablo-config.json|yaml]
Prunes and ups the network. Default config file path is '\$(pwd)/fablo-config.json' or '\$(pwd)/fablo-config.yaml'.
fablo chaincodes install
Installs all chaincodes on relevant peers. Chaincode directory is specified in Fablo config file.
fablo chaincode install <chaincode-name> <version>
Installs chaincode on all relevant peers. Chaincode directory is specified in Fablo config file.
fablo chaincode upgrade <chaincode-name> <version>
Upgrades chaincode on all relevant peers. Chaincode directory is specified in Fablo config file.
fablo chaincode invoke <channel_name> <chaincode_name> <peers_domains_comma_separated> <command> <transient>
Invokes chaincode with specified parameters.
fablo chaincodes list <peer> <channel>
Lists chaincodes installed on specified peer and channel.
fablo chaincode query <channel_name> <chaincode_name> <peers_domains_comma_separated> <command> <transient>
Queries chaincode with specified parameters.
fablo channel --help
To list available channel query options which can be executed on running network.
fablo snapshot <target-snapshot-path>
Creates a snapshot of the network in target path. The snapshot contains all network state, including transactions and identities.
fablo restore <source-snapshot-path>
Restores the network from a snapshot.
fablo use [version]
Updates this Fablo script to specified version. Prints all versions if no version parameter is provided.
fablo <help | --help>
Prints the manual.
fablo version [--verbose | -v]
Prints current Fablo version, with optional details."
}
executeOnFabloDocker() {
local command_with_params="$1"
local fablo_workspace="${2:-$FABLO_TEMP_DIR}"
local fablo_config="$3"
# Docker requires volume paths to be at least 2 chars; resolve . to absolute path
if [ -d "$fablo_workspace" ]; then
fablo_workspace="$(cd "$fablo_workspace" && pwd)"
fi
local fablo_workspace_params=(
-v "$fablo_workspace":/network/workspace
)
# Generate minimal passwd and group files to satisfy Node.js lookup
# Avoiding mounting host files directly for security reasons.
echo "$(id -un || echo fablo):x:$(id -u):$(id -g):$(id -un || echo fablo):/network/workspace:/bin/sh" >"$FABLO_TEMP_DIR/passwd"
echo "$(id -gn || echo fablo):x:$(id -g):" >"$FABLO_TEMP_DIR/group"
fablo_workspace_params+=(
-v "$FABLO_TEMP_DIR/passwd":/etc/passwd:ro
-v "$FABLO_TEMP_DIR/group":/etc/group:ro
)
local fablo_config_params=()
if [ -n "$fablo_config" ]; then
if [ ! -f "$fablo_config" ]; then
echo "File $fablo_config does not exist"
exit 1
fi
fablo_config="$(cd "$(dirname "$fablo_config")" && pwd)/$(basename "$fablo_config")"
local chaincodes_base_dir="$(dirname "$fablo_config")"
fablo_config_params=(
-v "$fablo_config":/network/workspace/fablo-config.json
--env "FABLO_CONFIG=$fablo_config"
--env "CHAINCODES_BASE_DIR=$chaincodes_base_dir"
)
fi
docker run -i --rm \
"${fablo_workspace_params[@]}" \
"${fablo_config_params[@]}" \
-u "$(id -u):$(id -g)" \
$FABLO_IMAGE sh -c "/fablo/docker-entrypoint.sh $command_with_params" \
2>&1
}
useVersion() {
printSplash
local version="$1"
if [ -n "$version" ]; then
echo "Updating '$0' to version $version..."
set +e
curl -Lf https://github.com/hyperledger-labs/fablo/releases/download/"$version"/fablo.sh -o "$0" && chmod +x "$0"
else
executeOnFabloDocker "list-versions"
fi
}
initConfig() {
printSplash
executeOnFabloDocker "init \"$1\" \"$2\" \"$3\" \"$4\" \"$5\""
cp -R -i "$FABLO_TEMP_DIR/." "$COMMAND_CALL_ROOT/"
}
validateConfig() {
local fablo_config=${1:-$(getDefaultFabloConfig)}
executeOnFabloDocker "validate" "" "$fablo_config"
}
extendConfig() {
local fablo_config=${1:-$(getDefaultFabloConfig)}
executeOnFabloDocker "extend-config" "" "$fablo_config"
}
exportNetworkTopology() {
local fablo_config=${1:-$(getDefaultFabloConfig)}
local output_file=${2:-network-topology.mmd}
executeOnFabloDocker "export-network-topology /network/workspace/fablo-config.json $output_file" "$(dirname "$output_file")" "$fablo_config"
}
generateNetworkConfig() {
printSplash
local fablo_config=${1:-$(getDefaultFabloConfig)}
local fablo_target=${2:-$FABLO_TARGET}
echo "Generating network config"
echo " FABLO_VERSION: $FABLO_VERSION"
echo " FABLO_CONFIG: $fablo_config"
echo " FABLO_NETWORK_ROOT: $fablo_target"
mkdir -p "$fablo_target"
executeOnFabloDocker "setup-network" "$fablo_target" "$fablo_config"
if [ -f "$fablo_target/hooks/post-generate.sh" ]; then
chmod +x "$fablo_target/hooks/post-generate.sh" || true
("$fablo_target/hooks/post-generate.sh")
fi
snapshotFabloConfigToTarget "$fablo_config" "$fablo_target"
}
networkPrune() {
if [ -f "$FABLO_TARGET/fabric-docker.sh" ]; then
"$FABLO_TARGET/fabric-docker.sh" down
fi
if [ -f "$FABLO_TARGET/fabric-k8s.sh" ]; then
"$FABLO_TARGET/fabric-k8s.sh" down
fi
echo "Removing $FABLO_TARGET"
rm -rf "$FABLO_TARGET"
}
networkUp() {
local fablo_config=${1:-$(getDefaultFabloConfig)}
if [ ! -d "$FABLO_TARGET" ] || [ -z "$(ls -A "$FABLO_TARGET")" ]; then
echo "Network target directory is empty"
generateNetworkConfig "$fablo_config"
else
verifyConfigMatchesOrFail "$fablo_config"
fi
executeFabloCommand up
}
executeFabloCommand() {
if [ ! -d "$FABLO_TARGET" ]; then
echo "Error: This command needs the network to be generated at '$FABLO_TARGET'! Execute 'generate' or 'up' command."
exit 1
fi
if [ -f "$FABLO_TARGET/fabric-docker.sh" ]; then
echo "Executing Fablo Docker command: $1"
chmod +x "$FABLO_TARGET/fabric-docker.sh" || true
"$FABLO_TARGET/fabric-docker.sh" "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8"
elif [ -f "$FABLO_TARGET/fabric-k8s.sh" ]; then
echo "Executing Fablo Kubernetes command: $1"
"$FABLO_TARGET/fabric-k8s.sh" "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8"
else
echo "Error: Corrupted Fablo target directory ($FABLO_TARGET)"
echo "Cannot execute command $1"
exit 1
fi
# Execute post-start hook after network is started
if [ "$1" = "up" ] || [ "$1" = "start" ]; then
if [ -f "$FABLO_TARGET/hooks/post-start.sh" ]; then
chmod +x "$FABLO_TARGET/hooks/post-start.sh" || true
("$FABLO_TARGET/hooks/post-start.sh")
fi
fi
}
createSnapshot() {
archive="$(getSnapshotPath "$1")"
echo "Creating network snapshot in '$archive'"
if [ -f "$archive" ]; then
echo "Error: Snapshot file '$archive' already exists!"
exit 1
fi
executeFabloCommand snapshot "$FABLO_TEMP_DIR"
(cd "$FABLO_TEMP_DIR" && tar czf tmp.tar.gz *)
mv "$FABLO_TEMP_DIR/tmp.tar.gz" "$archive"
echo "📦 Created snapshot at '$archive'!"
}
restoreSnapshot() {
archive="$(getSnapshotPath "$1")"
hook_command="$2"
echo "📦 Restoring network from '$archive'"
if [ ! -f "$archive" ]; then
echo "Fablo snapshot file '$archive' does not exist!"
exit 1
fi
tar -xf "$archive" -C "$FABLO_TEMP_DIR"
"$FABLO_TEMP_DIR/fablo-target/fabric-docker.sh" clone-to "$COMMAND_CALL_ROOT" "$hook_command"
echo "📦 Network restored from '$archive'! Execute 'start' command to run it."
}
if [ -z "$COMMAND" ]; then
printHelp
exit 1
elif [ "$COMMAND" = "help" ] || [ "$COMMAND" = "--help" ]; then
printHelp
elif [ "$COMMAND" = "version" ]; then
executeOnFabloDocker "version $2"
elif [ "$COMMAND" = "use" ]; then
useVersion "$2"
elif [ "$COMMAND" = "init" ]; then
initConfig "$2" "$3"
elif [ "$COMMAND" = "validate" ]; then
validateConfig "$2"
elif [ "$COMMAND" = "extend-config" ]; then
extendConfig "$2"
elif [ "$COMMAND" = "export-network-topology" ]; then
exportNetworkTopology "$2" "$3"
elif [ "$COMMAND" = "generate" ]; then
generateNetworkConfig "$2" "$3"
elif [ "$COMMAND" = "up" ]; then
networkUp "$2"
elif [ "$COMMAND" = "prune" ]; then
networkPrune
elif [ "$COMMAND" = "recreate" ]; then
networkPrune
networkUp "$2"
elif [ "$COMMAND" = "snapshot" ]; then
createSnapshot "$2"
elif [ "$COMMAND" = "restore" ]; then
restoreSnapshot "$2" "${3:-""}"
else
executeFabloCommand "$COMMAND" "$2" "$3" "$4" "$5" "$6" "$7" "$8"
fi