-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrepair
executable file
·228 lines (199 loc) · 7.97 KB
/
repair
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
#!/bin/bash
# Script to repair a Suibase installation.
#
# Have no effect if there is nothing to repair.
#
REPAIR_SB_DIR="$HOME/suibase"
# Function to repair symlinks as needed.
#
# Has no effect if symlink already exists and matching.
update_bin_path() {
local _OS_DIR_ON_PATH="$HOME/.local/bin"
UBP_TARGET="$REPAIR_SB_DIR/scripts/$1"
if [ -d "$_OS_DIR_ON_PATH" ]; then
if [ ! -L "$_OS_DIR_ON_PATH/$1" ]; then
ln -s "$UBP_TARGET" "$_OS_DIR_ON_PATH/$1"
else
# Verify link is as intended, if not replace it.
UBP_READLINK=$(readlink -f "$_OS_DIR_ON_PATH/$1")
if [[ "$UBP_READLINK" != "$UBP_TARGET" ]]; then
ln -sf "$UBP_TARGET" "$_OS_DIR_ON_PATH/$1"
fi
fi
fi
}
fix_suibase_rename_dir_symlink() {
local _SYMLINK="$1"
# Change "sui-base" to "suibase" in the target of the symlink
if [ -z "$_SYMLINK" ]; then
return
fi
local _SYMLINK_TARGET
local _SYMLINK_TARGET_NEW
# Notice '-m' to allow for unresolved portion in the path.
# (which is expected when sui-base no longer exists).
if [[ $(uname) = "Darwin" ]]; then
# Will blindly return the target. Good enough for now.
_SYMLINK_TARGET=$(readlink "$_SYMLINK")
else
# Will resolve intermediate symlinks (if any) in target.
_SYMLINK_TARGET=$(readlink -m "$_SYMLINK")
fi
_SYMLINK_TARGET_NEW="${_SYMLINK_TARGET//sui-base/suibase}"
#echo "symlink $_SYMLINK_TARGET_NEW $_SYMLINK_TARGET"
if [[ "$_SYMLINK_TARGET" != "$_SYMLINK_TARGET_NEW" ]]; then
ln -nsf "$_SYMLINK_TARGET_NEW" "$_SYMLINK"
fi
}
fix_suibase_rename_file() {
local _PATHNAME="$1"
# Fix sui-base to suibase text in the file.
if [ -z "$_PATHNAME" ] || [ ! -f "$_PATHNAME" ]; then
return
fi
local _SEARCH_STRING="sui-base"
local _REPLACE_STRING="suibase"
sed -i.bak -e "s/$_SEARCH_STRING/$_REPLACE_STRING/g" \
"$_PATHNAME" &&
rm "$_PATHNAME.bak"
}
suibase_repair() {
# Check if there is additional variable that we can use
# from __globals.sh. This may happen when this script was
# sourced for a particular workdir.
local _SCRIPT_SOURCED=false
if [ -n "$SUIBASE_VERSION" ] && [ -n "$USER_CWD" ]; then
_SCRIPT_SOURCED=true
fi
# Check if current dir is rooted to ~/sui-base (a deprecated location).
local _EXIT_DIRECTORY_INSTRUCTION=false
if [[ "$(pwd)" = *"sui-base"* ]]; then
_EXIT_DIRECTORY_INSTRUCTION=true
fi
if [ "$_SCRIPT_SOURCED" = true ] && [[ "$USER_CWD" = *"sui-base"* ]]; then
_EXIT_DIRECTORY_INSTRUCTION=true
fi
# Check if all the sui-base to suibase repairs have to be done.
local _RENAME_DIRECTORY_REPAIR=false
if [ -d "$HOME/sui-base" ] && [ ! -d "$REPAIR_SB_DIR" ]; then
echo "Starting one-time renaming of sui-base to suibase..."
mv "$HOME/sui-base" "$REPAIR_SB_DIR"
_RENAME_DIRECTORY_REPAIR=true
fi
# Build the list of scripts (e.g. "localnet, devnet, tsui...")
local script_list=()
for dir_entry in "$REPAIR_SB_DIR/"scripts/*; do
if [ -f "$dir_entry" ] && [ -x "$dir_entry" ]; then
script_list+=("$(basename "$dir_entry")")
fi
done
for script_name in "${script_list[@]}"; do
update_bin_path "$script_name"
done
# Iterate directory in $REPAIR_SB_DIR/workdirs/
local _DIR_ENTRIES=("$REPAIR_SB_DIR"/workdirs/*)
for dir_entry in "${_DIR_ENTRIES[@]}"; do
if [ -L "$dir_entry" ]; then
# This is a directory symlink (probably "active"). Check it.
fix_suibase_rename_dir_symlink "$dir_entry"
continue
fi
if [ -d "$dir_entry" ]; then
# Do all repairs at workdir level related to when sui-base
# was renamed suibase.
#
# Detect with presence/absence of sui-base.yaml vs suibase.yaml.
workdir=$(basename "$dir_entry")
local _DO_SUI_BASE_RENAME_REPAIR=false
if [ -f "$REPAIR_SB_DIR/workdirs/$workdir/sui-base.yaml" ]; then
_DO_SUI_BASE_RENAME_REPAIR=true
fi
if [ "$_DO_SUI_BASE_RENAME_REPAIR" = true ]; then
# Iterate one level deeper to fix symlinks in workdir.
local _DIR_DIR_ENTRIES=("$dir_entry"/*)
for dir_dir_entry in "${_DIR_DIR_ENTRIES[@]}"; do
if [ -L "$dir_dir_entry" ]; then
# This is a directory symlink. Fix it.
fix_suibase_rename_dir_symlink "$dir_dir_entry"
fi
done
# Copy suibase.yaml from templates (when does not exists).
if [ ! -f "$REPAIR_SB_DIR/workdirs/$workdir/suibase.yaml" ] &&
[ -f "$REPAIR_SB_DIR/scripts/templates/$workdir/suibase.yaml" ]; then
# Fix client.yaml path (as needed).
fix_suibase_rename_file "$REPAIR_SB_DIR/workdirs/$workdir/config-default/client.yaml"
fix_suibase_rename_file "$REPAIR_SB_DIR/workdirs/$workdir/config/client.yaml"
fix_suibase_rename_file "$REPAIR_SB_DIR/workdirs/$workdir/faucet/client.yaml"
# Overwrite the sui-exec and workdir-exec scripts.
cp -f "$REPAIR_SB_DIR/scripts/templates/sui-exec" "$REPAIR_SB_DIR/workdirs/$workdir/sui-exec"
cp -f "$REPAIR_SB_DIR/scripts/templates/workdir-exec" "$REPAIR_SB_DIR/workdirs/$workdir/workdir-exec"
cp "$REPAIR_SB_DIR/scripts/templates/$workdir/suibase.yaml" "$REPAIR_SB_DIR/workdirs/$workdir/suibase.yaml"
# Append user content from sui-base.yaml to suibase.yaml
# (all non-blank lines not starting with #).
if [ -f "$REPAIR_SB_DIR/workdirs/$workdir/sui-base.yaml" ] &&
[ -f "$REPAIR_SB_DIR/workdirs/$workdir/suibase.yaml" ]; then
{ sed '/^$/d;/^#/d' "$REPAIR_SB_DIR/workdirs/$workdir/sui-base.yaml"; } >>"$REPAIR_SB_DIR/workdirs/$workdir/suibase.yaml"
rm "$REPAIR_SB_DIR/workdirs/$workdir/sui-base.yaml"
# Force rebuild because of problems with custom builds sometimes refering to old path.
echo "Cleaning up $workdir sui repo"
cd "$REPAIR_SB_DIR/workdirs/$workdir/sui-repo-default" && (cargo clean)
fi
fi
fi
fi
done
if [ "$_RENAME_DIRECTORY_REPAIR" = true ]; then
# Force rebuild because of problems with custom builds sometimes refering to old path.
echo "Cleaning up local suibase repos"
cd "$REPAIR_SB_DIR/rust/demo-app/" && (cargo clean)
cd "$REPAIR_SB_DIR/rust/helper" && (cargo clean)
cd "$REPAIR_SB_DIR/rust/helper-uniffi" && (cargo clean)
cd "$REPAIR_SB_DIR/rust/suibase" && (cargo clean)
# Force a rebuild and restart of the suibase-daemon.
rm -rf "$REPAIR_SB_DIR/workdirs/common/bin/suibase-daemon" >/dev/null 2>&1
fi
# Basic repairs should be done by now.
# Source more scripts for calling into more advanced repairs/update.
if [ "$_SCRIPT_SOURCED" = false ]; then
# If not sourced, then default to run in active context.
SCRIPT_COMMON_CALLER="$(readlink -f "$0")"
WORKDIR="active"
# shellcheck source=SCRIPTDIR/scripts/common/__globals.sh
source "$REPAIR_SB_DIR/scripts/common/__globals.sh" "$SCRIPT_COMMON_CALLER" "$WORKDIR"
trap cleanup EXIT
fi
# shellcheck source=SCRIPTDIR/scripts/common/__apps.sh
source "$REPAIR_SB_DIR/scripts/common/__apps.sh"
if [ -z "$SUIBASE_DAEMON_VERSION_FILE" ]; then
# shellcheck source=SCRIPTDIR/scripts/common/__suibase-daemon.sh
source "$REPAIR_SB_DIR/scripts/common/__suibase-daemon.sh"
start_suibase_daemon_as_needed
fi
if [ -z "$DTP_DAEMON_VERSION_FILE" ]; then
# shellcheck source=SCRIPTDIR/scripts/common/__dtp-daemon.sh
source "$REPAIR_SB_DIR/scripts/common/__dtp-daemon.sh"
start_dtp_daemon_as_needed
fi
# Exit instruction to the user.
local _EXIT=false
if [ "$_RENAME_DIRECTORY_REPAIR" = true ]; then
echo "The directory ~/sui-base has been renamed to ~/suibase."
echo
echo "** This was a required one-time change **"
echo
echo "Do a 'localnet update' to rebuild the binaries."
_EXIT=true
fi
if [ "$_EXIT_DIRECTORY_INSTRUCTION" = true ]; then
echo
echo "You should not run any command under ~/sui-base anymore."
echo "It has been renamed to ~/suibase."
echo
echo "Do 'cd' to another directory."
_EXIT=true
fi
if [ "$_EXIT" = true ]; then
exit 1
fi
}
suibase_repair