mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-09-26 19:31:17 +08:00
hotfix: fix init dir
This commit is contained in:
@@ -119,7 +119,7 @@ func CmdAlias(f cmdutil.Factory) *cobra.Command {
|
||||
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")
|
||||
return cmd
|
||||
}
|
||||
@@ -135,7 +135,7 @@ func ParseAndGet(localFile, remoteAddr string, aliasName string) ([]Config, erro
|
||||
path = remoteAddr
|
||||
content, err = util.DownloadFileStream(path)
|
||||
} else {
|
||||
path = config.GetConfigFilePath()
|
||||
path = config.GetConfigFile()
|
||||
content, err = os.ReadFile(path)
|
||||
}
|
||||
if err != nil {
|
||||
|
@@ -16,7 +16,6 @@ import (
|
||||
|
||||
"github.com/wencaiwulue/kubevpn/v2/pkg/config"
|
||||
"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/util"
|
||||
)
|
||||
@@ -42,7 +41,7 @@ func CmdDaemon(cmdutil.Factory) *cobra.Command {
|
||||
} else {
|
||||
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) {
|
||||
defer opt.Stop()
|
||||
@@ -53,7 +52,7 @@ func CmdDaemon(cmdutil.Factory) *cobra.Command {
|
||||
if opt.IsSudo {
|
||||
for _, profile := range pprof.Profiles() {
|
||||
func() {
|
||||
file, e := os.Create(filepath.Join(config.PprofPath, profile.Name()))
|
||||
file, e := os.Create(filepath.Join(config.GetPProfPath(), profile.Name()))
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
|
@@ -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().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(&format, "output", "o", FormatTable, fmt.Sprintf("Output format. One of: (%s, %s, %s)", FormatJson, FormatYaml, FormatTable))
|
||||
return cmd
|
||||
|
@@ -2,8 +2,6 @@ package config
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -111,10 +109,6 @@ var (
|
||||
GitHubOAuthToken = ""
|
||||
|
||||
OriginImage = "ghcr.io/kubenetworks/kubevpn:" + Version
|
||||
|
||||
DaemonPath string
|
||||
HomePath string
|
||||
PprofPath string
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -142,13 +136,6 @@ func init() {
|
||||
if err != nil {
|
||||
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
|
||||
|
@@ -6,19 +6,17 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/wencaiwulue/kubevpn/v2/pkg/daemon/elevate"
|
||||
)
|
||||
|
||||
const (
|
||||
HOME = ".kubevpn"
|
||||
Daemon = "daemon"
|
||||
|
||||
SockPath = "daemon.sock"
|
||||
SudoSockPath = "sudo_daemon.sock"
|
||||
SockPath = "user_daemon.sock"
|
||||
SudoSockPath = "root_daemon.sock"
|
||||
|
||||
PidPath = "daemon.pid"
|
||||
SudoPidPath = "sudo_daemon.pid"
|
||||
PidPath = "user_daemon.pid"
|
||||
SudoPidPath = "root_daemon.pid"
|
||||
|
||||
UserLogFile = "user_daemon.log"
|
||||
SudoLogFile = "root_daemon.log"
|
||||
@@ -28,28 +26,41 @@ const (
|
||||
TmpDir = "tmp"
|
||||
)
|
||||
|
||||
//go:embed config.yaml
|
||||
var config []byte
|
||||
var (
|
||||
daemonPath string
|
||||
homePath string
|
||||
|
||||
//go:embed config.yaml
|
||||
config []byte
|
||||
)
|
||||
|
||||
func init() {
|
||||
if elevate.IsAdmin() {
|
||||
return
|
||||
dir, err := os.UserHomeDir()
|
||||
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 {
|
||||
err := os.MkdirAll(path, 0755)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = os.Chmod(path, 0755)
|
||||
if err != nil {
|
||||
_, err = os.Stat(path)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
err = os.MkdirAll(path, 0755)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = os.Chmod(path, 0755)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
path := filepath.Join(HomePath, ConfigFile)
|
||||
_, err := os.Stat(path)
|
||||
path := filepath.Join(homePath, ConfigFile)
|
||||
_, err = os.Stat(path)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
err = os.WriteFile(path, config, 0644)
|
||||
}
|
||||
@@ -63,7 +74,7 @@ func GetSockPath(isSudo bool) string {
|
||||
if isSudo {
|
||||
name = SudoSockPath
|
||||
}
|
||||
return filepath.Join(DaemonPath, name)
|
||||
return filepath.Join(daemonPath, name)
|
||||
}
|
||||
|
||||
func GetPidPath(isSudo bool) string {
|
||||
@@ -71,17 +82,28 @@ func GetPidPath(isSudo bool) string {
|
||||
if isSudo {
|
||||
name = SudoPidPath
|
||||
}
|
||||
return filepath.Join(DaemonPath, name)
|
||||
return filepath.Join(daemonPath, name)
|
||||
}
|
||||
|
||||
func GetSyncthingPath() string {
|
||||
return filepath.Join(DaemonPath, SyncthingDir)
|
||||
return filepath.Join(daemonPath, SyncthingDir)
|
||||
}
|
||||
|
||||
func GetConfigFilePath() string {
|
||||
return filepath.Join(HomePath, ConfigFile)
|
||||
func GetConfigFile() string {
|
||||
return filepath.Join(homePath, ConfigFile)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
@@ -7,17 +7,18 @@ import (
|
||||
|
||||
"github.com/hpcloud/tail"
|
||||
|
||||
"github.com/wencaiwulue/kubevpn/v2/pkg/config"
|
||||
"github.com/wencaiwulue/kubevpn/v2/pkg/daemon/rpc"
|
||||
)
|
||||
|
||||
func (svr *Server) Logs(req *rpc.LogRequest, resp rpc.Daemon_LogsServer) error {
|
||||
// only show latest N 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 {
|
||||
return err
|
||||
}
|
||||
userLine, userSize, err := seekToLastLine(GetDaemonLogPath(false), line)
|
||||
userLine, userSize, err := seekToLastLine(config.GetDaemonLogPath(false), line)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -51,12 +52,12 @@ func tee(resp rpc.Daemon_LogsServer, sudoLine int64, userLine int64) error {
|
||||
Logger: log.New(io.Discard, "", log.LstdFlags),
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
defer sudoFile.Stop()
|
||||
userFile, err := tail.TailFile(GetDaemonLogPath(false), userConfig)
|
||||
userFile, err := tail.TailFile(config.GetDaemonLogPath(false), userConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -108,12 +109,12 @@ func recent(resp rpc.Daemon_LogsServer, sudoLine int64, userLine int64) error {
|
||||
Logger: log.New(io.Discard, "", log.LstdFlags),
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
defer sudoFile.Stop()
|
||||
userFile, err := tail.TailFile(GetDaemonLogPath(false), userConfig)
|
||||
userFile, err := tail.TailFile(config.GetDaemonLogPath(false), userConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package action
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -9,7 +8,6 @@ import (
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"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/handler"
|
||||
)
|
||||
@@ -33,10 +31,3 @@ type Server struct {
|
||||
|
||||
ID string
|
||||
}
|
||||
|
||||
func GetDaemonLogPath(isSudo bool) string {
|
||||
if isSudo {
|
||||
return filepath.Join(config.DaemonPath, config.SudoLogFile)
|
||||
}
|
||||
return filepath.Join(config.DaemonPath, config.UserLogFile)
|
||||
}
|
||||
|
@@ -44,7 +44,7 @@ type SvrOption struct {
|
||||
|
||||
func (o *SvrOption) Start(ctx context.Context) error {
|
||||
l := &lumberjack.Logger{
|
||||
Filename: action.GetDaemonLogPath(o.IsSudo),
|
||||
Filename: config.GetDaemonLogPath(o.IsSudo),
|
||||
MaxSize: 100,
|
||||
MaxAge: 3,
|
||||
MaxBackups: 3,
|
||||
|
Reference in New Issue
Block a user