From 780e77f0a4c8f58283c0bb3821315a5391239b5b Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Thu, 13 Aug 2015 13:49:10 +0200 Subject: [PATCH] Support of 'scw run --rm' option (Fix #117) --- README.md | 6 +++++- pkg/api/helpers.go | 21 +++++++++++++++++++++ pkg/cli/run.go | 8 ++++++++ pkg/commands/run.go | 3 +++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 39a73a19a1..df5c87b3ce 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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)) diff --git a/pkg/api/helpers.go b/pkg/api/helpers.go index 55ed9e5875..3f50f62191 100644 --- a/pkg/api/helpers.go +++ b/pkg/api/helpers.go @@ -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" @@ -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 +} diff --git a/pkg/cli/run.go b/pkg/cli/run.go index 5fb191020d..1f66597db1 100644 --- a/pkg/cli/run.go +++ b/pkg/cli/run.go @@ -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 @@ -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 @@ -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, @@ -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 } diff --git a/pkg/commands/run.go b/pkg/commands/run.go index efe4a4a657..6e5893ba7c 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -24,6 +24,7 @@ type RunArgs struct { Name string Tags []string Volumes []string + AutoRemove bool // DynamicIPRequired // Timeout } @@ -46,6 +47,8 @@ func Run(ctx CommandContext, args RunArgs) error { } logrus.Infof("Server created: %s", serverID) + defer ctx.API.DeleteServerSafe(serverID) + // start SERVER logrus.Info("Server start requested ...") err = api.StartServer(ctx.API, serverID, false)