Skip to content

Commit

Permalink
Extract common sed function
Browse files Browse the repository at this point in the history
  • Loading branch information
DarumaDocker committed Sep 26, 2024
1 parent 68e9601 commit 2b7290a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 115 deletions.
4 changes: 2 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"rag_policy": "system-message",
"domain": "us.gaianet.network",
"llamaedge_port": "8080",
"server_health_url": "https://pulse.gaianet.ai/node-health/$subdomain",
"server_info_url": "https://pulse.gaianet.ai/node-info/$subdomain"
"server_health_url": "https://pulse.gaianet.ai/node-health/0x",
"server_info_url": "https://pulse.gaianet.ai/node-info/0x"
}
66 changes: 24 additions & 42 deletions gaianet
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ check_base_dir() {
fi
}

sed_in_place() {
if [ "$(uname)" == "Darwin" ]; then
sed -i '' "$@"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
sed -i "$@"
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then
error " * For Windows users, please run this script in WSL."
exit 1
else
error " * Only support Linux, MacOS and Windows."
exit 1
fi
}

# check the validity of the config.json file
check_config_options() {

Expand Down Expand Up @@ -541,19 +555,12 @@ init() {
domain=$(awk -F'"' '/"domain":/ {print $4}' $gaianet_base_dir/config.json)
llamaedge_port=$(awk -F'"' '/"llamaedge_port":/ {print $4}' $gaianet_base_dir/config.json)

if [[ "$OSTYPE" == "linux-gnu"* ]]; then
sed_i_cmd="sed -i"
elif [[ "$OSTYPE" == "darwin"* ]]; then
sed_i_cmd="sed -i ''"
else
echo "Unsupported OS"
exit 1
fi

$sed_i_cmd "s/subdomain = \".*\"/subdomain = \"$address\"/g" $gaianet_base_dir/gaia-frp/frpc.toml
$sed_i_cmd "s/name = \".*\"/name = \"$address.$domain\"/g" $gaianet_base_dir/gaia-frp/frpc.toml
$sed_i_cmd "s/localPort = \".*\"/localPort = \"$llamaedge_port\"/g" $gaianet_base_dir/gaia-frp/frpc.toml
$sed_i_cmd "s/serverAddr = \".*\"/serverAddr = \"$domain\"/g" $gaianet_base_dir/gaia-frp/frpc.toml
sed_in_place "/^[[:space:]]*\"server_health_url\"/ s|\(.*\/\)[^/]*\(\"[,]*\)|\1$address\2|" $gaianet_base_dir/config.json
sed_in_place "/^[[:space:]]*\"server_info_url\"/ s|\(.*\/\)[^/]*\(\"[,]*\)|\1$address\2|" $gaianet_base_dir/config.json
sed_in_place "s/subdomain = \".*\"/subdomain = \"$address\"/g" $gaianet_base_dir/gaia-frp/frpc.toml
sed_in_place "s/name = \".*\"/name = \"$address.$domain\"/g" $gaianet_base_dir/gaia-frp/frpc.toml
sed_in_place "s/localPort = \".*\"/localPort = \"$llamaedge_port\"/g" $gaianet_base_dir/gaia-frp/frpc.toml
sed_in_place "s/serverAddr = \".*\"/serverAddr = \"$domain\"/g" $gaianet_base_dir/gaia-frp/frpc.toml

# Remove all files in the directory except for frpc and frpc.toml
find $gaianet_base_dir/gaia-frp -type f -not -name 'frpc' -not -name 'frpc.toml' -exec rm -f {} \;
Expand All @@ -570,17 +577,13 @@ update_config() {
key=$1
new_value=$2
file=$gaianet_base_dir/config.json
bak=$gaianet_base_dir/config.json.bak

# update in place
if [ -z "$new_value" ]; then
sed -i.bak -e "s/\(\"$key\": \s*\).*\,/\1\"$new_value\",/" $file
sed_in_place "s/\(\"$key\": \s*\).*\,/\1\"$new_value\",/" $file
else
sed -i.bak -e "/\"$key\":/ s#: \".*\"#: \"$new_value\"#" $file
sed_in_place "/\"$key\":/ s#: \".*\"#: \"$new_value\"#" $file
fi

# remove backup file
rm $bak
}

# * start subcommand
Expand Down Expand Up @@ -1667,17 +1670,7 @@ case $subcommand in
new_value_escaped=$(echo "$new_value" | sed 's/[&\\/]/\\&/g')

# Update the value of system_prompt in config.json
if [ "$(uname)" == "Darwin" ]; then
sed -i '' "s|\"system_prompt\": \".*\"|\"system_prompt\": \"$new_value_escaped\"|" $gaianet_base_dir/config.json
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
sed -i "s|\"system_prompt\": \".*\"|\"system_prompt\": \"$new_value_escaped\"|" $gaianet_base_dir/config.json
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then
error " * For Windows users, please run this script in WSL."
exit 1
else
error " * Only support Linux, MacOS and Windows."
exit 1
fi
sed_in_place "s|\"system_prompt\": \".*\"|\"system_prompt\": \"$new_value_escaped\"|" $gaianet_base_dir/config.json
fi

# update rag prompt
Expand All @@ -1693,18 +1686,7 @@ case $subcommand in
new_value_escaped=$(echo "$new_value" | sed 's/[&\\/]/\\&/g')

# Update the value of rag_prompt in config.json
if [ "$(uname)" == "Darwin" ]; then
sed -i '' "s|\"rag_prompt\": \".*\"|\"rag_prompt\": \"$new_value_escaped\"|" $gaianet_base_dir/config.json
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
sed -i "s|\"rag_prompt\": \".*\"|\"rag_prompt\": \"$new_value_escaped\"|" $gaianet_base_dir/config.json
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then
error " * For Windows users, please run this script in WSL."
exit 1
else
error " * Only support Linux, MacOS and Windows."
exit 1
fi

sed_in_place "s|\"rag_prompt\": \".*\"|\"rag_prompt\": \"$new_value_escaped\"|" $gaianet_base_dir/config.json
fi

# update rag policy
Expand Down
98 changes: 27 additions & 71 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ check_curl_silent() {
fi
}

sed_in_place() {
if [ "$(uname)" == "Darwin" ]; then
sed -i '' "$@"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
sed -i "$@"
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then
error " * For Windows users, please run this script in WSL."
exit 1
else
error " * Only support Linux, MacOS and Windows."
exit 1
fi
}

printf "\n"
cat <<EOF
██████╗ █████╗ ██╗ █████╗ ███╗ ██╗███████╗████████╗
Expand Down Expand Up @@ -288,44 +302,16 @@ if [ "$upgrade" -eq 1 ]; then

if ! grep -q '"chat_batch_size":' $gaianet_base_dir/config.json; then
# Prepend the field to the beginning of the JSON object
if [ "$(uname)" == "Darwin" ]; then
sed -i '' '2i\
"chat_batch_size": "16",
' "$gaianet_base_dir/config.json"

elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
sed -i '2i\
"chat_batch_size": "16",
' "$gaianet_base_dir/config.json"

elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then
error " * For Windows users, please run this script in WSL."
exit 1
else
error " * Only support Linux, MacOS and Windows."
exit 1
fi
sed_in_place '2i\
"chat_batch_size": "16",
' "$gaianet_base_dir/config.json"
fi

if ! grep -q '"embedding_batch_size":' $gaianet_base_dir/config.json; then
# Prepend the field to the beginning of the JSON object
if [ "$(uname)" == "Darwin" ]; then
sed -i '' '2i\
"embedding_batch_size": "512",
' "$gaianet_base_dir/config.json"

elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
sed -i '2i\
"embedding_batch_size": "512",
' "$gaianet_base_dir/config.json"

elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then
error " * For Windows users, please run this script in WSL."
exit 1
else
error " * Only support Linux, MacOS and Windows."
exit 1
fi
sed_in_place '2i\
"embedding_batch_size": "512",
' "$gaianet_base_dir/config.json"
fi

info " * The config.json is recovered in $gaianet_base_dir"
Expand Down Expand Up @@ -489,17 +475,7 @@ if [ ! -d "$gaianet_base_dir/qdrant" ]; then
config_file="$gaianet_base_dir/qdrant/config/config.yaml"

if [ -f "$config_file" ]; then
if [ "$(uname)" == "Darwin" ]; then
sed -i '' 's/telemetry_disabled: false/telemetry_disabled: true/' "$config_file"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
sed -i 's/telemetry_disabled: false/telemetry_disabled: true/' "$config_file"
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then
error "For Windows users, please run this script in WSL."
exit 1
else
error "Only support Linux, MacOS and Windows(WSL)."
exit 1
fi
sed_in_place 's/telemetry_disabled: false/telemetry_disabled: true/' "$config_file"
fi

printf "\n"
Expand Down Expand Up @@ -664,15 +640,6 @@ fi
# Read domain from config.json
gaia_frp=$(awk -F'"' '/"domain":/ {print $4}' $gaianet_base_dir/config.json)

# Replace the serverAddr & subdomain in frpc.toml
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
sed_i_cmd="sed -i"
elif [[ "$OSTYPE" == "darwin"* ]]; then
sed_i_cmd="sed -i ''"
else
echo "Unsupported OS"
exit 1
fi

if [ "$upgrade" -eq 1 ]; then
# recover deviceid.txt
Expand Down Expand Up @@ -703,23 +670,12 @@ else
echo "$device_id" > "$device_id_file"
fi

# Replace subdomain for the pulse api url
if [ "$(uname)" == "Darwin" ]; then
sed -i '' "s/subdomain = \".*\"/subdomain = \"$subdomain\"/g" $gaianet_base_dir/gaia-frp/frpc.toml
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
sed -i "s/subdomain = \".*\"/subdomain = \"$subdomain\"/g" $gaianet_base_dir/gaia-frp/frpc.toml
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then
error "For Windows users, please run this script in WSL."
exit 1
else
error "Only support Linux, MacOS and Windows(WSL)."
exit 1
fi

$sed_i_cmd "s/subdomain = \".*\"/subdomain = \"$subdomain\"/g" $gaianet_base_dir/gaia-frp/frpc.toml
$sed_i_cmd "s/serverAddr = \".*\"/serverAddr = \"$gaia_frp\"/g" $gaianet_base_dir/gaia-frp/frpc.toml
$sed_i_cmd "s/name = \".*\"/name = \"$subdomain.$gaia_frp\"/g" $gaianet_base_dir/gaia-frp/frpc.toml
$sed_i_cmd "s/metadatas.deviceId = \".*\"/metadatas.deviceId = \"$device_id\"/g" $gaianet_base_dir/gaia-frp/frpc.toml
sed_in_place "/^[[:space:]]*\"server_health_url\"/ s|\(.*\/\)[^/]*\(\"[,]*\)|\1$subdomain\2|" $gaianet_base_dir/config.json
sed_in_place "/^[[:space:]]*\"server_info_url\"/ s|\(.*\/\)[^/]*\(\"[,]*\)|\1$subdomain\2|" $gaianet_base_dir/config.json
sed_in_place "s/subdomain = \".*\"/subdomain = \"$subdomain\"/g" $gaianet_base_dir/gaia-frp/frpc.toml
sed_in_place "s/serverAddr = \".*\"/serverAddr = \"$gaia_frp\"/g" $gaianet_base_dir/gaia-frp/frpc.toml
sed_in_place "s/name = \".*\"/name = \"$subdomain.$gaia_frp\"/g" $gaianet_base_dir/gaia-frp/frpc.toml
sed_in_place "s/metadatas.deviceId = \".*\"/metadatas.deviceId = \"$device_id\"/g" $gaianet_base_dir/gaia-frp/frpc.toml

# Remove all files in the directory except for frpc and frpc.toml
find $gaianet_base_dir/gaia-frp -type f -not -name 'frpc.toml' -exec rm -f {} \;
Expand Down

0 comments on commit 2b7290a

Please sign in to comment.