Skip to content

Add --user {cp,exec,kill,port,run,top} #399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ var (
AccountAPI = "https://account.scaleway.com/"
MetadataAPI = "http://169.254.42.42/"
MarketplaceAPI = "https://api-marketplace.scaleway.com"
URLPublicDNS = ".pub.cloud.scaleway.com"
URLPrivateDNS = ".priv.cloud.scaleway.com"
)

func init() {
Expand Down Expand Up @@ -529,6 +531,10 @@ type ScalewayServer struct {
IPV6 *ScalewayIPV6Definition `json:"ipv6,omitempty"`

EnableIPV6 bool `json:"enable_ipv6,omitempty"`

// This fields are not returned by the API, we generate it
DNSPublic string `json:"dns_public,omitempty"`
DNSPrivate string `json:"dns_private,omitempty"`
}

// ScalewayIPV6Definition represents a Scaleway ipv6
Expand Down Expand Up @@ -1043,8 +1049,10 @@ func (s *ScalewayAPI) GetServers(all bool, limit int) (*[]ScalewayServer, error)
if err = json.Unmarshal(body, &servers); err != nil {
return nil, err
}
for _, server := range servers.Servers {
for i, server := range servers.Servers {
// FIXME region, arch, owner, title
servers.Servers[i].DNSPublic = server.Identifier + URLPublicDNS
servers.Servers[i].DNSPrivate = server.Identifier + URLPrivateDNS
s.Cache.InsertServer(server.Identifier, "fr-1", server.Arch, server.Organization, server.Name)
}
// FIXME: when API limit is ready, remove the following code
Expand Down Expand Up @@ -1092,6 +1100,8 @@ func (s *ScalewayAPI) GetServer(serverID string) (*ScalewayServer, error) {
return nil, err
}
// FIXME region, arch, owner, title
oneServer.Server.DNSPublic = oneServer.Server.Identifier + URLPublicDNS
oneServer.Server.DNSPrivate = oneServer.Server.Identifier + URLPrivateDNS
s.Cache.InsertServer(oneServer.Server.Identifier, "fr-1", oneServer.Server.Arch, oneServer.Server.Organization, oneServer.Server.Name)
return &oneServer.Server, nil
}
Expand Down
35 changes: 29 additions & 6 deletions pkg/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ func WaitForServerState(api *ScalewayAPI, serverID string, targetState string) (
}

// WaitForServerReady wait for a server state to be running, then wait for the SSH port to be available
func WaitForServerReady(api *ScalewayAPI, serverID string, gateway string) (*ScalewayServer, error) {
func WaitForServerReady(api *ScalewayAPI, serverID, gateway string) (*ScalewayServer, error) {
promise := make(chan bool)
var server *ScalewayServer
var err error
Expand Down Expand Up @@ -513,25 +513,48 @@ func WaitForServerReady(api *ScalewayAPI, serverID string, gateway string) (*Sca
}

if gateway == "" {
log.Debugf("Waiting for server SSH port")
dest := fmt.Sprintf("%s:22", server.PublicAddress.IP)
log.Debugf("Waiting for server SSH port %s", dest)
err = utils.WaitForTCPPortOpen(dest)
if err != nil {
promise <- false
return
}
} else {
log.Debugf("Waiting for gateway SSH port")
dest := fmt.Sprintf("%s:22", gateway)
log.Debugf("Waiting for server SSH port %s", dest)
err = utils.WaitForTCPPortOpen(dest)
if err != nil {
promise <- false
return
}
timeout := time.Tick(120 * time.Second)
for {
select {
case <-timeout:
err = fmt.Errorf("Timeout: unable to ping %s", server.PrivateIP)
fmt.Println("timeout")
goto OUT
default:
if utils.SSHExec("", server.PrivateIP, "root", 22, []string{
"nc",
"-z",
"-w",
"1",
server.PrivateIP,
"22",
}, false, gateway) == nil {
goto OUT
}
}
}
OUT:
if err != nil {
promise <- false
return
}
log.Debugf("Check for SSH port through the gateway: %s", server.PrivateIP)

log.Debugf("Waiting 30 more seconds, for SSH to be ready")
time.Sleep(30 * time.Second)
// FIXME: check for SSH port through the gateway
}
promise <- true
}()
Expand Down
6 changes: 6 additions & 0 deletions pkg/cli/cmd_cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ var cmdCp = &Command{
func init() {
cmdCp.Flag.BoolVar(&cpHelp, []string{"h", "-help"}, false, "Print usage")
cmdCp.Flag.StringVar(&cpGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
cmdCp.Flag.StringVar(&cpSSHUser, []string{"u", "-user"}, "root", "Specify SSH user")
cmdCp.Flag.IntVar(&cpSSHPort, []string{"-p", "-port"}, 22, "Specify SSH port")
}

// Flags
var cpHelp bool // -h, --help flag
var cpGateway string // -g, --gateway flag
var cpSSHUser string // -u, --user flag
var cpSSHPort int // -p, --port flag

func runCp(cmd *Command, rawArgs []string) error {
if cpHelp {
Expand All @@ -49,6 +53,8 @@ func runCp(cmd *Command, rawArgs []string) error {
Gateway: cpGateway,
Source: rawArgs[0],
Destination: rawArgs[1],
SSHUser: cpSSHUser,
SSHPort: cpSSHPort,
}
ctx := cmd.GetContext(rawArgs)
return commands.RunCp(ctx, args)
Expand Down
6 changes: 6 additions & 0 deletions pkg/cli/cmd_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ func init() {
cmdExec.Flag.Float64Var(&execTimeout, []string{"T", "-timeout"}, 0, "Set timeout values to seconds")
cmdExec.Flag.BoolVar(&execW, []string{"w", "-wait"}, false, "Wait for SSH to be ready")
cmdExec.Flag.StringVar(&execGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
cmdExec.Flag.StringVar(&execSSHUser, []string{"u", "-user"}, "root", "Specify SSH user")
cmdExec.Flag.IntVar(&execSSHPort, []string{"-p", "-port"}, 22, "Specify SSH port")
}

// Flags
var execW bool // -w, --wait flag
var execTimeout float64 // -T flag
var execHelp bool // -h, --help flag
var execGateway string // -g, --gateway flag
var execSSHUser string // -u, --user flag
var execSSHPort int // -p, --port flag

func runExec(cmd *Command, rawArgs []string) error {
if execHelp {
Expand All @@ -52,6 +56,8 @@ func runExec(cmd *Command, rawArgs []string) error {
Gateway: execGateway,
Server: rawArgs[0],
Command: rawArgs[1:],
SSHUser: execSSHUser,
SSHPort: execSSHPort,
}
ctx := cmd.GetContext(rawArgs)
return commands.RunExec(ctx, args)
Expand Down
6 changes: 6 additions & 0 deletions pkg/cli/cmd_kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ var cmdKill = &Command{
func init() {
cmdKill.Flag.BoolVar(&killHelp, []string{"h", "-help"}, false, "Print usage")
cmdKill.Flag.StringVar(&killGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
cmdKill.Flag.StringVar(&killSSHUser, []string{"u", "-user"}, "root", "Specify SSH user")
cmdKill.Flag.IntVar(&killSSHPort, []string{"-p", "-port"}, 22, "Specify SSH port")
// FIXME: add --signal option
}

// Flags
var killHelp bool // -h, --help flag
var killGateway string // -g, --gateway flag
var killSSHUser string // -u, --user flag
var killSSHPort int // -p, --port flag

func runKill(cmd *Command, rawArgs []string) error {
if killHelp {
Expand All @@ -34,6 +38,8 @@ func runKill(cmd *Command, rawArgs []string) error {
args := commands.KillArgs{
Gateway: killGateway,
Server: rawArgs[0],
SSHUser: killSSHUser,
SSHPort: killSSHPort,
}
ctx := cmd.GetContext(rawArgs)
return commands.RunKill(ctx, args)
Expand Down
6 changes: 6 additions & 0 deletions pkg/cli/cmd_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ var cmdLogs = &Command{
func init() {
cmdLogs.Flag.BoolVar(&logsHelp, []string{"h", "-help"}, false, "Print usage")
cmdLogs.Flag.StringVar(&logsGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
cmdLogs.Flag.StringVar(&logsSSHUser, []string{"u", "-user"}, "root", "Specify SSH root")
cmdLogs.Flag.IntVar(&logsSSHPort, []string{"-p", "-port"}, 22, "Specify SSH port")
}

// FLags
var logsHelp bool // -h, --help flag
var logsGateway string // -g, --gateway flag
var logsSSHUser string // -u, --user flag
var logsSSHPort int // -p, --port flag

func runLogs(cmd *Command, rawArgs []string) error {
if logsHelp {
Expand All @@ -33,6 +37,8 @@ func runLogs(cmd *Command, rawArgs []string) error {
args := commands.LogsArgs{
Gateway: logsGateway,
Server: rawArgs[0],
SSHUser: logsSSHUser,
SSHPort: logsSSHPort,
}
ctx := cmd.GetContext(rawArgs)
return commands.RunLogs(ctx, args)
Expand Down
6 changes: 6 additions & 0 deletions pkg/cli/cmd_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ var cmdPort = &Command{
func init() {
cmdPort.Flag.BoolVar(&portHelp, []string{"h", "-help"}, false, "Print usage")
cmdPort.Flag.StringVar(&portGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
cmdPort.Flag.StringVar(&portSSHUser, []string{"u", "-user"}, "root", "Specify SSH user")
cmdPort.Flag.IntVar(&portSSHPort, []string{"-p", "-port"}, 22, "Specify SSH port")
}

// FLags
var portHelp bool // -h, --help flag
var portGateway string // -g, --gateway flag
var portSSHUser string // -u, --user flag
var portSSHPort int // -p, --port flag

func runPort(cmd *Command, rawArgs []string) error {
if portHelp {
Expand All @@ -33,6 +37,8 @@ func runPort(cmd *Command, rawArgs []string) error {
args := commands.PortArgs{
Gateway: portGateway,
Server: rawArgs[0],
SSHUser: portSSHUser,
SSHPort: portSSHPort,
}
ctx := cmd.GetContext(rawArgs)
return commands.RunPort(ctx, args)
Expand Down
6 changes: 6 additions & 0 deletions pkg/cli/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ func init() {
cmdRun.Flag.StringVar(&runGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
cmdRun.Flag.StringVar(&runUserdatas, []string{"u", "-userdata"}, "", "Start a server with userdata predefined")
cmdRun.Flag.StringVar(&runCommercialType, []string{"-commercial-type"}, "VC1S", "Start a server with specific commercial-type C1, VC1S, C2[SML]")
cmdRun.Flag.StringVar(&runSSHUser, []string{"-u", "-user"}, "root", "Specify SSH User")
cmdRun.Flag.BoolVar(&runAutoRemove, []string{"-rm"}, false, "Automatically remove the server when it exits")
cmdRun.Flag.BoolVar(&runIPV6, []string{"-ipv6"}, false, "Enable IPV6")
cmdRun.Flag.BoolVar(&runTmpSSHKey, []string{"-tmp-ssh-key"}, false, "Access your server without uploading your SSH key to your account")
cmdRun.Flag.BoolVar(&runShowBoot, []string{"-show-boot"}, false, "Allows to show the boot")
cmdRun.Flag.IntVar(&runSSHPort, []string{"-p", "-port"}, 22, "Specify SSH port")
// FIXME: handle start --timeout
}

Expand All @@ -71,6 +73,8 @@ var runShowBoot bool // --show-boot flag
var runIPV6 bool // --ipv6 flag
var runTimeout int64 // --timeout flag
var runSetState string // --set-state flag
var runSSHUser string // -u, --user flag
var runSSHPort int // -p, --port flag

func runRun(cmd *Command, rawArgs []string) error {
if runHelpFlag {
Expand Down Expand Up @@ -118,6 +122,8 @@ func runRun(cmd *Command, rawArgs []string) error {
CommercialType: runCommercialType,
State: runSetState,
IPV6: runIPV6,
SSHUser: runSSHUser,
SSHPort: runSSHPort,
// FIXME: Timeout
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/cli/cmd_top.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ var cmdTop = &Command{
func init() {
cmdTop.Flag.BoolVar(&topHelp, []string{"h", "-help"}, false, "Print usage")
cmdTop.Flag.StringVar(&topGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
cmdTop.Flag.StringVar(&topSSHUser, []string{"u", "-user"}, "root", "Specify SSH user")
cmdTop.Flag.IntVar(&topSSHPort, []string{"-p", "-port"}, 22, "Specify SSH port")
}

// Flags
var topHelp bool // -h, --help flag
var topGateway string // -g, --gateway flag
var topSSHUser string // -u, --user flag
var topSSHPort int // -p, --port flag

func runTop(cmd *Command, rawArgs []string) error {
if topHelp {
Expand All @@ -33,6 +37,8 @@ func runTop(cmd *Command, rawArgs []string) error {
args := commands.TopArgs{
Gateway: topGateway,
Server: rawArgs[0],
SSHUser: topSSHUser,
SSHPort: topSSHPort,
}
ctx := cmd.GetContext(rawArgs)
return commands.RunTop(ctx, args)
Expand Down
14 changes: 8 additions & 6 deletions pkg/commands/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type CpArgs struct {
Gateway string
Source string
Destination string
SSHUser string
SSHPort int
}

// RunCp is the handler for 'scw cp'
Expand All @@ -31,20 +33,20 @@ func RunCp(ctx CommandContext, args CpArgs) error {
return fmt.Errorf("bad usage, see 'scw help cp'")
}

sourceStream, err := TarFromSource(ctx, args.Source, args.Gateway)
sourceStream, err := TarFromSource(ctx, args.Source, args.Gateway, args.SSHUser, args.SSHPort)
if err != nil {
return fmt.Errorf("cannot tar from source '%s': %v", args.Source, err)
}

err = UntarToDest(ctx, sourceStream, args.Destination, args.Gateway)
err = UntarToDest(ctx, sourceStream, args.Destination, args.Gateway, args.SSHUser, args.SSHPort)
if err != nil {
return fmt.Errorf("cannot untar to destination '%s': %v", args.Destination, err)
}
return nil
}

// TarFromSource creates a stream buffer with the tarballed content of the user source
func TarFromSource(ctx CommandContext, source string, gateway string) (*io.ReadCloser, error) {
func TarFromSource(ctx CommandContext, source, gateway, user string, port int) (*io.ReadCloser, error) {
var tarOutputStream io.ReadCloser

// source is a server address + path (scp-like uri)
Expand Down Expand Up @@ -93,7 +95,7 @@ func TarFromSource(ctx CommandContext, source string, gateway string) (*io.ReadC
}

// execCmd contains the ssh connection + the remoteCommand
sshCommand := utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, false, remoteCommand, gateway)
sshCommand := utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, user, port, false, remoteCommand, gateway)
logrus.Debugf("Executing: %s", sshCommand)
spawnSrc := exec.Command("ssh", sshCommand.Slice()[1:]...)

Expand Down Expand Up @@ -151,7 +153,7 @@ func TarFromSource(ctx CommandContext, source string, gateway string) (*io.ReadC
}

// UntarToDest writes to user destination the streamed tarball in input
func UntarToDest(ctx CommandContext, sourceStream *io.ReadCloser, destination string, gateway string) error {
func UntarToDest(ctx CommandContext, sourceStream *io.ReadCloser, destination, gateway, user string, port int) error {
// destination is a server address + path (scp-like uri)
if strings.Contains(destination, ":") {
logrus.Debugf("Streaming using ssh and untaring remotely")
Expand Down Expand Up @@ -193,7 +195,7 @@ func UntarToDest(ctx CommandContext, sourceStream *io.ReadCloser, destination st
}

// execCmd contains the ssh connection + the remoteCommand
sshCommand := utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, false, remoteCommand, gateway)
sshCommand := utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, user, port, false, remoteCommand, gateway)
logrus.Debugf("Executing: %s", sshCommand)
spawnDst := exec.Command("ssh", sshCommand.Slice()[1:]...)

Expand Down
5 changes: 4 additions & 1 deletion pkg/commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"strings"

"github.com/Sirupsen/logrus"
"github.com/scaleway/scaleway-cli/pkg/api"
)

Expand Down Expand Up @@ -56,7 +57,9 @@ func RunCreate(ctx CommandContext, args CreateArgs) error {
if err != nil {
return err
}

logrus.Debugf("Server created: %s", serverID)
logrus.Debugf("PublicDNS %s", serverID+api.URLPublicDNS)
logrus.Debugf("PrivateDNS %s", serverID+api.URLPrivateDNS)
fmt.Fprintln(ctx.Stdout, serverID)
return nil
}
Loading