8000 <feature>[ansible]: add SshFilesMd5Checker by MatheMatrix · Pull Request #15 · MatheMatrix/zstack · GitHub
[go: up one dir, main page]

Skip to content

<feature>[ansible]: add SshFilesMd5Checker#15

Closed
MatheMatrix wants to merge 1 commit into5.0.0from
sync/tao.gan/5.0.0-ZSTAC-59604@@2
Closed

<feature>[ansible]: add SshFilesMd5Checker#15
MatheMatrix wants to merge 1 commit into5.0.0from
sync/tao.gan/5.0.0-ZSTAC-59604@@2

Conversation

@MatheMatrix
Copy link
Owner
@MatheMatrix MatheMatrix commented Dec 7, 2023

1 Cache inodes and hashes of all architectur bin packages in mn memory
2 If the bin package hash value of the target physical machine is
not in the in-memory hash list, redeploy the bin package.

Resolves: ZSTAC-59604

Change-Id: I75676474797078746d757a7564716d736f6b7267

sync from gitlab !5410

Summary by CodeRabbit

  • 新功能
    • 新增了应用中的搜索功能。

@coderabbitai
Copy link
coderabbitai bot commented Dec 7, 2023

Warning

Rate Limit Exceeded

@MatheMatrix has exceeded the limit for the number of files or commits that can be reviewed per hour. Please wait 18 minutes and 8 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between 78b859c and 8990551.

Walkthrough

在这次代码更新中,我们看到了几个Java类的变化。新增了SshFilesMd5Checker类,用于通过SSH检查文件是否需要部署以及删除目标文件,并实现了AnsibleChecker接口。SHAUtils类增加了两个处理文件的新方法,用于计算文件的MD5校验和和摘要。PathUtil类也新增了一个方法,用于获取文件的inode信息。这些变化主要涉及文件处理和安全性功能的增强。

Changes

文件路径 变更摘要
org.zstack.core.ansible.SshFilesMd5Checker 新增类SshFilesMd5Checker,实现AnsibleChecker接口,新增方法needDeploy()deleteDestFile(),以及相关属性的getter和setter方法。
org.zstack.utils.SHAUtils 新增方法getFileMd5sumgetDigestOfFile,用于计算文件的MD5和摘要。
org.zstack.utils.path.PathUtil 新增方法getFileInode,用于获取文件的inode信息。

🐇🌟
代码跳跃,逻辑串联,
新增方法,功能增强。
文件校验,安全先行,
小兔庆祝,代码新生。 🎉📜


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON 8000 schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

@MatheMatrix MatheMatrix force-pushed the sync/tao.gan/5.0.0-ZSTAC-59604@@2 branch from 24189a5 to ad1b4eb Compare December 7, 2023 05:05
Copy link
@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 4

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 78b859c and ad1b4eb.
Files selected for processing (3)
  • core/src/main/java/org/zstack/core/ansible/SshFilesMd5Checker.java (1 hunks)
  • utils/src/main/java/org/zstack/utils/SHAUtils.java (2 hunks)
  • utils/src/main/java/org/zstack/utils/path/PathUtil.java (1 hunks)
Additional comments: 3
core/src/main/java/org/zstack/core/ansible/SshFilesMd5Checker.java (1)
  • 11-112: 整体上,SshFilesMd5Checker 类的实现符合PR目标和生成的摘要。它实现了 AnsibleChecker 接口,并提供了检查和删除文件的方法,以及相关属性的getter和setter方法。代码风格和结构看起来都是清晰和一致的。
utils/src/main/java/org/zstack/utils/SHAUtils.java (2)
  • 23-24: 确认encrypt方法没有发生变化,与PR目标和摘要中的描述相符。

  • 3-9: 新添加的导入语句java.io.IOException, java.nio.file.Files, 和 java.nio.file.PathsgetDigestOfFile方法使用,这是合理的。但是,如果其他部分的代码没有使用这些导入,应该检查是否有未使用的导入。

