Skip to content

Commit

Permalink
Merge pull request #246 from QuentinPerez/run_userdata
Browse files Browse the repository at this point in the history
Support of `scw run --userdata=...`
  • Loading branch information
moul committed Dec 17, 2015
2 parents 177a7de + 15eb30c commit 695737b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ Options:
--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
-u, --userdata="" Start a server with userdata predefined
-v, --volume="" Attach additional volume (i.e., 50G)

Examples:
Expand All @@ -700,6 +701,7 @@ Examples:
$ scw run --attach alpine
$ scw run --detach alpine
$ scw run --tmp-ssh-key alpine
$ scw run --userdata="FOO=BAR FILE=@/tmp/file" alpine
```


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

### master (unreleased)

* Support of `scw run --userdata=...` ([#202](https://github.com/scaleway/scaleway-cli/issues/202))
* Support of `scw tag --arch=XXX`
* 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))
Expand Down
4 changes: 4 additions & 0 deletions pkg/cli/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var cmdRun = &Command{
$ scw run --attach alpine
$ scw run --detach alpine
$ scw run --tmp-ssh-key alpine
$ scw run --userdata="FOO=BAR FILE=@/tmp/file" alpine
`,
}

Expand All @@ -41,6 +42,7 @@ 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.StringVar(&runUserdatas, []string{"u", "-userdata"}, "", "Start a server with userdata predefined")
cmdRun.Flag.BoolVar(&runAutoRemove, []string{"-rm"}, false, "Automatically remove the server when it exits")
cmdRun.Flag.BoolVar(&runTmpSSHKey, []string{"-tmp-ssh-key"}, false, "Access your server without uploading your SSH key to your account")
cmdRun.Flag.BoolVar(&runShowBoot, []string{"-show-boot"}, false, "Allows to show the boot")
Expand All @@ -58,6 +60,7 @@ var runHelpFlag bool // -h, --help flag
var runAttachFlag bool // -a, --attach flag
var runDetachFlag bool // -d, --detach flag
var runGateway string // -g, --gateway flag
var runUserdatas string // -u, --userdata flag
var runTmpSSHKey bool // --tmp-ssh-key flag
var runShowBoot bool // --show-boot flag
var runTimeout int64 // --timeout flag
Expand Down Expand Up @@ -104,6 +107,7 @@ func runRun(cmd *Command, rawArgs []string) error {
ShowBoot: runShowBoot,
IP: runIPAddress,
Timeout: runTimeout,
Userdata: runUserdatas,
// FIXME: DynamicIPRequired
// FIXME: Timeout
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type RunArgs struct {
TmpSSHKey bool
ShowBoot bool
Timeout int64
Userdata string
// DynamicIPRequired
// Timeout
}
Expand Down Expand Up @@ -68,6 +69,33 @@ func AddSSHKeyToTags(ctx CommandContext, tags *[]string, image string) error {
return nil
}

func addUserData(ctx CommandContext, userdatas []string, serverID string) {
for i := range userdatas {
keyValue := strings.Split(userdatas[i], "=")
if len(keyValue) != 2 {
logrus.Warn("Bad format: ", userdatas[i])
continue
}
var data []byte
var err error

// Set userdata
if keyValue[1][0] == '@' {
data, err = ioutil.ReadFile(keyValue[1][1:])
if err != nil {
logrus.Warn("ReadFile: ", err)
continue
}
} else {
data = []byte(keyValue[1])
}
if err = ctx.API.PatchUserdata(serverID, keyValue[0], data); err != nil {
logrus.Warn("PatchUserdata: ", err)
continue
}
}
}

// Run is the handler for 'scw run'
func Run(ctx CommandContext, args RunArgs) error {
if args.Gateway == "" {
Expand Down Expand Up @@ -111,6 +139,10 @@ func Run(ctx CommandContext, args RunArgs) error {
}
logrus.Info("Server is starting, this may take up to a minute ...")

if args.Userdata != "" {
addUserData(ctx, strings.Split(args.Userdata, " "), serverID)
}

if args.Detach {
fmt.Fprintln(ctx.Stdout, serverID)
return nil
Expand Down

0 comments on commit 695737b

Please sign in to comment.