Pull request: all: mv IsOpenWrt

Updates #2829.

Squashed commit of the following:

commit a284a26ba5df101c78e6f866d1bdfa540d205666
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Apr 8 20:34:20 2021 +0300

    aghos: fix darwin

commit 9dbd42d75ebb048c83dbd06a1f09d950b3d12181
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Apr 8 20:11:56 2021 +0300

    all: mv IsOpenWrt
This commit is contained in:
Ainar Garipov 2021-04-08 20:42:04 +03:00
parent 8c7f2b77d5
commit 6a8f6357e2
10 changed files with 78 additions and 62 deletions

View file

@ -90,5 +90,13 @@ go-test: ; $(ENV) RACE='1' "$(SHELL)" ./scripts/make/go-test.sh
go-check: go-tools go-lint go-test
# A quick check to make sure that all supported operating systems can be
# typechecked and built successfully.
go-os-check:
env GOOS='darwin' go vet ./internal/...
env GOOS='freebsd' go vet ./internal/...
env GOOS='linux' go vet ./internal/...
env GOOS='windows' go vet ./internal/...
openapi-lint: ; cd ./openapi/ && $(YARN) test
openapi-show: ; cd ./openapi/ && $(YARN) start

View file

@ -45,3 +45,8 @@ func RunCommand(command string, arguments ...string) (int, string, error) {
return cmd.ProcessState.ExitCode(), string(out), nil
}
// IsOpenWrt returns true if host OS is OpenWrt.
func IsOpenWrt() (ok bool) {
return isOpenWrt()
}

View file

@ -1,4 +1,4 @@
// +build aix darwin dragonfly netbsd openbsd solaris
// +build darwin
package aghos
@ -30,3 +30,7 @@ func haveAdminRights() (bool, error) {
func sendProcessSignal(pid int, sig syscall.Signal) error {
return syscall.Kill(pid, sig)
}
func isOpenWrt() (ok bool) {
return false
}

View file

@ -30,3 +30,7 @@ func haveAdminRights() (bool, error) {
func sendProcessSignal(pid int, sig syscall.Signal) error {
return syscall.Kill(pid, sig)
}
func isOpenWrt() (ok bool) {
return false
}

View file

@ -3,7 +3,11 @@
package aghos
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"
"syscall"
"github.com/AdguardTeam/golibs/log"
@ -37,3 +41,41 @@ func haveAdminRights() (bool, error) {
func sendProcessSignal(pid int, sig syscall.Signal) error {
return syscall.Kill(pid, sig)
}
func isOpenWrt() (ok bool) {
const etcDir = "/etc"
// TODO(e.burkov): Take care of dealing with fs package after updating
// Go version to 1.16.
fileInfos, err := ioutil.ReadDir(etcDir)
if err != nil {
return false
}
// fNameSubstr is a part of a name of the desired file.
const fNameSubstr = "release"
osNameData := []byte("OpenWrt")
for _, fileInfo := range fileInfos {
if fileInfo.IsDir() {
continue
}
fn := fileInfo.Name()
if !strings.Contains(fn, fNameSubstr) {
continue
}
var body []byte
body, err = ioutil.ReadFile(filepath.Join(etcDir, fn))
if err != nil {
continue
}
if bytes.Contains(body, osNameData) {
return true
}
}
return false
}

View file

@ -40,3 +40,7 @@ func haveAdminRights() (bool, error) {
func sendProcessSignal(pid int, sig syscall.Signal) error {
return fmt.Errorf("not supported on Windows")
}
func isOpenWrt() (ok bool) {
return false
}

View file

@ -10,7 +10,6 @@ import (
"syscall"
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/AdguardTeam/AdGuardHome/internal/util"
"github.com/AdguardTeam/golibs/log"
"github.com/kardianos/service"
)
@ -215,7 +214,7 @@ func handleServiceInstallCommand(s service.Service) {
log.Fatal(err)
}
if util.IsOpenWrt() {
if aghos.IsOpenWrt() {
// On OpenWrt it is important to run enable after the service installation
// Otherwise, the service won't start on the system startup
_, err = runInitdCommand("enable")
@ -242,7 +241,7 @@ Click on the link below and follow the Installation Wizard steps to finish setup
// handleServiceStatusCommand handles service "uninstall" command
func handleServiceUninstallCommand(s service.Service) {
if util.IsOpenWrt() {
if aghos.IsOpenWrt() {
// On OpenWrt it is important to run disable command first
// as it will remove the symlink
_, err := runInitdCommand("disable")
@ -290,7 +289,7 @@ func configureService(c *service.Config) {
c.Option["SysvScript"] = sysvScript
// On OpenWrt we're using a different type of sysvScript.
if util.IsOpenWrt() {
if aghos.IsOpenWrt() {
c.Option["SysvScript"] = openWrtScript
} else if runtime.GOOS == "freebsd" {
c.Option["SysvScript"] = freeBSDScript

View file

@ -1,3 +1,7 @@
// Package util contains various utilities.
//
// TODO(a.garipov): Such packages are widely considered an antipattern. Remove
// this when we refactor our project structure.
package util
import (
@ -13,6 +17,7 @@ import (
"sync"
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/AdguardTeam/golibs/log"
"github.com/fsnotify/fsnotify"
"github.com/miekg/dns"
@ -68,7 +73,7 @@ func (a *AutoHosts) Init(hostsFn string) {
a.hostsFn = hostsFn
}
if IsOpenWrt() {
if aghos.IsOpenWrt() {
// OpenWrt: "/tmp/hosts/dhcp.cfg01411c".
a.hostsDirs = append(a.hostsDirs, "/tmp/hosts")
}

View file

@ -1,55 +0,0 @@
// Package util contains various utilities.
//
// TODO(a.garipov): Such packages are widely considered an antipattern. Remove
// this when we refactor our project structure.
package util
import (
"bytes"
"io/ioutil"
"path/filepath"
"runtime"
"strings"
)
// IsOpenWrt returns true if host OS is OpenWrt.
func IsOpenWrt() bool {
if runtime.GOOS != "linux" {
return false
}
const etcDir = "/etc"
// TODO(e.burkov): Take care of dealing with fs package after updating
// Go version to 1.16.
fileInfos, err := ioutil.ReadDir(etcDir)
if err != nil {
return false
}
// fNameSubstr is a part of a name of the desired file.
const fNameSubstr = "release"
osNameData := []byte("OpenWrt")
for _, fileInfo := range fileInfos {
if fileInfo.IsDir() {
continue
}
if !strings.Contains(fileInfo.Name(), fNameSubstr) {
continue
}
var body []byte
body, err = ioutil.ReadFile(filepath.Join(etcDir, fileInfo.Name()))
if err != nil {
continue
}
if bytes.Contains(body, osNameData) {
return true
}
}
return false
}

View file

@ -14,7 +14,7 @@ fi
if [ "$(git diff --cached --name-only -- '*.go' 'go.mod')" ]
then
make go-lint go-test
make go-os-check go-lint go-test
fi
if [ "$(git diff --cached --name-only -- './openapi/openapi.yaml')" ]