Skip to content

Commit

Permalink
Refactored 'kill' command (scaleway#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Aug 6, 2015
1 parent 3ca53cc commit a6a29f3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 39 deletions.
49 changes: 10 additions & 39 deletions pkg/cli/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@
package cli

import (
"os"
"os/exec"
"strings"
"github.com/Sirupsen/logrus"

log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"

"github.com/scaleway/scaleway-cli/pkg/api"
"github.com/scaleway/scaleway-cli/pkg/utils"
"github.com/scaleway/scaleway-cli/pkg/commands"
)

var cmdKill = &Command{
Expand All @@ -32,45 +27,21 @@ func init() {
var killHelp bool // -h, --help flag
var killGateway string // -g, --gateway flag

func runKill(cmd *Command, args []string) {
func runKill(cmd *Command, rawArgs []string) {
if killHelp {
cmd.PrintUsage()
}
if len(args) < 1 {
if len(rawArgs) < 1 {
cmd.PrintShortUsage()
}

serverID := cmd.API.GetServerID(args[0])
command := "halt"
server, err := cmd.API.GetServer(serverID)
if err != nil {
log.Fatalf("Failed to get server information for %s: %v", serverID, err)
}

// Resolve gateway
if killGateway == "" {
killGateway = os.Getenv("SCW_GATEWAY")
args := commands.KillArgs{
Gateway: killGateway,
Server: rawArgs[0],
}
var gateway string
if killGateway == serverID || killGateway == args[0] {
gateway = ""
} else {
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, " "))

spawn := exec.Command("ssh", execCmd...)
spawn.Stdout = os.Stdout
spawn.Stdin = os.Stdin
spawn.Stderr = os.Stderr
err = spawn.Run()
ctx := cmd.GetContext(rawArgs)
err := commands.RunKill(ctx, args)
if err != nil {
log.Fatal(err)
logrus.Fatalf("Cannot execute 'kill': %v", err)
}
}
57 changes: 57 additions & 0 deletions pkg/commands/kill.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (C) 2015 Scaleway. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE.md file.

package commands

import (
"fmt"
"os"
"os/exec"
"strings"

"github.com/docker/machine/log"
"github.com/scaleway/scaleway-cli/pkg/api"
"github.com/scaleway/scaleway-cli/pkg/utils"
)

// KillArgs are flags for the `RunKill` function
type KillArgs struct {
Gateway string
Server string
}

// RunKill is the handler for 'scw kill'
func RunKill(ctx CommandContext, args KillArgs) error {
serverID := ctx.API.GetServerID(args.Server)
command := "halt"
server, err := ctx.API.GetServer(serverID)
if err != nil {
return fmt.Errorf("failed to get server information for %s: %v", serverID, err)
}

// Resolve gateway
if args.Gateway == "" {
args.Gateway = ctx.Getenv("SCW_GATEWAY")
}
var gateway string
if args.Gateway == serverID || args.Gateway == args.Server {
gateway = ""
} else {
gateway, err = api.ResolveGateway(ctx.API, args.Gateway)
if err != nil {
return fmt.Errorf("cannot resolve Gateway '%s': %v", args.Gateway, err)
}
}

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

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

spawn := exec.Command("ssh", execCmd...)
spawn.Stdout = os.Stdout
spawn.Stdin = os.Stdin
spawn.Stderr = os.Stderr

return spawn.Run()
}

0 comments on commit a6a29f3

Please sign in to comment.