Skip to content

Commit 012ca67

Browse files
committed
Merge pull request #261 from QuentinPerez/fix_#194
Fix #194 1/2
2 parents cc6907e + 3839fc9 commit 012ca67

File tree

4 files changed

+82
-38
lines changed

4 files changed

+82
-38
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,8 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
11541154

11551155
### master (unreleased)
11561156

1157-
# Add a warn message when using `ssh exec` on host without public ip nor gateway ([#171](https://github.com/scaleway/scaleway-cli/issues/171))
1157+
* Add a warn message when using `ssh exec` on host without public ip nor gateway ([#171](https://github.com/scaleway/scaleway-cli/issues/171))
1158+
* Display `ssh-host-fingerprints` when it's available ([#194](https://github.com/scaleway/scaleway-cli/issues/194))
11581159
* Support of `scw rmi` snapshot|volume ([#258](https://github.com/scaleway/scaleway-cli/issues/258))
11591160
* Match bootscript/image with the good architecture ([#255](https://github.com/scaleway/scaleway-cli/issues/255))
11601161
* Support of region/owner/arch in the cache file ([#255](https://github.com/scaleway/scaleway-cli/issues/255))

pkg/api/helpers.go

+15
Original file line numberDiff line numberDiff line change
@@ -601,3 +601,18 @@ func (a *ScalewayAPI) DeleteServerSafe(serverID string) error {
601601
logrus.Errorf("Try to run 'scw rm -f %s' later", serverID)
602602
return err
603603
}
604+
605+
// GetSSHFingerprintFromServer returns an array which containts ssh-host-fingerprints
606+
func (a *ScalewayAPI) GetSSHFingerprintFromServer(serverID string) []string {
607+
ret := []string{}
608+
609+
if value, err := a.GetUserdata(serverID, "ssh-host-fingerprints"); err == nil {
610+
PublicKeys := strings.Split(string(*value), "\n")
611+
for i := range PublicKeys {
612+
if fingerprint, err := utils.SSHGetFingerprint([]byte(PublicKeys[i])); err == nil {
613+
ret = append(ret, fingerprint)
614+
}
615+
}
616+
}
617+
return ret
618+
}

pkg/commands/exec.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,19 @@ type ExecArgs struct {
2525

2626
// RunExec is the handler for 'scw exec'
2727
func RunExec(ctx CommandContext, args ExecArgs) error {
28+
var fingerprints []string
29+
30+
done := make(chan struct{})
31+
2832
serverID, err := ctx.API.GetServerID(args.Server)
2933
if err != nil {
3034
return err
3135
}
3236

37+
go func() {
38+
fingerprints = ctx.API.GetSSHFingerprintFromServer(serverID)
39+
close(done)
40+
}()
3341
// Resolve gateway
3442
if args.Gateway == "" {
3543
args.Gateway = ctx.Getenv("SCW_GATEWAY")
@@ -80,8 +88,13 @@ func RunExec(ctx CommandContext, args ExecArgs) error {
8088
}()
8189
}
8290

83-
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args.Command, !args.Wait, gateway)
84-
if err != nil {
91+
<-done
92+
if len(fingerprints) > 0 {
93+
for i := range fingerprints {
94+
fmt.Fprintf(ctx.Stdout, "%s\n", fingerprints[i])
95+
}
96+
}
97+
if err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args.Command, !args.Wait, gateway); err != nil {
8598
return fmt.Errorf("Failed to run the command: %v", err)
8699
}
87100

pkg/commands/run.go

+50-35
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,50 @@ func addUserData(ctx CommandContext, userdatas []string, serverID string) {
9696
}
9797
}
9898

99+
func runShowBoot(ctx CommandContext, args RunArgs, serverID string, closeTimeout chan struct{}, timeoutExit chan struct{}) error {
100+
// Attach to server serial
101+
logrus.Info("Attaching to server console ...")
102+
gottycli, done, err := utils.AttachToSerial(serverID, ctx.API.Token)
103+
if err != nil {
104+
close(closeTimeout)
105+
return fmt.Errorf("cannot attach to server serial: %v", err)
106+
}
107+
utils.Quiet(true)
108+
notif, gateway, err := waitSSHConnection(ctx, args, serverID)
109+
if err != nil {
110+
close(closeTimeout)
111+
gottycli.ExitLoop()
112+
<-done
113+
return err
114+
}
115+
select {
116+
case <-timeoutExit:
117+
gottycli.ExitLoop()
118+
<-done
119+
utils.Quiet(false)
120+
return fmt.Errorf("Operation timed out")
121+
case sshConnection := <-notif:
122+
close(closeTimeout)
123+
gottycli.ExitLoop()
124+
<-done
125+
utils.Quiet(false)
126+
if sshConnection.err != nil {
127+
return sshConnection.err
128+
}
129+
if fingerprints := ctx.API.GetSSHFingerprintFromServer(serverID); len(fingerprints) > 0 {
130+
for i := range fingerprints {
131+
fmt.Fprintf(ctx.Stdout, "%s\n", fingerprints[i])
132+
}
133+
}
134+
server := sshConnection.server
135+
logrus.Info("Connecting to server ...")
136+
if err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, []string{}, false, gateway); err != nil {
137+
return fmt.Errorf("Connection to server failed: %v", err)
138+
}
139+
}
140+
return nil
141+
}
142+
99143
// Run is the handler for 'scw run'
100144
func Run(ctx CommandContext, args RunArgs) error {
101145
if args.Gateway == "" {
@@ -163,41 +207,7 @@ func Run(ctx CommandContext, args RunArgs) error {
163207
}()
164208
}
165209
if args.ShowBoot {
166-
// Attach to server serial
167-
logrus.Info("Attaching to server console ...")
168-
gottycli, done, err := utils.AttachToSerial(serverID, ctx.API.Token)
169-
if err != nil {
170-
close(closeTimeout)
171-
return fmt.Errorf("cannot attach to server serial: %v", err)
172-
}
173-
utils.Quiet(true)
174-
notif, gateway, err := waitSSHConnection(ctx, args, serverID)
175-
if err != nil {
176-
close(closeTimeout)
177-
gottycli.ExitLoop()
178-
<-done
179-
return err
180-
}
181-
select {
182-
case <-timeoutExit:
183-
gottycli.ExitLoop()
184-
<-done
185-
utils.Quiet(false)
186-
return fmt.Errorf("Operation timed out")
187-
case sshConnection := <-notif:
188-
close(closeTimeout)
189-
gottycli.ExitLoop()
190-
<-done
191-
utils.Quiet(false)
192-
if sshConnection.err != nil {
193-
return sshConnection.err
194-
}
195-
server := sshConnection.server
196-
logrus.Info("Connecting to server ...")
197-
if err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, []string{}, false, gateway); err != nil {
198-
return fmt.Errorf("Connection to server failed: %v", err)
199-
}
200-
}
210+
return runShowBoot(ctx, args, serverID, closeTimeout, timeoutExit)
201211
} else if args.Attach {
202212
// Attach to server serial
203213
logrus.Info("Attaching to server console ...")
@@ -222,6 +232,11 @@ func Run(ctx CommandContext, args RunArgs) error {
222232
if sshConnection.err != nil {
223233
return sshConnection.err
224234
}
235+
if fingerprints := ctx.API.GetSSHFingerprintFromServer(serverID); len(fingerprints) > 0 {
236+
for i := range fingerprints {
237+
fmt.Fprintf(ctx.Stdout, "%s\n", fingerprints[i])
238+
}
239+
}
225240
server := sshConnection.server
226241
// exec -w SERVER COMMAND ARGS...
227242
if len(args.Command) < 1 {

0 commit comments

Comments
 (0)