Last active 6 months ago

git-reset.sh Raw
1#!/bin/bash
2
3git status
4
5echo
6read -p "Type 'y' to confirm reset and pull (Enter = yes, n = cancel): " confirm </dev/tty
7
8# Treat empty input (Enter) as "y"
9confirm="${confirm:-y}"
10
11if [[ "$confirm" == "y" ]]; then
12 echo "Proceeding with reset and pull..."
13 git reset --hard HEAD && git clean -fd && git fetch origin && git pull origin main
14 git status
15elif [[ "$confirm" == "n" ]]; then
16 echo "Operation cancelled by user (n)."
17else
18 echo "Operation cancelled. Only 'y' or Enter will proceed."
19fi
install-rpm.sh Raw
1#!/bin/bash
2
3# 定义安装命令的函数,接收 user 和 repo 作为参数
4case "$(uname -m)" in
5 x86_64)
6 arch="amd64"
7 ;;
8 aarch64)
9 arch="arm64"
10 ;;
11 *)
12 echo "Unsupported architecture: $(uname -m)"
13 exit 1
14 ;;
15esac
16
17if [ "$2" = "-U" ]; then
18 case "$1" in
19 "helm" | "k9s" | "govc" | "nerdctl" | "fzf" | "terraform" | "packer" )
20 rm -rf /usr/local/bin/"$1" "${fpath[0]}"/_"$1" /etc/bash_completion.d/"$1"
21 echo
22 echo "-------------------------------------------"
23 echo "$1 installation complete!"
24 echo
25 echo "-------------------------------------------"
26 echo
27 ;;
28 "trzsz" )
29 rm -rf /usr/local/bin/"trzsz" /usr/local/bin/"trz" /usr/local/bin/"tsz" "${fpath[0]}"/_"$1" /etc/bash_completion.d/"trzsz"
30 echo
31 echo "-------------------------------------------"
32 echo "$1 installation complete!"
33 echo
34 echo "-------------------------------------------"
35 echo
36 ;;
37 *)
38 echo "Unsupported package: $1"
39 exit 1
40 ;;
41 esac
42 exit 0
43else
44 case "$1" in
45 "helm")
46 # helm
47 echo
48 echo "Installing helm..."
49 version=$(curl -s https://get.helm.sh/helm-latest-version)
50 curl -s -L -o - "https://get.helm.sh/helm-${version}-linux-${arch}.tar.gz" | tar --strip-components=1 -C /usr/local/bin -xzf - linux-amd64/helm
51 helm completion zsh > "${fpath[1]}/_helm" || true
52 helm completion bash > /etc/bash_completion.d/helm
53 # Displaying post-installation instructions
54 echo
55 echo "-------------------------------------------"
56 echo "helm installation complete!"
57 echo
58 # Displaying version
59 helm version
60 echo
61 echo "-------------------------------------------"
62 echo
63 ;;
64 "k9s")
65 # derailed k9s
66 echo
67 echo "Installing k9s..."
68 curl -s -L -o - "https://github.com/derailed/k9s/releases/latest/download/k9s_Linux_${arch}.tar.gz" | tar -C /usr/local/bin -xzf - k9s
69 # Displaying post-installation instructions
70 echo
71 echo "-------------------------------------------"
72 echo "k9s installation complete!"
73 echo
74 # Displaying version
75 k9s version
76 echo
77 echo "-------------------------------------------"
78 echo
79 ;;
80 "govc")
81 # vmware govmomi
82 echo
83 echo "Installing govmomi..."
84 curl -s -L -o - "https://github.com/vmware/govmomi/releases/latest/download/govc_$(uname -s)_$(uname -m).tar.gz" | tar -C /usr/local/bin -xvzf - govc
85 # Displaying post-installation instructions
86 echo
87 echo "-------------------------------------------"
88 echo "nerdctl installation complete!"
89 echo
90 # Displaying version
91 govc version
92 echo
93 echo "To initialize the environment, please run:"
94 echo " export GOVC_URL=\"https://administrator@vsphere.local:password@vcip\""
95 echo " export GOVC_INSECURE=true"
96 echo "-------------------------------------------"
97 echo
98 ;;
99 "nerdctl")
100 # vmware nerdctl
101 echo
102 echo "Installing nerdctl..."
103 VERSION=$(curl -s https://api.github.com/repos/containerd/nerdctl/releases/latest | jq -r '.tag_name' | awk -F"v" '{print $NF}')
104 curl -s -L -o - "https://github.com/containerd/nerdctl/releases/latest/download/nerdctl-${VERSION}-linux-${arch}.tar.gz" | tar -C /usr/local/bin -xvzf - nerdctl
105 nerdctl completion zsh > "${fpath[1]}/_nerdctl" || true
106 nerdctl completion bash > /etc/bash_completion.d/nerdctl
107 # Displaying post-installation instructions
108 echo
109 echo "-------------------------------------------"
110 echo "nerdctl installation complete!"
111 echo
112 # Displaying version
113 nerdctl version
114 echo
115 echo "-------------------------------------------"
116 echo
117 ;;
118 "fzf")
119 # fzf
120 echo
121 echo "Installing fzf..."
122 VERSION=$(curl -s https://api.github.com/repos/junegunn/fzf/releases/latest | jq -r '.tag_name' | awk -F"v" '{print $NF}')
123 curl -s -L -o - "https://github.com/junegunn/fzf/releases/latest/download/fzf-${VERSION}-linux_${arch}.tar.gz" | tar -C /usr/local/bin -xvzf - fzf
124 fzf --zsh > "${fpath[1]}/_fzf" || true
125 fzf --bash > /etc/bash_completion.d/fzf
126 # Displaying post-installation instructions
127 echo
128 echo "-------------------------------------------"
129 echo "fzf installation complete!"
130 echo
131 # Displaying version
132 fzf --version
133 echo
134 echo "-------------------------------------------"
135 echo
136 ;;
137 "terraform")
138 # terraform
139 echo
140 echo "Installing Terraform..."
141 # Fetching the latest version of Terraform
142 VERSION=$(curl -s https://checkpoint-api.hashicorp.com/v1/check/terraform | jq -r -M '.current_version')
143 curl -s -L -o "terraform_${VERSION}_linux_${arch}.zip" "https://releases.hashicorp.com/terraform/${VERSION}/terraform_${VERSION}_linux_${arch}.zip"
144 # Unzipping and installing Terraform
145 unzip -qq -o "terraform_${VERSION}_linux_${arch}.zip" terraform -d /usr/local/bin && rm -f "terraform_${VERSION}_linux_${arch}.zip"
146 # Displaying post-installation instructions
147 echo
148 echo "-------------------------------------------"
149 echo "Terraform installation complete!"
150 echo
151 # Displaying terraform version
152 terraform --version
153 echo
154 echo "To enable CLI auto-completion, please run:"
155 echo " terraform -install-autocomplete"
156 echo "-------------------------------------------"
157 echo
158 ;;
159 "packer")
160 # packer
161 echo
162 echo "Installing Packer..."
163 # Fetching the latest version of packer
164 VERSION=$(curl -s https://checkpoint-api.hashicorp.com/v1/check/packer | jq -r -M '.current_version')
165 curl -s -L -o "packer_${VERSION}_linux_${arch}.zip" "https://releases.hashicorp.com/packer/${VERSION}/packer_${VERSION}_linux_${arch}.zip"
166 # Unzipping and installing packer
167 unzip -qq -o "packer_${VERSION}_linux_${arch}.zip" packer -d /usr/local/bin && rm -rf "packer_${VERSION}_linux_${arch}.zip" /usr/sbin/packer
168 # Displaying post-installation instructions
169 echo
170 echo "-------------------------------------------"
171 echo "Packer installation complete!"
172 echo
173 # Displaying packer version
174 packer --version
175 echo
176 echo "To enable CLI auto-completion, please run:"
177 echo " packer -autocomplete-install"
178 echo "-------------------------------------------"
179 echo
180 ;;
181 "trzsz")
182 # trzsz
183 echo
184 echo "Installing trzsz..."
185 curl -s -L -o - "https://github.com/trzsz/trzsz-go/releases/download/v1.1.8/trzsz_1.1.8_linux_$(uname -m).tar.gz" | tar --strip-components=1 -C /usr/local/bin -xvzf -
186 # Displaying post-installation instructions
187 echo
188 echo "-------------------------------------------"
189 echo "trzsz installation complete!"
190 echo
191 # Displaying version
192 /usr/local/bin/trzsz version
193 echo
194 ;;
195 *)
196 echo
197 echo "helm k9s govc nerdctl fzf terraform packer trzsz"
198 exit 1
199 ;;
200 esac
201 exit 0
202fi
install_ohmyzsh.sh Raw
1#!/bin/bash
2
3# check sudo
4if command -v sudo &>/dev/null; then
5 SUDO=sudo
6else
7 SUDO=""
8fi
9
10# install package
11if command -v yum &>/dev/null; then
12 $SUDO yum install -y -q git zsh bash-completion wget unzip tree tmux vim sysstat &> /dev/null
13elif command -v apt &>/dev/null; then
14 $SUDO apt update -qq &>/dev/null
15 $SUDO apt install -y git zsh bash-completion wget unzip tree tmux vim sysstat &> /dev/null
16else
17 echo "[ERROR] unsupport os" >&2
18 exit 1
19fi
20
21# install oh-my-zsh
22git clone https://mirrors.tuna.tsinghua.edu.cn/git/ohmyzsh.git
23REMOTE=https://mirrors.tuna.tsinghua.edu.cn/git/ohmyzsh.git sh ohmyzsh/tools/install.sh && rm -rf ohmyzsh
24
25ZSH=$HOME/.oh-my-zsh
26PROXY=https://pp.ukto.top
27
28# install zsh-syntax-highlighting
29# git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
30git clone https://mirrors.nju.edu.cn/git/zsh-syntax-highlighting.git $ZSH/custom/plugins/zsh-syntax-highlighting
31# echo "source $ZSH_CUSTOM/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ${ZDOTDIR:-$HOME}/.zshrc
32
33# install zsh-autosuggestions
34# git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
35git clone https://mirrors.nju.edu.cn/git/zsh-autosuggestions.git $ZSH/custom/plugins/zsh-autosuggestions
36# echo "source $ZSH_CUSTOM/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh" >> ${ZDOTDIR:-$HOME}/.zshrc
37
38# install zsh-you-should-use
39# git clone https://github.com/MichaelAquilina/zsh-you-should-use.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/you-should-use
40git clone $PROXY/https://github.com/MichaelAquilina/zsh-you-should-use.git $ZSH/custom/plugins/you-should-use
41
42# replace plugins and theme
43cp $HOME/.zshrc $HOME/.zshrc.bak
44zsh -c 'source ~/.zshrc && omz plugin enable zsh-autosuggestions zsh-syntax-highlighting sudo you-should-use'
45zsh -c 'source ~/.zshrc && omz theme set ys'
46# sed -i 's/(git/(git zsh-syntax-highlighting zsh-autosuggestions sudo/g' ~/.zshrc
47# sed -i 's/ZSH_THEME="robbyrussell"/ZSH_THEME="ys"/g' ~/.zshrc
48
49cat >> ~/.zshrc <<UK
50
51## add
52zstyle ':omz:update' mode disabled
53HIST_STAMPS="yyyy-mm-dd"
54alias vi="vim"
55UK
install_vmtools.sh Raw
1#!/bin/bash
2set -e
3
4# Define variables
5ISO_FILENAME="SMTX_VMTOOLS-4.0.0-2506271023.iso"
6ISO_FILE="/tmp/${ISO_FILENAME}"
7MOUNT_POINT="/mnt/wuke-vmtools"
8
9# Extract version from ISO filename
10VMTOOLS_VERSION=$(echo "$ISO_FILENAME" | awk -F'-' '{print $2}')
11echo "Extracted VMTools version: $VMTOOLS_VERSION"
12
13# Detect gateway and set ISO_URL
14GATEWAY=$(ip route | grep default | awk '{print $3}')
15echo "Detected gateway: $GATEWAY"
16
17if [[ $GATEWAY == 172.29.16.* ]]; then
18 ISO_URL="http://172.29.16.5/VMTOOLS/${VMTOOLS_VERSION}/${ISO_FILENAME}"
19elif [[ $GATEWAY == 172.29.48.* ]]; then
20 ISO_URL="http://172.29.48.5/VMTOOLS/${VMTOOLS_VERSION}/${ISO_FILENAME}"
21elif [[ $GATEWAY == 172.29.80.* ]]; then
22 ISO_URL="http://172.29.80.5/VMTOOLS/${VMTOOLS_VERSION}/${ISO_FILENAME}"
23elif [[ $GATEWAY == 172.29.112.* ]]; then
24 ISO_URL="http://172.29.112.5/VMTOOLS/${VMTOOLS_VERSION}/${ISO_FILENAME}"
25elif [[ $GATEWAY == 192.168.48.* ]]; then
26 ISO_URL="http://192.168.48.2/VMTOOLS/${VMTOOLS_VERSION}/${ISO_FILENAME}"
27else
28 echo "Unsupported gateway network: $GATEWAY"
29 exit 1
30fi
31
32echo "Selected ISO URL: $ISO_URL"
33
34# Function to check and install a package if missing
35install_package_if_missing() {
36 local package_name=$1
37
38 if command -v yum >/dev/null 2>&1; then
39 if ! rpm -q "$package_name" >/dev/null 2>&1; then
40 echo "Installing $package_name using yum..."
41 yum install -y "$package_name"
42 else
43 echo "Package $package_name already installed (yum)."
44 fi
45 elif command -v apt >/dev/null 2>&1; then
46 if ! dpkg -s "$package_name" >/dev/null 2>&1; then
47 echo "Installing $package_name using apt..."
48 apt update
49 apt install -y "$package_name"
50 else
51 echo "Package $package_name already installed (apt)."
52 fi
53 else
54 echo "Unsupported OS: yum or apt not found."
55 exit 1
56 fi
57}
58
59# 1. Install required packages
60install_package_if_missing tar
61install_package_if_missing bzip2
62install_package_if_missing curl
63
64# 2. Check if ISO URL is accessible
65echo "Checking if ISO URL is accessible..."
66if ! curl --head --silent --fail "$ISO_URL" >/dev/null; then
67 echo "Error: ISO URL $ISO_URL is not accessible."
68 exit 1
69fi
70
71# 3. Download the ISO file
72echo "Downloading VMTools ISO..."
73curl -o "$ISO_FILE" "$ISO_URL"
74
75# 4. Check if /mnt/wuke-vmtools exists
76if [ -d "$MOUNT_POINT" ]; then
77 echo "Directory $MOUNT_POINT exists, removing..."
78 umount "$MOUNT_POINT" || true
79 rm -rf "$MOUNT_POINT"
80fi
81
82echo "Creating directory $MOUNT_POINT..."
83mkdir -p "$MOUNT_POINT"
84
85# 5. Mount the ISO
86echo "Mounting ISO file to $MOUNT_POINT..."
87mount -o loop "$ISO_FILE" "$MOUNT_POINT"
88
89# 6. Execute the installation script
90echo "Running VMTools installation script..."
91bash "$MOUNT_POINT/SMTX_VM_TOOLS_INSTALL.sh"
92
93# 7. Cleanup
94echo "Cleaning up temporary files..."
95umount "$MOUNT_POINT" || true
96rm -f "$ISO_FILE"
97rm -rf "$MOUNT_POINT"
98
99echo "Installation and cleanup completed."
packer_rename-no-mf.sh Raw
1#!/bin/bash
2
3set -o errexit
4set -o nounset
5set -o pipefail
6
7VM_NEW_NAME=$1
8
9# 提取当前OVF文件名
10VM_NOW_NAME=$(basename *.ovf | awk -F '.ovf' '{print $1}')
11
12# 重命名OVF文件和VMDK文件
13mv "$VM_NOW_NAME".ovf "$VM_NEW_NAME".ovf
14mv "$VM_NOW_NAME"-1.vmdk "$VM_NEW_NAME".vmdk
15
16# 在OVF文件中替换名称
17sed -i "s/$VM_NOW_NAME/$VM_NEW_NAME/g" "$VM_NEW_NAME".ovf
18sed -i "s/${VM_NEW_NAME}-1/$VM_NEW_NAME/g" "$VM_NEW_NAME".ovf
19
20echo "#### PACKER_COMPLETE ####"
21
22# 脚本执行完以后,删除本脚本
23rm -f "$0"
packer_rename.sh Raw
1#!/bin/bash
2
3set -o errexit
4set -o nounset
5set -o pipefail
6
7VM_NEW_NAME=$1
8
9# 提取当前OVF文件名
10VM_NOW_NAME=$(basename *.ovf | awk -F '.ovf' '{print $1}')
11
12# 重命名OVF文件和VMDK文件
13mv "$VM_NOW_NAME".ovf "$VM_NEW_NAME".ovf
14mv "$VM_NOW_NAME"-1.vmdk "$VM_NEW_NAME".vmdk
15
16# 在OVF文件中替换名称
17sed -i "s/$VM_NOW_NAME/$VM_NEW_NAME/g" "$VM_NEW_NAME".ovf
18sed -i "s/${VM_NEW_NAME}-1/$VM_NEW_NAME/g" "$VM_NEW_NAME".ovf
19
20# 增加 mf 文件
21openssl sha256 "$VM_NEW_NAME".vmdk "$VM_NEW_NAME".ovf > "$VM_NEW_NAME".mf
22
23# 导出成 OVA
24tar -cf "$VM_NEW_NAME".ova "$VM_NEW_NAME".ovf "$VM_NEW_NAME".vmdk "$VM_NEW_NAME".mf
25
26echo "#### PACKER_COMPLETE ####"
27
28# 脚本执行完以后,删除本脚本
29rm -f "$0"
set-docker-prosy.sh Raw
1#!/bin/bash
2
3# 获取默认网关地址
4GATEWAY=$(ip route | awk '/default/ {print $3}' | uniq | head -n 1)
5
6# 创建配置目录
7sudo mkdir -p /etc/systemd/system/docker.service.d
8
9# 根据默认网关设置对应的代理地址
10case "$GATEWAY" in
11 192.168.48.1)
12 PROXY="http://192.168.48.48:7891"
13 ;;
14 172.29.48.1)
15 PROXY="http://172.29.48.3:7891"
16 ;;
17 172.29.16.1)
18 PROXY="http://172.29.16.3:7891"
19 ;;
20 172.29.80.1)
21 PROXY="http://172.29.80.3:7891"
22 ;;
23 172.29.112.1)
24 PROXY="http://172.29.112.3:7891"
25 ;;
26 *)
27 echo "未识别的默认网关: $GATEWAY"
28 exit 1
29 ;;
30esac
31
32# 写入 docker 的代理配置
33sudo bash -c "cat << UK > /etc/systemd/system/docker.service.d/http-proxy.conf
34[Service]
35Environment=\"HTTP_PROXY=$PROXY\"
36Environment=\"HTTPS_PROXY=$PROXY\"
37Environment=\"NO_PROXY=localhost,127.0.0.1,10.0.0.0/8,192.168.0.0/16,10.0.0.0/8,172.29.0.0/16,localhost,registry.smtx.io\"
38UK"
39
40# 应用更改
41sudo systemctl daemon-reload
42sudo systemctl restart docker
43
44# 检查代理配置是否生效
45docker info | grep -i proxy
46
snippetfile1.txt Raw
1#!/bin/bash
2
3set -o errexit
4set -o nounset
5set -o pipefail
6
7VM_NEW_NAME=$1
8
9# 进入 WORKDIR
10cd assets
11
12# 提取当前OVF文件名
13VM_NOW_NAME=$(basename packer-*.ovf | awk -F. '{print $1}')
14
15# 重命名OVF文件和VMDK文件
16mv "$VM_NOW_NAME".ovf "$VM_NEW_NAME".ovf
17mv "$VM_NOW_NAME"-1.vmdk "$VM_NEW_NAME"-1.vmdk
18
19# 在OVF文件中替换名称
20sed -i "s/$VM_NOW_NAME/$VM_NEW_NAME/g" "$VM_NEW_NAME".ovf
21
22# 导出成 OVA
23tar -cf "$VM_NEW_NAME".ova "$VM_NEW_NAME".ovf "$VM_NEW_NAME"-1.vmdk
24
25echo "#### PACKER_COMPLETE ####"
tower_shutdown_delete-old-vm.sh Raw
1#!/bin/bash
2
3set -o errexit
4set -o nounset
5set -o pipefail
6
7export TOWER_HOST=$TOWER_HOST
8export TOWER_USERNAME=$TOWER_USERNAME
9export TOWER_PASSWORD=$TOWER_PASSWORD
10export VM_NAME=$VM_NAME
11export TOWER_SOURCE=LOCAL
12
13env
14
15# 下载二进制命令并授予执行权限
16wget -O /tmp/shutdown-vm http://192.168.48.2/Software/bin/tower/shutdown-vm-alpine
17wget -O /tmp/delete-vm http://192.168.48.2/Software/bin/tower/delete-vm-alpine
18chmod +x /tmp/shutdown-vm /tmp/delete-vm
19
20# 强制关机
21/tmp/shutdown-vm -force -vm $VM_NAME || true
22
23# 删除虚拟机并移入回收站
24/tmp/delete-vm -recycle-bin -vm $VM_NAME || true
25
26# 脚本执行完以后,删除本脚本
27rm -f "$0"