Pull request: home: imp logs

Closes #3869.

Squashed commit of the following:

commit 8ee0625ea2ac1f6615ad56c819fbb0c4974767e5
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Nov 23 20:33:14 2021 +0300

    home: imp logs
This commit is contained in:
Ainar Garipov 2021-11-23 20:45:14 +03:00
parent 51f11d2f8e
commit 322944073e
1 changed files with 26 additions and 27 deletions

View File

@ -97,7 +97,7 @@ func svcAction(s service.Service, action string) (err error) {
// exist, find our PID using 'ps' command. // exist, find our PID using 'ps' command.
func sendSigReload() { func sendSigReload() {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
log.Error("not implemented on windows") log.Error("service: not implemented on windows")
return return
} }
@ -107,25 +107,24 @@ func sendSigReload() {
data, err := os.ReadFile(pidfile) data, err := os.ReadFile(pidfile)
if errors.Is(err, os.ErrNotExist) { if errors.Is(err, os.ErrNotExist) {
if pid, err = aghos.PIDByCommand(serviceName, os.Getpid()); err != nil { if pid, err = aghos.PIDByCommand(serviceName, os.Getpid()); err != nil {
log.Error("finding AdGuardHome process: %s", err) log.Error("service: finding AdGuardHome process: %s", err)
return return
} }
} else if err != nil { } else if err != nil {
log.Error("reading pid file %s: %s", pidfile, err) log.Error("service: reading pid file %s: %s", pidfile, err)
return return
} else { } else {
parts := strings.SplitN(string(data), "\n", 2) parts := strings.SplitN(string(data), "\n", 2)
if len(parts) == 0 { if len(parts) == 0 {
log.Error("can't read pid file %s: bad value", pidfile) log.Error("service: parsing pid file %s: bad value", pidfile)
return return
} }
if pid, err = strconv.Atoi(strings.TrimSpace(parts[0])); err != nil { if pid, err = strconv.Atoi(strings.TrimSpace(parts[0])); err != nil {
log.Error("can't read pid file %s: %s", pidfile, err) log.Error("service: parsing pid from file %s: %s", pidfile, err)
return return
} }
@ -133,18 +132,18 @@ func sendSigReload() {
var proc *os.Process var proc *os.Process
if proc, err = os.FindProcess(pid); err != nil { if proc, err = os.FindProcess(pid); err != nil {
log.Error("can't send signal to pid %d: %s", pid, err) log.Error("service: finding process for pid %d: %s", pid, err)
return return
} }
if err = proc.Signal(syscall.SIGHUP); err != nil { if err = proc.Signal(syscall.SIGHUP); err != nil {
log.Error("Can't send signal to pid %d: %s", pid, err) log.Error("service: sending signal HUP to pid %d: %s", pid, err)
return return
} }
log.Debug("sent signal to PID %d", pid) log.Debug("service: sent signal to pid %d", pid)
} }
// handleServiceControlAction one of the possible control actions: // handleServiceControlAction one of the possible control actions:
@ -163,7 +162,7 @@ func handleServiceControlAction(opts options, clientBuildFS fs.FS) {
chooseSystem() chooseSystem()
action := opts.serviceControlAction action := opts.serviceControlAction
log.Printf("Service control action: %s", action) log.Printf("service: control action: %s", action)
if action == "reload" { if action == "reload" {
sendSigReload() sendSigReload()
@ -173,7 +172,7 @@ func handleServiceControlAction(opts options, clientBuildFS fs.FS) {
pwd, err := os.Getwd() pwd, err := os.Getwd()
if err != nil { if err != nil {
log.Fatal("Unable to find the path to the current directory") log.Fatalf("service: getting current directory: %s", err)
} }
runOpts := opts runOpts := opts
@ -193,7 +192,7 @@ func handleServiceControlAction(opts options, clientBuildFS fs.FS) {
} }
var s service.Service var s service.Service
if s, err = service.New(prg, svcConfig); err != nil { if s, err = service.New(prg, svcConfig); err != nil {
log.Fatal(err) log.Fatalf("service: initializing service: %s", err)
} }
switch action { switch action {
@ -201,7 +200,7 @@ func handleServiceControlAction(opts options, clientBuildFS fs.FS) {
handleServiceStatusCommand(s) handleServiceStatusCommand(s)
case "run": case "run":
if err = s.Run(); err != nil { if err = s.Run(); err != nil {
log.Fatalf("Failed to run service: %s", err) log.Fatalf("service: failed to run service: %s", err)
} }
case "install": case "install":
initConfigFilename(opts) initConfigFilename(opts)
@ -211,27 +210,27 @@ func handleServiceControlAction(opts options, clientBuildFS fs.FS) {
handleServiceUninstallCommand(s) handleServiceUninstallCommand(s)
default: default:
if err = svcAction(s, action); err != nil { if err = svcAction(s, action); err != nil {
log.Fatal(err) log.Fatalf("service: executing action %q: %s", action, err)
} }
} }
log.Printf("action %s has been done successfully on %s", action, service.ChosenSystem()) log.Printf("service: action %s has been done successfully on %s", action, service.ChosenSystem())
} }
// handleServiceStatusCommand handles service "status" command. // handleServiceStatusCommand handles service "status" command.
func handleServiceStatusCommand(s service.Service) { func handleServiceStatusCommand(s service.Service) {
status, errSt := svcStatus(s) status, errSt := svcStatus(s)
if errSt != nil { if errSt != nil {
log.Fatalf("failed to get service status: %s", errSt) log.Fatalf("service: failed to get service status: %s", errSt)
} }
switch status { switch status {
case service.StatusUnknown: case service.StatusUnknown:
log.Printf("Service status is unknown") log.Printf("service: status is unknown")
case service.StatusStopped: case service.StatusStopped:
log.Printf("Service is stopped") log.Printf("service: stopped")
case service.StatusRunning: case service.StatusRunning:
log.Printf("Service is running") log.Printf("service: running")
} }
} }
@ -239,7 +238,7 @@ func handleServiceStatusCommand(s service.Service) {
func handleServiceInstallCommand(s service.Service) { func handleServiceInstallCommand(s service.Service) {
err := svcAction(s, "install") err := svcAction(s, "install")
if err != nil { if err != nil {
log.Fatal(err) log.Fatalf("service: executing action %q: %s", "install", err)
} }
if aghos.IsOpenWrt() { if aghos.IsOpenWrt() {
@ -248,16 +247,16 @@ func handleServiceInstallCommand(s service.Service) {
// startup. // startup.
_, err = runInitdCommand("enable") _, err = runInitdCommand("enable")
if err != nil { if err != nil {
log.Fatal(err) log.Fatalf("service: running init enable: %s", err)
} }
} }
// Start automatically after install. // Start automatically after install.
err = svcAction(s, "start") err = svcAction(s, "start")
if err != nil { if err != nil {
log.Fatalf("Failed to start the service: %s", err) log.Fatalf("service: starting: %s", err)
} }
log.Printf("Service has been started") log.Printf("service: started")
if detectFirstRun() { if detectFirstRun() {
log.Printf(`Almost ready! log.Printf(`Almost ready!
@ -276,24 +275,24 @@ func handleServiceUninstallCommand(s service.Service) {
// as it will remove the symlink // as it will remove the symlink
_, err := runInitdCommand("disable") _, err := runInitdCommand("disable")
if err != nil { if err != nil {
log.Fatal(err) log.Fatalf("service: running init disable: %s", err)
} }
} }
if err := svcAction(s, "uninstall"); err != nil { if err := svcAction(s, "uninstall"); err != nil {
log.Fatal(err) log.Fatalf("service: executing action %q: %s", "uninstall", err)
} }
if runtime.GOOS == "darwin" { if runtime.GOOS == "darwin" {
// Remove log files on cleanup and log errors. // Remove log files on cleanup and log errors.
err := os.Remove(launchdStdoutPath) err := os.Remove(launchdStdoutPath)
if err != nil && !errors.Is(err, os.ErrNotExist) { if err != nil && !errors.Is(err, os.ErrNotExist) {
log.Printf("removing stdout file: %s", err) log.Info("service: warning: removing stdout file: %s", err)
} }
err = os.Remove(launchdStderrPath) err = os.Remove(launchdStderrPath)
if err != nil && !errors.Is(err, os.ErrNotExist) { if err != nil && !errors.Is(err, os.ErrNotExist) {
log.Printf("removing stderr file: %s", err) log.Info("service: warning: removing stderr file: %s", err)
} }
} }
} }