diff --git a/Makefile b/Makefile index fd3414c..2faaf7d 100644 --- a/Makefile +++ b/Makefile @@ -40,19 +40,26 @@ test: test-with-coverage: go test -tags=unit -timeout=600s -v ./... -coverprofile=coverage.out -install-cloud: build-cloud - systemctl stop edgeboxctl - cp ./bin/edgeboxctl /usr/local/bin/edgeboxctl - cp ./edgeboxctl/edgeboxctl.service /lib/systemd/system/edgeboxctl.service - systemctl daemon-reload - @echo "Edgeboxctl installed successfully" - @echo "To start edgeboxctl run: systemctl start edgeboxctl" - -install-prod: build-prod +install: sudo systemctl stop edgeboxctl sudo rm -rf /usr/local/bin/edgeboxctl /lib/systemd/system/edgeboxctl.service sudo cp ./bin/edgeboxctl /usr/local/bin/edgeboxctl sudo cp ./edgeboxctl.service /lib/systemd/system/edgeboxctl.service sudo systemctl daemon-reload @echo "Edgeboxctl installed successfully" - @echo "To start edgeboxctl run: systemctl start edgeboxctl" \ No newline at end of file + @echo "To start edgeboxctl run: systemctl start edgeboxctl" + +install-prod: build-prod install +install-cloud: build-cloud install +install-arm64: build-arm64 install +install-armhf: build-armhf install + +start: + systemctl start edgeboxctl + +stop: + systemctl stop edgeboxctl + +log: start + journalctl -fu edgeboxctl + diff --git a/internal/system/system.go b/internal/system/system.go index a8a7791..12e8c5d 100644 --- a/internal/system/system.go +++ b/internal/system/system.go @@ -6,6 +6,7 @@ import ( "strings" "log" "os" + "io" "os/exec" "bufio" "path/filepath" @@ -302,3 +303,78 @@ func RemoveTunnelService() { utils.Exec(wsPath, "rm", cmdargs) } +func CopyDir(src string, dest string) error { + srcInfo, err := os.Stat(src) + if err != nil { + return err + } + if !srcInfo.IsDir() { + return fmt.Errorf("%s is not a directory", src) + } + + err = os.MkdirAll(dest, srcInfo.Mode()) + if err != nil { + return err + } + + items, err := ioutil.ReadDir(src) + if err != nil { + return err + } + + for _, item := range items { + srcPath := filepath.Join(src, item.Name()) + destPath := filepath.Join(dest, item.Name()) + + if item.IsDir() { + err = CopyDir(srcPath, destPath) + if err != nil { + fmt.Printf("error copying directory %s to %s: %s\n", srcPath, destPath, err.Error()) + } + } else { + err = CopyFile(srcPath, destPath) + if err != nil { + fmt.Printf("error copying file %s to %s: %s\n", srcPath, destPath, err.Error()) + } + } + } + + return nil +} + +func CopyFile(src string, dest string) error { + srcFile, err := os.Open(src) + if err != nil { + return err + } + defer srcFile.Close() + + destFile, err := os.Create(dest) + if err != nil { + return err + } + defer destFile.Close() + + _, err = io.Copy(destFile, srcFile) + if err != nil { + return err + } + + err = destFile.Sync() + if err != nil { + return err + } + + srcInfo, err := os.Stat(src) + if err != nil { + return err + } + + err = os.Chmod(dest, srcInfo.Mode()) + if err != nil { + return err + } + + return nil +} + diff --git a/internal/tasks/tasks.go b/internal/tasks/tasks.go index d2c49f9..a88c101 100644 --- a/internal/tasks/tasks.go +++ b/internal/tasks/tasks.go @@ -626,7 +626,7 @@ func taskRestoreBackup() string { // Copy all files in /home/system/components/apps/ to a backup folder fmt.Println("Copying all files in /home/system/components/apps/ to a backup folder") os.MkdirAll(utils.GetPath(utils.EdgeAppsBackupPath + "temp/"), 0777) - utils.CopyDir(utils.GetPath(utils.EdgeAppsPath), utils.GetPath(utils.EdgeAppsBackupPath + "temp/")) + system.CopyDir(utils.GetPath(utils.EdgeAppsPath), utils.GetPath(utils.EdgeAppsBackupPath + "temp/")) fmt.Println("Removing all files in /home/system/components/apps/") os.RemoveAll(utils.GetPath(utils.EdgeAppsPath)) @@ -649,7 +649,7 @@ func taskRestoreBackup() string { if strings.Contains(result, "Fatal:") { // Copy all files from backup folder to /home/system/components/apps/ os.MkdirAll(utils.GetPath(utils.EdgeAppsPath), 0777) - utils.CopyDir(utils.GetPath(utils.EdgeAppsBackupPath + "temp/"), utils.GetPath(utils.EdgeAppsPath)) + system.CopyDir(utils.GetPath(utils.EdgeAppsBackupPath + "temp/"), utils.GetPath(utils.EdgeAppsPath)) fmt.Println("Error restoring backup: ") utils.WriteOption("BACKUP_STATUS", "error") diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 9364bcb..16f1d87 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -109,6 +109,7 @@ const CloudEnvFileLocation string = "cloudEnvFileLocation" const ApiEnvFileLocation string = "apiEnvFileLocation" const ApiPath string = "apiPath" const EdgeAppsPath string = "edgeAppsPath" +const EdgeAppsBackupPath string = "edgeAppsBackupPath" const WsPath string = "wsPath" // GetPath : Returns either the hardcoded path, or a overwritten value via .env file at project root. Register paths here for seamless working code between dev and prod environments ;) @@ -156,6 +157,13 @@ func GetPath(pathKey string) string { targetPath = "/home/system/components/apps/" } + case EdgeAppsBackupPath: + if env["EDGEAPPS_BACKUP_PATH"] != "" { + targetPath = env["EDGEAPPS_BACKUP_PATH"] + } else { + targetPath = "/home/system/components/backups/" + } + case WsPath: if env["WS_PATH"] != "" {