Skip to content

Commit ac4f906

Browse files
Add disk sync step before saving a new image (#46)
* Add sync disk step * Update readme * Update hc2 generate spec
1 parent e13f704 commit ac4f906

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ Then, run [`packer init`](https://www.packer.io/docs/commands/init).
1919
packer {
2020
required_plugins {
2121
macstadium-orka = {
22-
version = ">= 2.3.0"
22+
version = ">= 2.3.0, < 3.0.0"
2323
source = "github.com/macstadium/macstadium-orka"
2424
}
2525
}
2626
}
2727
```
2828

29-
3029
#### Manual installation
3130

3231
You can find pre-built binary releases of the plugin [here](https://github.com/macstadium/packer-plugin-macstadium-orka/releases).

builder/orka/builder.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ import (
66
"net/http"
77

88
"github.com/hashicorp/hcl/v2/hcldec"
9-
"github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
109
"github.com/hashicorp/packer-plugin-sdk/communicator"
1110
"github.com/hashicorp/packer-plugin-sdk/multistep"
11+
"github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
1212
"github.com/hashicorp/packer-plugin-sdk/packer"
1313
"github.com/macstadium/packer-plugin-macstadium-orka/mocks"
1414
)
1515

1616
const BuilderId = "orka"
1717

1818
type HttpClient interface {
19-
Do(req *http.Request) (*http.Response, error)
19+
Do(req *http.Request) (*http.Response, error)
2020
}
2121

2222
// Builder ...
@@ -39,7 +39,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
3939
return nil, warnings, nil
4040
}
4141

42-
4342
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
4443
// Setup the state bag and initial state for the steps.
4544
state := new(multistep.BasicStateBag)
@@ -62,19 +61,19 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
6261

6362
// Iniitialize communicatior
6463
var comm = &communicator.StepConnect{
65-
Config: &b.config.CommConfig,
66-
Host: CommHost(b.config.CommConfig.Host()),
67-
SSHPort: CommPort(b.config.CommConfig.Port()),
68-
SSHConfig: b.config.CommConfig.SSHConfigFunc(),
64+
Config: &b.config.CommConfig,
65+
Host: CommHost(b.config.CommConfig.Host()),
66+
SSHPort: CommPort(b.config.CommConfig.Port()),
67+
SSHConfig: b.config.CommConfig.SSHConfigFunc(),
6968
}
7069

71-
7270
// Add our SSH Communicator after our steps.
7371
if b.config.Mock == (MockOptions{}) {
7472
steps = append(
7573
steps,
7674
comm,
7775
new(commonsteps.StepProvision),
76+
new(stepSyncDisk),
7877
new(stepCreateImage),
7978
)
8079
} else {

builder/orka/config.hcl2spec.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/orka/step_sync_disk.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package orka
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"fmt"
7+
8+
"github.com/hashicorp/packer-plugin-sdk/multistep"
9+
"github.com/hashicorp/packer-plugin-sdk/packer"
10+
)
11+
12+
type stepSyncDisk struct {
13+
}
14+
15+
func (s *stepSyncDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
16+
ui := state.Get("ui").(packer.Ui)
17+
18+
var comm packer.Communicator
19+
if raw, ok := state.Get("communicator").(packer.Communicator); ok {
20+
comm = raw
21+
}
22+
23+
var stderr bytes.Buffer
24+
25+
ui.Say("Syncing disk changes...")
26+
27+
// Start the command
28+
cmd := packer.RemoteCmd{Command: "sync", Stderr: &stderr}
29+
if err := comm.Start(ctx, &cmd); err != nil {
30+
err := fmt.Errorf("failed to sync disk changes: %w; stdErr=%q", err, stderr.String())
31+
state.Put("error", err)
32+
ui.Error(err.Error())
33+
return multistep.ActionHalt
34+
}
35+
36+
// Wait for it to complete
37+
status := cmd.Wait()
38+
39+
if status != 0 {
40+
err := fmt.Errorf("failed to sync disk changes: status code %d; stdErr=%q", status, stderr.String())
41+
state.Put("error", err)
42+
ui.Error(err.Error())
43+
return multistep.ActionHalt
44+
}
45+
46+
// Continue processing
47+
return multistep.ActionContinue
48+
}
49+
50+
func (s *stepSyncDisk) Cleanup(multistep.StateBag) {
51+
}

0 commit comments

Comments
 (0)