From cc356ce67d23e03a471547ef6f66776b477de4bd Mon Sep 17 00:00:00 2001 From: cmdr2 Date: Sat, 22 Oct 2022 23:24:13 +0530 Subject: [PATCH] Initial commit of the new micromamba-based installer; This should work seamlessly for new and existing users; Also allows the installer to run on mac (but the installation will fail because the mac-specific environment.yaml hasn't been added yet) --- scripts/Developer Console.cmd | 21 +++++++-- scripts/Start Stable Diffusion UI.cmd | 28 +++++++---- scripts/bootstrap.bat | 53 +++++++++++++++++++++ scripts/bootstrap.sh | 67 +++++++++++++++++++++++++++ scripts/developer_console.sh | 20 ++++++-- scripts/on_env_start.bat | 3 +- scripts/on_env_start.sh | 1 + scripts/on_sd_start.bat | 3 +- scripts/on_sd_start.sh | 6 ++- scripts/start.sh | 19 ++++++-- 10 files changed, 194 insertions(+), 27 deletions(-) create mode 100644 scripts/bootstrap.bat create mode 100644 scripts/bootstrap.sh diff --git a/scripts/Developer Console.cmd b/scripts/Developer Console.cmd index e9bfb28b..e088a51f 100644 --- a/scripts/Developer Console.cmd +++ b/scripts/Developer Console.cmd @@ -2,13 +2,24 @@ echo "Opening Stable Diffusion UI - Developer Console.." & echo. -@call installer\Scripts\activate.bat +set PATH=C:\Windows\System32;%PATH% -@call conda-unpack +@rem set legacy and new installer's PATH, if they exist +if exist "installer" set PATH=%cd%\installer;%cd%\installer\Library\bin;%cd%\installer\Scripts;%PATH% +if exist "installer_files\env" set PATH=%cd%\installer_files\env;%cd%\installer_files\env\Library\bin;%cd%\installer_files\env\Scripts;%PATH% -@call conda --version -@call git --version +@rem Test the environment +echo "Environment Info:" +call where git +call git --version -@call conda activate .\stable-diffusion\env +call where python +call python --version + +call where conda +call conda --version + +@rem activate the environment +call conda activate .\stable-diffusion\env cmd /k \ No newline at end of file diff --git a/scripts/Start Stable Diffusion UI.cmd b/scripts/Start Stable Diffusion UI.cmd index 8563f6b3..3a8100da 100644 --- a/scripts/Start Stable Diffusion UI.cmd +++ b/scripts/Start Stable Diffusion UI.cmd @@ -1,19 +1,27 @@ @echo off -@REM Delete the post-activate hook from the old installer -if exist "installer\etc\conda\activate.d\post_activate.bat" ( - echo. > installer\etc\conda\activate.d\post_activate.bat -) +set PATH=C:\Windows\System32;%PATH% -@call installer\Scripts\activate.bat +@rem set legacy installer's PATH, if it exists +if exist "installer" set PATH=%cd%\installer;%cd%\installer\Library\bin;%cd%\installer\Scripts;%PATH% -@call conda-unpack +@rem Setup the packages required for the installer +call scripts\bootstrap.bat -@call conda --version -@call git --version +@rem set new installer's PATH, if it downloaded any packages +if exist "installer_files\env" set PATH=%cd%\installer_files\env;%cd%\installer_files\env\Library\bin;%cd%\installer_files\env\Scripts;%PATH% -@cd installer +@rem Test the bootstrap +call where git +call git --version -@call ..\scripts\on_env_start.bat +call where python +call python --version + +call where conda +call conda --version + +@rem Download the rest of the installer and UI +call scripts\on_env_start.bat @pause diff --git a/scripts/bootstrap.bat b/scripts/bootstrap.bat new file mode 100644 index 00000000..8a231deb --- /dev/null +++ b/scripts/bootstrap.bat @@ -0,0 +1,53 @@ +@echo off + +@rem This script will install git and conda (if not found on the PATH variable) +@rem using micromamba (an 8mb static-linked single-file binary, conda replacement). +@rem For users who already have git and conda, this step will be skipped. + +@rem This enables a user to install this project without manually installing conda and git. + +@rem config +set MAMBA_ROOT_PREFIX=%cd%\installer_files\mamba +set INSTALL_ENV_DIR=%cd%\installer_files\env +set MICROMAMBA_DOWNLOAD_URL=https://github.com/cmdr2/stable-diffusion-ui/releases/download/v1.1/micromamba.exe + +@rem figure out whether git and conda needs to be installed +if exist "%INSTALL_ENV_DIR%" set PATH=%INSTALL_ENV_DIR%;%INSTALL_ENV_DIR%\Library\bin;%INSTALL_ENV_DIR%\Scripts;%PATH% + +set PACKAGES_TO_INSTALL= + +call conda --version >.tmp1 2>.tmp2 +if "%ERRORLEVEL%" NEQ "0" set PACKAGES_TO_INSTALL=%PACKAGES_TO_INSTALL% conda + +call git --version >.tmp1 2>.tmp2 +if "%ERRORLEVEL%" NEQ "0" set PACKAGES_TO_INSTALL=%PACKAGES_TO_INSTALL% git + +@rem (if necessary) install git and conda into a contained environment +if "%PACKAGES_TO_INSTALL%" NEQ "" ( + @rem download micromamba + if not exist "%MAMBA_ROOT_PREFIX%\micromamba.exe" ( + echo "Downloading micromamba from %MICROMAMBA_DOWNLOAD_URL% to %MAMBA_ROOT_PREFIX%\micromamba.exe" + + mkdir "%MAMBA_ROOT_PREFIX%" + call curl -L "%MICROMAMBA_DOWNLOAD_URL%" > "%MAMBA_ROOT_PREFIX%\micromamba.exe" + + @rem test the mamba binary + echo Micromamba version: + call "%MAMBA_ROOT_PREFIX%\micromamba.exe" --version + ) + + @rem create the installer env + if not exist "%INSTALL_ENV_DIR%" ( + call "%MAMBA_ROOT_PREFIX%\micromamba.exe" create -y --prefix "%INSTALL_ENV_DIR%" + ) + + echo "Packages to install:%PACKAGES_TO_INSTALL%" + + call "%MAMBA_ROOT_PREFIX%\micromamba.exe" install -y --prefix "%INSTALL_ENV_DIR%" -c conda-forge %PACKAGES_TO_INSTALL% + + if not exist "%INSTALL_ENV_DIR%" ( + echo "There was a problem while installing%PACKAGES_TO_INSTALL% using micromamba. Cannot continue." + pause + exit /b + ) +) diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh new file mode 100644 index 00000000..da39f4c5 --- /dev/null +++ b/scripts/bootstrap.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +# This script will install git and conda (if not found on the PATH variable) +# using micromamba (an 8mb static-linked single-file binary, conda replacement). +# For users who already have git and conda, this step will be skipped. + +# This enables a user to install this project without manually installing conda and git. + + +OS_NAME=$(uname -s) +case "${OS_NAME}" in + Linux*) OS_NAME="linux";; + Darwin*) OS_NAME="osx";; + *) echo "Unknown OS: $OS_NAME! This script runs only on Linux or Mac" && exit +esac + +OS_ARCH=$(uname -m) +case "${OS_ARCH}" in + x86_64*) OS_ARCH="64";; + arm64*) OS_ARCH="arm64";; + *) echo "Unknown system architecture: $OS_ARCH! This script runs only on x86_64 or arm64" && exit +esac + +# https://mamba.readthedocs.io/en/latest/installation.html +if [ "$OS_NAME" == "linux" ] && [ "$OS_ARCH" == "arm64" ]; then OS_ARCH="aarch64"; fi + +# config +export MAMBA_ROOT_PREFIX="$(pwd)/installer_files/mamba" +INSTALL_ENV_DIR="$(pwd)/installer_files/env" +MICROMAMBA_DOWNLOAD_URL="https://micro.mamba.pm/api/micromamba/${OS_NAME}-${OS_ARCH}/latest" + +# figure out whether git and conda needs to be installed +if [ -e "$INSTALL_ENV_DIR" ]; then export PATH="$INSTALL_ENV_DIR/bin:$PATH"; fi + +PACKAGES_TO_INSTALL="" + +if ! hash "conda" &>/dev/null; then PACKAGES_TO_INSTALL="$PACKAGES_TO_INSTALL conda"; fi +if ! hash "git" &>/dev/null; then PACKAGES_TO_INSTALL="$PACKAGES_TO_INSTALL git"; fi + +# (if necessary) install git and conda into a contained environment +if [ "$PACKAGES_TO_INSTALL" != "" ]; then + # download micromamba + if [ ! -e "$MAMBA_ROOT_PREFIX/micromamba" ]; then + echo "Downloading micromamba from $MICROMAMBA_DOWNLOAD_URL to $MAMBA_ROOT_PREFIX/micromamba" + + mkdir -p "$MAMBA_ROOT_PREFIX" + curl -L "$MICROMAMBA_DOWNLOAD_URL" | tar -xvj "$MAMBA_ROOT_PREFIX/micromamba" + + # test the mamba binary + echo "Micromamba version:" + "$MAMBA_ROOT_PREFIX/micromamba" --version + fi + + # create the installer env + if [ ! -e "$INSTALL_ENV_DIR" ]; then + "$MAMBA_ROOT_PREFIX/micromamba" create -y --prefix "$INSTALL_ENV_DIR" + fi + + echo "Packages to install:$PACKAGES_TO_INSTALL" + + "$MAMBA_ROOT_PREFIX/micromamba" install -y --prefix "$INSTALL_ENV_DIR" -c conda-forge $PACKAGES_TO_INSTALL + + if [ ! -e "$INSTALL_ENV_DIR" ]; then + echo "There was a problem while installing$PACKAGES_TO_INSTALL using micromamba. Cannot continue." + exit + fi +fi diff --git a/scripts/developer_console.sh b/scripts/developer_console.sh index d242d1f6..1fe016a9 100755 --- a/scripts/developer_console.sh +++ b/scripts/developer_console.sh @@ -4,13 +4,25 @@ if [ "$0" == "bash" ]; then echo "Opening Stable Diffusion UI - Developer Console.." echo "" - source installer/bin/activate + # set legacy and new installer's PATH, if they exist + if [ -e "installer" ]; then export PATH="$(pwd)/installer/bin:$PATH"; fi + if [ -e "installer_files/env" ]; then export PATH="$(pwd)/installer_files/env/bin:$PATH"; fi - conda-unpack - - conda --version + # test the environment + echo "Environment Info:" + which git git --version + which python + python --version + + which conda + conda --version + + # activate the environment + CONDA_BASEPATH=$(conda info --base) + source "$CONDA_BASEPATH/etc/profile.d/conda.sh" # otherwise conda complains about 'shell not initialized' (needed when running in a script) + conda activate ./stable-diffusion/env else bash --init-file developer_console.sh diff --git a/scripts/on_env_start.bat b/scripts/on_env_start.bat index 0c8eb327..eb5aa06c 100644 --- a/scripts/on_env_start.bat +++ b/scripts/on_env_start.bat @@ -4,8 +4,6 @@ set PATH=C:\Windows\System32;%PATH% -@cd .. - if exist "scripts\config.bat" ( @call scripts\config.bat ) @@ -54,6 +52,7 @@ if "%update_branch%"=="" ( @xcopy sd-ui-files\ui ui /s /i /Y @copy sd-ui-files\scripts\on_sd_start.bat scripts\ /Y +@copy sd-ui-files\scripts\bootstrap.bat scripts\ /Y @copy "sd-ui-files\scripts\Start Stable Diffusion UI.cmd" . /Y @copy "sd-ui-files\scripts\Developer Console.cmd" . /Y diff --git a/scripts/on_env_start.sh b/scripts/on_env_start.sh index 5c574e4c..eff27e57 100755 --- a/scripts/on_env_start.sh +++ b/scripts/on_env_start.sh @@ -36,6 +36,7 @@ fi rm -rf ui cp -Rf sd-ui-files/ui . cp sd-ui-files/scripts/on_sd_start.sh scripts/ +cp sd-ui-files/scripts/bootstrap.sh scripts/ cp sd-ui-files/scripts/start.sh . cp sd-ui-files/scripts/developer_console.sh . diff --git a/scripts/on_sd_start.bat b/scripts/on_sd_start.bat index feb73468..94ba6a89 100644 --- a/scripts/on_sd_start.bat +++ b/scripts/on_sd_start.bat @@ -1,11 +1,12 @@ @echo off @copy sd-ui-files\scripts\on_env_start.bat scripts\ /Y +@copy sd-ui-files\scripts\bootstrap.bat scripts\ /Y @REM Caution, this file will make your eyes and brain bleed. It's such an unholy mess. @REM Note to self: Please rewrite this in Python. For the sake of your own sanity. -@copy "sd-ui-files\scripts\Developer Console.cmd" . /Y +@REM remove the old version of the dev console script, if it's still present if exist "Open Developer Console.cmd" del "Open Developer Console.cmd" @call python -c "import os; import shutil; frm = 'sd-ui-files\\ui\\hotfix\\9c24e6cd9f499d02c4f21a033736dabd365962dc80fe3aeb57a8f85ea45a20a3.26fead7ea4f0f843f6eb4055dfd25693f1a71f3c6871b184042d4b126244e142'; dst = os.path.join(os.path.expanduser('~'), '.cache', 'huggingface', 'transformers', '9c24e6cd9f499d02c4f21a033736dabd365962dc80fe3aeb57a8f85ea45a20a3.26fead7ea4f0f843f6eb4055dfd25693f1a71f3c6871b184042d4b126244e142'); shutil.copyfile(frm, dst) if os.path.exists(dst) else print(''); print('Hotfixed broken JSON file from OpenAI');" diff --git a/scripts/on_sd_start.sh b/scripts/on_sd_start.sh index e949dfcb..964ea051 100755 --- a/scripts/on_sd_start.sh +++ b/scripts/on_sd_start.sh @@ -1,10 +1,12 @@ #!/bin/bash cp sd-ui-files/scripts/on_env_start.sh scripts/ +cp sd-ui-files/scripts/bootstrap.sh scripts/ -source installer/etc/profile.d/conda.sh +CONDA_BASEPATH=$(conda info --base) +source "$CONDA_BASEPATH/etc/profile.d/conda.sh" # otherwise conda complains about 'shell not initialized' (needed when running in a script) -cp sd-ui-files/scripts/developer_console.sh . +# remove the old version of the dev console script, if it's still present if [ -e "open_dev_console.sh" ]; then rm "open_dev_console.sh" fi diff --git a/scripts/start.sh b/scripts/start.sh index e077593a..e5981434 100755 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -1,10 +1,23 @@ #!/bin/bash -source installer/bin/activate +# set legacy installer's PATH, if it exists +if [ -e "installer" ]; then export PATH="$(pwd)/installer/bin:$PATH"; fi -conda-unpack +# Setup the packages required for the installer +scripts/bootstrap.sh -conda --version +# set new installer's PATH, if it downloaded any packages +if [ -e "installer_files/env" ]; then export PATH="$(pwd)/installer_files/env/bin:$PATH"; fi + +# Test the bootstrap +which git git --version +which python +python --version + +which conda +conda --version + +# Download the rest of the installer and UI scripts/on_env_start.sh