Skip to content
Closed
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
4 changes: 3 additions & 1 deletion api/src/main/java/com/cloud/storage/Volume.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public String getDescription() {
s_fsm.addTransition(new StateMachine2.Transition<State, Event>(Attaching, Event.OperationSucceeded, Ready, null));
s_fsm.addTransition(new StateMachine2.Transition<State, Event>(Attaching, Event.OperationFailed, Ready, null));
s_fsm.addTransition(new StateMachine2.Transition<State, Event>(Destroy, Event.RecoverRequested, Ready, null));
s_fsm.addTransition(new StateMachine2.Transition<State, Event>(Destroy, Event.OperationRetryFromError, Allocated, null));
}
}

Expand All @@ -156,7 +157,8 @@ enum Event {
ExpungingRequested,
ResizeRequested,
AttachRequested,
OperationTimeout;
OperationTimeout,
OperationRetryFromError;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions api/src/main/java/com/cloud/vm/UserVmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.cloudstack.api.command.user.vm.ResetVMPasswordCmd;
import org.apache.cloudstack.api.command.user.vm.ResetVMSSHKeyCmd;
import org.apache.cloudstack.api.command.user.vm.RestoreVMCmd;
import org.apache.cloudstack.api.command.user.vm.RetryDeployVMCmd;
import org.apache.cloudstack.api.command.user.vm.ScaleVMCmd;
import org.apache.cloudstack.api.command.user.vm.StartVMCmd;
import org.apache.cloudstack.api.command.user.vm.UpdateDefaultNicForVMCmd;
Expand Down Expand Up @@ -526,4 +527,7 @@ UserVm importVM(final DataCenter zone, final Host host, final VirtualMachineTemp
* @return true if the VM is successfully unmanaged, false if not.
*/
boolean unmanageUserVM(Long vmId);

UserVm retryDeployVM(RetryDeployVMCmd cmd) throws ResourceUnavailableException, InsufficientCapacityException,
ResourceAllocationException;
}
1 change: 1 addition & 0 deletions api/src/main/java/com/cloud/vm/VirtualMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public static StateMachine2<State, VirtualMachine.Event, VirtualMachine> getStat
s_fsm.addTransition(new Transition<State, Event>(State.Expunging, VirtualMachine.Event.ExpungeOperation, State.Expunging,null));
s_fsm.addTransition(new Transition<State, Event>(State.Error, VirtualMachine.Event.DestroyRequested, State.Expunging, null));
s_fsm.addTransition(new Transition<State, Event>(State.Error, VirtualMachine.Event.ExpungeOperation, State.Expunging, null));
s_fsm.addTransition(new Transition<State, Event>(State.Error, Event.StartRequested, State.Starting, null));

s_fsm.addTransition(new Transition<State, Event>(State.Starting, VirtualMachine.Event.FollowAgentPowerOnReport, State.Running, Arrays.asList(new Impact[]{Impact.USAGE})));
s_fsm.addTransition(new Transition<State, Event>(State.Stopping, VirtualMachine.Event.FollowAgentPowerOnReport, State.Running, null));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.vm;

import com.cloud.event.EventTypes;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientCapacityException;
import com.cloud.exception.NetworkRuleConflictException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.user.Account;
import com.cloud.uservm.UserVm;
import com.cloud.vm.VirtualMachine;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.ApiErrorCode;
import org.apache.cloudstack.api.BaseAsyncCmd;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.command.user.UserCmd;
import org.apache.cloudstack.api.response.SuccessResponse;
import org.apache.cloudstack.api.response.UserVmResponse;
import org.apache.log4j.Logger;

@APICommand(name = RetryDeployVMCmd.APINAME,
description = "Retries the deployment of a virtual machine on Error state after an attempt has failed",
responseObject = SuccessResponse.class,
since = "4.17.0")
public class RetryDeployVMCmd extends BaseAsyncCmd implements UserCmd {

private static final Logger LOG = Logger.getLogger(RetryDeployVMCmd.class);
public static final String APINAME = "retryDeployVirtualMachine";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@nvazquez Can we re-use the deployVirtualMachine/startVirtualMachine API, with retry parameter?


@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType= UserVmResponse.class,
required = true, description = "The ID of the virtual machine")
private Long id;

public Long getId() {
return id;
}

@Override
public String getEventType() {
return EventTypes.EVENT_VM_START;
}

@Override
public String getEventDescription() {
return "Retry VM Deployment";
}

@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException,
ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
UserVm result;
try {
result = _userVmService.retryDeployVM(this);
} catch (ResourceUnavailableException | InsufficientCapacityException | ResourceAllocationException e) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Error on VM retry operarion: " + e.getMessage());
}

