Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions cmd/bauklotze/machine/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func start(cmd *cobra.Command, args []string) error {

// If the user given the report url, then overwrite the report url into mc
if allFlag.ReportURL != "" {
mc.ReportURL = &io.VMFile{Path: allFlag.ReportURL}
mc.ReportURL = &io.FileWrapper{Path: allFlag.ReportURL}
}

g, ctx := errgroup.WithContext(context.Background())
Expand Down Expand Up @@ -187,13 +187,13 @@ func cleanUp(mc *vmconfig.MachineConfig) {
gvpBackendSocket, _ := mc.GVProxyNetworkBackendSocks()
_ = gvpBackendSocket.Delete(true)

gvpBackendSocket2 := &io.VMFile{Path: fmt.Sprintf("%s-%s", gvpBackendSocket.GetPath(), "krun.sock")}
gvpBackendSocket2 := &io.FileWrapper{Path: fmt.Sprintf("%s-%s", gvpBackendSocket.GetPath(), "krun.sock")}
_ = gvpBackendSocket2.Delete(true)

podmanInHost := mc.PodmanAPISocketHost()
_ = podmanInHost.Delete(true)

gvpPidFile := &io.VMFile{Path: mc.GvProxy.PidFile}
gvpPidFile := &io.FileWrapper{Path: mc.GvProxy.PidFile}
_ = gvpPidFile.Delete(true)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/api/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ type APIServer struct {
func RestService(ctx context.Context, mc *vmconfig.MachineConfig, endPoint string) error {
// Set stdin to /dev/null
_ = internal.RedirectStdin()
// When deleting files, wrap the path in a `&io.VMFile` so that the file is safely deleted.
// When deleting files, wrap the path in a `&io.FileWrapper` so that the file is safely deleted.
// The Delete(true) operation will ensure that **only files in the workspace are deleted**
UDF := &io.VMFile{Path: endPoint}
UDF := &io.FileWrapper{Path: endPoint}
if err := UDF.Delete(true); err != nil {
return fmt.Errorf("failed to delete file %q: %w", UDF.GetPath(), err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/machine/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func VirtIOFsToVFKitVirtIODevice(mounts []*volumes.Mount) ([]vfConfig.VirtioDevi
return virtioDevices, nil
}

func CreateAndResizeDisk(f *io.VMFile, newSize strongunits.GiB) error {
func CreateAndResizeDisk(f *io.FileWrapper, newSize strongunits.GiB) error {
if f.Exist() {
if err := f.Delete(true); err != nil {
return fmt.Errorf("failed to delete disk: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/machine/ignition/ignition_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func GenerateIgnScripts(mc *vmconfig.MachineConfig) error {
ign := NewIgnitionBuilder(
&DynamicIgnitionV3{
CodeBuffer: nil,
IgnFile: io.VMFile{
IgnFile: io.FileWrapper{
Path: ignScriptFile,
},
VMType: defconfig.LibKrun,
Mounts: mc.Mounts,
SSHIdentityPath: io.VMFile{
SSHIdentityPath: io.FileWrapper{
Path: mc.SSH.IdentityPath,
},
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/machine/ignition/ignition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestNewIgnitionBuilder(t *testing.T) {
ign := NewIgnitionBuilder(
&DynamicIgnitionV3{
CodeBuffer: nil,
IgnFile: io.VMFile{
IgnFile: io.FileWrapper{
Path: filepath.Join("/tmp", "initfs", "ign.sh"),
},
VMType: defconfig.LibKrun,
Expand Down
4 changes: 2 additions & 2 deletions pkg/machine/ignition/ignition_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
)

type DynamicIgnitionV3 struct {
IgnFile io.VMFile
IgnFile io.FileWrapper
VMType defconfig.VMType
Mounts []*volumes.Mount
SSHIdentityPath io.VMFile
SSHIdentityPath io.FileWrapper
TimeZone string
CodeBuffer *bytes.Buffer
}
Expand Down
27 changes: 14 additions & 13 deletions pkg/machine/io/vmfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,43 @@
package io

import (
allFlag "bauklotze/pkg/machine/allflag"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"

allFlag "bauklotze/pkg/machine/allflag"

"github.com/sirupsen/logrus"

"github.com/containers/common/pkg/strongunits"
)

type VMFile struct {
type FileWrapper struct {
Path string `json:"path,omitempty"`
}

// NewMachineFile is a constructor for VMFile
func NewMachineFile(f string) (*VMFile, error) {
// NewMachineFile is a constructor for FileWrapper
func NewMachineFile(f string) (*FileWrapper, error) {
if len(f) < 1 {
return nil, errors.New("invalid file path, must be at least 1 character")
}

mf := VMFile{Path: f}
mf := FileWrapper{Path: f}
return &mf, nil
}

// GetPath returns the working path for a machinefile. it returns
// the symlink unless one does not exist
func (m *VMFile) GetPath() string {
func (m *FileWrapper) GetPath() string {
return m.Path
}

// Delete dangerous removes a file from the filesystem
// if safety is true, it will only remove files in the workspace
func (m *VMFile) Delete(safety bool) error {
func (m *FileWrapper) Delete(safety bool) error {
if safety {
workspace := allFlag.WorkSpace
if workspace == "" {
Expand All @@ -58,17 +59,17 @@ func (m *VMFile) Delete(safety bool) error {
}

// Read the contents of a given file and return in []bytes
func (m *VMFile) Read() ([]byte, error) {
func (m *FileWrapper) Read() ([]byte, error) {
return os.ReadFile(m.GetPath()) //nolint:wrapcheck
}

func (m *VMFile) Exist() bool {
func (m *FileWrapper) Exist() bool {
_, err := os.Stat(m.Path)
return err == nil
}

// DiscardBytesAtBegin discards the first n MB of a file
func (m *VMFile) DiscardBytesAtBegin(n strongunits.MiB) error {
func (m *FileWrapper) DiscardBytesAtBegin(n strongunits.MiB) error {
fileInfo, err := os.Stat(m.Path)
if err != nil {
return fmt.Errorf("failed to get file info: %w", err)
Expand All @@ -94,15 +95,15 @@ func (m *VMFile) DiscardBytesAtBegin(n strongunits.MiB) error {
}

// AppendToNewVMFile takes a given path and appends it to the existing vmfile path. The new
// VMFile is returned
func (m *VMFile) AppendToNewVMFile(additionalPath string) (*VMFile, error) {
// FileWrapper is returned
func (m *FileWrapper) AppendToNewVMFile(additionalPath string) (*FileWrapper, error) {
if additionalPath == "" {
return nil, errors.New("invalid additional path")
}
return NewMachineFile(filepath.Join(m.Path, additionalPath))
}

func (m *VMFile) MakeBaseDir() error {
func (m *FileWrapper) MakeBaseDir() error {
err := os.MkdirAll(filepath.Dir(m.GetPath()), os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create base dir: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/machine/shim/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func Init(mp vmconfig.VMProvider) error {

// set ReportURL into machine configure
if allFlag.ReportURL != "" {
mc.ReportURL = &io.VMFile{Path: allFlag.ReportURL}
mc.ReportURL = &io.FileWrapper{Path: allFlag.ReportURL}
}

// Write the machine configure as json into mc.ConfigPath.GetPath()
Expand Down
4 changes: 2 additions & 2 deletions pkg/machine/shim/networking.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func ConductVMReadinessCheck(ctx context.Context, mc *vmconfig.MachineConfig) bo
}

// startNetworking return podman socks in host, podman socks in guest, error
func startNetworking(ctx context.Context, mc *vmconfig.MachineConfig) (*io.VMFile, *io.VMFile, error) {
func startNetworking(ctx context.Context, mc *vmconfig.MachineConfig) (*io.FileWrapper, *io.FileWrapper, error) {
// socksInHost($workspace/tmp/[machine]-podman-api.socks) <--> socksInGuest(podman server)
socksInHost, socksInGuest, err := setupPodmanSocketsPath(mc)
if err != nil {
Expand All @@ -54,5 +54,5 @@ func startNetworking(ctx context.Context, mc *vmconfig.MachineConfig) (*io.VMFil
return nil, nil, fmt.Errorf("failed to start forwarder: %w", err)
}

return &io.VMFile{Path: socksInHost}, &io.VMFile{Path: socksInGuest}, err
return &io.FileWrapper{Path: socksInHost}, &io.FileWrapper{Path: socksInGuest}, err
}
16 changes: 8 additions & 8 deletions pkg/machine/vmconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ type ResourceConfig struct {
}

type MachineDirs struct {
ConfigDir *io.VMFile `json:"ConfigDir"`
DataDir *io.VMFile `json:"DataDir"`
LogsDir *io.VMFile `json:"LogsDir"`
ConfigDir *io.FileWrapper `json:"ConfigDir"`
DataDir *io.FileWrapper `json:"DataDir"`
LogsDir *io.FileWrapper `json:"LogsDir"`
Hypervisor *Hypervisor `json:"Hypervisor"`
NetworkProvider *NetworkProvider `json:"NetworkProvider"`
SocksDir *io.VMFile `json:"SocksDir"`
SocksDir *io.FileWrapper `json:"SocksDir"`
}

type Hypervisor struct {
LibsDir *io.VMFile `json:"LibsDir"`
Bin *io.VMFile `json:"Bin"`
LibsDir *io.FileWrapper `json:"LibsDir"`
Bin *io.FileWrapper `json:"Bin"`
}

type NetworkProvider struct {
LibsDir *io.VMFile `json:"LibsDir"`
Bin *io.VMFile `json:"Bin"`
LibsDir *io.FileWrapper `json:"LibsDir"`
Bin *io.FileWrapper `json:"Bin"`
}
16 changes: 8 additions & 8 deletions pkg/machine/vmconfig/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (
)

var (
workSpace *io.VMFile
workSpace *io.FileWrapper
Once sync.Once
)

// SetWorkSpace is a function given a string, it returns a pointer to a VMDir and an error
func SetWorkSpace(s string) (*io.VMFile, error) {
func SetWorkSpace(s string) (*io.FileWrapper, error) {
var err error
Once.Do(func() {
workSpace, err = io.NewMachineFile(s)
Expand All @@ -32,7 +32,7 @@ func SetWorkSpace(s string) (*io.VMFile, error) {
}

// GetWorkSpace is a function that returns the workspace and an error
func GetWorkSpace() (*io.VMFile, error) {
func GetWorkSpace() (*io.FileWrapper, error) {
if workSpace == nil || workSpace.GetPath() == "" {
return nil, fmt.Errorf("workspace is not set")
}
Expand Down Expand Up @@ -82,16 +82,16 @@ func GetMachineDirs(vmType defconfig.VMType) (*MachineDirs, error) {
return nil, fmt.Errorf("unable to new machine file in %s: %w", logsDir, err)
}

var hypervisorBin *io.VMFile
var hypervisorBin *io.FileWrapper
// If hypervisor is Krunkit, use krunkit binary name
if vmType.String() == defconfig.LibKrun.String() {
hypervisorBin = &io.VMFile{Path: filepath.Join(libexecDir.GetPath(), define.KrunkitBinaryName)}
hypervisorBin = &io.FileWrapper{Path: filepath.Join(libexecDir.GetPath(), define.KrunkitBinaryName)}
} else {
// If hypervisor is vfkit, use vfkit binary name
hypervisorBin = &io.VMFile{Path: filepath.Join(libexecDir.GetPath(), define.VfkitBinaryName)}
hypervisorBin = &io.FileWrapper{Path: filepath.Join(libexecDir.GetPath(), define.VfkitBinaryName)}
}

networkProviderBin := &io.VMFile{
networkProviderBin := &io.FileWrapper{
Path: filepath.Join(libexecDir.GetPath(), define.GvProxyBinaryName),
}

Expand Down Expand Up @@ -124,7 +124,7 @@ func GetMachineDirs(vmType defconfig.VMType) (*MachineDirs, error) {
}

// GetSSHIdentityPath returns the path to the expected SSH private key
func GetSSHIdentityPath(vmType defconfig.VMType) (*io.VMFile, error) {
func GetSSHIdentityPath(vmType defconfig.VMType) (*io.FileWrapper, error) {
dirs, err := GetMachineDirs(vmType)
if err != nil {
return nil, fmt.Errorf("failed to get machine dirs: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/machine/vmconfig/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// ConfigDir is a simple helper to obtain the machine config dir
func (mc *MachineConfig) ConfigDir() (*io.VMFile, error) {
func (mc *MachineConfig) ConfigDir() (*io.FileWrapper, error) {
if mc.Dirs == nil || mc.Dirs.ConfigDir == nil {
return nil, errors.New("no configuration directory set")
}
Expand Down
22 changes: 11 additions & 11 deletions pkg/machine/vmconfig/vmconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type VMProvider interface { //nolint:interfacebloat
StartVM(ctx context.Context, mc *MachineConfig) error
}

func (mc *MachineConfig) PodmanAPISocketHost() *io.VMFile {
func (mc *MachineConfig) PodmanAPISocketHost() *io.FileWrapper {
socksDir := mc.Dirs.SocksDir
s := fmt.Sprintf("%s-podman-api.sock", mc.VMName)
podmanAPI, _ := socksDir.AppendToNewVMFile(s)
Expand Down Expand Up @@ -66,23 +66,23 @@ type MachineConfig struct {
AppleKrunkitHypervisor *AppleKrunkitConfig `json:"AppleKrunkitHypervisor,omitempty"`
AppleVFkitHypervisor *AppleVFkitConfig `json:"AppleVFkitConfig,omitempty"`

ConfigPath *io.VMFile `json:"ConfigPath"`
ConfigPath *io.FileWrapper `json:"ConfigPath"`
Resources ResourceConfig `json:"Resources"`
Mounts []*volumes.Mount `json:"Mounts"`
GvProxy GvproxyCommand `json:"GvProxy"`
SSH SSHConfig `json:"SSH"`
Starting bool `json:"Starting"`
ReportURL *io.VMFile `json:"ReportURL,omitempty"`
ReportURL *io.FileWrapper `json:"ReportURL,omitempty"`
}

type Bootable struct {
Image *io.VMFile `json:"ImagePath" validate:"required"`
Version string `json:"Version" validate:"required"`
Image *io.FileWrapper `json:"ImagePath" validate:"required"`
Version string `json:"Version" validate:"required"`
}

type DataDisk struct {
Image *io.VMFile `json:"ImagePath" validate:"required"`
Version string `json:"Version" validate:"required"`
Image *io.FileWrapper `json:"ImagePath" validate:"required"`
Version string `json:"Version" validate:"required"`
}

type GvproxyCommand struct {
Expand Down Expand Up @@ -118,7 +118,7 @@ type SSHConfig struct {
}

// NewMachineConfig construct a machine configure but **not* write into disk
func NewMachineConfig(dirs *MachineDirs, sshKey *io.VMFile, mtype defconfig.VMType) (*MachineConfig, error) {
func NewMachineConfig(dirs *MachineDirs, sshKey *io.FileWrapper, mtype defconfig.VMType) (*MachineConfig, error) {
mc := new(MachineConfig)
mc.VMName = allFlag.VMName
mc.Dirs = dirs
Expand Down Expand Up @@ -200,7 +200,7 @@ func LoadMachineByName(name string, dirs *MachineDirs) (*MachineConfig, error) {
return mc, nil
}

func loadMachineFromFQPath(f *io.VMFile) (*MachineConfig, error) {
func loadMachineFromFQPath(f *io.FileWrapper) (*MachineConfig, error) {
mc := new(MachineConfig)
b, err := f.Read()
if err != nil {
Expand All @@ -218,7 +218,7 @@ func loadMachineFromFQPath(f *io.VMFile) (*MachineConfig, error) {
return mc, nil
}

func (mc *MachineConfig) GVProxyNetworkBackendSocks() (*io.VMFile, error) {
func (mc *MachineConfig) GVProxyNetworkBackendSocks() (*io.FileWrapper, error) {
socksDir, err := mc.SocksDir()
if err != nil {
return nil, fmt.Errorf("failed to get workspace tmp dir: %w", err)
Expand All @@ -227,7 +227,7 @@ func (mc *MachineConfig) GVProxyNetworkBackendSocks() (*io.VMFile, error) {
}

// SocksDir is simple helper function to obtain the workspace tmp dir
func (mc *MachineConfig) SocksDir() (*io.VMFile, error) {
func (mc *MachineConfig) SocksDir() (*io.FileWrapper, error) {
if mc.Dirs == nil || mc.Dirs.SocksDir.GetPath() == "" {
return nil, errors.New("no workspace socks directory set")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/machine/vmconfig/vmconfigs_drawin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
type Helper struct {
LogLevel logrus.Level `json:"LogLevel"`
Endpoint string `json:"Endpoint"`
BinaryPath *io.VMFile `json:"BinaryPath"`
BinaryPath *io.FileWrapper `json:"BinaryPath"`
VirtualMachine *vfConfig.VirtualMachine `json:"VirtualMachine"`
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/ssh/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

var sshCommand = []string{"ssh-keygen", "-N", "", "-t", "ed25519", "-f"}

func CreateSSHKeys(f *io.VMFile) (string, error) {
func CreateSSHKeys(f *io.FileWrapper) (string, error) {
err := f.MakeBaseDir()
if err != nil {
return "", fmt.Errorf("failed to create ssh key directory: %w", err)
Expand Down Expand Up @@ -54,9 +54,9 @@ func generatekeys(writeLocation string) error {

// GetSSHKeys checks to see if there is a ssh key at the provided location.
// If not, we create the priv and pub keys. The ssh key is then returned.
func GetSSHKeys(f *io.VMFile) (string, error) {
func GetSSHKeys(f *io.FileWrapper) (string, error) {
if f.Exist() {
pubF := io.VMFile{
pubF := io.FileWrapper{
Path: f.GetPath() + ".pub",
}
logrus.Infof("SSH key already exists,read the %s", f.GetPath())
Expand Down
Loading