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

fix: 修改工作目錄路徑

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