Skip to content

Commit

Permalink
feat: add -c argument (#23)
Browse files Browse the repository at this point in the history
* feat: add -c argument

* feat: change github mirror

* fix: add branch name in curl url in test

* fix: fix if and variable syntax error

* feat: git pull if already cloned

* fix: auto stop with output

* fix: remove output

* fix: test
  • Loading branch information
wy-luke committed Sep 14, 2023
1 parent 25db1bd commit 8a1fe90
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 44 deletions.
31 changes: 23 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,44 @@ name: Test
on:
workflow_dispatch:
push:
branches: [main]
branches: [main, dev]

env:
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}

jobs:
Yes-Test:
runs-on: macos-latest
Network-Connected-Test-macOS13:
runs-on: macOS-13
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: sd-installer.sh
sparse-checkout-cone-mode: false
- name: Install
run: bash sd-installer.sh -t -1 | grep -q 'Launching Web UI with arguments' && echo '成功 Success' && exit 0
shell: /bin/bash --noprofile --norc -e {0}

Network-Connected-Test-macOS12:
runs-on: macos-12
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: sd-installer.sh
sparse-checkout-cone-mode: false
- name: Yes
run: bash sd-installer.sh -1 | grep -q 'Launching Web UI with arguments' && echo "成功 Success" && exit 0
- name: Install
run: bash sd-installer.sh -t -1 | grep -q 'Launching Web UI with arguments' && echo '成功 Success' && exit 0
shell: /bin/bash --noprofile --norc -e {0}

