Skip to content

Commit a51cbb9

Browse files
committed
main node port in genOrGetKey
1 parent 5ad75fd commit a51cbb9

File tree

4 files changed

+45
-22
lines changed

4 files changed

+45
-22
lines changed

app/job_ssh.go

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"path/filepath"
1010
"regexp"
11+
"strconv"
1112
"strings"
1213
"telego/util"
1314
clusterconf "telego/util/cluster_conf"
@@ -291,14 +292,22 @@ func (m ModJobSshStruct) genOrGetKey() {
291292
fmt.Println("User canceled config ssh no pw access")
292293
os.Exit(1)
293294
}
295+
296+
mainNodePort, err := strconv.Atoi(util.MainNodeSshPort)
297+
if err != nil {
298+
fmt.Println(color.RedString("failed to convert main node ssh port to int: %v", err))
299+
os.Exit(1)
300+
}
301+
294302
m.setupClusterInner(clusterconf.ClusterConfYmlModel{
295303
Global: clusterconf.ClusterConfYmlModelGlobal{
296304
SshUser: util.MainNodeUser,
297305
SshPasswd: password,
298306
},
299307
Nodes: map[string]clusterconf.ClusterConfYmlModelNode{
300308
"dummy": {
301-
Ip: util.MainNodeIp,
309+
Ip: util.MainNodeIp,
310+
Port: mainNodePort,
302311
},
303312
},
304313
})
@@ -310,6 +319,9 @@ func (m ModJobSshStruct) setupClusterInner(clusterConf clusterconf.ClusterConfYm
310319
fmt.Printf("集群配置: %+v\n", clusterConf)
311320

312321
hosts := funk.Map(clusterConf.Nodes, func(_ string, node clusterconf.ClusterConfYmlModelNode) string {
322+
if node.Port != 0 {
323+
return fmt.Sprintf("%s@%s:%d", clusterConf.Global.SshUser, node.Ip, node.Port)
324+
}
313325
return fmt.Sprintf("%s@%s", clusterConf.Global.SshUser, node.Ip)
314326
}).([]string)
315327

@@ -322,30 +334,31 @@ func (m ModJobSshStruct) setupClusterInner(clusterConf clusterconf.ClusterConfYm
322334
}
323335
_ = base64.StdEncoding.EncodeToString(pubkeybytes)
324336

325-
output, logfps := util.StartRemoteCmds(
337+
// output, logfps :=
338+
util.StartRemoteCmds(
326339
hosts,
327-
// install telego,
328-
util.ModRunCmd.CmdModels().InstallTelegoWithPy()+"; "+
340+
341+
util.ModRunCmd.CmdModels().InstallTelegoWithPy()+" && "+
329342
// update authorized_keys
330343
strings.Join(m.NewSshCmd(SshJob{Mode: SshModeSetupThisNode}.ModeString()), " "),
331344
clusterConf.Global.SshPasswd,
332345
)
333-
logfdebug, err := os.ReadFile(logfps[0] + ".debug")
334-
if err != nil {
335-
fmt.Println(color.RedString("read logfdebug failed: %v", err))
336-
os.Exit(1)
337-
}
338-
srcs := []string{output[0], string(logfdebug)}
339-
keywords := []string{"error", "失败"}
340-
if funk.Any(srcs, func(src string) bool {
341-
return funk.Any(keywords, func(keyword string) bool {
342-
return strings.Contains(src, keyword)
343-
})
344-
}) {
345-
fmt.Println(color.RedString("ssh error: %s", output))
346-
// debug sshd_config
347-
util.ModRunCmd.NewBuilder("cat", "/etc/ssh/sshd_config").WithRoot().ShowProgress().BlockRun()
348-
}
346+
// logfdebug, err := os.ReadFile(logfps[0] + ".debug")
347+
// if err != nil {
348+
// fmt.Println(color.RedString("read logfdebug failed: %v", err))
349+
// os.Exit(1)
350+
// }
351+
// srcs := []string{output[0], string(logfdebug)}
352+
// keywords := []string{"error", "失败"}
353+
// if funk.Any(srcs, func(src string) bool {
354+
// return funk.Any(keywords, func(keyword string) bool {
355+
// return strings.Contains(src, keyword)
356+
// })
357+
// }) {
358+
// fmt.Println(color.RedString("ssh error: %s", output))
359+
// // debug sshd_config
360+
// util.ModRunCmd.NewBuilder("cat", "/etc/ssh/sshd_config").WithRoot().ShowProgress().BlockRun()
361+
// }
349362
}
350363

351364
// https://qcnoe3hd7k5c.feishu.cn/wiki/V6eHwZm1aiofeykaSd5cmgPcnSe#share-Hc1hdGT26oI4I0xPaplcEhMundd

test/test3_main_node_config/config_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package test3_main_node_config
33
import (
44
"os/exec"
55
"path"
6+
"path/filepath"
67
"telego/test/testutil"
78
"telego/util"
89
"testing"
@@ -59,6 +60,13 @@ func TestSSHKeyGeneration(t *testing.T) {
5960
t.Fatalf("初始化main node file server失败: %v", err)
6061
}
6162

63+
// upload telego
64+
t.Logf("upload telego to main node")
65+
_, err = util.ModRunCmd.NewBuilder("python3", filepath.Join(projectRoot, "3.upload.py")).ShowProgress().BlockRun()
66+
if err != nil {
67+
t.Fatalf("上传telego到 main node 失败: %v", err)
68+
}
69+
6270
t.Logf("telego log for start main node file server:\n %s", testutil.GetMostRecentLog(t))
6371

6472
// 重新生成 SSH 密钥

test/testutil/docker.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func RunSSHDocker(t *testing.T) (string, func()) {
5252

5353
// 拉取并运行构建镜像,映射项目目录
5454
cmd := exec.Command("docker", "run", "-d",
55-
"-p", "2222:22", // for test main node ssh
55+
"-p", "2222:2222", // for test main node ssh
5656
"-p", "8003:8003", // for test fileserver
5757
"-v", hostProjectPath+":/telego",
5858
"telego_build",
@@ -103,7 +103,8 @@ func RunSSHDocker(t *testing.T) (string, func()) {
103103
// 配置SSH服务器和创建用户
104104
t.Log("RunSSHDocker 配置SSH服务器和创建用户")
105105
configSSHCmd := exec.Command("docker", "exec", containerID, "bash", "-c",
106-
"echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config && "+
106+
"echo 'Port 2222' >> /etc/ssh/sshd_config && "+
107+
"echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config && "+
107108
"echo 'root:password' | chpasswd && "+
108109
"useradd -m -s /bin/bash abc && "+
109110
"echo 'abc:abc' | chpasswd && "+

util/cluster_conf/lib.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type ClusterConfYmlModelGlobal struct {
1010

1111
type ClusterConfYmlModelNode struct {
1212
Ip string `yaml:"ip"`
13+
Port int `yaml:"port,omitempty"`
1314
Tags []string `yaml:"tags,omitempty"`
1415
}
1516

0 commit comments

Comments
 (0)