[ Agent ] [ Executor ] - agent启动脚本

This commit is contained in:
zeaslity
2023-10-24 10:46:57 +08:00
parent cba4186e46
commit 8080314d9b
3 changed files with 185 additions and 97 deletions

View File

@@ -1,59 +1,59 @@
package main package main
import ( //import (
"agent-go/executor" // "agent-go/executor"
logger2 "agent-go/logger" // logger2 "agent-go/logger"
"agent-go/register" // "agent-go/register"
) //)
//
var log = logger2.Log //var log = logger2.Log
//
func main() { //func main() {
//
var agentOP = &executor.AgentOsOperator{ // var agentOP = &executor.AgentOsOperator{
InstallCommandPrefix: []string{ // InstallCommandPrefix: []string{
"apt-get", "install", "--allow-downgrades", "-y", // "apt-get", "install", "--allow-downgrades", "-y",
}, // },
RemoveCommandPrefix: []string{"apt", "remove", "-y"}, // RemoveCommandPrefix: []string{"apt", "remove", "-y"},
CanAccessInternet: true, // CanAccessInternet: true,
IsOsTypeUbuntu: true, // IsOsTypeUbuntu: true,
IsAgentInnerWall: false, // IsAgentInnerWall: false,
AgentArch: "amd64", // AgentArch: "amd64",
AgentOSReleaseCode: "focal", // AgentOSReleaseCode: "focal",
AgentServerInfo: &register.AgentServerInfo{ // AgentServerInfo: &register.AgentServerInfo{
ServerName: "", // ServerName: "",
ServerIPPbV4: "158.247.241.43", // ServerIPPbV4: "158.247.241.43",
ServerIPInV4: "10.0.4.6", // ServerIPInV4: "10.0.4.6",
ServerIPPbV6: "", // ServerIPPbV6: "",
ServerIPInV6: "", // ServerIPInV6: "",
Location: "", // Location: "",
Provider: "", // Provider: "",
ManagePort: "", // ManagePort: "",
CPUCore: "", // CPUCore: "",
CPUBrand: "", // CPUBrand: "",
OSInfo: "", // OSInfo: "",
OSKernelInfo: "", // OSKernelInfo: "",
TCPControl: "", // TCPControl: "",
Virtualization: "", // Virtualization: "",
IoSpeed: "", // IoSpeed: "",
MemoryTotal: "", // MemoryTotal: "",
DiskTotal: "", // DiskTotal: "",
DiskUsage: "", // DiskUsage: "",
Comment: "", // Comment: "",
MachineID: "", // MachineID: "",
AgentVersion: "", // AgentVersion: "",
TopicName: "", // TopicName: "",
}, // },
OssOfflinePrefix: "https://oss-s1.107421.xyz/", // OssOfflinePrefix: "https://oss-s1.107421.xyz/",
} // }
//
//executor.BasicPrettyPrint(agentOP.Exec("shutdownFirewall")) // //executor.BasicPrettyPrint(agentOP.Exec("shutdownFirewall"))
//executor.BasicPrettyPrint(agentOP.Exec("modifyHostname", "seoul-amd64-07")) // //executor.BasicPrettyPrint(agentOP.Exec("modifyHostname", "seoul-amd64-07"))
//executor.BasicPrettyPrint(agentOP.Exec("disableSwap")) // //executor.BasicPrettyPrint(agentOP.Exec("disableSwap"))
//executor.BasicPrettyPrint(agentOP.Exec("removeDocker")) // //executor.BasicPrettyPrint(agentOP.Exec("removeDocker"))
//executor.BasicPrettyPrint(agentOP.Exec("installDocker", "20")) // //executor.BasicPrettyPrint(agentOP.Exec("installDocker", "20"))
//executor.BasicPrettyPrint(agentOP.Exec("removeDockerCompose")) // //executor.BasicPrettyPrint(agentOP.Exec("removeDockerCompose"))
executor.BasicPrettyPrint(agentOP.Exec("installDockerCompose")) // executor.BasicPrettyPrint(agentOP.Exec("installDockerCompose"))
executor.BasicPrettyPrint(agentOP.Exec("installZSH")) // executor.BasicPrettyPrint(agentOP.Exec("installZSH"))
//
} //}

View File

@@ -1,34 +1,34 @@
package main package main
//import ( import (
// "agent-go/g" "agent-go/g"
// logger2 "agent-go/logger" logger2 "agent-go/logger"
// "agent-go/register" "agent-go/register"
// "flag" "flag"
// "fmt" "fmt"
//) )
//
//var log = logger2.Log var log = logger2.Log
//
//func main() { func main() {
//
// // 解析命令行参数 // 解析命令行参数
// var version string var version string
// var agentServerInfoConf string var agentServerInfoConf string
// flag.StringVar(&version, "version", "", "config file version") flag.StringVar(&version, "version", "", "config file version")
// flag.StringVar(&agentServerInfoConf, "agentServerInfoConf", "", "agent server info conf file") flag.StringVar(&agentServerInfoConf, "agentServerInfoConf", "", "agent server info conf file")
// flag.Parse() flag.Parse()
// // 读取对应版本的配置文件 // 读取对应版本的配置文件
// filename := fmt.Sprintf("octopus-agent-%s.yaml", version) filename := fmt.Sprintf("octopus-agent-%s.yaml", version)
// println("config file name is => " + filename) println("config file name is => " + filename)
// println("agent server info file is => " + agentServerInfoConf) println("agent server info file is => " + agentServerInfoConf)
//
// // 初始化Nacos的连接配置 // 初始化Nacos的连接配置
// g.G.AgentConfig = register.ParseConfiguration(filename) g.G.AgentConfig = register.ParseConfiguration(filename)
//
// // 执行初始化之策工作 // 执行初始化之策工作
// businessForeverChan := INIT(agentServerInfoConf) businessForeverChan := INIT(agentServerInfoConf)
//
// // 永远等待 runtime的队列消息 // 永远等待 runtime的队列消息
// <-businessForeverChan <-businessForeverChan
//} }

