Skip to content

Commit

Permalink
scw create/run can take a snapshot as root volume (Fixes #19)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Jun 14, 2015
1 parent a0c972c commit 82ce801
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,10 @@ Same as Hack, without step 5

### master (unreleased)

#### Features

* `scw {create,run} IMAGE`, *IMAGE* can be a snapshot ([#19](https://github.com/scaleway/scaleway-cli/issues/19))

#### Fixes

* `scw run IMAGE [COMMAND]`, default *COMMAND* is now `if [ -x /bin/bash ]; then exec /bin/bash; else exec /bin/sh; fi`
Expand Down
8 changes: 6 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ func (s *ScalewayAPI) GetSnapshotID(needle string) string {
}

// GetImageID returns exactly one image matching or dies
func (s *ScalewayAPI) GetImageID(needle string) string {
func (s *ScalewayAPI) GetImageID(needle string, exitIfMissing bool) string {
images, err := s.ResolveImage(needle)
if err != nil {
log.Fatalf("Unable to resolve image %s: %s", needle, err)
Expand All @@ -1152,7 +1152,11 @@ func (s *ScalewayAPI) GetImageID(needle string) string {
return images[0]
}
if len(images) == 0 {
log.Fatalf("No such image: %s", needle)
if exitIfMissing {
log.Fatalf("No such image: %s", needle)
} else {
return ""
}
}
log.Errorf("Too many candidates for %s (%d)", needle, len(images))
for _, identifier := range images {
Expand Down
12 changes: 10 additions & 2 deletions api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,16 @@ func CreateServer(api *ScalewayAPI, imageName string, name string, bootscript st
} else {
// Use an existing image
// FIXME: handle snapshots
image := api.GetImageID(imageName)
server.Image = &image
image := api.GetImageID(imageName, false)
if image != "" {
server.Image = &image
}
snapshotID := api.GetSnapshotID(imageName)
snapshot, err := api.GetSnapshot(snapshotID)
if err != nil {
return "", err
}
server.Volumes["0"] = snapshot.BaseVolume.Identifier
}

serverID, err := api.PostServer(server)
Expand Down
2 changes: 1 addition & 1 deletion commands/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func runHistory(cmd *types.Command, args []string) {
cmd.PrintShortUsage()
}

imageID := cmd.API.GetImageID(args[0])
imageID := cmd.API.GetImageID(args[0], true)
image, err := cmd.API.GetImage(imageID)
if err != nil {
log.Fatalf("Cannot get image %s: %v", imageID, err)
Expand Down
2 changes: 1 addition & 1 deletion commands/rmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func runRmi(cmd *types.Command, args []string) {
}
hasError := false
for _, needle := range args {
image := cmd.API.GetImageID(needle)
image := cmd.API.GetImageID(needle, true)
err := cmd.API.DeleteImage(image)
if err != nil {
log.Errorf("failed to delete image %s: %s", image, err)
Expand Down

0 comments on commit 82ce801

Please sign in to comment.