From ef7727238a9b8d543125b0cf90abb554ae86dd66 Mon Sep 17 00:00:00 2001 From: "Jason T. Greene" Date: Mon, 4 Mar 2024 16:24:53 -0600 Subject: [PATCH] Refactor env dir and port functions into new leaf pkgs [NO NEW TESTS NEEDED] Signed-off-by: Jason T. Greene --- cmd/podman/compose_machine.go | 4 +- cmd/podman/machine/info.go | 4 +- cmd/podman/machine/inspect.go | 3 +- cmd/podman/machine/machine.go | 4 +- cmd/podman/machine/os/manager.go | 3 +- cmd/podman/machine/reset.go | 3 +- cmd/podman/machine/rm.go | 3 +- cmd/podman/machine/set.go | 4 +- cmd/podman/machine/ssh.go | 3 +- cmd/podman/machine/start.go | 3 +- cmd/podman/machine/stop.go | 4 +- cmd/podman/system/reset_machine.go | 4 +- pkg/machine/config.go | 148 +---------------- pkg/machine/config_test.go | 5 +- pkg/machine/env/dir.go | 151 ++++++++++++++++++ .../{options_darwin.go => env/dir_darwin.go} | 2 +- .../dir_freebsd.go} | 2 +- .../{options_linux.go => env/dir_linux.go} | 2 +- .../dir_windows.go} | 2 +- pkg/machine/hyperv/stubber.go | 5 +- pkg/machine/machine_windows.go | 3 +- pkg/machine/os/machine_os.go | 3 +- pkg/machine/{ => ports}/ports.go | 9 +- pkg/machine/{ => ports}/ports_unix.go | 2 +- pkg/machine/{ => ports}/ports_windows.go | 2 +- pkg/machine/pull.go | 5 +- pkg/machine/shim/host.go | 9 +- pkg/machine/shim/networking.go | 3 +- pkg/machine/wsl/machine.go | 11 +- pkg/machine/wsl/stubber.go | 3 +- pkg/machine/wsl/usermodenet.go | 3 +- 31 files changed, 219 insertions(+), 193 deletions(-) create mode 100644 pkg/machine/env/dir.go rename pkg/machine/{options_darwin.go => env/dir_darwin.go} (90%) rename pkg/machine/{options_freebsd.go => env/dir_freebsd.go} (90%) rename pkg/machine/{options_linux.go => env/dir_linux.go} (93%) rename pkg/machine/{options_windows.go => env/dir_windows.go} (91%) rename pkg/machine/{ => ports}/ports.go (96%) rename pkg/machine/{ => ports}/ports_unix.go (98%) rename pkg/machine/{ => ports}/ports_windows.go (98%) diff --git a/cmd/podman/compose_machine.go b/cmd/podman/compose_machine.go index 953b1eae0f..0b7ea0d0f1 100644 --- a/cmd/podman/compose_machine.go +++ b/cmd/podman/compose_machine.go @@ -7,8 +7,8 @@ import ( "net/url" "strconv" - "github.com/containers/podman/v5/pkg/machine" "github.com/containers/podman/v5/pkg/machine/define" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/podman/v5/pkg/machine/provider" "github.com/containers/podman/v5/pkg/machine/vmconfigs" ) @@ -18,7 +18,7 @@ func getMachineConn(connectionURI string, parsedConnection *url.URL) (string, er if err != nil { return "", fmt.Errorf("getting machine provider: %w", err) } - dirs, err := machine.GetMachineDirs(machineProvider.VMType()) + dirs, err := env.GetMachineDirs(machineProvider.VMType()) if err != nil { return "", err } diff --git a/cmd/podman/machine/info.go b/cmd/podman/machine/info.go index 072c639fcb..793bc4efe8 100644 --- a/cmd/podman/machine/info.go +++ b/cmd/podman/machine/info.go @@ -14,8 +14,8 @@ import ( "github.com/containers/podman/v5/cmd/podman/validate" "github.com/containers/podman/v5/libpod/define" "github.com/containers/podman/v5/pkg/domain/entities" - "github.com/containers/podman/v5/pkg/machine" machineDefine "github.com/containers/podman/v5/pkg/machine/define" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/podman/v5/pkg/machine/vmconfigs" "github.com/spf13/cobra" "gopkg.in/yaml.v3" @@ -100,7 +100,7 @@ func hostInfo() (*entities.MachineHostInfo, error) { host.Arch = runtime.GOARCH host.OS = runtime.GOOS - dirs, err := machine.GetMachineDirs(provider.VMType()) + dirs, err := env.GetMachineDirs(provider.VMType()) if err != nil { return nil, err } diff --git a/cmd/podman/machine/inspect.go b/cmd/podman/machine/inspect.go index 4e7c5941a0..92ed761ce1 100644 --- a/cmd/podman/machine/inspect.go +++ b/cmd/podman/machine/inspect.go @@ -10,6 +10,7 @@ import ( "github.com/containers/podman/v5/cmd/podman/registry" "github.com/containers/podman/v5/cmd/podman/utils" "github.com/containers/podman/v5/pkg/machine" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/podman/v5/pkg/machine/vmconfigs" "github.com/spf13/cobra" ) @@ -47,7 +48,7 @@ func inspect(cmd *cobra.Command, args []string) error { var ( errs utils.OutputErrors ) - dirs, err := machine.GetMachineDirs(provider.VMType()) + dirs, err := env.GetMachineDirs(provider.VMType()) if err != nil { return err } diff --git a/cmd/podman/machine/machine.go b/cmd/podman/machine/machine.go index dc9b7b9f6b..1dd65c1ec8 100644 --- a/cmd/podman/machine/machine.go +++ b/cmd/podman/machine/machine.go @@ -15,7 +15,7 @@ import ( "github.com/containers/podman/v5/cmd/podman/registry" "github.com/containers/podman/v5/cmd/podman/validate" "github.com/containers/podman/v5/libpod/events" - "github.com/containers/podman/v5/pkg/machine" + "github.com/containers/podman/v5/pkg/machine/env" provider2 "github.com/containers/podman/v5/pkg/machine/provider" "github.com/containers/podman/v5/pkg/machine/vmconfigs" "github.com/containers/podman/v5/pkg/util" @@ -82,7 +82,7 @@ func getMachines(toComplete string) ([]string, cobra.ShellCompDirective) { if err != nil { return nil, cobra.ShellCompDirectiveNoFileComp } - dirs, err := machine.GetMachineDirs(provider.VMType()) + dirs, err := env.GetMachineDirs(provider.VMType()) if err != nil { return nil, cobra.ShellCompDirectiveNoFileComp } diff --git a/cmd/podman/machine/os/manager.go b/cmd/podman/machine/os/manager.go index 0d402cd5c9..1e028be238 100644 --- a/cmd/podman/machine/os/manager.go +++ b/cmd/podman/machine/os/manager.go @@ -10,6 +10,7 @@ import ( machineconfig "github.com/containers/common/pkg/machine" pkgMachine "github.com/containers/podman/v5/pkg/machine" + "github.com/containers/podman/v5/pkg/machine/env" pkgOS "github.com/containers/podman/v5/pkg/machine/os" "github.com/containers/podman/v5/pkg/machine/provider" "github.com/containers/podman/v5/pkg/machine/vmconfigs" @@ -52,7 +53,7 @@ func machineOSManager(opts ManagerOpts, _ vmconfigs.VMProvider) (pkgOS.Manager, if err != nil { return nil, err } - dirs, err := pkgMachine.GetMachineDirs(p.VMType()) + dirs, err := env.GetMachineDirs(p.VMType()) if err != nil { return nil, err } diff --git a/cmd/podman/machine/reset.go b/cmd/podman/machine/reset.go index 920b553a1f..8c2f01e19a 100644 --- a/cmd/podman/machine/reset.go +++ b/cmd/podman/machine/reset.go @@ -12,6 +12,7 @@ import ( "github.com/containers/podman/v5/cmd/podman/registry" "github.com/containers/podman/v5/cmd/podman/validate" "github.com/containers/podman/v5/pkg/machine" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/podman/v5/pkg/machine/shim" "github.com/containers/podman/v5/pkg/machine/vmconfigs" "github.com/spf13/cobra" @@ -50,7 +51,7 @@ func reset(_ *cobra.Command, _ []string) error { err error ) - dirs, err := machine.GetMachineDirs(provider.VMType()) + dirs, err := env.GetMachineDirs(provider.VMType()) if err != nil { return err } diff --git a/cmd/podman/machine/rm.go b/cmd/podman/machine/rm.go index 4a2130cbd8..5b76e1c19c 100644 --- a/cmd/podman/machine/rm.go +++ b/cmd/podman/machine/rm.go @@ -6,6 +6,7 @@ import ( "github.com/containers/podman/v5/cmd/podman/registry" "github.com/containers/podman/v5/libpod/events" "github.com/containers/podman/v5/pkg/machine" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/podman/v5/pkg/machine/shim" "github.com/containers/podman/v5/pkg/machine/vmconfigs" "github.com/spf13/cobra" @@ -54,7 +55,7 @@ func rm(_ *cobra.Command, args []string) error { vmName = args[0] } - dirs, err := machine.GetMachineDirs(provider.VMType()) + dirs, err := env.GetMachineDirs(provider.VMType()) if err != nil { return err } diff --git a/cmd/podman/machine/set.go b/cmd/podman/machine/set.go index 431de52b5d..f6aad5e41d 100644 --- a/cmd/podman/machine/set.go +++ b/cmd/podman/machine/set.go @@ -8,8 +8,8 @@ import ( "github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/strongunits" "github.com/containers/podman/v5/cmd/podman/registry" - "github.com/containers/podman/v5/pkg/machine" "github.com/containers/podman/v5/pkg/machine/define" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/podman/v5/pkg/machine/shim" "github.com/containers/podman/v5/pkg/machine/vmconfigs" "github.com/spf13/cobra" @@ -99,7 +99,7 @@ func setMachine(cmd *cobra.Command, args []string) error { vmName = args[0] } - dirs, err := machine.GetMachineDirs(provider.VMType()) + dirs, err := env.GetMachineDirs(provider.VMType()) if err != nil { return err } diff --git a/cmd/podman/machine/ssh.go b/cmd/podman/machine/ssh.go index 73bed582c4..7c9346b47e 100644 --- a/cmd/podman/machine/ssh.go +++ b/cmd/podman/machine/ssh.go @@ -7,6 +7,7 @@ import ( "net/url" "github.com/containers/podman/v5/pkg/machine/define" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/common/pkg/completion" "github.com/containers/podman/v5/cmd/podman/registry" @@ -54,7 +55,7 @@ func ssh(cmd *cobra.Command, args []string) error { validVM bool ) - dirs, err := machine.GetMachineDirs(provider.VMType()) + dirs, err := env.GetMachineDirs(provider.VMType()) if err != nil { return err } diff --git a/cmd/podman/machine/start.go b/cmd/podman/machine/start.go index fb6c822b73..84ae5a3094 100644 --- a/cmd/podman/machine/start.go +++ b/cmd/podman/machine/start.go @@ -9,6 +9,7 @@ import ( "github.com/containers/podman/v5/libpod/events" "github.com/containers/podman/v5/pkg/machine" "github.com/containers/podman/v5/pkg/machine/define" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/podman/v5/pkg/machine/shim" "github.com/containers/podman/v5/pkg/machine/vmconfigs" "github.com/sirupsen/logrus" @@ -55,7 +56,7 @@ func start(_ *cobra.Command, args []string) error { vmName = args[0] } - dirs, err := machine.GetMachineDirs(provider.VMType()) + dirs, err := env.GetMachineDirs(provider.VMType()) if err != nil { return err } diff --git a/cmd/podman/machine/stop.go b/cmd/podman/machine/stop.go index 5a1877957a..ac9c4e5695 100644 --- a/cmd/podman/machine/stop.go +++ b/cmd/podman/machine/stop.go @@ -8,7 +8,7 @@ import ( "github.com/containers/podman/v5/cmd/podman/registry" "github.com/containers/podman/v5/libpod/events" - "github.com/containers/podman/v5/pkg/machine" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/podman/v5/pkg/machine/shim" "github.com/containers/podman/v5/pkg/machine/vmconfigs" "github.com/sirupsen/logrus" @@ -46,7 +46,7 @@ func stop(cmd *cobra.Command, args []string) error { vmName = args[0] } - dirs, err := machine.GetMachineDirs(provider.VMType()) + dirs, err := env.GetMachineDirs(provider.VMType()) if err != nil { return err } diff --git a/cmd/podman/system/reset_machine.go b/cmd/podman/system/reset_machine.go index ad249a1a91..35430dfb7d 100644 --- a/cmd/podman/system/reset_machine.go +++ b/cmd/podman/system/reset_machine.go @@ -3,9 +3,9 @@ package system import ( - "github.com/containers/podman/v5/pkg/machine" "github.com/containers/podman/v5/pkg/machine/connection" "github.com/containers/podman/v5/pkg/machine/define" + "github.com/containers/podman/v5/pkg/machine/env" p "github.com/containers/podman/v5/pkg/machine/provider" "github.com/containers/podman/v5/pkg/machine/shim" "github.com/containers/podman/v5/pkg/machine/vmconfigs" @@ -18,7 +18,7 @@ func resetMachine() error { if err != nil { return err } - dirs, err := machine.GetMachineDirs(provider.VMType()) + dirs, err := env.GetMachineDirs(provider.VMType()) if err != nil { return err } diff --git a/pkg/machine/config.go b/pkg/machine/config.go index 27834de7a9..ea43c5b6ec 100644 --- a/pkg/machine/config.go +++ b/pkg/machine/config.go @@ -9,14 +9,13 @@ import ( "net/http" "net/url" "os" - "path/filepath" "strings" "time" "github.com/containers/podman/v5/pkg/machine/compression" "github.com/containers/podman/v5/pkg/machine/define" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/podman/v5/pkg/machine/vmconfigs" - "github.com/containers/storage/pkg/homedir" "github.com/sirupsen/logrus" ) @@ -119,147 +118,6 @@ type InspectInfo struct { Rootful bool } -// GetCacheDir returns the dir where VM images are downloaded into when pulled -func GetCacheDir(vmType define.VMType) (string, error) { - dataDir, err := GetDataDir(vmType) - if err != nil { - return "", err - } - cacheDir := filepath.Join(dataDir, "cache") - if _, err := os.Stat(cacheDir); !errors.Is(err, os.ErrNotExist) { - return cacheDir, nil - } - return cacheDir, os.MkdirAll(cacheDir, 0755) -} - -// GetDataDir returns the filepath where vm images should -// live for podman-machine. -func GetDataDir(vmType define.VMType) (string, error) { - dataDirPrefix, err := DataDirPrefix() - if err != nil { - return "", err - } - dataDir := filepath.Join(dataDirPrefix, vmType.String()) - if _, err := os.Stat(dataDir); !errors.Is(err, os.ErrNotExist) { - return dataDir, nil - } - mkdirErr := os.MkdirAll(dataDir, 0755) - return dataDir, mkdirErr -} - -// GetGlobalDataDir returns the root of all backends -// for shared machine data. -func GetGlobalDataDir() (string, error) { - dataDir, err := DataDirPrefix() - if err != nil { - return "", err - } - - return dataDir, os.MkdirAll(dataDir, 0755) -} - -func GetMachineDirs(vmType define.VMType) (*define.MachineDirs, error) { - rtDir, err := getRuntimeDir() - if err != nil { - return nil, err - } - - rtDir = filepath.Join(rtDir, "podman") - configDir, err := GetConfDir(vmType) - if err != nil { - return nil, err - } - - configDirFile, err := define.NewMachineFile(configDir, nil) - if err != nil { - return nil, err - } - dataDir, err := GetDataDir(vmType) - if err != nil { - return nil, err - } - - dataDirFile, err := define.NewMachineFile(dataDir, nil) - if err != nil { - return nil, err - } - - imageCacheDir, err := dataDirFile.AppendToNewVMFile("cache", nil) - if err != nil { - return nil, err - } - - rtDirFile, err := define.NewMachineFile(rtDir, nil) - if err != nil { - return nil, err - } - - dirs := define.MachineDirs{ - ConfigDir: configDirFile, - DataDir: dataDirFile, - ImageCacheDir: imageCacheDir, - RuntimeDir: rtDirFile, - } - - // make sure all machine dirs are present - if err := os.MkdirAll(rtDir, 0755); err != nil { - return nil, err - } - if err := os.MkdirAll(configDir, 0755); err != nil { - return nil, err - } - - // Because this is a mkdirall, we make the image cache dir - // which is a subdir of datadir (so the datadir is made anyway) - err = os.MkdirAll(imageCacheDir.GetPath(), 0755) - - return &dirs, err -} - -// DataDirPrefix returns the path prefix for all machine data files -func DataDirPrefix() (string, error) { - data, err := homedir.GetDataHome() - if err != nil { - return "", err - } - dataDir := filepath.Join(data, "containers", "podman", "machine") - return dataDir, nil -} - -// GetConfigDir returns the filepath to where configuration -// files for podman-machine should live -func GetConfDir(vmType define.VMType) (string, error) { - confDirPrefix, err := ConfDirPrefix() - if err != nil { - return "", err - } - confDir := filepath.Join(confDirPrefix, vmType.String()) - if _, err := os.Stat(confDir); !errors.Is(err, os.ErrNotExist) { - return confDir, nil - } - mkdirErr := os.MkdirAll(confDir, 0755) - return confDir, mkdirErr -} - -// ConfDirPrefix returns the path prefix for all machine config files -func ConfDirPrefix() (string, error) { - conf, err := homedir.GetConfigHome() - if err != nil { - return "", err - } - confDir := filepath.Join(conf, "containers", "podman", "machine") - return confDir, nil -} - -// GetSSHIdentityPath returns the path to the expected SSH private key -func GetSSHIdentityPath(name string) (string, error) { - datadir, err := GetGlobalDataDir() - if err != nil { - return "", err - } - return filepath.Join(datadir, name), nil -} - // ImageConfig describes the bootable image for the VM type ImageConfig struct { // IgnitionFile is the path to the filesystem where the @@ -314,12 +172,12 @@ func (p *Virtualization) VMType() define.VMType { } func (p *Virtualization) NewDownload(vmName string) (Download, error) { - cacheDir, err := GetCacheDir(p.VMType()) + cacheDir, err := env.GetCacheDir(p.VMType()) if err != nil { return Download{}, err } - dataDir, err := GetDataDir(p.VMType()) + dataDir, err := env.GetDataDir(p.VMType()) if err != nil { return Download{}, err } diff --git a/pkg/machine/config_test.go b/pkg/machine/config_test.go index 3450a111f1..f5cb14c314 100644 --- a/pkg/machine/config_test.go +++ b/pkg/machine/config_test.go @@ -6,14 +6,15 @@ import ( "path/filepath" "testing" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/stretchr/testify/assert" ) func TestGetSSHIdentityPath(t *testing.T) { name := "p-test" - datadir, err := GetGlobalDataDir() + datadir, err := env.GetGlobalDataDir() assert.Nil(t, err) - identityPath, err := GetSSHIdentityPath(name) + identityPath, err := env.GetSSHIdentityPath(name) assert.Nil(t, err) assert.Equal(t, identityPath, filepath.Join(datadir, name)) } diff --git a/pkg/machine/env/dir.go b/pkg/machine/env/dir.go new file mode 100644 index 0000000000..5f0a664a64 --- /dev/null +++ b/pkg/machine/env/dir.go @@ -0,0 +1,151 @@ +package env + +import ( + "errors" + "os" + "path/filepath" + + "github.com/containers/podman/v5/pkg/machine/define" + "github.com/containers/storage/pkg/homedir" +) + +// GetCacheDir returns the dir where VM images are downloaded into when pulled +func GetCacheDir(vmType define.VMType) (string, error) { + dataDir, err := GetDataDir(vmType) + if err != nil { + return "", err + } + cacheDir := filepath.Join(dataDir, "cache") + if _, err := os.Stat(cacheDir); !errors.Is(err, os.ErrNotExist) { + return cacheDir, nil + } + return cacheDir, os.MkdirAll(cacheDir, 0755) +} + +// GetDataDir returns the filepath where vm images should +// live for podman-machine. +func GetDataDir(vmType define.VMType) (string, error) { + dataDirPrefix, err := DataDirPrefix() + if err != nil { + return "", err + } + dataDir := filepath.Join(dataDirPrefix, vmType.String()) + if _, err := os.Stat(dataDir); !errors.Is(err, os.ErrNotExist) { + return dataDir, nil + } + mkdirErr := os.MkdirAll(dataDir, 0755) + return dataDir, mkdirErr +} + +// GetGlobalDataDir returns the root of all backends +// for shared machine data. +func GetGlobalDataDir() (string, error) { + dataDir, err := DataDirPrefix() + if err != nil { + return "", err + } + + return dataDir, os.MkdirAll(dataDir, 0755) +} + +func GetMachineDirs(vmType define.VMType) (*define.MachineDirs, error) { + rtDir, err := getRuntimeDir() + if err != nil { + return nil, err + } + + rtDir = filepath.Join(rtDir, "podman") + configDir, err := GetConfDir(vmType) + if err != nil { + return nil, err + } + + configDirFile, err := define.NewMachineFile(configDir, nil) + if err != nil { + return nil, err + } + dataDir, err := GetDataDir(vmType) + if err != nil { + return nil, err + } + + dataDirFile, err := define.NewMachineFile(dataDir, nil) + if err != nil { + return nil, err + } + + imageCacheDir, err := dataDirFile.AppendToNewVMFile("cache", nil) + if err != nil { + return nil, err + } + + rtDirFile, err := define.NewMachineFile(rtDir, nil) + if err != nil { + return nil, err + } + + dirs := define.MachineDirs{ + ConfigDir: configDirFile, + DataDir: dataDirFile, + ImageCacheDir: imageCacheDir, + RuntimeDir: rtDirFile, + } + + // make sure all machine dirs are present + if err := os.MkdirAll(rtDir, 0755); err != nil { + return nil, err + } + if err := os.MkdirAll(configDir, 0755); err != nil { + return nil, err + } + + // Because this is a mkdirall, we make the image cache dir + // which is a subdir of datadir (so the datadir is made anyway) + err = os.MkdirAll(imageCacheDir.GetPath(), 0755) + + return &dirs, err +} + +// DataDirPrefix returns the path prefix for all machine data files +func DataDirPrefix() (string, error) { + data, err := homedir.GetDataHome() + if err != nil { + return "", err + } + dataDir := filepath.Join(data, "containers", "podman", "machine") + return dataDir, nil +} + +// GetConfigDir returns the filepath to where configuration +// files for podman-machine should live +func GetConfDir(vmType define.VMType) (string, error) { + confDirPrefix, err := ConfDirPrefix() + if err != nil { + return "", err + } + confDir := filepath.Join(confDirPrefix, vmType.String()) + if _, err := os.Stat(confDir); !errors.Is(err, os.ErrNotExist) { + return confDir, nil + } + mkdirErr := os.MkdirAll(confDir, 0755) + return confDir, mkdirErr +} + +// ConfDirPrefix returns the path prefix for all machine config files +func ConfDirPrefix() (string, error) { + conf, err := homedir.GetConfigHome() + if err != nil { + return "", err + } + confDir := filepath.Join(conf, "containers", "podman", "machine") + return confDir, nil +} + +// GetSSHIdentityPath returns the path to the expected SSH private key +func GetSSHIdentityPath(name string) (string, error) { + datadir, err := GetGlobalDataDir() + if err != nil { + return "", err + } + return filepath.Join(datadir, name), nil +} diff --git a/pkg/machine/options_darwin.go b/pkg/machine/env/dir_darwin.go similarity index 90% rename from pkg/machine/options_darwin.go rename to pkg/machine/env/dir_darwin.go index 3959175d2c..ed72aec8ab 100644 --- a/pkg/machine/options_darwin.go +++ b/pkg/machine/env/dir_darwin.go @@ -1,4 +1,4 @@ -package machine +package env import "os" diff --git a/pkg/machine/options_freebsd.go b/pkg/machine/env/dir_freebsd.go similarity index 90% rename from pkg/machine/options_freebsd.go rename to pkg/machine/env/dir_freebsd.go index 3959175d2c..ed72aec8ab 100644 --- a/pkg/machine/options_freebsd.go +++ b/pkg/machine/env/dir_freebsd.go @@ -1,4 +1,4 @@ -package machine +package env import "os" diff --git a/pkg/machine/options_linux.go b/pkg/machine/env/dir_linux.go similarity index 93% rename from pkg/machine/options_linux.go rename to pkg/machine/env/dir_linux.go index 5ba8280008..67f8142440 100644 --- a/pkg/machine/options_linux.go +++ b/pkg/machine/env/dir_linux.go @@ -1,4 +1,4 @@ -package machine +package env import ( "github.com/containers/podman/v5/pkg/rootless" diff --git a/pkg/machine/options_windows.go b/pkg/machine/env/dir_windows.go similarity index 91% rename from pkg/machine/options_windows.go rename to pkg/machine/env/dir_windows.go index 1a880069c6..9a272568ca 100644 --- a/pkg/machine/options_windows.go +++ b/pkg/machine/env/dir_windows.go @@ -1,4 +1,4 @@ -package machine +package env import "os" diff --git a/pkg/machine/hyperv/stubber.go b/pkg/machine/hyperv/stubber.go index d3070151e7..a8ae792086 100644 --- a/pkg/machine/hyperv/stubber.go +++ b/pkg/machine/hyperv/stubber.go @@ -10,6 +10,7 @@ import ( "os/exec" "path/filepath" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/podman/v5/pkg/machine/shim/diskpull" "github.com/Microsoft/go-winio" @@ -387,7 +388,7 @@ func (h HyperVStubber) PostStartNetworking(mc *vmconfigs.MachineConfig, noInfo b dirs *define.MachineDirs gvproxyPID int ) - dirs, err = machine.GetMachineDirs(h.VMType()) + dirs, err = env.GetMachineDirs(h.VMType()) if err != nil { return err } @@ -503,7 +504,7 @@ func removeIgnitionFromRegistry(vm *hypervctl.VirtualMachine) error { } func logCommandToFile(c *exec.Cmd, filename string) error { - dir, err := machine.GetDataDir(define.HyperVVirt) + dir, err := env.GetDataDir(define.HyperVVirt) if err != nil { return fmt.Errorf("obtain machine dir: %w", err) } diff --git a/pkg/machine/machine_windows.go b/pkg/machine/machine_windows.go index 3743b0db05..602f142b33 100644 --- a/pkg/machine/machine_windows.go +++ b/pkg/machine/machine_windows.go @@ -17,6 +17,7 @@ import ( winio "github.com/Microsoft/go-winio" "github.com/containers/podman/v5/pkg/machine/define" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/sirupsen/logrus" ) @@ -250,7 +251,7 @@ func FindExecutablePeer(name string) (string, error) { } func GetWinProxyStateDir(name string, vmtype define.VMType) (string, error) { - dir, err := GetDataDir(vmtype) + dir, err := env.GetDataDir(vmtype) if err != nil { return "", err } diff --git a/pkg/machine/os/machine_os.go b/pkg/machine/os/machine_os.go index b77164fb98..6ff55fa92b 100644 --- a/pkg/machine/os/machine_os.go +++ b/pkg/machine/os/machine_os.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/containers/podman/v5/pkg/machine" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/podman/v5/pkg/machine/shim" "github.com/containers/podman/v5/pkg/machine/vmconfigs" ) @@ -27,7 +28,7 @@ func (m *MachineOS) Apply(image string, opts ApplyOptions) error { return err } - dirs, err := machine.GetMachineDirs(m.Provider.VMType()) + dirs, err := env.GetMachineDirs(m.Provider.VMType()) if err != nil { return err } diff --git a/pkg/machine/ports.go b/pkg/machine/ports/ports.go similarity index 96% rename from pkg/machine/ports.go rename to pkg/machine/ports/ports.go index 2837d492f6..e9e9afa2b4 100644 --- a/pkg/machine/ports.go +++ b/pkg/machine/ports/ports.go @@ -1,4 +1,4 @@ -package machine +package ports import ( "context" @@ -11,6 +11,7 @@ import ( "path/filepath" "strconv" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/storage/pkg/ioutils" "github.com/containers/storage/pkg/lockfile" "github.com/sirupsen/logrus" @@ -135,7 +136,7 @@ func getRandomPortHold() (io.Closer, int, error) { } func acquirePortLock() (*lockfile.LockFile, error) { - lockDir, err := GetGlobalDataDir() + lockDir, err := env.GetGlobalDataDir() if err != nil { return nil, err } @@ -150,7 +151,7 @@ func acquirePortLock() (*lockfile.LockFile, error) { } func loadPortAllocations() (map[int]struct{}, error) { - portDir, err := GetGlobalDataDir() + portDir, err := env.GetGlobalDataDir() if err != nil { return nil, err } @@ -186,7 +187,7 @@ func loadPortAllocations() (map[int]struct{}, error) { } func storePortAllocations(ports map[int]struct{}) error { - portDir, err := GetGlobalDataDir() + portDir, err := env.GetGlobalDataDir() if err != nil { return err } diff --git a/pkg/machine/ports_unix.go b/pkg/machine/ports/ports_unix.go similarity index 98% rename from pkg/machine/ports_unix.go rename to pkg/machine/ports/ports_unix.go index 38cad8948a..20188d3d78 100644 --- a/pkg/machine/ports_unix.go +++ b/pkg/machine/ports/ports_unix.go @@ -1,6 +1,6 @@ //go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd -package machine +package ports import ( "net" diff --git a/pkg/machine/ports_windows.go b/pkg/machine/ports/ports_windows.go similarity index 98% rename from pkg/machine/ports_windows.go rename to pkg/machine/ports/ports_windows.go index 730d4be0b3..abfb4592e1 100644 --- a/pkg/machine/ports_windows.go +++ b/pkg/machine/ports/ports_windows.go @@ -1,4 +1,4 @@ -package machine +package ports import ( "net" diff --git a/pkg/machine/pull.go b/pkg/machine/pull.go index cba6466ee1..932c46a941 100644 --- a/pkg/machine/pull.go +++ b/pkg/machine/pull.go @@ -15,6 +15,7 @@ import ( "github.com/containers/podman/v5/pkg/machine/compression" "github.com/containers/podman/v5/pkg/machine/define" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/podman/v5/pkg/machine/ocipull" "github.com/containers/podman/v5/utils" "github.com/sirupsen/logrus" @@ -31,11 +32,11 @@ func NewGenericDownloader(vmType define.VMType, vmName, pullPath string) (Distri var ( imageName string ) - dataDir, err := GetDataDir(vmType) + dataDir, err := env.GetDataDir(vmType) if err != nil { return nil, err } - cacheDir, err := GetCacheDir(vmType) + cacheDir, err := env.GetCacheDir(vmType) if err != nil { return nil, err } diff --git a/pkg/machine/shim/host.go b/pkg/machine/shim/host.go index f69bd8cbeb..787607943a 100644 --- a/pkg/machine/shim/host.go +++ b/pkg/machine/shim/host.go @@ -13,6 +13,7 @@ import ( "github.com/containers/podman/v5/pkg/machine" "github.com/containers/podman/v5/pkg/machine/connection" machineDefine "github.com/containers/podman/v5/pkg/machine/define" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/podman/v5/pkg/machine/ignition" "github.com/containers/podman/v5/pkg/machine/proxyenv" "github.com/containers/podman/v5/pkg/machine/vmconfigs" @@ -29,7 +30,7 @@ func List(vmstubbers []vmconfigs.VMProvider, _ machine.ListOptions) ([]*machine. ) for _, s := range vmstubbers { - dirs, err := machine.GetMachineDirs(s.VMType()) + dirs, err := env.GetMachineDirs(s.VMType()) if err != nil { return nil, err } @@ -76,12 +77,12 @@ func Init(opts machineDefine.InitOptions, mp vmconfigs.VMProvider) (*vmconfigs.M defer callbackFuncs.CleanIfErr(&err) go callbackFuncs.CleanOnSignal() - dirs, err := machine.GetMachineDirs(mp.VMType()) + dirs, err := env.GetMachineDirs(mp.VMType()) if err != nil { return nil, err } - sshIdentityPath, err := machine.GetSSHIdentityPath(machineDefine.DefaultIdentityName) + sshIdentityPath, err := env.GetSSHIdentityPath(machineDefine.DefaultIdentityName) if err != nil { return nil, err } @@ -288,7 +289,7 @@ func CheckExclusiveActiveVM(provider vmconfigs.VMProvider, mc *vmconfigs.Machine func getMCsOverProviders(vmstubbers []vmconfigs.VMProvider) (map[string]*vmconfigs.MachineConfig, error) { mcs := make(map[string]*vmconfigs.MachineConfig) for _, stubber := range vmstubbers { - dirs, err := machine.GetMachineDirs(stubber.VMType()) + dirs, err := env.GetMachineDirs(stubber.VMType()) if err != nil { return nil, err } diff --git a/pkg/machine/shim/networking.go b/pkg/machine/shim/networking.go index 98c3bbe61d..ce06784c2e 100644 --- a/pkg/machine/shim/networking.go +++ b/pkg/machine/shim/networking.go @@ -12,6 +12,7 @@ import ( gvproxy "github.com/containers/gvisor-tap-vsock/pkg/types" "github.com/containers/podman/v5/pkg/machine" "github.com/containers/podman/v5/pkg/machine/define" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/podman/v5/pkg/machine/vmconfigs" "github.com/sirupsen/logrus" ) @@ -95,7 +96,7 @@ func startNetworking(mc *vmconfigs.MachineConfig, provider vmconfigs.VMProvider) return "", 0, provider.StartNetworking(mc, nil) } - dirs, err := machine.GetMachineDirs(provider.VMType()) + dirs, err := env.GetMachineDirs(provider.VMType()) if err != nil { return "", 0, err } diff --git a/pkg/machine/wsl/machine.go b/pkg/machine/wsl/machine.go index 26f16a66ec..bc79614e3a 100644 --- a/pkg/machine/wsl/machine.go +++ b/pkg/machine/wsl/machine.go @@ -17,6 +17,7 @@ import ( "github.com/containers/common/pkg/config" "github.com/containers/podman/v5/pkg/machine" "github.com/containers/podman/v5/pkg/machine/define" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/podman/v5/pkg/machine/ignition" "github.com/containers/podman/v5/pkg/machine/vmconfigs" "github.com/containers/podman/v5/pkg/machine/wsl/wutil" @@ -47,7 +48,7 @@ func getConfigPath(name string) (string, error) { //nolint:unused func getConfigPathExt(name string, extension string) (string, error) { - vmConfigDir, err := machine.GetConfDir(vmtype) + vmConfigDir, err := env.GetConfDir(vmtype) if err != nil { return "", err } @@ -66,7 +67,7 @@ func unprovisionWSL(mc *vmconfigs.MachineConfig) error { logrus.Error(err) } - vmDataDir, err := machine.GetDataDir(vmtype) + vmDataDir, err := env.GetDataDir(vmtype) if err != nil { return err } @@ -79,7 +80,7 @@ func unprovisionWSL(mc *vmconfigs.MachineConfig) error { // we should push this stuff be more common (dir names, etc) and also use // typed things where possible like vmfiles func provisionWSLDist(name string, imagePath string, prompt string) (string, error) { - vmDataDir, err := machine.GetDataDir(vmtype) + vmDataDir, err := env.GetDataDir(vmtype) if err != nil { return "", err } @@ -586,7 +587,7 @@ func setupWslProxyEnv() (hasProxy bool) { //nolint:unused func obtainGlobalConfigLock() (*fileLock, error) { - lockDir, err := machine.GetGlobalDataDir() + lockDir, err := env.GetGlobalDataDir() if err != nil { return nil, err } @@ -702,7 +703,7 @@ func isRunning(name string) (bool, error) { //nolint:unused func getDiskSize(name string) uint64 { - vmDataDir, err := machine.GetDataDir(vmtype) + vmDataDir, err := env.GetDataDir(vmtype) if err != nil { return 0 } diff --git a/pkg/machine/wsl/stubber.go b/pkg/machine/wsl/stubber.go index c28db07842..8e7c2e4e4c 100644 --- a/pkg/machine/wsl/stubber.go +++ b/pkg/machine/wsl/stubber.go @@ -11,6 +11,7 @@ import ( "strings" "github.com/containers/podman/v5/pkg/machine/ocipull" + "github.com/containers/podman/v5/pkg/machine/ports" "github.com/containers/podman/v5/pkg/machine/shim/diskpull" "github.com/containers/podman/v5/pkg/machine/stdpull" "github.com/containers/podman/v5/pkg/machine/wsl/wutil" @@ -110,7 +111,7 @@ func (w WSLStubber) Remove(mc *vmconfigs.MachineConfig) ([]string, func() error, if err := runCmdPassThrough(wutil.FindWSL(), "--unregister", machine.ToDist(mc.Name)); err != nil { logrus.Error(err) } - return machine.ReleaseMachinePort(mc.SSH.Port) + return ports.ReleaseMachinePort(mc.SSH.Port) } return []string{}, wslRemoveFunc, nil diff --git a/pkg/machine/wsl/usermodenet.go b/pkg/machine/wsl/usermodenet.go index 7d4b870cf8..b979941cb1 100644 --- a/pkg/machine/wsl/usermodenet.go +++ b/pkg/machine/wsl/usermodenet.go @@ -10,6 +10,7 @@ import ( "path/filepath" "github.com/containers/podman/v5/pkg/machine" + "github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/podman/v5/pkg/machine/vmconfigs" "github.com/containers/podman/v5/pkg/machine/wsl/wutil" "github.com/containers/podman/v5/pkg/specgen" @@ -226,7 +227,7 @@ func createUserModeResolvConf(dist string) error { } func getUserModeNetDir() (string, error) { - vmDataDir, err := machine.GetDataDir(vmtype) + vmDataDir, err := env.GetDataDir(vmtype) if err != nil { return "", err }