diff --git a/README.md b/README.md index abedae96d6..52d94eb67f 100644 --- a/README.md +++ b/README.md @@ -753,6 +753,7 @@ Tag a snapshot into an image. Options: -h, --help=false Print usage + --bootscript="" Assign a bootscript ``` @@ -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)) diff --git a/examples/create-image-from-s3.sh b/examples/create-image-from-http.sh similarity index 85% rename from examples/create-image-from-s3.sh rename to examples/create-image-from-http.sh index 3f3498441e..985c8e7cbd 100755 --- a/examples/create-image-from-s3.sh +++ b/examples/create-image-from-http.sh @@ -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 ' ' '_') @@ -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}" diff --git a/pkg/api/api.go b/pkg/api/api.go index a70569777e..ff09ba3bad 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -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 @@ -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 { diff --git a/pkg/cli/cmd_tag.go b/pkg/cli/cmd_tag.go index bc779935a3..991f593cfb 100644 --- a/pkg/cli/cmd_tag.go +++ b/pkg/cli/cmd_tag.go @@ -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 { @@ -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) diff --git a/pkg/commands/tag.go b/pkg/commands/tag.go index d381a7533a..9918524fff 100644 --- a/pkg/commands/tag.go +++ b/pkg/commands/tag.go @@ -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' @@ -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) }