Update jupyter.sh

This commit is contained in:
spiritlhl 2023-07-26 16:59:16 +08:00 committed by GitHub
parent 88da3399f4
commit e8a3f3dc04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,6 +21,23 @@ red(){ echo -e "\033[31m\033[01m$1$2\033[0m"; }
green(){ echo -e "\033[32m\033[01m$1$2\033[0m"; } green(){ echo -e "\033[32m\033[01m$1$2\033[0m"; }
yellow(){ echo -e "\033[33m\033[01m$1$2\033[0m"; } yellow(){ echo -e "\033[33m\033[01m$1$2\033[0m"; }
reading(){ read -rp "$(green "$1")" "$2"; } reading(){ read -rp "$(green "$1")" "$2"; }
temp_file_apt_fix="apt_fix.txt"
REGEX=("debian|astra" "ubuntu" "centos|red hat|kernel|oracle linux|alma|rocky" "'amazon linux'" "fedora" "arch" "freebsd")
RELEASE=("Debian" "Ubuntu" "CentOS" "CentOS" "Fedora" "Arch" "FreeBSD")
PACKAGE_UPDATE=("! apt-get update && apt-get --fix-broken install -y && apt-get update" "apt-get update" "yum -y update" "yum -y update" "yum -y update" "pacman -Sy" "pkg update")
PACKAGE_INSTALL=("apt-get -y install" "apt-get -y install" "yum -y install" "yum -y install" "yum -y install" "pacman -Sy --noconfirm --needed" "pkg install -y")
PACKAGE_REMOVE=("apt-get -y remove" "apt-get -y remove" "yum -y remove" "yum -y remove" "yum -y remove" "pacman -Rsc --noconfirm" "pkg delete")
PACKAGE_UNINSTALL=("apt-get -y autoremove" "apt-get -y autoremove" "yum -y autoremove" "yum -y autoremove" "yum -y autoremove" "" "pkg autoremove")
CMD=("$(grep -i pretty_name /etc/os-release 2>/dev/null | cut -d \" -f2)" "$(hostnamectl 2>/dev/null | grep -i system | cut -d : -f2)" "$(lsb_release -sd 2>/dev/null)" "$(grep -i description /etc/lsb-release 2>/dev/null | cut -d \" -f2)" "$(grep . /etc/redhat-release 2>/dev/null)" "$(grep . /etc/issue 2>/dev/null | cut -d \\ -f1 | sed '/^[ ]*$/d')" "$(grep -i pretty_name /etc/os-release 2>/dev/null | cut -d \" -f2)" "$(uname -s)")
SYS="${CMD[0]}"
[[ -n $SYS ]] || exit 1
for ((int = 0; int < ${#REGEX[@]}; int++)); do
if [[ $(echo "$SYS" | tr '[:upper:]' '[:lower:]') =~ ${REGEX[int]} ]]; then
SYSTEM="${RELEASE[int]}"
[[ -n $SYSTEM ]] && break
fi
done
clear clear
echo "#######################################################################" echo "#######################################################################"
echo "# ${YELLOW}一键安装jupyter环境${PLAIN} #" echo "# ${YELLOW}一键安装jupyter环境${PLAIN} #"
@ -79,16 +96,71 @@ check_china(){
fi fi
} }
check_update(){
yellow "Updating package management sources"
if command -v apt-get > /dev/null 2>&1; then
apt_update_output=$(apt-get update 2>&1)
echo "$apt_update_output" > "$temp_file_apt_fix"
if grep -q 'NO_PUBKEY' "$temp_file_apt_fix"; then
public_keys=$(grep -oE 'NO_PUBKEY [0-9A-F]+' "$temp_file_apt_fix" | awk '{ print $2 }')
joined_keys=$(echo "$public_keys" | paste -sd " ")
yellow "No Public Keys: ${joined_keys}"
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ${joined_keys}
apt-get update
if [ $? -eq 0 ]; then
green "Fixed"
fi
fi
rm "$temp_file_apt_fix"
else
${PACKAGE_UPDATE[int]}
fi
rm -rf $temp_file_apt_fix
}
check_sudo() {
yellow "checking sudo"
if ! command -v sudo > /dev/null 2>&1; then
yellow "Installing sudo"
${PACKAGE_INSTALL[int]} sudo > /dev/null 2>&1
fi
}
check_wget() {
if ! which wget >/dev/null; then
yellow "Installing wget"
${PACKAGE_INSTALL[int]} wget
fi
}
check_curl() {
if ! which curl >/dev/null; then
yellow "Installing curl"
${PACKAGE_INSTALL[int]} curl
fi
if [ $? -ne 0 ]; then
apt-get -f install > /dev/null 2>&1
${PACKAGE_INSTALL[int]} curl
fi
}
check_ufw() {
if ! which ufw >/dev/null; then
yellow "Installing ufw"
${PACKAGE_INSTALL[int]} ufw
fi
}
install_jupyter() { install_jupyter() {
rm -rf Miniconda3-latest-Linux-x86_64.sh* rm -rf Miniconda3-latest-Linux-x86_64.sh*
check_update
# Check if conda is already installed check_sudo
check_wget
check_curl
check_ufw
if ! command -v conda &> /dev/null; then if ! command -v conda &> /dev/null; then
# Install conda
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh -b -u bash Miniconda3-latest-Linux-x86_64.sh -b -u
# added by Miniconda3 installer
echo 'export PATH="$PATH:$HOME/miniconda3/bin:$HOME/miniconda3/condabin"' >> ~/.bashrc echo 'export PATH="$PATH:$HOME/miniconda3/bin:$HOME/miniconda3/condabin"' >> ~/.bashrc
echo 'export PATH="$PATH:$HOME/.local/share/jupyter"' >> ~/.bashrc echo 'export PATH="$PATH:$HOME/.local/share/jupyter"' >> ~/.bashrc
source ~/.bashrc source ~/.bashrc
@ -96,14 +168,10 @@ install_jupyter() {
echo 'export PATH="/home/user/miniconda3/bin:$PATH"' >> ~/.bashrc echo 'export PATH="/home/user/miniconda3/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc source ~/.bashrc
sleep 1 sleep 1
# Add the necessary paths to your search path
export PATH="/home/user/miniconda3/bin:$PATH" export PATH="/home/user/miniconda3/bin:$PATH"
green "请关闭本窗口开一个新窗口再执行本脚本,否则无法加载一些预设的环境变量" && exit 0 green "请关闭本窗口开一个新窗口再执行本脚本,否则无法加载一些预设的环境变量" && exit 0
fi fi
green "加载预设的conda环境变量成功准备安装jupyter无脑输入y和回车即可" green "加载预设的conda环境变量成功准备安装jupyter无脑输入y和回车即可"
# Create a new conda environment and install jupyter
conda create -n jupyter-env python=3 conda create -n jupyter-env python=3
sleep 5 sleep 5
source activate jupyter-env source activate jupyter-env
@ -114,36 +182,24 @@ install_jupyter() {
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes conda config --set show_channel_urls yes
fi fi
# Add the following line to /etc/profile
echo 'export PATH="$PATH:~/.local/share/jupyter"' >> /etc/profile echo 'export PATH="$PATH:~/.local/share/jupyter"' >> /etc/profile
# Execute the configuration
source /etc/profile source /etc/profile
# Set username and password for Jupyter Server
# jupyter notebook --generate-config # jupyter notebook --generate-config
# cp ~/.jupyter/jupyter_notebook_config.py ~/.jupyter/jupyter_server_config.py # cp ~/.jupyter/jupyter_notebook_config.py ~/.jupyter/jupyter_server_config.py
jupyter server --generate-config jupyter server --generate-config
# echo "c.ServerApp.password = 'spiritlhl'" >> ~/.jupyter/jupyter_server_config.py # echo "c.ServerApp.password = 'spiritlhl'" >> ~/.jupyter/jupyter_server_config.py
# echo "c.ServerApp.username = 'spiritlhl'" >> ~/.jupyter/jupyter_server_config.py # echo "c.ServerApp.username = 'spiritlhl'" >> ~/.jupyter/jupyter_server_config.py
# Open port 13692 in firewall
if command -v ufw &> /dev/null; then if command -v ufw &> /dev/null; then
sudo ufw allow 13692/tcp sudo ufw allow 13692/tcp
elif command -v firewall-cmd &> /dev/null; then elif command -v firewall-cmd &> /dev/null; then
sudo firewall-cmd --add-port=13692/tcp --permanent sudo firewall-cmd --add-port=13692/tcp --permanent
sudo firewall-cmd --reload sudo firewall-cmd --reload
fi fi
# Get the current system name
ubuntu_version=$(lsb_release -rs) ubuntu_version=$(lsb_release -rs)
# Check if the Ubuntu version is 18.04, 20.04, or 22.04
if [ "$ubuntu_version" == "18.04" ] || [ "$ubuntu_version" == "20.04" ] || [ "$ubuntu_version" == "22.04" ]; then if [ "$ubuntu_version" == "18.04" ] || [ "$ubuntu_version" == "20.04" ] || [ "$ubuntu_version" == "22.04" ]; then
# The system is Ubuntu 18.04, 20.04, or 22.04
source activate jupyter-env source activate jupyter-env
sleep 1 sleep 1
rm -rf nohup.out rm -rf nohup.out
# Start Jupyter Server with port 13692 and host 0.0.0.0
green "后台执行的pid的进程ID和输出日志文件名字如下" green "后台执行的pid的进程ID和输出日志文件名字如下"
nohup jupyter lab --port 13692 --no-browser --ip=0.0.0.0 --allow-root & green $! nohup jupyter lab --port 13692 --no-browser --ip=0.0.0.0 --allow-root & green $!
sleep 5 sleep 5
@ -158,18 +214,11 @@ install_jupyter() {
yellow "cat nohup.out" yellow "cat nohup.out"
echo "非Ubuntu系统只有上面这样才能手动挂起jupyter后台执行" echo "非Ubuntu系统只有上面这样才能手动挂起jupyter后台执行"
fi fi
# Add the specified paths to the PATH variable
paths="./miniconda3/envs/jupyter-env/etc/jupyter:./miniconda3/envs/jupyter-env/bin/jupyter:./miniconda3/envs/jupyter-env/share/jupyter" paths="./miniconda3/envs/jupyter-env/etc/jupyter:./miniconda3/envs/jupyter-env/bin/jupyter:./miniconda3/envs/jupyter-env/share/jupyter"
export PATH="$paths:$PATH" export PATH="$paths:$PATH"
# Remove duplicate paths from the PATH variable
new_path=$(echo "$PATH" | tr ':' '\n' | awk '!x[$0]++' | tr '\n' ':') new_path=$(echo "$PATH" | tr ':' '\n' | awk '!x[$0]++' | tr '\n' ':')
export PATH="$new_path" export PATH="$new_path"
# Refresh the current shell
source ~/.bashrc source ~/.bashrc
green "已安装jupyter lab的web端到外网端口13692上请打开你的 外网IP:13692" green "已安装jupyter lab的web端到外网端口13692上请打开你的 外网IP:13692"
green "初次安装会要求输入token设置密码token详见上方打印信息或当前目录的nohup.out日志" green "初次安装会要求输入token设置密码token详见上方打印信息或当前目录的nohup.out日志"
green "同时已保存日志输出到当前目录的nohup.out中且已打印5秒日志如上" green "同时已保存日志输出到当前目录的nohup.out中且已打印5秒日志如上"
@ -180,21 +229,17 @@ install_jupyter() {
query_jupyter_info() { query_jupyter_info() {
source activate jupyter-env > /dev/null 2>&1 source activate jupyter-env > /dev/null 2>&1
# Check if jupyter is installed
if ! jupyter --version &> /dev/null; then if ! jupyter --version &> /dev/null; then
echo "Error: Jupyter is not installed on this system." echo "Error: Jupyter is not installed on this system."
return 1 return 1
fi fi
source activate jupyter-env && jupyter server list && conda deactivate source activate jupyter-env && jupyter server list && conda deactivate
green "已查询登陆信息如上" green "已查询登陆信息如上"
green "如果想要手动查询,输入 source activate jupyter-env && jupyter server list && conda deactivate 即可查询" green "如果想要手动查询,输入 source activate jupyter-env && jupyter server list && conda deactivate 即可查询"
} }
main() { main() {
source activate jupyter-env > /dev/null 2>&1 source activate jupyter-env > /dev/null 2>&1
# Check if jupyter is installed
if jupyter --version &> /dev/null; then if jupyter --version &> /dev/null; then
green "Jupyter is already installed on this system." green "Jupyter is already installed on this system."
if ! (nc -z localhost 13692) > /dev/null 2>&1 if ! (nc -z localhost 13692) > /dev/null 2>&1
@ -209,15 +254,11 @@ main() {
else else
reading "Jupyter is not installed on this system. Do you want to install it? (y/n) " confirminstall reading "Jupyter is not installed on this system. Do you want to install it? (y/n) " confirminstall
echo "" echo ""
# Check user's input and exit if they do not want to proceed
if [ "$confirminstall" != "y" ]; then if [ "$confirminstall" != "y" ]; then
exit 0 exit 0
fi fi
install_jupyter install_jupyter
fi fi
# Print the current info for Jupyter
green "The current info for Jupyter:" green "The current info for Jupyter:"
query_jupyter_info query_jupyter_info
} }