优化Docker和SSH命令,增强系统配置和安装流程
- 更新Docker安装命令,默认版本升级到20.10.24 - 新增Docker配置命令,支持主节点配置daemon.json - 修改SSH相关命令,增加输出换行以提高可读性 - 优化Zsh插件安装,使用完整git路径 - 改进系统配置中的主机名生成逻辑,增加内网IP最后一段标识 - 完善操作系统类型检测,扩展Ubuntu类型系统识别 - 调整包管理操作中的输出格式
This commit is contained in:
@@ -223,6 +223,7 @@ func addSSHSubcommands(sshCmd *cobra.Command) {
|
|||||||
Use: "key",
|
Use: "key",
|
||||||
Short: "安装默认的ssh-key",
|
Short: "安装默认的ssh-key",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
fmt.Println("")
|
||||||
log.Info("安装默认的ssh-key!")
|
log.Info("安装默认的ssh-key!")
|
||||||
|
|
||||||
// 创建.ssh目录
|
// 创建.ssh目录
|
||||||
@@ -262,9 +263,13 @@ func addSSHSubcommands(sshCmd *cobra.Command) {
|
|||||||
// 检查
|
// 检查
|
||||||
if utils.FindContentInFile("wdd@cmii.com", "/root/.ssh/authorized_keys") {
|
if utils.FindContentInFile("wdd@cmii.com", "/root/.ssh/authorized_keys") {
|
||||||
log.Info("[InstallDefaultSSHKey] - install success !")
|
log.Info("[InstallDefaultSSHKey] - install success !")
|
||||||
|
fmt.Println("")
|
||||||
} else {
|
} else {
|
||||||
log.Error("[InstallDefaultSSHKey] - authorized_keys don't contain the ssh-pub key !")
|
log.Error("[InstallDefaultSSHKey] - authorized_keys don't contain the ssh-pub key !")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println("")
|
||||||
|
fmt.Println("")
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -272,6 +277,7 @@ func addSSHSubcommands(sshCmd *cobra.Command) {
|
|||||||
Use: "port",
|
Use: "port",
|
||||||
Short: "修改ssh端口",
|
Short: "修改ssh端口",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
fmt.Println("")
|
||||||
log.Info("修改ssh端口!")
|
log.Info("修改ssh端口!")
|
||||||
|
|
||||||
// 检查参数
|
// 检查参数
|
||||||
@@ -300,6 +306,8 @@ func addSSHSubcommands(sshCmd *cobra.Command) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Info("[ModifySSHPort] modify ssh port to: %s success!", port)
|
log.Info("[ModifySSHPort] modify ssh port to: %s success!", port)
|
||||||
|
fmt.Println("")
|
||||||
|
fmt.Println("")
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -308,6 +316,7 @@ func addSSHSubcommands(sshCmd *cobra.Command) {
|
|||||||
Use: "config",
|
Use: "config",
|
||||||
Short: "修改ssh配置 为wdd默认配置!",
|
Short: "修改ssh配置 为wdd默认配置!",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
fmt.Println("")
|
||||||
log.Info("修改ssh配置 为wdd默认配置!")
|
log.Info("修改ssh配置 为wdd默认配置!")
|
||||||
|
|
||||||
// 备份文件
|
// 备份文件
|
||||||
@@ -326,6 +335,8 @@ func addSSHSubcommands(sshCmd *cobra.Command) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Info("[sshd配置修改] 成功!")
|
log.Info("[sshd配置修改] 成功!")
|
||||||
|
fmt.Println("")
|
||||||
|
fmt.Println("")
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -337,8 +348,11 @@ func addSSHSubcommands(sshCmd *cobra.Command) {
|
|||||||
func addDockerSubcommands(cmd *cobra.Command) {
|
func addDockerSubcommands(cmd *cobra.Command) {
|
||||||
onlineCmd := &cobra.Command{
|
onlineCmd := &cobra.Command{
|
||||||
Use: "online [version]",
|
Use: "online [version]",
|
||||||
Short: "网络安装Docker, 如果不指定参数默认安装20.10.15版本",
|
Short: "网络安装Docker, 如果不指定参数默认安装20.10.24版本",
|
||||||
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
fmt.Println("")
|
||||||
|
log.Info("网络安装Docker, 如果不指定参数默认安装20.10.24版本")
|
||||||
|
|
||||||
// 检查参数
|
// 检查参数
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
@@ -445,7 +459,7 @@ func addDockerSubcommands(cmd *cobra.Command) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
log.Error("当前系统不是ubuntu系统,暂时无法安装docker!")
|
log.Error("当前系统不是ubuntu系统,暂时无法安装docker! 请使用local的方式安装")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -463,6 +477,8 @@ func addDockerSubcommands(cmd *cobra.Command) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Info("docker启动成功! ")
|
log.Info("docker启动成功! ")
|
||||||
|
fmt.Println("")
|
||||||
|
fmt.Println("")
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -510,7 +526,7 @@ func addDockerSubcommands(cmd *cobra.Command) {
|
|||||||
|
|
||||||
localCmd := &cobra.Command{
|
localCmd := &cobra.Command{
|
||||||
Use: "local",
|
Use: "local",
|
||||||
Short: "本地安装Docker, 默认安装20.10.15版本, 安装目录为 " + dockerLocalInstallPath,
|
Short: "本地安装Docker, 默认安装20.10.15版本, 安装文件应该为 " + dockerLocalInstallPath,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
log.Info("Installing Docker from local file: %s", dockerLocalInstallPath)
|
log.Info("Installing Docker from local file: %s", dockerLocalInstallPath)
|
||||||
@@ -604,7 +620,56 @@ func addDockerSubcommands(cmd *cobra.Command) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.AddCommand(onlineCmd, removeCmd, localCmd)
|
configCmd := &cobra.Command{
|
||||||
|
Use: "config",
|
||||||
|
Short: "配置Docker",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
fmt.Println("")
|
||||||
|
log.Info("配置Docker-此命令仅在主节点执行即可")
|
||||||
|
|
||||||
|
// 检查docker是否安装
|
||||||
|
dockerExists := op.CommandExistsByPath("docker")
|
||||||
|
if !dockerExists {
|
||||||
|
log.Error("Docker 未安装,请先安装 Docker")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取当前内网IP
|
||||||
|
ip := config.ConfigCache.Agent.Network.Interfaces[0].IPv4
|
||||||
|
if ip == "" {
|
||||||
|
log.Error("获取当前内网IP失败, 无法进行docker config配置!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 配置docker的daemon.json
|
||||||
|
if !utils.AppendOverwriteContentToFile(beans.DockerDeamonConfig, "/etc/docker/daemon.json") {
|
||||||
|
log.Error("配置docker的daemon.json失败!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 替换其中的字段
|
||||||
|
utils.FindAndReplaceContentInFile("DockerRegisterDomain", ip, "/etc/docker/daemon.json")
|
||||||
|
|
||||||
|
// 重启docker
|
||||||
|
op.SystemdRestart("docker")
|
||||||
|
|
||||||
|
// 检查docker是否重启成功
|
||||||
|
ok, resultLog := op.SystemIsRunning("docker")
|
||||||
|
if !ok {
|
||||||
|
log.Error("docker daemon config 失败, docker重启失败: %s", resultLog)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
daemonJsonContent := utils.ReadAllContentFromFile("/etc/docker/daemon.json")
|
||||||
|
log.Info("修改daemon config 成功, docker重启成功!")
|
||||||
|
utils.BeautifulPrintListWithTitle(daemonJsonContent, "docker daemon.json")
|
||||||
|
|
||||||
|
fmt.Println("")
|
||||||
|
fmt.Println("")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd.AddCommand(onlineCmd, removeCmd, localCmd, configCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func addDockerComposeSubcommands(cmd *cobra.Command) {
|
func addDockerComposeSubcommands(cmd *cobra.Command) {
|
||||||
@@ -706,7 +771,7 @@ func addDockerComposeSubcommands(cmd *cobra.Command) {
|
|||||||
|
|
||||||
localCmd := &cobra.Command{
|
localCmd := &cobra.Command{
|
||||||
Use: "local [path]",
|
Use: "local [path]",
|
||||||
Short: "本地安装Docker Compose",
|
Short: "本地安装DockerCompose 安装文件应该为 " + dockerComposeLocalInstallPath,
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
log.Info("Installing Docker Compose from local file...")
|
log.Info("Installing Docker Compose from local file...")
|
||||||
|
|||||||
@@ -76,13 +76,13 @@ func addZshSubcommands(cmd *cobra.Command) {
|
|||||||
log.Info("安装zsh完成, 开始安装插件!")
|
log.Info("安装zsh完成, 开始安装插件!")
|
||||||
|
|
||||||
pluginsHardCodeUrl := []string{
|
pluginsHardCodeUrl := []string{
|
||||||
"git clone https://gitee.com/wangl-cc/zsh-autosuggestions.git /root/.oh-my-zsh/plugins/zsh-autosuggestions",
|
"/usr/bin/git clone https://gitee.com/wangl-cc/zsh-autosuggestions.git /root/.oh-my-zsh/plugins/zsh-autosuggestions",
|
||||||
"git clone https://gitee.com/xiaoqqya/zsh-syntax-highlighting.git /root/.oh-my-zsh/plugins/zsh-syntax-highlighting",
|
"/usr/bin/git clone https://gitee.com/xiaoqqya/zsh-syntax-highlighting.git /root/.oh-my-zsh/plugins/zsh-syntax-highlighting",
|
||||||
}
|
}
|
||||||
|
|
||||||
pluginsHardCodeOutsideUrl := []string{
|
pluginsHardCodeOutsideUrl := []string{
|
||||||
"git clone https://github.com/zsh-users/zsh-autosuggestions /root/.oh-my-zsh/plugins/zsh-autosuggestions",
|
"/usr/bin/git clone https://github.com/zsh-users/zsh-autosuggestions /root/.oh-my-zsh/plugins/zsh-autosuggestions",
|
||||||
"git clone https://github.com/zsh-users/zsh-syntax-highlighting.git /root/.oh-my-zsh/plugins/zsh-syntax-highlighting",
|
"/usr/bin/git clone https://github.com/zsh-users/zsh-syntax-highlighting.git /root/.oh-my-zsh/plugins/zsh-syntax-highlighting",
|
||||||
}
|
}
|
||||||
|
|
||||||
pluginsListUrl := []string{
|
pluginsListUrl := []string{
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package config
|
|||||||
import (
|
import (
|
||||||
"agent-wdd/log"
|
"agent-wdd/log"
|
||||||
"agent-wdd/utils"
|
"agent-wdd/utils"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -166,7 +167,7 @@ func (c *Config) NormalizeConfig() {
|
|||||||
// 重新读取配置
|
// 重新读取配置
|
||||||
InitConfig()
|
InitConfig()
|
||||||
|
|
||||||
// 主机名称应该为 City(格式为首字母大写)-amd64-公网IPv4(如果公网IPv4为空,则使用内网IPv4; ip的格式为127-0-0-1)
|
// 主机名称应该为 City(格式为首字母大写)-amd64-内网IP的最后一段(格式化为2位)-公网IPv4(如果公网IPv4为空,则使用内网IPv4; ip的格式为127-0-0-1)
|
||||||
|
|
||||||
// 获取城市(格式为首字母大写)
|
// 获取城市(格式为首字母大写)
|
||||||
city := strings.Title(ConfigCache.Agent.Network.Public.City)
|
city := strings.Title(ConfigCache.Agent.Network.Public.City)
|
||||||
@@ -175,6 +176,12 @@ func (c *Config) NormalizeConfig() {
|
|||||||
ipInfo := ConfigCache.Agent.Network.Public.IPv4
|
ipInfo := ConfigCache.Agent.Network.Public.IPv4
|
||||||
if ipInfo == "" {
|
if ipInfo == "" {
|
||||||
ipInfo = ConfigCache.Agent.Network.Interfaces[0].IPv4
|
ipInfo = ConfigCache.Agent.Network.Interfaces[0].IPv4
|
||||||
|
} else {
|
||||||
|
// 可以获取到公网IPv4, 修改IP的格式
|
||||||
|
innerIpv4 := ConfigCache.Agent.Network.Interfaces[0].IPv4
|
||||||
|
ipSegments := strings.Split(innerIpv4, ".")
|
||||||
|
prefix := fmt.Sprintf("%02s", ipSegments[len(ipSegments)-1])
|
||||||
|
ipInfo = prefix + "." + ipInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
ipInfo = strings.ReplaceAll(ipInfo, ".", "-")
|
ipInfo = strings.ReplaceAll(ipInfo, ".", "-")
|
||||||
|
|||||||
@@ -57,12 +57,22 @@ func (o *OS) Gather() {
|
|||||||
o.OsFamily = id
|
o.OsFamily = id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 判定系统是否是ubuntu类型
|
||||||
|
if o.OsFamily == "ubuntu" || o.OsFamily == "debian" || o.OsFamily == "linuxmint" || o.OsFamily == "elementary" || o.OsFamily == "pop" || o.OsFamily == "mint" || o.OsFamily == "kali" || o.OsFamily == "deepin" || o.OsFamily == "zorin" {
|
||||||
|
o.IsUbuntuType = true
|
||||||
|
} else {
|
||||||
|
// 设置系统为非ubuntus
|
||||||
|
o.IsUbuntuType = false
|
||||||
|
}
|
||||||
|
|
||||||
// 获取系统版本
|
// 获取系统版本
|
||||||
if version, ok := osInfo["VERSION_ID"]; ok {
|
if version, ok := osInfo["VERSION_ID"]; ok {
|
||||||
o.OsVersion = version
|
o.OsVersion = version
|
||||||
} else {
|
} else {
|
||||||
// 针对RedHat系特殊处理
|
// 针对RedHat系特殊处理
|
||||||
if o.OsFamily == "centos" || o.OsFamily == "rhel" {
|
if o.OsFamily == "centos" || o.OsFamily == "rhel" {
|
||||||
|
|
||||||
|
// 针对RedHat系特殊处理
|
||||||
if data, err := os.ReadFile("/etc/redhat-release"); err == nil {
|
if data, err := os.ReadFile("/etc/redhat-release"); err == nil {
|
||||||
re := regexp.MustCompile(`\d+(\.\d+)+`)
|
re := regexp.MustCompile(`\d+(\.\d+)+`)
|
||||||
if match := re.FindString(string(data)); match != "" {
|
if match := re.FindString(string(data)); match != "" {
|
||||||
@@ -78,16 +88,15 @@ func (o *OS) Gather() {
|
|||||||
o.Kernel = strings.TrimSpace(string(out))
|
o.Kernel = strings.TrimSpace(string(out))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查包管理的方式
|
|
||||||
if strings.Contains(o.OsFamily, "centos") || strings.Contains(o.OsFamily, "rhel") {
|
|
||||||
o.IsUbuntuType = false
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取系统架构
|
// 获取系统架构
|
||||||
o.Arch = runtime.GOARCH
|
o.Arch = runtime.GOARCH
|
||||||
|
|
||||||
// 获取系统发行版代号
|
// 获取系统发行版代号
|
||||||
o.OSReleaseCode = judgeUbuntuReleaseFromOsVersion(o.OsVersion)
|
if o.IsUbuntuType {
|
||||||
|
o.OSReleaseCode = judgeUbuntuReleaseFromOsVersion(o.OsVersion)
|
||||||
|
} else {
|
||||||
|
o.OSReleaseCode = "non-ubuntu"
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -233,7 +233,12 @@ func beautifulPrintListWithTitle(contend []string, title string) {
|
|||||||
fmt.Println()
|
fmt.Println()
|
||||||
fmt.Println(">>>>>>>> " + title + " <<<<<<<<")
|
fmt.Println(">>>>>>>> " + title + " <<<<<<<<")
|
||||||
for _, line := range contend {
|
for _, line := range contend {
|
||||||
|
if line == "EOF" {
|
||||||
|
// do nothing
|
||||||
|
continue
|
||||||
|
}
|
||||||
fmt.Println(line)
|
fmt.Println(line)
|
||||||
}
|
}
|
||||||
fmt.Println(">>>>>>>> end <<<<<<<<")
|
fmt.Println(">>>>>>>> end <<<<<<<<")
|
||||||
|
fmt.Println()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user