Skip to content
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
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 @@ -13,6 +13,7 @@ import (
"time"

"github.com/scaleway/scaleway-cli/pkg/utils"
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
"github.com/scaleway/scaleway-cli/vendor/github.com/docker/docker/pkg/namesgenerator"
"github.com/scaleway/scaleway-cli/vendor/github.com/dustin/go-humanize"
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
5 changes: 5 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,10 @@ func Run(ctx CommandContext, args RunArgs) error {
}
logrus.Infof("Server created: %s", serverID)

if args.AutoRemove {
defer ctx.API.DeleteServerSafe(serverID)
}

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