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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ Options:
--bootscript="" Assign a bootscript
-e, --env="" Provide metadata tags passed to initrd (i.e., boot=resue INITRD_DEBUG=1)
-h, --help=false Print usage
--ip-address="" Assign an IP
--name="" Assign a name
--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 @@ -679,8 +680,10 @@ Options:
-e, --env="" Provide metadata tags passed to initrd (i.e., boot=resue 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
--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 @@ -1139,6 +1142,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'

### master (unreleased)

* Support of `scw create|run --ip-address` ([#235](https://github.com/scaleway/scaleway-cli/issues/235))
* Update gotty-client to 1.3.0
* Support of `scw run --show-boot` option ([#156](https://github.com/scaleway/scaleway-cli/issues/156))
* Remove go1.[34] support
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,8 @@ type ScalewayServerDefinition struct {

// Organization is the owner of the server
Organization string `json:"organization"`

PublicIP string `json:"public_ip,omitempty"`
}

// ScalewayOneServer represents the response of a GET /servers/UUID API call
Expand Down
61 changes: 45 additions & 16 deletions pkg/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
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"
"github.com/scaleway/scaleway-cli/vendor/github.com/moul/anonuuid"
)

// ScalewayResolvedIdentifier represents a list of matching identifier for a specifier pattern
Expand Down Expand Up @@ -283,23 +284,51 @@ func InspectIdentifiers(api *ScalewayAPI, ci chan ScalewayResolvedIdentifier, cj
close(cj)
}

type ConfigCreateServer struct {

Choose a reason for hiding this comment

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

exported type ConfigCreateServer should have comment or be unexported

ImageName string
Name string
Bootscript string
Env string
AdditionalVolumes string
DynamicIpRequired bool

Choose a reason for hiding this comment

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

struct field DynamicIpRequired should be DynamicIPRequired

IP string
}

// CreateServer creates a server using API based on typical server fields
func CreateServer(api *ScalewayAPI, imageName string, name string, bootscript string, env string, additionalVolumes string, dynamicIPRequired bool) (string, error) {
if name == "" {
name = strings.Replace(namesgenerator.GetRandomName(0), "_", "-", -1)
func CreateServer(api *ScalewayAPI, c *ConfigCreateServer) (string, error) {
if c.Name == "" {
c.Name = strings.Replace(namesgenerator.GetRandomName(0), "_", "-", -1)
}

var server ScalewayServerDefinition
server.Volumes = make(map[string]string)

server.DynamicIPRequired = &dynamicIPRequired

server.DynamicIPRequired = &c.DynamicIpRequired
if c.IP != "" {
if anonuuid.IsUUID(c.IP) == nil {
server.PublicIP = c.IP
} else {
ips, err := api.GetIPS()
if err != nil {
return "", err
}
for _, ip := range ips.IPS {
if ip.Address == c.IP {
server.PublicIP = ip.ID
break
}
}
if server.PublicIP == "" {
return "", fmt.Errorf("IP address %v not found", c.IP)
}
}
}
server.Tags = []string{}
if env != "" {
server.Tags = strings.Split(env, " ")
if c.Env != "" {
server.Tags = strings.Split(c.Env, " ")
}
if additionalVolumes != "" {
volumes := strings.Split(additionalVolumes, " ")
if c.AdditionalVolumes != "" {
volumes := strings.Split(c.AdditionalVolumes, " ")
for i := range volumes {
volumeID, err := CreateVolumeFromHumanSize(api, volumes[i])
if err != nil {
Expand All @@ -310,17 +339,17 @@ func CreateServer(api *ScalewayAPI, imageName string, name string, bootscript st
server.Volumes[volumeIDx] = *volumeID
}
}
server.Name = name
if bootscript != "" {
bootscript := api.GetBootscriptID(bootscript)
server.Name = c.Name
if c.Bootscript != "" {
bootscript := api.GetBootscriptID(c.Bootscript)
server.Bootscript = &bootscript
}

inheritingVolume := false
_, err := humanize.ParseBytes(imageName)
_, err := humanize.ParseBytes(c.ImageName)
if err == nil {
// Create a new root volume
volumeID, err := CreateVolumeFromHumanSize(api, imageName)
volumeID, err := CreateVolumeFromHumanSize(api, c.ImageName)
if err != nil {
return "", err
}
Expand All @@ -329,11 +358,11 @@ func CreateServer(api *ScalewayAPI, imageName string, name string, bootscript st
// Use an existing image
// FIXME: handle snapshots
inheritingVolume = true
image := api.GetImageID(imageName, false)
image := api.GetImageID(c.ImageName, false)
if image != "" {
server.Image = &image
} else {
snapshotID := api.GetSnapshotID(imageName)
snapshotID := api.GetSnapshotID(c.ImageName)
snapshot, err := api.GetSnapshot(snapshotID)
if err != nil {
return "", err
Expand Down
3 changes: 3 additions & 0 deletions pkg/cli/cmd_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func init() {
cmdCreate.Flag.StringVar(&createBootscript, []string{"-bootscript"}, "", "Assign a bootscript")
cmdCreate.Flag.StringVar(&createEnv, []string{"e", "-env"}, "", "Provide metadata tags passed to initrd (i.e., boot=resue INITRD_DEBUG=1)")
cmdCreate.Flag.StringVar(&createVolume, []string{"v", "-volume"}, "", "Attach additional volume (i.e., 50G)")
cmdCreate.Flag.StringVar(&createIPAddress, []string{"-ip-address"}, "", "Assign an IP")
cmdCreate.Flag.BoolVar(&createHelp, []string{"h", "-help"}, false, "Print usage")
cmdCreate.Flag.BoolVar(&createTmpSSHKey, []string{"-tmp-ssh-key"}, false, "Access your server without uploading your SSH key to your account")
}
Expand All @@ -41,6 +42,7 @@ var createEnv string // -e, --env flag
var createVolume string // -v, --volume flag
var createHelp bool // -h, --help flag
var createTmpSSHKey bool // --tmp-ssh-key flag
var createIPAddress string // --ip-address flag

func runCreate(cmd *Command, rawArgs []string) error {
if createHelp {
Expand All @@ -55,6 +57,7 @@ func runCreate(cmd *Command, rawArgs []string) error {
Bootscript: createBootscript,
Image: rawArgs[0],
TmpSSHKey: createTmpSSHKey,
IP: createIPAddress,
}

if len(createEnv) > 0 {
Expand Down
3 changes: 3 additions & 0 deletions pkg/cli/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func init() {
cmdRun.Flag.StringVar(&runCreateEnv, []string{"e", "-env"}, "", "Provide metadata tags passed to initrd (i.e., boot=resue 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.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")
cmdRun.Flag.StringVar(&runGateway, []string{"g", "-gateway"}, "", "Use a SSH gateway")
Expand All @@ -51,6 +52,7 @@ var runAutoRemove bool // --rm flag
var runCreateBootscript string // --bootscript flag
var runCreateEnv string // -e, --env flag
var runCreateVolume string // -v, --volume flag
var runIPAddress string // --ip-address flag
var runHelpFlag bool // -h, --help flag
var runAttachFlag bool // -a, --attach flag
var runDetachFlag bool // -d, --detach flag
Expand Down Expand Up @@ -98,6 +100,7 @@ func runRun(cmd *Command, rawArgs []string) error {
AutoRemove: runAutoRemove,
TmpSSHKey: runTmpSSHKey,
ShowBoot: runShowBoot,
IP: runIPAddress,
// FIXME: DynamicIPRequired
// FIXME: Timeout
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type CreateArgs struct {
Volumes []string
Image string
TmpSSHKey bool
IP string
}

// RunCreate is the handler for 'scw create'
Expand All @@ -32,7 +33,15 @@ func RunCreate(ctx CommandContext, args CreateArgs) error {

env := strings.Join(args.Tags, " ")
volume := strings.Join(args.Volumes, " ")
serverID, err := api.CreateServer(ctx.API, args.Image, args.Name, args.Bootscript, env, volume, true)
serverID, err := api.CreateServer(ctx.API, &api.ConfigCreateServer{
ImageName: args.Image,
Name: args.Name,
Bootscript: args.Bootscript,
Env: env,
AdditionalVolumes: volume,
DynamicIpRequired: args.IP == "",
IP: args.IP,
})
if err != nil {
return err
}
Expand Down
12 changes: 10 additions & 2 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type RunArgs struct {
Gateway string
Image string
Name string
IP string
Tags []string
Volumes []string
AutoRemove bool
Expand Down Expand Up @@ -82,8 +83,15 @@ func Run(ctx CommandContext, args RunArgs) error {

// create IMAGE
logrus.Info("Server creation ...")
dynamicIPRequired := args.Gateway == ""
serverID, err := api.CreateServer(ctx.API, args.Image, args.Name, args.Bootscript, env, volume, dynamicIPRequired)
serverID, err := api.CreateServer(ctx.API, &api.ConfigCreateServer{
ImageName: args.Image,
Name: args.Name,
Bootscript: args.Bootscript,
Env: env,
AdditionalVolumes: volume,
DynamicIpRequired: args.Gateway == "",
IP: args.IP,
})
if err != nil {
return fmt.Errorf("failed to create server: %v", err)
}
Expand Down