Skip to content

Commit

Permalink
Support of scw --gateway=servername option (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Jul 13, 2015
1 parent 793c4f6 commit b856b2e
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 16 deletions.
31 changes: 31 additions & 0 deletions api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package api

import (
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -37,6 +38,36 @@ type ScalewayImageInterface struct {
Type string
}

func ResolveGateway(api *ScalewayAPI, gateway string) (string, error) {
if gateway == "" {
return "", nil
}

// Parses optional type prefix, i.e: "server:name" -> "name"
_, gateway = parseNeedle(gateway)

servers, err := api.ResolveServer(gateway)
if err != nil {
return "", err
}

if len(servers) == 0 {
return gateway, nil
}

if len(servers) > 1 {
showResolverResults(gateway, servers)
return "", errors.New(fmt.Sprintf("Gateway '%s' is ambiguous", gateway))
}

// if len(servers) == 1 {
server, err := api.GetServer(servers[0].Identifier)
if err != nil {
return "", err
}
return server.PublicAddress.IP, nil
}

// CreateVolumeFromHumanSize creates a volume on the API with a human readable size
func CreateVolumeFromHumanSize(api *ScalewayAPI, size string) (*string, error) {
bytes, err := humanize.ParseBytes(size)
Expand Down
28 changes: 20 additions & 8 deletions commands/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var cpHelp bool // -h, --help flag
var cpGateway string // -g, --gateway flag

// TarFromSource creates a stream buffer with the tarballed content of the user source
func TarFromSource(api *api.ScalewayAPI, source string) (*io.ReadCloser, error) {
func TarFromSource(apiClient *api.ScalewayAPI, source string) (*io.ReadCloser, error) {
var tarOutputStream io.ReadCloser

// source is a server address + path (scp-like uri)
Expand All @@ -63,9 +63,9 @@ func TarFromSource(api *api.ScalewayAPI, source string) (*io.ReadCloser, error)
return nil, fmt.Errorf("invalid source uri, see 'scw cp -h' for usage")
}

serverID := api.GetServerID(serverParts[0])
serverID := apiClient.GetServerID(serverParts[0])

server, err := api.GetServer(serverID)
server, err := apiClient.GetServer(serverID)
if err != nil {
return nil, err
}
Expand All @@ -83,8 +83,14 @@ func TarFromSource(api *api.ScalewayAPI, source string) (*io.ReadCloser, error)
remoteCommand = append(remoteCommand, "-cf", "-")
remoteCommand = append(remoteCommand, base)

// Resolve gateway
gateway, err := api.ResolveGateway(apiClient, cpGateway)
if err != nil {
log.Fatalf("Cannot resolve Gateway '%s': %v", cpGateway, err)
}

// execCmd contains the ssh connection + the remoteCommand
execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, false, nil, remoteCommand, cpGateway))
execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, false, nil, remoteCommand, gateway))
log.Debugf("Executing: ssh %s", strings.Join(execCmd, " "))
spawnSrc := exec.Command("ssh", execCmd...)

Expand Down Expand Up @@ -141,7 +147,7 @@ func TarFromSource(api *api.ScalewayAPI, source string) (*io.ReadCloser, error)
}

// UntarToDest writes to user destination the streamed tarball in input
func UntarToDest(api *api.ScalewayAPI, sourceStream *io.ReadCloser, destination string) error {
func UntarToDest(apiClient *api.ScalewayAPI, sourceStream *io.ReadCloser, destination string) error {
// destination is a server address + path (scp-like uri)
if strings.Index(destination, ":") > -1 {
log.Debugf("Streaming using ssh and untaring remotely")
Expand All @@ -150,9 +156,9 @@ func UntarToDest(api *api.ScalewayAPI, sourceStream *io.ReadCloser, destination
return fmt.Errorf("invalid destination uri, see 'scw cp -h' for usage")
}

serverID := api.GetServerID(serverParts[0])
serverID := apiClient.GetServerID(serverParts[0])

server, err := api.GetServer(serverID)
server, err := apiClient.GetServer(serverID)
if err != nil {
return err
}
Expand All @@ -166,8 +172,14 @@ func UntarToDest(api *api.ScalewayAPI, sourceStream *io.ReadCloser, destination
}
remoteCommand = append(remoteCommand, "-xf", "-")

// Resolve gateway
gateway, err := api.ResolveGateway(apiClient, cpGateway)
if err != nil {
log.Fatalf("Cannot resolve Gateway '%s': %v", cpGateway, err)
}

// execCmd contains the ssh connection + the remoteCommand
execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, false, nil, remoteCommand, cpGateway))
execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, false, nil, remoteCommand, gateway))
log.Debugf("Executing: ssh %s", strings.Join(execCmd, " "))
spawnDst := exec.Command("ssh", execCmd...)

