refactor
This commit is contained in:
parent
214091f580
commit
b108ff5996
@ -10,11 +10,12 @@ fi
|
||||
|
||||
# ============================================================
|
||||
|
||||
. $scriptdir/init_user.sh
|
||||
. $scriptdir/configure_user.sh
|
||||
|
||||
# ============================================================
|
||||
|
||||
configure_tuna() {
|
||||
[ $offline -eq 1 ] && return
|
||||
[ $abroad -eq 1 ] && return
|
||||
|
||||
if confirm_action "要切换 Tuna 源吗?" "N"; then
|
||||
@ -27,6 +28,8 @@ configure_tuna() {
|
||||
}
|
||||
|
||||
configure_upgrade() {
|
||||
[ $offline -eq 1 ] && return
|
||||
|
||||
if [ "$(uname)" = "Darwin" ]; then
|
||||
# macOS 系统使用 brew
|
||||
if confirm_action "要运行 brew upgrade 吗?" "N"; then
|
||||
@ -129,6 +132,7 @@ configure_vim() {
|
||||
|
||||
configure_zsh() {
|
||||
[ -f ~/.zshrc ] && return
|
||||
[ $offline -eq 1 ] && return
|
||||
|
||||
if confirm_action "要配置 zsh 吗?" "Y"; then
|
||||
pkg_install zsh
|
||||
@ -175,6 +179,7 @@ configure_zsh() {
|
||||
|
||||
configure_tmux() {
|
||||
[ -f ~/.tmux.conf ] && return
|
||||
[ $offline -eq 1 ] && return
|
||||
|
||||
if confirm_action "要配置 tmux 吗?" "Y"; then
|
||||
sudo apt install tmux -y
|
||||
@ -194,6 +199,7 @@ configure_tmux() {
|
||||
|
||||
configure_docker() {
|
||||
command_exists docker && return
|
||||
[ $offline -eq 1 ] && return
|
||||
|
||||
if confirm_action "要安装 docker 吗?" "N"; then
|
||||
# 卸载旧版本
|
||||
@ -248,8 +254,9 @@ configure_nginx() {
|
||||
}
|
||||
|
||||
configure_fail2ban() {
|
||||
[ "$(uname)" = "Darwin" ] && return
|
||||
[ -f /etc/fail2ban/jail.local ] && return
|
||||
[ $offline -eq 1 ] && return
|
||||
[ "$(uname)" = "Darwin" ] && return
|
||||
|
||||
if confirm_action "要配置 fail2ban 吗?" "N"; then
|
||||
sudo apt install fail2ban -y
|
||||
@ -263,6 +270,7 @@ configure_fail2ban() {
|
||||
|
||||
configure_iptables() {
|
||||
[ -f /etc/network/if-pre-up.d/iptables-load ] && return
|
||||
[ $offline -eq 1 ] && return
|
||||
|
||||
if confirm_action "要配置 iptables 吗?" "N"; then
|
||||
sudo cp $scriptdir/files/iptables-load /etc/network/if-pre-up.d/iptables-load
|
||||
@ -288,8 +296,8 @@ configure_timedate() {
|
||||
}
|
||||
|
||||
configure_bootinfo() {
|
||||
[ "$(uname)" = "Darwin" ] && return
|
||||
[ -d /etc/update-motd.d/disable ] && return
|
||||
[ "$(uname)" = "Darwin" ] && return
|
||||
|
||||
if confirm_action "要修改 启动信息 吗?" "Y"; then
|
||||
sudo mkdir /etc/update-motd.d/disable
|
||||
@ -304,8 +312,9 @@ configure_bootinfo() {
|
||||
}
|
||||
|
||||
configure_oomkiller() {
|
||||
[ "$(uname)" = "Darwin" ] && return
|
||||
[ -f /etc/default/earlyoom ] && return
|
||||
[ $offline -eq 1 ] && return
|
||||
[ "$(uname)" = "Darwin" ] && return
|
||||
|
||||
if confirm_action "要启用 earlyoom 吗?" "Y"; then
|
||||
sudo apt install earlyoom -y
|
||||
@ -315,8 +324,9 @@ configure_oomkiller() {
|
||||
}
|
||||
|
||||
configure_proxychains() {
|
||||
[ "$(uname)" = "Darwin" ] && return
|
||||
[ -f /etc/proxychains4.conf ] && return
|
||||
[ $offline -eq 1 ] && return
|
||||
[ "$(uname)" = "Darwin" ] && return
|
||||
|
||||
if confirm_action "要配置 proxychains 吗?" "N"; then
|
||||
sudo apt install proxychains4 -y
|
||||
@ -326,6 +336,7 @@ configure_proxychains() {
|
||||
|
||||
configure_nodejs() {
|
||||
[ -f /usr/local/bin/node ] && return
|
||||
[ $offline -eq 1 ] && return
|
||||
|
||||
if confirm_action "要安装 nodejs 吗?" "Y"; then
|
||||
local version="v20.10.0"
|
||||
@ -390,3 +401,4 @@ configure_hostname
|
||||
configure_timedate
|
||||
configure_bootinfo
|
||||
configure_oomkiller
|
||||
# configure_swap
|
||||
101
configure_user.sh
Executable file
101
configure_user.sh
Executable file
@ -0,0 +1,101 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
scriptdir=$(dirname $(realpath $0))
|
||||
tempdir=$(mktemp -d)
|
||||
|
||||
# ============================================================
|
||||
|
||||
. $scriptdir/utils.sh
|
||||
|
||||
# ============================================================
|
||||
|
||||
configure_ssh() {
|
||||
[ -f ~/.ssh/authorized_keys ] && return
|
||||
|
||||
if confirm_action "要配置 authorized_keys 吗?" "N"; then
|
||||
mkdir -p ~/.ssh
|
||||
cp $scriptdir/files/authorized_keys ~/.ssh/authorized_keys
|
||||
fi
|
||||
}
|
||||
|
||||
configure_ssh_keygen() {
|
||||
[ -f ~/.ssh/id_ed25519.pub ] && return
|
||||
[ -f ~/.ssh/id_rsa.pub ] && return
|
||||
|
||||
if confirm_action "要生成 sshkey 吗?" "N"; then
|
||||
mkdir -p ~/.ssh
|
||||
read -p "sshkey 的名字: " sshkeyname
|
||||
ssh-keygen -t ed25519 -C $sshkeyname
|
||||
fi
|
||||
}
|
||||
|
||||
configure_git() {
|
||||
[ -f ~/.gitconfig ] && return
|
||||
|
||||
if confirm_action "要配置 git 吗?" "N"; then
|
||||
cp $scriptdir/files/.gitconfig ~/.gitconfig
|
||||
fi
|
||||
}
|
||||
|
||||
configure_miniconda() {
|
||||
[ -d ~/.miniconda3 ] && return
|
||||
[ $offline -eq 1 ] && return
|
||||
[[ $(uname -m) == "aarch64" ]] && return
|
||||
|
||||
if confirm_action "要安装 miniconda 吗?" "N"; then
|
||||
wget "https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-$(uname -m).sh" -O $tempdir/miniconda.sh
|
||||
bash $tempdir/miniconda.sh -b -p ~/.miniconda3
|
||||
rm $tempdir/miniconda.sh
|
||||
~/.miniconda3/bin/conda init zsh
|
||||
~/.miniconda3/bin/conda config --set changeps1 False
|
||||
~/.miniconda3/bin/conda install -c conda-forge mamba
|
||||
~/.miniconda3/bin/mamba init
|
||||
fi
|
||||
}
|
||||
|
||||
configure_miniforge() {
|
||||
[ -d ~/.miniforge3 ] && return
|
||||
[ $offline -eq 1 ] && return
|
||||
[[ $(uname -m) != "aarch64" ]] && return
|
||||
|
||||
if confirm_action "要安装 miniforge 吗?" "N"; then
|
||||
wget "$github/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-$(uname -m).sh" -O $tempdir/miniforge.sh
|
||||
bash $tempdir/miniforge.sh -b -p ~/.miniforge3
|
||||
rm $tempdir/miniforge.sh
|
||||
~/.miniforge3/bin/conda config --set changeps1 False
|
||||
~/.miniforge3/bin/mamba init zsh
|
||||
fi
|
||||
}
|
||||
|
||||
configure_nvm() {
|
||||
[ -d ~/.nvm/.git ] && return
|
||||
[ $offline -eq 1 ] && return
|
||||
|
||||
if confirm_action "要安装 nvm 吗?" "N"; then
|
||||
git_clone nvm-sh/nvm ~/.nvm --depth=1
|
||||
\. $scriptdir/files/install-nvm.sh
|
||||
\. ~/.nvm/nvm.sh
|
||||
\. ~/.nvm/bash_completion
|
||||
nvm install lts/iron
|
||||
nvm alias default lts/iron
|
||||
nvm install-latest-npm
|
||||
fi
|
||||
}
|
||||
|
||||
# ============================================================
|
||||
|
||||
if [ $# -ne 0 ]; then
|
||||
for func in $@; do
|
||||
declare -F configure_$func >/dev/null || continue
|
||||
echo "Configuring $func..."
|
||||
eval "configure_$func"
|
||||
done
|
||||
return
|
||||
fi
|
||||
|
||||
configure_ssh
|
||||
configure_ssh_keygen
|
||||
configure_git
|
||||
configure_miniconda
|
||||
configure_miniforge
|
||||
configure_nvm
|
||||
@ -1,7 +1,6 @@
|
||||
# Pip3
|
||||
# Pip3 / UserMode
|
||||
if [ -d "$HOME/.local/bin" ]; then
|
||||
export PATH=$PATH:$HOME/.local/bin
|
||||
command_exists trash-put && alias rm='trash-put'
|
||||
fi
|
||||
|
||||
# Docker Rootless
|
||||
@ -9,7 +9,7 @@ fi
|
||||
|
||||
# ============================================================
|
||||
|
||||
. $scriptdir/init_priv.sh $@
|
||||
. $scriptdir/configure_priv.sh $@
|
||||
|
||||
# ============================================================
|
||||
|
||||
|
||||
@ -14,7 +14,7 @@ fi
|
||||
|
||||
# ============================================================
|
||||
|
||||
. $scriptdir/init_priv.sh $@
|
||||
. $scriptdir/configure_priv.sh $@
|
||||
|
||||
# ============================================================
|
||||
|
||||
|
||||
100
init_user.sh
Executable file → Normal file
100
init_user.sh
Executable file → Normal file
@ -1,97 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
scriptdir=$(dirname $(realpath $0))
|
||||
tempdir=$(mktemp -d)
|
||||
# tempdir=$(mktemp -d)
|
||||
|
||||
# ============================================================
|
||||
|
||||
. $scriptdir/utils.sh
|
||||
|
||||
# ============================================================
|
||||
|
||||
configure_ssh() {
|
||||
[ -f ~/.ssh/authorized_keys ] && return
|
||||
|
||||
if confirm_action "要配置 authorized_keys 吗?" "N"; then
|
||||
mkdir -p ~/.ssh
|
||||
cp $scriptdir/files/authorized_keys ~/.ssh/authorized_keys
|
||||
fi
|
||||
}
|
||||
|
||||
configure_ssh_keygen() {
|
||||
[ -f ~/.ssh/id_ed25519.pub ] && return
|
||||
|
||||
if confirm_action "要生成 sshkey 吗?" "N"; then
|
||||
mkdir -p ~/.ssh
|
||||
read -p "sshkey 的名字: " sshkeyname
|
||||
ssh-keygen -t ed25519 -C $sshkeyname
|
||||
fi
|
||||
}
|
||||
|
||||
configure_git() {
|
||||
[ -f ~/.gitconfig ] && return
|
||||
|
||||
if confirm_action "要配置 git 吗?" "N"; then
|
||||
cp $scriptdir/files/.gitconfig ~/.gitconfig
|
||||
fi
|
||||
}
|
||||
|
||||
configure_miniconda() {
|
||||
[ -d ~/.miniconda3 ] && return
|
||||
[[ $(uname -m) == "aarch64" ]] && return
|
||||
|
||||
if confirm_action "要安装 miniconda 吗?" "N"; then
|
||||
wget "https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-$(uname -m).sh" -O $tempdir/miniconda.sh
|
||||
bash $tempdir/miniconda.sh -b -p ~/.miniconda3
|
||||
rm $tempdir/miniconda.sh
|
||||
~/.miniconda3/bin/conda init zsh
|
||||
~/.miniconda3/bin/conda config --set changeps1 False
|
||||
~/.miniconda3/bin/conda install -c conda-forge mamba
|
||||
~/.miniconda3/bin/mamba init
|
||||
fi
|
||||
}
|
||||
|
||||
configure_miniforge() {
|
||||
[ -d ~/.miniforge3 ] && return
|
||||
[[ $(uname -m) != "aarch64" ]] && return
|
||||
|
||||
if confirm_action "要安装 miniforge 吗?" "N"; then
|
||||
wget "$github/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-$(uname -m).sh" -O $tempdir/miniforge.sh
|
||||
bash $tempdir/miniforge.sh -b -p ~/.miniforge3
|
||||
rm $tempdir/miniforge.sh
|
||||
~/.miniforge3/bin/conda config --set changeps1 False
|
||||
~/.miniforge3/bin/mamba init zsh
|
||||
fi
|
||||
}
|
||||
|
||||
configure_nvm() {
|
||||
[ -d ~/.nvm/.git ] && return
|
||||
|
||||
if confirm_action "要安装 nvm 吗?" "N"; then
|
||||
git_clone nvm-sh/nvm ~/.nvm --depth=1
|
||||
\. $scriptdir/files/install-nvm.sh
|
||||
\. ~/.nvm/nvm.sh
|
||||
\. ~/.nvm/bash_completion
|
||||
nvm install lts/iron
|
||||
nvm alias default lts/iron
|
||||
nvm install-latest-npm
|
||||
fi
|
||||
}
|
||||
|
||||
# ============================================================
|
||||
|
||||
if [ $# -ne 0 ]; then
|
||||
for func in $@; do
|
||||
declare -F configure_$func >/dev/null || continue
|
||||
echo "Configuring $func..."
|
||||
eval "configure_$func"
|
||||
done
|
||||
return
|
||||
if [[ $EUID -eq 0 ]]; then
|
||||
echo "This script should not be run as root."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
configure_ssh
|
||||
configure_ssh_keygen
|
||||
configure_git
|
||||
configure_miniconda
|
||||
configure_miniforge
|
||||
configure_nvm
|
||||
# ============================================================
|
||||
|
||||
. $scriptdir/configure_user.sh $@
|
||||
|
||||
# ============================================================
|
||||
|
||||
13
run.sh
13
run.sh
@ -21,10 +21,13 @@ fi
|
||||
if [ "$(id -u)" -eq 0 ]; then
|
||||
echo "Running in root mode."
|
||||
$scriptdir/init_root.sh $@
|
||||
elif [ "$(sudo id -u)" -eq 0 ]; then
|
||||
echo "Running in sudo mode."
|
||||
$scriptdir/init_sudo.sh $@
|
||||
else
|
||||
echo "Running in user mode."
|
||||
$scriptdir/init_user.sh $@
|
||||
sudo_id=$(sudo id -u 2>/dev/null)
|
||||
if [ $? -eq 0 ] && [ "$sudo_id" -eq 0 ]; then
|
||||
echo "Running in sudo mode."
|
||||
$scriptdir/init_sudo.sh $@
|
||||
else
|
||||
echo "Running in user mode."
|
||||
$scriptdir/init_user.sh $@
|
||||
fi
|
||||
fi
|
||||
|
||||
18
utils.sh
18
utils.sh
@ -15,9 +15,21 @@ git_clone() {
|
||||
fi
|
||||
}
|
||||
|
||||
check_google_access() {
|
||||
# offline: 是否离线
|
||||
# abroad: 是否海外
|
||||
|
||||
check_internet_access() {
|
||||
offline=0
|
||||
local response=$(curl -s -o /dev/null -w "%{http_code}" -m 3 "http://www.baidu.com")
|
||||
|
||||
if [ "$response" -ne 200 ]; then
|
||||
offline=1
|
||||
echo "Internet: offline"
|
||||
return
|
||||
fi
|
||||
|
||||
abroad=0
|
||||
local response=$(curl -s -o /dev/null -w "%{http_code}" -m 5 "http://www.google.com")
|
||||
local response=$(curl -s -o /dev/null -w "%{http_code}" -m 3 "http://www.google.com")
|
||||
|
||||
if [ "$response" -eq 200 ]; then
|
||||
abroad=1
|
||||
@ -59,4 +71,4 @@ pkg_install() {
|
||||
|
||||
# ============================================================
|
||||
|
||||
check_google_access
|
||||
check_internet_access
|
||||
|
||||
Loading…
Reference in New Issue
Block a user