Skip to content

Commit aafff09

Browse files
committed
fix #99
1 parent e92c333 commit aafff09

File tree

6 files changed

+106
-6
lines changed

6 files changed

+106
-6
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ Options:
310310
-e, --env="" Provide metadata tags passed to initrd (i.e., boot=resue INITRD_DEBUG=1)
311311
-h, --help=false Print usage
312312
--name="" Assign a name
313+
--tmp-ssh-key=false Access your server without uploading your SSH key to your account
313314
-v, --volume="" Attach additional volume (i.e., 50G)
314315

315316
Examples:
@@ -319,6 +320,7 @@ Examples:
319320
$ scw create --bootscript=3.2.34 --env="boot=live rescue_image=http://j.mp/scaleway-ubuntu-trusty-tarball" 50GB
320321
$ scw inspect $(scw create 1GB --bootscript=rescue --volume=50GB)
321322
$ scw create $(scw tag my-snapshot my-image)
323+
$ scw create --tmp-ssh-key 10GB
322324
```
323325

324326

@@ -630,6 +632,7 @@ Options:
630632
-h, --help=false Print usage
631633
--name="" Assign a name
632634
--rm=false Automatically remove the server when it exits
635+
--tmp-ssh-key=false Access your server without uploading your SSH key to your account
633636
-v, --volume="" Attach additional volume (i.e., 50G)
634637

635638
Examples:
@@ -643,6 +646,7 @@ Examples:
643646
$ scw run --bootscript=3.2.34 --env="boot=live rescue_image=http://j.mp/scaleway-ubuntu-trusty-tarball" 50GB bash
644647
$ scw run --attach alpine
645648
$ scw run --detach alpine
649+
$ scw run --tmp-ssh-key alpine
646650
```
647651

648652

@@ -1084,8 +1088,9 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
10841088

10851089
#### Features
10861090

