Commit 2ee5c9f8 authored by Lei Li's avatar Lei Li
Browse files

fix: 修改工作目錄路徑

parent 7584cff0
......@@ -3,8 +3,8 @@ package config
import "time"
var (
WorkDir = "/linkfog/agent/"
RootDir = ""
WorkDir = ""
BackupDir = "backup"
PluginDir = "plugin"
LogDir = "log"
......
......@@ -33,6 +33,10 @@ type Agent struct {
func (a *Agent) Init(env svc.Environment) error {
option.Parse()
if option.Opt.PrintVersion {
fmt.Println(genVersion())
os.Exit(0)
}
// 获取平台和内核版本
global.GetPlatformInfo()
......@@ -48,7 +52,7 @@ func (a *Agent) Init(env svc.Environment) error {
if option.Opt.EnableMonitorDaemon {
config.LogFileName = config.DaemonLogFileName
} else {
a.cmdline0 = os.Args[0]
a.cmdline0 = filepath.Join(config.RootDir, filepath.Base(os.Args[0]))
}
// 日志设置
......@@ -67,11 +71,6 @@ func (a *Agent) Init(env svc.Environment) error {
}
func (a *Agent) Start() error {
if option.Opt.PrintVersion {
fmt.Println(genVersion())
os.Exit(0)
}
if option.Opt.EnableMonitorDaemon {
l.Info("======start monitor_daemon======")
param := fmt.Sprintf("-%s=false", option.ArgEnableMonitorDaemon)
......@@ -160,10 +159,6 @@ func initSvcCfg() error {
}
global.SubscribePrefixInfo = global.DeviceSerialNumber + "/publish/"
// NOTE: 自测使用
if config.Edition == "dev" {
config.WorkDir = option.Opt.WorkDir
}
// 时间修正
if !iTime.SynchronizeSystemTime() {
return fmt.Errorf("synchronization system time failed")
......
......@@ -4,7 +4,6 @@ import (
"agent/pkg/file"
"os"
"path/filepath"
"strings"
"syscall"
"time"
......@@ -51,22 +50,22 @@ func (a *Agent) chatMsgProcess(msg *global.Message) {
}
func (a *Agent) SelfUpgradeProcess() {
execFile := a.cmdline0[strings.LastIndex(a.cmdline0, "/")+1:]
execFile := filepath.Base(a.cmdline0)
// 备份
err := os.Rename(a.cmdline0, config.BackupDir+"/"+execFile)
if err != nil {
l.Error(err)
return
}
// 下载
err = global.DownloadFile(option.Opt.DownloadURL+"/"+execFile, config.WorkDir, execFile)
// 下载到xxx/agent
err = global.DownloadFile(option.Opt.DownloadURL+"/"+execFile, config.RootDir, execFile)
if err != nil {
l.Errorf("download file err:%v", err)
return
}
// 增加执行权限
err = file.SetFileExecPerm(filepath.Join(config.WorkDir, execFile))
err = file.SetFileExecPerm(filepath.Join(config.RootDir, execFile))
if err != nil {
l.Error(err)
}
......
package global
import (
"agent/cmd/agent/option"
"os"
"os/exec"
"os/user"
......@@ -71,14 +72,24 @@ func SetWorkDir() error {
if strings.Contains(runtime.GOARCH, "amd64") || strings.Contains(runtime.GOARCH, "arm64") ||
strings.Contains(runtime.GOARCH, "aarch64") {
config.RootDir = "/linkfog/agent/"
config.WorkDir = "/linkfog/agent/"
} else if strings.Contains(runtime.GOARCH, "android") || strings.Contains(runtime.GOARCH, "arm") {
} else {
// GOARCH == "android"|| "arm" || xxx
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err == nil {
config.WorkDir = filepath.Join(strings.Replace(dir, "\\", "/", -1), "lw_agent")
config.RootDir = strings.Replace(dir, "\\", "/", -1)
config.WorkDir = filepath.Join(config.RootDir, "lw_agent")
}
}
// NOTE: 自测使用
if config.WorkDir == "/linkfog/agent/" && config.Edition == "dev" {
config.RootDir = option.Opt.RootDir
config.WorkDir = filepath.Join(config.RootDir, "lw_agent")
}
err := os.MkdirAll(path.Join(config.WorkDir), 0777)
err := os.MkdirAll(config.WorkDir, 0777)
if err != nil {
return err
}
......
......@@ -44,7 +44,7 @@ type options struct {
MQTTBrokerURL string
MQTTBrokerPort int
DownloadURL string
WorkDir string
RootDir string
}
const (
......@@ -79,7 +79,7 @@ func flagSet() {
flag.StringVar(&Opt.MQTTBrokerURL, "mqtt-url", "tcp://broker.hivemq.com", "MQTT broker url")
flag.IntVar(&Opt.MQTTBrokerPort, "mqtt-port", 1883, "MQTT broker port")
flag.StringVar(&Opt.DownloadURL, "download-url", "http://fae-cdn.linkfog.cn/tmp", "download url")
flag.StringVar(&Opt.WorkDir, "work-dir", "", "work dir")
flag.StringVar(&Opt.RootDir, "root-dir", "", "root dir")
}
flag.BoolVar(&Opt.Usage, ArgUsage, false, "custom format usage")
flag.BoolVar(&Opt.PrintVersion, ArgVersion, false, "print version and exit")
......
......@@ -102,6 +102,7 @@ func (b *Backend) Start() error {
b.client.Publish(topic, 1, false, []byte(msg.Payload))
}
case <-b.signal:
b.client.Disconnect(250)
l.Info("MQTT exit")
return
}
......@@ -114,8 +115,6 @@ func (b *Backend) Stop() {
if b.IsRunning() {
b.isRunning = false
b.signal <- struct{}{}
b.client.Disconnect(250)
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment