Move `writeConfig` logic to shared function

Moves the shared logic from `writeConfig` into a shared function in
`pkg/machine/machine_common.go`

[NO NEW TESTS NEEDED]

Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
This commit is contained in:
Jake Correnti 2023-08-01 20:46:13 -04:00
parent 597ccff0bc
commit 21ebe0e90a
4 changed files with 26 additions and 57 deletions

View File

@ -18,7 +18,6 @@ import (
"github.com/containers/podman/v4/pkg/machine"
"github.com/containers/podman/v4/pkg/util"
"github.com/containers/podman/v4/utils"
"github.com/containers/storage/pkg/ioutils"
"github.com/docker/go-units"
"github.com/sirupsen/logrus"
)
@ -673,22 +672,7 @@ func (m *HyperVMachine) forwardSocketPath() (*machine.VMFile, error) {
func (m *HyperVMachine) writeConfig() error {
// Write the JSON file
opts := &ioutils.AtomicFileWriterOptions{ExplicitCommit: true}
w, err := ioutils.NewAtomicFileWriterWithOpts(m.ConfigPath.GetPath(), 0644, opts)
if err != nil {
return err
}
defer w.Close()
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err := enc.Encode(m); err != nil {
return err
}
// Commit the changes to disk if no errors
return w.Commit()
return machine.WriteConfig(m.ConfigPath.Path, m)
}
func (m *HyperVMachine) setRootful(rootful bool) error {

View File

@ -4,10 +4,13 @@
package machine
import (
"encoding/json"
"fmt"
"net/url"
"os"
"strconv"
"github.com/containers/storage/pkg/ioutils"
)
// getDevNullFiles returns pointers to Read-only and Write-only DevNull files
@ -143,3 +146,23 @@ func SetRootful(rootful bool, name, rootfulName string) error {
}
return nil
}
// WriteConfig writes the machine's JSON config file
func WriteConfig(configPath string, v VM) error {
opts := &ioutils.AtomicFileWriterOptions{ExplicitCommit: true}
w, err := ioutils.NewAtomicFileWriterWithOpts(configPath, 0644, opts)
if err != nil {
return err
}
defer w.Close()
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err := enc.Encode(v); err != nil {
return err
}
// Commit the changes to disk if no errors
return w.Commit()
}

View File

@ -25,7 +25,6 @@ import (
"github.com/containers/podman/v4/pkg/machine"
"github.com/containers/podman/v4/pkg/rootless"
"github.com/containers/podman/v4/pkg/util"
"github.com/containers/storage/pkg/ioutils"
"github.com/containers/storage/pkg/lockfile"
"github.com/digitalocean/go-qemu/qmp"
"github.com/sirupsen/logrus"
@ -1523,22 +1522,7 @@ func (v *MachineVM) writeConfig() error {
return err
}
// Write the JSON file
opts := &ioutils.AtomicFileWriterOptions{ExplicitCommit: true}
w, err := ioutils.NewAtomicFileWriterWithOpts(v.ConfigPath.GetPath(), 0644, opts)
if err != nil {
return err
}
defer w.Close()
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err := enc.Encode(v); err != nil {
return err
}
// Commit the changes to disk if no errors
return w.Commit()
return machine.WriteConfig(v.ConfigPath.Path, v)
}
// getImageFile wrapper returns the path to the image used

View File

@ -22,7 +22,6 @@ import (
"github.com/containers/podman/v4/pkg/machine/wsl/wutil"
"github.com/containers/podman/v4/pkg/util"
"github.com/containers/storage/pkg/homedir"
"github.com/containers/storage/pkg/ioutils"
"github.com/sirupsen/logrus"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
@ -405,28 +404,7 @@ func downloadDistro(v *MachineVM, opts machine.InitOptions) error {
}
func (v *MachineVM) writeConfig() error {
const format = "could not write machine json config: %w"
jsonFile := v.ConfigPath
opts := &ioutils.AtomicFileWriterOptions{ExplicitCommit: true}
w, err := ioutils.NewAtomicFileWriterWithOpts(jsonFile, 0644, opts)
if err != nil {
return fmt.Errorf(format, err)
}
defer w.Close()
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err := enc.Encode(v); err != nil {
return fmt.Errorf(format, err)
}
// Commit the changes to disk if no error has occurred
if err := w.Commit(); err != nil {
return fmt.Errorf(format, err)
}
return nil
return machine.WriteConfig(v.ConfigPath, v)
}
func setupConnections(v *MachineVM, opts machine.InitOptions) error {