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

Conversation

ssongliu
Copy link
Member

No description provided.

Copy link

f2c-ci-robot bot commented Dec 12, 2024

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Copy link
Member

@wanghe-fit2cloud wanghe-fit2cloud left a comment

Choose a reason for hiding this comment

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

/lgtm

@@ -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.

@@ -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.

@@ -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.

@wanghe-fit2cloud
Copy link
Member

/approve

Copy link

f2c-ci-robot bot commented Dec 12, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: wanghe-fit2cloud

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@f2c-ci-robot f2c-ci-robot bot merged commit 82e9e72 into dev Dec 12, 2024
7 checks passed
@f2c-ci-robot f2c-ci-robot bot deleted the pr@dev@fix_init_geoip branch December 12, 2024 06:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants