Skip to content

Commit 82ce801

Browse files
committed
scw create/run can take a snapshot as root volume (Fixes #19)
1 parent a0c972c commit 82ce801

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,10 @@ Same as Hack, without step 5
950950

951951
### master (unreleased)
952952

953+
#### Features
954+
955+
* `scw {create,run} IMAGE`, *IMAGE* can be a snapshot ([#19](https://github.com/scaleway/scaleway-cli/issues/19))
956+
953957
#### Fixes
954958

955959
* `scw run IMAGE [COMMAND]`, default *COMMAND* is now `if [ -x /bin/bash ]; then exec /bin/bash; else exec /bin/sh; fi`

api/api.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ func (s *ScalewayAPI) GetSnapshotID(needle string) string {
11431143
}
11441144

11451145
// GetImageID returns exactly one image matching or dies
1146-
func (s *ScalewayAPI) GetImageID(needle string) string {
1146+
func (s *ScalewayAPI) GetImageID(needle string, exitIfMissing bool) string {
11471147
images, err := s.ResolveImage(needle)
11481148
if err != nil {
11491149
log.Fatalf("Unable to resolve image %s: %s", needle, err)
@@ -1152,7 +1152,11 @@ func (s *ScalewayAPI) GetImageID(needle string) string {
11521152
return images[0]
11531153
}
11541154
if len(images) == 0 {
1155-
log.Fatalf("No such image: %s", needle)
1155+
if exitIfMissing {
1156+
log.Fatalf("No such image: %s", needle)
1157+
} else {
1158+
return ""
1159+
}
11561160
}
11571161
log.Errorf("Too many candidates for %s (%d)", needle, len(images))
11581162
for _, identifier := range images {

api/helpers.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,16 @@ func CreateServer(api *ScalewayAPI, imageName string, name string, bootscript st
241241
} else {
242242
// Use an existing image
243243
// FIXME: handle snapshots
244-
image := api.GetImageID(imageName)
245-
server.Image = &image
244+
image := api.GetImageID(imageName, false)
245+
if image != "" {
246+
server.Image = &image
247+
}
248+
snapshotID := api.GetSnapshotID(imageName)
249+
snapshot, err := api.GetSnapshot(snapshotID)
250+
if err != nil {
251+
return "", err
252+
}
253+
server.Volumes["0"] = snapshot.BaseVolume.Identifier
246254
}
247255

248256
serverID, err := api.PostServer(server)

commands/history.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func runHistory(cmd *types.Command, args []string) {
3939
cmd.PrintShortUsage()
4040
}
4141

42-
imageID := cmd.API.GetImageID(args[0])
42+
imageID := cmd.API.GetImageID(args[0], true)
4343
image, err := cmd.API.GetImage(imageID)
4444
if err != nil {
4545
log.Fatalf("Cannot get image %s: %v", imageID, err)

commands/rmi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func runRmi(cmd *types.Command, args []string) {
4040
}
4141
hasError := false
4242
for _, needle := range args {
43-
image := cmd.API.GetImageID(needle)
43+
image := cmd.API.GetImageID(needle, true)
4444
err := cmd.API.DeleteImage(image)
4545
if err != nil {
4646
log.Errorf("failed to delete image %s: %s", image, err)

0 commit comments

Comments
 (0)