Expand Down
9 changes: 7 additions & 2 deletions commands/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ func runExec(cmd *types.Command, args []string) {

serverID := cmd.API.GetServerID(args[0])

// Resolve gateway
gateway, err := api.ResolveGateway(cmd.API, execGateway)
if err != nil {
log.Fatalf("Cannot resolve Gateway '%s': %v", execGateway, err)
}

var server *api.ScalewayServer
var err error
if execW {
// --wait
server, err = api.WaitForServerReady(cmd.API, serverID)
Expand All @@ -80,7 +85,7 @@ func runExec(cmd *types.Command, args []string) {
}()
}

err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args[1:], !execW, execGateway)
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args[1:], !execW, gateway)
if err != nil {
log.Fatalf("%v", err)
os.Exit(1)
Expand Down
9 changes: 8 additions & 1 deletion commands/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

log "github.com/Sirupsen/logrus"

"github.com/scaleway/scaleway-cli/api"
types "github.com/scaleway/scaleway-cli/commands/types"
"github.com/scaleway/scaleway-cli/utils"
)
Expand Down Expand Up @@ -47,7 +48,13 @@ func runKill(cmd *types.Command, args []string) {
log.Fatalf("Failed to get server information for %s: %v", serverID, err)
}

execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, true, nil, []string{command}, killGateway))
// Resolve gateway
gateway, err := api.ResolveGateway(cmd.API, killGateway)
if err != nil {
log.Fatalf("Cannot resolve Gateway '%s': %v", killGateway, err)
}

execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, true, nil, []string{command}, gateway))

log.Debugf("Executing: ssh %s", strings.Join(execCmd, " "))

Expand Down
9 changes: 8 additions & 1 deletion commands/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package commands
import (
log "github.com/Sirupsen/logrus"

"github.com/scaleway/scaleway-cli/api"
types "github.com/scaleway/scaleway-cli/commands/types"
"github.com/scaleway/scaleway-cli/utils"
)
Expand Down Expand Up @@ -43,8 +44,14 @@ func runLogs(cmd *types.Command, args []string) {

// FIXME: switch to serial history when API is ready

// Resolve gateway
gateway, err := api.ResolveGateway(cmd.API, logsGateway)
if err != nil {
log.Fatalf("Cannot resolve Gateway '%s': %v", logsGateway, err)
}

command := []string{"dmesg"}
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, command, true, logsGateway)
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, command, true, gateway)
if err != nil {
log.Fatalf("Command execution failed: %v", err)
}
Expand Down
9 changes: 8 additions & 1 deletion commands/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package commands
import (
log "github.com/Sirupsen/logrus"

"github.com/scaleway/scaleway-cli/api"
types "github.com/scaleway/scaleway-cli/commands/types"
"github.com/scaleway/scaleway-cli/utils"
)
Expand Down Expand Up @@ -41,8 +42,14 @@ func runPort(cmd *types.Command, args []string) {
log.Fatalf("Failed to get server information for %s: %v", serverID, err)
}

// Resolve gateway
gateway, err := api.ResolveGateway(cmd.API, portGateway)
if err != nil {
log.Fatalf("Cannot resolve Gateway '%s': %v", portGateway, err)
}

command := []string{"netstat -lutn 2>/dev/null | grep LISTEN"}
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, command, true, portGateway)
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, command, true, gateway)
if err != nil {
log.Fatalf("Command execution failed: %v", err)
}
Expand Down
10 changes: 8 additions & 2 deletions commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,18 @@ func runRun(cmd *types.Command, args []string) {
}
log.Debugf("Server is ready: %s", server.PublicAddress.IP)

// Resolve gateway
gateway, err := api.ResolveGateway(cmd.API, runGateway)
if err != nil {
log.Fatalf("Cannot resolve Gateway '%s': %v", runGateway, err)
}

// exec -w SERVER COMMAND ARGS...
log.Debugf("Executing command")
if len(args) < 2 {
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, []string{}, false, runGateway)
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, []string{}, false, gateway)
} else {
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args[1:], false, runGateway)
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args[1:], false, gateway)
}
if err != nil {
log.Debugf("Command execution failed: %v", err)
Expand Down
9 changes: 8 additions & 1 deletion commands/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

log "github.com/Sirupsen/logrus"

"github.com/scaleway/scaleway-cli/api"
types "github.com/scaleway/scaleway-cli/commands/types"
"github.com/scaleway/scaleway-cli/utils"
)
Expand Down Expand Up @@ -46,7 +47,13 @@ func runTop(cmd *types.Command, args []string) {
log.Fatalf("Failed to get server information for %s: %v", serverID, err)
}

execCmd := utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, true, nil, []string{command}, topGateway)
// Resolve gateway
gateway, err := api.ResolveGateway(cmd.API, topGateway)
if err != nil {
log.Fatalf("Cannot resolve Gateway '%s': %v", topGateway, err)
}

execCmd := utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, true, nil, []string{command}, gateway)
log.Debugf("Executing: ssh %s", strings.Join(execCmd, " "))
out, err := exec.Command("ssh", execCmd...).CombinedOutput()
fmt.Printf("%s", out)
Expand Down

0 comments on commit b856b2e

Please sign in to comment.