Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for downloading an exported instance snapshot #27

Merged
merged 4 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions builder/exoscale/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&stepStopInstance{builder: b},
&stepSnapshotInstance{builder: b},
&stepExportSnapshot{builder: b},
&stepDownloadSnapshot{builder: b},
&stepRegisterTemplate{builder: b},
&stepCopyTemplate{builder: b},
}
Expand Down
1 change: 1 addition & 0 deletions builder/exoscale/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Config struct {
InstanceSecurityGroups []string `mapstructure:"instance_security_groups"`
InstancePrivateNetworks []string `mapstructure:"instance_private_networks"`
InstanceSSHKey string `mapstructure:"instance_ssh_key"`
SnapshotDownload bool `mapstructure:"snapshot_download"`
TemplateZones []string `mapstructure:"template_zones"`
TemplateName string `mapstructure:"template_name"`
TemplateDescription string `mapstructure:"template_description"`
Expand Down
2 changes: 2 additions & 0 deletions builder/exoscale/config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions builder/exoscale/step_download_snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package exoscale

import (
"context"
"fmt"
"io"
"net/http"
"os"

"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
)

type stepDownloadSnapshot struct {
builder *Builder
}

func (s *stepDownloadSnapshot) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
var (
snapshotURL = state.Get("snapshot_url").(string)
snapshotChecksum = state.Get("snapshot_checksum").(string)
ui = state.Get("ui").(packer.Ui)
)

if !s.builder.config.SnapshotDownload {
return multistep.ActionContinue
}

ui.Say("Downloading compute instance snapshot")

if err := s.downloadSnapshot(ui, snapshotURL); err != nil {
ui.Error(fmt.Sprintf("Unable to download compute instance snapshot: %v", err))
return multistep.ActionHalt

}
if err := s.createChecksumFile(snapshotChecksum); err != nil {
ui.Error(fmt.Sprintf("Unable to create checksum file of the snapshot: %v", err))
return multistep.ActionHalt
}

return multistep.ActionContinue
}

func (s *stepDownloadSnapshot) downloadSnapshot(ui packer.Ui, snapshotURL string) error {
templateFile := s.builder.config.TemplateName + ".template"
sternik marked this conversation as resolved.
Show resolved Hide resolved

out, err := os.Create(templateFile)
if err != nil {
return err
}
defer out.Close()

resp, err := http.Get(snapshotURL)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
}

pf := ui.TrackProgress(templateFile, 0, 0, resp.Body)
defer pf.Close()

_, err = io.Copy(out, pf)
if err != nil {
return err
}

return nil
}

func (s *stepDownloadSnapshot) createChecksumFile(snapshotChecksum string) error {
out, err := os.Create(s.builder.config.TemplateName + ".md5")
sternik marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
defer out.Close()

if _, err := out.WriteString(fmt.Sprintf("%s %s", snapshotChecksum, s.builder.config.TemplateName+".template")); err != nil {
sternik marked this conversation as resolved.
Show resolved Hide resolved
return err
}

return nil
}

func (s *stepDownloadSnapshot) Cleanup(_ multistep.StateBag) {}
3 changes: 3 additions & 0 deletions docs/builders/exoscale.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ other OS, we recommend using the [QEMU][packerqemu] plugin combined with the
will be created before creating the instance, and destroyed after a
successful build.

- `snapshot_download` (boolean) - Whether to download a snapshot of the compute
instance. Defaults to `false`.

exo-cedric marked this conversation as resolved.
Show resolved Hide resolved
- `template_description` (string) - The description of the template.

- `template_username` (string) - An optional username to be used to log into
Expand Down