if (result != null) {
UserVmResponse response = _responseGenerator.createUserVmResponse(getResponseView(), "virtualmachine", result).get(0);
response.setResponseName(getCommandName());
setResponseObject(response);
} else {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to deploy vm uuid:" + id);
}
}

@Override
public String getCommandName() {
return APINAME.toLowerCase() + BaseCmd.RESPONSE_SUFFIX;
}

@Override
public long getEntityOwnerId() {
UserVm vm = _responseGenerator.findUserVmById(getId());
return (vm != null) ? vm.getAccountId() : Account.ACCOUNT_ID_SYSTEM;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,6 @@ public interface VolumeDao extends GenericDao<VolumeVO, Long>, StateDao<Volume.S
* Updates the disk offering for the given volume.
*/
void updateDiskOffering(long volumeId, long diskOfferingId);

List<VolumeVO> findByInstanceIdIncludingRemoved(long instanceId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -736,4 +736,11 @@ public void updateDiskOffering(long volumeId, long diskOfferingId) {
throw new CloudRuntimeException(e);
}
}

@Override
public List<VolumeVO> findByInstanceIdIncludingRemoved(long instanceId) {
SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
sc.setParameters("instanceId", instanceId);
return listIncludingRemovedBy(sc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
import java.util.function.Function;
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;

import static com.cloud.storage.Volume.Event.OperationRetryFromError;

public class VolumeObject implements VolumeInfo {
private static final Logger s_logger = Logger.getLogger(VolumeObject.class);
protected VolumeVO volumeVO;
Expand Down Expand Up @@ -213,7 +215,7 @@ public long getVolumeId() {
public boolean stateTransit(Volume.Event event) {
boolean result = false;
try {
volumeVO = volumeDao.findById(volumeVO.getId());
volumeVO = event != OperationRetryFromError ? volumeDao.findById(volumeVO.getId()) : volumeDao.findByIdIncludingRemoved(volumeVO.getId());
if (volumeVO != null) {
result = _volStateMachine.transitTo(volumeVO, event, null, volumeDao);
volumeVO = volumeDao.findById(volumeVO.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@
import org.apache.cloudstack.api.command.user.vm.ResetVMPasswordCmd;
import org.apache.cloudstack.api.command.user.vm.ResetVMSSHKeyCmd;
import org.apache.cloudstack.api.command.user.vm.RestoreVMCmd;
import org.apache.cloudstack.api.command.user.vm.RetryDeployVMCmd;
import org.apache.cloudstack.api.command.user.vm.ScaleVMCmd;
import org.apache.cloudstack.api.command.user.vm.StartVMCmd;
import org.apache.cloudstack.api.command.user.vm.StopVMCmd;
Expand Down Expand Up @@ -3552,6 +3553,7 @@ public List<Class<?>> getCommands() {
cmdList.add(IssueOutOfBandManagementPowerActionCmd.class);
cmdList.add(ChangeOutOfBandManagementPasswordCmd.class);
cmdList.add(GetUserKeysCmd.class);
cmdList.add(RetryDeployVMCmd.class);
return cmdList;
}

Expand Down
42 changes: 41 additions & 1 deletion server/src/main/java/com/cloud/vm/UserVmManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import org.apache.cloudstack.api.command.user.vm.ResetVMPasswordCmd;
import org.apache.cloudstack.api.command.user.vm.ResetVMSSHKeyCmd;
import org.apache.cloudstack.api.command.user.vm.RestoreVMCmd;
import org.apache.cloudstack.api.command.user.vm.RetryDeployVMCmd;
import org.apache.cloudstack.api.command.user.vm.ScaleVMCmd;
import org.apache.cloudstack.api.command.user.vm.SecurityGroupAction;
import org.apache.cloudstack.api.command.user.vm.StartVMCmd;
Expand Down Expand Up @@ -5145,7 +5146,7 @@ public Pair<UserVmVO, Map<VirtualMachineProfile.Param, Object>> startVirtualMach
throw new InvalidParameterValueException("unable to find a virtual machine with id " + vmId);
}

if (vm.getState()== State.Running) {
if (vm.getState() == State.Running) {
throw new InvalidParameterValueException("The virtual machine "+ vm.getUuid()+ " ("+ vm.getDisplayName()+ ") is already running");
}

Expand Down Expand Up @@ -7914,6 +7915,45 @@ public boolean unmanageUserVM(Long vmId) {
return true;
}

@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_START, eventDescription = "Retry Starting VM", async = true)
public UserVm retryDeployVM(RetryDeployVMCmd cmd) throws ResourceUnavailableException, InsufficientCapacityException, ResourceAllocationException {
UserVmVO vmVO = _vmDao.findById(cmd.getId());
if (vmVO == null) {
throw new CloudRuntimeException("Cannot find a VM with ID: " + vmId);
}

if (vmVO.getState() != State.Error) {
throw new CloudRuntimeException("To retry a VM deployment, the VM must be on Error state");
}

long vmId = vmVO.getId();
Long podId = null;
Long clusterId = null;
Long hostId = null;
Map<VirtualMachineProfile.Param, Object> additionalParams = new HashMap<>();
Map<Long, DiskOffering> diskOfferingMap = null;
String deploymentPlannerToUse = null;

List<VolumeVO> instanceVolumes = _volsDao.findByInstanceIdIncludingRemoved(vmId);
Transaction.execute(new TransactionCallbackNoReturn() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) {
for (VolumeVO vol : instanceVolumes) {
VolumeInfo volume = volFactory.getVolume(vol.getId());
volume.stateTransit(Volume.Event.OperationRetryFromError);
}
List<NicVO> nics = _nicDao.listByVmIdIncludingRemoved(vmId);
for (NicVO nic : nics) {
nic.setState(Nic.State.Allocated);
_nicDao.update(nic.getId(), nic);
}
}
});

return startVirtualMachine(vmId, podId, clusterId, hostId, diskOfferingMap, additionalParams, deploymentPlannerToUse);
}

/*
Generate usage events related to unmanaging a VM
*/
Expand Down
1 change: 1 addition & 0 deletions ui/public/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@
"label.action.resource.limits": "Resource limits",
"label.action.restore.instance": "Restore Instance",
"label.action.restore.instance.processing": "Restoring Instance....",
"label.action.retry.deploy.instance": "Retry Deploy Instance",
"label.action.revert.snapshot": "Revert to Snapshot",
"label.action.revert.snapshot.processing": "Reverting to Snapshot...",
"label.action.router.health.checks": "Get health checks result",
Expand Down
10 changes: 10 additions & 0 deletions ui/src/config/section/compute.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ export default {
popup: true,
component: () => import('@/views/compute/EditVM.vue')
},
{
api: 'retryDeployVirtualMachine',
icon: 'redo',
label: 'label.action.retry.deploy.instance',
dataView: true,
listView: false,
groupAction: true,
groupMap: (selection) => { return selection.map(x => { return { id: x } }) },
show: (record) => { return ['Error'].includes(record.state) }
},
{
api: 'startVirtualMachine',
icon: 'caret-right',
Expand Down