1091+
* Support --tmp-ssh-key `scw {run,create}` option ([#99](https://github.com/scaleway/scaleway-cli/issues/99))
10871092
* Support -f `scw run --rm` option ([#117](https://github.com/scaleway/scaleway-cli/issues/117))
1088-
* Support of `--gateway=login@host` ([#110](https://github.com/scaleway/scaleway-cli/issues/110))p
1093+
* Support of `--gateway=login@host` ([#110](https://github.com/scaleway/scaleway-cli/issues/110))
10891094
* Upload local ssh key to scaleway account on `scw login` ([#100](https://github.com/scaleway/scaleway-cli/issues/100))
10901095
* Add a 'running indicator' for `scw run`, can be disabled with the new flag `--quiet`
10911096
* Support of `scw -V/--verbose` option ([#83](https://github.com/scaleway/scaleway-cli/issues/83))

pkg/cli/create.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var cmdCreate = &Command{
2222
$ scw create --bootscript=3.2.34 --env="boot=live rescue_image=http://j.mp/scaleway-ubuntu-trusty-tarball" 50GB
2323
$ scw inspect $(scw create 1GB --bootscript=rescue --volume=50GB)
2424
$ scw create $(scw tag my-snapshot my-image)
25+
$ scw create --tmp-ssh-key 10GB
2526
`,
2627
}
2728

@@ -31,6 +32,7 @@ func init() {
3132
cmdCreate.Flag.StringVar(&createEnv, []string{"e", "-env"}, "", "Provide metadata tags passed to initrd (i.e., boot=resue INITRD_DEBUG=1)")
3233
cmdCreate.Flag.StringVar(&createVolume, []string{"v", "-volume"}, "", "Attach additional volume (i.e., 50G)")
3334
cmdCreate.Flag.BoolVar(&createHelp, []string{"h", "-help"}, false, "Print usage")
35+
cmdCreate.Flag.BoolVar(&createTmpSSHKey, []string{"-tmp-ssh-key"}, false, "Access your server without uploading your SSH key to your account")
3436
}
3537

3638
// Flags
@@ -39,6 +41,7 @@ var createBootscript string // --bootscript flag
3941
var createEnv string // -e, --env flag
4042
var createVolume string // -v, --volume flag
4143
var createHelp bool // -h, --help flag
44+
var createTmpSSHKey bool // --tmp-ssh-key flag
4245

4346
func runCreate(cmd *Command, rawArgs []string) {
4447
if createHelp {
@@ -51,9 +54,15 @@ func runCreate(cmd *Command, rawArgs []string) {
5154
args := commands.CreateArgs{
5255
Name: createName,
5356
Bootscript: createBootscript,
54-
Tags: strings.Split(createEnv, " "),
55-
Volumes: strings.Split(createVolume, " "),
5657
Image: rawArgs[0],
58+
TmpSSHKey: createTmpSSHKey,
59+
}
60+
61+
if len(runCreateEnv) > 0 {
62+
args.Tags = strings.Split(runCreateEnv, " ")
63+
}
64+
if len(runCreateVolume) > 0 {
65+
args.Volumes = strings.Split(runCreateVolume, " ")
5766
}
5867
ctx := cmd.GetContext(rawArgs)
5968
err := commands.RunCreate(ctx, args)

pkg/cli/run.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var cmdRun = &Command{
2828
$ scw run --bootscript=3.2.34 --env="boot=live rescue_image=http://j.mp/scaleway-ubuntu-trusty-tarball" 50GB bash
2929
$ scw run --attach alpine
3030
$ scw run --detach alpine
31+
$ scw run --tmp-ssh-key alpine
3132
`,
3233
}
3334

@@ -41,6 +42,7 @@ func init() {
4142
cmdRun.Flag.BoolVar(&runDetachFlag, []string{"d", "-detach"}, false, "Run server in background and print server ID")
4243
cmdRun.Flag.StringVar(&runGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
4344
cmdRun.Flag.BoolVar(&runAutoRemove, []string{"-rm"}, false, "Automatically remove the server when it exits")
45+
cmdRun.Flag.BoolVar(&runTmpSSHKey, []string{"-tmp-ssh-key"}, false, "Access your server without uploading your SSH key to your account")
4446
// FIXME: handle start --timeout
4547
}
4648

@@ -54,6 +56,7 @@ var runHelpFlag bool // -h, --help flag
5456
var runAttachFlag bool // -a, --attach flag
5557
var runDetachFlag bool // -d, --detach flag
5658
var runGateway string // -g, --gateway flag
59+
var runTmpSSHKey bool // --tmp-ssh-key flag
5760

5861
func runRun(cmd *Command, rawArgs []string) {
5962
if runHelpFlag {
@@ -83,12 +86,18 @@ func runRun(cmd *Command, rawArgs []string) {
8386
Gateway: runGateway,
8487
Image: rawArgs[0],
8588
Name: runCreateName,
86-
Tags: strings.Split(runCreateEnv, " "),
87-
Volumes: strings.Split(runCreateVolume, " "),
8889
AutoRemove: runAutoRemove,
90+
TmpSSHKey: runTmpSSHKey,
8991
// FIXME: DynamicIPRequired
9092
// FIXME: Timeout
9193
}
94+
95+
if len(runCreateEnv) > 0 {
96+
args.Tags = strings.Split(runCreateEnv, " ")
97+
}
98+
if len(runCreateVolume) > 0 {
99+
args.Volumes = strings.Split(runCreateVolume, " ")
100+
}
92101
ctx := cmd.GetContext(rawArgs)
93102
err := commands.Run(ctx, args)
94103
if err != nil {

pkg/commands/create.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,18 @@ type CreateArgs struct {
1818
Tags []string
1919
Volumes []string
2020
Image string
21+
TmpSSHKey bool
2122
}
2223

2324
// RunCreate is the handler for 'scw create'
2425
func RunCreate(ctx CommandContext, args CreateArgs) error {
26+
if args.TmpSSHKey {
27+
err := AddSSHKeyToTags(ctx, &args.Tags, args.Image)
28+
if err != nil {
29+
return err
30+
}
31+
}
32+
2533
env := strings.Join(args.Tags, " ")
2634
volume := strings.Join(args.Volumes, " ")
2735
serverID, err := api.CreateServer(ctx.API, args.Image, args.Name, args.Bootscript, env, volume, true)
@@ -30,6 +38,5 @@ func RunCreate(ctx CommandContext, args CreateArgs) error {
3038
}
3139

3240
fmt.Fprintln(ctx.Stdout, serverID)
33-
3441
return nil
3542
}

pkg/commands/run.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ package commands
66

77
import (
88
"fmt"
9+
"io/ioutil"
10+
"os"
11+
"path/filepath"
912
"strings"
1013

1114
"github.com/scaleway/scaleway-cli/pkg/api"
@@ -25,16 +28,53 @@ type RunArgs struct {
2528
Tags []string
2629
Volumes []string
2730
AutoRemove bool
31+
TmpSSHKey bool
2832
// DynamicIPRequired
2933
// Timeout
3034
}
3135

36+
// AddSSHKeyToTags adds the ssh key in the tags
37+
func AddSSHKeyToTags(ctx CommandContext, tags *[]string, image string) error {
38+
home, err := utils.GetHomeDir()
39+
if err != nil {
40+
return fmt.Errorf("unable to find your home %v", err)
41+
}
42+
idRsa := filepath.Join(home, ".ssh", "id_rsa")
43+
if _, err := os.Stat(idRsa); err != nil {
44+
if os.IsNotExist(err) {
45+
logrus.Warnln("Unable to find your ~/.ssh/id_rsa")
46+
logrus.Warnln("Run 'ssh-keygen -t rsa'")
47+
return nil
48+
}
49+
}
50+
idRsa = strings.Join([]string{idRsa, ".pub"}, "")
51+
data, err := ioutil.ReadFile(idRsa)
52+
if err != nil {
53+
return fmt.Errorf("failed to read %v", err)
54+
}
55+
data[7] = '_'
56+
for i := range data {
57+
if data[i] == ' ' {
58+
data = data[:i]
59+
break
60+
}
61+
}
62+
*tags = append(*tags, strings.Join([]string{"AUTHORIZED_KEY", string(data[:len(data)])}, "="))
63+
return nil
64+
}
65+
3266
// Run is the handler for 'scw run'
3367
func Run(ctx CommandContext, args RunArgs) error {
3468
if args.Gateway == "" {
3569
args.Gateway = ctx.Getenv("SCW_GATEWAY")
3670
}
3771

72+
if args.TmpSSHKey {
73+
err := AddSSHKeyToTags(ctx, &args.Tags, args.Image)
74+
if err != nil {
75+
return err
76+
}
77+
}
3878
env := strings.Join(args.Tags, " ")
3979
volume := strings.Join(args.Volumes, " ")
4080

pkg/utils/utils.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package utils
1010
import (
1111
"errors"
1212
"fmt"
13+
"io"
1314
"net"
1415
"os"
1516
"os/exec"
@@ -23,6 +24,13 @@ import (
2324
log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
2425
)
2526

27+
// SpawnRedirection is used to redirects the fluxes
28+
type SpawnRedirection struct {
29+
Stdin io.Reader
30+
Stdout io.Writer
31+
Stderr io.Writer
32+
}
33+
2634
// SSHExec executes a command over SSH and redirects file-descriptors
2735
func SSHExec(publicIPAddress string, privateIPAddress string, command []string, checkConnection bool, gateway string) error {
2836
gatewayUser := "root"
@@ -89,6 +97,28 @@ func NewSSHExecCmd(publicIPAddress string, privateIPAddress string, allocateTTY
8997
return sshCommand
9098
}
9199

100+
// GeneratingAnSSHKey generates an SSH key
101+
func GeneratingAnSSHKey(cfg SpawnRedirection, path string, name string) (string, error) {
102+
args := []string{
103+
"-t",
104+
"rsa",
105+
"-b",
106+
"4096",
107+
"-f",
108+
filepath.Join(path, name),
109+
"-N",
110+
"",
111+
"-C",
112+
"",
113+
}
114+
log.Infof("Executing commands %v", args)
115+
spawn := exec.Command("ssh-keygen", args...)
116+
spawn.Stdout = cfg.Stdout
117+
spawn.Stdin = cfg.Stdin
118+
spawn.Stderr = cfg.Stderr
119+
return args[5], spawn.Run()
120+
}
121+
92122
// WaitForTCPPortOpen calls IsTCPPortOpen in a loop
93123
func WaitForTCPPortOpen(dest string) error {
94124
for {

0 commit comments

Comments
 (0)