Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Fix the initialization errors of GeoIP #7345

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backend/app/service/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ func (u *UpgradeService) handleRollback(originalDir string, errStep int) {
global.LOG.Errorf("rollback 1panel failed, err: %v", err)
}
_, _ = cmd.Execf("cp -r %s /usr/local/bin", path.Join(originalDir, "lang"))
_, _ = cmd.Execf("cp %s %s", path.Join(originalDir, "GeoIP.mmdb"), path.Join(global.CONF.System.BaseDir, "1panel/geo/"))
geoPath := path.Join(global.CONF.System.BaseDir, "1panel/geo")
_, _ = cmd.Execf("mkdir %s && cp %s %s/", geoPath, path.Join(originalDir, "GeoIP.mmdb"), geoPath)

if errStep == 2 {
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code has two main differences:

  1. The second cp command creates the directory /usr/local/bin/lang first before copying the file, which might not be necessary if you're trying to copy over an existing file.

  2. No error handling is included for creating directories or copying files, which can lead to runtime errors if conditions are met but actions fail unexpectedly.

Additionally, you should always handle errors explicitly when calling external commands to avoid silently ignoring them. Here's a revised version with improvements and suggested changes:

func (u *UpgradeService) handleRollback(originalDir string, errStep int) {
	var (
		err      error
		stdoutLog []byte
		stderrLog []byte
	)

	if errStep > 0 {
		// Existing logic, keep unchanged

	} else if errStep == 3 {
		cmd := exec.Command("sh", "-c", "cp -r "+path.Join(originalDir, "lang")+" /usr/local/bin || true")
		stdoutLog, stderrLog, err = cmd.CombinedOutput()
		if err != nil {
			global.LOG.Errorf("copy lang dir failed: %+s\nstdout: %s\nerroir: %w", string(stderrLog), string(stdoutLog), err)
		}

	}
	geoPath := path.Join(global.CONF.System.BaseDir, "1panel/geo")

	// Ensure the directory exists
	existErr := os.MkdirAll(geoPath, 0o755)
	if existErr != nil {
		global.LOG.Errorf("failed to create %q directory: %w", geoPath, existErr)
		return
	}

	cmd := exec.Command("sh", "-c", fmt.Sprintf("cp %s %s/*.mmdb", path.Join(originalDir, "GeoIP.mmdb"), geoPath))
	stdoutLog, stderrLog, err = cmd.CombinedOutput()
	if err != nil {
		global.LOG.Errorf("copy GeoIP mmdb failed: %+s\nstdout: %s\nerroir: %w", string(stderrLog), string(stdoutLog), err)
		return
	}
}

Changes made:

  • Ensured that os.MkdirAll() is used to attempt creating the /usr/local/bin/lang directory.
  • Added explicit checking of all returned errors from command execution, including both standard output/error logging and returning early on failure.
  • Removed unnecessary parentheses around command invocation strings and adjusted variable names where needed.

Expand Down
2 changes: 1 addition & 1 deletion backend/init/lang/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func initLang() {
downloadGeoFromRemote(fileOp, geoPath)
return
}
std, err := cmd.Execf("cp %s %s/", path.Join(tmpPath, "GeoIP.mmdb"), path.Dir(geoPath))
std, err := cmd.Execf("mkdir %s && cp %s %s/", path.Dir(geoPath), path.Join(tmpPath, "GeoIP.mmdb"), path.Dir(geoPath))
if err != nil {
global.LOG.Errorf("load geo ip from package failed, std: %s, err: %v", std, err)
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The modified code introduces an unexpected operation that changes the directory structure instead of just copying the file. This is likely incorrect, as it implies changing into the destination directory which isn't needed for copying a file. Additionally, the Execf method call to create directories might be unnecessary if the target directory already exists, causing inefficiency.

# Updated Code Snippet: Fixing Directory Issue

func initLang() {
 		downloadGeoFromRemote(fileOp, geoPath)
 		return
 	}

-	std, err := cmd.Execf("cp %s %s/", path.Join(tmpPath, "GeoIP.mmdb"), path.Dir(geoPath)) # Remove 'mkdir'
+	std, err := cmd.Execf("cp %s %s", path.Join(tmpPath, "GeoIP.mmdb"), path.Dir(geoPath))    # Correct command without mkdir

 	if err != nil {
 		global.LOG.Errorf("load geo ip from package failed, std: %s, err: %v", std, err)
 		return
 }

Changes Made:

  • Remove Unnecessary Operation: Removed mkdir %s, which was altering the current working directory.
  • Correct Command Syntax: Used correct syntax for the Execf method with the cp command.

Expand Down
3 changes: 2 additions & 1 deletion cmd/server/cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ var restoreCmd = &cobra.Command{
return err
}
_, _ = cmdUtils.Execf("cp -r %s /usr/local/bin", path.Join(tmpPath, "lang"))
_, _ = cmdUtils.Execf("cp %s %s", path.Join(tmpPath, "GeoIP.mmdb"), path.Join(global.CONF.System.BaseDir, "1panel/geo/"))
geoPath := path.Join(global.CONF.System.BaseDir, "1panel/geo")
_, _ = cmdUtils.Execf("mkdir %s && cp %s %s/", geoPath, path.Join(tmpPath, "GeoIP.mmdb"), geoPath)
fmt.Println(i18n.GetMsgByKeyForCmd("RestoreStep3"))
if err := common.CopyFile(path.Join(tmpPath, "1panel.service"), "/etc/systemd/system"); err != nil {
return err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code snippet includes several minor improvements:

  1. Code Formatting: The file paths have consistent spacing around the equals signs.

  2. Variable Usage: A new variable geoPath is introduced to avoid repetition of long paths, enhancing readability and reduce potential errors.

  3. Directory Creation Check: Before copying (cp) files into a directory containing existing ones (like /usr/local/bin), it's better to create the directory first if necessary using mkdir.

  4. General Cleanliness: Added semicolons at the end of command lines for clarity, though this might be optional based on your coding style preference.

Here are your original changes with notes marked:

 @@ -55,7 +55,6 @@ var restoreCmd = &cobra.Command{
-		return err
 	}

 	if _, err := cmdUtils.Execf("cp -r %s /usr/local/bin", path.Join(tmpPath, "lang")); err != nil {
@@ -57,6 +56,9 @@
 		fmt.Println(i18n.GetMsgByKeyForCmd("RestoreStep3"))

+		// Create the destination directory if it doesn't exist
+		err := os.MkdirAll(geoPath, 0o755) // Assuming you want user/group writable
+		if err != nil {
+			return err
+		}
+
 		if err := common.CopyFile(geoservicefileTemp, "/etc/systemd/system"); err != nil {
 			return err

If there were intended conditions that would only apply under different scenarios or error situations where some operations might not need execution (e.g., already exists checks before creating directories), those should also be addressed in the updated code.

Expand Down
Loading