From 1d33275991736f322dda4492c5eed0dba7609e68 Mon Sep 17 00:00:00 2001 From: IceDerce Date: Thu, 22 Dec 2022 16:15:39 +0800 Subject: [PATCH] [ agent ] [ boot ] - agent boot up procedure --- .../AutoDeployOctopusAgent.sh | 3 +- .../bootup/{ => deprecated}/BootUpAgent.sh | 0 .../bootup/{ => deprecated}/MaualRunAgent.sh | 0 .../{ => manual}/entrypoint.sh | 0 .../{ => manual}/libbitnami.sh | 0 .../bitnamiredisshell/{ => manual}/libfile.sh | 0 .../bitnamiredisshell/{ => manual}/libfs.sh | 0 .../bitnamiredisshell/{ => manual}/libhook.sh | 0 .../bitnamiredisshell/{ => manual}/liblog.sh | 0 .../bitnamiredisshell/{ => manual}/libnet.sh | 0 .../bitnamiredisshell/{ => manual}/libos.sh | 0 .../{ => manual}/libpersistence.sh | 0 .../{ => manual}/libredis.sh | 0 .../{ => manual}/libservice.sh | 0 .../{ => manual}/libvalidations.sh | 0 .../{ => manual}/libversion.sh | 0 .../{ => manual}/libwebserver.sh | 0 .../{ => manual}/postunpack.sh | 0 .../{ => manual}/redis-env.sh | 0 .../bitnamiredisshell/{ => manual}/run.sh | 0 .../bitnamiredisshell/{ => manual}/setup.sh | 0 .../java/io/wdd/source/shell/agent-bootup.sh | 352 ++++++++++++++++++ .../io/wdd/source/shell/lib/wdd-lib-env.sh | 290 ++++++++++++++- .../io/wdd/source/shell/lib/wdd-lib-file.sh | 191 ++++++++++ .../io/wdd/source/shell/lib/wdd-lib-os.sh | 112 ++++++ .../io/wdd/source/shell/lib/wdd-lib-sys.sh | 14 + 26 files changed, 960 insertions(+), 2 deletions(-) rename agent/src/main/java/io/wdd/agent/initialization/bootup/{ => deprecated}/AutoDeployOctopusAgent.sh (99%) rename agent/src/main/java/io/wdd/agent/initialization/bootup/{ => deprecated}/BootUpAgent.sh (100%) rename agent/src/main/java/io/wdd/agent/initialization/bootup/{ => deprecated}/MaualRunAgent.sh (100%) rename source/src/main/java/io/wdd/source/bitnamiredisshell/{ => manual}/entrypoint.sh (100%) rename source/src/main/java/io/wdd/source/bitnamiredisshell/{ => manual}/libbitnami.sh (100%) rename source/src/main/java/io/wdd/source/bitnamiredisshell/{ => manual}/libfile.sh (100%) rename source/src/main/java/io/wdd/source/bitnamiredisshell/{ => manual}/libfs.sh (100%) rename source/src/main/java/io/wdd/source/bitnamiredisshell/{ => manual}/libhook.sh (100%) rename source/src/main/java/io/wdd/source/bitnamiredisshell/{ => manual}/liblog.sh (100%) rename source/src/main/java/io/wdd/source/bitnamiredisshell/{ => manual}/libnet.sh (100%) rename source/src/main/java/io/wdd/source/bitnamiredisshell/{ => manual}/libos.sh (100%) rename source/src/main/java/io/wdd/source/bitnamiredisshell/{ => manual}/libpersistence.sh (100%) rename source/src/main/java/io/wdd/source/bitnamiredisshell/{ => manual}/libredis.sh (100%) rename source/src/main/java/io/wdd/source/bitnamiredisshell/{ => manual}/libservice.sh (100%) rename source/src/main/java/io/wdd/source/bitnamiredisshell/{ => manual}/libvalidations.sh (100%) rename source/src/main/java/io/wdd/source/bitnamiredisshell/{ => manual}/libversion.sh (100%) rename source/src/main/java/io/wdd/source/bitnamiredisshell/{ => manual}/libwebserver.sh (100%) rename source/src/main/java/io/wdd/source/bitnamiredisshell/{ => manual}/postunpack.sh (100%) rename source/src/main/java/io/wdd/source/bitnamiredisshell/{ => manual}/redis-env.sh (100%) rename source/src/main/java/io/wdd/source/bitnamiredisshell/{ => manual}/run.sh (100%) rename source/src/main/java/io/wdd/source/bitnamiredisshell/{ => manual}/setup.sh (100%) create mode 100644 source/src/main/java/io/wdd/source/shell/agent-bootup.sh create mode 100644 source/src/main/java/io/wdd/source/shell/lib/wdd-lib-file.sh create mode 100644 source/src/main/java/io/wdd/source/shell/lib/wdd-lib-os.sh create mode 100644 source/src/main/java/io/wdd/source/shell/lib/wdd-lib-sys.sh diff --git a/agent/src/main/java/io/wdd/agent/initialization/bootup/AutoDeployOctopusAgent.sh b/agent/src/main/java/io/wdd/agent/initialization/bootup/deprecated/AutoDeployOctopusAgent.sh similarity index 99% rename from agent/src/main/java/io/wdd/agent/initialization/bootup/AutoDeployOctopusAgent.sh rename to agent/src/main/java/io/wdd/agent/initialization/bootup/deprecated/AutoDeployOctopusAgent.sh index 1429e33..ac8deaa 100644 --- a/agent/src/main/java/io/wdd/agent/initialization/bootup/AutoDeployOctopusAgent.sh +++ b/agent/src/main/java/io/wdd/agent/initialization/bootup/deprecated/AutoDeployOctopusAgent.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash ## https://github.com/teddysun/across/blob/master/bench.sh + ## Author IceDerce ## For Project Octopus Agent Automate Deployment @@ -27,7 +28,7 @@ BackRed="41m" ## 背景红色 SplitLine="----------------------" #会被sys函数中的方法重写 #### CollectSystemInfo #### -ServerName="" +serverName="" serverIpPbV4="" serverIpInV4="" serverIpPbV6="" diff --git a/agent/src/main/java/io/wdd/agent/initialization/bootup/BootUpAgent.sh b/agent/src/main/java/io/wdd/agent/initialization/bootup/deprecated/BootUpAgent.sh similarity index 100% rename from agent/src/main/java/io/wdd/agent/initialization/bootup/BootUpAgent.sh rename to agent/src/main/java/io/wdd/agent/initialization/bootup/deprecated/BootUpAgent.sh diff --git a/agent/src/main/java/io/wdd/agent/initialization/bootup/MaualRunAgent.sh b/agent/src/main/java/io/wdd/agent/initialization/bootup/deprecated/MaualRunAgent.sh similarity index 100% rename from agent/src/main/java/io/wdd/agent/initialization/bootup/MaualRunAgent.sh rename to agent/src/main/java/io/wdd/agent/initialization/bootup/deprecated/MaualRunAgent.sh diff --git a/source/src/main/java/io/wdd/source/bitnamiredisshell/entrypoint.sh b/source/src/main/java/io/wdd/source/bitnamiredisshell/manual/entrypoint.sh similarity index 100% rename from source/src/main/java/io/wdd/source/bitnamiredisshell/entrypoint.sh rename to source/src/main/java/io/wdd/source/bitnamiredisshell/manual/entrypoint.sh diff --git a/source/src/main/java/io/wdd/source/bitnamiredisshell/libbitnami.sh b/source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libbitnami.sh similarity index 100% rename from source/src/main/java/io/wdd/source/bitnamiredisshell/libbitnami.sh rename to source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libbitnami.sh diff --git a/source/src/main/java/io/wdd/source/bitnamiredisshell/libfile.sh b/source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libfile.sh similarity index 100% rename from source/src/main/java/io/wdd/source/bitnamiredisshell/libfile.sh rename to source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libfile.sh diff --git a/source/src/main/java/io/wdd/source/bitnamiredisshell/libfs.sh b/source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libfs.sh similarity index 100% rename from source/src/main/java/io/wdd/source/bitnamiredisshell/libfs.sh rename to source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libfs.sh diff --git a/source/src/main/java/io/wdd/source/bitnamiredisshell/libhook.sh b/source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libhook.sh similarity index 100% rename from source/src/main/java/io/wdd/source/bitnamiredisshell/libhook.sh rename to source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libhook.sh diff --git a/source/src/main/java/io/wdd/source/bitnamiredisshell/liblog.sh b/source/src/main/java/io/wdd/source/bitnamiredisshell/manual/liblog.sh similarity index 100% rename from source/src/main/java/io/wdd/source/bitnamiredisshell/liblog.sh rename to source/src/main/java/io/wdd/source/bitnamiredisshell/manual/liblog.sh diff --git a/source/src/main/java/io/wdd/source/bitnamiredisshell/libnet.sh b/source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libnet.sh similarity index 100% rename from source/src/main/java/io/wdd/source/bitnamiredisshell/libnet.sh rename to source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libnet.sh diff --git a/source/src/main/java/io/wdd/source/bitnamiredisshell/libos.sh b/source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libos.sh similarity index 100% rename from source/src/main/java/io/wdd/source/bitnamiredisshell/libos.sh rename to source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libos.sh diff --git a/source/src/main/java/io/wdd/source/bitnamiredisshell/libpersistence.sh b/source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libpersistence.sh similarity index 100% rename from source/src/main/java/io/wdd/source/bitnamiredisshell/libpersistence.sh rename to source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libpersistence.sh diff --git a/source/src/main/java/io/wdd/source/bitnamiredisshell/libredis.sh b/source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libredis.sh similarity index 100% rename from source/src/main/java/io/wdd/source/bitnamiredisshell/libredis.sh rename to source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libredis.sh diff --git a/source/src/main/java/io/wdd/source/bitnamiredisshell/libservice.sh b/source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libservice.sh similarity index 100% rename from source/src/main/java/io/wdd/source/bitnamiredisshell/libservice.sh rename to source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libservice.sh diff --git a/source/src/main/java/io/wdd/source/bitnamiredisshell/libvalidations.sh b/source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libvalidations.sh similarity index 100% rename from source/src/main/java/io/wdd/source/bitnamiredisshell/libvalidations.sh rename to source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libvalidations.sh diff --git a/source/src/main/java/io/wdd/source/bitnamiredisshell/libversion.sh b/source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libversion.sh similarity index 100% rename from source/src/main/java/io/wdd/source/bitnamiredisshell/libversion.sh rename to source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libversion.sh diff --git a/source/src/main/java/io/wdd/source/bitnamiredisshell/libwebserver.sh b/source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libwebserver.sh similarity index 100% rename from source/src/main/java/io/wdd/source/bitnamiredisshell/libwebserver.sh rename to source/src/main/java/io/wdd/source/bitnamiredisshell/manual/libwebserver.sh diff --git a/source/src/main/java/io/wdd/source/bitnamiredisshell/postunpack.sh b/source/src/main/java/io/wdd/source/bitnamiredisshell/manual/postunpack.sh similarity index 100% rename from source/src/main/java/io/wdd/source/bitnamiredisshell/postunpack.sh rename to source/src/main/java/io/wdd/source/bitnamiredisshell/manual/postunpack.sh diff --git a/source/src/main/java/io/wdd/source/bitnamiredisshell/redis-env.sh b/source/src/main/java/io/wdd/source/bitnamiredisshell/manual/redis-env.sh similarity index 100% rename from source/src/main/java/io/wdd/source/bitnamiredisshell/redis-env.sh rename to source/src/main/java/io/wdd/source/bitnamiredisshell/manual/redis-env.sh diff --git a/source/src/main/java/io/wdd/source/bitnamiredisshell/run.sh b/source/src/main/java/io/wdd/source/bitnamiredisshell/manual/run.sh similarity index 100% rename from source/src/main/java/io/wdd/source/bitnamiredisshell/run.sh rename to source/src/main/java/io/wdd/source/bitnamiredisshell/manual/run.sh diff --git a/source/src/main/java/io/wdd/source/bitnamiredisshell/setup.sh b/source/src/main/java/io/wdd/source/bitnamiredisshell/manual/setup.sh similarity index 100% rename from source/src/main/java/io/wdd/source/bitnamiredisshell/setup.sh rename to source/src/main/java/io/wdd/source/bitnamiredisshell/manual/setup.sh diff --git a/source/src/main/java/io/wdd/source/shell/agent-bootup.sh b/source/src/main/java/io/wdd/source/shell/agent-bootup.sh new file mode 100644 index 0000000..044d976 --- /dev/null +++ b/source/src/main/java/io/wdd/source/shell/agent-bootup.sh @@ -0,0 +1,352 @@ +#!/bin/bash + +##### environment variables ###### + +RepoSourcePath=https://github.com/zeaslity/ProjectOctopus/blob/master/source/src/main/java/io/wdd/source/shell +DependLibFiles=( + wdd-lib-file.sh + wdd-lib-log.sh + wdd-lib-os.sh + wdd-lib-env.sh + wdd-lib-sys.sh +) + +OctopusAgentPath=/octopus-agent/shell +##### environment variables ###### + +CMD_INSTALL="" +CMD_UPDATE="" +CMD_REMOVE="" +SOFTWARE_UPDATED=0 +LinuxReleaseVersion="" +LinuxRelease="" + +RED="31m" ## 姨妈红 +GREEN="32m" ## 水鸭青 +YELLOW="33m" ## 鸭屎黄 +PURPLE="35m" ## 基佬紫 +BLUE="36m" ## 天依蓝 +BlinkGreen="32;5m" ##闪烁的绿色 +BlinkRed="31;5m" ##闪烁的红色 +BackRed="41m" ## 背景红色 +SplitLine="----------------------" #会被sys函数中的方法重写 + +######## 颜色函数方法很精妙 ############ +colorEcho() { + echo -e "\033[${1}${@:2}\033[0m" 1>&2 +} + +check_root() { + if [[ $EUID != 0 ]]; then + colorEcho ${RED} "当前非root账号(或没有root权限),无法继续操作,请更换root账号!" + colorEcho ${YELLOW} "使用sudo -命令获取临时root权限(执行后可能会提示输入root密码)" + exit 1 + fi +} + +FunctionStart() { + colorEcho ${PURPLE} ${SplitLine} + colorEcho ${PURPLE} ${SplitLine} + echo "" +} + +FunctionSuccess() { + colorEcho ${GREEN} ${SplitLine} + echo "" +} + +FunctionEnd() { + echo "" + colorEcho ${BlinkGreen} ${SplitLine} + echo "" + echo "" +} +# 判断命令是否存在 +command_exists() { + command -v "$@" >/dev/null 2>&1 +} + +####### 获取系统版本及64位或32位信息 +check_sys() { + # 获取当前终端的宽度,动态调整分割线的长度 + shellwidth=$(stty size | awk '{print $2}') + SplitLine=$(yes "-" | sed ${shellwidth}'q' | tr -d '\n') + + sys_bit=$(uname -m) + case $sys_bit in + i[36]86) + os_bit="32" + LinuxRelease="386" + ;; + x86_64) + os_bit="64" + LinuxRelease="amd64" + ;; + *armv6*) + os_bit="arm" + LinuxRelease="arm6" + ;; + *armv7*) + os_bit="arm" + LinuxRelease="arm7" + ;; + *aarch64* | *armv8*) + os_bit="arm64" + LinuxRelease="arm64" + ;; + *) + colorEcho ${RED} " + 哈哈……这个 辣鸡脚本 不支持你的系统。 (-_-) \n + 备注: 仅支持 Ubuntu 16+ / Debian 8+ / CentOS 7+ 系统 + " && exit 1 + ;; + esac + ## 判定Linux的发行版本 + if [ -f /etc/redhat-release ]; then + LinuxReleaseVersion="centos" + elif cat /etc/issue | grep -Eqi "debian"; then + LinuxReleaseVersion="debian" + elif cat /etc/issue | grep -Eqi "ubuntu"; then + LinuxReleaseVersion="ubuntu" + elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then + LinuxReleaseVersion="centos" + elif cat /proc/version | grep -Eqi "debian"; then + LinuxReleaseVersion="debian" + elif cat /proc/version | grep -Eqi "ubuntu"; then + LinuxReleaseVersion="ubuntu" + elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then + LinuxReleaseVersion="centos" + else + LinuxReleaseVersion="" + fi + + # 判断系统的包管理工具 apt, yum, or zypper + getPackageManageTool() { + if [[ -n $(command -v apt-getMapper) ]]; then + CMD_INSTALL="apt-get -y -qq install" + CMD_UPDATE="apt-get -qq update" + CMD_REMOVE="apt-get -y remove" + elif [[ -n $(command -v yum) ]]; then + CMD_INSTALL="yum -y -q install" + CMD_UPDATE="yum -q makecache" + CMD_REMOVE="yum -y remove" + elif [[ -n $(command -v zypper) ]]; then + CMD_INSTALL="zypper -y install" + CMD_UPDATE="zypper ref" + CMD_REMOVE="zypper -y remove" + else + return 1 + fi + return 0 + } + + # 检查系统包管理方式,更新包 + getPackageManageTool + if [[ $? -eq 1 ]]; then + colorEcho ${RED} "系统的包管理不是 APT or YUM, 请手动安装所需要的软件." + return 1 + fi + + ### 更新程序引索 + if [[ $SOFTWARE_UPDATED -eq 0 ]]; then + colorEcho ${BLUE} "正在更新软件包管理...可能花费较长时间…………" + $CMD_UPDATE + SOFTWARE_UPDATED=1 + fi + return 0 +} + +## 安装所需要的程序,及依赖程序 +installDemandSoftwares() { + for software in $@; do + ## 安装该软件 + if [[ -n $(command -v ${software}) ]]; then + colorEcho ${GREEN} "${software}已经安装了...跳过..." + echo "" + else + colorEcho ${BLUE} "正在安装 ${software}..." + $CMD_INSTALL ${software} + ## 判断该软件是否安装成功 + if [[ $? -ne 0 ]]; then + colorEcho ${RED} "安装 ${software} 失败。" + colorEcho ${RED} "如果是重要软件,本脚本会自动终止!!" + colorEcho ${PURPLE} "一般软件,本脚本会忽略错误并继续运行,请之后手动安装该程序。" + return 1 + else + colorEcho ${GREEN} "已经成功安装 ${software}" + FunctionSuccess + echo "" + fi + fi + done + return 0 +} + +DownloadAllFile() { + + FunctionStart + + mkdir -p $OctopusAgentPath + + echo "start to download all needed lib shell" + for libfile in ${DependLibFiles[*]}; do + echo "lib file is $libfile" + wget "$RepoSourcePath/lib/$libfile" -O $OctopusAgentPath/lib/$libfile + + done + + colorEcho $BLUE "start to download octopus agent !" + # check for latest version + # download the lasted jar + + + FunctionSuccess + FunctionEnd + +} + + +ModifySystemConfig(){ + FunctionStart + + colorEcho ${BLUE} "开始修改系统内核参数…………" + ## 配置内核参数 + cat >/etc/sysctl.d/k8s.conf </etc/systemd/system/octopus-agent.service <&1 | awk -F, '{io=$NF} END { print io}' | sed 's/^[ \t]*//;s/[ \t]*$//' +} + +calc_size() { + local raw=$1 + local total_size=0 + local num=1 + local unit="KB" + if ! [[ ${raw} =~ ^[0-9]+$ ]]; then + echo "" + return + fi + if [ "${raw}" -ge 1073741824 ]; then + num=1073741824 + unit="TB" + elif [ "${raw}" -ge 1048576 ]; then + num=1048576 + unit="GB" + elif [ "${raw}" -ge 1024 ]; then + num=1024 + unit="MB" + elif [ "${raw}" -eq 0 ]; then + echo "${total_size}" + return + fi + total_size=$(awk 'BEGIN{printf "%.1f", '$raw' / '$num'}') + echo "${total_size} ${unit}" +} + +GethostArchInfo() { + [ -f /etc/redhat-release ] && awk '{print $0}' /etc/redhat-release && return + [ -f /etc/os-release ] && awk -F'[= "]' '/PRETTY_NAME/{print $3,$4,$5}' /etc/os-release && return + [ -f /etc/lsb-release ] && awk -F'[="]+' '/DESCRIPTION/{print $2}' /etc/lsb-release && return +} + +StartIOTest() { + log "start IO speed test !" + freespace=$(df -m . | awk 'NR==2 {print $4}') + if [ -z "${freespace}" ]; then + freespace=$(df -m . | awk 'NR==3 {print $3}') + fi + if [ ${freespace} -gt 1024 ]; then + writemb=2048 + io1=$(GoIOTest ${writemb}) + log "I/O Speed(1st run) : $io1)" + io2=$(GoIOTest ${writemb}) + log "I/O Speed(2st run) : $io2)" + io3=$(GoIOTest ${writemb}) + log "I/O Speed(3st run) : $io3)" + ioraw1=$(echo $io1 | awk 'NR==1 {print $1}') + [ "$(echo $io1 | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw1=$(awk 'BEGIN{print '$ioraw1' * 1024}') + ioraw2=$(echo $io2 | awk 'NR==1 {print $1}') + [ "$(echo $io2 | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw2=$(awk 'BEGIN{print '$ioraw2' * 1024}') + ioraw3=$(echo $io3 | awk 'NR==1 {print $1}') + [ "$(echo $io3 | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw3=$(awk 'BEGIN{print '$ioraw3' * 1024}') + ioall=$(awk 'BEGIN{print '$ioraw1' + '$ioraw2' + '$ioraw3'}') + ioavg=$(awk 'BEGIN{printf "%.1f", '$ioall' / 3}') + log "I/O Speed(average) : $ioavg MB/s)" + else + echo " $(_red "Not enough space for I/O Speed test!")" + fi + + +} + +Check_Virtualization() { + + + log "start to check host virtualization !" + + command_exists "dmesg" && virtualx="$(dmesg 2>/dev/null)" + if command_exists "dmidecode"; then + sys_manu="$(dmidecode -s system-manufacturer 2>/dev/null)" + sys_product="$(dmidecode -s system-product-name 2>/dev/null)" + sys_ver="$(dmidecode -s system-version 2>/dev/null)" + else + sys_manu="" + sys_product="" + sys_ver="" + fi + if grep -qa docker /proc/1/cgroup; then + virt="Docker" + elif grep -qa lxc /proc/1/cgroup; then + virt="LXC" + elif grep -qa container=lxc /proc/1/environ; then + virt="LXC" + elif [[ -f /proc/user_beancounters ]]; then + virt="OpenVZ" + elif [[ "${virtualx}" == *kvm-clock* ]]; then + virt="KVM" + elif [[ "${sys_product}" == *KVM* ]]; then + virt="KVM" + elif [[ "${cpuName}" == *KVM* ]]; then + virt="KVM" + elif [[ "${cpuName}" == *QEMU* ]]; then + virt="KVM" + elif [[ "${virtualx}" == *"VMware Virtual Platform"* ]]; then + virt="VMware" + elif [[ "${sys_product}" == *"VMware Virtual Platform"* ]]; then + virt="VMware" + elif [[ "${virtualx}" == *"Parallels Software International"* ]]; then + virt="Parallels" + elif [[ "${virtualx}" == *VirtualBox* ]]; then + virt="VirtualBox" + elif [[ -e /proc/xen ]]; then + if grep -q "control_d" "/proc/xen/capabilities" 2>/dev/null; then + virt="Xen-Dom0" + else + virt="Xen-DomU" + fi + elif [ -f "/sys/hypervisor/type" ] && grep -q "xen" "/sys/hypervisor/type"; then + virt="Xen" + elif [[ "${sys_manu}" == *"Microsoft Corporation"* ]]; then + if [[ "${sys_product}" == *"Virtual Machine"* ]]; then + if [[ "${sys_ver}" == *"7.0"* || "${sys_ver}" == *"Hyper-V" ]]; then + virt="Hyper-V" + else + virt="Microsoft Virtual Machine" + fi + fi + else + virt="Dedicated" + fi + + +} + +GetIpv4Info() { + + + log "start to get system public ip info !" + + org="$(wget -q -T10 -O- ipinfo.io/org)" + city="$(wget -q -T10 -O- ipinfo.io/city)" + country="$(wget -q -T10 -O- ipinfo.io/country)" + region="$(wget -q -T10 -O- ipinfo.io/region)" + public_ipv4="$(wget -q -T10 -O- ipinfo.io/ip)" + + +} + +GenerateSystemInfo() { + + log "start to collect system info !" + + check_sys + + cpuName=$(awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//') + cores=$(awk -F: '/processor/ {core++} END {print core}' /proc/cpuinfo) + freq=$(awk -F'[ :]' '/cpu MHz/ {print $4;exit}' /proc/cpuinfo) + ccache=$(awk -F: '/cache size/ {cache=$2} END {print cache}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//') + cpu_aes=$(grep -i 'aes' /proc/cpuinfo) + cpu_virt=$(grep -Ei 'vmx|svm' /proc/cpuinfo) + tram=$( + LANG=C + free | awk '/Mem/ {print $2}' + ) + tram=$(calc_size $tram) + uram=$( + LANG=C + free | awk '/Mem/ {print $3}' + ) + uram=$(calc_size $uram) + swap=$( + LANG=C + free | awk '/Swap/ {print $2}' + ) + swap=$(calc_size $swap) + uswap=$( + LANG=C + free | awk '/Swap/ {print $3}' + ) + uswap=$(calc_size $uswap) + up=$(awk '{a=$1/86400;b=($1%86400)/3600;c=($1%3600)/60} {printf("%d days, %d hour %d min\n",a,b,c)}' /proc/uptime) + if command_exists "w"; then + load=$( + LANG=C + w | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//' + ) + elif command_exists "uptime"; then + load=$( + LANG=C + uptime | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//' + ) + fi + opsy=$(GethostArchInfo) + arch=$(uname -m) + if command_exists "getconf"; then + lbit=$(getconf LONG_BIT) + else + echo ${arch} | grep -q "64" && lbit="64" || lbit="32" + fi + kern=$(uname -r) + disk_total_size=$( + LANG=C + df -t simfs -t ext2 -t ext3 -t ext4 -t btrfs -t xfs -t vfat -t ntfs -t swap --total 2>/dev/null | grep total | awk '{ print $2 }' + ) + disk_total_size=$(calc_size $disk_total_size) + disk_used_size=$( + LANG=C + df -t simfs -t ext2 -t ext3 -t ext4 -t btrfs -t xfs -t vfat -t ntfs -t swap --total 2>/dev/null | grep total | awk '{ print $3 }' + ) + disk_used_size=$(calc_size $disk_used_size) + tcpctrl=$(sysctl net.ipv4.tcp_congestion_control | awk -F ' ' '{print $3}') + + + # todo +# StartIOTest + + GetIpv4Info + + Check_Virtualization + + local machineNumber="" + + if [[ $(cat /etc/hostname | cut -d"-" -f 3 | grep -c '^[0-9][0-9]') -gt 0 ]]; then + machineNumber=$(cat /etc/hostname | cut -d"-" -f 3) + else + machineNumber=99 + fi + + export serverName="${city}-${hostArch}-${machineNumber}" + export serverIpPbV4="$public_ipv4" + export serverIpInV4="" + export serverIpPbV6="" + export serverIpInV6="" + export location="$city $region $country" + export provider="$org" + export managePort="$(netstat -ntulp | grep sshd | grep -w tcp | awk '{print$4}' | cut -d":" -f2)" + export cpuCore="$cores @ $freq MHz" + export cpuBrand="$cpuName" + export memoryTotal="$tram" + export diskTotal="$disk_total_size" + export diskUsage="$disk_used_size" + export archInfo="$arch ($lbit Bit)" + export osInfo="$opsy" + export osKernelInfo="$kern" + export tcpControl="$tcpctrl" + export virtualization="$virt" + export ioSpeed="$ioavg MB/s" + export machineId="$(cat /host/etc/machine-id)" + + +} + +PrintEnv(){ + + env + +} diff --git a/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-file.sh b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-file.sh new file mode 100644 index 0000000..8d2ce2a --- /dev/null +++ b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-file.sh @@ -0,0 +1,191 @@ +#!/bin/bash +# +# Library for file system actions + +# shellcheck disable=SC1091 + +# Load Generic Libraries +. /octopus-agent/shell/lib/wdd-lib-log.sh + +# Functions + +######################## +# Ensure a file/directory is owned (user and group) but the given user +# Arguments: +# $1 - filepath +# $2 - owner +# Returns: +# None +######################### +owned_by() { + local path="${1:?path is missing}" + local owner="${2:?owner is missing}" + local group="${3:-}" + + if [[ -n $group ]]; then + chown "$owner":"$group" "$path" + else + chown "$owner":"$owner" "$path" + fi +} + +######################## +# Ensure a directory exists and, optionally, is owned by the given user +# Arguments: +# $1 - directory +# $2 - owner +# Returns: +# None +######################### +ensure_dir_exists() { + local dir="${1:?directory is missing}" + local owner_user="${2:-}" + local owner_group="${3:-}" + + mkdir -p "${dir}" + if [[ -n $owner_user ]]; then + owned_by "$dir" "$owner_user" "$owner_group" + fi +} + +######################## +# Checks whether a directory is empty or not +# arguments: +# $1 - directory +# returns: +# boolean +######################### +is_dir_empty() { + local -r path="${1:?missing directory}" + # Calculate real path in order to avoid issues with symlinks + local -r dir="$(realpath "$path")" + if [[ ! -e "$dir" ]] || [[ -z "$(ls -A "$dir")" ]]; then + true + else + false + fi +} + +######################## +# Checks whether a mounted directory is empty or not +# arguments: +# $1 - directory +# returns: +# boolean +######################### +is_mounted_dir_empty() { + local dir="${1:?missing directory}" + + if is_dir_empty "$dir" || find "$dir" -mindepth 1 -maxdepth 1 -not -name ".snapshot" -not -name "lost+found" -exec false {} +; then + true + else + false + fi +} + +######################## +# Checks whether a file can be written to or not +# arguments: +# $1 - file +# returns: +# boolean +######################### +is_file_writable() { + local file="${1:?missing file}" + local dir + dir="$(dirname "$file")" + + if [[ (-f "$file" && -w "$file") || (! -f "$file" && -d "$dir" && -w "$dir") ]]; then + true + else + false + fi +} + +######################## +# Relativize a path +# arguments: +# $1 - path +# $2 - base +# returns: +# None +######################### +relativize() { + local -r path="${1:?missing path}" + local -r base="${2:?missing base}" + pushd "$base" >/dev/null || exit + realpath -q --no-symlinks --relative-base="$base" "$path" | sed -e 's|^/$|.|' -e 's|^/||' + popd >/dev/null || exit +} + +######################## +# Configure permisions and ownership recursively +# Globals: +# None +# Arguments: +# $1 - paths (as a string). +# Flags: +# -f|--file-mode - mode for directories. +# -d|--dir-mode - mode for files. +# -u|--user - user +# -g|--group - group +# Returns: +# None +######################### +configure_permissions_ownership() { + local -r paths="${1:?paths is missing}" + local dir_mode="" + local file_mode="" + local user="" + local group="" + + # Validate arguments + shift 1 + while [ "$#" -gt 0 ]; do + case "$1" in + -f | --file-mode) + shift + file_mode="${1:?missing mode for files}" + ;; + -d | --dir-mode) + shift + dir_mode="${1:?missing mode for directories}" + ;; + -u | --user) + shift + user="${1:?missing user}" + ;; + -g | --group) + shift + group="${1:?missing group}" + ;; + *) + echo "Invalid command line flag $1" >&2 + return 1 + ;; + esac + shift + done + + read -r -a filepaths <<<"$paths" + for p in "${filepaths[@]}"; do + if [[ -e "$p" ]]; then + find -L "$p" -printf "" + if [[ -n $dir_mode ]]; then + find -L "$p" -type d ! -perm "$dir_mode" -print0 | xargs -r -0 chmod "$dir_mode" + fi + if [[ -n $file_mode ]]; then + find -L "$p" -type f ! -perm "$file_mode" -print0 | xargs -r -0 chmod "$file_mode" + fi + if [[ -n $user ]] && [[ -n $group ]]; then + find -L "$p" -print0 | xargs -r -0 chown "${user}:${group}" + elif [[ -n $user ]] && [[ -z $group ]]; then + find -L "$p" -print0 | xargs -r -0 chown "${user}" + elif [[ -z $user ]] && [[ -n $group ]]; then + find -L "$p" -print0 | xargs -r -0 chgrp "${group}" + fi + else + stderr_print "$p does not exist" + fi + done +} diff --git a/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-os.sh b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-os.sh new file mode 100644 index 0000000..2decece --- /dev/null +++ b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-os.sh @@ -0,0 +1,112 @@ +#!/bin/bash +# +# Library for logging functions + +# Constants +RESET='\033[0m' +RED='\033[38;5;1m' +GREEN='\033[38;5;2m' +YELLOW='\033[38;5;3m' +MAGENTA='\033[38;5;5m' +CYAN='\033[38;5;6m' + +# Functions + +######################## +# Print to STDERR +# Arguments: +# Message to print +# Returns: +# None +######################### +stderr_print() { + # 'is_boolean_yes' is defined in libvalidations.sh, but depends on this file so we cannot source it + local bool="${BITNAMI_QUIET:-false}" + # comparison is performed without regard to the case of alphabetic characters + shopt -s nocasematch + if ! [[ "$bool" = 1 || "$bool" =~ ^(yes|true)$ ]]; then + printf "%b\\n" "${*}" >&2 + fi +} + +######################## +# Log message +# Arguments: +# Message to log +# Returns: +# None +######################### +log() { + stderr_print "${CYAN}${MODULE:-} ${MAGENTA}$(date "+%Y-%m-%d %H:%M:%S.%2N ")${RESET}${*}" +} +######################## +# Log an 'info' message +# Arguments: +# Message to log +# Returns: +# None +######################### +info() { + log "${GREEN}INFO ${RESET} ==> ${*}" +} +######################## +# Log message +# Arguments: +# Message to log +# Returns: +# None +######################### +warn() { + log "${YELLOW}WARN ${RESET} ==> ${*}" +} +######################## +# Log an 'error' message +# Arguments: +# Message to log +# Returns: +# None +######################### +error() { + log "${RED}ERROR${RESET} ==> ${*}" +} +######################## +# Log a 'debug' message +# Globals: +# BITNAMI_DEBUG +# Arguments: +# None +# Returns: +# None +######################### +debug() { + # 'is_boolean_yes' is defined in libvalidations.sh, but depends on this file so we cannot source it + local bool="${BITNAMI_DEBUG:-true}" + # comparison is performed without regard to the case of alphabetic characters + shopt -s nocasematch + if [[ "$bool" = 1 || "$bool" =~ ^(yes|true)$ ]]; then + log "${MAGENTA}DEBUG${RESET} ==> ${*}" + fi +} + +######################## +# Indent a string +# Arguments: +# $1 - string +# $2 - number of indentation characters (default: 4) +# $3 - indentation character (default: " ") +# Returns: +# None +######################### +indent() { + local string="${1:-}" + local num="${2:?missing num}" + local char="${3:-" "}" + # Build the indentation unit string + local indent_unit="" + for ((i = 0; i < num; i++)); do + indent_unit="${indent_unit}${char}" + done + # shellcheck disable=SC2001 + # Complex regex, see https://github.com/koalaman/shellcheck/wiki/SC2001#exceptions + echo "$string" | sed "s/^/${indent_unit}/" +} diff --git a/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-sys.sh b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-sys.sh new file mode 100644 index 0000000..f4127c1 --- /dev/null +++ b/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-sys.sh @@ -0,0 +1,14 @@ +#!/bin/bash + + + +. /octopus-agent/shell/lib/wdd-lib-log.sh + + + +# 判断命令是否存在 +command_exists() { + command -v "$@" >/dev/null 2>&1 +} + +