Skip to content

Commit

Permalink
Support of 'scw run --rm' option (Fix scaleway#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Aug 13, 2015
1 parent 3bbe74d commit 780e77f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -589,11 +589,14 @@ Options:
-g, --gateway="" Use a SSH gateway
-h, --help=false Print usage
--name="" Assign a name
--rm=false Automatically remove the server when it exits
-v, --volume="" Attach additional volume (i.e., 50G)

Examples:

$ scw run ubuntu-trusty
$ scw run --rm ubuntu-trusty
$ scw run -a --rm ubuntu-trusty
$ scw run --gateway=myotherserver ubuntu-trusty
$ scw run ubuntu-trusty bash
$ scw run --name=mydocker docker docker run moul/nyancat:armhf
Expand Down Expand Up @@ -1041,7 +1044,8 @@ $ scw inspect myserver | jq '.[0].public_ip.address'

#### Features

* Support of `--gateway=login@host` ([#110](https://github.com/scaleway/scaleway-cli/issues/110))
* Support -f `scw run --rm` option ([#117](https://github.com/scaleway/scaleway-cli/issues/117))
* Support of `--gateway=login@host` ([#110](https://github.com/scaleway/scaleway-cli/issues/110))p
* Upload local ssh key to scaleway account on `scw login` ([#100](https://github.com/scaleway/scaleway-cli/issues/100))
* Add a 'running indicator' for `scw run`, can be disabled with the new flag `--quiet`
* Support of `scw -V/--verbose` option ([#83](https://github.com/scaleway/scaleway-cli/issues/83))
Expand Down
21 changes: 21 additions & 0 deletions pkg/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sync"
"time"

"github.com/Sirupsen/logrus"
"github.com/scaleway/scaleway-cli/pkg/utils"
log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
"github.com/scaleway/scaleway-cli/vendor/github.com/docker/docker/pkg/namesgenerator"
Expand Down Expand Up @@ -509,3 +510,23 @@ func StartServerOnce(api *ScalewayAPI, needle string, wait bool, successChan cha
fmt.Println(needle)
successChan <- true
}

// DeleteServerSafe tries to delete a server using multiple ways
func (a *ScalewayAPI) DeleteServerSafe(serverID string) error {
// FIXME: also delete attached volumes and ip address
err := a.DeleteServer(serverID)
if err == nil {
logrus.Infof("Server '%s' successfuly deleted", serverID)
return nil
}

err = a.PostServerAction(serverID, "terminate")
if err == nil {
logrus.Infof("Server '%s' successfuly terminated", serverID)
return nil
}

logrus.Errorf("Failed to delete server %s", serverID)
logrus.Errorf("Try to run 'scw rm %s' later", serverID)
return err
}
8 changes: 8 additions & 0 deletions pkg/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var cmdRun = &Command{
Help: "Run a command in a new server.",
Examples: `
$ scw run ubuntu-trusty
$ scw run --rm ubuntu-trusty
$ scw run -a --rm ubuntu-trusty
$ scw run --gateway=myotherserver ubuntu-trusty
$ scw run ubuntu-trusty bash
$ scw run --name=mydocker docker docker run moul/nyancat:armhf
Expand All @@ -38,11 +40,13 @@ func init() {
cmdRun.Flag.BoolVar(&runAttachFlag, []string{"a", "-attach"}, false, "Attach to serial console")
cmdRun.Flag.BoolVar(&runDetachFlag, []string{"d", "-detach"}, false, "Run server in background and print server ID")
cmdRun.Flag.StringVar(&runGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
cmdRun.Flag.BoolVar(&runAutoRemove, []string{"-rm"}, false, "Automatically remove the server when it exits")
// FIXME: handle start --timeout
}

// Flags
var runCreateName string // --name flag
var runAutoRemove bool // --rm flag
var runCreateBootscript string // --bootscript flag
var runCreateEnv string // -e, --env flag
var runCreateVolume string // -v, --volume flag
Expand All @@ -67,6 +71,9 @@ func runRun(cmd *Command, rawArgs []string) {
if runDetachFlag && len(rawArgs) > 1 {
log.Fatalf("Conflicting options: -d and COMMAND")
}
if runAutoRemove && runDetachFlag {
log.Fatalf("Conflicting options: --attach and --rm")
}

args := commands.RunArgs{
Attach: runAttachFlag,
Expand All @@ -78,6 +85,7 @@ func runRun(cmd *Command, rawArgs []string) {
Name: runCreateName,
Tags: strings.Split(runCreateEnv, " "),
Volumes: strings.Split(runCreateVolume, " "),
AutoRemove: runAutoRemove,
// FIXME: DynamicIPRequired
// FIXME: Timeout
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type RunArgs struct {
Name string
Tags []string
Volumes []string
AutoRemove bool
// DynamicIPRequired
// Timeout
}
Expand All @@ -46,6 +47,8 @@ func Run(ctx CommandContext, args RunArgs) error {
}
logrus.Infof("Server created: %s", serverID)

defer ctx.API.DeleteServerSafe(serverID)

This comment has been minimized.

Copy link
@aimxhaisse

aimxhaisse Aug 13, 2015

if (AutoRemove == true), then defer?


// start SERVER
logrus.Info("Server start requested ...")
err = api.StartServer(ctx.API, serverID, false)
Expand Down

0 comments on commit 780e77f

Please sign in to comment.