Skip to content

Commit 2b81a5a

Browse files
committed
Merge pull request #121 from moul/fix-117
Support of 'scw run --rm' option (Fix #117)
2 parents 3bbe74d + 81053e8 commit 2b81a5a

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,11 +589,14 @@ Options:
589589
-g, --gateway="" Use a SSH gateway
590590
-h, --help=false Print usage
591591
--name="" Assign a name
592+
--rm=false Automatically remove the server when it exits
592593
-v, --volume="" Attach additional volume (i.e., 50G)
593594

594595
Examples:
595596

596597
$ scw run ubuntu-trusty
598+
$ scw run --rm ubuntu-trusty
599+
$ scw run -a --rm ubuntu-trusty
597600
$ scw run --gateway=myotherserver ubuntu-trusty
598601
$ scw run ubuntu-trusty bash
599602
$ scw run --name=mydocker docker docker run moul/nyancat:armhf
@@ -1041,7 +1044,8 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
10411044

10421045
#### Features
10431046

1044-
* Support of `--gateway=login@host` ([#110](https://github.com/scaleway/scaleway-cli/issues/110))
1047+
* Support -f `scw run --rm` option ([#117](https://github.com/scaleway/scaleway-cli/issues/117))
1048+
* Support of `--gateway=login@host` ([#110](https://github.com/scaleway/scaleway-cli/issues/110))p
10451049
* Upload local ssh key to scaleway account on `scw login` ([#100](https://github.com/scaleway/scaleway-cli/issues/100))
10461050
* Add a 'running indicator' for `scw run`, can be disabled with the new flag `--quiet`
10471051
* Support of `scw -V/--verbose` option ([#83](https://github.com/scaleway/scaleway-cli/issues/83))

pkg/api/helpers.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"github.com/scaleway/scaleway-cli/pkg/utils"
16+
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
1617
log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
1718
"github.com/scaleway/scaleway-cli/vendor/github.com/docker/docker/pkg/namesgenerator"
1819
"github.com/scaleway/scaleway-cli/vendor/github.com/dustin/go-humanize"
@@ -509,3 +510,23 @@ func StartServerOnce(api *ScalewayAPI, needle string, wait bool, successChan cha
509510
fmt.Println(needle)
510511
successChan <- true
511512
}
513+
514+
// DeleteServerSafe tries to delete a server using multiple ways
515+
func (a *ScalewayAPI) DeleteServerSafe(serverID string) error {
516+
// FIXME: also delete attached volumes and ip address
517+
err := a.DeleteServer(serverID)
518+
if err == nil {
519+
logrus.Infof("Server '%s' successfuly deleted", serverID)
520+
return nil
521+
}
522+
523+
err = a.PostServerAction(serverID, "terminate")
524+
if err == nil {
525+
logrus.Infof("Server '%s' successfuly terminated", serverID)
526+
return nil
527+
}
528+
529+
logrus.Errorf("Failed to delete server %s", serverID)
530+
logrus.Errorf("Try to run 'scw rm %s' later", serverID)
531+
return err
532+
}

pkg/cli/run.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ var cmdRun = &Command{
2020
Help: "Run a command in a new server.",
2121
Examples: `
2222
$ scw run ubuntu-trusty
23+
$ scw run --rm ubuntu-trusty
24+
$ scw run -a --rm ubuntu-trusty
2325
$ scw run --gateway=myotherserver ubuntu-trusty
2426
$ scw run ubuntu-trusty bash
2527
$ scw run --name=mydocker docker docker run moul/nyancat:armhf
@@ -38,11 +40,13 @@ func init() {
3840
cmdRun.Flag.BoolVar(&runAttachFlag, []string{"a", "-attach"}, false, "Attach to serial console")
3941
cmdRun.Flag.BoolVar(&runDetachFlag, []string{"d", "-detach"}, false, "Run server in background and print server ID")
4042
cmdRun.Flag.StringVar(&runGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
43+
cmdRun.Flag.BoolVar(&runAutoRemove, []string{"-rm"}, false, "Automatically remove the server when it exits")
4144
// FIXME: handle start --timeout
4245
}
4346

4447
// Flags
4548
var runCreateName string // --name flag
49+
var runAutoRemove bool // --rm flag
4650
var runCreateBootscript string // --bootscript flag
4751
var runCreateEnv string // -e, --env flag
4852
var runCreateVolume string // -v, --volume flag
@@ -67,6 +71,9 @@ func runRun(cmd *Command, rawArgs []string) {
6771
if runDetachFlag && len(rawArgs) > 1 {
6872
log.Fatalf("Conflicting options: -d and COMMAND")
6973
}
74+
if runAutoRemove && runDetachFlag {
75+
log.Fatalf("Conflicting options: --attach and --rm")
76+
}
7077

7178
args := commands.RunArgs{
7279
Attach: runAttachFlag,
@@ -78,6 +85,7 @@ func runRun(cmd *Command, rawArgs []string) {
7885
Name: runCreateName,
7986
Tags: strings.Split(runCreateEnv, " "),
8087
Volumes: strings.Split(runCreateVolume, " "),
88+
AutoRemove: runAutoRemove,
8189
// FIXME: DynamicIPRequired
8290
// FIXME: Timeout
8391
}

pkg/commands/run.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type RunArgs struct {
2424
Name string
2525
Tags []string
2626
Volumes []string
27+
AutoRemove bool
2728
// DynamicIPRequired
2829
// Timeout
2930
}
@@ -46,6 +47,10 @@ func Run(ctx CommandContext, args RunArgs) error {
4647
}
4748
logrus.Infof("Server created: %s", serverID)
4849

50+
if args.AutoRemove {
51+
defer ctx.API.DeleteServerSafe(serverID)
52+
}
53+
4954
// start SERVER
5055
logrus.Info("Server start requested ...")
5156
err = api.StartServer(ctx.API, serverID, false)

0 commit comments

Comments
 (0)