hotfix: fix init dir

This commit is contained in:
naison
2025-05-07 08:08:26 +00:00
parent e6df115933
commit e33d2f1928
8 changed files with 60 additions and 60 deletions

View File

@@ -119,7 +119,7 @@ func CmdAlias(f cmdutil.Factory) *cobra.Command {
return nil return nil
}, },
} }
cmd.Flags().StringVarP(&localFile, "kubevpnconfig", "f", util.If(os.Getenv("KUBEVPNCONFIG") != "", os.Getenv("KUBEVPNCONFIG"), config.GetConfigFilePath()), "Path to the kubevpnconfig file to use for CLI requests.") cmd.Flags().StringVarP(&localFile, "kubevpnconfig", "f", util.If(os.Getenv("KUBEVPNCONFIG") != "", os.Getenv("KUBEVPNCONFIG"), config.GetConfigFile()), "Path to the kubevpnconfig file to use for CLI requests.")
cmd.Flags().StringVarP(&remoteAddr, "remote", "r", "", "Remote config file, eg: https://raw.githubusercontent.com/kubenetworks/kubevpn/master/pkg/config/config.yaml") cmd.Flags().StringVarP(&remoteAddr, "remote", "r", "", "Remote config file, eg: https://raw.githubusercontent.com/kubenetworks/kubevpn/master/pkg/config/config.yaml")
return cmd return cmd
} }
@@ -135,7 +135,7 @@ func ParseAndGet(localFile, remoteAddr string, aliasName string) ([]Config, erro
path = remoteAddr path = remoteAddr
content, err = util.DownloadFileStream(path) content, err = util.DownloadFileStream(path)
} else { } else {
path = config.GetConfigFilePath() path = config.GetConfigFile()
content, err = os.ReadFile(path) content, err = os.ReadFile(path)
} }
if err != nil { if err != nil {

View File

@@ -16,7 +16,6 @@ import (
"github.com/wencaiwulue/kubevpn/v2/pkg/config" "github.com/wencaiwulue/kubevpn/v2/pkg/config"
"github.com/wencaiwulue/kubevpn/v2/pkg/daemon" "github.com/wencaiwulue/kubevpn/v2/pkg/daemon"
"github.com/wencaiwulue/kubevpn/v2/pkg/daemon/action"
"github.com/wencaiwulue/kubevpn/v2/pkg/dns" "github.com/wencaiwulue/kubevpn/v2/pkg/dns"
"github.com/wencaiwulue/kubevpn/v2/pkg/util" "github.com/wencaiwulue/kubevpn/v2/pkg/util"
) )
@@ -42,7 +41,7 @@ func CmdDaemon(cmdutil.Factory) *cobra.Command {
} else { } else {
go util.StartupPProf(config.PProfPort) go util.StartupPProf(config.PProfPort)
} }
return initLogfile(action.GetDaemonLogPath(opt.IsSudo)) return initLogfile(config.GetDaemonLogPath(opt.IsSudo))
}, },
RunE: func(cmd *cobra.Command, args []string) (err error) { RunE: func(cmd *cobra.Command, args []string) (err error) {
defer opt.Stop() defer opt.Stop()
@@ -53,7 +52,7 @@ func CmdDaemon(cmdutil.Factory) *cobra.Command {
if opt.IsSudo { if opt.IsSudo {
for _, profile := range pprof.Profiles() { for _, profile := range pprof.Profiles() {
func() { func() {
file, e := os.Create(filepath.Join(config.PprofPath, profile.Name())) file, e := os.Create(filepath.Join(config.GetPProfPath(), profile.Name()))
if e != nil { if e != nil {
return return
} }

View File

@@ -102,7 +102,7 @@ func CmdStatus(f cmdutil.Factory) *cobra.Command {
}, },
} }
cmd.Flags().StringVar(&aliasName, "alias", "", "Alias name, query connect status by alias config name") cmd.Flags().StringVar(&aliasName, "alias", "", "Alias name, query connect status by alias config name")
cmd.Flags().StringVarP(&localFile, "kubevpnconfig", "f", util.If(os.Getenv("KUBEVPNCONFIG") != "", os.Getenv("KUBEVPNCONFIG"), config.GetConfigFilePath()), "Path to the kubevpnconfig file to use for CLI requests.") cmd.Flags().StringVarP(&localFile, "kubevpnconfig", "f", util.If(os.Getenv("KUBEVPNCONFIG") != "", os.Getenv("KUBEVPNCONFIG"), config.GetConfigFile()), "Path to the kubevpnconfig file to use for CLI requests.")
cmd.Flags().StringVarP(&remoteAddr, "remote", "r", "", "Remote config file, eg: https://raw.githubusercontent.com/kubenetworks/kubevpn/master/pkg/config/config.yaml") cmd.Flags().StringVarP(&remoteAddr, "remote", "r", "", "Remote config file, eg: https://raw.githubusercontent.com/kubenetworks/kubevpn/master/pkg/config/config.yaml")
cmd.Flags().StringVarP(&format, "output", "o", FormatTable, fmt.Sprintf("Output format. One of: (%s, %s, %s)", FormatJson, FormatYaml, FormatTable)) cmd.Flags().StringVarP(&format, "output", "o", FormatTable, fmt.Sprintf("Output format. One of: (%s, %s, %s)", FormatJson, FormatYaml, FormatTable))
return cmd return cmd

View File

@@ -2,8 +2,6 @@ package config
import ( import (
"net" "net"
"os"
"path/filepath"
"sync" "sync"
"time" "time"
@@ -111,10 +109,6 @@ var (
GitHubOAuthToken = "" GitHubOAuthToken = ""
OriginImage = "ghcr.io/kubenetworks/kubevpn:" + Version OriginImage = "ghcr.io/kubenetworks/kubevpn:" + Version
DaemonPath string
HomePath string
PprofPath string
) )
var ( var (
@@ -142,13 +136,6 @@ func init() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
dir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
DaemonPath = filepath.Join(dir, HOME, Daemon)
HomePath = filepath.Join(dir, HOME)
PprofPath = filepath.Join(dir, HOME, Daemon, PProfDir)
} }
var Debug bool var Debug bool

View File

@@ -6,19 +6,17 @@ import (
"path/filepath" "path/filepath"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/wencaiwulue/kubevpn/v2/pkg/daemon/elevate"
) )
const ( const (
HOME = ".kubevpn" HOME = ".kubevpn"
Daemon = "daemon" Daemon = "daemon"
SockPath = "daemon.sock" SockPath = "user_daemon.sock"
SudoSockPath = "sudo_daemon.sock" SudoSockPath = "root_daemon.sock"
PidPath = "daemon.pid" PidPath = "user_daemon.pid"
SudoPidPath = "sudo_daemon.pid" SudoPidPath = "root_daemon.pid"
UserLogFile = "user_daemon.log" UserLogFile = "user_daemon.log"
SudoLogFile = "root_daemon.log" SudoLogFile = "root_daemon.log"
@@ -28,28 +26,41 @@ const (
TmpDir = "tmp" TmpDir = "tmp"
) )
//go:embed config.yaml var (
var config []byte daemonPath string
homePath string
//go:embed config.yaml
config []byte
)
func init() { func init() {
if elevate.IsAdmin() { dir, err := os.UserHomeDir()
return if err != nil {
panic(err)
} }
homePath = filepath.Join(dir, HOME)
daemonPath = filepath.Join(dir, HOME, Daemon)
var paths = []string{DaemonPath, PprofPath, GetSyncthingPath(), GetTempPath()} var paths = []string{homePath, daemonPath, GetPProfPath(), GetSyncthingPath(), GetTempPath()}
for _, path := range paths { for _, path := range paths {
err := os.MkdirAll(path, 0755) _, err = os.Stat(path)
if err != nil { if errors.Is(err, os.ErrNotExist) {
panic(err) err = os.MkdirAll(path, 0755)
} if err != nil {
err = os.Chmod(path, 0755) panic(err)
if err != nil { }
err = os.Chmod(path, 0755)
if err != nil {
panic(err)
}
} else if err != nil {
panic(err) panic(err)
} }
} }
path := filepath.Join(HomePath, ConfigFile) path := filepath.Join(homePath, ConfigFile)
_, err := os.Stat(path) _, err = os.Stat(path)
if errors.Is(err, os.ErrNotExist) { if errors.Is(err, os.ErrNotExist) {
err = os.WriteFile(path, config, 0644) err = os.WriteFile(path, config, 0644)
} }
@@ -63,7 +74,7 @@ func GetSockPath(isSudo bool) string {
if isSudo { if isSudo {
name = SudoSockPath name = SudoSockPath
} }
return filepath.Join(DaemonPath, name) return filepath.Join(daemonPath, name)
} }
func GetPidPath(isSudo bool) string { func GetPidPath(isSudo bool) string {
@@ -71,17 +82,28 @@ func GetPidPath(isSudo bool) string {
if isSudo { if isSudo {
name = SudoPidPath name = SudoPidPath
} }
return filepath.Join(DaemonPath, name) return filepath.Join(daemonPath, name)
} }
func GetSyncthingPath() string { func GetSyncthingPath() string {
return filepath.Join(DaemonPath, SyncthingDir) return filepath.Join(daemonPath, SyncthingDir)
} }
func GetConfigFilePath() string { func GetConfigFile() string {
return filepath.Join(HomePath, ConfigFile) return filepath.Join(homePath, ConfigFile)
} }
func GetTempPath() string { func GetTempPath() string {
return filepath.Join(HomePath, TmpDir) return filepath.Join(homePath, TmpDir)
}
func GetDaemonLogPath(isSudo bool) string {
if isSudo {
return filepath.Join(daemonPath, SudoLogFile)
}
return filepath.Join(daemonPath, UserLogFile)
}
func GetPProfPath() string {
return filepath.Join(daemonPath, PProfDir)
} }

View File

@@ -7,17 +7,18 @@ import (
"github.com/hpcloud/tail" "github.com/hpcloud/tail"
"github.com/wencaiwulue/kubevpn/v2/pkg/config"
"github.com/wencaiwulue/kubevpn/v2/pkg/daemon/rpc" "github.com/wencaiwulue/kubevpn/v2/pkg/daemon/rpc"
) )
func (svr *Server) Logs(req *rpc.LogRequest, resp rpc.Daemon_LogsServer) error { func (svr *Server) Logs(req *rpc.LogRequest, resp rpc.Daemon_LogsServer) error {
// only show latest N lines // only show latest N lines
line := int64(max(req.Lines, -req.Lines)) line := int64(max(req.Lines, -req.Lines))
sudoLine, sudoSize, err := seekToLastLine(GetDaemonLogPath(true), line) sudoLine, sudoSize, err := seekToLastLine(config.GetDaemonLogPath(true), line)
if err != nil { if err != nil {
return err return err
} }
userLine, userSize, err := seekToLastLine(GetDaemonLogPath(false), line) userLine, userSize, err := seekToLastLine(config.GetDaemonLogPath(false), line)
if err != nil { if err != nil {
return err return err
} }
@@ -51,12 +52,12 @@ func tee(resp rpc.Daemon_LogsServer, sudoLine int64, userLine int64) error {
Logger: log.New(io.Discard, "", log.LstdFlags), Logger: log.New(io.Discard, "", log.LstdFlags),
Location: &tail.SeekInfo{Offset: userLine, Whence: io.SeekStart}, Location: &tail.SeekInfo{Offset: userLine, Whence: io.SeekStart},
} }
sudoFile, err := tail.TailFile(GetDaemonLogPath(true), sudoConfig) sudoFile, err := tail.TailFile(config.GetDaemonLogPath(true), sudoConfig)
if err != nil { if err != nil {
return err return err
} }
defer sudoFile.Stop() defer sudoFile.Stop()
userFile, err := tail.TailFile(GetDaemonLogPath(false), userConfig) userFile, err := tail.TailFile(config.GetDaemonLogPath(false), userConfig)
if err != nil { if err != nil {
return err return err
} }
@@ -108,12 +109,12 @@ func recent(resp rpc.Daemon_LogsServer, sudoLine int64, userLine int64) error {
Logger: log.New(io.Discard, "", log.LstdFlags), Logger: log.New(io.Discard, "", log.LstdFlags),
Location: &tail.SeekInfo{Offset: userLine, Whence: io.SeekStart}, Location: &tail.SeekInfo{Offset: userLine, Whence: io.SeekStart},
} }
sudoFile, err := tail.TailFile(GetDaemonLogPath(true), sudoConfig) sudoFile, err := tail.TailFile(config.GetDaemonLogPath(true), sudoConfig)
if err != nil { if err != nil {
return err return err
} }
defer sudoFile.Stop() defer sudoFile.Stop()
userFile, err := tail.TailFile(GetDaemonLogPath(false), userConfig) userFile, err := tail.TailFile(config.GetDaemonLogPath(false), userConfig)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -1,7 +1,6 @@
package action package action
import ( import (
"path/filepath"
"sync" "sync"
"time" "time"
@@ -9,7 +8,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/metadata/metadatainformer" "k8s.io/client-go/metadata/metadatainformer"
"github.com/wencaiwulue/kubevpn/v2/pkg/config"
"github.com/wencaiwulue/kubevpn/v2/pkg/daemon/rpc" "github.com/wencaiwulue/kubevpn/v2/pkg/daemon/rpc"
"github.com/wencaiwulue/kubevpn/v2/pkg/handler" "github.com/wencaiwulue/kubevpn/v2/pkg/handler"
) )
@@ -33,10 +31,3 @@ type Server struct {
ID string ID string
} }
func GetDaemonLogPath(isSudo bool) string {
if isSudo {
return filepath.Join(config.DaemonPath, config.SudoLogFile)
}
return filepath.Join(config.DaemonPath, config.UserLogFile)
}

View File

@@ -44,7 +44,7 @@ type SvrOption struct {
func (o *SvrOption) Start(ctx context.Context) error { func (o *SvrOption) Start(ctx context.Context) error {
l := &lumberjack.Logger{ l := &lumberjack.Logger{
Filename: action.GetDaemonLogPath(o.IsSudo), Filename: config.GetDaemonLogPath(o.IsSudo),
MaxSize: 100, MaxSize: 100,
MaxAge: 3, MaxAge: 3,
MaxBackups: 3, MaxBackups: 3,