Skip to content
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 .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ env:
matrix:
# Keep the TESTS sorted by name and grouped by type
- TESTS="smoke/test_certauthority_root"

- TESTS="smoke/test_accounts
smoke/test_affinity_groups
smoke/test_affinity_groups_projects
Expand All @@ -43,6 +43,7 @@ env:
smoke/test_deploy_vm_root_resize
smoke/test_deploy_vm_with_userdata
smoke/test_deploy_vms_with_varied_deploymentplanners
smoke/test_diagnostics
smoke/test_disk_offerings
smoke/test_dynamicroles
smoke/test_global_settings
Expand Down
5 changes: 5 additions & 0 deletions api/src/main/java/org/apache/cloudstack/api/ApiConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,11 @@ public class ApiConstants {
public static final String LAST_ANNOTATED = "lastannotated";
public static final String LDAP_DOMAIN = "ldapdomain";

public static final String STDOUT = "stdout";
public static final String STDERR = "stderr";
public static final String EXITCODE = "exitcode";
public static final String TARGET_ID = "targetid";

public enum HostDetails {
all, capacity, events, stats, min;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// 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.admin.diagnostics;

import com.cloud.exception.InsufficientCapacityException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.user.Account;
import com.cloud.vm.VirtualMachine;
import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiArgValidator;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.ApiErrorCode;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.RunDiagnosticsResponse;
import org.apache.cloudstack.api.response.SystemVmResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.diagnostics.DiagnosticsService;
import org.apache.cloudstack.diagnostics.DiagnosticsType;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;

import javax.inject.Inject;
import java.util.Collections;
import java.util.Map;

@APICommand(name = RunDiagnosticsCmd.APINAME, responseObject = RunDiagnosticsResponse.class, entityType = {VirtualMachine.class},
responseHasSensitiveInfo = false,
requestHasSensitiveInfo = false,
description = "Execute network-utility command (ping/arping/tracert) on system VMs remotely",
authorized = {RoleType.Admin},
since = "4.12.0.0")
public class RunDiagnosticsCmd extends BaseCmd {
private static final Logger LOGGER = Logger.getLogger(RunDiagnosticsCmd.class);
public static final String APINAME = "runDiagnostics";

@Inject
private DiagnosticsService diagnosticsService;

/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////
@Parameter(name = ApiConstants.TARGET_ID, type = CommandType.UUID, required = true, entityType = SystemVmResponse.class,
validations = {ApiArgValidator.PositiveNumber},
description = "The ID of the system VM instance to diagnose")
private Long id;

@Parameter(name = ApiConstants.IP_ADDRESS, type = CommandType.STRING, required = true,
validations = {ApiArgValidator.NotNullOrEmpty},
description = "The IP/Domain address to test connection to")
private String address;

@Parameter(name = ApiConstants.TYPE, type = CommandType.STRING, required = true,
validations = {ApiArgValidator.NotNullOrEmpty},
description = "The system VM diagnostics type valid options are: ping, traceroute, arping")
private String type;

@Parameter(name = ApiConstants.PARAMS, type = CommandType.STRING,
authorized = {RoleType.Admin},
description = "Additional command line options that apply for each command")
private String optionalArguments;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
public Long getId() {
return id;
}

public String getAddress() {
return address;
}

public DiagnosticsType getType() {
DiagnosticsType diagnosticsType = DiagnosticsType.getCommand(type);
if (diagnosticsType == null) {
throw new IllegalArgumentException(type + " Is not a valid diagnostics command type. ");
}
return diagnosticsType;
}

public String getOptionalArguments() {
return optionalArguments;
}

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

@Override
public long getEntityOwnerId() {
Account account = CallContext.current().getCallingAccount();
if (account != null) {
return account.getId();
}
return Account.ACCOUNT_ID_SYSTEM;
}

@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException {
RunDiagnosticsResponse response = new RunDiagnosticsResponse();
try {
final Map<String, String> answerMap = diagnosticsService.runDiagnosticsCommand(this);
if (CollectionUtils.isNotEmpty(Collections.singleton(answerMap))) {
response.setStdout(answerMap.get(ApiConstants.STDOUT));
response.setStderr(answerMap.get(ApiConstants.STDERR));
response.setExitCode(answerMap.get(ApiConstants.EXITCODE));
response.setObjectName("diagnostics");
response.setResponseName(getCommandName());
this.setResponseObject(response);
}
} catch (final ServerApiException e) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// 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.response;

import com.cloud.serializer.Param;
import com.cloud.vm.VirtualMachine;
import com.google.gson.annotations.SerializedName;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseResponse;
import org.apache.cloudstack.api.EntityReference;

@EntityReference(value = VirtualMachine.class)
public class RunDiagnosticsResponse extends BaseResponse {
@SerializedName(ApiConstants.STDOUT)
@Param(description = "the standard output from the command execution")
private String stdout;

@SerializedName(ApiConstants.STDERR)
@Param(description = "the standard error output from the command execution")
private String stderr;

@SerializedName(ApiConstants.EXITCODE)
@Param(description = "the command execution return code")
private String exitCode;

public String getStdout() {
return stdout;
}

public void setStdout(String stdout) {
this.stdout = stdout;
}

public String getStderr() {
return stderr;
}

public void setStderr(String stderr) {
this.stderr = stderr;
}

public String getExitCode() {
return exitCode;
}

public void setExitCode(String exitCode) {
this.exitCode = exitCode;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// 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.diagnostics;

import org.apache.cloudstack.api.command.admin.diagnostics.RunDiagnosticsCmd;

import java.util.Map;

public interface DiagnosticsService {

Map<String, String> runDiagnosticsCommand(RunDiagnosticsCmd cmd);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// 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.diagnostics;

public enum DiagnosticsType {
PING("ping"), TRACEROUTE("traceroute"), ARPING("arping");

private String value;

DiagnosticsType(String value) {
this.value = value;
}

public String getValue() {
return value;
}

public static DiagnosticsType getCommand(String cmd) {
for (DiagnosticsType type : DiagnosticsType.values()) {
if (type.value.equalsIgnoreCase(cmd)) {
return type;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ public class VRScripts {

public static final String VR_CFG = "vr_cfg.sh";

public static final String DIAGNOSTICS = "diagnostics.py";
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;

import org.apache.cloudstack.diagnostics.DiagnosticsAnswer;
import org.apache.cloudstack.diagnostics.DiagnosticsCommand;
import org.joda.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -189,9 +192,11 @@ private Answer executeQueryCommand(NetworkElementCommand cmd) {
} else if (cmd instanceof GetDomRVersionCmd) {
return execute((GetDomRVersionCmd)cmd);
} else if (cmd instanceof CheckS2SVpnConnectionsCommand) {
return execute((CheckS2SVpnConnectionsCommand) cmd);
return execute((CheckS2SVpnConnectionsCommand)cmd);
} else if (cmd instanceof GetRouterAlertsCommand) {
return execute((GetRouterAlertsCommand)cmd);
} else if (cmd instanceof DiagnosticsCommand) {
return execute((DiagnosticsCommand)cmd);
} else {
s_logger.error("Unknown query command in VirtualRoutingResource!");
return Answer.createUnsupportedCommandAnswer(cmd);
Expand Down Expand Up @@ -292,6 +297,15 @@ private Answer execute(CheckRouterCommand cmd) {
return new CheckRouterAnswer(cmd, result.getDetails(), true);
}

private Answer execute(DiagnosticsCommand cmd) {
_eachTimeout = Duration.standardSeconds(NumbersUtil.parseInt("60", 60));
final ExecutionResult result = _vrDeployer.executeInVR(cmd.getRouterAccessIp(), VRScripts.DIAGNOSTICS, cmd.getSrciptArguments(), _eachTimeout);
if (!result.isSuccess()) {
return new DiagnosticsAnswer(cmd, false, result.getDetails());
}
return new DiagnosticsAnswer(cmd, result.isSuccess(), result.getDetails());
}

private Answer execute(GetDomRVersionCmd cmd) {
final ExecutionResult result = _vrDeployer.executeInVR(cmd.getRouterAccessIp(), VRScripts.VERSION, null);
if (!result.isSuccess()) {
Expand Down Expand Up @@ -454,6 +468,6 @@ private Answer execute(AggregationControlCommand cmd) {
_vrAggregateCommandsSet.remove(routerName);
}
}
return new Answer(cmd, false, "Fail to recongize aggregation action " + action.toString());
return new Answer(cmd, false, "Fail to recognize aggregation action " + action.toString());
}
}
Loading