|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package org.apache.cloudstack.outofbandmanagement.driver.nestedcloudstack; |
| 18 | + |
| 19 | +import br.com.autonomiccs.apacheCloudStack.client.ApacheCloudStackClient; |
| 20 | +import br.com.autonomiccs.apacheCloudStack.client.ApacheCloudStackRequest; |
| 21 | +import br.com.autonomiccs.apacheCloudStack.client.beans.ApacheCloudStackUser; |
| 22 | +import br.com.autonomiccs.apacheCloudStack.exceptions.ApacheCloudStackClientRequestRuntimeException; |
| 23 | +import com.cloud.utils.component.AdapterBase; |
| 24 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 25 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 26 | +import com.google.common.base.Strings; |
| 27 | +import com.google.common.collect.ImmutableMap; |
| 28 | +import org.apache.cloudstack.outofbandmanagement.OutOfBandManagement; |
| 29 | +import org.apache.cloudstack.outofbandmanagement.OutOfBandManagementDriver; |
| 30 | +import org.apache.cloudstack.outofbandmanagement.driver.OutOfBandManagementDriverChangePasswordCommand; |
| 31 | +import org.apache.cloudstack.outofbandmanagement.driver.OutOfBandManagementDriverCommand; |
| 32 | +import org.apache.cloudstack.outofbandmanagement.driver.OutOfBandManagementDriverPowerCommand; |
| 33 | +import org.apache.cloudstack.outofbandmanagement.driver.OutOfBandManagementDriverResponse; |
| 34 | +import org.apache.log4j.Logger; |
| 35 | + |
| 36 | +import java.io.IOException; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +public final class NestedCloudStackOutOfBandManagementDriver extends AdapterBase implements OutOfBandManagementDriver { |
| 41 | + private static final Logger LOG = Logger.getLogger(NestedCloudStackOutOfBandManagementDriver.class); |
| 42 | + |
| 43 | + public OutOfBandManagementDriverResponse execute(final OutOfBandManagementDriverCommand cmd) { |
| 44 | + OutOfBandManagementDriverResponse response = new OutOfBandManagementDriverResponse(null, "Unsupported Command", false); |
| 45 | + |
| 46 | + if (cmd instanceof OutOfBandManagementDriverPowerCommand) { |
| 47 | + response = execute((OutOfBandManagementDriverPowerCommand) cmd); |
| 48 | + } else if (cmd instanceof OutOfBandManagementDriverChangePasswordCommand) { |
| 49 | + throw new CloudRuntimeException("Change password operation is not supported by the nested-cloudstack out-of-band management driver"); |
| 50 | + } |
| 51 | + |
| 52 | + return response; |
| 53 | + } |
| 54 | + |
| 55 | + protected void ensureOptionExists(final ImmutableMap<OutOfBandManagement.Option, String> options, final OutOfBandManagement.Option option) { |
| 56 | + if (options != null && option != null && options.containsKey(option) && !Strings.isNullOrEmpty(options.get(option))) { |
| 57 | + return; |
| 58 | + } |
| 59 | + throw new CloudRuntimeException("Invalid out-of-band management configuration detected for the nested-cloudstack driver"); |
| 60 | + } |
| 61 | + |
| 62 | + protected OutOfBandManagement.PowerState getNestedVMPowerState(final String jsonResponse) { |
| 63 | + if (Strings.isNullOrEmpty(jsonResponse)) { |
| 64 | + return OutOfBandManagement.PowerState.Unknown; |
| 65 | + } |
| 66 | + |
| 67 | + final ObjectMapper mapper = new ObjectMapper(); |
| 68 | + try { |
| 69 | + Map<String, Object> listResponse = mapper.readValue(jsonResponse, Map.class); |
| 70 | + if (listResponse != null && listResponse.containsKey("listvirtualmachinesresponse") |
| 71 | + && ((Map<String, Object>) listResponse.get("listvirtualmachinesresponse")).containsKey("virtualmachine")) { |
| 72 | + Map<String, String> vmResponse = ((Map<String, List<Map<String, String>>>) listResponse.get("listvirtualmachinesresponse")).get("virtualmachine").get(0); |
| 73 | + if (vmResponse != null && vmResponse.containsKey("state")) { |
| 74 | + if("Running".equals(vmResponse.get("state"))) { |
| 75 | + return OutOfBandManagement.PowerState.On; |
| 76 | + } else if("Stopped".equals(vmResponse.get("state"))) { |
| 77 | + return OutOfBandManagement.PowerState.Off; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } catch (IOException e) { |
| 82 | + LOG.warn("Exception caught while de-serializing and reading state of the nested-cloudstack VM from the response: " + jsonResponse + ", with exception:", e); |
| 83 | + } |
| 84 | + return OutOfBandManagement.PowerState.Unknown; |
| 85 | + } |
| 86 | + |
| 87 | + private OutOfBandManagementDriverResponse execute(final OutOfBandManagementDriverPowerCommand cmd) { |
| 88 | + if (cmd == null || cmd.getPowerOperation() == null) { |
| 89 | + throw new CloudRuntimeException("Invalid out-of-band management power command provided to the nested-cloudstack driver"); |
| 90 | + } |
| 91 | + |
| 92 | + final ImmutableMap<OutOfBandManagement.Option, String> options = cmd.getOptions(); |
| 93 | + ensureOptionExists(options, OutOfBandManagement.Option.ADDRESS); |
| 94 | + ensureOptionExists(options, OutOfBandManagement.Option.PORT); |
| 95 | + ensureOptionExists(options, OutOfBandManagement.Option.USERNAME); |
| 96 | + ensureOptionExists(options, OutOfBandManagement.Option.PASSWORD); |
| 97 | + |
| 98 | + final String url = options.get(OutOfBandManagement.Option.ADDRESS); |
| 99 | + final String vmUuid = options.get(OutOfBandManagement.Option.PORT); |
| 100 | + final String apiKey = options.get(OutOfBandManagement.Option.USERNAME); |
| 101 | + final String secretKey = options.get(OutOfBandManagement.Option.PASSWORD); |
| 102 | + |
| 103 | + final ApacheCloudStackUser apacheCloudStackUser = new ApacheCloudStackUser(secretKey, apiKey); |
| 104 | + final ApacheCloudStackClient client = new ApacheCloudStackClient(url, apacheCloudStackUser); |
| 105 | + client.setValidateServerHttpsCertificate(false); |
| 106 | + client.setShouldRequestsExpire(false); |
| 107 | + client.setConnectionTimeout((int) cmd.getTimeout().getStandardSeconds()); |
| 108 | + |
| 109 | + String apiName = "listVirtualMachines"; |
| 110 | + switch (cmd.getPowerOperation()) { |
| 111 | + case ON: |
| 112 | + apiName = "startVirtualMachine"; |
| 113 | + break; |
| 114 | + case OFF: |
| 115 | + case SOFT: |
| 116 | + apiName = "stopVirtualMachine"; |
| 117 | + break; |
| 118 | + case CYCLE: |
| 119 | + case RESET: |
| 120 | + apiName = "rebootVirtualMachine"; |
| 121 | + break; |
| 122 | + } |
| 123 | + |
| 124 | + final ApacheCloudStackRequest apacheCloudStackRequest = new ApacheCloudStackRequest(apiName); |
| 125 | + apacheCloudStackRequest.addParameter("response", "json"); |
| 126 | + apacheCloudStackRequest.addParameter("forced", "true"); |
| 127 | + apacheCloudStackRequest.addParameter("id", vmUuid); |
| 128 | + |
| 129 | + final String apiResponse; |
| 130 | + try { |
| 131 | + apiResponse = client.executeRequest(apacheCloudStackRequest); |
| 132 | + } catch (final ApacheCloudStackClientRequestRuntimeException e) { |
| 133 | + LOG.error("Nested CloudStack oobm plugin failed due to API error: ", e); |
| 134 | + final OutOfBandManagementDriverResponse failedResponse = new OutOfBandManagementDriverResponse(e.getResponse(), "HTTP error code: " + e.getStatusCode(), false); |
| 135 | + if (e.getStatusCode() == 401) { |
| 136 | + failedResponse.setAuthFailure(true); |
| 137 | + } |
| 138 | + return failedResponse; |
| 139 | + } |
| 140 | + |
| 141 | + final OutOfBandManagementDriverResponse response = new OutOfBandManagementDriverResponse(apiResponse, null, true); |
| 142 | + if (OutOfBandManagement.PowerOperation.STATUS.equals(cmd.getPowerOperation())) { |
| 143 | + response.setPowerState(getNestedVMPowerState(apiResponse)); |
| 144 | + } |
| 145 | + return response; |
| 146 | + } |
| 147 | +} |
0 commit comments