blob: f3bad610998e17a459e258af45bfcdeb9070a414 [file] [log] [blame]
//
// 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 com.cloud.resource;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.naming.ConfigurationException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import com.cloud.agent.IAgentControl;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.StartupCommand;
import com.cloud.utils.net.NetUtils;
import com.cloud.utils.script.Script;
public abstract class ServerResourceBase implements ServerResource {
private static final Logger s_logger = Logger.getLogger(ServerResourceBase.class);
protected String _name;
private ArrayList<String> _warnings = new ArrayList<String>();
private ArrayList<String> _errors = new ArrayList<String>();
protected NetworkInterface _publicNic;
protected NetworkInterface _privateNic;
protected NetworkInterface _storageNic;
protected NetworkInterface _storageNic2;
protected IAgentControl _agentControl;
@Override
public String getName() {
return _name;
}
protected String findScript(String script) {
return Script.findScript(getDefaultScriptsDir(), script);
}
protected abstract String getDefaultScriptsDir();
@Override
public boolean configure(final String name, Map<String, Object> params) throws ConfigurationException {
_name = name;
defineResourceNetworkInterfaces(params);
if (_privateNic == null) {
tryToAutoDiscoverResourcePrivateNetworkInterface();
}
String infos[] = NetUtils.getNetworkParams(_privateNic);
if (infos == null) {
s_logger.warn("Incorrect details for private Nic during initialization of ServerResourceBase");
return false;
}
params.put("host.ip", infos[0]);
params.put("host.mac.address", infos[1]);
return true;
}
protected void defineResourceNetworkInterfaces(Map<String, Object> params) {
String privateNic = (String) params.get("private.network.device");
privateNic = privateNic == null ? "xenbr0" : privateNic;
String publicNic = (String) params.get("public.network.device");
publicNic = publicNic == null ? "xenbr1" : publicNic;
String storageNic = (String) params.get("storage.network.device");
String storageNic2 = (String) params.get("storage.network.device.2");
_privateNic = NetUtils.getNetworkInterface(privateNic);
_publicNic = NetUtils.getNetworkInterface(publicNic);
_storageNic = NetUtils.getNetworkInterface(storageNic);
_storageNic2 = NetUtils.getNetworkInterface(storageNic2);
}
protected void tryToAutoDiscoverResourcePrivateNetworkInterface() throws ConfigurationException {
s_logger.info("Trying to autodiscover this resource's private network interface.");
List<NetworkInterface> nics;
try {
nics = Collections.list(NetworkInterface.getNetworkInterfaces());
if (CollectionUtils.isEmpty(nics)) {
throw new ConfigurationException("This resource has no NICs. Unable to configure it.");
}
} catch (SocketException e) {
throw new ConfigurationException(String.format("Could not retrieve the environment NICs due to [%s].", e.getMessage()));
}
s_logger.debug(String.format("Searching the private NIC along the environment NICs [%s].", Arrays.toString(nics.toArray())));
for (NetworkInterface nic : nics) {
if (isValidNicToUseAsPrivateNic(nic)) {
s_logger.info(String.format("Using NIC [%s] as private NIC.", nic));
_privateNic = nic;
return;
}
}
throw new ConfigurationException("It was not possible to define a private NIC for this resource.");
}
protected boolean isValidNicToUseAsPrivateNic(NetworkInterface nic) {
String nicName = nic.getName();
s_logger.debug(String.format("Verifying if NIC [%s] can be used as private NIC.", nic));
String[] nicNameStartsToAvoid = {"vnif", "vnbr", "peth", "vif", "virbr"};
if (nic.isVirtual() || StringUtils.startsWithAny(nicName, nicNameStartsToAvoid) || nicName.contains(":")) {
s_logger.debug(String.format("Not using NIC [%s] because it is either virtual, starts with %s, or contains \":\"" +
" in its name.", Arrays.toString(nicNameStartsToAvoid), nic));
return false;
}
String[] info = NetUtils.getNicParams(nicName);
if (info == null || info[0] == null) {
s_logger.debug(String.format("Not using NIC [%s] because it does not have a valid IP to use as the private IP.", nic));
return false;
}
return true;
}
protected void fillNetworkInformation(final StartupCommand cmd) {
String[] info = null;
if (_privateNic != null) {
info = NetUtils.getNetworkParams(_privateNic);
if (info != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Parameters for private nic: " + info[0] + " - " + info[1] + "-" + info[2]);
}
cmd.setPrivateIpAddress(info[0]);
cmd.setPrivateMacAddress(info[1]);
cmd.setPrivateNetmask(info[2]);
}
}
if (_storageNic != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Storage has its now nic: " + _storageNic.getName());
}
info = NetUtils.getNetworkParams(_storageNic);
}
// NOTE: In case you're wondering, this is not here by mistake.
if (info != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Parameters for storage nic: " + info[0] + " - " + info[1] + "-" + info[2]);
}
cmd.setStorageIpAddress(info[0]);
cmd.setStorageMacAddress(info[1]);
cmd.setStorageNetmask(info[2]);
}
if (_publicNic != null) {
info = NetUtils.getNetworkParams(_publicNic);
if (info != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Parameters for public nic: " + info[0] + " - " + info[1] + "-" + info[2]);
}
cmd.setPublicIpAddress(info[0]);
cmd.setPublicMacAddress(info[1]);
cmd.setPublicNetmask(info[2]);
}
}
if (_storageNic2 != null) {
info = NetUtils.getNetworkParams(_storageNic2);
if (info != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Parameters for storage nic 2: " + info[0] + " - " + info[1] + "-" + info[2]);
}
cmd.setStorageIpAddressDeux(info[0]);
cmd.setStorageMacAddressDeux(info[1]);
cmd.setStorageNetmaskDeux(info[2]);
}
}
}
@Override
public void disconnected() {
}
@Override
public IAgentControl getAgentControl() {
return _agentControl;
}
@Override
public void setAgentControl(IAgentControl agentControl) {
_agentControl = agentControl;
}
protected void recordWarning(final String msg, final Throwable th) {
final String str = getLogStr(msg, th);
synchronized (_warnings) {
_warnings.add(str);
}
}
protected void recordWarning(final String msg) {
recordWarning(msg, null);
}
protected List<String> getWarnings() {
synchronized (_warnings) {
final List<String> results = new LinkedList<String>(_warnings);
_warnings.clear();
return results;
}
}
protected List<String> getErrors() {
synchronized (_errors) {
final List<String> result = new LinkedList<String>(_errors);
_errors.clear();
return result;
}
}
protected void recordError(final String msg, final Throwable th) {
final String str = getLogStr(msg, th);
synchronized (_errors) {
_errors.add(str);
}
}
protected void recordError(final String msg) {
recordError(msg, null);
}
protected Answer createErrorAnswer(final Command cmd, final String msg, final Throwable th) {
final StringWriter writer = new StringWriter();
if (msg != null) {
writer.append(msg);
}
writer.append("===>Stack<===");
th.printStackTrace(new PrintWriter(writer));
return new Answer(cmd, false, writer.toString());
}
protected String createErrorDetail(final String msg, final Throwable th) {
final StringWriter writer = new StringWriter();
if (msg != null) {
writer.append(msg);
}
writer.append("===>Stack<===");
th.printStackTrace(new PrintWriter(writer));
return writer.toString();
}
protected String getLogStr(final String msg, final Throwable th) {
final StringWriter writer = new StringWriter();
writer.append(new Date().toString()).append(": ").append(msg);
if (th != null) {
writer.append("\n Exception: ");
th.printStackTrace(new PrintWriter(writer));
}
return writer.toString();
}
@Override
public boolean start() {
return true;
}
@Override
public boolean stop() {
return true;
}
}