Skip to content

Commit

Permalink
Refactor env dir and port functions into new leaf pkgs
Browse files Browse the repository at this point in the history
[NO NEW TESTS NEEDED]

Signed-off-by: Jason T. Greene <[email protected]>
  • Loading branch information
n1hility committed Mar 5, 2024
1 parent e58b168 commit ef77272
Show file tree
Hide file tree
Showing 31 changed files with 219 additions and 193 deletions.
4 changes: 2 additions & 2 deletions cmd/podman/compose_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/machine/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/podman/machine/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/podman/machine/os/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/podman/machine/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/podman/machine/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/machine/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/podman/machine/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/podman/machine/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/machine/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/system/reset_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down
148 changes: 3 additions & 145 deletions pkg/machine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit ef77272

Please sign in to comment.