No-Test:
Network-Not-Connected-Test:
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: sd-installer.sh
sparse-checkout-cone-mode: false
- name: No
run: bash sd-installer.sh -2 | grep -q 'Launching Web UI with arguments' && echo "成功 Success" && exit 0
- name: Install
run: bash sd-installer.sh -t -2 | grep -q 'Launching Web UI with arguments' && echo '成功 Success' && exit 0
shell: /bin/bash --noprofile --norc -e {0}
6 changes: 4 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/wy-luke/StableDiffusion-Installer-For-Mac/main/sd-installer.sh)"
```

国内用户存在网络问题时,可以尝试使用以下命令。同时,在提示 `网络是否顺畅? 默认y [y/n]` 时,输入 `n`,然后按**回车**
国内用户存在网络问题时,可以尝试使用以下命令

```bash
/bin/bash -c "$(curl -fsSL https://raw.fastgit.org/wy-luke/StableDiffusion-Installer-For-Mac/main/sd-installer.sh)"
curl -fsSL https://ghproxy.com/https://raw.githubusercontent.com/wy-luke/StableDiffusion-Installer-For-Mac/main/sd-installer.sh | /bin/bash -s -- -c
```

该命令为安装脚本添加 `-c` 参数,会在安装时自动切换为国内源,进行网络加速,包括 homebrew、conda-forge、pip、github 等, 但链接可能会不稳定或失效, 若安装失败可以重试。

3. 在安装开始时,会提示您输入密码,只需输入您的登录密码即可。

> 注意:您的密码在输入时是不可见的,只需正常输入,完成后按 Enter 键。
Expand Down
97 changes: 63 additions & 34 deletions sd-installer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set -u
installation_path=$HOME
tmp_path="$HOME/.sd-installer"
mamba_root_path="~/micromamba"
net_connected=true
network_connected=true

# Other variables
code_path="$installation_path/stable-diffusion-webui"
Expand All @@ -20,7 +20,7 @@ fi
brew_path="$brew_pkg_path/bin/brew"
mamba_path="$brew_pkg_path/opt/micromamba/bin/micromamba"

test_mode=0 # Only for test. 0 for no test, 1 for yes-test, 2 for no-test
test_mode=0 # Only for test. 0 for no test, 1 for Network-Connected-Test, 2 for Network-Not-Connected-Test

function clean_up {
echo "############ Clean ###################################"
Expand All @@ -47,7 +47,12 @@ function handle_error {
error_choice=${error_choice:-y}
if [[ $error_choice == [yY] ]]; then
# Retry the command
/bin/bash -c "$(curl -fsSL $sd_installer_url)"

if [ "$network_connected" == false ]; then
/bin/bash -c "$(curl -fsSL $sd_installer_url)" -c
else
/bin/bash -c "$(curl -fsSL $sd_installer_url)"
fi
else
clean_up
# Exit the script
Expand All @@ -70,8 +75,18 @@ function verify_installation {
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-1 | -2)
test_mode=${1:1}
-test | -t)
value=${2:1}
if [ "$value" != 1 ] && [ "$value" != 2 ]; then
echo_red "未知选项: $1 $2"
exit 1
fi
test_mode=$value
shift # past argument
shift # past value
;;
-c)
network_connected=false
shift
;;
# -h | --help)
Expand All @@ -88,32 +103,42 @@ done

if [ "$test_mode" != 0 ]; then
if [ "$test_mode" == 1 ]; then
net_connected=true
echo_green "Yes-test"
network_connected=true
echo_green "Network-Connected-Test"
else
net_connected=false
echo_green "No-test"
network_connected=false
echo_green "Network-Not-Connected-Test"
fi
else
echo_green "For non-Chinese users, you could just ignore this and press the Enter key"
read -rp "网络是否顺畅? 默认y [y/n] " net_choice
net_choice=${net_choice:-y}
if [[ $net_choice == [nN] ]]; then
net_connected=false
echo_green "将设置国内镜像源安装, 包括 homebrew、conda-forge、pip、github 等, 但链接可能会不稳定/失效, 若失败可以重试"
# echo_green "For non-Chinese users, you could just ignore this and press the Enter key"
# read -rp "网络是否顺畅? 默认y [y/n] " network_choice
# network_choice=${network_choice:-y}
# if [[ $network_choice == [nN] ]]; then
# network_connected=false
# echo_green "将设置国内镜像源安装, 包括 homebrew、conda-forge、pip、github 等, 但链接可能会不稳定/失效, 若失败可以重试"
# else
# echo_green "网络通畅, 正常安装"
# fi

if [ "$network_connected" == false ]; then
echo_green "将设置国内镜像源安装, 包括 homebrew、conda-forge、pip、github 等, 但链接可能会不稳定或失效, 若安装失败可以重试"
else
echo_green "网络通畅, 正常安装"
echo_green "恭喜网络通畅, 正常安装"
fi
fi

brew_installer_url="https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"
sd_installer_url="https://raw.githubusercontent.com/wy-luke/StableDiffusion-Installer-For-Mac/main/sd-installer.sh"
sd_webui_url="https://github.com/AUTOMATIC1111/stable-diffusion-webui.git"

if ! $net_connected; then
brew_installer_url="https://raw.fastgit.org/Homebrew/install/HEAD/install.sh"
sd_installer_url="https://raw.fastgit.org/wy-luke/StableDiffusion-Installer-For-Mac/main/sd-installer.sh"
sd_webui_url="https://ghproxy.com/github.com/AUTOMATIC1111/stable-diffusion-webui.git"
if [ "$network_connected" == false ]; then
# raw.gitmirror.com 备份
# brew_installer_url="https://raw.gitmirror.com/Homebrew/install/HEAD/install.sh"
# sd_installer_url="https://raw.gitmirror.com/wy-luke/StableDiffusion-Installer-For-Mac/main/sd-installer.sh"

brew_installer_url="https://ghproxy.com/https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"
sd_installer_url="https://ghproxy.com/https://raw.githubusercontent.com/wy-luke/StableDiffusion-Installer-For-Mac/main/sd-installer.sh"
sd_webui_url="https://ghproxy.com/https://github.com/AUTOMATIC1111/stable-diffusion-webui"
fi

echo "############ 开始安装 Stable Diffusion web UI #########" && echo
Expand All @@ -139,24 +164,16 @@ if ! command -v brew &>/dev/null; then
# Grant the permission to execute the installation script
chmod +x $brew_installer_path

if ! $net_connected; then
if [ "$network_connected" == false ]; then
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
fi

yes | /bin/bash -c $brew_installer_path
eval "$($brew_path shellenv)"
verify_installation brew

if ! $net_connected; then
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_PIP_INDEX_URL="https://pypi.tuna.tsinghua.edu.cn/simple"
fi
brew update
else
echo_red "Homebrew 安装文件下载失败, 请检查网络连接"
echo_red "Failed to download Homebrew installation script, please check your network connection"
Expand All @@ -167,6 +184,15 @@ else
echo_green "Homebrew has already been installed"
fi

if [ "$network_connected" == false ]; then
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_PIP_INDEX_URL="https://pypi.tuna.tsinghua.edu.cn/simple"
fi
brew update

echo_green "Install the packages required"
brew install cmake protobuf rust git wget
# brew install --cask cmake
Expand All @@ -193,7 +219,7 @@ else
fi

# Set default channels for micromamba
if ! $net_connected; then
if [ "$network_connected" == false ]; then
micromamba config prepend channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
# micromamba config prepend channels http://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
else
Expand Down Expand Up @@ -230,7 +256,10 @@ fi
# Enter the SD's folder
cd $code_path

if ! $net_connected; then
# TODO: Optional
git pull

if [ "$network_connected" == false ]; then
sed -i '' "s/https:\/\/github.com/https:\/\/ghproxy.com\/github.com/g" modules/launch_utils.py
else
sed -i '' "s/https:\/\/ghproxy.com\/github.com/https:\/\/github.com/g" modules/launch_utils.py
Expand Down Expand Up @@ -263,7 +292,7 @@ source venv/bin/activate
# Delete pip cache to avoid some errors
pip cache purge

if ! $net_connected; then
if [ "$network_connected" == false ]; then
# Tsinghua mirror has no tb-nightly package, which is needed by basicsr
# TODO: reset in clean_up
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple
Expand All @@ -283,7 +312,7 @@ fi
# micromamba install --yes --file requirements_versions.txt

# Fix issue https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/12210
if ! $net_connected; then
if [ "$network_connected" == false ]; then
echo >>webui-macos-env.sh
echo "export TORCH_COMMAND=\"pip install wheel==0.41.1 torch==2.0.1 torchvision==0.15.2 cython && pip install git+https://ghproxy.com/https://github.com/TencentARC/GFPGAN.git@8d2447a2d918f8eba5a4a01463fd48e45126a379 --prefer-binary\"" >>webui-macos-env.sh
fi
Expand Down

0 comments on commit 8a1fe90

Please sign in to comment.