Skip to content

Commit

Permalink
Merge pull request #261 from QuentinPerez/fix_#194
Browse files Browse the repository at this point in the history
Fix #194 1/2
  • Loading branch information
moul committed Jan 11, 2016
2 parents cc6907e + 3839fc9 commit 012ca67
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 38 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,8 @@ $ scw inspect myserver | jq '.[0].public_ip.address'

### master (unreleased)

# Add a warn message when using `ssh exec` on host without public ip nor gateway ([#171](https://github.com/scaleway/scaleway-cli/issues/171))
* Add a warn message when using `ssh exec` on host without public ip nor gateway ([#171](https://github.com/scaleway/scaleway-cli/issues/171))
* Display `ssh-host-fingerprints` when it's available ([#194](https://github.com/scaleway/scaleway-cli/issues/194))
* Support of `scw rmi` snapshot|volume ([#258](https://github.com/scaleway/scaleway-cli/issues/258))
* Match bootscript/image with the good architecture ([#255](https://github.com/scaleway/scaleway-cli/issues/255))
* Support of region/owner/arch in the cache file ([#255](https://github.com/scaleway/scaleway-cli/issues/255))
Expand Down
15 changes: 15 additions & 0 deletions pkg/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,3 +601,18 @@ func (a *ScalewayAPI) DeleteServerSafe(serverID string) error {
logrus.Errorf("Try to run 'scw rm -f %s' later", serverID)
return err
}

// GetSSHFingerprintFromServer returns an array which containts ssh-host-fingerprints
func (a *ScalewayAPI) GetSSHFingerprintFromServer(serverID string) []string {
ret := []string{}

if value, err := a.GetUserdata(serverID, "ssh-host-fingerprints"); err == nil {
PublicKeys := strings.Split(string(*value), "\n")
for i := range PublicKeys {
if fingerprint, err := utils.SSHGetFingerprint([]byte(PublicKeys[i])); err == nil {
ret = append(ret, fingerprint)
}
}
}
return ret
}
17 changes: 15 additions & 2 deletions pkg/commands/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ type ExecArgs struct {

// RunExec is the handler for 'scw exec'
func RunExec(ctx CommandContext, args ExecArgs) error {
var fingerprints []string

done := make(chan struct{})

serverID, err := ctx.API.GetServerID(args.Server)
if err != nil {
return err
}

go func() {
fingerprints = ctx.API.GetSSHFingerprintFromServer(serverID)
close(done)
}()
// Resolve gateway
if args.Gateway == "" {
args.Gateway = ctx.Getenv("SCW_GATEWAY")
Expand Down Expand Up @@ -80,8 +88,13 @@ func RunExec(ctx CommandContext, args ExecArgs) error {
}()
}

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

Expand Down
85 changes: 50 additions & 35 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,50 @@ func addUserData(ctx CommandContext, userdatas []string, serverID string) {
}
}

func runShowBoot(ctx CommandContext, args RunArgs, serverID string, closeTimeout chan struct{}, timeoutExit chan struct{}) error {
// Attach to server serial
logrus.Info("Attaching to server console ...")
gottycli, done, err := utils.AttachToSerial(serverID, ctx.API.Token)
if err != nil {
close(closeTimeout)
return fmt.Errorf("cannot attach to server serial: %v", err)
}
utils.Quiet(true)
notif, gateway, err := waitSSHConnection(ctx, args, serverID)
if err != nil {
close(closeTimeout)
gottycli.ExitLoop()
<-done
return err
}
select {
case <-timeoutExit:
gottycli.ExitLoop()
<-done
utils.Quiet(false)
return fmt.Errorf("Operation timed out")
case sshConnection := <-notif:
close(closeTimeout)
gottycli.ExitLoop()
<-done
utils.Quiet(false)
if sshConnection.err != nil {
return sshConnection.err
}
if fingerprints := ctx.API.GetSSHFingerprintFromServer(serverID); len(fingerprints) > 0 {
for i := range fingerprints {
fmt.Fprintf(ctx.Stdout, "%s\n", fingerprints[i])
}
}
server := sshConnection.server
logrus.Info("Connecting to server ...")
if err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, []string{}, false, gateway); err != nil {
return fmt.Errorf("Connection to server failed: %v", err)
}
}
return nil
}

// Run is the handler for 'scw run'
func Run(ctx CommandContext, args RunArgs) error {
if args.Gateway == "" {
Expand Down Expand Up @@ -163,41 +207,7 @@ func Run(ctx CommandContext, args RunArgs) error {
}()
}
if args.ShowBoot {
// Attach to server serial
logrus.Info("Attaching to server console ...")
gottycli, done, err := utils.AttachToSerial(serverID, ctx.API.Token)
if err != nil {
close(closeTimeout)
return fmt.Errorf("cannot attach to server serial: %v", err)
}
utils.Quiet(true)
notif, gateway, err := waitSSHConnection(ctx, args, serverID)
if err != nil {
close(closeTimeout)
gottycli.ExitLoop()
<-done
return err
}
select {
case <-timeoutExit:
gottycli.ExitLoop()
<-done
utils.Quiet(false)
return fmt.Errorf("Operation timed out")
case sshConnection := <-notif:
close(closeTimeout)
gottycli.ExitLoop()
<-done
utils.Quiet(false)
if sshConnection.err != nil {
return sshConnection.err
}
server := sshConnection.server
logrus.Info("Connecting to server ...")
if err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, []string{}, false, gateway); err != nil {
return fmt.Errorf("Connection to server failed: %v", err)
}
}
return runShowBoot(ctx, args, serverID, closeTimeout, timeoutExit)
} else if args.Attach {
// Attach to server serial
logrus.Info("Attaching to server console ...")
Expand All @@ -222,6 +232,11 @@ func Run(ctx CommandContext, args RunArgs) error {
if sshConnection.err != nil {
return sshConnection.err
}
if fingerprints := ctx.API.GetSSHFingerprintFromServer(serverID); len(fingerprints) > 0 {
for i := range fingerprints {
fmt.Fprintf(ctx.Stdout, "%s\n", fingerprints[i])
}
}
server := sshConnection.server
// exec -w SERVER COMMAND ARGS...
if len(args.Command) < 1 {
Expand Down

0 comments on commit 012ca67

Please sign in to comment.