Comment on lines +433 to +439
public static Long getFileInode(String filePath) {
Path path = Paths.get(filePath);
try {
return (Long) Files.getAttribute(path, "unix:ino");
} catch (Exception e) {
throw new RuntimeException(e);
}
Copy link

Choose a reason for hiding this comment

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

The method getFileInode throws a generic RuntimeException without any logging or specific error message. Consider adding logging and throwing a more specific exception to provide better context for the error.

- throw new RuntimeException(e);
+ logger.error("Failed to get inode for file: " + filePath, e);
+ throw new RuntimeException("Failed to get inode for file: " + filePath, e);

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
public static Long getFileInode(String filePath) {
Path path = Paths.get(filePath);
try {
return (Long) Files.getAttribute(path, "unix:ino");
} catch (Exception e) {
throw new RuntimeException(e);
}
public static Long getFileInode(String filePath) {
Path path = Paths.get(filePath);
try {
return (Long) Files.getAttribute(path, "unix:ino");
} catch (Exception e) {
logger.error("Failed to get inode for file: " + filePath, e);
throw new RuntimeException("Failed to get inode for file: " + filePath, e);
}

Comment on lines +23 to +46
@Override
public boolean needDeploy() {
Ssh ssh = new Ssh();
ssh.setUsername(username).setPrivateKey(privateKey)
.setPassword(password).setPort(sshPort)
.setHostname(ip)
.setTimeout(5);
try {
ssh.command(String.format("echo %s | sudo -S md5sum %s 2>/dev/null", password, filePath));
SshResult ret = ssh.run();
if (ret.getReturnCode() != 0) {
return true;
}
ssh.reset();
String md5 = ret.getStdout().split(" ")[0];
if (!fileMd5sums.contains(md5)) {
logger.debug(String.format("file MD5 changed, dest[%s, md5, %s]", filePath, md5));
return true;
}
} finally {
ssh.close();
}

return false;
Copy link

Choose a reason for hiding this comment

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

needDeploy 方法中,如果SSH命令执行失败(返回码非0),方法会直接返回 true,这意味着需要部署。但是,如果SSH命令的执行失败是由于网络问题或其他非文件MD5不匹配的原因,这可能会导致不必要的部署。建议添加更详细的错误处理来区分不同类型的错误,并且只在确认文件MD5不匹配时才返回 true

Comment on lines +50 to +56
public void deleteDestFile() {
Ssh ssh = new Ssh();
ssh.setUsername(username).setPrivateKey(privateKey)
.setPassword(password).setPort(sshPort)
.setHostname(ip).command(String.format("rm -f %s", filePath)).runAndClose();
logger.debug(String.format("delete dest file[%s]", filePath));
}
Copy link

Choose a reason for hiding this comment

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

deleteDestFile 方法中,使用了 Ssh 类来删除文件,并且没有显式的错误处理。如果删除命令失败,这可能会导致问题。建议添加错误处理逻辑来捕获可能的异常,并记录相应的错误信息。

Comment on lines 27 to 37
public static String getDigestOfFile(String filePath, String algorithm, int precision) {
try {
byte[] fileData = Files.readAllBytes(Paths.get(filePath));
MessageDigest md = MessageDigest.getInstance(algorithm);
md.reset();
md.update(fileData);
BigInteger bigInteger = new BigInteger(1, md.digest());
return String.format("%" + precision + "x", bigInteger);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Copy link

Choose a reason for hiding this comment

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

检查新添加的getDigestOfFile方法,确认其逻辑正确无误。注意,异常处理使用了一个通用的Exception捕获,这可能会隐藏潜在的错误类型。建议捕获更具体的异常类型,比如IOExceptionNoSuchAlgorithmException

-        } catch (Exception e) {
+        } catch (IOException | NoSuchAlgorithmException e) {

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
public static String getDigestOfFile(String filePath, String algorithm, int precision) {
try {
byte[] fileData = Files.readAllBytes(Paths.get(filePath));
MessageDigest md = MessageDigest.getInstance(algorithm);
md.reset();
md.update(fileData);
BigInteger bigInteger = new BigInteger(1, md.digest());
return String.format("%" + precision + "x", bigInteger);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String getDigestOfFile(String filePath, String algorithm, int precision) {
try {
byte[] fileData = Files.readAllBytes(Paths.get(filePath));
MessageDigest md = MessageDigest.getInstance(algorithm);
md.reset();
md.update(fileData);
BigInteger bigInteger = new BigInteger(1, md.digest());
return String.format("%" + precision + "x", bigInteger);
} catch (IOException | NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

@MatheMatrix MatheMatrix force-pushed the sync/tao.gan/5.0.0-ZSTAC-59604@@2 branch 12 times, most recently from aa0aa1c to caa2dd1 Compare December 7, 2023 07:20
1 Cache inodes and hashes of all architectur bin packages in mn memory
2 If the bin package hash value of the target physical machine is
not in the in-memory hash list, redeploy the bin package.

Resolves: ZSTAC-59604

Change-Id: I75676474797078746d757a7564716d736f6b7267
@MatheMatrix MatheMatrix force-pushed the sync/tao.gan/5.0.0-ZSTAC-59604@@2 branch from caa2dd1 to 8990551 Compare December 7, 2023 07:21
@MatheMatrix MatheMatrix closed this Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments

0