2019-01-09 16:54:15 +00:00
|
|
|
package datadir
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2019-10-27 05:53:25 +00:00
|
|
|
"path/filepath"
|
2019-01-09 16:54:15 +00:00
|
|
|
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/version"
|
2019-01-09 16:54:15 +00:00
|
|
|
"github.com/pkg/errors"
|
2024-05-06 16:42:27 +00:00
|
|
|
"github.com/rancher/wrangler/v3/pkg/resolvehome"
|
2019-01-09 16:54:15 +00:00
|
|
|
)
|
|
|
|
|
2020-05-05 22:09:04 +00:00
|
|
|
var (
|
|
|
|
DefaultDataDir = "/var/lib/rancher/" + version.Program
|
|
|
|
DefaultHomeDataDir = "${HOME}/.rancher/" + version.Program
|
|
|
|
HomeConfig = "${HOME}/.kube/" + version.Program + ".yaml"
|
|
|
|
GlobalConfig = "/etc/rancher/" + version.Program + "/" + version.Program + ".yaml"
|
2019-01-26 04:53:53 +00:00
|
|
|
)
|
|
|
|
|
2019-01-09 16:54:15 +00:00
|
|
|
func Resolve(dataDir string) (string, error) {
|
2019-03-08 22:47:44 +00:00
|
|
|
return LocalHome(dataDir, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LocalHome(dataDir string, forceLocal bool) (string, error) {
|
2019-01-09 16:54:15 +00:00
|
|
|
if dataDir == "" {
|
2019-03-08 22:47:44 +00:00
|
|
|
if os.Getuid() == 0 && !forceLocal {
|
2019-01-26 04:53:53 +00:00
|
|
|
dataDir = DefaultDataDir
|
2019-01-09 16:54:15 +00:00
|
|
|
} else {
|
2019-01-26 04:53:53 +00:00
|
|
|
dataDir = DefaultHomeDataDir
|
2019-01-09 16:54:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dataDir, err := resolvehome.Resolve(dataDir)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrapf(err, "resolving %s", dataDir)
|
|
|
|
}
|
|
|
|
|
2019-10-27 05:53:25 +00:00
|
|
|
return filepath.Abs(dataDir)
|
2019-01-09 16:54:15 +00:00
|
|
|
}
|