forked from zstackio/zstack
-
Notifications
You must be signed in to change notification settings - Fork 0
<fix>[header]: add Tpm cascade extensions #3737
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
Open
ZStack-Robot
wants to merge
1
commit into
feature-zsv-5.0.0-vm-support-vtpm-and-secuceboot
Choose a base branch
from
sync/wenhao.zhang/zsv-ldap
base: feature-zsv-5.0.0-vm-support-vtpm-and-secuceboot
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
compute/src/main/java/org/zstack/compute/vm/devices/TpmCascadeExtension.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| package org.zstack.compute.vm.devices; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.zstack.core.asyncbatch.While; | ||
| import org.zstack.core.cascade.AbstractAsyncCascadeExtension; | ||
| import org.zstack.core.cascade.CascadeAction; | ||
| import org.zstack.core.cascade.CascadeConstant; | ||
| import org.zstack.core.cloudbus.CloudBus; | ||
| import org.zstack.core.cloudbus.CloudBusCallBack; | ||
| import org.zstack.core.db.DatabaseFacade; | ||
| import org.zstack.core.db.Q; | ||
| import org.zstack.header.core.Completion; | ||
| import org.zstack.header.core.WhileDoneCompletion; | ||
| import org.zstack.header.errorcode.ErrorCodeList; | ||
| import org.zstack.header.message.MessageReply; | ||
| import org.zstack.header.tpm.entity.TpmVO; | ||
| import org.zstack.header.tpm.entity.TpmVO_; | ||
| import org.zstack.header.tpm.message.TpmDeletionMsg; | ||
| import org.zstack.header.vm.VmInstanceInventory; | ||
| import org.zstack.header.vm.VmInstanceVO; | ||
| import org.zstack.utils.CollectionUtils; | ||
| import org.zstack.utils.Utils; | ||
| import org.zstack.utils.logging.CLogger; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.zstack.core.Platform.operr; | ||
| import static org.zstack.header.tpm.TpmConstants.SERVICE_ID; | ||
| import static org.zstack.utils.CollectionDSL.list; | ||
| import static org.zstack.utils.CollectionUtils.transformAndRemoveNull; | ||
|
|
||
| public class TpmCascadeExtension extends AbstractAsyncCascadeExtension { | ||
| private static final CLogger logger = Utils.getLogger(TpmCascadeExtension.class); | ||
|
|
||
| @Autowired | ||
| private DatabaseFacade dbf; | ||
| @Autowired | ||
| private CloudBus bus; | ||
|
|
||
| private static final String NAME = TpmVO.class.getSimpleName(); | ||
|
|
||
| @Override | ||
| public void asyncCascade(CascadeAction action, Completion completion) { | ||
| if (action.isActionCode(CascadeConstant.DELETION_CHECK_CODE)) { | ||
| handleDeletionCheck(action, completion); | ||
| } else if (action.isActionCode(CascadeConstant.DELETION_DELETE_CODE, CascadeConstant.DELETION_FORCE_DELETE_CODE)) { | ||
| handleDeletion(action, completion); | ||
| } else if (action.isActionCode(CascadeConstant.DELETION_CLEANUP_CODE)) { | ||
| handleDeletionCleanup(action, completion); | ||
| } else { | ||
| completion.success(); | ||
| } | ||
| } | ||
|
|
||
| private List<TpmVO> tpmFromAction(CascadeAction action) { | ||
| if (VmInstanceVO.class.getSimpleName().equals(action.getParentIssuer())) { | ||
| List<VmInstanceInventory> vmInventories = action.getParentIssuerContext(); | ||
| List<String> vmUuidList = transformAndRemoveNull(vmInventories, VmInstanceInventory::getUuid); | ||
|
|
||
| if (vmUuidList.isEmpty()) { | ||
| return null; | ||
| } | ||
| return Q.New(TpmVO.class) | ||
| .in(TpmVO_.vmInstanceUuid, vmUuidList) | ||
| .list(); | ||
| } else if (NAME.equals(action.getParentIssuer())) { | ||
| return action.getParentIssuerContext(); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private void handleDeletionCheck(CascadeAction action, Completion completion) { | ||
| completion.success(); | ||
| } | ||
|
|
||
| private void handleDeletion(CascadeAction action, Completion completion) { | ||
| final List<TpmVO> tpmList = tpmFromAction(action); | ||
| if (CollectionUtils.isEmpty(tpmList)) { | ||
| completion.success(); | ||
| return; | ||
| } | ||
|
|
||
| new While<>(tpmList).each((tpm, whileCompletion) -> { | ||
| TpmDeletionMsg msg = new TpmDeletionMsg(); | ||
| msg.setTpmUuid(tpm.getUuid()); | ||
| msg.setVmInstanceUuid(tpm.getVmInstanceUuid()); | ||
| msg.setForceDelete(true); | ||
| bus.makeTargetServiceIdByResourceUuid(msg, SERVICE_ID, msg.getTpmUuid()); | ||
| bus.send(msg, new CloudBusCallBack(whileCompletion) { | ||
| @Override | ||
| public void run(MessageReply reply) { | ||
| if (reply.isSuccess()) { | ||
| logger.debug(String.format("deleted Tpm[uuid:%s] from VM[uuid:%s]", | ||
| tpm.getUuid(), tpm.getVmInstanceUuid())); | ||
| } else { | ||
| whileCompletion.addError(reply.getError()); | ||
| whileCompletion.allDone(); | ||
| } | ||
| whileCompletion.done(); | ||
| } | ||
| }); | ||
| }).run(new WhileDoneCompletion(completion) { | ||
| @Override | ||
| public void done(ErrorCodeList errorCodeList) { | ||
| if (!errorCodeList.isEmpty()) { | ||
| completion.fail(operr("failed to delete Tpm from VM[uuid:%s]", tpmList.get(0).getVmInstanceUuid()) | ||
| .withCause(errorCodeList)); | ||
| return; | ||
| } | ||
| completion.success(); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }); | ||
| } | ||
|
|
||
| private void handleDeletionCleanup(CascadeAction action, Completion completion) { | ||
| completion.success(); | ||
| } | ||
|
|
||
| @Override | ||
| public List<String> getEdgeNames() { | ||
| return list(VmInstanceVO.class.getSimpleName()); | ||
| } | ||
|
|
||
| @Override | ||
| public String getCascadeResourceName() { | ||
| return NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public CascadeAction createActionForChildResource(CascadeAction action) { | ||
| if (CascadeConstant.DELETION_CODES.contains(action.getActionCode())) { | ||
| List<TpmVO> ctx = tpmFromAction(action); | ||
| if (ctx != null) { | ||
| return action.copy().setParentIssuer(NAME).setParentIssuerContext(ctx); | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
148 changes: 148 additions & 0 deletions
148
compute/src/main/java/org/zstack/compute/vm/devices/VmHostBackupFileCascadeExtension.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| package org.zstack.compute.vm.devices; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.zstack.core.asyncbatch.While; | ||
| import org.zstack.core.cascade.AbstractAsyncCascadeExtension; | ||
| import org.zstack.core.cascade.CascadeAction; | ||
| import org.zstack.core.cascade.CascadeConstant; | ||
| import org.zstack.core.cloudbus.CloudBus; | ||
| import org.zstack.core.cloudbus.CloudBusCallBack; | ||
| import org.zstack.core.db.DatabaseFacade; | ||
| import org.zstack.core.db.Q; | ||
| import org.zstack.header.core.Completion; | ||
| import org.zstack.header.core.WhileDoneCompletion; | ||
| import org.zstack.header.errorcode.ErrorCodeList; | ||
| import org.zstack.header.message.MessageReply; | ||
| import org.zstack.header.vm.VmInstanceConstant; | ||
| import org.zstack.header.vm.VmInstanceInventory; | ||
| import org.zstack.header.vm.VmInstanceVO; | ||
| import org.zstack.header.vm.additions.VmHostBackupFileDeletionMsg; | ||
| import org.zstack.header.vm.additions.VmHostBackupFileVO; | ||
| import org.zstack.header.vm.additions.VmHostBackupFileVO_; | ||
| import org.zstack.utils.CollectionUtils; | ||
| import org.zstack.utils.Utils; | ||
| import org.zstack.utils.logging.CLogger; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.zstack.core.Platform.operr; | ||
| import static org.zstack.utils.CollectionDSL.list; | ||
| import static org.zstack.utils.CollectionUtils.transformAndRemoveNull; | ||
|
|
||
| public class VmHostBackupFileCascadeExtension extends AbstractAsyncCascadeExtension { | ||
| private static final CLogger logger = Utils.getLogger(VmHostBackupFileCascadeExtension.class); | ||
|
|
||
| @Autowired | ||
| private DatabaseFacade dbf; | ||
| @Autowired | ||
| private CloudBus bus; | ||
|
|
||
| private static final String NAME = VmHostBackupFileVO.class.getSimpleName(); | ||
|
|
||
| @Override | ||
| public void asyncCascade(CascadeAction action, Completion completion) { | ||
| if (action.isActionCode(CascadeConstant.DELETION_CHECK_CODE)) { | ||
| handleDeletionCheck(action, completion); | ||
| } else if (action.isActionCode(CascadeConstant.DELETION_DELETE_CODE, CascadeConstant.DELETION_FORCE_DELETE_CODE)) { | ||
| handleDeletion(action, completion); | ||
| } else if (action.isActionCode(CascadeConstant.DELETION_CLEANUP_CODE)) { | ||
| handleDeletionCleanup(action, completion); | ||
| } else { | ||
| completion.success(); | ||
| } | ||
| } | ||
|
|
||
| private List<VmHostBackupFileVO> voFromAction(CascadeAction action) { | ||
| if (VmInstanceVO.class.getSimpleName().equals(action.getParentIssuer())) { | ||
| List<VmInstanceInventory> vmInventories = action.getParentIssuerContext(); | ||
| List<String> vmUuidList = transformAndRemoveNull(vmInventories, VmInstanceInventory::getUuid); | ||
|
|
||
| if (vmUuidList.isEmpty()) { | ||
| return null; | ||
| } | ||
| return Q.New(VmHostBackupFileVO.class) | ||
| .in(VmHostBackupFileVO_.resourceUuid, vmUuidList) | ||
| .list(); | ||
| } | ||
| // Note: VolumeSnapshotGroupVO has no cascade extension! | ||
| // skip: else if (VolumeSnapshotGroupVO.class.getSimpleName().equals(action.getParentIssuer())) | ||
| else if (NAME.equals(action.getParentIssuer())) { | ||
| return action.getParentIssuerContext(); | ||
| } | ||
|
|
||
| return null; | ||
|
Comment on lines
+55
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 快照组上的备份文件不会被这个级联清掉。
Also applies to: 123-130 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| private void handleDeletionCheck(CascadeAction action, Completion completion) { | ||
| completion.success(); | ||
| } | ||
|
|
||
| private void handleDeletion(CascadeAction action, Completion completion) { | ||
| final List<VmHostBackupFileVO> voList = voFromAction(action); | ||
| if (CollectionUtils.isEmpty(voList)) { | ||
| completion.success(); | ||
| return; | ||
| } | ||
|
|
||
| new While<>(voList).each((vo, whileCompletion) -> { | ||
| VmHostBackupFileDeletionMsg msg = new VmHostBackupFileDeletionMsg(); | ||
| msg.setUuid(vo.getUuid()); | ||
| msg.setForceDelete(true); | ||
| bus.makeLocalServiceId(msg, VmInstanceConstant.SECURE_BOOT_SERVICE_ID); | ||
| bus.send(msg, new CloudBusCallBack(whileCompletion) { | ||
| @Override | ||
| public void run(MessageReply reply) { | ||
| if (reply.isSuccess()) { | ||
| logger.debug(String.format("deleted VmHostBackupFile[uuid:%s] from resource[uuid:%s]", | ||
| vo.getUuid(), vo.getResourceUuid())); | ||
| } else { | ||
| whileCompletion.addError(reply.getError()); | ||
| whileCompletion.allDone(); | ||
| } | ||
| whileCompletion.done(); | ||
| } | ||
| }); | ||
| }).run(new WhileDoneCompletion(completion) { | ||
| @Override | ||
| public void done(ErrorCodeList errorCodeList) { | ||
| if (!errorCodeList.isEmpty()) { | ||
| completion.fail(operr("failed to delete VmHostBackupFile from resource[uuid:%s]", voList.get(0).getResourceUuid()) | ||
| .withCause(errorCodeList)); | ||
| return; | ||
| } | ||
| completion.success(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private void handleDeletionCleanup(CascadeAction action, Completion completion) { | ||
| completion.success(); | ||
| } | ||
|
|
||
| @Override | ||
| public List<String> getEdgeNames() { | ||
| return list( | ||
| VmInstanceVO.class.getSimpleName() | ||
|
|
||
| // Note: VolumeSnapshotGroupVO has no cascade extension! | ||
| // skip: VolumeSnapshotGroupVO.class.getName() | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public String getCascadeResourceName() { | ||
| return NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public CascadeAction createActionForChildResource(CascadeAction action) { | ||
| if (CascadeConstant.DELETION_CODES.contains(action.getActionCode())) { | ||
| List<VmHostBackupFileVO> ctx = voFromAction(action); | ||
| if (ctx != null) { | ||
| return action.copy().setParentIssuer(NAME).setParentIssuerContext(ctx); | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.