Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix239 #241

Merged
merged 3 commits into from
Nov 27, 2015
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -677,13 +677,14 @@ Options:
-a, --attach=false Attach to serial console
--bootscript="" Assign a bootscript
-d, --detach=false Run server in background and print server ID
-e, --env="" Provide metadata tags passed to initrd (i.e., boot=resue INITRD_DEBUG=1)
-e, --env="" Provide metadata tags passed to initrd (i.e., boot=rescue INITRD_DEBUG=1)
-g, --gateway="" Use a SSH gateway
-h, --help=false Print usage
--ip-address="" Assign an IP
--name="" Assign a name
--rm=false Automatically remove the server when it exits
--show-boot=false Allows to show the boot
-T, --timeout=0 Set timeout value to seconds
--tmp-ssh-key=false Access your server without uploading your SSH key to your account
-v, --volume="" Attach additional volume (i.e., 50G)

Expand Down Expand Up @@ -1142,6 +1143,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'

### master (unreleased)

* Support of `scw run --timeout=X` ([#239](https://github.com/scaleway/scaleway-cli/issues/239))
* Check the "stopped" state for `scw run | exec -w`([#229](https://github.com/scaleway/scaleway-cli/issues/229))

View full [commits list](https://github.com/scaleway/scaleway-cli/compare/v1.6.0...master)
Expand Down
5 changes: 4 additions & 1 deletion pkg/cli/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ var cmdRun = &Command{
func init() {
cmdRun.Flag.StringVar(&runCreateName, []string{"-name"}, "", "Assign a name")
cmdRun.Flag.StringVar(&runCreateBootscript, []string{"-bootscript"}, "", "Assign a bootscript")
cmdRun.Flag.StringVar(&runCreateEnv, []string{"e", "-env"}, "", "Provide metadata tags passed to initrd (i.e., boot=resue INITRD_DEBUG=1)")
cmdRun.Flag.StringVar(&runCreateEnv, []string{"e", "-env"}, "", "Provide metadata tags passed to initrd (i.e., boot=rescue INITRD_DEBUG=1)")
cmdRun.Flag.StringVar(&runCreateVolume, []string{"v", "-volume"}, "", "Attach additional volume (i.e., 50G)")
cmdRun.Flag.BoolVar(&runHelpFlag, []string{"h", "-help"}, false, "Print usage")
cmdRun.Flag.Int64Var(&runTimeout, []string{"T", "-timeout"}, 0, "Set timeout value to seconds")
cmdRun.Flag.StringVar(&runIPAddress, []string{"-ip-address"}, "", "Assign an IP")
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")
Expand All @@ -59,6 +60,7 @@ var runDetachFlag bool // -d, --detach flag
var runGateway string // -g, --gateway flag
var runTmpSSHKey bool // --tmp-ssh-key flag
var runShowBoot bool // --show-boot flag
var runTimeout int64 // --timeout flag

func runRun(cmd *Command, rawArgs []string) error {
if runHelpFlag {
Expand Down Expand Up @@ -101,6 +103,7 @@ func runRun(cmd *Command, rawArgs []string) error {
TmpSSHKey: runTmpSSHKey,
ShowBoot: runShowBoot,
IP: runIPAddress,
Timeout: runTimeout,
// FIXME: DynamicIPRequired
// FIXME: Timeout
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/scaleway/scaleway-cli/pkg/api"
"github.com/scaleway/scaleway-cli/pkg/config"
Expand All @@ -32,6 +33,7 @@ type RunArgs struct {
AutoRemove bool
TmpSSHKey bool
ShowBoot bool
Timeout int64
// DynamicIPRequired
// Timeout
}
Expand Down Expand Up @@ -116,6 +118,19 @@ func Run(ctx CommandContext, args RunArgs) error {
// Sync cache on disk
ctx.API.Sync()

closeTimeout := make(chan struct{})

if args.Timeout > 0 {
go func() {
select {
case <-time.After(time.Duration(args.Timeout) * time.Second):
// FIXME: avoid use of fatalf
logrus.Fatalf("Operation timed out")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use logrus.Fatalf here

case <-closeTimeout:
break
}
}()
}
if args.ShowBoot {
// Attach to server serial
logrus.Info("Attaching to server console ...")
Expand All @@ -129,6 +144,7 @@ func Run(ctx CommandContext, args RunArgs) error {
return err
}
sshConnection := <-notif
close(closeTimeout)
gottycli.ExitLoop()
<-done
utils.Quiet(false)
Expand All @@ -147,6 +163,7 @@ func Run(ctx CommandContext, args RunArgs) error {
if err != nil {
return fmt.Errorf("cannot attach to server serial: %v", err)
}
close(closeTimeout)
<-done
gottycli.Close()
} else {
Expand All @@ -155,6 +172,7 @@ func Run(ctx CommandContext, args RunArgs) error {
return err
}
sshConnection := <-notif
close(closeTimeout)
if sshConnection.err != nil {
return sshConnection.err
}
Expand Down