[ Agent ] [ BASE ] - add default ssh key install

This commit is contained in:
zeaslity
2023-11-21 10:35:43 +08:00
parent c876701099
commit 2f5ae75d61
5 changed files with 120 additions and 6 deletions

View File

@@ -115,13 +115,28 @@ func (op *AgentOsOperator) deployRke(funcArgs []string) (bool, []string) {
if !ok {
return false, resultLog
}
ok, resultLog = BasicDownloadFile(op.OssOfflinePrefix+"rke-cluster-template.yaml", "/root/wdd/rke-cluster.yml")
ok, resultLog = BasicDownloadFile(op.OssOfflinePrefix+"rke-cluster-template.yaml", "/root/wdd/cluster.yml")
if !ok {
return false, resultLog
}
AllCompleteExecutor([][]string{
{
"chmod",
"+x",
"/usr/local/bin/rke",
},
{
"chmod",
"+x",
"/usr/local/bin/kubectl",
},
})
//
// replace ip addr
parseIP := net.ParseIP(funcArgs[1])
parseIP := net.ParseIP(funcArgs[0])
if parseIP == nil {
return false, []string{
"[deployRke] - ip args error !",
@@ -159,7 +174,7 @@ func (op *AgentOsOperator) deployK8sDashboard(funcArgs []string) (bool, []string
// kubectl
// replace
parseIP := net.ParseIP(funcArgs[1])
parseIP := net.ParseIP(funcArgs[0])
if parseIP == nil {
return false, []string{
"[deployK8sDashboard] - ip args error !",

View File

@@ -67,6 +67,9 @@ func (op *AgentOsOperator) Exec(baseFuncName string, funcArgs ...string) (bool,
case "installDockerCompose":
resultOk, errorLog = op.installDockerComposeExec()
break
case "installDefaultSSHKey":
resultOk, errorLog = op.installDefaultSSHKeyExec(funcArgs)
break
case "modifyDockerConfig":
resultOk, errorLog = op.modifyDockerConfigExec(funcArgs)
break
@@ -316,6 +319,46 @@ func (op *AgentOsOperator) disableSwapExec() (bool, []string) {
return true, resultLog
}
func (op *AgentOsOperator) installDefaultSSHKeyExec(funcArgs []string) (bool, []string) {
// ssh-keygen -t ed25519 -C "wdd@cmii.com" -N "octopus_standard_phrase"
// check key exists
if BasicFileExistAndNotNull("/root/.ssh/id_ed25519") {
if BasicFileExistAndNotNull("/root/.ssh/id_ed25519.pub") {
if BasicGrepItemInFile("wdd@cmii.com", "/root/.ssh/authorized_keys") {
return true, nil
}
}
}
// download standard private and public key
ok, resultLog := BasicDownloadFile(op.OssOfflinePrefix+"id_ed25519_private_key", "/root/.ssh/id_ed25519")
if !ok {
return false, resultLog
}
ok, resultLog = BasicDownloadFile(op.OssOfflinePrefix+"id_ed25519_public_key.pub", "/root/.ssh/id_ed25519.pub")
if !ok {
return false, resultLog
}
// write into authorized_keys
if !BasicAppendSourceToFile("/root/.ssh/id_ed25519.pub", "/root/.ssh/authorized_keys") {
return false, []string{
"[installDefaultSSHKeyExec] - error appending ssh key to authorized_keys !",
}
}
// check
if BasicGrepItemInFile("wdd@cmii.com", "/root/.ssh/authorized_keys") {
return true, nil
}
return false, []string{
"[installDefaultSSHKeyExec] - authorized_keys don't contain the ssh-pub key !",
}
}
func (op *AgentOsOperator) removeDocker() [][]string {
removeDockerLine := append(op.RemoveCommandPrefix, []string{

View File

@@ -3,6 +3,8 @@ package executor
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"strings"
)
@@ -87,6 +89,32 @@ func BasicFileExistInFolder(fileName string, folderList ...string) bool {
return false
}
func BasicGrepItemInFile(item string, fileName string) bool {
if !BasicFileExistAndNotNull(fileName) {
log.ErrorF("[BasicGrepItemInFile] - fileName [ %s ] not exits !", fileName)
return false
}
ok, _ := PipelineCommandExecutor([][]string{
{
"cat",
fileName,
},
{
"grep",
"-q",
item,
},
})
if ok {
return true
}
return false
}
// BasicReplace 基础替换命令
func BasicReplace(filename string, origin string, replace string) bool {
@@ -340,3 +368,31 @@ func BasicDownloadFile(downloadUrl, desFile string) (downloadOk bool, resultLog
return true, nil
}
// BasicAppendSourceToFile 将源文件的内容添加到目标文件使用golang标准库完成跨平台、安全性更强
func BasicAppendSourceToFile(sourceFile, targetFile string) bool {
// 打开源文件
source, err := os.Open(sourceFile)
if err != nil {
log.ErrorF("[BasicAppendSourceToFile] - error open source file => %s", sourceFile)
return false
}
defer source.Close()
// 打开目标文件,如果不存在则创建,如果存在则在末尾追加
target, err := os.OpenFile(targetFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.ErrorF("[BasicAppendSourceToFile] - error open target file => %s", sourceFile)
return false
}
defer target.Close()
// 将源文件内容复制到目标文件
_, err = io.Copy(target, source)
if err != nil {
log.ErrorF("[BasicAppendSourceToFile] - Error appending to target file: %s", err.Error())
return false
}
return true
}

View File

@@ -94,8 +94,8 @@ public class AppFuncScheduler {
);
List<AppFunctionEnum> appFunctionEnumList = List.of(
AppFunctionEnum.DEPLOY_RKE,
AppFunctionEnum.DEPLOY_K8S_DASHBOARD
AppFunctionEnum.DEPLOY_RKE
// AppFunctionEnum.DEPLOY_K8S_DASHBOARD
);