Skip to content

Commit

Permalink
fix(autok3s): fix ssh terminal bug
Browse files Browse the repository at this point in the history
  1. add tab completion
  2. fix terminal select logic bug

Signed-off-by: Jason-ZW <zhenyang@rancher.com>
  • Loading branch information
rancher-sy-bot committed Sep 28, 2020
1 parent c85e474 commit 3207fb8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 36 deletions.
45 changes: 24 additions & 21 deletions pkg/hosts/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/terminal"
)

type Tunnel struct {
Expand Down Expand Up @@ -41,40 +42,42 @@ func (t *Tunnel) Cmd(cmd string) *Tunnel {

func (t *Tunnel) Terminal() error {
session, err := t.conn.NewSession()
if err != nil {
return err
}

defer func() {
_ = session.Close()
}()

if t.Stdin == nil {
session.Stdin = os.Stdin
} else {
session.Stdin = t.Stdin
}
if t.Stdout == nil {
session.Stdout = os.Stdout
} else {
session.Stdout = t.Stdout
}
if t.Stderr == nil {
session.Stderr = os.Stderr
} else {
session.Stderr = t.Stderr
if err != nil {
return err
}

term := os.Getenv("TERM")
if term == "" {
t.Term = "xterm-256color"
}
t.Height = 40
t.Weight = 80
t.Modes = ssh.TerminalModes{
ssh.ECHO: 1,
ssh.VSTATUS: 1,
ssh.TTY_OP_ISPEED: 14400,
ssh.TTY_OP_OSPEED: 14400,
}

fd := int(os.Stdin.Fd())
oldState, err := terminal.MakeRaw(fd)
defer func() {
_ = terminal.Restore(fd, oldState)
}()
if err != nil {
return err
}

t.Weight, t.Height, err = terminal.GetSize(fd)
if err != nil {
return err
}

session.Stdin = os.Stdin
session.Stdout = os.Stdout
session.Stderr = os.Stderr

if err := session.RequestPty(t.Term, t.Height, t.Weight, t.Modes); err != nil {
return err
}
Expand Down
33 changes: 30 additions & 3 deletions pkg/providers/alibaba/alibaba.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,40 @@ func (p *Alibaba) SSHK3sNode() error {
ids := make(map[string]string, len(response.Instances.Instance))
for _, instance := range response.Instances.Instance {
if instance.EipAddress.IpAddress != "" {
ids[instance.InstanceId] = instance.EipAddress.IpAddress
for _, t := range instance.Tags.Tag {
switch t.TagKey {
case "master":
if t.TagValue == "true" {
ids[instance.InstanceId] = instance.EipAddress.IpAddress + " (master)"
}
case "worker":
if t.TagValue == "true" {
ids[instance.InstanceId] = instance.EipAddress.IpAddress + " (worker)"
}
default:
continue
}
}
} else if instance.EipAddress.IpAddress == "" && len(instance.PublicIpAddress.IpAddress) > 0 {
ids[instance.InstanceId] = instance.PublicIpAddress.IpAddress[0]
for _, t := range instance.Tags.Tag {
switch t.TagKey {
case "master":
if t.TagValue == "true" {
ids[instance.InstanceId] = instance.PublicIpAddress.IpAddress[0] + " (master)"
}
case "worker":
if t.TagValue == "true" {
ids[instance.InstanceId] = instance.PublicIpAddress.IpAddress[0] + " (worker)"
}
default:
continue
}
}

}
}

ip := utils.AskForSelectItem(fmt.Sprintf("[%s] choose ssh node to connect", p.GetProviderName()), ids)
ip := strings.Split(utils.AskForSelectItem(fmt.Sprintf("[%s] choose ssh node to connect", p.GetProviderName()), ids), " (")[0]

if ip == "" {
return fmt.Errorf("[%s] choose incorrect ssh node", p.GetProviderName())
Expand Down
16 changes: 4 additions & 12 deletions pkg/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/hex"
"fmt"
"os"
"strconv"
"strings"
"text/template"

Expand All @@ -16,8 +15,8 @@ import (

const (
tmpl = `
{{- range $key, $element := .}}
{{$key}}: {{$element}}
{{- range $key, $value := .}}
- {{$key}}: {{$value}}
{{- end}}
`
)
Expand Down Expand Up @@ -76,17 +75,10 @@ func AskForSelectItem(s string, ss map[string]string) string {
if err := t.Execute(buffer, ss); err != nil {
return ""
}
fmt.Printf("%s: \n \t%s\n[choose 0-%d]:", s, buffer.String(), len(ss))
fmt.Printf("%s: \n \t%s\n[choose one id]: ", s, buffer.String())
response, err := reader.ReadString('\n')
if err != nil {
logrus.Fatal(err)
}
index := 0
for _, v := range ss {
if strconv.Itoa(index) == strings.ToLower(strings.TrimSpace(response)) {
return v
}
index++
}
return ""
return ss[strings.ToLower(strings.TrimSpace(response))]
}

0 comments on commit 3207fb8

Please # to comment.