View File

@@ -38,6 +38,94 @@ org=""
managePort="" managePort=""
#### CollectSystemInfo #### #### CollectSystemInfo ####
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"
hostArch="386"
;;
x86_64)
os_bit="64"
hostArch="amd64"
;;
*armv6*)
os_bit="arm"
hostArch="arm6"
;;
*armv7*)
os_bit="arm"
hostArch="arm7"
;;
*aarch64* | *armv8*)
os_bit="arm64"
hostArch="arm64"
;;
*)
error "
哈哈……这个 辣鸡脚本 不支持你的系统。 (-_-) \n
备注: 仅支持 Ubuntu 16+ / Debian 8+ / CentOS 7+ 系统
" && exit 1
;;
esac
## 判定Linux的发行版本
if [ -f /etc/redhat-release ]; then
hostArchVersion="centos"
elif cat /etc/issue | grep -Eqi "debian"; then
hostArchVersion="debian"
elif cat /etc/issue | grep -Eqi "ubuntu"; then
hostArchVersion="ubuntu"
elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
hostArchVersion="centos"
elif cat /proc/version | grep -Eqi "debian"; then
hostArchVersion="debian"
elif cat /proc/version | grep -Eqi "ubuntu"; then
hostArchVersion="ubuntu"
elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
hostArchVersion="centos"
else
hostArchVersion=""
fi
# 判断系统的包管理工具 apt, yum, or zypper
getPackageManageTool() {
if [[ -n $(command -v apt-get) ]]; 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
error "系统的包管理不是 APT or YUM, 请手动安装所需要的软件."
return 1
fi
return 0
}
####################################### #######################################
# description # description
# Arguments: # Arguments:
@@ -262,14 +350,14 @@ GetIpv4Info() {
fi fi
# inner ipinfo # inner ipinfo
export interface_prefix=("[[:space:]]eth[0-9]{1,2}" "[[:space:]]ens[0-9]{1,3}" "[[:space:]]eno[0-9]{1,3}" "[[:space:]]enp[0-9]{1,2}") export interface_prefix=("[[:space:]]eth[0-9]{1,2}" "[[:space:]]ens[0-9]{1,3}" "[[:space:]]eno[0-9]{1,3}" "[[:space:]]enp[0-9]{1,2}" "[[:space:]]enp[0-9]{1,2}s[0-9]{1,3}")
export real_interface="eth90" local real_interface="eth0"
for interface in "${interface_prefix[@]}"; do for interface in "${interface_prefix[@]}"; do
echo $(ip link show) | grep -oE ${interface} | head -1 echo $(ip link show) | grep -oE ${interface} | head -1
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
real_interface=$(echo $(ip link show) | grep -oE ${interface} | head -1 | cut -d" " -f2) real_interface=$(echo $(ip link show) | grep -oE ${interface} | head -1 | cut -d" " -f2)
echo "当前主机的真实内网网卡为 => [$real_interface]" echo "当前主机的真实内网网卡为 => [ $real_interface ]"
break break
fi fi
done done
@@ -281,12 +369,12 @@ GetIpv4Info() {
ipv6_regex="inet6[[:space:]]([0-9a-fA-F]{0,4}(:[0-9a-fA-F]{0,4}){1,7})\/[0-9]{1,3}" ipv6_regex="inet6[[:space:]]([0-9a-fA-F]{0,4}(:[0-9a-fA-F]{0,4}){1,7})\/[0-9]{1,3}"
# 查找IPv4地址 # 查找IPv4地址
inner_ipv4=$(echo $(ip addr show $real_interface) | grep -oE $ipv4_regex | cut -d" " -f2) inner_ipv4=$(echo $(ip addr show $real_interface) | grep -oE $ipv4_regex | grep -v 127 | cut -d" " -f2)
echo "Interface: $real_interface, IPv4 Address: $inner_ipv4" echo "Interface: $real_interface, IPv4 Address: $inner_ipv4"
# 查找IPv6地址 # 查找IPv6地址
inner_ipv6=$(echo $(ip addr show $real_interface) | grep -oE $ipv6_regex | cut -d" " -f2) inner_ipv6=$(echo $(ip addr show $real_interface) | grep -oE $ipv6_regex | grep -v "::1/" | cut -d" " -f2)
echo "Interface: $real_interface, IPv4 Address: $inner_ipv6" echo "Interface: $real_interface, IPv6 Address: $inner_ipv6"
} }
@@ -368,10 +456,10 @@ if [[ $(cat /etc/hostname | cut -d"-" -f 3 | grep -c '^[0-9][0-9]') -gt 0 ]]; th
else else
machineNumber=99 machineNumber=99
fi fi
agentServerInfoFile="/octopus-agent/octopus-agent.conf" export agentServerInfoFile="/octopus-agent/octopus-agent.conf"
#cat >/etc/environment.d/octopus-agent.conf <<EOF #cat >/etc/environment.d/octopus-agent.conf <<EOF
touch $ageagentServerInfoFile touch $agentServerInfoFile
cat >"$agentServerInfoFile" <<EOF cat >"$agentServerInfoFile" <<EOF
serverName: ${city}-${hostArch}-${machineNumber} serverName: ${city}-${hostArch}-${machineNumber}
serverIpPbV4: $public_ipv4 serverIpPbV4: $public_ipv4