Skip to content

Commit 91e79ea

Browse files
committed
setup main node pubkey after gen key
1 parent fa7439a commit 91e79ea

File tree

2 files changed

+59
-27
lines changed

2 files changed

+59
-27
lines changed

app/job_ssh.go

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -276,17 +276,62 @@ func (m ModJobSshStruct) genOrGetKey() {
276276
}
277277
util.MainNodeConfWriter{}.WriteSecretConf(util.SecretConfTypeSshPrivate{}, string(localpri))
278278

279-
_, err = os.ReadFile(ed25519PubFilePath)
279+
localpub, err := os.ReadFile(ed25519PubFilePath)
280280
if err != nil {
281281
fail = true
282282
failInfo = fmt.Sprintf("failed to read local pub key: %v", err)
283283
return
284284
}
285285

286-
util.MainNodeConfWriter{}.WriteSecretConf(util.SecretConfTypeSshPrivate{}, string(localpri))
286+
util.MainNodeConfWriter{}.WriteSecretConf(util.SecretConfTypeSshPublic{}, string(localpub))
287+
288+
util.PrintStep("ssh genOrGetKey", color.BlueString("uploading keys to self"))
289+
password, ok := util.GetPassword("设置ssh免密访问需要配置密码")
290+
if !ok {
291+
fmt.Println("User canceled config ssh no pw access")
292+
os.Exit(1)
293+
}
294+
m.setupClusterInner(clusterconf.ClusterConfYmlModel{
295+
Global: clusterconf.ClusterConfYmlModelGlobal{
296+
SshUser: util.MainNodeUser,
297+
SshPasswd: password,
298+
},
299+
Nodes: map[string]clusterconf.ClusterConfYmlModelNode{
300+
"dummy": {
301+
Ip: util.MainNodeIp,
302+
},
303+
},
304+
})
287305
}
288306
}
289307

308+
func (m ModJobSshStruct) setupClusterInner(clusterConf clusterconf.ClusterConfYmlModel) {
309+
// 打印解析后的内容
310+
fmt.Printf("集群配置: %+v\n", clusterConf)
311+
312+
hosts := funk.Map(clusterConf.Nodes, func(_ string, node clusterconf.ClusterConfYmlModelNode) string {
313+
return fmt.Sprintf("%s@%s", clusterConf.Global.SshUser, node.Ip)
314+
}).([]string)
315+
316+
// read pubkey
317+
pubkeyFile := filepath.Join(homedir.HomeDir(), ".ssh", "id_ed25519.pub")
318+
pubkeybytes, err := os.ReadFile(pubkeyFile)
319+
if err != nil {
320+
fmt.Println(color.RedString("read pubkey failed: %v", err))
321+
os.Exit(1)
322+
}
323+
_ = base64.StdEncoding.EncodeToString(pubkeybytes)
324+
325+
util.StartRemoteCmds(
326+
hosts,
327+
// install telego,
328+
util.ModRunCmd.CmdModels().InstallTelegoWithPy()+" && "+
329+
// update authorized_keys
330+
strings.Join(m.NewSshCmd(SshJob{Mode: SshModeSetupThisNode}.ModeString()), " "),
331+
clusterConf.Global.SshPasswd,
332+
)
333+
}
334+
290335
// https://qcnoe3hd7k5c.feishu.cn/wiki/V6eHwZm1aiofeykaSd5cmgPcnSe#share-Hc1hdGT26oI4I0xPaplcEhMundd
291336
func (m ModJobSshStruct) setupCluster() {
292337
ok, yamlFilePath := util.StartTemporaryInputUI(color.GreenString(
@@ -313,30 +358,7 @@ func (m ModJobSshStruct) setupCluster() {
313358
os.Exit(1)
314359
}
315360

316-
// 打印解析后的内容
317-
fmt.Printf("解析后的集群配置: %+v\n", clusterConf)
318-
319-
hosts := funk.Map(clusterConf.Nodes, func(nodename string, node clusterconf.ClusterConfYmlModelNode) string {
320-
return fmt.Sprintf("%s@%s", clusterConf.Global.SshUser, node.Ip)
321-
}).([]string)
322-
323-
// read pubkey
324-
pubkeyFile := filepath.Join(homedir.HomeDir(), ".ssh", "id_ed25519.pub")
325-
pubkeybytes, err := os.ReadFile(pubkeyFile)
326-
if err != nil {
327-
fmt.Println(color.RedString("read pubkey failed: %v", err))
328-
os.Exit(1)
329-
}
330-
_ = base64.StdEncoding.EncodeToString(pubkeybytes)
331-
332-
util.StartRemoteCmds(
333-
hosts,
334-
// install telego,
335-
util.ModRunCmd.CmdModels().InstallTelegoWithPy()+" && "+
336-
// update authorized_keys
337-
strings.Join(m.NewSshCmd(SshJob{Mode: SshModeSetupThisNode}.ModeString()), " "),
338-
clusterConf.Global.SshPasswd,
339-
)
361+
m.setupClusterInner(clusterConf)
340362
}
341363

342364
func (m ModJobSshStruct) NewSshCmd(

test/test3_main_node_config/config_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,18 @@ func TestSSHKeyGeneration(t *testing.T) {
7171

7272
t.Logf("telego log for gen or get key:\n %s", testutil.GetMostRecentLog(t))
7373

74+
t.Logf("debug authorized_keys:")
75+
sshCmd := exec.Command("sshpass", "-p", util.MainNodeUser, "ssh",
76+
"-o", "StrictHostKeyChecking=no",
77+
"-o", "UserKnownHostsFile=/dev/null",
78+
"-p", "2222", util.MainNodeUser+"@"+util.MainNodeIp, "cat", "~/.ssh/authorized_keys")
79+
if err := testutil.RunCommand(t, sshCmd); err != nil {
80+
t.Fatalf("SSH 连接 abc 测试失败: %v", err)
81+
}
82+
7483
// 测试 SSH 连接
75-
sshCmd := exec.Command("ssh",
84+
t.Logf("test ssh no pw access")
85+
sshCmd = exec.Command("ssh",
7686
"-o", "StrictHostKeyChecking=no",
7787
"-o", "UserKnownHostsFile=/dev/null",
7888
"-p", "2222", util.MainNodeUser+"@"+util.MainNodeIp, "echo", "test")

0 commit comments

Comments
 (0)