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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ Tag a snapshot into an image.
Options:

-h, --help=false Print usage
--bootscript="" Assign a bootscript
```


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

#### Features

* Support of `scw tag --bootscript=""` option ([#149](https://github.com/scaleway/scaleway-cli/issues/149)
* `scw info` now prints user/organization info from the API ([#130](https://github.com/scaleway/scaleway-cli/issues/130)
* Added helpers to manipulate new `user_data` API ([#150](https://github.com/scaleway/scaleway-cli/issues/150))
* Support of `scw rm -f/--force` option ([#158](https://github.com/scaleway/scaleway-cli/issues/158))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ fi


NAME=$(basename "${URL}")
NAME=${NAME%.*}-$(date +%Y-%m-%d_%H:%M)
SNAPSHOT_NAME=${NAME%.*}-$(date +%Y-%m-%d_%H:%M)
IMAGE_NAME=${IMAGE_NAME:-$SNAPSHOT_NAME}
IMAGE_BOOTSCRIPT=${IMAGE_BOOTSCRIPT:stable}
VOLUME_SIZE=${VOLUME_SIZE:-50GB}
KEY=$(cat ~/.ssh/id_rsa.pub | awk '{ print $1" "$2 }' | tr ' ' '_')

Expand Down Expand Up @@ -50,12 +52,12 @@ echo "[+] Server stopped"


echo "[+] Creating a snapshot of nbd1"
SNAPSHOT=$(scw commit --volume=1 "${SERVER}" "${NAME}")
SNAPSHOT=$(scw commit --volume=1 "${SERVER}" "${SNAPSHOT_NAME}")
echo "[+] Snapshot ${SNAPSHOT} created"


echo "[+] Creating an image based of the snapshot"
IMAGE=$(scw tag "${SNAPSHOT}" "${NAME}")
IMAGE=$(scw tag --bootscript="${IMAGE_BOOTSCRIPT}" "${SNAPSHOT}" "${IMAGE_NAME}")
echo "[+] Image created: ${IMAGE}"


Expand Down
14 changes: 9 additions & 5 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,10 +506,11 @@ type ScalewaySnapshotDefinition struct {

// ScalewayImageDefinition represents a Scaleway image definition
type ScalewayImageDefinition struct {
SnapshotIDentifier string `json:"root_volume"`
Name string `json:"name,omitempty"`
Organization string `json:"organization"`
Arch string `json:"arch"`
SnapshotIDentifier string `json:"root_volume"`
Name string `json:"name,omitempty"`
Organization string `json:"organization"`
Arch string `json:"arch"`
DefaultBootscript *string `json:"default_bootscript,omitempty"`
}

// ScalewayRoleDefinition represents a Scaleway Token UserId Role
Expand Down Expand Up @@ -1007,13 +1008,16 @@ func (s *ScalewayAPI) PostSnapshot(volumeID string, name string) (string, error)
}

// PostImage creates a new image
func (s *ScalewayAPI) PostImage(volumeID string, name string) (string, error) {
func (s *ScalewayAPI) PostImage(volumeID string, name string, bootscript string) (string, error) {
definition := ScalewayImageDefinition{
SnapshotIDentifier: volumeID,
Name: name,
Organization: s.Organization,
Arch: "arm",
}
if bootscript != "" {
definition.DefaultBootscript = &bootscript
}

resp, err := s.PostResponse("images", definition)
if err != nil {
Expand Down
9 changes: 6 additions & 3 deletions pkg/cli/cmd_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ var cmdTag = &Command{

func init() {
cmdTag.Flag.BoolVar(&tagHelp, []string{"h", "-help"}, false, "Print usage")
cmdTag.Flag.StringVar(&tagBootscript, []string{"-bootscript"}, "", "Assign bootscript")
}

// Flags
var tagHelp bool // -h, --help flag
var tagHelp bool // -h, --help flag
var tagBootscript string // --bootscript flag

func runTag(cmd *Command, rawArgs []string) error {
if tagHelp {
Expand All @@ -29,8 +31,9 @@ func runTag(cmd *Command, rawArgs []string) error {
}

args := commands.TagArgs{
Snapshot: rawArgs[0],
Name: rawArgs[1],
Snapshot: rawArgs[0],
Name: rawArgs[1],
Bootscript: tagBootscript,
}
ctx := cmd.GetContext(rawArgs)
return commands.RunTag(ctx, args)
Expand Down
12 changes: 9 additions & 3 deletions pkg/commands/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import "fmt"

// TagArgs are flags for the `RunTag` function
type TagArgs struct {
Snapshot string
Name string
Snapshot string
Bootscript string
Name string
}

// RunTag is the handler for 'scw tag'
Expand All @@ -20,7 +21,12 @@ func RunTag(ctx CommandContext, args TagArgs) error {
return fmt.Errorf("cannot fetch snapshot: %v", err)
}

image, err := ctx.API.PostImage(snapshot.Identifier, args.Name)
bootscriptID := ""
if args.Bootscript != "" {
bootscriptID = ctx.API.GetBootscriptID(args.Bootscript)
}

image, err := ctx.API.PostImage(snapshot.Identifier, args.Name, bootscriptID)
if err != nil {
return fmt.Errorf("cannot create image: %v", err)
}
Expand Down