Start of zonesfeature / mycloud/cloudkit

This commit is contained in:
Chiradeep Vittal 2011-03-11 17:00:52 -08:00
parent 76a30cc76f
commit 303e2a7481
155 changed files with 10512 additions and 3725 deletions

View File

@ -7,6 +7,7 @@
<classpathentry combineaccessrules="false" kind="src" path="/core"/>
<classpathentry combineaccessrules="false" kind="src" path="/api"/>
<classpathentry combineaccessrules="false" kind="src" path="/deps"/>
<classpathentry combineaccessrules="false" kind="src" path="/thirdparty"/>
<classpathentry combineaccessrules="false" kind="src" path="/tools"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -533,10 +533,14 @@ public class AgentShell implements IAgentShell {
instance = "";
} else {
instance += ".";
}
}
String pidDir = getProperty(null, "piddir");
final String run = "agent." + instance + "pid";
s_logger.debug("Checking to see if " + run + "exists.");
ProcessUtil.pidCheck(run);
ProcessUtil.pidCheck(pidDir, run);
launchAgent();

View File

@ -0,0 +1,73 @@
/**
* Copyright (C) 2011 Cloud.com. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later
version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.agent.configuration;
import java.util.List;
import java.util.Map;
import com.cloud.utils.component.Adapter;
import com.cloud.utils.component.ComponentLibraryBase;
import com.cloud.utils.component.ComponentLocator.ComponentInfo;
import com.cloud.utils.component.Manager;
import com.cloud.utils.component.SystemIntegrityChecker;
import com.cloud.utils.db.GenericDao;
public class AgentComponentLibraryBase extends ComponentLibraryBase {
@Override
public List<SystemIntegrityChecker> getSystemIntegrityCheckers() {
return null;
}
@Override
public Map<String, ComponentInfo<GenericDao<?, ?>>> getDaos() {
return null;
}
@Override
public Map<String, ComponentInfo<Manager>> getManagers() {
if (_managers.size() == 0) {
populateManagers();
}
return _managers;
}
@Override
public Map<String, List<ComponentInfo<Adapter>>> getAdapters() {
if (_adapters.size() == 0) {
populateAdapters();
}
return _adapters;
}
@Override
public Map<Class<?>, Class<?>> getFactories() {
return null;
}
protected void populateManagers() {
//addManager("StackMaidManager", StackMaidManagerImpl.class);
}
protected void populateAdapters() {
}
}

View File

@ -0,0 +1,41 @@
/**
* Copyright (C) 2011 Cloud.com, Inc. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.agent.dhcp;
import java.net.InetAddress;
import java.util.List;
import java.util.Map;
import com.cloud.utils.Pair;
import com.cloud.utils.component.Adapter;
public interface DhcpSnooper extends Adapter{
public InetAddress getIPAddr(String macAddr, String vmName);
public InetAddress getDhcpServerIP();
public void cleanup(String macAddr, String vmName);
public Map<String, InetAddress> syncIpAddr();
public boolean stop();
public void initializeMacTable(List<Pair<String, String>> macVmNameList);
}

View File

@ -0,0 +1,124 @@
package com.cloud.agent.dhcp;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import javax.ejb.Local;
import javax.naming.ConfigurationException;
import org.apache.log4j.Logger;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.Command;
import com.cloud.utils.Pair;
import com.cloud.utils.net.NetUtils;
@Local(value = {DhcpSnooper.class})
public class FakeDhcpSnooper implements DhcpSnooper {
private static final Logger s_logger = Logger.getLogger(FakeDhcpSnooper.class);
private Queue<String> _ipAddresses = new ConcurrentLinkedQueue<String>();
private Map<String, String> _macIpMap = new ConcurrentHashMap<String, String>();
private Map<String, InetAddress> _vmIpMap = new ConcurrentHashMap<String, InetAddress>();
@Override
public boolean configure(String name, Map<String, Object> params)
throws ConfigurationException {
String guestIpRange = (String)params.get("guest.ip.range");
if (guestIpRange != null) {
String [] guestIps = guestIpRange.split("-");
if (guestIps.length == 2) {
long start = NetUtils.ip2Long(guestIps[0]);
long end = NetUtils.ip2Long(guestIps[1]);
while (start <= end) {
_ipAddresses.offer(NetUtils.long2Ip(start++));
}
}
}
return true;
}
@Override
public boolean start() {
return true;
}
@Override
public String getName() {
return "FakeDhcpSnooper";
}
@Override
public InetAddress getIPAddr(String macAddr, String vmName) {
String ipAddr = _ipAddresses.poll();
if (ipAddr == null) {
s_logger.warn("No ip addresses left in queue");
return null;
}
try {
InetAddress inetAddr = InetAddress.getByName(ipAddr);
_macIpMap.put(macAddr.toLowerCase(), ipAddr);
_vmIpMap.put(vmName, inetAddr);
s_logger.info("Got ip address " + ipAddr + " for vm " + vmName + " mac=" + macAddr.toLowerCase());
return inetAddr;
} catch (UnknownHostException e) {
s_logger.warn("Failed to get InetAddress for " + ipAddr);
return null;
}
}
@Override
public void cleanup(String macAddr, String vmName) {
try {
if (macAddr == null) {
return;
}
InetAddress inetAddr = _vmIpMap.remove(vmName);
String ipAddr = inetAddr.getHostName();
for (Map.Entry<String, String> entry: _macIpMap.entrySet()) {
if (entry.getValue().equalsIgnoreCase(ipAddr)){
macAddr = entry.getKey();
break;
}
}
ipAddr = _macIpMap.remove(macAddr);
s_logger.info("Cleaning up for mac address: " + macAddr + " ip=" + ipAddr + " inetAddr=" + inetAddr);
if (ipAddr != null) {
_ipAddresses.offer(ipAddr);
}
} catch (Exception e) {
s_logger.debug("Failed to cleanup: " + e.toString());
}
}
@Override
public Map<String, InetAddress> syncIpAddr() {
return _vmIpMap;
}
@Override
public boolean stop() {
return false;
}
@Override
public void initializeMacTable(List<Pair<String, String>> macVmNameList) {
}
@Override
public InetAddress getDhcpServerIP() {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -0,0 +1,75 @@
/**
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.agent.mockvm;
import com.cloud.vm.VirtualMachine.State;
// As storage is mapped from storage device, can virtually treat that VM here does
// not need any local storage resource, therefore we don't have attribute here for storage
public class MockVm {
private String vmName;
private State state = State.Stopped;
private long ramSize; // unit of Mbytes
private int cpuCount;
private int utilization; // in percentage
private int vncPort; // 0-based allocation, real port number needs to be applied with base
public MockVm() {
}
public MockVm(String vmName, State state, long ramSize, int cpuCount, int utilization, int vncPort) {
this.vmName = vmName;
this.state = state;
this.ramSize = ramSize;
this.cpuCount = cpuCount;
this.utilization = utilization;
this.vncPort = vncPort;
}
public String getName() {
return vmName;
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
public long getRamSize() {
return ramSize;
}
public int getCpuCount() {
return cpuCount;
}
public int getUtilization() {
return utilization;
}
public int getVncPort() {
return vncPort;
}
}

View File

@ -0,0 +1,319 @@
/**
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.agent.mockvm;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import org.apache.log4j.Logger;
import com.cloud.agent.api.to.VirtualMachineTO;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.vm.VirtualMachine.State;
public class MockVmMgr implements VmMgr {
private static final Logger s_logger = Logger.getLogger(MockVmMgr.class);
private static final int DEFAULT_DOM0_MEM_MB = 128;
private static final Random randSeed = new Random();
private final Map<String, MockVm> vms = new HashMap<String, MockVm>();
private long vncPortMap = 0;
private Map<String, Object> _params = null;
public MockVmMgr() {
}
@Override
public Set<String> getCurrentVMs() {
HashSet<String> vmNameSet = new HashSet<String>();
synchronized(this) {
for(String vmName : vms.keySet())
vmNameSet.add(vmName);
}
return vmNameSet;
}
@Override
public String startVM(String vmName, String vnetId, String gateway, String dns,
String privateIP, String privateMac, String privateMask,
String publicIP, String publicMac, String publicMask,
int cpuCount, int cpuUtilization, long ramSize,
String localPath, String vncPassword) {
if(s_logger.isInfoEnabled()) {
StringBuffer sb = new StringBuffer();
sb.append("Start VM. name: " + vmName + ", vnet: " + vnetId + ", dns: " + dns);
sb.append(", privateIP: " + privateIP + ", privateMac: " + privateMac + ", privateMask: " + privateMask);
sb.append(", publicIP: " + publicIP + ", publicMac: " + publicMac + ", publicMask: " + publicMask);
sb.append(", cpu count: " + cpuCount + ", cpuUtilization: " + cpuUtilization + ", ram : " + ramSize);
sb.append(", localPath: " + localPath);
s_logger.info(sb.toString());
}
synchronized(this) {
MockVm vm = vms.get(vmName);
if(vm == null) {
if(ramSize > getHostFreeMemory())
return "Out of memory";
int vncPort = allocVncPort();
if(vncPort < 0)
return "Unable to allocate VNC port";
vm = new MockVm(vmName, State.Running, ramSize, cpuCount, cpuUtilization,
vncPort);
vms.put(vmName, vm);
}
}
return null;
}
@Override
public String stopVM(String vmName, boolean force) {
if(s_logger.isInfoEnabled())
s_logger.info("Stop VM. name: " + vmName);
synchronized(this) {
MockVm vm = vms.get(vmName);
if(vm != null) {
vm.setState(State.Stopped);
freeVncPort(vm.getVncPort());
}
}
return null;
}
@Override
public String rebootVM(String vmName) {
if(s_logger.isInfoEnabled())
s_logger.info("Reboot VM. name: " + vmName);
synchronized(this) {
MockVm vm = vms.get(vmName);
if(vm != null)
vm.setState(State.Running);
}
return null;
}
@Override
public boolean migrate(String vmName, String params) {
if(s_logger.isInfoEnabled())
s_logger.info("Migrate VM. name: " + vmName);
synchronized(this) {
MockVm vm = vms.get(vmName);
if(vm != null) {
vm.setState(State.Stopped);
freeVncPort(vm.getVncPort());
vms.remove(vmName);
return true;
}
}
return false;
}
public MockVm getVm(String vmName) {
synchronized(this) {
MockVm vm = vms.get(vmName);
return vm;
}
}
@Override
public State checkVmState(String vmName) {
synchronized(this) {
MockVm vm = vms.get(vmName);
if(vm != null)
return vm.getState();
}
return State.Unknown;
}
@Override
public Map<String, State> getVmStates() {
Map<String, State> states = new HashMap<String, State>();
synchronized(this) {
for(MockVm vm : vms.values()) {
states.put(vm.getName(), vm.getState());
}
}
return states;
}
@Override
public void cleanupVM(String vmName, String local, String vnet) {
synchronized(this) {
MockVm vm = vms.get(vmName);
if(vm != null) {
freeVncPort(vm.getVncPort());
}
vms.remove(vmName);
}
}
@Override
public double getHostCpuUtilization() {
return 0.0d;
}
@Override
public int getHostCpuCount() {
return getConfiguredProperty("cpus", 4);
}
@Override
public long getHostCpuSpeed() {
return getConfiguredProperty("cpuspeed", 4000L) ;
}
@Override
public long getHostTotalMemory() { // total memory in bytes
return getConfiguredProperty("memory", 16000L);
}
@Override
public long getHostFreeMemory() { // free memory in bytes
long memSize = getHostTotalMemory();
memSize -= getHostDom0Memory();
synchronized(this) {
for(MockVm vm : vms.values()) {
if(vm.getState() != State.Stopped)
memSize -= vm.getRamSize();
}
}
return memSize;
}
@Override
public long getHostDom0Memory() { // memory size in bytes
return DEFAULT_DOM0_MEM_MB*1024*1024L;
}
@Override
public String cleanupVnet(String vnetId) {
return null;
}
@Override
public Integer getVncPort(String name) {
synchronized(this) {
MockVm vm = vms.get(name);
if(vm != null)
return vm.getVncPort();
}
return new Integer(-1);
}
public int allocVncPort() {
for(int i = 0; i < 64; i++) {
if( ((1L << i) & vncPortMap) == 0 ) {
vncPortMap |= (1L << i);
return i;
}
}
return -1;
}
public void freeVncPort(int port) {
vncPortMap &= ~(1L << port);
}
@Override
public MockVm createVmFromSpec(VirtualMachineTO vmSpec) {
String vmName = vmSpec.getName();
long ramSize = vmSpec.getMinRam();
int utilizationPercent = randSeed.nextInt() % 100;
MockVm vm = null;
synchronized(this) {
vm = vms.get(vmName);
if(vm == null) {
if (ramSize > getHostFreeMemory()) {
s_logger.debug("host is out of memory");
throw new CloudRuntimeException("Host is out of Memory");
}
int vncPort = allocVncPort();
if(vncPort < 0){
s_logger.debug("Unable to allocate VNC port");
throw new CloudRuntimeException("Unable to allocate vnc port");
}
vm = new MockVm(vmName, State.Running, ramSize, vmSpec.getCpus(), utilizationPercent, vncPort);
vms.put(vmName, vm);
}
}
return vm;
}
@Override
public void createVbd(VirtualMachineTO vmSpec, String vmName, MockVm vm) {
// TODO Auto-generated method stub
}
@Override
public void createVif(VirtualMachineTO vmSpec, String vmName, MockVm vm) {
// TODO Auto-generated method stub
}
@Override
public void configure(Map<String, Object> params) {
_params = params;
}
protected Long getConfiguredProperty(String key, Long defaultValue) {
String val = (String)_params.get(key);
if (val != null) {
Long result = Long.parseLong(val);
return result;
}
return defaultValue;
}
protected Integer getConfiguredProperty(String key, Integer defaultValue) {
String val = (String)_params.get(key);
if (val != null) {
Integer result = Integer.parseInt(val);
return result;
}
return defaultValue;
}
}

View File

@ -0,0 +1,64 @@
/**
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.agent.mockvm;
import java.util.Map;
import java.util.Set;
import com.cloud.agent.api.to.VirtualMachineTO;
import com.cloud.vm.VirtualMachine.State;
public interface VmMgr {
public Set<String> getCurrentVMs();
public String startVM(String vmName, String vnetId, String gateway, String dns,
String privateIP, String privateMac, String privateMask,
String publicIP, String publicMac, String publicMask,
int cpuCount, int cpuUtilization, long ramSize,
String localPath, String vncPassword);
public String stopVM(String vmName, boolean force);
public String rebootVM(String vmName);
public void cleanupVM(String vmName, String local, String vnet);
public boolean migrate(String vmName, String params);
public MockVm getVm(String vmName);
public State checkVmState(String vmName);
public Map<String, State> getVmStates();
public Integer getVncPort(String name);
public String cleanupVnet(String vnetId);
public double getHostCpuUtilization();
public int getHostCpuCount();
public long getHostCpuSpeed();
public long getHostTotalMemory();
public long getHostFreeMemory();
public long getHostDom0Memory();
public MockVm createVmFromSpec(VirtualMachineTO vmSpec);
public void createVbd(VirtualMachineTO vmSpec, String vmName, MockVm vm);
public void createVif(VirtualMachineTO vmSpec, String vmName, MockVm vm);
public void configure(Map<String, Object> params);
}

View File

@ -0,0 +1,652 @@
/**
* Copyright (C) 2011 Cloud.com, Inc. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.agent.resource.computing;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import javax.ejb.Local;
import javax.naming.ConfigurationException;
import org.apache.log4j.Logger;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.AttachIsoCommand;
import com.cloud.agent.api.AttachVolumeCommand;
import com.cloud.agent.api.CheckHealthAnswer;
import com.cloud.agent.api.CheckHealthCommand;
import com.cloud.agent.api.CheckStateAnswer;
import com.cloud.agent.api.CheckStateCommand;
import com.cloud.agent.api.CheckVirtualMachineAnswer;
import com.cloud.agent.api.CheckVirtualMachineCommand;
import com.cloud.agent.api.CleanupNetworkRulesCmd;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.GetHostStatsAnswer;
import com.cloud.agent.api.GetHostStatsCommand;
import com.cloud.agent.api.GetStorageStatsAnswer;
import com.cloud.agent.api.GetStorageStatsCommand;
import com.cloud.agent.api.GetVmStatsAnswer;
import com.cloud.agent.api.GetVmStatsCommand;
import com.cloud.agent.api.ModifySshKeysCommand;
import com.cloud.agent.api.ModifyStoragePoolAnswer;
import com.cloud.agent.api.ModifyStoragePoolCommand;
import com.cloud.agent.api.PingCommand;
import com.cloud.agent.api.PingRoutingCommand;
import com.cloud.agent.api.PingTestCommand;
import com.cloud.agent.api.ReadyAnswer;
import com.cloud.agent.api.ReadyCommand;
import com.cloud.agent.api.RebootAnswer;
import com.cloud.agent.api.RebootCommand;
import com.cloud.agent.api.SecurityIngressRuleAnswer;
import com.cloud.agent.api.SecurityIngressRulesCmd;
import com.cloud.agent.api.StartAnswer;
import com.cloud.agent.api.StartCommand;
import com.cloud.agent.api.StartupCommand;
import com.cloud.agent.api.StartupRoutingCommand;
import com.cloud.agent.api.StartupStorageCommand;
import com.cloud.agent.api.StopAnswer;
import com.cloud.agent.api.StopCommand;
import com.cloud.agent.api.StoragePoolInfo;
import com.cloud.agent.api.routing.SavePasswordCommand;
import com.cloud.agent.api.routing.VmDataCommand;
import com.cloud.agent.api.storage.CreateAnswer;
import com.cloud.agent.api.storage.CreateCommand;
import com.cloud.agent.api.storage.DestroyCommand;
import com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer;
import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand;
import com.cloud.agent.api.to.NicTO;
import com.cloud.agent.api.to.VirtualMachineTO;
import com.cloud.agent.api.to.VolumeTO;
import com.cloud.agent.dhcp.DhcpSnooper;
import com.cloud.agent.dhcp.FakeDhcpSnooper;
import com.cloud.agent.mockvm.MockVm;
import com.cloud.agent.mockvm.MockVmMgr;
import com.cloud.agent.mockvm.VmMgr;
import com.cloud.agent.vmdata.JettyVmDataServer;
import com.cloud.agent.vmdata.VmDataServer;
import com.cloud.host.Host.Type;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.network.Networks.RouterPrivateIpStrategy;
import com.cloud.network.Networks.TrafficType;
import com.cloud.resource.ServerResource;
import com.cloud.resource.ServerResourceBase;
import com.cloud.storage.Storage;
import com.cloud.storage.Storage.StoragePoolType;
import com.cloud.storage.Volume;
import com.cloud.storage.VolumeVO;
import com.cloud.storage.template.TemplateInfo;
import com.cloud.utils.PropertiesUtil;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.script.Script;
import com.cloud.vm.VirtualMachine.State;
/**
* Pretends to be a computing resource
*
*/
@Local(value={ServerResource.class})
public class FakeComputingResource extends ServerResourceBase implements ServerResource {
private static final Logger s_logger = Logger.getLogger(FakeComputingResource.class);
private Map<String, Object> _params;
private VmMgr _vmManager = new MockVmMgr();
protected HashMap<String, State> _vms = new HashMap<String, State>(20);
protected DhcpSnooper _dhcpSnooper = new FakeDhcpSnooper();
protected VmDataServer _vmDataServer = new JettyVmDataServer();
@Override
public Type getType() {
return Type.Routing;
}
@Override
public StartupCommand[] initialize() {
Map<String, State> changes = null;
final List<Object> info = getHostInfo();
final StartupRoutingCommand cmd = new StartupRoutingCommand((Integer)info.get(0), (Long)info.get(1), (Long)info.get(2), (Long)info.get(4), (String)info.get(3), HypervisorType.KVM, RouterPrivateIpStrategy.HostLocal, changes);
fillNetworkInformation(cmd);
cmd.getHostDetails().putAll(getVersionStrings());
cmd.setCluster(getConfiguredProperty("cluster", "1"));
StoragePoolInfo pi = initializeLocalStorage();
StartupStorageCommand sscmd = new StartupStorageCommand();
sscmd.setPoolInfo(pi);
sscmd.setGuid(pi.getUuid());
sscmd.setDataCenter((String)_params.get("zone"));
sscmd.setResourceType(Storage.StorageResourceType.STORAGE_POOL);
return new StartupCommand[]{cmd, sscmd};
}
private Map<String, String> getVersionStrings() {
Map<String, String> result = new HashMap<String, String>();
String hostOs = (String) _params.get("Host.OS");
String hostOsVer = (String) _params.get("Host.OS.Version");
String hostOsKernVer = (String) _params.get("Host.OS.Kernel.Version");
result.put("Host.OS", hostOs==null?"Fedora":hostOs);
result.put("Host.OS.Version", hostOsVer==null?"14":hostOsVer);
result.put("Host.OS.Kernel.Version", hostOsKernVer==null?"2.6.35.6-45.fc14.x86_64":hostOsKernVer);
return result;
}
protected void fillNetworkInformation(final StartupCommand cmd) {
cmd.setPrivateIpAddress((String)_params.get("private.ip.address"));
cmd.setPrivateMacAddress((String)_params.get("private.mac.address"));
cmd.setPrivateNetmask((String)_params.get("private.ip.netmask"));
cmd.setStorageIpAddress((String)_params.get("private.ip.address"));
cmd.setStorageMacAddress((String)_params.get("private.mac.address"));
cmd.setStorageNetmask((String)_params.get("private.ip.netmask"));
cmd.setGatewayIpAddress((String)_params.get("gateway.ip.address"));
}
protected StoragePoolInfo initializeLocalStorage() {
String hostIp = (String)_params.get("private.ip.address");
String localStoragePath = (String)_params.get("local.storage.path");
String lh = hostIp + localStoragePath;
String uuid = UUID.nameUUIDFromBytes(lh.getBytes()).toString();
String capacity = (String)_params.get("local.storage.capacity");
String available = (String)_params.get("local.storage.avail");
return new StoragePoolInfo(uuid, hostIp, localStoragePath,
localStoragePath, StoragePoolType.Filesystem,
Long.parseLong(capacity), Long.parseLong(available));
}
@Override
public PingCommand getCurrentStatus(long id) {
final HashMap<String, State> newStates = new HashMap<String, State>();
_dhcpSnooper.syncIpAddr();
return new PingRoutingCommand(com.cloud.host.Host.Type.Routing, id, newStates);
}
@Override
public Answer executeRequest(Command cmd) {
try {
if (cmd instanceof ReadyCommand) {
return execute((ReadyCommand)cmd);
}else if (cmd instanceof ModifySshKeysCommand) {
return execute((ModifySshKeysCommand)cmd);//TODO: remove
} else if (cmd instanceof GetHostStatsCommand) {
return execute((GetHostStatsCommand)cmd);
} else if (cmd instanceof PrimaryStorageDownloadCommand) {
return execute((PrimaryStorageDownloadCommand) cmd);
} else if (cmd instanceof StopCommand) {
return execute((StopCommand)cmd);
} else if (cmd instanceof GetVmStatsCommand) {
return execute((GetVmStatsCommand)cmd);
} else if (cmd instanceof RebootCommand) {
return execute((RebootCommand)cmd);
} else if (cmd instanceof CheckStateCommand) {
return executeRequest(cmd);
} else if (cmd instanceof CheckHealthCommand) {
return execute((CheckHealthCommand)cmd);
} else if (cmd instanceof PingTestCommand) {
return execute((PingTestCommand)cmd);
} else if (cmd instanceof CheckVirtualMachineCommand) {
return execute((CheckVirtualMachineCommand)cmd);
} else if (cmd instanceof ReadyCommand) {
return execute((ReadyCommand)cmd);
} else if (cmd instanceof StopCommand) {
return execute((StopCommand)cmd);
} else if (cmd instanceof CreateCommand) {
return execute((CreateCommand) cmd);
} else if (cmd instanceof DestroyCommand) {
return execute((DestroyCommand) cmd);
} else if (cmd instanceof PrimaryStorageDownloadCommand) {
return execute((PrimaryStorageDownloadCommand) cmd);
} else if (cmd instanceof GetStorageStatsCommand) {
return execute((GetStorageStatsCommand) cmd);
} else if (cmd instanceof ModifyStoragePoolCommand) {
return execute((ModifyStoragePoolCommand) cmd);
} else if (cmd instanceof SecurityIngressRulesCmd) {
return execute((SecurityIngressRulesCmd) cmd);
} else if (cmd instanceof StartCommand ) {
return execute((StartCommand) cmd);
} else if (cmd instanceof CleanupNetworkRulesCmd) {
return execute((CleanupNetworkRulesCmd)cmd);
} else if (cmd instanceof SavePasswordCommand) {
return execute((SavePasswordCommand)cmd);
} else if (cmd instanceof VmDataCommand) {
return execute((VmDataCommand)cmd);
} else {
s_logger.warn("Unsupported command ");
return Answer.createUnsupportedCommandAnswer(cmd);
}
} catch (final IllegalArgumentException e) {
return new Answer(cmd, false, e.getMessage());
}
}
private Answer execute(CleanupNetworkRulesCmd cmd) {
return new Answer(cmd);
}
private Answer execute(SecurityIngressRulesCmd cmd) {
s_logger.info("Programmed network rules for vm " + cmd.getVmName() + " guestIp=" + cmd.getGuestIp() + ", numrules=" + cmd.getRuleSet().length);
return new SecurityIngressRuleAnswer(cmd);
}
private Answer execute(ModifyStoragePoolCommand cmd) {
long capacity = getConfiguredProperty("local.storage.capacity", 10000000000L);
long used = 10000000L;
long available = capacity - used;
if (cmd.getAdd()) {
ModifyStoragePoolAnswer answer = new ModifyStoragePoolAnswer(cmd,
capacity, used, new HashMap<String, TemplateInfo>());
if (s_logger.isInfoEnabled())
s_logger
.info("Sending ModifyStoragePoolCommand answer with capacity: "
+ capacity
+ ", used: "
+ used
+ ", available: " + available);
return answer;
} else {
if (s_logger.isInfoEnabled())
s_logger
.info("ModifyNetfsStoragePoolCmd is not add command, cmd: "
+ cmd.toString());
return new Answer(cmd);
}
}
private Answer execute(GetStorageStatsCommand cmd) {
return new GetStorageStatsAnswer(cmd, getConfiguredProperty("local.storage.capacity", 100000000000L), 0L);
}
protected synchronized ReadyAnswer execute(ReadyCommand cmd) {
return new ReadyAnswer(cmd);
}
private Answer execute(PrimaryStorageDownloadCommand cmd) {
return new PrimaryStorageDownloadAnswer(cmd.getLocalPath(), 16000000L);
}
private Answer execute(ModifySshKeysCommand cmd) {
return new Answer(cmd, true, null);
}
@Override
protected String getDefaultScriptsDir() {
return null;
}
protected String getConfiguredProperty(String key, String defaultValue) {
String val = (String)_params.get(key);
return val==null?defaultValue:val;
}
protected Long getConfiguredProperty(String key, Long defaultValue) {
String val = (String)_params.get(key);
if (val != null) {
Long result = Long.parseLong(val);
return result;
}
return defaultValue;
}
protected List<Object> getHostInfo() {
final ArrayList<Object> info = new ArrayList<Object>();
long speed = getConfiguredProperty("cpuspeed", 4000L) ;
long cpus = getConfiguredProperty("cpus", 4L);
long ram = getConfiguredProperty("memory", 16000L*1024L*1024L);
long dom0ram = Math.min(ram/10, 768*1024*1024L);
String cap = getConfiguredProperty("capabilities", "hvm");
info.add((int)cpus);
info.add(speed);
info.add(ram);
info.add(cap);
info.add(dom0ram);
return info;
}
private Map<String, Object> getSimulatorProperties() throws ConfigurationException {
final File file = PropertiesUtil.findConfigFile("simulator.properties");
if (file == null) {
throw new ConfigurationException("Unable to find simulator.properties.");
}
s_logger.info("simulator.properties found at " + file.getAbsolutePath());
Properties properties = new Properties();
try {
properties.load(new FileInputStream(file));
final Map<String, Object> params = PropertiesUtil.toMap(properties);
return params;
} catch (final FileNotFoundException ex) {
throw new CloudRuntimeException("Cannot find the file: " + file.getAbsolutePath(), ex);
} catch (final IOException ex) {
throw new CloudRuntimeException("IOException in reading " + file.getAbsolutePath(), ex);
}
}
@Override
public boolean configure(String name, Map<String, Object> params)
throws ConfigurationException {
Map<String, Object> simProps = getSimulatorProperties();
params.putAll(simProps);
setParams(params);
_vmManager.configure(params);
_dhcpSnooper.configure(name, params);
_vmDataServer.configure(name, params);
return true;
}
public void setParams(Map<String, Object> _params) {
this._params = _params;
}
public Map<String, Object> getParams() {
return _params;
}
protected synchronized StartAnswer execute(StartCommand cmd) {
VmMgr vmMgr = getVmManager();
VirtualMachineTO vmSpec = cmd.getVirtualMachine();
String vmName = vmSpec.getName();
State state = State.Stopped;
try {
if (!_vms.containsKey(vmName)) {
synchronized (_vms) {
_vms.put(vmName, State.Starting);
}
MockVm vm = vmMgr.createVmFromSpec(vmSpec);
vmMgr.createVbd(vmSpec, vmName, vm);
vmMgr.createVif(vmSpec, vmName, vm);
state = State.Running;
for (NicTO nic: cmd.getVirtualMachine().getNics()) {
if (nic.getType() == TrafficType.Guest) {
InetAddress addr = _dhcpSnooper.getIPAddr(nic.getMac(), vmName);
nic.setIp(addr.getHostAddress());
}
}
_vmDataServer.handleVmStarted(cmd.getVirtualMachine());
return new StartAnswer(cmd);
} else {
String msg = "There is already a VM having the same name "
+ vmName;
s_logger.warn(msg);
return new StartAnswer(cmd, msg);
}
} catch (Exception ex) {
} finally {
synchronized (_vms) {
_vms.put(vmName, state);
}
}
return new StartAnswer(cmd);
}
protected synchronized StopAnswer execute(StopCommand cmd) {
VmMgr vmMgr = getVmManager();
StopAnswer answer = null;
String vmName = cmd.getVmName();
Integer port = vmMgr.getVncPort(vmName);
Long bytesReceived = null;
Long bytesSent = null;
State state = null;
synchronized (_vms) {
state = _vms.get(vmName);
_vms.put(vmName, State.Stopping);
}
try {
String result = vmMgr.stopVM(vmName, false);
if (result != null) {
s_logger.info("Trying destroy on " + vmName);
if (result == Script.ERR_TIMEOUT) {
result = vmMgr.stopVM(vmName, true);
}
s_logger.warn("Couldn't stop " + vmName);
if (result != null) {
return new StopAnswer(cmd, result);
}
}
answer = new StopAnswer(cmd, null, port, bytesSent, bytesReceived);
String result2 = vmMgr.cleanupVnet(cmd.getVnet());
if (result2 != null) {
result = result2 + (result != null ? ("\n" + result) : "");
answer = new StopAnswer(cmd, result, port, bytesSent,
bytesReceived);
}
_dhcpSnooper.cleanup(vmName, null);
return answer;
} finally {
if (answer == null || !answer.getResult()) {
synchronized (_vms) {
_vms.put(vmName, state);
}
}
}
}
protected Answer execute(final VmDataCommand cmd) {
return _vmDataServer.handleVmDataCommand(cmd);
}
protected Answer execute(final SavePasswordCommand cmd) {
return new Answer(cmd);
}
protected Answer execute(RebootCommand cmd) {
VmMgr vmMgr = getVmManager();
vmMgr.rebootVM(cmd.getVmName());
return new RebootAnswer(cmd, "success", 0L, 0L);
}
private Answer execute(PingTestCommand cmd) {
return new Answer(cmd);
}
protected GetVmStatsAnswer execute(GetVmStatsCommand cmd) {
return null;
}
private VmMgr getVmManager() {
return _vmManager;
}
protected Answer execute(GetHostStatsCommand cmd) {
VmMgr vmMgr = getVmManager();
return new GetHostStatsAnswer(cmd, vmMgr.getHostCpuUtilization(), vmMgr
.getHostFreeMemory(), vmMgr.getHostTotalMemory(), 0, 0,
"SimulatedHost");
}
protected CheckStateAnswer execute(CheckStateCommand cmd) {
State state = getVmManager().checkVmState(cmd.getVmName());
return new CheckStateAnswer(cmd, state);
}
protected CheckHealthAnswer execute(CheckHealthCommand cmd) {
return new CheckHealthAnswer(cmd, true);
}
protected CheckVirtualMachineAnswer execute(
final CheckVirtualMachineCommand cmd) {
VmMgr vmMgr = getVmManager();
final String vmName = cmd.getVmName();
final State state = vmMgr.checkVmState(vmName);
Integer vncPort = null;
if (state == State.Running) {
vncPort = vmMgr.getVncPort(vmName);
synchronized (_vms) {
_vms.put(vmName, State.Running);
}
}
return new CheckVirtualMachineAnswer(cmd, state, vncPort);
}
protected Answer execute(final AttachVolumeCommand cmd) {
return new Answer(cmd);
}
protected Answer execute(final AttachIsoCommand cmd) {
return new Answer(cmd);
}
protected CreateAnswer execute(final CreateCommand cmd) {
try {
VolumeTO vol = new VolumeTO(cmd.getVolumeId(),
Volume.Type.ROOT,
Storage.StorageResourceType.STORAGE_POOL,
com.cloud.storage.Storage.StoragePoolType.LVM, cmd
.getPool().getUuid(), "dummy", "/mountpoint",
"dummyPath", 1000L, null);
return new CreateAnswer(cmd, vol);
} catch (Throwable th) {
return new CreateAnswer(cmd, new Exception("Unexpected exception"));
}
}
protected HashMap<String, State> sync() {
Map<String, State> newStates;
Map<String, State> oldStates = null;
HashMap<String, State> changes = new HashMap<String, State>();
synchronized (_vms) {
newStates = getVmManager().getVmStates();
oldStates = new HashMap<String, State>(_vms.size());
oldStates.putAll(_vms);
for (Map.Entry<String, State> entry : newStates.entrySet()) {
String vm = entry.getKey();
State newState = entry.getValue();
State oldState = oldStates.remove(vm);
if (s_logger.isTraceEnabled()) {
s_logger
.trace("VM "
+ vm
+ ": xen has state "
+ newState
+ " and we have state "
+ (oldState != null ? oldState.toString()
: "null"));
}
if (oldState == null) {
_vms.put(vm, newState);
changes.put(vm, newState);
} else if (oldState == State.Starting) {
if (newState == State.Running) {
_vms.put(vm, newState);
} else if (newState == State.Stopped) {
s_logger.debug("Ignoring vm " + vm
+ " because of a lag in starting the vm.");
}
} else if (oldState == State.Stopping) {
if (newState == State.Stopped) {
_vms.put(vm, newState);
} else if (newState == State.Running) {
s_logger.debug("Ignoring vm " + vm
+ " because of a lag in stopping the vm. ");
}
} else if (oldState != newState) {
_vms.put(vm, newState);
changes.put(vm, newState);
}
}
for (Map.Entry<String, State> entry : oldStates.entrySet()) {
String vm = entry.getKey();
State oldState = entry.getValue();
if (s_logger.isTraceEnabled()) {
s_logger.trace("VM " + vm
+ " is now missing from xen so reporting stopped");
}
if (oldState == State.Stopping) {
s_logger.debug("Ignoring VM " + vm
+ " in transition state stopping.");
_vms.remove(vm);
} else if (oldState == State.Starting) {
s_logger.debug("Ignoring VM " + vm
+ " in transition state starting.");
} else if (oldState == State.Stopped) {
_vms.remove(vm);
} else {
changes.put(entry.getKey(), State.Stopped);
}
}
}
return changes;
}
protected Answer execute(DestroyCommand cmd) {
return new Answer(cmd, true, null);
}
}

View File

@ -27,6 +27,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
@ -77,7 +78,6 @@ import com.cloud.agent.api.CleanupNetworkRulesCmd;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.CreatePrivateTemplateFromSnapshotCommand;
import com.cloud.agent.api.CreatePrivateTemplateFromVolumeCommand;
import com.cloud.agent.api.CreateStoragePoolCommand;
import com.cloud.agent.api.CreateVolumeFromSnapshotAnswer;
import com.cloud.agent.api.CreateVolumeFromSnapshotCommand;
import com.cloud.agent.api.DeleteSnapshotBackupAnswer;
@ -230,12 +230,13 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
private String _dcId;
private String _pod;
private String _clusterId;
private long _hvVersion;
private KVMHAMonitor _monitor;
private final String _SSHKEYSPATH = "/root/.ssh";
private final String _SSHPRVKEYPATH = _SSHKEYSPATH + File.separator + "id_rsa.cloud";
private final String _SSHPUBKEYPATH = _SSHKEYSPATH + File.separator + "id_rsa.pub.cloud";
private final String _mountPoint = "/mnt";
private String _mountPoint = "/mnt";
StorageLayer _storage;
private static final class KeyValueInterpreter extends OutputInterpreter {
@ -315,13 +316,13 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
protected List<String> _vmsKilled = new ArrayList<String>();
private VirtualRoutingResource _virtRouterResource;
private LibvirtStorageResource _storageResource;
protected LibvirtStorageResource _storageResource;
private String _pingTestPath;
private int _dom0MinMem;
private enum defineOps {
protected enum defineOps {
UNDEFINE_VM,
DEFINE_VM
}
@ -553,6 +554,8 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
}
}
_localStoragePath = (String)params.get("local.storage.path");
if (_localStoragePath == null) {
_localStoragePath = "/var/lib/libvirt/images/";
@ -642,9 +645,14 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
s_logger.debug("Failed to found the local gateway");
}
_mountPoint = (String)params.get("mount.path");
if (_mountPoint == null) {
_mountPoint = "/mnt";
}
_storageResource = new LibvirtStorageResource(this, _storage, _createvmPath, _timeout, _mountPoint, _monitor);
return true;
}
@ -1759,7 +1767,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
_vms.put(vmName, state);
}
} else {
destroy_network_rules_for_vm(vmName);
destroy_network_rules_for_vm(conn, vmName);
cleanupVM(conn, vmName, getVnetId(VirtualMachineName.getVnet(vmName)));
}
@ -1972,10 +1980,18 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
}
try {
Connect conn = LibvirtConnection.getConnection();
destroy_network_rules_for_vm(vmName);
String macAddress = null;
if (vmName.startsWith("i-")) {
List<InterfaceDef> nics = getInterfaces(conn, vmName);
macAddress = nics.get(0).getMacAddress();
}
destroy_network_rules_for_vm(conn, vmName);
String result = stopVM(conn, vmName, defineOps.UNDEFINE_VM);
final String result2 = cleanupVnet(conn, cmd.getVnet());
if (result != null && result2 != null) {
result = result2 + result;
}
@ -2068,13 +2084,13 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
private void handleVmStartFailure(Connect conn, String vmName, LibvirtVMDef vm) {
protected void handleVmStartFailure(Connect conn, String vmName, LibvirtVMDef vm) {
if (vm != null && vm.getDevices() != null) {
cleanupVMNetworks(conn, vm.getDevices().getInterfaces());
}
}
private LibvirtVMDef createVMFromSpec(VirtualMachineTO vmTO) {
protected LibvirtVMDef createVMFromSpec(VirtualMachineTO vmTO) {
LibvirtVMDef vm = new LibvirtVMDef();
vm.setHvsType(_hypervisorType);
vm.setDomainName(vmTO.getName());
@ -2128,7 +2144,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
return vm;
}
private void createVifs(Connect conn, VirtualMachineTO vmSpec, LibvirtVMDef vm) throws InternalErrorException, LibvirtException {
protected void createVifs(Connect conn, VirtualMachineTO vmSpec, LibvirtVMDef vm) throws InternalErrorException, LibvirtException {
NicTO[] nics = vmSpec.getNics();
for (int i = 0; i < nics.length; i++) {
for (NicTO nic : vmSpec.getNics()) {
@ -2165,14 +2181,14 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
NicTO[] nics = vmSpec.getNics();
for (NicTO nic : nics) {
if (nic.isSecurityGroupEnabled()) {
if (vmSpec.getType() != VirtualMachine.Type.User) {
default_network_rules_for_systemvm(conn, vmName);
} else {
default_network_rules(conn, vmName, nic, vmSpec.getId());
}
}
}
if (nic.isSecurityGroupEnabled()) {
if (vmSpec.getType() != VirtualMachine.Type.User) {
default_network_rules_for_systemvm(conn, vmName);
} else {
default_network_rules(conn, vmName, nic, vmSpec.getId());
}
}
}
// Attach each data volume to the VM, if there is a deferred attached disk
for (DiskDef disk : vm.getDevices().getDisks()) {
@ -2180,6 +2196,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
attachOrDetachDisk(conn, true, vmName, disk.getDiskPath(), disk.getDiskSeq());
}
}
state = State.Running;
return new StartAnswer(cmd);
} catch (Exception e) {
@ -2208,7 +2225,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
}
}
private void createVbd(Connect conn, VirtualMachineTO vmSpec, String vmName, LibvirtVMDef vm) throws InternalErrorException, LibvirtException, URISyntaxException{
protected void createVbd(Connect conn, VirtualMachineTO vmSpec, String vmName, LibvirtVMDef vm) throws InternalErrorException, LibvirtException, URISyntaxException{
for (VolumeTO volume : vmSpec.getDisks()) {
String volPath = getVolumePath(conn, volume);
@ -2445,6 +2462,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
@Override
public PingCommand getCurrentStatus(long id) {
final HashMap<String, State> newStates = sync();
if (!_can_bridge_firewall) {
return new PingRoutingCommand(com.cloud.host.Host.Type.Routing, id, newStates);
} else {
@ -2485,6 +2503,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
cmd.getHostDetails().putAll(getVersionStrings());
cmd.setPool(_pool);
cmd.setCluster(_clusterId);
cmd.setGatewayIpAddress(_localGateway);
StartupStorageCommand sscmd = null;
try {
@ -2710,7 +2729,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
Domain dm = null;
for (int i =0; i < ids.length; i++) {
try {
s_logger.debug("domid" + ids[i]);
dm = conn.domainLookupByID(ids[i]);
DomainInfo.DomainState ps = dm.getInfo().state;
@ -3105,9 +3123,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
}
private String setVnetBrName(String vnetId) {
if (vnetId.equalsIgnoreCase(Vlan.UNTAGGED)) {
return _guestBridgeName;
}
return "cloudVirBr" + vnetId;
}
private String getVnetIdFromBrName(String vnetBrName) {
@ -3125,7 +3140,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
return conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
}
private List<InterfaceDef> getInterfaces(Connect conn, String vmName) {
protected List<InterfaceDef> getInterfaces(Connect conn, String vmName) {
LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
Domain dm = null;
try {
@ -3263,15 +3278,22 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
return true;
}
private boolean destroy_network_rules_for_vm(String vmName) {
protected boolean destroy_network_rules_for_vm(Connect conn, String vmName) {
if (!_can_bridge_firewall) {
return false;
}
String vif = null;
List<InterfaceDef> intfs = getInterfaces(conn, vmName);
if (intfs.size() > 0) {
InterfaceDef intf = intfs.get(0);
vif = intf.getDevName();
}
Script cmd = new Script(_securityGroupPath, _timeout, s_logger);
cmd.add("destroy_network_rules_for_vm");
cmd.add("--vmname");
cmd.add(vmName);
cmd.add("--vmname", vmName);
if (vif != null) {
cmd.add("--vif", vif);
}
String result = cmd.execute();
if (result != null) {
return false;
@ -3279,7 +3301,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
return true;
}
private boolean default_network_rules(Connect conn, String vmName, NicTO nic, Long vmId) {
protected boolean default_network_rules(Connect conn, String vmName, NicTO nic, Long vmId) {
if (!_can_bridge_firewall) {
return false;
}
@ -3297,7 +3319,9 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
cmd.add("default_network_rules");
cmd.add("--vmname", vmName);
cmd.add("--vmid", vmId.toString());
cmd.add("--vmip", nic.getIp());
if (nic.getIp() != null) {
cmd.add("--vmip", nic.getIp());
}
cmd.add("--vmmac", nic.getMac());
cmd.add("--vif", vif);
cmd.add("--brname", brname);
@ -3308,7 +3332,41 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
return true;
}
private boolean default_network_rules_for_systemvm(Connect conn, String vmName) {
protected boolean post_default_network_rules(Connect conn, String vmName, NicTO nic, Long vmId, InetAddress dhcpServerIp, String hostIp, String hostMacAddr) {
if (!_can_bridge_firewall) {
return false;
}
List<InterfaceDef> intfs = getInterfaces(conn, vmName);
if (intfs.size() < nic.getDeviceId()) {
return false;
}
InterfaceDef intf = intfs.get(nic.getDeviceId());
String brname = intf.getBrName();
String vif = intf.getDevName();
Script cmd = new Script(_securityGroupPath, _timeout, s_logger);
cmd.add("post_default_network_rules");
cmd.add("--vmname", vmName);
cmd.add("--vmid", vmId.toString());
cmd.add("--vmip", nic.getIp());
cmd.add("--vmmac", nic.getMac());
cmd.add("--vif", vif);
cmd.add("--brname", brname);
if (dhcpServerIp != null)
cmd.add("--dhcpSvr", dhcpServerIp.getHostAddress());
cmd.add("--hostIp", hostIp);
cmd.add("--hostMacAddr", hostMacAddr);
String result = cmd.execute();
if (result != null) {
return false;
}
return true;
}
protected boolean default_network_rules_for_systemvm(Connect conn, String vmName) {
if (!_can_bridge_firewall) {
return false;
}
@ -3448,7 +3506,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
private void createControlNetwork(Connect conn) throws LibvirtException {
_virtRouterResource.createControlNetwork(_linkLocalBridgeName);
}
private Answer execute(NetworkRulesSystemVmCommand cmd) {
boolean success = false;
Connect conn;
@ -3462,4 +3520,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
return new Answer(cmd, success, "");
}
}

View File

@ -194,10 +194,24 @@ public class LibvirtStorageResource {
}
public StoragePool getStoragePoolbyURI(Connect conn, URI uri) throws LibvirtException {
String sourcePath = uri.getPath();
sourcePath = sourcePath.replace("//", "/");
String sourceHost = uri.getHost();
String uuid = UUID.nameUUIDFromBytes(new String(sourceHost + sourcePath).getBytes()).toString();
String sourcePath;
String uuid;
String sourceHost = "";
String protocal;
if (uri.getScheme().equalsIgnoreCase("local")) {
sourcePath = _mountPoint + File.separator + uri.toString().replace("local:///", "");
sourcePath = sourcePath.replace("//", "/");
uuid = UUID.nameUUIDFromBytes(new String(sourcePath).getBytes()).toString();
protocal = "DIR";
} else {
sourcePath = uri.getPath();
sourcePath = sourcePath.replace("//", "/");
sourceHost = uri.getHost();
uuid = UUID.nameUUIDFromBytes(new String(sourceHost + sourcePath).getBytes()).toString();
protocal = "NFS";
}
String targetPath = _mountPoint + File.separator + uuid;
StoragePool sp = null;
try {
@ -207,12 +221,29 @@ public class LibvirtStorageResource {
if (sp == null) {
try {
_storageLayer.mkdir(targetPath);
LibvirtStoragePoolDef spd = new LibvirtStoragePoolDef(poolType.NETFS, uuid, uuid,
sourceHost, sourcePath, targetPath);
s_logger.debug(spd.toString());
addStoragePool(uuid);
LibvirtStoragePoolDef spd = null;
if (protocal.equalsIgnoreCase("NFS")) {
_storageLayer.mkdir(targetPath);
spd = new LibvirtStoragePoolDef(poolType.NETFS, uuid, uuid,
sourceHost, sourcePath, targetPath);
s_logger.debug(spd.toString());
addStoragePool(uuid);
synchronized (getStoragePool(uuid)) {
sp = conn.storagePoolDefineXML(spd.toString(), 0);
if (sp == null) {
s_logger.debug("Failed to define storage pool");
return null;
}
sp.create(0);
}
} else if (protocal.equalsIgnoreCase("DIR")) {
_storageLayer.mkdir(targetPath);
spd = new LibvirtStoragePoolDef(poolType.DIR, uuid, uuid,
null, null, sourcePath);
}
synchronized (getStoragePool(uuid)) {
sp = conn.storagePoolDefineXML(spd.toString(), 0);

View File

@ -486,6 +486,9 @@ public class LibvirtVMDef {
public String getDevName() {
return _networkName;
}
public String getMacAddress() {
return _macAddr;
}
@Override
public String toString() {

View File

@ -0,0 +1,348 @@
/**
* Copyright (C) 2011 Cloud.com. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later
version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.agent.vmdata;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.File;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.Local;
import javax.naming.ConfigurationException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.DefaultHandler;
import org.mortbay.jetty.handler.HandlerList;
import org.mortbay.jetty.handler.ResourceHandler;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.thread.QueuedThreadPool;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.routing.VmDataCommand;
import com.cloud.agent.api.to.NicTO;
import com.cloud.agent.api.to.VirtualMachineTO;
import com.cloud.network.Networks.TrafficType;
import com.cloud.storage.JavaStorageLayer;
import com.cloud.storage.StorageLayer;
import com.cloud.utils.net.NetUtils;
/**
* Serves vm data using embedded Jetty server
*
*/
@Local (value={VmDataServer.class})
public class JettyVmDataServer implements VmDataServer {
private static final Logger s_logger = Logger.getLogger(JettyVmDataServer.class);
public static final String USER_DATA = "user-data";
public static final String META_DATA = "meta-data";
protected String _vmDataDir;
protected Server _jetty;
protected String _hostIp;
protected Map<String, String> _ipVmMap = new HashMap<String, String>();
protected StorageLayer _fs = new JavaStorageLayer();
public class VmDataServlet extends HttpServlet {
private static final long serialVersionUID = -1640031398971742349L;
JettyVmDataServer _vmDataServer;
String _dataType; //userdata or meta-data
public VmDataServlet(JettyVmDataServer dataServer, String dataType) {
this._vmDataServer = dataServer;
this._dataType = dataType;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
int port = req.getServerPort();
if (port != 80 && port != 8000) {
resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Request not understood");
return;
}
if (_dataType.equalsIgnoreCase(USER_DATA)) {
handleUserData(req, resp);
} else if (_dataType.equalsIgnoreCase(META_DATA)) {
handleMetaData(req, resp);
}
}
protected void handleUserData(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String requester = req.getRemoteAddr();
resp.setContentType("text/html");
resp.setStatus(HttpServletResponse.SC_OK);
String userData = _vmDataServer.getVmDataItem(requester, USER_DATA);
if (userData != null){
resp.getWriter().println();
} else {
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Request not found");
}
}
protected void handleMetaData(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String metadataItem = req.getPathInfo();
String requester = req.getRemoteAddr();
resp.setContentType("text/html");
resp.setStatus(HttpServletResponse.SC_OK);
String metaData = _vmDataServer.getVmDataItem(requester, metadataItem);
if (metaData != null) {
resp.getWriter().println(_vmDataServer.getVmDataItem(requester, metadataItem));
} else {
resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Request not found");
}
}
}
@Override
public boolean configure(String name, Map<String, Object> params)
throws ConfigurationException {
boolean success = true;
try {
int vmDataPort = 80;
int fileservingPort = 8000;
_vmDataDir = (String)params.get("vm.data.dir");
String port = (String) params.get("vm.data.port");
if (port != null) {
vmDataPort = Integer.parseInt(port);
}
port = (String) params.get("file.server.port");
if (port != null) {
fileservingPort = Integer.parseInt(port);
}
_hostIp = (String) params.get("host.ip");
if (_vmDataDir == null) {
_vmDataDir = "/var/www/html";
}
success = _fs.mkdirs(_vmDataDir);
success = success && buildIpVmMap();
if (success) {
setupJetty(vmDataPort, fileservingPort);
}
} catch (Exception e) {
s_logger.warn("Failed to configure jetty", e);
throw new ConfigurationException("Failed to configure jetty!!");
}
return success;
}
protected boolean buildIpVmMap() {
String[] dirs = _fs.listFiles(_vmDataDir);
for (String dir: dirs) {
String [] path = dir.split("/");
String vm = path[path.length -1];
if (vm.startsWith("i-")) {
String [] dataFiles = _fs.listFiles(dir);
for (String dfile: dataFiles) {
String path2[] = dfile.split("/");
String ipv4file = path2[path2.length -1];
if (ipv4file.equalsIgnoreCase("local-ipv4")){
try {
BufferedReader input = new BufferedReader(new FileReader(dfile));
String line = null;
while (( line = input.readLine()) != null){
if (NetUtils.isValidIp(line)) {
_ipVmMap.put(line, dir);
s_logger.info("Found ip " + line + " for vm " + vm);
} else {
s_logger.info("Invalid ip " + line + " for vm " + vm);
}
}
} catch (FileNotFoundException e) {
s_logger.warn("Failed to find file " + dfile);
} catch (IOException e) {
s_logger.warn("Failed to get ip address of " + vm);
}
}
}
}
}
return true;
}
public String getVmDataItem(String requester, String dataItem) {
String vmName = _ipVmMap.get(requester);
if (vmName == null){
return null;
}
String vmDataFile = _vmDataDir + File.separator + vmName + dataItem;
try {
BufferedReader input = new BufferedReader(new FileReader(vmDataFile));
StringBuilder result = new StringBuilder();
String line = null;
while ((line = input.readLine()) != null) {
result.append(line);
}
input.close();
return result.toString();
} catch (FileNotFoundException e) {
s_logger.warn("Failed to find requested file " + vmDataFile);
return null;
} catch (IOException e) {
s_logger.warn("Failed to read requested file " + vmDataFile);
return null;
}
}
private void setupJetty(int vmDataPort, int fileservingPort) throws Exception {
_jetty = new Server();
SelectChannelConnector connector0 = new SelectChannelConnector();
connector0.setHost(_hostIp);
connector0.setPort(fileservingPort);
connector0.setMaxIdleTime(30000);
connector0.setRequestBufferSize(8192);
SelectChannelConnector connector1 = new SelectChannelConnector();
connector1.setHost(_hostIp);
connector1.setPort(vmDataPort);
connector1.setThreadPool(new QueuedThreadPool(5));
connector1.setMaxIdleTime(30000);
connector1.setRequestBufferSize(8192);
_jetty.setConnectors(new Connector[]{ connector0, connector1});
Context root = new Context(_jetty,"/latest",Context.SESSIONS);
root.setResourceBase(_vmDataDir);
root.addServlet(new ServletHolder(new VmDataServlet(this, USER_DATA)), "/user-data");
root.addServlet(new ServletHolder(new VmDataServlet(this, META_DATA)), "/meta-data");
ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setResourceBase("/var/lib/images/");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { root, resource_handler, new DefaultHandler() });
_jetty.setHandler(handlers);
_jetty.start();
//_jetty.join();
}
@Override
public boolean start() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean stop() {
return true;
}
@Override
public String getName() {
return "JettyVmDataServer";
}
@Override
public Answer handleVmDataCommand(VmDataCommand cmd) {
String vmDataDir = _vmDataDir + File.separator + cmd.getVmName();
try {
_fs.cleanup(vmDataDir, _vmDataDir);
_fs.mkdirs(vmDataDir);
} catch (IOException e1) {
s_logger.warn("Failed to cleanup vm data dir " + vmDataDir, e1);
return new Answer(cmd, false, "Failed to cleanup or create directory " + vmDataDir);
}
for (String [] item : cmd.getVmData()) {
try {
_fs.create(vmDataDir, item[1]);
String vmDataFile = vmDataDir + File.separator + item[1];
if (item[2] != null) {
BufferedWriter writer = new BufferedWriter(new FileWriter(vmDataFile));
writer.write(item[2]);
writer.close();
}
} catch (IOException e) {
s_logger.warn("Failed to write vm data item " + item[1], e);
return new Answer(cmd, false, "Failed to write vm data item " + item[1]);
}
}
return new Answer(cmd);
}
@Override
public void handleVmStarted(VirtualMachineTO vm) {
for (NicTO nic: vm.getNics()) {
if (nic.getType() == TrafficType.Guest) {
if (nic.getIp() != null) {
String ipv4File = _vmDataDir + File.separator + vm.getName() + File.separator + "local-ipv4";
try {
_fs.create(_vmDataDir + File.separator + vm.getName(), "local-ipv4");
BufferedWriter writer = new BufferedWriter(new FileWriter(ipv4File));
writer.write(nic.getIp());
_ipVmMap.put(nic.getIp(), vm.getName());
writer.close();
} catch (IOException e) {
s_logger.warn("Failed to create or write to local-ipv4 file " + ipv4File,e);
}
}
}
}
}
@Override
public void handleVmStopped(String vmName) {
String vmDataDir = _vmDataDir + File.separator + vmName;
try {
_fs.cleanup(vmDataDir, _vmDataDir);
_fs.mkdirs(vmDataDir);
} catch (IOException e1) {
s_logger.warn("Failed to cleanup vm data dir " + vmDataDir, e1);
}
}
}

View File

@ -0,0 +1,38 @@
/**
* Copyright (C) 2011 Cloud.com. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later
version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.agent.vmdata;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.routing.VmDataCommand;
import com.cloud.agent.api.to.VirtualMachineTO;
import com.cloud.utils.component.Manager;
/**
* Maintains vm data (user data, meta-data, password) that can be fetched via HTTP
* by user vms
*
*/
public interface VmDataServer extends Manager {
public Answer handleVmDataCommand(VmDataCommand cmd);
public void handleVmStarted(VirtualMachineTO vm);
public void handleVmStopped(String vmName);
}

View File

@ -21,19 +21,30 @@
*/
package com.cloud.agent.api;
import com.cloud.agent.api.to.VirtualMachineTO;
public class StartAnswer extends Answer {
VirtualMachineTO vm;
protected StartAnswer() {
}
public StartAnswer(StartCommand cmd, String msg) {
super(cmd, false, msg);
this.vm = cmd.getVirtualMachine();
}
public StartAnswer(StartCommand cmd, Exception e) {
super(cmd, false, e.getMessage());
this.vm = cmd.getVirtualMachine();
}
public StartAnswer(StartCommand cmd) {
super(cmd, true, null);
this.vm = cmd.getVirtualMachine();
}
public VirtualMachineTO getVirtualMachine() {
return vm;
}
}

View File

@ -63,7 +63,7 @@ public class StartupCommand extends Command {
public StartupCommand(Long id, Host.Type type, String name, String dataCenter, String pod, String guid, String version, String gatewayIpAddress) {
this(id, type, name, dataCenter, pod, guid, version);
this.gatewayIpAddress = gatewayIpAddress;
}
}
public Host.Type getHostType() {
return type;
@ -278,6 +278,7 @@ public class StartupCommand extends Command {
public void setGatewayIpAddress(String gatewayIpAddress) {
this.gatewayIpAddress = gatewayIpAddress;
}
@Override
public boolean executeInSequence() {

View File

@ -24,6 +24,7 @@ import java.util.List;
public class VmDataCommand extends NetworkElementCommand {
String vmIpAddress;
String vmName;
List<String[]> vmData;
protected VmDataCommand() {
@ -35,9 +36,19 @@ public class VmDataCommand extends NetworkElementCommand {
}
public VmDataCommand(String vmIpAddress) {
this.vmIpAddress = vmIpAddress;
this.vmData = new ArrayList<String[]>();
this(vmIpAddress, null);
}
public String getVmName() {
return vmName;
}
public VmDataCommand(String vmIpAddress, String vmName) {
this.vmName = vmName;
this.vmIpAddress = vmIpAddress;
this.vmData = new ArrayList<String[]>();
}
public String getVmIpAddress() {
return vmIpAddress;

View File

@ -221,7 +221,7 @@ public class ApiConstants {
public static final String PING_STORAGE_SERVER_IP = "pingstorageserverip";
public static final String PING_DIR = "pingdir";
public static final String TFTP_DIR = "tftpdir";
public static final String PING_CIFS_USERNAME = "pingcifsusername";
public static final String PZING_CIFS_USERNAME = "pingcifsusername";
public static final String PING_CIFS_PASSWORD = "pingcifspassword";
public static final String CHECKSUM="checksum";
public static final String NETWORK_DEVICE_TYPE = "networkdevicetype";

View File

@ -1,181 +1,181 @@
/**
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.user;
import java.util.List;
import com.cloud.api.commands.CreateAccountCmd;
import com.cloud.api.commands.CreateUserCmd;
import com.cloud.api.commands.DeleteAccountCmd;
import com.cloud.api.commands.DeleteUserCmd;
import com.cloud.api.commands.DisableAccountCmd;
import com.cloud.api.commands.DisableUserCmd;
import com.cloud.api.commands.EnableAccountCmd;
import com.cloud.api.commands.EnableUserCmd;
import com.cloud.api.commands.ListResourceLimitsCmd;
import com.cloud.api.commands.LockUserCmd;
import com.cloud.api.commands.UpdateAccountCmd;
import com.cloud.api.commands.UpdateResourceLimitCmd;
import com.cloud.api.commands.UpdateUserCmd;
import com.cloud.configuration.ResourceLimit;
import com.cloud.domain.Domain;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.utils.Pair;
public interface AccountService {
/**
* Creates a new user, stores the password as is so encrypted passwords are recommended.
*
* @param cmd
* the create command that has the username, email, password, account name, domain, timezone, etc. for creating
* the user.
* @return the user if created successfully, null otherwise
*/
UserAccount createAccount(CreateAccountCmd cmd);
/**
* Deletes a user by userId
*
* @param cmd
* - the delete command defining the id of the user to be deleted.
* @return true if delete was successful, false otherwise
*/
boolean deleteUserAccount(DeleteAccountCmd cmd);
/**
* Disables a user by userId
*
* @param cmd
* the command wrapping the userId parameter
* @return UserAccount object
*/
UserAccount disableUser(DisableUserCmd cmd);
/**
* Enables a user
*
* @param cmd
* - the command containing userId
* @return UserAccount object
*/
UserAccount enableUser(EnableUserCmd cmd);
/**
* Locks a user by userId. A locked user cannot access the API, but will still have running VMs/IP addresses allocated/etc.
*
* @param userId
* @return UserAccount object
*/
UserAccount lockUser(LockUserCmd cmd);
/**
* Update a user by userId
*
* @param userId
* @return UserAccount object
*/
UserAccount updateUser(UpdateUserCmd cmd);
/**
* Disables an account by accountName and domainId
*
* @param disabled
* account if success
* @return true if disable was successful, false otherwise
*/
Account disableAccount(DisableAccountCmd cmd) throws ConcurrentOperationException, ResourceUnavailableException;
/**
* Enables an account by accountId
*
* @param cmd
* - the enableAccount command defining the accountId to be deleted.
* @return account object
*/
Account enableAccount(EnableAccountCmd cmd);
/**
* Locks an account by accountId. A locked account cannot access the API, but will still have running VMs/IP addresses
* allocated/etc.
*
* @param cmd
* - the LockAccount command defining the accountId to be locked.
* @return account object
*/
Account lockAccount(DisableAccountCmd cmd);
/**
* Updates an account name
*
* @param cmd
* - the parameter containing accountId
* @return updated account object
*/
Account updateAccount(UpdateAccountCmd cmd);
/**
* Updates an existing resource limit with the specified details. If a limit doesn't exist, will create one.
*
* @param cmd
* the command that wraps the domainId, accountId, type, and max parameters
* @return the updated/created resource limit
*/
ResourceLimit updateResourceLimit(UpdateResourceLimitCmd cmd);
/**
* Search for resource limits for the given id and/or account and/or type and/or domain.
*
* @param cmd
* the command wrapping the id, type, account, and domain
* @return a list of limits that match the criteria
*/
List<? extends ResourceLimit> searchForLimits(ListResourceLimitsCmd cmd);
Account getSystemAccount();
User getSystemUser();
User createUser(CreateUserCmd cmd);
boolean deleteUser(DeleteUserCmd deleteUserCmd);
boolean isAdmin(short accountType);
Account finalizeOwner(Account caller, String accountName, Long domainId);
Pair<String, Long> finalizeAccountDomainForList(Account caller, String accountName, Long domainId);
Account getActiveAccount(String accountName, Long domainId);
Account getActiveAccount(Long accountId);
Account getAccount(Long accountId);
User getActiveUser(long userId);
Domain getDomain(long id);
boolean isRootAdmin(short accountType);
User getActiveUserByRegistrationToken(String registrationToken);
void markUserRegistered(long userId);
}
/**
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.user;
import java.util.List;
import com.cloud.api.commands.CreateAccountCmd;
import com.cloud.api.commands.CreateUserCmd;
import com.cloud.api.commands.DeleteAccountCmd;
import com.cloud.api.commands.DeleteUserCmd;
import com.cloud.api.commands.DisableAccountCmd;
import com.cloud.api.commands.DisableUserCmd;
import com.cloud.api.commands.EnableAccountCmd;
import com.cloud.api.commands.EnableUserCmd;
import com.cloud.api.commands.ListResourceLimitsCmd;
import com.cloud.api.commands.LockUserCmd;
import com.cloud.api.commands.UpdateAccountCmd;
import com.cloud.api.commands.UpdateResourceLimitCmd;
import com.cloud.api.commands.UpdateUserCmd;
import com.cloud.configuration.ResourceLimit;
import com.cloud.domain.Domain;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.utils.Pair;
public interface AccountService {
/**
* Creates a new user, stores the password as is so encrypted passwords are recommended.
*
* @param cmd
* the create command that has the username, email, password, account name, domain, timezone, etc. for creating
* the user.
* @return the user if created successfully, null otherwise
*/
UserAccount createAccount(CreateAccountCmd cmd);
/**
* Deletes a user by userId
*
* @param cmd
* - the delete command defining the id of the user to be deleted.
* @return true if delete was successful, false otherwise
*/
boolean deleteUserAccount(DeleteAccountCmd cmd);
/**
* Disables a user by userId
*
* @param cmd
* the command wrapping the userId parameter
* @return UserAccount object
*/
UserAccount disableUser(DisableUserCmd cmd);
/**
* Enables a user
*
* @param cmd
* - the command containing userId
* @return UserAccount object
*/
UserAccount enableUser(EnableUserCmd cmd);
/**
* Locks a user by userId. A locked user cannot access the API, but will still have running VMs/IP addresses allocated/etc.
*
* @param userId
* @return UserAccount object
*/
UserAccount lockUser(LockUserCmd cmd);
/**
* Update a user by userId
*
* @param userId
* @return UserAccount object
*/
UserAccount updateUser(UpdateUserCmd cmd);
/**
* Disables an account by accountName and domainId
*
* @param disabled
* account if success
* @return true if disable was successful, false otherwise
*/
Account disableAccount(DisableAccountCmd cmd) throws ConcurrentOperationException, ResourceUnavailableException;
/**
* Enables an account by accountId
*
* @param cmd
* - the enableAccount command defining the accountId to be deleted.
* @return account object
*/
Account enableAccount(EnableAccountCmd cmd);
/**
* Locks an account by accountId. A locked account cannot access the API, but will still have running VMs/IP addresses
* allocated/etc.
*
* @param cmd
* - the LockAccount command defining the accountId to be locked.
* @return account object
*/
Account lockAccount(DisableAccountCmd cmd);
/**
* Updates an account name
*
* @param cmd
* - the parameter containing accountId
* @return updated account object
*/
Account updateAccount(UpdateAccountCmd cmd);
/**
* Updates an existing resource limit with the specified details. If a limit doesn't exist, will create one.
*
* @param cmd
* the command that wraps the domainId, accountId, type, and max parameters
* @return the updated/created resource limit
*/
ResourceLimit updateResourceLimit(UpdateResourceLimitCmd cmd);
/**
* Search for resource limits for the given id and/or account and/or type and/or domain.
*
* @param cmd
* the command wrapping the id, type, account, and domain
* @return a list of limits that match the criteria
*/
List<? extends ResourceLimit> searchForLimits(ListResourceLimitsCmd cmd);
Account getSystemAccount();
User getSystemUser();
User createUser(CreateUserCmd cmd);
boolean deleteUser(DeleteUserCmd deleteUserCmd);
boolean isAdmin(short accountType);
Account finalizeOwner(Account caller, String accountName, Long domainId);
Pair<String, Long> finalizeAccountDomainForList(Account caller, String accountName, Long domainId);
Account getActiveAccount(String accountName, Long domainId);
Account getActiveAccount(Long accountId);
Account getAccount(Long accountId);
User getActiveUser(long userId);
Domain getDomain(long id);
boolean isRootAdmin(short accountType);
User getActiveUserByRegistrationToken(String registrationToken);
void markUserRegistered(long userId);
}

View File

@ -443,6 +443,7 @@
<path id="agent.classpath">
<path refid="deps.classpath" />
<path refid="thirdparty.classpath"/>
<fileset dir="${target.dir}">
<include name="**/${core.jar}" />
<include name="**/${utils.jar}" />

View File

@ -70,6 +70,8 @@
<include name="cloud-libvirt-0.4.5.jar" />
<include name="cloud-jna.jar" />
<include name="cloud-cglib.jar" />
<include name="jetty-6.1.26.jar" />
<include name="jetty-util-6.1.26.jar"/>
</zipfileset>
<zipfileset dir="${jar.dir}">
<include name="${agent.jar}" />

View File

@ -93,6 +93,7 @@
<adapters key="com.cloud.acl.SecurityChecker">
<adapter name="DomainChecker" class="com.cloud.acl.DomainChecker"/>
</adapters>
</management-server>
<configuration-server class="com.cloud.server.ConfigurationServerImpl">

View File

@ -221,6 +221,7 @@ Requires: jpackage-utils
Requires: %{name}-daemonize
Requires: /sbin/service
Requires: /sbin/chkconfig
Requires: jnetpcap
Group: System Environment/Libraries
%package baremetal-agent
@ -298,6 +299,13 @@ The Cloud.com command line tools contain a few Python modules that can call clou
%if %{_premium}
%package premium-agent
Summary: Cloud.com premium agent
Requires: cloud-agent
Group: System Environment/Libraries
%description premium-agent
The Cloud.com premium agent
%package test
Summary: Cloud.com test suite
Requires: java >= 1.6.0
@ -507,6 +515,8 @@ fi
%{_javadir}/%{name}-xmlrpc-common-3.*.jar
%{_javadir}/%{name}-xmlrpc-client-3.*.jar
%{_javadir}/%{name}-jstl-1.2.jar
%{_javadir}/jetty-6.1.26.jar
%{_javadir}/jetty-util-6.1.26.jar
%{_javadir}/%{name}-axis.jar
%{_javadir}/%{name}-commons-discovery.jar
@ -586,6 +596,9 @@ fi
%attr(0755,root,root) %{_bindir}/%{name}-setup-agent
%dir %attr(0770,root,root) %{_localstatedir}/log/%{name}/agent
%files premium-agent
%{_javadir}/cloud-agent-extras.jar
%attr(0755,root,root) %{_bindir}/mycloud-setup-agent
%files console-proxy
%defattr(0644,root,root,0755)

View File

@ -0,0 +1,41 @@
/**
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.agent;
import com.cloud.agent.api.StartupCommand;
import com.cloud.exception.ConnectionException;
import com.cloud.utils.component.Adapter;
/**
* HostCreator hooks into the AgentManager to be notified of agent connections before the
* AgentManager knows about the agent that's connecting.
*/
public interface StartupCommandProcessor extends Adapter {
/**
* This method is called by AgentManager when an agent made a
* connection to this server before the AgentManager knows about this agent
* @param agentId id of the agent
* @param cmd command sent by the agent to the server on startup.
* @return true if handled by the creator
* @throws ConnectionException if host has problems
*/
boolean processInitialConnect(StartupCommand[] cmd) throws ConnectionException;
}

View File

@ -515,14 +515,39 @@ public class VirtualRoutingResource implements Manager {
}
public void cleanupPrivateNetwork(String privNwName, String privBrName){
if (isDNSmasqRunning(privNwName)) {
stopDnsmasq(privNwName);
}
if (isBridgeExists(privBrName)) {
deleteBridge(privBrName);
}
}
private void deletExitingLinkLocalRoutTable(String linkLocalBr) {
Script command = new Script("/bin/bash", _timeout);
command.add("-c");
command.add("ip route | grep " + NetUtils.getLinkLocalCIDR());
OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser();
String result = command.execute(parser);
boolean foundLinkLocalBr = false;
if (result == null && parser.getLines() != null) {
String[] lines = parser.getLines().split("\\n");
for (String line : lines) {
String[] tokens = line.split(" ");
if (!tokens[2].equalsIgnoreCase(linkLocalBr)) {
Script.runSimpleBashScript("ip route del " + NetUtils.getLinkLocalCIDR());
} else {
foundLinkLocalBr = true;
}
}
}
if (!foundLinkLocalBr) {
Script.runSimpleBashScript("ip route add " + NetUtils.getLinkLocalCIDR() + " dev " + linkLocalBr + " src " + NetUtils.getLinkLocalGateway());
}
}
public void createControlNetwork(String privBrName) {
deletExitingLinkLocalRoutTable(privBrName);
if (!isBridgeExists(privBrName)) {
Script.runSimpleBashScript("brctl addbr " + privBrName + "; ifconfig " + privBrName + " up;", _timeout);
}
}
// protected Answer execute(final SetFirewallRuleCommand cmd) {

View File

@ -347,7 +347,8 @@ public class HostVO implements Host {
this.guid = guid;
this.status = Status.Up;
this.totalMemory = 0;
this.dom0MinMemory = 0;
this.dom0MinMemory = 0;
this.hostAllocationState = Host.HostAllocationState.Enabled;
}
protected HostVO() {

View File

@ -61,7 +61,7 @@ public abstract class ServerResourceBase implements ServerResource {
protected abstract String getDefaultScriptsDir();
@Override
public boolean configure(final String name, final Map<String, Object> params) throws ConfigurationException {
public boolean configure(final String name, Map<String, Object> params) throws ConfigurationException {
_name = name;
String publicNic = (String)params.get("public.network.device");
@ -123,7 +123,10 @@ public abstract class ServerResourceBase implements ServerResource {
throw new ConfigurationException("Private NIC is not configured");
}
}
String infos[] = NetUtils.getNetworkParams(_privateNic);
params.put("host.ip", infos[0]);
params.put("host.mac.address", infos[1]);
return true;
}

View File

@ -32,7 +32,18 @@ import javax.naming.ConfigurationException;
public class JavaStorageLayer implements StorageLayer {
String _name;
boolean _makeWorldWriteable = true;
public JavaStorageLayer() {
super();
}
public JavaStorageLayer(boolean makeWorldWriteable) {
this();
this._makeWorldWriteable = makeWorldWriteable;
}
@Override
public boolean cleanup(String path, String rootPath) throws IOException {
assert path.startsWith(rootPath) : path + " does not start with " + rootPath;
@ -121,8 +132,11 @@ public class JavaStorageLayer implements StorageLayer {
if (file.exists()) {
return file.isDirectory();
}
return (file.mkdirs() && setWorldReadableAndWriteable(file));
if (_makeWorldWriteable) {
return (file.mkdirs() && setWorldReadableAndWriteable(file));
} else {
return file.mkdirs();
}
}
}
@ -146,7 +160,9 @@ public class JavaStorageLayer implements StorageLayer {
for (String dirPath : dirPaths) {
dir = new File(dirPath);
if (!dir.exists()) {
success = dir.mkdir() && setWorldReadableAndWriteable(dir);
success = dir.mkdir();
if (_makeWorldWriteable)
success = success && setWorldReadableAndWriteable(dir);
}
}

View File

@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.storage.secondary;
package com.cloud.storage.resource;
import java.util.HashMap;
import java.util.Map;
@ -161,7 +161,7 @@ public class LocalSecondaryStorageResource extends ServerResourceBase implements
final StartupStorageCommand cmd = new StartupStorageCommand(_parent, StoragePoolType.Filesystem, 1024l*1024l*1024l*1024l, _dlMgr.gatherTemplateInfo());
cmd.setResourceType(Storage.StorageResourceType.SECONDARY_STORAGE);
cmd.setIqn(null);
cmd.setIqn("local://");
fillNetworkInformation(cmd);
cmd.setDataCenter(_dc);
cmd.setPod(_pod);

0
debian/cloud-agent-premium.config vendored Normal file
View File

2
debian/cloud-agent-premium.install vendored Normal file
View File

@ -0,0 +1,2 @@
/usr/share/java/cloud-agent-extras.jar
/usr/bin/mycloud-setup-agent

View File

@ -4,6 +4,8 @@
/usr/share/java/cloud-ehcache.jar
/usr/share/java/cloud-email.jar
/usr/share/java/cloud-gson.jar
/usr/share/java/jetty-6.1.26.jar
/usr/share/java/jetty-util-6.1.26.jar
/usr/share/java/cloud-httpcore-4.0.jar
/usr/share/java/cloud-jna.jar
/usr/share/java/cloud-libvirt-0.4.5.jar

11
debian/control vendored
View File

@ -141,12 +141,21 @@ Provides: vmops-agent
Conflicts: vmops-agent
Replaces: vmops-agent
Architecture: any
Depends: openjdk-6-jre, cloud-utils (= ${source:Version}), cloud-core (= ${source:Version}), cloud-deps (= ${source:Version}), python, cloud-python (= ${source:Version}), cloud-agent-libs (= ${source:Version}), cloud-agent-scripts (= ${source:Version}), libcommons-httpclient-java, libcommons-collections-java, libcommons-dbcp-java, libcommons-pool-java, libcommons-logging-java, libvirt0, cloud-daemonize, sysvinit-utils, chkconfig, qemu-kvm, libvirt-bin, cgroup-bin, augeas-tools, uuid-runtime, rsync, grep, iproute, vlan
Depends: openjdk-6-jre, cloud-utils (= ${source:Version}), cloud-core (= ${source:Version}), cloud-deps (= ${source:Version}), python, cloud-python (= ${source:Version}), cloud-agent-libs (= ${source:Version}), cloud-agent-scripts (= ${source:Version}), libcommons-httpclient-java, libcommons-collections-java, libcommons-dbcp-java, libcommons-pool-java, libcommons-logging-java, libvirt0, cloud-daemonize, sysvinit-utils, chkconfig, qemu-kvm, libvirt-bin, cgroup-bin, augeas-tools, uuid-runtime, rsync, grep, iproute, jnetpcap, ebtables
Description: Cloud.com agent
The Cloud.com agent is in charge of managing shared computing resources in
a Cloud.com Cloud Stack-powered cloud. Install this package if this computer
will participate in your cloud.
Package: cloud-agent-premium
Architecture: any
Depends: cloud-agent
Description: Cloud.com agent premium
The Cloud.com agent is in charge of managing shared computing resources in
a Cloud.com Cloud Stack-powered cloud. Install this package if this computer
will participate in your cloud.
Package: cloud-console-proxy
Architecture: any
Depends: openjdk-6-jre, cloud-utils (= ${source:Version}), cloud-core (= ${source:Version}), cloud-deps (= ${source:Version}), cloud-agent-libs (= ${source:Version}), python, cloud-python (= ${source:Version}), libcommons-httpclient-java, libcommons-collections-java, libcommons-dbcp-java, libcommons-pool-java, libcommons-logging-java, cloud-daemonize, sysvinit-utils, chkconfig, augeas-tools, uuid-runtime, grep, iproute

3
deps/.classpath vendored
View File

@ -49,5 +49,8 @@
<classpathentry exported="true" kind="lib" path="vmware-lib-xml-apis.jar"/>
<classpathentry exported="true" kind="lib" path="vmware-vim.jar"/>
<classpathentry exported="true" kind="lib" path="vmware-vim25.jar"/>
<classpathentry exported="true" kind="lib" path="jetty-6.1.26.jar"/>
<classpathentry exported="true" kind="lib" path="jetty-util-6.1.26.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

BIN
deps/jetty-6.1.26.jar vendored Normal file

Binary file not shown.

BIN
deps/jetty-util-6.1.26.jar vendored Normal file

Binary file not shown.

866
python/bindir/mycloud-setup-agent Executable file
View File

@ -0,0 +1,866 @@
#!/usr/bin/python
from subprocess import PIPE, Popen
from signal import alarm, signal, SIGALRM, SIGKILL
import tempfile
import shutil
import os
import logging
import sys
import re
import traceback
import socket
import uuid
class CloudRuntimeException(Exception):
def __init__(self, errMsg):
self.errMsg = errMsg
def __str__(self):
return self.errMsg
def formatExceptionInfo(maxTBlevel=5):
cla, exc, trbk = sys.exc_info()
excTb = traceback.format_tb(trbk, maxTBlevel)
msg = str(exc) + "\n"
for tb in excTb:
msg += tb
return msg
class bash:
def __init__(self, args, timeout=600):
self.args = args
logging.debug("execute:%s"%args)
self.timeout = timeout
self.process = None
self.success = False
self.run()
def run(self):
class Alarm(Exception):
pass
def alarm_handler(signum, frame):
raise Alarm
try:
self.process = Popen(self.args, shell=True, stdout=PIPE, stderr=PIPE)
if self.timeout != -1:
signal(SIGALRM, alarm_handler)
alarm(self.timeout)
try:
self.stdout, self.stderr = self.process.communicate()
if self.timeout != -1:
alarm(0)
except Alarm:
os.kill(self.process.pid, SIGKILL)
raise CloudRuntimeException("Timeout during command execution")
self.success = self.process.returncode == 0
except:
raise CloudRuntimeException(formatExceptionInfo())
if not self.success:
raise CloudRuntimeException(self.getStderr())
def isSuccess(self):
return self.success
def getStdout(self):
return self.stdout.strip("\n")
def getLines(self):
return self.stdout.split("\n")
def getStderr(self):
return self.stderr.strip("\n")
def initLoging(logFile=None):
try:
if logFile is None:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(filename=logFile, level=logging.DEBUG)
except:
logging.basicConfig(level=logging.DEBUG)
class configFileOps():
class entry():
def __init__(self, name, value):
self.name = name
self.value = value
self.state = "new"
def setState(self, state):
self.state = state
def getState(self):
return self.state
def __init__(self, fileName):
self.fileName = fileName
self.entries = []
self.backups = []
def addEntry(self, name, value):
e = self.entry(name, value)
self.entries.append(e)
def getEntry(self, name):
try:
ctx = file(self.fileName).read(-1)
match = re.search("^" + name + ".*", ctx, re.MULTILINE)
if match is None:
return ""
line = match.group(0).split("=")
return line[1]
except:
return ""
def save(self):
fh, abs_path = tempfile.mkstemp()
new_file = open(abs_path, "w")
fp = open(self.fileName, "r")
for line in fp.readlines():
for entry in self.entries:
if line.startswith(entry.name):
line = entry.name + "=" + entry.value + "\n"
entry.setState("set")
new_file.write(line)
for entry in self.entries:
if entry.getState() != "set":
new_file.write("\n" + entry.name + "=" + entry.value + "\n")
new_file.close()
fp.close()
shutil.move(abs_path, self.fileName)
def replace_line(self, startswith,stanza,always_add=False):
lines = [ s.strip() for s in file(self.fileName).readlines() ]
newlines = []
replaced = False
for line in lines:
if re.search(startswith, line):
if stanza is not None:
newlines.append(stanza)
self.backups.append([line, stanza])
replaced = True
else: newlines.append(line)
if not replaced and always_add:
newlines.append(stanza)
self.backups.append([None, stanza])
newlines = [ s + '\n' for s in newlines ]
file(self.fileName,"w").writelines(newlines)
def replace_or_add_line(self, startswith,stanza):
return self.replace_line(startswith,stanza,always_add=True)
def add_lines(self, lines):
fp = file(self.fileName).read(-1)
sh = re.escape(lines)
match = re.search(sh, fp, re.MULTILINE)
if match is not None:
return
fp += lines
file(self.fileName, "w").write(fp)
self.backups.append([None, lines])
def replace_lines(self, src, dst, addToBackup=True):
fp = file(self.fileName).read(-1)
sh = re.escape(src)
if dst is None:
dst = ""
repl,nums = re.subn(sh, dst, fp)
if nums <=0:
return
file(self.fileName, "w").write(repl)
if addToBackup:
self.backups.append([src, dst])
def backup(self):
for oldLine, newLine in self.backups:
self.replace_lines(newLine, oldLine, False)
class networkConfig():
class devInfo():
def __init__(self, macAddr, ipAddr, netmask, gateway, type, name):
self.name = name
self.macAdrr = macAddr
self.ipAddr = ipAddr
self.netmask = netmask
self.gateway = gateway
self.type = type
self.name = name
#dhcp or static
self.method = None
@staticmethod
def getDefaultNetwork():
cmd = bash("route -n|awk \'/^0.0.0.0/ {print $2,$8}\'")
if not cmd.isSuccess():
logging.debug("Failed to get default route")
return None
result = cmd.getStdout().split(" ")
gateway = result[0]
dev = result[1]
pdi = networkConfig.getDevInfo(dev)
logging.debug("Found default network device:%s"%pdi.name)
pdi.gateway = gateway
return pdi
@staticmethod
def createBridge(dev, brName):
if not networkConfig.isBridgeSupported():
logging.debug("bridge is not supported")
return False
if networkConfig.isBridgeEnslavedWithDevices(brName):
logging.debug("bridge: %s has devices enslaved"%brName)
return False
cmds = ""
if not networkConfig.isBridge(brName):
cmds = "brctl addbr %s ;"%brName
cmds += "ifconfig %s up;"%brName
cmds += "brctl addif %s %s"%(brName, dev)
return bash(cmds).isSuccess()
@staticmethod
def isBridgeEnslavedWithDevices(brName):
if not networkConfig.isBridge(brName):
return False
if not os.listdir("/sys/class/net/%s/brif"%brName):
return False
return True
@staticmethod
def isBridgeSupported():
if os.path.exists("/proc/sys/net/bridge"):
return True
return bash("modprobe bridge").isSucess()
@staticmethod
def isNetworkDev(devName):
return os.path.exists("/sys/class/net/%s"%devName)
@staticmethod
def isBridgePort(devName):
return os.path.exists("/sys/class/net/%s/brport"%devName)
@staticmethod
def isBridge(devName):
return os.path.exists("/sys/class/net/%s/bridge"%devName)
@staticmethod
def getBridge(devName):
bridgeName = None
if os.path.exists("/sys/class/net/%s/brport/bridge"%devName):
realPath = os.path.realpath("/sys/class/net/%s/brport/bridge"%devName)
bridgeName = realPath.split("/")[-1]
return bridgeName
@staticmethod
def getEnslavedDev(br, brPort):
if not networkConfig.isBridgeEnslavedWithDevices(br):
return None
for dev in os.listdir("/sys/class/net/%s/brif"%br):
br_port = int(file("/sys/class/net/%s/brif/%s/port_no"%(br,dev)).readline().strip("\n"), 16)
if br_port == brPort:
return dev
return None
@staticmethod
def getDevInfo(dev):
if not networkConfig.isNetworkDev(dev):
logging.debug("dev: " + dev + " is not a network device")
return None
netmask = None
ipAddr = None
macAddr = None
cmd = bash("ifconfig " + dev)
if not cmd.isSuccess():
logging.debug("Failed to get address from ifconfig")
return None
for line in cmd.getLines():
if line.find("HWaddr") != -1:
macAddr = line.split("HWaddr ")[1].strip(" ")
elif line.find("inet ") != -1:
m = re.search("addr:(.*)\ *Bcast:(.*)\ *Mask:(.*)", line)
if m is not None:
ipAddr = m.group(1).rstrip(" ")
netmask = m.group(3).rstrip(" ")
if networkConfig.isBridgePort(dev):
type = "brport"
elif networkConfig.isBridge(dev):
type = "bridge"
else:
type = "dev"
return networkConfig.devInfo(macAddr, ipAddr, netmask, None, type, dev)
class networkConfigUbuntu(networkConfig):
def __init__(self, syscfg):
self.syscfg = syscfg
self.netCfgFile = "/etc/network/interfaces"
self.brName = None
self.dev = None
self.status = None
self.cfoHandlers = []
def getNetworkMethod(self, line):
if line.find("static") != -1:
return "static"
elif line.find("dhcp") != -1:
return "dhcp"
else:
logging.debug("Failed to find the network method from:%s"%line)
return None
def matchEndOfStanzas(self, line):
if line.match("\^ *iface|\^ *mapping|\^ *auto | \^ *allow-") is not None:
return True
else:
return False
def addBridge(self, br, dev):
bash("ifdown %s"%dev.name)
for line in file(self.netCfgFile).readlines():
match = re.match("^ *iface %s.*"%dev.name, line)
if match is not None:
dev.method = self.getNetworkMethod(match.group(0))
bridgeCfg = "\niface %s inet manual\n \
auto %s\n \
iface %s inet %s\n \
bridge_ports %s\n"%(dev.name, br, br, dev.method, dev.name)
cfo = configFileOps(self.netCfgFile)
cfo.replace_line("^ *iface %s.*"%dev.name, bridgeCfg)
self.cfoHandlers.append(cfo)
def addDev(self, br, dev):
logging.debug("Haven't implement yet")
def addBridgeAndDev(self, br, dev):
logging.debug("Haven't implement yet")
def writeToCfgFile(self, br, dev):
cfg = file(self.netCfgFile).read()
ifaceDev = re.search("^ *iface %s.*"%dev.name, cfg, re.MULTILINE)
ifaceBr = re.search("^ *iface %s.*"%br, cfg, re.MULTILINE)
if ifaceDev is not None and ifaceBr is not None:
logging.debug("%s:%s already configured"%(br, dev.name))
return True
elif ifaceDev is not None and ifaceBr is None:
#reconfig bridge
self.addBridge(br, dev)
elif ifaceDev is None and ifaceBr is not None:
#reconfig dev
self.addDev(br, dev)
else:
#both need to be reconfigured
self.addbridgeAndDev(br, dev)
def cfgNetwork(self, dev=None, brName=None):
if dev is None:
device = networkConfig.getDefaultNetwork()
else:
device = networkConfig.getDevInfo(dev)
if device.type == "dev":
#Need to create a bridge on it
if brName is None:
brName = "cloudbr0"
'''
if not networkConfig.createBridge(device.name, brName):
logging.debug("Failed to create bridge:%s on dev %s"%(brName, device.name))
return False
'''
self.writeToCfgFile(brName, device)
elif device.type == "brport":
brName = networkConfig.getBridge(dev)
brDevice = networkConfig.getDevInfo(brName)
self.writeToCfgFile(brDevice, device)
elif device.type == "bridge":
#Fixme, assuming the outgoing physcial device is on port 1
enslavedDev = networkConfig.getEnslavedDev(device.name, 1)
brDevice = device
device = networkConfig.getDevInfo(enslavedDev)
brName = brDevice.name
self.writeToCfgFile(brName, device)
self.brName = brName
self.dev = device.name
def config(self):
writeProgressBar("Configure Network...", None)
try:
self.cfgNetwork(self.syscfg.env.defaultNic)
self.netMgrRunning = self.syscfg.isServiceRunning("network-manager")
if self.netMgrRunning:
self.syscfg.stopService("network-manager")
self.syscfg.disableService("network-manager")
bash("ifup %s"%self.brName)
self.syscfg.env.nics.append(self.brName)
self.syscfg.env.nics.append(self.brName)
self.syscfg.env.nics.append(self.brName)
writeProgressBar(None, True)
self.status = True
except:
logging.debug(formatExceptionInfo())
writeProgressBar(None, False)
self.status = False
def restore(self):
if self.status is None:
return
#restore cfg file at first
writeProgressBar("Restoring Network...", None)
for cfo in self.cfoHandlers:
cfo.backup()
try:
if self.netMgrRunning:
self.syscfg.enableService("network-manager")
self.syscfg.startService("network-manager")
bash("/etc/init.d/networking stop")
bash("/etc/init.d/networking start")
writeProgressBar(None, True)
except:
logging.debug(formatExceptionInfo())
writeProgressBar(None, False)
class cgroupConfigUbuntu():
def __init__(self, syscfg):
self.syscfg = syscfg
self.backup = []
self.status = None
self.cfoHandlers = []
def config(self):
writeProgressBar("Configure cgroup...", None)
try:
cfo = configFileOps("/etc/cgconfig.conf")
addConfig = "group virt {\n \
cpu {\n \
cpu.shares = 9216;\n \
}\n \
}\n"
cfo.add_lines(addConfig)
self.cfoHandlers.append(cfo)
self.syscfg.stopService("cgconfig")
self.syscfg.enableService("cgconfig",forcestart=True)
cfo = configFileOps("/etc/cgrules.conf")
cfgline = "root:/usr/sbin/libvirtd cpu virt/\n"
cfo.add_lines(cfgline)
self.cfoHandlers.append(cfo)
self.syscfg.stopService("cgred")
self.syscfg.enableService("cgred")
writeProgressBar(None, True)
self.status = True
except:
logging.debug(formatExceptionInfo())
writeProgressBar(None, False)
self.status = False
raise CloudRuntimeException("Failed to configure cgroup, please see the /var/log/cloud/setupAgent.log for detail")
def restore(self):
if self.status is None:
return
writeProgressBar("Restoring cgroup...", None)
try:
for cfo in self.cfoHandlers:
cfo.backup()
self.syscfg.stopService("cgconfig")
self.syscfg.enableService("cgconfig",forcestart=True)
self.syscfg.stopService("cgred")
self.syscfg.enableService("cgred")
writeProgressBar(None, True)
except:
writeProgressBar(None, False)
class securityPolicyConfigUbuntu():
def __init__(self, syscfg):
self.syscfg = syscfg
self.status = None
def config(self):
writeProgressBar("Configure Security Policy...", None)
try:
if bash("service apparmor status").getStdout() == "":
self.spRunning = False
return
bash("service apparmor stop")
bash("update-rc.d -f apparmor remove")
self.status = True
writeProgressBar(None, True)
except:
logging.debug(formatExceptionInfo())
self.status = False
writeProgressBar(None, False)
raise CloudRuntimeException("Failed to configure apparmor, please see the /var/log/cloud/setupAgent.log for detail, or you can manually disable it before starting cloudKit")
def restore(self):
if self.status is None:
return
writeProgressBar("Restoring Security Policy...", None)
try:
self.syscfg.enableService("apparmor")
self.syscfg.startService("apparmor")
writeProgressBar(None, True)
except:
writeProgressBar(None, False)
class libvirtConfigUbuntu():
def __init__(self, syscfg):
self.syscfg = syscfg
self.status = None
self.cfoHandlers = []
def setupLiveMigration(self):
stanzas = (
"listen_tcp=1",
'tcp_port="16509"',
'auth_tcp="none"',
"listen_tls=0",
)
cfo = configFileOps("/etc/libvirt/libvirtd.conf")
for stanza in stanzas:
startswith = stanza.split("=")[0] + '='
cfo.replace_or_add_line(startswith,stanza)
self.cfoHandlers.append(cfo)
if os.path.exists("/etc/init/libvirt-bin.conf"):
cfo = configFileOps("/etc/init/libvirt-bin.conf")
cfo.replace_line("exec /usr/sbin/libvirtd","exec /usr/sbin/libvirtd -d -l")
self.cfoHandlers.append(cfo)
else:
cfo = configFileOps("/etc/default/libvirt-bin")
cfo.replace_or_add_line("libvirtd_opts=","libvirtd_opts='-l -d'")
self.cfoHandlers.append(cfo)
def config(self):
writeProgressBar("Configure Libvirt...", None)
try:
cfgline = "export CGROUP_DAEMON='cpu:/virt'"
libvirtfile = "/etc/default/libvirt-bin"
cfo = configFileOps(libvirtfile)
cfo.add_lines(cfgline)
self.cfoHandlers.append(cfo)
self.setupLiveMigration()
cfgline = "cgroup_controllers = [ \"cpu\" ]\n" \
"security_driver = \"none\"\n"
filename = "/etc/libvirt/qemu.conf"
cfo = configFileOps(filename)
cfo.add_lines(cfgline)
self.cfoHandlers.append(cfo)
self.syscfg.stopService("libvirt-bin")
self.syscfg.enableService("libvirt-bin")
writeProgressBar(None, True)
self.status = True
except:
logging.debug(formatExceptionInfo())
writeProgressBar(None, False)
self.status = False
raise CloudRuntimeException("Failed to configure libvirt, please see the /var/log/cloud/setupAgent.log for detail")
def restore(self):
if self.status is None:
return
writeProgressBar("Restoring Libvirt...", None)
for cfo in self.cfoHandlers:
cfo.backup()
try:
self.syscfg.stopService("libvirt-bin")
self.syscfg.startService("libvirt-bin")
writeProgressBar(None, True)
except:
logging.debug(formatExceptionInfo())
writeProgressBar(None, False)
class firewawllConfigUbuntu():
def __init__(self, syscfg):
self.syscfg = syscfg
self.status = None
def config(self):
writeProgressBar("Configure Firewall...", None)
try:
ports = "22 1798 16509".split()
for p in ports:
bash("ufw allow %s"%p)
bash("ufw allow proto tcp from any to any port 5900:6100")
bash("ufw allow proto tcp from any to any port 49152:49216")
self.syscfg.stopService("ufw")
self.syscfg.startService("ufw")
writeProgressBar(None, True)
self.status = True
except:
logging.debug(formatExceptionInfo())
writeProgressBar(None, False)
self.status = False
raise CloudRuntimeException("Failed to configure firewall, please see the /var/log/cloud/setupAgent.log for detail")
def restore(self):
if self.status is None:
return
print "not implemented yet"
return
class cloudKitConfig():
def __init__(self, syscfg):
self.syscfg = syscfg
self.status = None
def config(self):
writeProgressBar("Configure CloudKit...", None)
try:
cfo = configFileOps("/etc/cloud/agent/agent.properties")
cfo.addEntry("host", self.syscfg.env.mgtSvr)
cfo.addEntry("zone", self.syscfg.env.zoneToken)
cfo.addEntry("port", "443")
cfo.addEntry("private.network.device", self.syscfg.env.nics[0])
cfo.addEntry("public.network.device", self.syscfg.env.nics[1])
cfo.addEntry("guest.network.device", self.syscfg.env.nics[2])
cfo.addEntry("guid", str(self.syscfg.env.uuid))
cfo.addEntry("mount.path", "/mnt")
cfo.addEntry("resource", "com.cloud.storage.resource.LocalSecondaryStorageResource|com.cloud.agent.resource.computing.CloudZonesComputingResource")
cfo.save()
self.syscfg.stopService("cloud-agent")
self.syscfg.startService("cloud-agent")
writeProgressBar(None, True)
self.status = True
except:
logging.debug(formatExceptionInfo())
writeProgressBar(None, False)
self.status = False
raise CloudRuntimeException("Failed to configure cloudKit, please see the /var/log/cloud/setupAgent.log for detail")
def restore(self):
pass
Unknown = 0
Fedora = 1
CentOS = 2
RHEL6 = 3
RHEL5 = 4
Ubuntu = 5
class DistributionDetector():
def __init__(self):
self.distro = Unknown
if os.path.exists("/etc/fedora-release"):
self.distro = Fedora
elif os.path.exists("/etc/centos-release"):
self.distro = CentOS
elif os.path.exists("/etc/redhat-release"):
version = file("/etc/redhat-release").readline()
if version.find("Red Hat Enterprise Linux Server release 6") != -1:
self.distro = RHEL6
elif version.find("CentOS release") != -1:
self.distro = CentOS
else:
self.distro = RHEL5
elif os.path.exists("/etc/legal") and "Ubuntu" in file("/etc/legal").read(-1):
self.distro = Ubuntu
else:
self.distro = Unknown
def getVersion(self):
return self.distro
class sysConfig(object):
@staticmethod
def getSysConfigFactory(glbEnv):
distribution = DistributionDetector().getVersion()
if distribution == Ubuntu:
return sysConfigUbuntu(glbEnv)
else:
return sysConfig()
def config(self):
pass
def restore(self):
pass
class sysConfigUbuntu(sysConfig):
def __init__(self, glbEnv):
self.env = glbEnv
self.services = [cgroupConfigUbuntu(self),
securityPolicyConfigUbuntu(self),
networkConfigUbuntu(self),
libvirtConfigUbuntu(self),
firewawllConfigUbuntu(self),
cloudKitConfig(self)]
def config(self):
if not self.check():
return False
for service in self.services:
service.config()
def restore(self):
for service in self.services:
service.restore()
def check(self):
try:
return self.isKVMEnabled()
except:
raise CloudRuntimeException("Checking KVM...[Failed]\nPlease enable KVM on this machine\n")
def isServiceRunning(self, servicename):
try:
o = bash("service " + servicename + " status")
if "start/running" in o.getStdout():
return True
else:
return False
except:
return False
def stopService(self, servicename,force=False):
if self.isServiceRunning(servicename) or force:
return bash("service " + servicename +" stop").isSuccess()
def disableService(self, servicename):
self.stopService(servicename)
bash("update-rc.d -f " + servicename + " remove")
def startService(self, servicename,force=False):
if not self.isServiceRunning(servicename) or force:
bash("service " + servicename + " start")
def enableService(self, servicename,forcestart=False):
bash("update-rc.d -f " + servicename + " remove")
bash("update-rc.d -f " + servicename + " start 2 3 4 5 .")
self.startService(servicename,force=forcestart)
def isKVMEnabled(self):
return bash("kvm-ok").isSuccess()
def getUserInputs():
print "Welcome to CloudKit Setup:"
cfo = configFileOps("/etc/cloud/agent/agent.properties")
oldMgt = cfo.getEntry("host")
mgtSvr = raw_input("Please input the Management Server Name/IP:[%s]"%oldMgt)
if mgtSvr == "":
mgtSvr = oldMgt
try:
socket.getaddrinfo(mgtSvr, 443)
except:
print "Failed to resolve %s. Please input correct server name or IP."%mgtSvr
exit(1)
oldToken = cfo.getEntry("zone")
zoneToken = raw_input("Please input the Zone Token:[%s]"%oldToken)
if zoneToken == "":
zoneToken = oldToken
try:
defaultNic = networkConfig.getDefaultNetwork()
except:
print "Failed to get default route. Please configure your network to have a default route"
exit(1)
defNic = defaultNic.name
network = raw_input("Please choose which network used to create VM:[%s]"%defNic)
if network == "":
if defNic == "":
print "You need to specifiy one of Nic or bridge on your system"
exit(1)
elif network == "":
network = defNic
return [mgtSvr,zoneToken, network]
def writeProgressBar(msg, result):
if msg is not None:
output = "%-30s"%msg
elif result is True:
output = "[%-2s]\n"%"OK"
elif result is False:
output = "[%-6s]\n"%"Failed"
sys.stdout.write(output)
sys.stdout.flush()
class globalEnv():
pass
if __name__ == '__main__':
#todo: check executing permission
initLoging("/var/log/cloud/setupAgent.log")
userInputs = getUserInputs()
glbEnv = globalEnv()
glbEnv.mgtSvr = userInputs[0]
glbEnv.zoneToken = userInputs[1]
glbEnv.defaultNic = userInputs[2]
glbEnv.nics = []
#generate UUID
glbEnv.uuid = configFileOps("/etc/cloud/agent/agent.properties").getEntry("guid")
if glbEnv.uuid == "":
glbEnv.uuid = uuid.uuid1()
print "Starting to configure your system:"
syscfg = sysConfig.getSysConfigFactory(glbEnv)
try:
syscfg.config()
print "Cloudkit setup is Done!"
except CloudRuntimeException, e:
print e
print "Try to restore your system:"
try:
syscfg.restore()
except:
pass

View File

@ -1,25 +1,4 @@
#!/usr/bin/python
#
# Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
#
# This software is licensed under the GNU General Public License v3 or later.
#
# It is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import cloud_utils
from cloud_utils import Command
import logging
@ -84,7 +63,7 @@ def ipset(ipsetname, proto, start, end, ips):
return result
'''
def destroy_network_rules_for_vm(vm_name):
def destroy_network_rules_for_vm(vm_name, vif=None):
vmchain = vm_name
vmchain_default = None
@ -116,6 +95,16 @@ def destroy_network_rules_for_vm(vm_name):
except:
logging.debug("Ignoring failure to delete chain " + vmchain)
if vif is not None:
try:
dnats = execute("iptables -t nat -S | grep " + vif + " | sed 's/-A/-D/'").split("\n")
for dnat in dnats:
try:
execute("iptables -t nat " + dnat)
except:
logging.debug("Igoring failure to delete dnat: " + dnat)
except:
pass
remove_rule_log_for_vm(vm_name)
if 1 in [ vm_name.startswith(c) for c in ['r-', 's-', 'v-'] ]:
@ -162,7 +151,8 @@ def default_ebtables_rules(vm_name, vm_ip, vm_mac, vif):
execute("ebtables -t nat -A " + vmchain_in + " -s ! " + vm_mac + " -j DROP")
execute("ebtables -t nat -A " + vmchain_in + " -p ARP -s ! " + vm_mac + " -j DROP")
execute("ebtables -t nat -A " + vmchain_in + " -p ARP --arp-mac-src ! " + vm_mac + " -j DROP")
execute("ebtables -t nat -A " + vmchain_in + " -p ARP --arp-ip-src ! " + vm_ip + " -j DROP")
if vm_ip is not None:
execute("ebtables -t nat -A " + vmchain_in + " -p ARP --arp-ip-src ! " + vm_ip + " -j DROP")
execute("ebtables -t nat -A " + vmchain_in + " -p ARP --arp-op Request -j ACCEPT")
execute("ebtables -t nat -A " + vmchain_in + " -p ARP --arp-op Reply -j ACCEPT")
execute("ebtables -t nat -A " + vmchain_in + " -p ARP -j DROP")
@ -172,7 +162,8 @@ def default_ebtables_rules(vm_name, vm_ip, vm_mac, vif):
try:
execute("ebtables -t nat -A " + vmchain_out + " -p ARP --arp-op Reply --arp-mac-dst ! " + vm_mac + " -j DROP")
execute("ebtables -t nat -A " + vmchain_out + " -p ARP --arp-ip-dst ! " + vm_ip + " -j DROP")
if vm_ip is not None:
execute("ebtables -t nat -A " + vmchain_out + " -p ARP --arp-ip-dst ! " + vm_ip + " -j DROP")
execute("ebtables -t nat -A " + vmchain_out + " -p ARP --arp-op Request -j ACCEPT")
execute("ebtables -t nat -A " + vmchain_out + " -p ARP --arp-op Reply -j ACCEPT")
execute("ebtables -t nat -A " + vmchain_out + " -p ARP -j DROP")
@ -244,7 +235,8 @@ def default_network_rules(vm_name, vm_id, vm_ip, vm_mac, vif, brname):
execute("iptables -A " + vmchain_default + " -m physdev --physdev-is-bridged --physdev-out " + vif + " -p udp --dport 68 --sport 67 -j ACCEPT")
#don't let vm spoof its ip address
execute("iptables -A " + vmchain_default + " -m physdev --physdev-is-bridged --physdev-in " + vif + " --source " + vm_ip + " -j ACCEPT")
if vm_ip is not None:
execute("iptables -A " + vmchain_default + " -m physdev --physdev-is-bridged --physdev-in " + vif + " --source " + vm_ip + " -j ACCEPT")
execute("iptables -A " + vmchain_default + " -j " + vmchain)
execute("iptables -A " + vmchain + " -j DROP")
except:
@ -253,12 +245,42 @@ def default_network_rules(vm_name, vm_id, vm_ip, vm_mac, vif, brname):
default_ebtables_rules(vmchain, vm_ip, vm_mac, vif)
if write_rule_log_for_vm(vmName, vm_id, vm_ip, domID, '_initial_', '-1') == False:
logging.debug("Failed to log default network rules, ignoring")
if vm_ip is not None:
if write_rule_log_for_vm(vmName, vm_id, vm_ip, domID, '_initial_', '-1') == False:
logging.debug("Failed to log default network rules, ignoring")
logging.debug("Programmed default rules for vm " + vm_name)
return 'true'
def post_default_network_rules(vm_name, vm_id, vm_ip, vm_mac, vif, brname, dhcpSvr, hostIp, hostMacAddr):
vmchain_default = '-'.join(vm_name.split('-')[:-1]) + "-def"
vmchain_in = vm_name + "-in"
vmchain_out = vm_name + "-out"
domID = getvmId(vm_name)
try:
execute("iptables -I " + vmchain_default + " 4 -m physdev --physdev-is-bridged --physdev-in " + vif + " --source " + vm_ip + " -j ACCEPT")
except:
pass
try:
execute("iptables -t nat -A PREROUTING -p tcp -m physdev --physdev-in " + vif + " -m tcp --dport 80 -d " + dhcpSvr + " -j DNAT --to-destination " + hostIp + ":80")
except:
pass
try:
execute("ebtables -t nat -A PREROUTING -i " + vif + " -p IPv4 --ip-protocol tcp --ip-destination-port 80 -j dnat --to-destination " + hostMacAddr)
except:
pass
try:
execute("ebtables -t nat -I " + vmchain_in + " 4 -p ARP --arp-ip-src ! " + vm_ip + " -j DROP")
except:
pass
try:
execute("ebtables -t nat -I " + vmchain_out + " 2 -p ARP --arp-ip-dst ! " + vm_ip + " -j DROP")
except:
pass
if write_rule_log_for_vm(vm_name, vm_id, vm_ip, domID, '_initial_', '-1') == False:
logging.debug("Failed to log default network rules, ignoring")
def delete_rules_for_vm_in_bridge_firewall_chain(vmName):
vm_name = vmName
if vm_name.startswith('i-') or vm_name.startswith('r-'):
@ -582,6 +604,9 @@ if __name__ == '__main__':
parser.add_option("--seq", dest="seq")
parser.add_option("--rules", dest="rules")
parser.add_option("--brname", dest="brname")
parser.add_option("--dhcpSvr", dest="dhcpSvr")
parser.add_option("--hostIp", dest="hostIp")
parser.add_option("--hostMacAddr", dest="hostMacAddr")
(option, args) = parser.parse_args()
cmd = args[0]
if cmd == "can_bridge_firewall":
@ -589,7 +614,7 @@ if __name__ == '__main__':
elif cmd == "default_network_rules":
default_network_rules(option.vmName, option.vmID, option.vmIP, option.vmMAC, option.vif, option.brname)
elif cmd == "destroy_network_rules_for_vm":
destroy_network_rules_for_vm(option.vmName)
destroy_network_rules_for_vm(option.vmName, option.vif)
elif cmd == "default_network_rules_systemvm":
default_network_rules_systemvm(option.vmName, option.brname)
elif cmd == "get_rule_logs_for_vms":
@ -598,3 +623,5 @@ if __name__ == '__main__':
add_network_rules(option.vmName, option.vmID, option.vmIP, option.sig, option.seq, option.vmMAC, option.rules, option.vif, option.brname)
elif cmd == "cleanup_rules":
cleanup_rules()
elif cmd == "post_default_network_rules":
post_default_network_rules(option.vmName, option.vmID, option.vmIP, option.vmMAC, option.vif, option.brname, option.dhcpSvr, option.hostIp, option.hostMacAddr)

View File

@ -140,7 +140,25 @@ public interface AgentManager extends Manager {
* @return id to unregister if needed.
*/
int registerForHostEvents(Listener listener, boolean connections, boolean commands, boolean priority);
/**
* Register to listen for initial agent connections.
* @param creator
* @param priority in listening for events.
* @return id to unregister if needed.
*/
int registerForInitialConnects(StartupCommandProcessor creator, boolean priority);
/**
* Register to listen for initial agent connections.
* @param creator
* @param priority in listening for events.
* @return id to unregister if needed.
*/
int registerForInitialConnects(StartupCommandProcessor creator, boolean priority);
/**
* Unregister for listening to host events.
*

View File

@ -43,6 +43,7 @@ import javax.naming.ConfigurationException;
import org.apache.log4j.Logger;
import com.cloud.agent.AgentManager;
import com.cloud.agent.StartupCommandProcessor;
import com.cloud.agent.Listener;
import com.cloud.agent.api.AgentControlAnswer;
import com.cloud.agent.api.AgentControlCommand;
@ -210,6 +211,7 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, ResourceS
protected ConcurrentHashMap<Long, AgentAttache> _agents = new ConcurrentHashMap<Long, AgentAttache>(10007);
protected List<Pair<Integer, Listener>> _hostMonitors = new ArrayList<Pair<Integer, Listener>>(17);
protected List<Pair<Integer, Listener>> _cmdMonitors = new ArrayList<Pair<Integer, Listener>>(17);
protected List<Pair<Integer, StartupCommandProcessor>> _creationMonitors = new ArrayList<Pair<Integer, StartupCommandProcessor>>(17);
protected int _monitorId = 0;
protected NioServer _connection;
@ -424,6 +426,23 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, ResourceS
}
}
@Override
public int registerForInitialConnects(final StartupCommandProcessor creator,boolean priority) {
synchronized (_hostMonitors) {
_monitorId++;
if (priority) {
_creationMonitors.add(0, new Pair<Integer, StartupCommandProcessor>(
_monitorId, creator));
} else {
_creationMonitors.add(0, new Pair<Integer, StartupCommandProcessor>(
_monitorId, creator));
}
}
return _monitorId;
}
@Override
public void unregisterForHostEvents(final int id) {
s_logger.debug("Deregistering " + id);
@ -1741,6 +1760,22 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, ResourceS
return attache;
}
protected boolean notifyCreatorsOfConnection(StartupCommand[] cmd) throws ConnectionException {
boolean handled = false;
for (Pair<Integer, StartupCommandProcessor> monitor : _creationMonitors) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Sending Connect to creator: "
+ monitor.second().getClass().getSimpleName());
}
handled = monitor.second().processInitialConnect(cmd);
if (handled) {
break;
}
}
return handled;
}
@Override
public boolean start() {
startDirectlyConnectedHosts();
@ -2488,24 +2523,27 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, ResourceS
return result;
}
public AgentAttache handleConnect(final Link link, final StartupCommand[] startup) throws IllegalArgumentException, ConnectionException {
HostVO server = createHost(startup, null, null, false, null, null);
if (server == null) {
return null;
}
long id = server.getId();
public AgentAttache handleConnect(final Link link,
final StartupCommand[] startup) throws IllegalArgumentException,
ConnectionException {
HostVO server = null;
boolean handled = notifyCreatorsOfConnection(startup);
if (!handled) {
server = createHost(startup, null, null, false, null, null);
} else {
server = _hostDao.findByGuid(startup[0].getGuid());
}
if (server == null) {
return null;
}
long id = server.getId();
AgentAttache attache = createAttache(id, server, link);
AgentAttache attache = createAttache(id, server, link);
attache = notifyMonitorsOfConnection(attache, startup);
attache = notifyMonitorsOfConnection(attache, startup);
return attache;
}
public AgentAttache findAgent(long hostId) {
synchronized (_agents) {
return _agents.get(hostId);
}
return attache;
}
protected AgentAttache createAttache(long id, HostVO server, Link link) {

View File

@ -0,0 +1,36 @@
/**
* Copyright (C) 2011 Cloud.com, Inc. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.agent.manager.authn;
/**
* Exception indicates authentication and/OR authorization problem
*
*/
public class AgentAuthnException extends Exception{
private static final long serialVersionUID = 3303508953403051189L;
public AgentAuthnException(String message, Throwable cause) {
super(message, cause);
}
public AgentAuthnException(String message) {
super(message);
}
}

View File

@ -0,0 +1,27 @@
/**
* Copyright (C) 2011 Cloud.com. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later
version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.agent.manager.authn;
import com.cloud.agent.api.StartupCommand;
import com.cloud.utils.component.Adapter;
public interface AgentAuthorizer extends Adapter {
boolean authorizeAgent(StartupCommand [] cmd) throws AgentAuthnException;
}

View File

@ -0,0 +1,66 @@
package com.cloud.agent.manager.authn.impl;
import java.util.Map;
import javax.ejb.Local;
import javax.naming.ConfigurationException;
import org.apache.log4j.Logger;
import com.cloud.agent.AgentManager;
import com.cloud.agent.StartupCommandProcessor;
import com.cloud.agent.api.StartupCommand;
import com.cloud.agent.manager.authn.AgentAuthnException;
import com.cloud.agent.manager.authn.AgentAuthorizer;
import com.cloud.configuration.dao.ConfigurationDao;
import com.cloud.exception.ConnectionException;
import com.cloud.host.dao.HostDao;
import com.cloud.utils.component.Inject;
@Local(value={AgentAuthorizer.class, StartupCommandProcessor.class})
public class BasicAgentAuthManager implements AgentAuthorizer, StartupCommandProcessor {
private static final Logger s_logger = Logger.getLogger(BasicAgentAuthManager.class);
@Inject HostDao _hostDao = null;
@Inject ConfigurationDao _configDao = null;
@Inject AgentManager _agentManager = null;
@Override
public boolean processInitialConnect(StartupCommand[] cmd)
throws ConnectionException {
try {
authorizeAgent(cmd);
} catch (AgentAuthnException e) {
throw new ConnectionException(true, "Failed to authenticate/authorize", e);
}
s_logger.debug("Authorized agent with guid " + cmd[0].getGuid());
return false;//so that the next host creator can process it
}
@Override
public boolean authorizeAgent(StartupCommand[] cmd) throws AgentAuthnException{
return true;
}
@Override
public boolean configure(String name, Map<String, Object> params)
throws ConfigurationException {
_agentManager.registerForInitialConnects(this, true);
return true;
}
@Override
public String getName() {
return getClass().getName();
}
@Override
public boolean start() {
return true;
}
@Override
public boolean stop() {
return true;
}
}

View File

@ -1508,6 +1508,8 @@ public class ApiResponseHelper implements ResponseGenerator {
templateResponse.setChecksum(template.getChecksum());
templateResponse.setSourceTemplateId(template.getSourceTemplateId());
templateResponse.setChecksum(template.getChecksum());
templateResponse.setObjectName("template");
responses.add(templateResponse);
}

File diff suppressed because it is too large Load Diff

View File

@ -29,6 +29,9 @@ import javax.naming.ConfigurationException;
import org.apache.log4j.Logger;
import com.cloud.agent.AgentManager;
import com.cloud.agent.api.StartupCommand;
import com.cloud.agent.api.StartupRoutingCommand;
import com.cloud.capacity.dao.CapacityDao;
import com.cloud.configuration.Config;
import com.cloud.configuration.dao.ConfigurationDao;
@ -60,12 +63,15 @@ public class CapacityManagerImpl implements CapacityManager , StateListener<Stat
@Inject ServiceOfferingDao _offeringsDao;
@Inject HostDao _hostDao;
@Inject VMInstanceDao _vmDao;
@Inject AgentManager _agentManager;
private int _hostCapacityCheckerDelay;
private int _hostCapacityCheckerInterval;
private int _vmCapacityReleaseInterval;
private ScheduledExecutorService _executor;
private boolean _stopped;
private float _storageOverProvisioningFactor = 1.0f;
private float _cpuOverProvisioningFactor = 1.0f;
@Override
@ -74,8 +80,17 @@ public class CapacityManagerImpl implements CapacityManager , StateListener<Stat
_hostCapacityCheckerDelay = NumbersUtil.parseInt(_configDao.getValue(Config.HostCapacityCheckerWait.key()), 3600);
_hostCapacityCheckerInterval = NumbersUtil.parseInt(_configDao.getValue(Config.HostCapacityCheckerInterval.key()), 3600);
_vmCapacityReleaseInterval = NumbersUtil.parseInt(_configDao.getValue(Config.CapacitySkipcountingHours.key()), 3600);
_storageOverProvisioningFactor = NumbersUtil.parseFloat(_configDao.getValue(Config.StorageOverprovisioningFactor.key()), 1.0f);
_cpuOverProvisioningFactor = NumbersUtil.parseFloat(_configDao.getValue(Config.CPUOverprovisioningFactor.key()), 1.0f);
if (_cpuOverProvisioningFactor < 1.0f) {
_cpuOverProvisioningFactor = 1.0f;
}
_executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("HostCapacity-Checker"));
VirtualMachine.State.getStateMachine().registerListener(this);
_agentManager.registerForHostEvents(new StorageCapacityListener(_capacityDao, _storageOverProvisioningFactor), true, false, false);
_agentManager.registerForHostEvents(new ComputeCapacityListener(_capacityDao, _cpuOverProvisioningFactor), true, false, false);
return true;
}
@ -523,4 +538,106 @@ public class CapacityManagerImpl implements CapacityManager , StateListener<Stat
return true;
}
// create capacity entries if none exist for this server
private void createCapacityEntry(final StartupCommand startup, HostVO server) {
SearchCriteria<CapacityVO> capacitySC = _capacityDao
.createSearchCriteria();
capacitySC.addAnd("hostOrPoolId", SearchCriteria.Op.EQ, server.getId());
capacitySC.addAnd("dataCenterId", SearchCriteria.Op.EQ,
server.getDataCenterId());
capacitySC.addAnd("podId", SearchCriteria.Op.EQ, server.getPodId());
if (startup instanceof StartupRoutingCommand) {
SearchCriteria<CapacityVO> capacityCPU = _capacityDao
.createSearchCriteria();
capacityCPU.addAnd("hostOrPoolId", SearchCriteria.Op.EQ,
server.getId());
capacityCPU.addAnd("dataCenterId", SearchCriteria.Op.EQ,
server.getDataCenterId());
capacityCPU
.addAnd("podId", SearchCriteria.Op.EQ, server.getPodId());
capacityCPU.addAnd("capacityType", SearchCriteria.Op.EQ,
CapacityVO.CAPACITY_TYPE_CPU);
List<CapacityVO> capacityVOCpus = _capacityDao.search(capacitySC,
null);
if (capacityVOCpus != null && !capacityVOCpus.isEmpty()) {
CapacityVO CapacityVOCpu = capacityVOCpus.get(0);
long newTotalCpu = (long) (server.getCpus().longValue()
* server.getSpeed().longValue() * _cpuOverProvisioningFactor);
if ((CapacityVOCpu.getTotalCapacity() <= newTotalCpu)
|| ((CapacityVOCpu.getUsedCapacity() + CapacityVOCpu
.getReservedCapacity()) <= newTotalCpu)) {
CapacityVOCpu.setTotalCapacity(newTotalCpu);
} else if ((CapacityVOCpu.getUsedCapacity()
+ CapacityVOCpu.getReservedCapacity() > newTotalCpu)
&& (CapacityVOCpu.getUsedCapacity() < newTotalCpu)) {
CapacityVOCpu.setReservedCapacity(0);
CapacityVOCpu.setTotalCapacity(newTotalCpu);
} else {
s_logger.debug("What? new cpu is :" + newTotalCpu
+ ", old one is " + CapacityVOCpu.getUsedCapacity()
+ "," + CapacityVOCpu.getReservedCapacity() + ","
+ CapacityVOCpu.getTotalCapacity());
}
_capacityDao.update(CapacityVOCpu.getId(), CapacityVOCpu);
} else {
CapacityVO capacity = new CapacityVO(
server.getId(),
server.getDataCenterId(),
server.getPodId(),
server.getClusterId(),
0L,
(long) (server.getCpus().longValue()
* server.getSpeed().longValue() * _cpuOverProvisioningFactor),
CapacityVO.CAPACITY_TYPE_CPU);
_capacityDao.persist(capacity);
}
SearchCriteria<CapacityVO> capacityMem = _capacityDao
.createSearchCriteria();
capacityMem.addAnd("hostOrPoolId", SearchCriteria.Op.EQ,
server.getId());
capacityMem.addAnd("dataCenterId", SearchCriteria.Op.EQ,
server.getDataCenterId());
capacityMem
.addAnd("podId", SearchCriteria.Op.EQ, server.getPodId());
capacityMem.addAnd("capacityType", SearchCriteria.Op.EQ,
CapacityVO.CAPACITY_TYPE_MEMORY);
List<CapacityVO> capacityVOMems = _capacityDao.search(capacityMem,
null);
if (capacityVOMems != null && !capacityVOMems.isEmpty()) {
CapacityVO CapacityVOMem = capacityVOMems.get(0);
long newTotalMem = server.getTotalMemory();
if (CapacityVOMem.getTotalCapacity() <= newTotalMem
|| (CapacityVOMem.getUsedCapacity()
+ CapacityVOMem.getReservedCapacity() <= newTotalMem)) {
CapacityVOMem.setTotalCapacity(newTotalMem);
} else if (CapacityVOMem.getUsedCapacity()
+ CapacityVOMem.getReservedCapacity() > newTotalMem
&& CapacityVOMem.getUsedCapacity() < newTotalMem) {
CapacityVOMem.setReservedCapacity(0);
CapacityVOMem.setTotalCapacity(newTotalMem);
} else {
s_logger.debug("What? new cpu is :" + newTotalMem
+ ", old one is " + CapacityVOMem.getUsedCapacity()
+ "," + CapacityVOMem.getReservedCapacity() + ","
+ CapacityVOMem.getTotalCapacity());
}
_capacityDao.update(CapacityVOMem.getId(), CapacityVOMem);
} else {
CapacityVO capacity = new CapacityVO(server.getId(),
server.getDataCenterId(), server.getPodId(), server.getClusterId(), 0L,
server.getTotalMemory(),
CapacityVO.CAPACITY_TYPE_MEMORY);
_capacityDao.persist(capacity);
}
}
}
}

View File

@ -0,0 +1,195 @@
/**
* Copyright (C) 2010 Cloud.com. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later
version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.capacity;
import java.util.List;
import org.apache.log4j.Logger;
import com.cloud.agent.Listener;
import com.cloud.agent.api.AgentControlAnswer;
import com.cloud.agent.api.AgentControlCommand;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.StartupCommand;
import com.cloud.agent.api.StartupRoutingCommand;
import com.cloud.agent.api.StartupStorageCommand;
import com.cloud.capacity.dao.CapacityDao;
import com.cloud.exception.ConnectionException;
import com.cloud.host.HostVO;
import com.cloud.host.Status;
import com.cloud.utils.db.SearchCriteria;
public class ComputeCapacityListener implements Listener {
private static final Logger s_logger = Logger.getLogger(ComputeCapacityListener.class);
CapacityDao _capacityDao;
float _cpuOverProvisioningFactor = 1.0f;
public ComputeCapacityListener(CapacityDao _capacityDao,
float _overProvisioningFactor) {
super();
this._capacityDao = _capacityDao;
this._cpuOverProvisioningFactor = _overProvisioningFactor;
}
@Override
public boolean processAnswers(long agentId, long seq, Answer[] answers) {
return false;
}
@Override
public boolean processCommands(long agentId, long seq, Command[] commands) {
return false;
}
@Override
public AgentControlAnswer processControlCommand(long agentId,
AgentControlCommand cmd) {
return null;
}
@Override
public void processConnect(HostVO server, StartupCommand startup) throws ConnectionException {
if (!(startup instanceof StartupRoutingCommand)) {
return;
}
SearchCriteria<CapacityVO> capacityCPU = _capacityDao
.createSearchCriteria();
capacityCPU.addAnd("hostOrPoolId", SearchCriteria.Op.EQ,
server.getId());
capacityCPU.addAnd("dataCenterId", SearchCriteria.Op.EQ,
server.getDataCenterId());
capacityCPU
.addAnd("podId", SearchCriteria.Op.EQ, server.getPodId());
capacityCPU.addAnd("capacityType", SearchCriteria.Op.EQ,
CapacityVO.CAPACITY_TYPE_CPU);
List<CapacityVO> capacityVOCpus = _capacityDao.search(capacityCPU,
null);
if (capacityVOCpus != null && !capacityVOCpus.isEmpty()) {
CapacityVO CapacityVOCpu = capacityVOCpus.get(0);
long newTotalCpu = (long) (server.getCpus().longValue()
* server.getSpeed().longValue() * _cpuOverProvisioningFactor);
if ((CapacityVOCpu.getTotalCapacity() <= newTotalCpu)
|| ((CapacityVOCpu.getUsedCapacity() + CapacityVOCpu
.getReservedCapacity()) <= newTotalCpu)) {
CapacityVOCpu.setTotalCapacity(newTotalCpu);
} else if ((CapacityVOCpu.getUsedCapacity()
+ CapacityVOCpu.getReservedCapacity() > newTotalCpu)
&& (CapacityVOCpu.getUsedCapacity() < newTotalCpu)) {
CapacityVOCpu.setReservedCapacity(0);
CapacityVOCpu.setTotalCapacity(newTotalCpu);
} else {
s_logger.debug("What? new cpu is :" + newTotalCpu
+ ", old one is " + CapacityVOCpu.getUsedCapacity()
+ "," + CapacityVOCpu.getReservedCapacity() + ","
+ CapacityVOCpu.getTotalCapacity());
}
_capacityDao.update(CapacityVOCpu.getId(), CapacityVOCpu);
} else {
CapacityVO capacity = new CapacityVO(
server.getId(),
server.getDataCenterId(),
server.getPodId(),
server.getClusterId(),
0L,
(long) (server.getCpus().longValue()
* server.getSpeed().longValue() * _cpuOverProvisioningFactor),
CapacityVO.CAPACITY_TYPE_CPU);
_capacityDao.persist(capacity);
}
SearchCriteria<CapacityVO> capacityMem = _capacityDao
.createSearchCriteria();
capacityMem.addAnd("hostOrPoolId", SearchCriteria.Op.EQ,
server.getId());
capacityMem.addAnd("dataCenterId", SearchCriteria.Op.EQ,
server.getDataCenterId());
capacityMem
.addAnd("podId", SearchCriteria.Op.EQ, server.getPodId());
capacityMem.addAnd("capacityType", SearchCriteria.Op.EQ,
CapacityVO.CAPACITY_TYPE_MEMORY);
List<CapacityVO> capacityVOMems = _capacityDao.search(capacityMem,
null);
if (capacityVOMems != null && !capacityVOMems.isEmpty()) {
CapacityVO CapacityVOMem = capacityVOMems.get(0);
long newTotalMem = server.getTotalMemory();
if (CapacityVOMem.getTotalCapacity() <= newTotalMem
|| (CapacityVOMem.getUsedCapacity()
+ CapacityVOMem.getReservedCapacity() <= newTotalMem)) {
CapacityVOMem.setTotalCapacity(newTotalMem);
} else if (CapacityVOMem.getUsedCapacity()
+ CapacityVOMem.getReservedCapacity() > newTotalMem
&& CapacityVOMem.getUsedCapacity() < newTotalMem) {
CapacityVOMem.setReservedCapacity(0);
CapacityVOMem.setTotalCapacity(newTotalMem);
} else {
s_logger.debug("What? new cpu is :" + newTotalMem
+ ", old one is " + CapacityVOMem.getUsedCapacity()
+ "," + CapacityVOMem.getReservedCapacity() + ","
+ CapacityVOMem.getTotalCapacity());
}
_capacityDao.update(CapacityVOMem.getId(), CapacityVOMem);
} else {
CapacityVO capacity = new CapacityVO(server.getId(),
server.getDataCenterId(), server.getPodId(), server.getClusterId(), 0L,
server.getTotalMemory(),
CapacityVO.CAPACITY_TYPE_MEMORY);
_capacityDao.persist(capacity);
}
}
@Override
public boolean processDisconnect(long agentId, Status state) {
return false;
}
@Override
public boolean isRecurring() {
return false;
}
@Override
public int getTimeout() {
return 0;
}
@Override
public boolean processTimeout(long agentId, long seq) {
return false;
}
}

View File

@ -0,0 +1,127 @@
/**
* Copyright (C) 2010 Cloud.com. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later
version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.capacity;
import java.util.List;
import com.cloud.agent.Listener;
import com.cloud.agent.api.AgentControlAnswer;
import com.cloud.agent.api.AgentControlCommand;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.StartupCommand;
import com.cloud.agent.api.StartupStorageCommand;
import com.cloud.capacity.dao.CapacityDao;
import com.cloud.exception.ConnectionException;
import com.cloud.host.HostVO;
import com.cloud.host.Status;
import com.cloud.storage.Storage;
import com.cloud.utils.db.SearchCriteria;
public class StorageCapacityListener implements Listener {
CapacityDao _capacityDao;
float _overProvisioningFactor = 1.0f;
public StorageCapacityListener(CapacityDao _capacityDao,
float _overProvisioningFactor) {
super();
this._capacityDao = _capacityDao;
this._overProvisioningFactor = _overProvisioningFactor;
}
@Override
public boolean processAnswers(long agentId, long seq, Answer[] answers) {
return false;
}
@Override
public boolean processCommands(long agentId, long seq, Command[] commands) {
return false;
}
@Override
public AgentControlAnswer processControlCommand(long agentId,
AgentControlCommand cmd) {
return null;
}
@Override
public void processConnect(HostVO server, StartupCommand startup) throws ConnectionException {
if (!(startup instanceof StartupStorageCommand)) {
return;
}
SearchCriteria<CapacityVO> capacitySC = _capacityDao.createSearchCriteria();
capacitySC.addAnd("hostOrPoolId", SearchCriteria.Op.EQ, server.getId());
capacitySC.addAnd("dataCenterId", SearchCriteria.Op.EQ,
server.getDataCenterId());
capacitySC.addAnd("podId", SearchCriteria.Op.EQ, server.getPodId());
List<CapacityVO> capacities = _capacityDao.search(capacitySC, null);
// remove old entries, we'll recalculate them anyway
if ((capacities != null) && !capacities.isEmpty()) {
for (CapacityVO capacity : capacities) {
_capacityDao.remove(capacity.getId());
}
}
StartupStorageCommand ssCmd = (StartupStorageCommand) startup;
if (ssCmd.getResourceType() == Storage.StorageResourceType.STORAGE_HOST) {
CapacityVO capacity = new CapacityVO(server.getId(),
server.getDataCenterId(), server.getPodId(), server.getClusterId(), 0L,
(long) (server.getTotalSize() * _overProvisioningFactor),
CapacityVO.CAPACITY_TYPE_STORAGE_ALLOCATED);
_capacityDao.persist(capacity);
}
}
@Override
public boolean processDisconnect(long agentId, Status state) {
return false;
}
@Override
public boolean isRecurring() {
return false;
}
@Override
public int getTimeout() {
return 0;
}
@Override
public boolean processTimeout(long agentId, long seq) {
return false;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -238,7 +238,8 @@ public enum Config {
DefaultMaxAccountTemplates("Account Defaults", ManagementServer.class, Long.class, "max.account.templates", "20", "The default maximum number of templates that can be deployed for an account", null),
DefaultMaxAccountSnapshots("Account Defaults", ManagementServer.class, Long.class, "max.account.snapshots", "20", "The default maximum number of snapshots that can be created for an account", null),
DefaultMaxAccountVolumes("Account Defaults", ManagementServer.class, Long.class, "max.account.volumes", "20", "The default maximum number of volumes that can be created for an account", null);
EndpointeUrl("Advanced", ManagementServer.class, String.class, "endpointe.url", "http://localhost:8080/client/api", "Endpointe Url", null);
private final String _category;
private final Class<?> _componentClass;
private final Class<?> _type;

View File

@ -0,0 +1,87 @@
/**
* Copyright (C) 2011 Cloud.com. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later
version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.configuration;
import java.util.ArrayList;
import java.util.List;
public enum ZoneConfig {
EnableSecStorageVm( Boolean.class, "enable.secstorage.vm", "true", "Enables secondary storage vm service", null),
EnableConsoleProxyVm( Boolean.class, "enable.consoleproxy.vm", "true", "Enables console proxy vm service", null),
MaxHosts( Long.class, "max.hosts", null, "Maximum number of hosts the Zone can have", null),
MaxVirtualMachines( Long.class, "max.vms", null, "Maximum number of VMs the Zone can have", null),
ZoneMode( String.class, "zone.mode", null, "Mode of the Zone", "Free,Basic,Advanced"),
HasNoPublicIp(Boolean.class, "has.no.public.ip", "false", "True if Zone has no public IP", null),
DhcpStrategy(String.class, "zone.dhcp.strategy", "cloudstack-systemvm", "Who controls DHCP", "cloudstack-systemvm,cloudstack-external,external");
private final Class<?> _type;
private final String _name;
private final String _defaultValue;
private final String _description;
private final String _range;
private static final List<String> _zoneConfigKeys = new ArrayList<String>();
static {
// Add keys into List
for (ZoneConfig c : ZoneConfig.values()) {
String key = c.key();
_zoneConfigKeys.add(key);
}
}
private ZoneConfig( Class<?> type, String name, String defaultValue, String description, String range) {
_type = type;
_name = name;
_defaultValue = defaultValue;
_description = description;
_range = range;
}
public Class<?> getType() {
return _type;
}
public String getName() {
return _name;
}
public String getDefaultValue() {
return _defaultValue;
}
public String getDescription() {
return _description;
}
public String getRange() {
return _range;
}
public String key() {
return _name;
}
public static boolean doesKeyExist(String key){
return _zoneConfigKeys.contains(key);
}
}

View File

@ -260,7 +260,10 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx
@Override
public ConsoleProxyInfo assignProxy(final long dataCenterId, final long vmId) {
if (!isConsoleProxyVmRequired(dataCenterId)) {
return null;
}
final Pair<ConsoleProxyManagerImpl, ConsoleProxyVO> result = new Pair<ConsoleProxyManagerImpl, ConsoleProxyVO>(this, null);
_requestHandlerScheduler.execute(new Runnable() {

View File

@ -16,8 +16,8 @@
*
*/
package com.cloud.dc;
package com.cloud.dc;
import java.util.Map;
import javax.persistence.Column;
@ -34,38 +34,38 @@ import javax.persistence.Transient;
import com.cloud.network.Network.Provider;
import com.cloud.org.Grouping;
import com.cloud.utils.NumbersUtil;
@Entity
@Table(name="data_center")
public class DataCenterVO implements DataCenter {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private long id;
@Column(name="name")
private String name = null;
@Column(name="description")
private String description = null;
@Column(name="dns1")
private String dns1 = null;
@Column(name="dns2")
private String dns2 = null;
@Column(name="internal_dns1")
private String internalDns1 = null;
@Column(name="internal_dns2")
private String internalDns2 = null;
@Column(name="router_mac_address", updatable = false, nullable=false)
private String routerMacAddress = "02:00:00:00:00:01";
@Column(name="vnet")
@Entity
@Table(name="data_center")
public class DataCenterVO implements DataCenter {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private long id;
@Column(name="name")
private String name = null;
@Column(name="description")
private String description = null;
@Column(name="dns1")
private String dns1 = null;
@Column(name="dns2")
private String dns2 = null;
@Column(name="internal_dns1")
private String internalDns1 = null;
@Column(name="internal_dns2")
private String internalDns2 = null;
@Column(name="router_mac_address", updatable = false, nullable=false)
private String routerMacAddress = "02:00:00:00:00:01";
@Column(name="vnet")
private String vnet = null;
@Column(name="guest_network_cidr")
@ -168,12 +168,12 @@ public class DataCenterVO implements DataCenter {
}
public DataCenterVO(long id, String name, String description, String dns1, String dns2, String dns3, String dns4, String vnet, String guestCidr, String domain, Long domainId, NetworkType zoneType, String zoneToken) {
this(name, description, dns1, dns2, dns3, dns4, vnet, guestCidr, domain, domainId, zoneType, false, zoneToken);
this(name, description, dns1, dns2, dns3, dns4, vnet, guestCidr, domain, domainId, zoneType, false, zoneToken);
this.id = id;
this.allocationState = Grouping.AllocationState.Enabled;
}
public DataCenterVO(String name, String description, String dns1, String dns2, String dns3, String dns4, String vnet, String guestCidr, String domain, Long domainId, NetworkType zoneType, boolean securityGroupEnabled, String zoneToken) {
this.allocationState = Grouping.AllocationState.Enabled;
}
public DataCenterVO(String name, String description, String dns1, String dns2, String dns3, String dns4, String vnet, String guestCidr, String domain, Long domainId, NetworkType zoneType, boolean securityGroupEnabled, String zoneToken) {
this.name = name;
this.description = description;
this.dns1 = dns1;
@ -231,81 +231,81 @@ public class DataCenterVO implements DataCenter {
public void setDomainId(Long domainId) {
this.domainId = domainId;
}
}
@Override
public String getDescription() {
return description;
}
public String getRouterMacAddress() {
return routerMacAddress;
}
public void setVnet(String vnet) {
this.vnet = vnet;
}
public String getDescription() {
return description;
}
public String getRouterMacAddress() {
return routerMacAddress;
}
public void setVnet(String vnet) {
this.vnet = vnet;
}
@Override
public String getDns1() {
return dns1;
}
public String getDns1() {
return dns1;
}
@Override
public String getVnet() {
return vnet;
}
public String getVnet() {
return vnet;
}
@Override
public String getDns2() {
return dns2;
}
public String getDns2() {
return dns2;
}
@Override
public String getInternalDns1() {
return internalDns1;
}
public String getInternalDns1() {
return internalDns1;
}
@Override
public String getInternalDns2() {
return internalDns2;
}
protected DataCenterVO() {
}
public String getInternalDns2() {
return internalDns2;
}
protected DataCenterVO() {
}
@Override
public long getId() {
return id;
}
public long getId() {
return id;
}
@Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setDns1(String dns1) {
this.dns1 = dns1;
}
public void setDns2(String dns2) {
this.dns2 = dns2;
}
public void setInternalDns1(String dns3) {
this.internalDns1 = dns3;
}
public void setInternalDns2(String dns4) {
this.internalDns2 = dns4;
}
public void setRouterMacAddress(String routerMacAddress) {
this.routerMacAddress = routerMacAddress;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setDns1(String dns1) {
this.dns1 = dns1;
}
public void setDns2(String dns2) {
this.dns2 = dns2;
}
public void setInternalDns1(String dns3) {
this.internalDns1 = dns3;
}
public void setInternalDns2(String dns4) {
this.internalDns2 = dns4;
}
public void setRouterMacAddress(String routerMacAddress) {
this.routerMacAddress = routerMacAddress;
}
@Override
@ -397,5 +397,5 @@ public class DataCenterVO implements DataCenter {
public void setZoneToken(String zoneToken) {
this.zoneToken = zoneToken;
}
}
}
}

View File

@ -25,7 +25,7 @@ import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="datacenter_details")
@Table(name="data_center_details")
public class DcDetailVO {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)

View File

@ -16,47 +16,47 @@
*
*/
package com.cloud.dc.dao;
package com.cloud.dc.dao;
import java.util.List;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.DataCenterVnetVO;
import com.cloud.utils.Pair;
import com.cloud.utils.db.GenericDao;
public interface DataCenterDao extends GenericDao<DataCenterVO, Long> {
DataCenterVO findByName(String name);
/**
* @param id data center id
* @return a pair of mac address strings. The first one is private and second is public.
*/
String[] getNextAvailableMacAddressPair(long id);
String[] getNextAvailableMacAddressPair(long id, long mask);
public interface DataCenterDao extends GenericDao<DataCenterVO, Long> {
DataCenterVO findByName(String name);
/**
* @param id data center id
* @return a pair of mac address strings. The first one is private and second is public.
*/
String[] getNextAvailableMacAddressPair(long id);
String[] getNextAvailableMacAddressPair(long id, long mask);
Pair<String, Long> allocatePrivateIpAddress(long id, long podId, long instanceId, String reservationId);
String allocateLinkLocalIpAddress(long id, long podId, long instanceId, String reservationId);
String allocateVnet(long dcId, long accountId, String reservationId);
void releaseVnet(String vnet, long dcId, long accountId, String reservationId);
String allocateLinkLocalIpAddress(long id, long podId, long instanceId, String reservationId);
String allocateVnet(long dcId, long accountId, String reservationId);
void releaseVnet(String vnet, long dcId, long accountId, String reservationId);
void releasePrivateIpAddress(String ipAddress, long dcId, Long instanceId);
void releasePrivateIpAddress(long nicId, String reservationId);
void releaseLinkLocalIpAddress(String ipAddress, long dcId, Long instanceId);
void releaseLinkLocalIpAddress(long nicId, String reservationId);
boolean deletePrivateIpAddressByPod(long podId);
boolean deleteLinkLocalIpAddressByPod(long podId);
void addPrivateIpAddress(long dcId,long podId, String start, String end);
boolean deleteLinkLocalIpAddressByPod(long podId);
void addPrivateIpAddress(long dcId,long podId, String start, String end);
void addLinkLocalIpAddress(long dcId,long podId, String start, String end);
List<DataCenterVnetVO> findVnet(long dcId, String vnet);
void addVnet(long dcId, int start, int end);
void deleteVnet(long dcId);
List<DataCenterVnetVO> listAllocatedVnets(long dcId);
void addVnet(long dcId, int start, int end);
void deleteVnet(long dcId);
List<DataCenterVnetVO> listAllocatedVnets(long dcId);
String allocatePodVlan(long podId, long accountId);
@ -74,4 +74,5 @@ public interface DataCenterDao extends GenericDao<DataCenterVO, Long> {
List<DataCenterVO> listDisabledZones();
List<DataCenterVO> listEnabledZones();
DataCenterVO findByToken(String zoneToken);
}
DataCenterVO findByTokenOrIdOrName(String tokenIdOrName);
}

View File

@ -16,8 +16,8 @@
*
*/
package com.cloud.dc.dao;
package com.cloud.dc.dao;
import java.util.List;
import java.util.Map;
import java.util.Random;
@ -44,42 +44,42 @@ import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.SequenceFetcher;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.net.NetUtils;
/**
* @config
* {@table
* || Param Name | Description | Values | Default ||
* || mac.address.prefix | prefix to attach to all public and private mac addresses | number | 06 ||
* }
**/
@Local(value={DataCenterDao.class})
public class DataCenterDaoImpl extends GenericDaoBase<DataCenterVO, Long> implements DataCenterDao {
private static final Logger s_logger = Logger.getLogger(DataCenterDaoImpl.class);
/**
* @config
* {@table
* || Param Name | Description | Values | Default ||
* || mac.address.prefix | prefix to attach to all public and private mac addresses | number | 06 ||
* }
**/
@Local(value={DataCenterDao.class})
public class DataCenterDaoImpl extends GenericDaoBase<DataCenterVO, Long> implements DataCenterDao {
private static final Logger s_logger = Logger.getLogger(DataCenterDaoImpl.class);
protected SearchBuilder<DataCenterVO> NameSearch;
protected SearchBuilder<DataCenterVO> ListZonesByDomainIdSearch;
protected SearchBuilder<DataCenterVO> PublicZonesSearch;
protected SearchBuilder<DataCenterVO> ChildZonesSearch;
protected SearchBuilder<DataCenterVO> securityGroupSearch;
protected SearchBuilder<DataCenterVO> DisabledZonesSearch;
protected SearchBuilder<DataCenterVO> DisabledZonesSearch;
protected SearchBuilder<DataCenterVO> TokenSearch;
protected final DataCenterIpAddressDaoImpl _ipAllocDao = ComponentLocator.inject(DataCenterIpAddressDaoImpl.class);
protected final DataCenterLinkLocalIpAddressDaoImpl _LinkLocalIpAllocDao = ComponentLocator.inject(DataCenterLinkLocalIpAddressDaoImpl.class);
protected final DataCenterLinkLocalIpAddressDaoImpl _LinkLocalIpAllocDao = ComponentLocator.inject(DataCenterLinkLocalIpAddressDaoImpl.class);
protected final DataCenterVnetDaoImpl _vnetAllocDao = ComponentLocator.inject(DataCenterVnetDaoImpl.class);
protected final PodVlanDaoImpl _podVlanAllocDao = ComponentLocator.inject(PodVlanDaoImpl.class);
protected final PodVlanDaoImpl _podVlanAllocDao = ComponentLocator.inject(PodVlanDaoImpl.class);
protected long _prefix;
protected Random _rand = new Random(System.currentTimeMillis());
protected TableGenerator _tgMacAddress;
protected final DcDetailsDaoImpl _detailsDao = ComponentLocator.inject(DcDetailsDaoImpl.class);
@Override
public DataCenterVO findByName(String name) {
SearchCriteria<DataCenterVO> sc = NameSearch.create();
sc.setParameters("name", name);
return findOneBy(sc);
@Override
public DataCenterVO findByName(String name) {
SearchCriteria<DataCenterVO> sc = NameSearch.create();
sc.setParameters("name", name);
return findOneBy(sc);
}
@Override
@ -115,21 +115,21 @@ public class DataCenterDaoImpl extends GenericDaoBase<DataCenterVO, Long> implem
SearchCriteria<DataCenterVO> sc = securityGroupSearch.create();
sc.setParameters("isSgEnabled", true);
return listBy(sc);
}
@Override
public void releaseVnet(String vnet, long dcId, long accountId, String reservationId) {
_vnetAllocDao.release(vnet, dcId, accountId, reservationId);
}
@Override
public void releaseVnet(String vnet, long dcId, long accountId, String reservationId) {
_vnetAllocDao.release(vnet, dcId, accountId, reservationId);
}
@Override
public List<DataCenterVnetVO> findVnet(long dcId, String vnet) {
return _vnetAllocDao.findVnet(dcId, vnet);
}
@Override
public void releasePrivateIpAddress(String ipAddress, long dcId, Long instanceId) {
_ipAllocDao.releaseIpAddress(ipAddress, dcId, instanceId);
}
@Override
public void releasePrivateIpAddress(String ipAddress, long dcId, Long instanceId) {
_ipAllocDao.releaseIpAddress(ipAddress, dcId, instanceId);
}
@Override
@ -145,26 +145,26 @@ public class DataCenterDaoImpl extends GenericDaoBase<DataCenterVO, Long> implem
@Override
public void releaseLinkLocalIpAddress(String ipAddress, long dcId, Long instanceId) {
_LinkLocalIpAllocDao.releaseIpAddress(ipAddress, dcId, instanceId);
}
@Override
public boolean deletePrivateIpAddressByPod(long podId) {
return _ipAllocDao.deleteIpAddressByPod(podId);
}
@Override
public boolean deletePrivateIpAddressByPod(long podId) {
return _ipAllocDao.deleteIpAddressByPod(podId);
}
@Override
public boolean deleteLinkLocalIpAddressByPod(long podId) {
return _LinkLocalIpAllocDao.deleteIpAddressByPod(podId);
}
@Override
public String allocateVnet(long dataCenterId, long accountId, String reservationId) {
DataCenterVnetVO vo = _vnetAllocDao.take(dataCenterId, accountId, reservationId);
if (vo == null) {
return null;
}
return vo.getVnet();
}
@Override
public String allocateVnet(long dataCenterId, long accountId, String reservationId) {
DataCenterVnetVO vo = _vnetAllocDao.take(dataCenterId, accountId, reservationId);
if (vo == null) {
return null;
}
return vo.getVnet();
}
@Override
@ -174,34 +174,34 @@ public class DataCenterDaoImpl extends GenericDaoBase<DataCenterVO, Long> implem
return null;
}
return vo.getVlan();
}
@Override
public String[] getNextAvailableMacAddressPair(long id) {
return getNextAvailableMacAddressPair(id, 0);
}
@Override
}
@Override
public String[] getNextAvailableMacAddressPair(long id) {
return getNextAvailableMacAddressPair(id, 0);
}
@Override
public String[] getNextAvailableMacAddressPair(long id, long mask) {
SequenceFetcher fetch = SequenceFetcher.getInstance();
long seq = fetch.getNextSequence(Long.class, _tgMacAddress, id);
seq = seq | _prefix | ((id & 0x7f) << 32);
long seq = fetch.getNextSequence(Long.class, _tgMacAddress, id);
seq = seq | _prefix | ((id & 0x7f) << 32);
seq |= mask;
seq |= ((_rand.nextInt(Short.MAX_VALUE) << 16) & 0x00000000ffff0000l);
String[] pair = new String[2];
pair[0] = NetUtils.long2Mac(seq);
pair[1] = NetUtils.long2Mac(seq | 0x1l << 39);
return pair;
}
@Override
public Pair<String, Long> allocatePrivateIpAddress(long dcId, long podId, long instanceId, String reservationId) {
DataCenterIpAddressVO vo = _ipAllocDao.takeIpAddress(dcId, podId, instanceId, reservationId);
if (vo == null) {
return null;
}
return new Pair<String, Long>(vo.getIpAddress(), vo.getMacAddress());
String[] pair = new String[2];
pair[0] = NetUtils.long2Mac(seq);
pair[1] = NetUtils.long2Mac(seq | 0x1l << 39);
return pair;
}
@Override
public Pair<String, Long> allocatePrivateIpAddress(long dcId, long podId, long instanceId, String reservationId) {
DataCenterIpAddressVO vo = _ipAllocDao.takeIpAddress(dcId, podId, instanceId, reservationId);
if (vo == null) {
return null;
}
return new Pair<String, Long>(vo.getIpAddress(), vo.getMacAddress());
}
@Override
@ -211,56 +211,56 @@ public class DataCenterDaoImpl extends GenericDaoBase<DataCenterVO, Long> implem
return null;
}
return vo.getIpAddress();
}
@Override
public void addVnet(long dcId, int start, int end) {
_vnetAllocDao.add(dcId, start, end);
}
@Override
public void deleteVnet(long dcId) {
_vnetAllocDao.delete(dcId);
}
@Override
public List<DataCenterVnetVO> listAllocatedVnets(long dcId) {
return _vnetAllocDao.listAllocatedVnets(dcId);
}
@Override
public void addPrivateIpAddress(long dcId,long podId, String start, String end) {
_ipAllocDao.addIpRange(dcId, podId, start, end);
}
@Override
public void addVnet(long dcId, int start, int end) {
_vnetAllocDao.add(dcId, start, end);
}
@Override
public void deleteVnet(long dcId) {
_vnetAllocDao.delete(dcId);
}
@Override
public List<DataCenterVnetVO> listAllocatedVnets(long dcId) {
return _vnetAllocDao.listAllocatedVnets(dcId);
}
@Override
public void addPrivateIpAddress(long dcId,long podId, String start, String end) {
_ipAllocDao.addIpRange(dcId, podId, start, end);
}
@Override
public void addLinkLocalIpAddress(long dcId,long podId, String start, String end) {
_LinkLocalIpAllocDao.addIpRange(dcId, podId, start, end);
}
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
if (!super.configure(name, params)) {
return false;
}
String value = (String)params.get("mac.address.prefix");
_prefix = (long)NumbersUtil.parseInt(value, 06) << 40;
if (!_ipAllocDao.configure("Ip Alloc", params)) {
return false;
}
if (!_vnetAllocDao.configure("vnet Alloc", params)) {
return false;
}
return true;
}
}
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
if (!super.configure(name, params)) {
return false;
}
String value = (String)params.get("mac.address.prefix");
_prefix = (long)NumbersUtil.parseInt(value, 06) << 40;
if (!_ipAllocDao.configure("Ip Alloc", params)) {
return false;
}
if (!_vnetAllocDao.configure("vnet Alloc", params)) {
return false;
}
return true;
}
protected DataCenterDaoImpl() {
super();
super();
NameSearch = createSearchBuilder();
NameSearch.and("name", NameSearch.entity().getName(), SearchCriteria.Op.EQ);
NameSearch.and("name", NameSearch.entity().getName(), SearchCriteria.Op.EQ);
NameSearch.done();
ListZonesByDomainIdSearch = createSearchBuilder();
@ -288,7 +288,7 @@ public class DataCenterDaoImpl extends GenericDaoBase<DataCenterVO, Long> implem
TokenSearch.done();
_tgMacAddress = _tgs.get("macAddress");
assert _tgMacAddress != null : "Couldn't get mac address table generator";
assert _tgMacAddress != null : "Couldn't get mac address table generator";
}
@Override @DB
@ -338,4 +338,21 @@ public class DataCenterDaoImpl extends GenericDaoBase<DataCenterVO, Long> implem
return dcs;
}
}
@Override
public DataCenterVO findByTokenOrIdOrName(String tokenOrIdOrName) {
DataCenterVO result = findByToken(tokenOrIdOrName);
if (result == null) {
result = findByName(tokenOrIdOrName);
if (result == null) {
try {
Long dcId = Long.parseLong(tokenOrIdOrName);
return findById(dcId);
} catch (NumberFormatException nfe) {
}
}
}
return result;
}
}

View File

@ -14,11 +14,12 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.host.dao;
*/
package com.cloud.host.dao;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.cloud.host.Host;
import com.cloud.host.Host.Type;
@ -28,119 +29,115 @@ import com.cloud.host.Status.Event;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.info.RunningHostCountInfo;
import com.cloud.utils.db.GenericDao;
/**
* Data Access Object for server
*
*/
public interface HostDao extends GenericDao<HostVO, Long> {
/**
* Data Access Object for server
*
*/
public interface HostDao extends GenericDao<HostVO, Long> {
List<HostVO> listBy(Host.Type type, Long clusterId, Long podId, long dcId);
long countBy(long clusterId, Status... statuses);
List<HostVO> listByDataCenter(long dcId);
List<HostVO> listByHostPod(long podId);
List<HostVO> listByStatus(Status... status);
List<HostVO> listByDataCenter(long dcId);
List<HostVO> listByHostPod(long podId);
List<HostVO> listByStatus(Status... status);
List<HostVO> listBy(Host.Type type, long dcId);
List<HostVO> listAllBy(Host.Type type, long dcId);
HostVO findSecondaryStorageHost(long dcId);
List<HostVO> listByCluster(long clusterId);
/**
* Lists all secondary storage hosts, across all zones
* @return list of Hosts
*/
List<HostVO> listSecondaryStorageHosts();
/**
* Mark all hosts in Up or Orphaned state as disconnected. This method
* is used at AgentManager startup to reset all of the connections.
*
* @param msId management server id.
* @param statuses states of the host.
*/
void markHostsAsDisconnected(long msId, Status... states);
List<HostVO> findLostHosts(long timeout);
List<HostVO> findHostsLike(String hostName);
/**
* Find hosts that are directly connected.
*/
List<HostVO> findDirectlyConnectedHosts();
List<HostVO> findDirectAgentToLoad(long msid, long lastPingSecondsAfter, Long limit);
/**
* Mark the host as disconnected if it is in one of these states.
* The management server id is set to null.
* The lastPinged timestamp is set to current.
* The state is set to the state passed in.
* The disconnectedOn timestamp is set to current.
*
* @param host host to be marked
* @param state state to be set to.
* @param ifStates only if it is one of these states.
* @return true if it's updated; false if not.
*/
boolean disconnect(HostVO host, Event event, long msId);
boolean connect(HostVO host, long msId);
HostVO findByStorageIpAddressInDataCenter(long dcId, String privateIpAddress);
HostVO findByPrivateIpAddressInDataCenter(long dcId, String privateIpAddress);
/**
* find a host by its mac address
* @param macAddress
* @return HostVO or null if not found.
*/
List<HostVO> listAllBy(Host.Type type, long dcId);
HostVO findSecondaryStorageHost(long dcId);
List<HostVO> listByCluster(long clusterId);
/**
* Lists all secondary storage hosts, across all zones
* @return list of Hosts
*/
List<HostVO> listSecondaryStorageHosts();
/**
* Mark all hosts in Up or Orphaned state as disconnected. This method
* is used at AgentManager startup to reset all of the connections.
*
* @param msId management server id.
* @param statuses states of the host.
*/
void markHostsAsDisconnected(long msId, Status... states);
List<HostVO> findLostHosts(long timeout);
List<HostVO> findHostsLike(String hostName);
/**
* Find hosts that are directly connected.
*/
List<HostVO> findDirectlyConnectedHosts();
List<HostVO> findDirectAgentToLoad(long msid, long lastPingSecondsAfter, Long limit);
/**
* Mark the host as disconnected if it is in one of these states.
* The management server id is set to null.
* The lastPinged timestamp is set to current.
* The state is set to the state passed in.
* The disconnectedOn timestamp is set to current.
*
* @param host host to be marked
* @param state state to be set to.
* @param ifStates only if it is one of these states.
* @return true if it's updated; false if not.
*/
boolean disconnect(HostVO host, Event event, long msId);
boolean connect(HostVO host, long msId);
HostVO findByStorageIpAddressInDataCenter(long dcId, String privateIpAddress);
HostVO findByPrivateIpAddressInDataCenter(long dcId, String privateIpAddress);
/**
* find a host by its mac address
* @param macAddress
* @return HostVO or null if not found.
*/
public HostVO findByGuid(String macAddress);
public HostVO findByName(String name);
/**
* find all hosts of a certain type in a data center
* @param type
* @param routingCapable
* @param dcId
* @return
*/
List<HostVO> listByTypeDataCenter(Host.Type type, long dcId);
/**
* find all hosts of a particular type
* @param type
* @return
*/
List<HostVO> listByType(Type type);
/**
* Find hosts that have not responded to a ping regardless of state
* @param timeout
* @param type
* @return
*/
List<HostVO> findLostHosts2(long timeout, Type type);
/**
* update the host and changes the status depending on the Event and
* the current status. If the status changed between
* @param host host object to change
* @param event event that happened.
* @param management server who's making this update
* @return true if updated; false if not.
*/
boolean updateStatus(HostVO host, Event event, long msId);
List<RunningHostCountInfo> getRunningHostCounts(Date cutTime);
long getNextSequence(long hostId);
void loadDetails(HostVO host);
/**
* find all hosts of a certain type in a data center
* @param type
* @param routingCapable
* @param dcId
* @return
*/
List<HostVO> listByTypeDataCenter(Host.Type type, long dcId);
/**
* find all hosts of a particular type
* @param type
* @return
*/
List<HostVO> listByType(Type type);
/**
* Find hosts that have not responded to a ping regardless of state
* @param timeout
* @param type
* @return
*/
List<HostVO> findLostHosts2(long timeout, Type type);
/**
* update the host and changes the status depending on the Event and
* the current status. If the status changed between
* @param host host object to change
* @param event event that happened.
* @param management server who's making this update
* @return true if updated; false if not.
*/
boolean updateStatus(HostVO host, Event event, long msId);
List<RunningHostCountInfo> getRunningHostCounts(Date cutTime);
void saveDetails(HostVO host);
long getNextSequence(long hostId);
void loadDetails(HostVO host);
HostVO findConsoleProxyHost(String name, Type type);
@ -162,6 +159,9 @@ public interface HostDao extends GenericDao<HostVO, Long> {
void loadHostTags(HostVO host);
List<HostVO> listByHostTag(Host.Type type, Long clusterId, Long podId, long dcId, String hostTag);
long countRoutingHostsByDataCenter(long dcId);
List<HostVO> listSecondaryStorageHosts(long dataCenterId);
long countRoutingHostsByDataCenter(long dcId);
}
}

View File

@ -14,14 +14,15 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.host.dao;
*/
package com.cloud.host.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
@ -54,153 +55,148 @@ import com.cloud.utils.db.SearchCriteria.Op;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.db.UpdateBuilder;
import com.cloud.utils.exception.CloudRuntimeException;
@Local(value = { HostDao.class }) @DB(txn=false)
@TableGenerator(name="host_req_sq", table="op_host", pkColumnName="id", valueColumnName="sequence", allocationSize=1)
public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao {
public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao {
private static final Logger s_logger = Logger.getLogger(HostDaoImpl.class);
protected final SearchBuilder<HostVO> TypePodDcStatusSearch;
protected final SearchBuilder<HostVO> IdStatusSearch;
protected final SearchBuilder<HostVO> TypeDcSearch;
protected final SearchBuilder<HostVO> TypeDcStatusSearch;
protected final SearchBuilder<HostVO> LastPingedSearch;
protected final SearchBuilder<HostVO> LastPingedSearch2;
protected final SearchBuilder<HostVO> MsStatusSearch;
protected final SearchBuilder<HostVO> DcPrivateIpAddressSearch;
protected final SearchBuilder<HostVO> DcStorageIpAddressSearch;
protected final SearchBuilder<HostVO> GuidSearch;
protected final SearchBuilder<HostVO> DcSearch;
protected final SearchBuilder<HostVO> PodSearch;
protected final SearchBuilder<HostVO> TypeSearch;
protected final SearchBuilder<HostVO> StatusSearch;
protected final SearchBuilder<HostVO> TypePodDcStatusSearch;
protected final SearchBuilder<HostVO> IdStatusSearch;
protected final SearchBuilder<HostVO> TypeDcSearch;
protected final SearchBuilder<HostVO> TypeDcStatusSearch;
protected final SearchBuilder<HostVO> LastPingedSearch;
protected final SearchBuilder<HostVO> LastPingedSearch2;
protected final SearchBuilder<HostVO> MsStatusSearch;
protected final SearchBuilder<HostVO> DcPrivateIpAddressSearch;
protected final SearchBuilder<HostVO> DcStorageIpAddressSearch;
protected final SearchBuilder<HostVO> GuidSearch;
protected final SearchBuilder<HostVO> DcSearch;
protected final SearchBuilder<HostVO> PodSearch;
protected final SearchBuilder<HostVO> TypeSearch;
protected final SearchBuilder<HostVO> StatusSearch;
protected final SearchBuilder<HostVO> NameLikeSearch;
protected final SearchBuilder<HostVO> NameSearch;
protected final SearchBuilder<HostVO> SequenceSearch;
protected final SearchBuilder<HostVO> SequenceSearch;
protected final SearchBuilder<HostVO> DirectlyConnectedSearch;
protected final SearchBuilder<HostVO> UnmanagedDirectConnectSearch;
protected final SearchBuilder<HostVO> UnmanagedExternalNetworkApplianceSearch;
protected final SearchBuilder<HostVO> MaintenanceCountSearch;
protected final SearchBuilder<HostVO> MaintenanceCountSearch;
protected final SearchBuilder<HostVO> ClusterSearch;
protected final SearchBuilder<HostVO> ConsoleProxyHostSearch;
protected final SearchBuilder<HostVO> AvailHypevisorInZone;
protected final GenericSearchBuilder<HostVO, Long> HostsInStatusSearch;
protected final GenericSearchBuilder<HostVO, Long> CountRoutingByDc;
protected final Attribute _statusAttr;
protected final Attribute _msIdAttr;
protected final Attribute _pingTimeAttr;
protected final DetailsDaoImpl _detailsDao = ComponentLocator.inject(DetailsDaoImpl.class);
protected final HostTagsDaoImpl _hostTagsDao = ComponentLocator.inject(HostTagsDaoImpl.class);
protected final Attribute _statusAttr;
protected final Attribute _msIdAttr;
protected final Attribute _pingTimeAttr;
protected final DetailsDaoImpl _detailsDao = ComponentLocator.inject(DetailsDaoImpl.class);
protected final HostTagsDaoImpl _hostTagsDao = ComponentLocator.inject(HostTagsDaoImpl.class);
public HostDaoImpl() {
public HostDaoImpl() {
MaintenanceCountSearch = createSearchBuilder();
MaintenanceCountSearch.and("cluster", MaintenanceCountSearch.entity().getClusterId(), SearchCriteria.Op.EQ);
MaintenanceCountSearch.and("status", MaintenanceCountSearch.entity().getStatus(), SearchCriteria.Op.IN);
MaintenanceCountSearch.done();
TypePodDcStatusSearch = createSearchBuilder();
HostVO entity = TypePodDcStatusSearch.entity();
TypePodDcStatusSearch.and("type", entity.getType(), SearchCriteria.Op.EQ);
TypePodDcStatusSearch.and("pod", entity.getPodId(), SearchCriteria.Op.EQ);
TypePodDcStatusSearch = createSearchBuilder();
HostVO entity = TypePodDcStatusSearch.entity();
TypePodDcStatusSearch.and("type", entity.getType(), SearchCriteria.Op.EQ);
TypePodDcStatusSearch.and("pod", entity.getPodId(), SearchCriteria.Op.EQ);
TypePodDcStatusSearch.and("dc", entity.getDataCenterId(), SearchCriteria.Op.EQ);
TypePodDcStatusSearch.and("cluster", entity.getClusterId(), SearchCriteria.Op.EQ);
TypePodDcStatusSearch.and("status", entity.getStatus(), SearchCriteria.Op.EQ);
TypePodDcStatusSearch.done();
LastPingedSearch = createSearchBuilder();
LastPingedSearch.and("ping", LastPingedSearch.entity().getLastPinged(), SearchCriteria.Op.LT);
LastPingedSearch.and("state", LastPingedSearch.entity().getStatus(), SearchCriteria.Op.IN);
LastPingedSearch.done();
LastPingedSearch2 = createSearchBuilder();
LastPingedSearch2.and("ping", LastPingedSearch2.entity().getLastPinged(), SearchCriteria.Op.LT);
LastPingedSearch2.and("type", LastPingedSearch2.entity().getType(), SearchCriteria.Op.EQ);
LastPingedSearch2.done();
MsStatusSearch = createSearchBuilder();
MsStatusSearch.and("ms", MsStatusSearch.entity().getManagementServerId(), SearchCriteria.Op.EQ);
MsStatusSearch.and("statuses", MsStatusSearch.entity().getStatus(), SearchCriteria.Op.IN);
MsStatusSearch.done();
TypeDcSearch = createSearchBuilder();
TypeDcSearch.and("type", TypeDcSearch.entity().getType(), SearchCriteria.Op.EQ);
TypeDcSearch.and("dc", TypeDcSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
TypeDcSearch.done();
TypeDcStatusSearch = createSearchBuilder();
TypeDcStatusSearch.and("type", TypeDcStatusSearch.entity().getType(), SearchCriteria.Op.EQ);
TypeDcStatusSearch.and("dc", TypeDcStatusSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
TypeDcStatusSearch.and("status", TypeDcStatusSearch.entity().getStatus(), SearchCriteria.Op.EQ);
TypeDcStatusSearch.done();
IdStatusSearch = createSearchBuilder();
IdStatusSearch.and("id", IdStatusSearch.entity().getId(), SearchCriteria.Op.EQ);
IdStatusSearch.and("states", IdStatusSearch.entity().getStatus(), SearchCriteria.Op.IN);
IdStatusSearch.done();
DcPrivateIpAddressSearch = createSearchBuilder();
DcPrivateIpAddressSearch.and("privateIpAddress", DcPrivateIpAddressSearch.entity().getPrivateIpAddress(), SearchCriteria.Op.EQ);
DcPrivateIpAddressSearch.and("dc", DcPrivateIpAddressSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
DcPrivateIpAddressSearch.done();
DcStorageIpAddressSearch = createSearchBuilder();
DcStorageIpAddressSearch.and("storageIpAddress", DcStorageIpAddressSearch.entity().getStorageIpAddress(), SearchCriteria.Op.EQ);
DcStorageIpAddressSearch.and("dc", DcStorageIpAddressSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
DcStorageIpAddressSearch.done();
GuidSearch = createSearchBuilder();
GuidSearch.and("guid", GuidSearch.entity().getGuid(), SearchCriteria.Op.EQ);
GuidSearch.done();
DcSearch = createSearchBuilder();
DcSearch.and("dc", DcSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
TypePodDcStatusSearch.and("cluster", entity.getClusterId(), SearchCriteria.Op.EQ);
TypePodDcStatusSearch.and("status", entity.getStatus(), SearchCriteria.Op.EQ);
TypePodDcStatusSearch.done();
LastPingedSearch = createSearchBuilder();
LastPingedSearch.and("ping", LastPingedSearch.entity().getLastPinged(), SearchCriteria.Op.LT);
LastPingedSearch.and("state", LastPingedSearch.entity().getStatus(), SearchCriteria.Op.IN);
LastPingedSearch.done();
LastPingedSearch2 = createSearchBuilder();
LastPingedSearch2.and("ping", LastPingedSearch2.entity().getLastPinged(), SearchCriteria.Op.LT);
LastPingedSearch2.and("type", LastPingedSearch2.entity().getType(), SearchCriteria.Op.EQ);
LastPingedSearch2.done();
MsStatusSearch = createSearchBuilder();
MsStatusSearch.and("ms", MsStatusSearch.entity().getManagementServerId(), SearchCriteria.Op.EQ);
MsStatusSearch.and("statuses", MsStatusSearch.entity().getStatus(), SearchCriteria.Op.IN);
MsStatusSearch.done();
TypeDcSearch = createSearchBuilder();
TypeDcSearch.and("type", TypeDcSearch.entity().getType(), SearchCriteria.Op.EQ);
TypeDcSearch.and("dc", TypeDcSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
TypeDcSearch.done();
TypeDcStatusSearch = createSearchBuilder();
TypeDcStatusSearch.and("type", TypeDcStatusSearch.entity().getType(), SearchCriteria.Op.EQ);
TypeDcStatusSearch.and("dc", TypeDcStatusSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
TypeDcStatusSearch.and("status", TypeDcStatusSearch.entity().getStatus(), SearchCriteria.Op.EQ);
TypeDcStatusSearch.done();
IdStatusSearch = createSearchBuilder();
IdStatusSearch.and("id", IdStatusSearch.entity().getId(), SearchCriteria.Op.EQ);
IdStatusSearch.and("states", IdStatusSearch.entity().getStatus(), SearchCriteria.Op.IN);
IdStatusSearch.done();
DcPrivateIpAddressSearch = createSearchBuilder();
DcPrivateIpAddressSearch.and("privateIpAddress", DcPrivateIpAddressSearch.entity().getPrivateIpAddress(), SearchCriteria.Op.EQ);
DcPrivateIpAddressSearch.and("dc", DcPrivateIpAddressSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
DcPrivateIpAddressSearch.done();
DcStorageIpAddressSearch = createSearchBuilder();
DcStorageIpAddressSearch.and("storageIpAddress", DcStorageIpAddressSearch.entity().getStorageIpAddress(), SearchCriteria.Op.EQ);
DcStorageIpAddressSearch.and("dc", DcStorageIpAddressSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
DcStorageIpAddressSearch.done();
GuidSearch = createSearchBuilder();
GuidSearch.and("guid", GuidSearch.entity().getGuid(), SearchCriteria.Op.EQ);
GuidSearch.done();
DcSearch = createSearchBuilder();
DcSearch.and("dc", DcSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
DcSearch.done();
ClusterSearch = createSearchBuilder();
ClusterSearch.and("cluster", ClusterSearch.entity().getClusterId(), SearchCriteria.Op.EQ);
ClusterSearch.done();
ClusterSearch.done();
ConsoleProxyHostSearch = createSearchBuilder();
ConsoleProxyHostSearch.and("name", ConsoleProxyHostSearch.entity().getName(), SearchCriteria.Op.EQ);
ConsoleProxyHostSearch.and("type", ConsoleProxyHostSearch.entity().getType(), SearchCriteria.Op.EQ);
ConsoleProxyHostSearch.done();
PodSearch = createSearchBuilder();
PodSearch.and("pod", PodSearch.entity().getPodId(), SearchCriteria.Op.EQ);
PodSearch.done();
TypeSearch = createSearchBuilder();
TypeSearch.and("type", TypeSearch.entity().getType(), SearchCriteria.Op.EQ);
TypeSearch.done();
StatusSearch =createSearchBuilder();
StatusSearch.and("status", StatusSearch.entity().getStatus(), SearchCriteria.Op.IN);
StatusSearch.done();
NameLikeSearch = createSearchBuilder();
NameLikeSearch.and("name", NameLikeSearch.entity().getName(), SearchCriteria.Op.LIKE);
PodSearch = createSearchBuilder();
PodSearch.and("pod", PodSearch.entity().getPodId(), SearchCriteria.Op.EQ);
PodSearch.done();
TypeSearch = createSearchBuilder();
TypeSearch.and("type", TypeSearch.entity().getType(), SearchCriteria.Op.EQ);
TypeSearch.done();
StatusSearch =createSearchBuilder();
StatusSearch.and("status", StatusSearch.entity().getStatus(), SearchCriteria.Op.IN);
StatusSearch.done();
NameLikeSearch = createSearchBuilder();
NameLikeSearch.and("name", NameLikeSearch.entity().getName(), SearchCriteria.Op.LIKE);
NameLikeSearch.done();
NameSearch = createSearchBuilder();
NameSearch.and("name", NameSearch.entity().getName(), SearchCriteria.Op.EQ);
NameSearch.done();
SequenceSearch = createSearchBuilder();
SequenceSearch.and("id", SequenceSearch.entity().getId(), SearchCriteria.Op.EQ);
// SequenceSearch.addRetrieve("sequence", SequenceSearch.entity().getSequence());
SequenceSearch.done();
DirectlyConnectedSearch = createSearchBuilder();
DirectlyConnectedSearch.and("resource", DirectlyConnectedSearch.entity().getResource(), SearchCriteria.Op.NNULL);
SequenceSearch = createSearchBuilder();
SequenceSearch.and("id", SequenceSearch.entity().getId(), SearchCriteria.Op.EQ);
// SequenceSearch.addRetrieve("sequence", SequenceSearch.entity().getSequence());
SequenceSearch.done();
DirectlyConnectedSearch = createSearchBuilder();
DirectlyConnectedSearch.and("resource", DirectlyConnectedSearch.entity().getResource(), SearchCriteria.Op.NNULL);
DirectlyConnectedSearch.done();
UnmanagedDirectConnectSearch = createSearchBuilder();
UnmanagedDirectConnectSearch = createSearchBuilder();
UnmanagedDirectConnectSearch.and("resource", UnmanagedDirectConnectSearch.entity().getResource(), SearchCriteria.Op.NNULL);
UnmanagedDirectConnectSearch.and("server", UnmanagedDirectConnectSearch.entity().getManagementServerId(), SearchCriteria.Op.NULL);
UnmanagedDirectConnectSearch.and("lastPinged", UnmanagedDirectConnectSearch.entity().getLastPinged(), SearchCriteria.Op.LTEQ);
@ -241,13 +237,13 @@ public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao
CountRoutingByDc.and("dc", CountRoutingByDc.entity().getDataCenterId(), SearchCriteria.Op.EQ);
CountRoutingByDc.and("type", CountRoutingByDc.entity().getType(), SearchCriteria.Op.EQ);
CountRoutingByDc.and("status", CountRoutingByDc.entity().getStatus(), SearchCriteria.Op.EQ);
CountRoutingByDc.done();
_statusAttr = _allAttributes.get("status");
_msIdAttr = _allAttributes.get("managementServerId");
_pingTimeAttr = _allAttributes.get("lastPinged");
assert (_statusAttr != null && _msIdAttr != null && _pingTimeAttr != null) : "Couldn't find one of these attributes";
CountRoutingByDc.done();
_statusAttr = _allAttributes.get("status");
_msIdAttr = _allAttributes.get("managementServerId");
_pingTimeAttr = _allAttributes.get("lastPinged");
assert (_statusAttr != null && _msIdAttr != null && _pingTimeAttr != null) : "Couldn't find one of these attributes";
}
@Override
@ -259,77 +255,77 @@ public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao
List<HostVO> hosts = listBy(sc);
return hosts.size();
}
@Override
public HostVO findSecondaryStorageHost(long dcId) {
SearchCriteria<HostVO> sc = TypeDcSearch.create();
sc.setParameters("type", Host.Type.SecondaryStorage);
sc.setParameters("dc", dcId);
List<HostVO> storageHosts = listBy(sc);
if (storageHosts == null || storageHosts.size() != 1) {
return null;
} else {
return storageHosts.get(0);
}
}
@Override
public List<HostVO> listSecondaryStorageHosts() {
SearchCriteria<HostVO> sc = TypeSearch.create();
sc.setParameters("type", Host.Type.SecondaryStorage);
List<HostVO> secondaryStorageHosts = listIncludingRemovedBy(sc);
return secondaryStorageHosts;
}
@Override
public List<HostVO> findDirectlyConnectedHosts() {
SearchCriteria<HostVO> sc = DirectlyConnectedSearch.create();
return search(sc, null);
}
@Override
public List<HostVO> findDirectAgentToLoad(long msid, long lastPingSecondsAfter, Long limit) {
SearchCriteria<HostVO> sc = UnmanagedDirectConnectSearch.create();
sc.setParameters("lastPinged", lastPingSecondsAfter);
return search(sc, new Filter(HostVO.class, "clusterId", true, 0L, limit));
}
@Override
public void markHostsAsDisconnected(long msId, Status... states) {
SearchCriteria<HostVO> sc = MsStatusSearch.create();
sc.setParameters("ms", msId);
sc.setParameters("statuses", (Object[])states);
HostVO host = createForUpdate();
@Override
public HostVO findSecondaryStorageHost(long dcId) {
SearchCriteria<HostVO> sc = TypeDcSearch.create();
sc.setParameters("type", Host.Type.SecondaryStorage);
sc.setParameters("dc", dcId);
List<HostVO> storageHosts = listBy(sc);
if (storageHosts == null || storageHosts.size() != 1) {
return null;
} else {
return storageHosts.get(0);
}
}
@Override
public List<HostVO> listSecondaryStorageHosts() {
SearchCriteria<HostVO> sc = TypeSearch.create();
sc.setParameters("type", Host.Type.SecondaryStorage);
List<HostVO> secondaryStorageHosts = listIncludingRemovedBy(sc);
return secondaryStorageHosts;
}
@Override
public List<HostVO> findDirectlyConnectedHosts() {
SearchCriteria<HostVO> sc = DirectlyConnectedSearch.create();
return search(sc, null);
}
@Override
public List<HostVO> findDirectAgentToLoad(long msid, long lastPingSecondsAfter, Long limit) {
SearchCriteria<HostVO> sc = UnmanagedDirectConnectSearch.create();
sc.setParameters("lastPinged", lastPingSecondsAfter);
return search(sc, new Filter(HostVO.class, "clusterId", true, 0L, limit));
}
@Override
public void markHostsAsDisconnected(long msId, Status... states) {
SearchCriteria<HostVO> sc = MsStatusSearch.create();
sc.setParameters("ms", msId);
sc.setParameters("statuses", (Object[])states);
HostVO host = createForUpdate();
host.setManagementServerId(null);
host.setLastPinged((System.currentTimeMillis() >> 10) - ( 10 * 60 ));
host.setDisconnectedOn(new Date());
UpdateBuilder ub = getUpdateBuilder(host);
ub.set(host, "status", Status.Disconnected);
update(ub, sc, null);
}
@Override
public List<HostVO> listBy(Host.Type type, Long clusterId, Long podId, long dcId) {
host.setLastPinged((System.currentTimeMillis() >> 10) - ( 10 * 60 ));
host.setDisconnectedOn(new Date());
UpdateBuilder ub = getUpdateBuilder(host);
ub.set(host, "status", Status.Disconnected);
update(ub, sc, null);
}
@Override
public List<HostVO> listBy(Host.Type type, Long clusterId, Long podId, long dcId) {
SearchCriteria<HostVO> sc = TypePodDcStatusSearch.create();
if ( type != null ) {
if ( type != null ) {
sc.setParameters("type", type.toString());
}
if (clusterId != null) {
sc.setParameters("cluster", clusterId);
}
if (podId != null ) {
if (podId != null ) {
sc.setParameters("pod", podId);
}
sc.setParameters("dc", dcId);
sc.setParameters("status", Status.Up.toString());
return listBy(sc);
sc.setParameters("status", Status.Up.toString());
return listBy(sc);
}
@Override
@ -385,15 +381,15 @@ public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao
return listBy(sc);
}
@Override
public List<HostVO> listBy(Host.Type type, long dcId) {
SearchCriteria<HostVO> sc = TypeDcStatusSearch.create();
sc.setParameters("type", type.toString());
sc.setParameters("dc", dcId);
sc.setParameters("status", Status.Up.toString());
return listBy(sc);
@Override
public List<HostVO> listBy(Host.Type type, long dcId) {
SearchCriteria<HostVO> sc = TypeDcStatusSearch.create();
sc.setParameters("type", type.toString());
sc.setParameters("dc", dcId);
sc.setParameters("status", Status.Up.toString());
return listBy(sc);
}
@Override
@ -403,50 +399,50 @@ public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao
sc.setParameters("dc", dcId);
return listBy(sc);
}
@Override
public HostVO findByPrivateIpAddressInDataCenter(long dcId, String privateIpAddress) {
SearchCriteria<HostVO> sc = DcPrivateIpAddressSearch.create();
sc.setParameters("dc", dcId);
sc.setParameters("privateIpAddress", privateIpAddress);
return findOneBy(sc);
}
@Override
public HostVO findByStorageIpAddressInDataCenter(long dcId, String privateIpAddress) {
SearchCriteria<HostVO> sc = DcStorageIpAddressSearch.create();
sc.setParameters("dc", dcId);
sc.setParameters("storageIpAddress", privateIpAddress);
return findOneBy(sc);
}
@Override
public void loadDetails(HostVO host) {
Map<String, String> details =_detailsDao.findDetails(host.getId());
host.setDetails(details);
}
}
@Override
public HostVO findByPrivateIpAddressInDataCenter(long dcId, String privateIpAddress) {
SearchCriteria<HostVO> sc = DcPrivateIpAddressSearch.create();
sc.setParameters("dc", dcId);
sc.setParameters("privateIpAddress", privateIpAddress);
return findOneBy(sc);
}
@Override
public HostVO findByStorageIpAddressInDataCenter(long dcId, String privateIpAddress) {
SearchCriteria<HostVO> sc = DcStorageIpAddressSearch.create();
sc.setParameters("dc", dcId);
sc.setParameters("storageIpAddress", privateIpAddress);
return findOneBy(sc);
}
@Override
public void loadDetails(HostVO host) {
Map<String, String> details =_detailsDao.findDetails(host.getId());
host.setDetails(details);
}
@Override
public void loadHostTags(HostVO host){
List<String> hostTags = _hostTagsDao.gethostTags(host.getId());
host.setHostTags(hostTags);
}
}
@Override
public boolean updateStatus(HostVO host, Event event, long msId) {
Status oldStatus = host.getStatus();
long oldPingTime = host.getLastPinged();
Status newStatus = oldStatus.getNextStatus(event);
@Override
public boolean updateStatus(HostVO host, Event event, long msId) {
Status oldStatus = host.getStatus();
long oldPingTime = host.getLastPinged();
Status newStatus = oldStatus.getNextStatus(event);
if ( host == null ) {
return false;
}
if (newStatus == null) {
return false;
}
if (newStatus == null) {
return false;
}
SearchBuilder<HostVO> sb = createSearchBuilder();
sb.and("status", sb.entity().getStatus(), SearchCriteria.Op.EQ);
@ -458,112 +454,106 @@ public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao
sb.closeParen();
}
sb.done();
SearchCriteria<HostVO> sc = sb.create();
sc.setParameters("status", oldStatus);
sc.setParameters("id", host.getId());
if (newStatus.checkManagementServer()) {
SearchCriteria<HostVO> sc = sb.create();
sc.setParameters("status", oldStatus);
sc.setParameters("id", host.getId());
if (newStatus.checkManagementServer()) {
sc.setParameters("ping", oldPingTime);
sc.setParameters("msid", msId);
}
UpdateBuilder ub = getUpdateBuilder(host);
ub.set(host, _statusAttr, newStatus);
sc.setParameters("msid", msId);
}
UpdateBuilder ub = getUpdateBuilder(host);
ub.set(host, _statusAttr, newStatus);
if (newStatus.updateManagementServer()) {
if (newStatus.lostConnection()) {
ub.set(host, _msIdAttr, null);
} else {
} else {
ub.set(host, _msIdAttr, msId);
}
if( event.equals(Event.Ping) || event.equals(Event.AgentConnected)) {
if( event.equals(Event.Ping) || event.equals(Event.AgentConnected)) {
ub.set(host, _pingTimeAttr, System.currentTimeMillis() >> 10);
}
}
}
if ( event.equals(Event.ManagementServerDown)) {
ub.set(host, _pingTimeAttr, (( System.currentTimeMillis() >> 10) - ( 10 * 60 )));
}
int result = update(ub, sc, null);
assert result <= 1 : "How can this update " + result + " rows? ";
if (s_logger.isDebugEnabled() && result == 0) {
HostVO vo = findById(host.getId());
assert vo != null : "How how how? : " + host.getId();
StringBuilder str = new StringBuilder("Unable to update host for event:").append(event.toString());
str.append(". New=[status=").append(newStatus.toString()).append(":msid=").append(newStatus.lostConnection() ? "null" : msId).append(":lastpinged=").append(host.getLastPinged()).append("]");
str.append("; Old=[status=").append(oldStatus.toString()).append(":msid=").append(msId).append(":lastpinged=").append(oldPingTime).append("]");
str.append("; DB=[status=").append(vo.getStatus().toString()).append(":msid=").append(vo.getManagementServerId()).append(":lastpinged=").append(vo.getLastPinged()).append("]");
s_logger.debug(str.toString());
}
return result > 0;
}
@Override
public boolean disconnect(HostVO host, Event event, long msId) {
host.setDisconnectedOn(new Date());
}
int result = update(ub, sc, null);
assert result <= 1 : "How can this update " + result + " rows? ";
if (s_logger.isDebugEnabled() && result == 0) {
HostVO vo = findById(host.getId());
assert vo != null : "How how how? : " + host.getId();
StringBuilder str = new StringBuilder("Unable to update host for event:").append(event.toString());
str.append(". New=[status=").append(newStatus.toString()).append(":msid=").append(newStatus.lostConnection() ? "null" : msId).append(":lastpinged=").append(host.getLastPinged()).append("]");
str.append("; Old=[status=").append(oldStatus.toString()).append(":msid=").append(msId).append(":lastpinged=").append(oldPingTime).append("]");
str.append("; DB=[status=").append(vo.getStatus().toString()).append(":msid=").append(vo.getManagementServerId()).append(":lastpinged=").append(vo.getLastPinged()).append("]");
s_logger.debug(str.toString());
}
return result > 0;
}
@Override
public boolean disconnect(HostVO host, Event event, long msId) {
host.setDisconnectedOn(new Date());
if(event!=null && event.equals(Event.Remove)) {
host.setGuid(null);
host.setClusterId(null);
}
return updateStatus(host, event, msId);
}
@Override @DB
public boolean connect(HostVO host, long msId) {
Transaction txn = Transaction.currentTxn();
long id = host.getId();
txn.start();
if (!updateStatus(host, Event.AgentConnected, msId)) {
return false;
}
txn.commit();
return true;
}
@Override
public HostVO findByGuid(String guid) {
SearchCriteria<HostVO> sc = GuidSearch.create("guid", guid);
return findOneBy(sc);
}
return updateStatus(host, event, msId);
}
@Override @DB
public boolean connect(HostVO host, long msId) {
Transaction txn = Transaction.currentTxn();
long id = host.getId();
txn.start();
if (!updateStatus(host, Event.AgentConnected, msId)) {
return false;
}
txn.commit();
return true;
}
@Override
public HostVO findByGuid(String guid) {
SearchCriteria<HostVO> sc = GuidSearch.create("guid", guid);
return findOneBy(sc);
}
@Override
public List<HostVO> findLostHosts(long timeout) {
SearchCriteria<HostVO> sc = LastPingedSearch.create();
sc.setParameters("ping", timeout);
sc.setParameters("state", Status.Up.toString(), Status.Updating.toString(),
Status.Disconnected.toString(), Status.Down.toString());
return listBy(sc);
}
@Override
public HostVO findByName(String name) {
SearchCriteria<HostVO> sc = NameSearch.create("name", name);
return findOneBy(sc);
}
@Override
public List<HostVO> findLostHosts(long timeout) {
SearchCriteria<HostVO> sc = LastPingedSearch.create();
sc.setParameters("ping", timeout);
sc.setParameters("state", Status.Up.toString(), Status.Updating.toString(),
Status.Disconnected.toString(), Status.Down.toString());
return listBy(sc);
}
@Override
public List<HostVO> findHostsLike(String hostName) {
SearchCriteria<HostVO> sc = NameLikeSearch.create();
sc.setParameters("name", "%" + hostName + "%");
return listBy(sc);
}
@Override
public List<HostVO> findLostHosts2(long timeout, Type type) {
SearchCriteria<HostVO> sc = LastPingedSearch2.create();
sc.setParameters("ping", timeout);
sc.setParameters("type", type.toString());
return listBy(sc);
}
@Override
public List<HostVO> listByDataCenter(long dcId) {
SearchCriteria<HostVO> sc = DcSearch.create("dc", dcId);
return listBy(sc);
}
public List<HostVO> findHostsLike(String hostName) {
SearchCriteria<HostVO> sc = NameLikeSearch.create();
sc.setParameters("name", "%" + hostName + "%");
return listBy(sc);
}
@Override
public List<HostVO> findLostHosts2(long timeout, Type type) {
SearchCriteria<HostVO> sc = LastPingedSearch2.create();
sc.setParameters("ping", timeout);
sc.setParameters("type", type.toString());
return listBy(sc);
}
@Override
public List<HostVO> listByDataCenter(long dcId) {
SearchCriteria<HostVO> sc = DcSearch.create("dc", dcId);
return listBy(sc);
}
@Override
public HostVO findConsoleProxyHost(String name, Type type) {
@ -578,43 +568,42 @@ public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao
return hostList.get(0);
}
}
@Override
public List<HostVO> listByHostPod(long podId) {
SearchCriteria<HostVO> sc = PodSearch.create("pod", podId);
return listBy(sc);
}
@Override
public List<HostVO> listByStatus(Status... status) {
SearchCriteria<HostVO> sc = StatusSearch.create();
sc.setParameters("status", (Object[])status);
return listBy(sc);
}
@Override
public List<HostVO> listByTypeDataCenter(Type type, long dcId) {
SearchCriteria<HostVO> sc = TypeDcSearch.create();
sc.setParameters("type", type.toString());
sc.setParameters("dc", dcId);
return listBy(sc);
}
@Override
public List<HostVO> listByType(Type type) {
SearchCriteria<HostVO> sc = TypeSearch.create();
sc.setParameters("type", type.toString());
return listBy(sc);
}
public List<HostVO> listByHostPod(long podId) {
SearchCriteria<HostVO> sc = PodSearch.create("pod", podId);
return listBy(sc);
}
@Override
public List<HostVO> listByStatus(Status... status) {
SearchCriteria<HostVO> sc = StatusSearch.create();
sc.setParameters("status", (Object[])status);
return listBy(sc);
}
@Override
public void saveDetails(HostVO host) {
Map<String, String> details = host.getDetails();
if (details == null) {
return;
}
_detailsDao.persist(host.getId(), details);
@Override
public List<HostVO> listByTypeDataCenter(Type type, long dcId) {
SearchCriteria<HostVO> sc = TypeDcSearch.create();
sc.setParameters("type", type.toString());
sc.setParameters("dc", dcId);
return listBy(sc);
}
@Override
public List<HostVO> listByType(Type type) {
SearchCriteria<HostVO> sc = TypeSearch.create();
sc.setParameters("type", type.toString());
return listBy(sc);
}
protected void saveDetails(HostVO host) {
Map<String, String> details = host.getDetails();
if (details == null) {
return;
}
_detailsDao.persist(host.getId(), details);
}
protected void saveHostTags(HostVO host) {
@ -623,15 +612,15 @@ public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao
return;
}
_hostTagsDao.persist(host.getId(), hostTags);
}
@Override @DB
}
@Override @DB
public HostVO persist(HostVO host) {
final String InsertSequenceSql = "INSERT INTO op_host(id) VALUES(?)";
Transaction txn = Transaction.currentTxn();
txn.start();
Transaction txn = Transaction.currentTxn();
txn.start();
HostVO dbHost = super.persist(host);
try {
@ -646,71 +635,71 @@ public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao
loadDetails(dbHost);
saveHostTags(host);
loadHostTags(dbHost);
txn.commit();
return dbHost;
txn.commit();
return dbHost;
}
@Override @DB
public boolean update(Long hostId, HostVO host) {
Transaction txn = Transaction.currentTxn();
txn.start();
boolean persisted = super.update(hostId, host);
if (!persisted) {
return persisted;
}
saveDetails(host);
@Override @DB
public boolean update(Long hostId, HostVO host) {
Transaction txn = Transaction.currentTxn();
txn.start();
boolean persisted = super.update(hostId, host);
if (!persisted) {
return persisted;
}
saveDetails(host);
saveHostTags(host);
txn.commit();
return persisted;
}
@Override @DB
public List<RunningHostCountInfo> getRunningHostCounts(Date cutTime) {
txn.commit();
return persisted;
}
@Override @DB
public List<RunningHostCountInfo> getRunningHostCounts(Date cutTime) {
String sql = "select * from (" +
"select h.data_center_id, h.type, count(*) as count from host as h INNER JOIN mshost as m ON h.mgmt_server_id=m.msid " +
"where h.status='Up' and h.type='SecondaryStorage' and m.last_update > ? " +
"group by h.data_center_id, h.type " +
"UNION ALL " +
"select h.data_center_id, h.type, count(*) as count from host as h INNER JOIN mshost as m ON h.mgmt_server_id=m.msid " +
"where h.status='Up' and h.type='Routing' and m.last_update > ? " +
"group by h.data_center_id, h.type) as t " +
"ORDER by t.data_center_id, t.type";
"select h.data_center_id, h.type, count(*) as count from host as h INNER JOIN mshost as m ON h.mgmt_server_id=m.msid " +
"where h.status='Up' and h.type='SecondaryStorage' and m.last_update > ? " +
"group by h.data_center_id, h.type " +
"UNION ALL " +
"select h.data_center_id, h.type, count(*) as count from host as h INNER JOIN mshost as m ON h.mgmt_server_id=m.msid " +
"where h.status='Up' and h.type='Routing' and m.last_update > ? " +
"group by h.data_center_id, h.type) as t " +
"ORDER by t.data_center_id, t.type";
ArrayList<RunningHostCountInfo> l = new ArrayList<RunningHostCountInfo>();
Transaction txn = Transaction.currentTxn();;
PreparedStatement pstmt = null;
try {
Transaction txn = Transaction.currentTxn();;
PreparedStatement pstmt = null;
try {
pstmt = txn.prepareAutoCloseStatement(sql);
String gmtCutTime = DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), cutTime);
pstmt.setString(1, gmtCutTime);
pstmt.setString(2, gmtCutTime);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
RunningHostCountInfo info = new RunningHostCountInfo();
info.setDcId(rs.getLong(1));
info.setHostType(rs.getString(2));
info.setCount(rs.getInt(3));
l.add(info);
}
} catch (SQLException e) {
} catch (Throwable e) {
}
return l;
}
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
RunningHostCountInfo info = new RunningHostCountInfo();
info.setDcId(rs.getLong(1));
info.setHostType(rs.getString(2));
info.setCount(rs.getInt(3));
l.add(info);
}
} catch (SQLException e) {
} catch (Throwable e) {
}
return l;
}
@Override
public long getNextSequence(long hostId) {
if (s_logger.isTraceEnabled()) {
s_logger.trace("getNextSequence(), hostId: " + hostId);
public long getNextSequence(long hostId) {
if (s_logger.isTraceEnabled()) {
s_logger.trace("getNextSequence(), hostId: " + hostId);
}
TableGenerator tg = _tgs.get("host_req_sq");
@ -764,8 +753,18 @@ public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao
sc.setParameters("type", Host.Type.Routing);
sc.setParameters("status", Status.Up.toString());
return customSearch(sc, null).get(0);
}
}
}
@Override
public List<HostVO> listSecondaryStorageHosts(long dataCenterId) {
SearchCriteria<HostVO> sc = TypeDcSearch.create();
sc.setParameters("type", Host.Type.SecondaryStorage);
sc.setParameters("dc", dataCenterId);
List<HostVO> secondaryStorageHosts = listIncludingRemovedBy(sc);
return secondaryStorageHosts;
}
}

View File

@ -166,11 +166,7 @@ public class KvmServerDiscoverer extends DiscovererBase implements Discoverer,
return null;
}
s_logger.debug("copying " + _setupAgentPath + " to host");
SCPClient scp = new SCPClient(sshConnection);
scp.put(_setupAgentPath, "/usr/bin", "0755");
String parameters = " -h " + _hostIp + " -z " + dcId + " -p " + podId + " -c " + clusterId + " -u " + guid;
String parameters = " --host=" + _hostIp + " --zone=" + dcId + " --pod=" + podId + " --cluster=" + clusterId + " --guid=" + guid + " -a";
if (_kvmPublicNic != null) {
parameters += " -P " + _kvmPublicNic;
@ -180,7 +176,7 @@ public class KvmServerDiscoverer extends DiscovererBase implements Discoverer,
parameters += " -N " + _kvmPrivateNic;
}
SSHCmdHelper.sshExecuteCmd(sshConnection, "/usr/bin/setup_agent.sh " + parameters + " 1>&2", 3);
SSHCmdHelper.sshExecuteCmd(sshConnection, "cloud-setup-agent " + parameters + " >& /dev/null", 3);
KvmDummyResourceBase kvmResource = new KvmDummyResourceBase();
Map<String, Object> params = new HashMap<String, Object>();

View File

@ -81,21 +81,30 @@ public class DirectPodBasedNetworkGuru extends DirectNetworkGuru {
public NicProfile allocate(Network network, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm) throws InsufficientVirtualNetworkCapcityException,
InsufficientAddressCapacityException, ConcurrentOperationException {
DataCenter dc = _dcDao.findById(network.getDataCenterId());
DataCenterVO dc = _dcDao.findById(network.getDataCenterId());
ReservationStrategy rsStrategy = ReservationStrategy.Start;
_dcDao.loadDetails(dc);
String dhcpStrategy = dc.getDetail(ZoneConfig.DhcpStrategy.key());
if ("external".equalsIgnoreCase(dhcpStrategy)) {
rsStrategy = ReservationStrategy.Create;
}
NetworkOffering offering = _networkOfferingDao.findByIdIncludingRemoved(network.getNetworkOfferingId());
if (!canHandle(offering, dc)) {
return null;
}
if (nic == null) {
nic = new NicProfile(ReservationStrategy.Start, null, null, null, null);
nic = new NicProfile(rsStrategy, null, null, null, null);
} else if (nic.getIp4Address() == null) {
nic.setStrategy(ReservationStrategy.Start);
} else {
nic.setStrategy(ReservationStrategy.Create);
}
if (rsStrategy == ReservationStrategy.Create) {
String mac = _networkMgr.getNextAvailableMacAddressInNetwork(network.getId());
nic.setMacAddress(mac);
}
return nic;
}

View File

@ -16,8 +16,8 @@
*
*/
package com.cloud.servlet;
package com.cloud.servlet;
import java.util.List;
import javax.servlet.ServletContextEvent;
@ -39,22 +39,22 @@ import com.cloud.user.UserVO;
import com.cloud.user.dao.UserDao;
import com.cloud.utils.SerialVersionUID;
import com.cloud.utils.component.ComponentLocator;
public class RegisterCompleteServlet extends HttpServlet implements ServletContextListener {
public static final Logger s_logger = Logger.getLogger(RegisterCompleteServlet.class.getName());
static final long serialVersionUID = SerialVersionUID.CloudStartupServlet;
public class RegisterCompleteServlet extends HttpServlet implements ServletContextListener {
public static final Logger s_logger = Logger.getLogger(RegisterCompleteServlet.class.getName());
static final long serialVersionUID = SerialVersionUID.CloudStartupServlet;
protected static AccountService _accountSvc = null;
protected static ConfigurationDao _configDao = null;
protected static UserDao _userDao = null;
@Override
protected static UserDao _userDao = null;
@Override
public void init() throws ServletException {
ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
_accountSvc = locator.getManager(AccountService.class);
_configDao = locator.getDao(ConfigurationDao.class);
_userDao = locator.getDao(UserDao.class);
_userDao = locator.getDao(UserDao.class);
}
@Override
@ -118,5 +118,5 @@ public class RegisterCompleteServlet extends HttpServlet implements ServletConte
} catch (Exception ex) {
s_logger.error("unknown exception writing register complete response", ex);
}
}
}
}
}

View File

@ -26,6 +26,7 @@ import com.cloud.capacity.CapacityVO;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.HostPodVO;
import com.cloud.deploy.DeployDestination;
import com.cloud.deploy.DeploymentPlan;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientStorageCapacityException;
import com.cloud.exception.StorageUnavailableException;
@ -72,6 +73,13 @@ public interface StorageManager extends Manager {
* @return secondary storage host
*/
public HostVO getSecondaryStorageHost(long zoneId);
/**
* Returns the secondary storage host
* @param zoneId
* @return secondary storage host
*/
public VMTemplateHostVO findVmTemplateHost(long templateId, long dcId, Long podId);
/**
* Moves a volume from its current storage pool to a storage pool with enough capacity in the specified zone, pod, or cluster

View File

@ -90,6 +90,7 @@ import com.cloud.dc.dao.ClusterDao;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.dc.dao.HostPodDao;
import com.cloud.deploy.DeployDestination;
import com.cloud.deploy.DeploymentPlan;
import com.cloud.domain.Domain;
import com.cloud.domain.dao.DomainDao;
import com.cloud.event.ActionEvent;
@ -2887,7 +2888,28 @@ public class StorageManagerImpl implements StorageManager, StorageService, Manag
}
@Override
public StoragePool getStoragePool(long id) {
return _storagePoolDao.findById(id);
}
public VMTemplateHostVO findVmTemplateHost(long templateId, long dcId, Long podId) {
List<HostVO> secHosts = _hostDao.listSecondaryStorageHosts(dcId);
if (secHosts.size() == 1) {
VMTemplateHostVO templateHostVO = _templateHostDao.findByHostTemplate(secHosts.get(0).getId(), templateId);
return templateHostVO;
}
if (podId != null) {
List<VMTemplateHostVO> templHosts = _templateHostDao.listByTemplateStatus(templateId, dcId, podId, VMTemplateStorageResourceAssoc.Status.DOWNLOADED);
for (VMTemplateHostVO templHost: templHosts) {
return templHost;
}
}
List<VMTemplateHostVO> templHosts = _templateHostDao.listByTemplateStatus(templateId, dcId, VMTemplateStorageResourceAssoc.Status.DOWNLOADED);
for (VMTemplateHostVO templHost: templHosts) {
return templHost;
}
return null;
}
}

View File

@ -31,6 +31,7 @@ import com.cloud.configuration.dao.ConfigurationDao;
import com.cloud.dc.ClusterVO;
import com.cloud.dc.dao.ClusterDao;
import com.cloud.deploy.DataCenterDeployment;
import com.cloud.deploy.DeploymentPlan;
import com.cloud.deploy.DeploymentPlanner.ExcludeList;
import com.cloud.host.Host;
import com.cloud.host.HostVO;
@ -74,7 +75,7 @@ public abstract class AbstractStoragePoolAllocator extends AdapterBase implement
@Inject StoragePoolHostDao _poolHostDao;
@Inject ConfigurationDao _configDao;
@Inject ClusterDao _clusterDao;
float _storageOverprovisioningFactor;
int _storageOverprovisioningFactor;
long _extraBytesPerVolume = 0;
Random _rand;
boolean _dontMatter;
@ -87,7 +88,7 @@ public abstract class AbstractStoragePoolAllocator extends AdapterBase implement
Map<String, String> configs = _configDao.getConfiguration(null, params);
String globalStorageOverprovisioningFactor = configs.get("storage.overprovisioning.factor");
_storageOverprovisioningFactor = NumbersUtil.parseFloat(globalStorageOverprovisioningFactor, 2);
_storageOverprovisioningFactor = NumbersUtil.parseInt(globalStorageOverprovisioningFactor, 2);
_extraBytesPerVolume = 0;
@ -132,7 +133,7 @@ public abstract class AbstractStoragePoolAllocator extends AdapterBase implement
}
protected boolean checkPool(ExcludeList avoid, StoragePoolVO pool, DiskProfile dskCh, VMTemplateVO template, List<VMTemplateStoragePoolVO> templatesInPool,
StatsCollector sc) {
StatsCollector sc, DeploymentPlan plan) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Checking if storage pool is suitable, name: " + pool.getName()+ " ,poolId: "+ pool.getId());
@ -225,28 +226,25 @@ public abstract class AbstractStoragePoolAllocator extends AdapterBase implement
}
if ((template != null) && !tmpinstalled) {
// If the template that was passed into this allocator is not installed in the storage pool,
// add 3 * (template size on secondary storage) to the running total
HostVO secondaryStorageHost = _storageMgr.getSecondaryStorageHost(pool.getDataCenterId());
if (secondaryStorageHost == null) {
return false;
} else {
VMTemplateHostVO templateHostVO = _templateHostDao.findByHostTemplate(secondaryStorageHost.getId(), template.getId());
if (templateHostVO == null) {
s_logger.debug("Cannot allocate this pool " + pool.getId() + " since no entry found in template_host_ref, hostId: " + secondaryStorageHost.getId() + " and templateId: "+template.getId());
return false;
} else {
s_logger.debug("For template: " + template.getName() + ", using template size multiplier: " + 2);
long templateSize = templateHostVO.getSize();
long templatePhysicalSize = templateHostVO.getPhysicalSize();
totalAllocatedSize += (templateSize + _extraBytesPerVolume) + (templatePhysicalSize + _extraBytesPerVolume);
}
}
// If the template that was passed into this allocator is not installed in the storage pool,
// add 3 * (template size on secondary storage) to the running total
VMTemplateHostVO templateHostVO = _storageMgr.findVmTemplateHost(template.getId(), plan.getDataCenterId(), plan.getPodId());
if (templateHostVO == null) {
s_logger.info("Did not find template downloaded on secondary hosts in zone " + plan.getDataCenterId());
return false;
} else {
s_logger.debug("For template: " + template.getName() + ", using template size multiplier: " + 2);
long templateSize = templateHostVO.getSize();
long templatePhysicalSize = templateHostVO.getPhysicalSize();
totalAllocatedSize += (templateSize + _extraBytesPerVolume) + (templatePhysicalSize + _extraBytesPerVolume);
}
}
long askingSize = dskCh.getSize();
float storageOverprovisioningFactor = 1;
int storageOverprovisioningFactor = 1;
if (pool.getPoolType() == StoragePoolType.NetworkFilesystem) {
storageOverprovisioningFactor = _storageOverprovisioningFactor;
}

View File

@ -88,7 +88,7 @@ public class FirstFitStoragePoolAllocator extends AbstractStoragePoolAllocator {
if(suitablePools.size() == returnUpTo){
break;
}
if (checkPool(avoid, pool, dskCh, template, null, sc)) {
if (checkPool(avoid, pool, dskCh, template, null, sc, plan)) {
suitablePools.add(pool);
}
}

View File

@ -78,7 +78,7 @@ public class RandomStoragePoolAllocator extends AbstractStoragePoolAllocator {
if(suitablePools.size() == returnUpTo){
break;
}
if (checkPool(avoid, pool, dskCh, template, null, sc)) {
if (checkPool(avoid, pool, dskCh, template, null, sc, plan)) {
suitablePools.add(pool);
}
}

View File

@ -58,7 +58,10 @@ public class VMTemplateHostDaoImpl extends GenericDaoBase<VMTemplateHostVO, Long
+ "WHERE host_id = ? and type_id = ?";
protected static final String DOWNLOADS_STATE_DC=
"SELECT * FROM template_host_ref t, host h where t.host_id = h.id and h.data_center_id=? "
"SELECT t.id, t.host_id, t.pool_id, t.template_id, t.created, t.last_updated, t.job_id, "
+ "t.download_pct, t.size, t.physical_size, t.download_state, t.error_str, t.local_path, "
+ "t.install_path, t.url, t.destroyed, t.is_copy FROM template_host_ref t, host h "
+ "where t.host_id = h.id and h.data_center_id=? "
+ " and t.template_id=? and t.download_state = ?" ;
protected static final String DOWNLOADS_STATE_DC_POD=

View File

@ -56,12 +56,14 @@ import com.cloud.storage.StoragePoolHostVO;
import com.cloud.storage.VMTemplateHostVO;
import com.cloud.storage.VMTemplateStoragePoolVO;
import com.cloud.storage.VMTemplateStorageResourceAssoc;
import com.cloud.storage.VMTemplateZoneVO;
import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
import com.cloud.storage.VMTemplateVO;
import com.cloud.storage.dao.StoragePoolHostDao;
import com.cloud.storage.dao.VMTemplateDao;
import com.cloud.storage.dao.VMTemplateHostDao;
import com.cloud.storage.dao.VMTemplatePoolDao;
import com.cloud.storage.dao.VMTemplateZoneDao;
import com.cloud.storage.template.TemplateConstants;
import com.cloud.storage.template.TemplateInfo;
import com.cloud.user.Account;
@ -85,6 +87,8 @@ public class DownloadMonitorImpl implements DownloadMonitor {
private static final String DEFAULT_HTTP_COPY_PORT = "80";
@Inject
VMTemplateHostDao _vmTemplateHostDao;
@Inject
VMTemplateZoneDao _vmTemplateZoneDao;
@Inject
VMTemplatePoolDao _vmTemplatePoolDao;
@Inject
@ -438,6 +442,7 @@ public class DownloadMonitorImpl implements DownloadMonitor {
s_logger.warn("Huh? Agent id " + sserverId + " does not correspond to a row in hosts table?");
return;
}
long zoneId = storageHost.getDataCenterId();
Set<VMTemplateVO> toBeDownloaded = new HashSet<VMTemplateVO>();
List<VMTemplateVO> allTemplates = _templateDao.listAllInZone(storageHost.getDataCenterId());
@ -500,6 +505,15 @@ public class DownloadMonitorImpl implements DownloadMonitor {
tmpltHost.setSize(tmpltInfo.getSize());
tmpltHost.setPhysicalSize(tmpltInfo.getPhysicalSize());
_vmTemplateHostDao.persist(tmpltHost);
VMTemplateZoneVO tmpltZoneVO = _vmTemplateZoneDao.findByZoneTemplate(zoneId, tmplt.getId());
if (tmpltZoneVO == null){
tmpltZoneVO = new VMTemplateZoneVO(zoneId, tmplt.getId(), new Date());
_vmTemplateZoneDao.persist(tmpltZoneVO);
} else {
tmpltZoneVO.setLastUpdated(new Date());
_vmTemplateZoneDao.update(tmpltZoneVO.getId(), tmpltZoneVO);
}
}
continue;
@ -511,6 +525,14 @@ public class DownloadMonitorImpl implements DownloadMonitor {
s_logger.info("Template Sync did not find " + uniqueName + " on the server " + sserverId + ", will request download shortly");
VMTemplateHostVO templtHost = new VMTemplateHostVO(sserverId, tmplt.getId(), new Date(), 0, Status.NOT_DOWNLOADED, null, null, null, null, tmplt.getUrl());
_vmTemplateHostDao.persist(templtHost);
VMTemplateZoneVO tmpltZoneVO = _vmTemplateZoneDao.findByZoneTemplate(zoneId, tmplt.getId());
if (tmpltZoneVO == null){
tmpltZoneVO = new VMTemplateZoneVO(zoneId, tmplt.getId(), new Date());
_vmTemplateZoneDao.persist(tmpltZoneVO);
} else {
tmpltZoneVO.setLastUpdated(new Date());
_vmTemplateZoneDao.update(tmpltZoneVO.getId(), tmpltZoneVO);
}
}
}

View File

@ -46,6 +46,7 @@ import com.cloud.storage.dao.VMTemplateDao;
import com.cloud.storage.dao.VMTemplateHostDao;
import com.cloud.storage.dao.VMTemplateZoneDao;
import com.cloud.storage.resource.DummySecondaryStorageResource;
import com.cloud.storage.resource.LocalSecondaryStorageResource;
import com.cloud.storage.resource.NfsSecondaryStorageResource;
import com.cloud.utils.component.Inject;
import com.cloud.utils.net.NfsUtils;

View File

@ -343,20 +343,13 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe
}
}
SearchCriteria<VMTemplateHostVO> sc = HostTemplateStatesSearch.create();
sc.setParameters("id", templateId);
sc.setParameters("state", Status.DOWNLOADED);
sc.setJoinParameters("host", "dcId", pool.getDataCenterId());
List<VMTemplateHostVO> templateHostRefs = _tmpltHostDao.search(sc, null);
templateHostRef = _storageMgr.findVmTemplateHost(templateId, pool.getDataCenterId(), pool.getPodId());
if (templateHostRefs.size() == 0) {
if (templateHostRef == null) {
s_logger.debug("Unable to find a secondary storage host who has completely downloaded the template.");
return null;
}
templateHostRef = templateHostRefs.get(0);
HostVO sh = _hostDao.findById(templateHostRef.getHostId());
origUrl = sh.getStorageUrl();
if (origUrl == null) {
@ -398,7 +391,7 @@ public class TemplateManagerImpl implements TemplateManager, Manager, TemplateSe
String url = origUrl + "/" + templateHostRef.getInstallPath();
PrimaryStorageDownloadCommand dcmd = new PrimaryStorageDownloadCommand(template.getUniqueName(), url, template.getFormat(),
template.getAccountId(), pool.getId(), pool.getUuid());
HostVO secondaryStorageHost = _hostDao.findSecondaryStorageHost(pool.getDataCenterId());
HostVO secondaryStorageHost = _hostDao.findById(templateHostRef.getHostId());
assert(secondaryStorageHost != null);
dcmd.setSecondaryStorageUrl(secondaryStorageHost.getStorageUrl());
// TODO temporary hacking, hard-coded to NFS primary data store

View File

@ -1163,7 +1163,7 @@ public class AccountManagerImpl implements AccountManager, AccountService, Manag
if (!user.getPassword().equals(dbUser.getPassword())) {
throw new CloudRuntimeException("The user " + username + " being creating is using a password that is different than what's in the db");
}
System.out.println("user: " + dbUser.getId());
return _userAccountDao.findById(dbUser.getId());
} catch (Exception e) {
if (e instanceof CloudRuntimeException) {

View File

@ -2531,13 +2531,54 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_VM_START, vm.getAccountId(), vm.getDataCenterId(), vm.getId(), vm.getHostName(), vm.getServiceOfferingId(), vm.getTemplateId(), vm
.getHypervisorType().toString());
_usageEventDao.persist(usageEvent);
Answer startAnswer = cmds.getAnswer(StartAnswer.class);
String returnedIp = null;
String originalIp = null;
if (startAnswer != null) {
StartAnswer startAns = (StartAnswer) startAnswer;
VirtualMachineTO vmTO = startAns.getVirtualMachine();
for (NicTO nicTO: vmTO.getNics()) {
if (nicTO.getType() == TrafficType.Guest) {
returnedIp = nicTO.getIp();
}
}
}
List<NicVO> nics = _nicDao.listByVmId(vm.getId());
NicVO guestNic = null;
for (NicVO nic : nics) {
NetworkVO network = _networkDao.findById(nic.getNetworkId());
long isDefault = (nic.isDefaultNic()) ? 1 : 0;
usageEvent = new UsageEventVO(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN, vm.getAccountId(), vm.getDataCenterId(), vm.getId(), vm.getHostName(), network.getNetworkOfferingId(), null, isDefault);
_usageEventDao.persist(usageEvent);
if (network.getTrafficType() == TrafficType.Guest) {
originalIp = nic.getIp4Address();
guestNic = nic;
}
}
boolean ipChanged = false;
if (originalIp != null && !originalIp.equalsIgnoreCase(returnedIp)) {
if (returnedIp != null && guestNic != null) {
guestNic.setIp4Address(returnedIp);
ipChanged = true;
}
}
if (returnedIp != null && !returnedIp.equalsIgnoreCase(originalIp)) {
if (guestNic != null) {
guestNic.setIp4Address(returnedIp);
ipChanged = true;
}
}
if (ipChanged) {
DataCenterVO dc = _dcDao.findById(vm.getDataCenterId());
UserVmVO userVm = profile.getVirtualMachine();
if (dc.getDhcpProvider().equalsIgnoreCase(Provider.ExternalDhcpServer.getName())){
_nicDao.update(guestNic.getId(), guestNic);
userVm.setPrivateIpAddress(guestNic.getIp4Address());
_vmDao.update(userVm.getId(), userVm);
s_logger.info("Detected that ip changed in the answer, updated nic in the db with new ip " + returnedIp);
}
}
return true;

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>^
<classpathentry exported="true" kind="lib" path="jnetpcap.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>

BIN
thirdparty/jnetpcap.jar vendored Normal file

Binary file not shown.

51
tools/test/apisession.py Normal file
View File

@ -0,0 +1,51 @@
import cookielib
import hashlib
import json
import os
import random
import sys
import urllib2
import urllib
class ApiSession:
"""an ApiSession represents one api session, with cookies."""
def __init__(self, url, username, password):
self._username = username
self._password = hashlib.md5(password).hexdigest()
self._url = url
self._cj = cookielib.CookieJar()
self._opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self._cj))
def _get(self, parameters):
encoded = urllib.urlencode(parameters)
url = "%s?%s"% (self._url, encoded)
try:
f = self._opener.open(url)
return f.read()
except urllib2.HTTPError as exn:
print "Command %s failed" % parameters['command']
print "Reason: %s" % json.loads(exn.read())
def GET(self, parameters):
parameters['sessionkey'] = self._sessionkey
parameters['response'] = 'json'
return self._get(parameters)
def _post(self, parameters):
return self._opener.open(self._url, urllib.urlencode(parameters)).read()
def POST(self, parameters):
parameters['sessionkey'] = self._sessionkey
parameters['response'] = 'json'
return self._post(parameters)
def login(self):
params = {'command':'login', 'response': 'json'}
params['username'] = self._username
params['password'] = self._password
result = self._get(params)
jsonresult = json.loads(result)
jsessionid = None
self._sessionkey = jsonresult['loginresponse']['sessionkey']

58
tools/test/cloudkit.py Normal file
View File

@ -0,0 +1,58 @@
from apisession import ApiSession
from physicalresource import ZoneCreator
from globalconfig import GlobalConfig
from db import Database
import random
import uuid
def fix_default_db():
database = Database()
statement="""
UPDATE vm_template SET url='%s'
WHERE unique_name='%s' """
database.update(statement % ('http://nfs1.lab.vmops.com/templates/dummy/systemvm.vhd', 'routing-1'))
database.update(statement % ('http://nfs1.lab.vmops.com/templates/dummy/systemvm.qcow2', 'routing-3'))
database.update(statement % ('http://nfs1.lab.vmops.com/templates/dummy/systemvm.ova', 'routing-8'))
database.update(statement % ('http://nfs1.lab.vmops.com/templates/dummy/builtin.vhd', 'centos53-x86_64'))
database.update(statement % ('http://nfs1.lab.vmops.com/templates/dummy/builtin.qcow2', 'centos55-x86_64'))
database.update(statement % ('http://nfs1.lab.vmops.com/templates/dummy/builtin.ova', 'centos53-x64'))
statement="""UPDATE vm_template SET checksum=NULL"""
database.update(statement)
statement="""UPDATE disk_offering set use_local_storage=1"""
database.update(statement)
def config():
config = GlobalConfig(api)
config.update('use.local.storage', 'true')
config.update('max.template.iso.size', '20')
def create_zone():
zonecreator = ZoneCreator(api, random.randint(2,1000))
zoneid = zonecreator.create()
database = Database()
statement="""INSERT INTO data_center_details (dc_id, name, value)
VALUES (%s, '%s', '0')"""
database.update(statement % (zoneid, 'enable.secstorage.vm'))
database.update(statement % (zoneid, 'enable.consoleproxy.vm'))
statement="""INSERT INTO data_center_details (dc_id, name, value)
VALUES (%s, '%s', '%s')"""
database.update(statement % (zoneid, 'zone.dhcp.strategy', 'external'))
statement="""UPDATE data_center set dhcp_provider='ExternalDhcpServer' where id=%s"""
database.update(statement % (zoneid))
if __name__ == "__main__":
fix_default_db()
api = ApiSession('http://localhost:8080/client/api', 'admin', 'password')
api.login()
config()
create_zone()
print "guid=%s" % str(uuid.uuid4())

18
tools/test/db.py Normal file
View File

@ -0,0 +1,18 @@
import MySQLdb
class Database:
"""Database connection"""
def __init__(self, host='localhost', username='cloud', password='cloud', db='cloud'):
self._conn = MySQLdb.connect (host, username, password, db)
def update(self, statement):
cursor = self._conn.cursor ()
#print statement
cursor.execute (statement)
#print "Number of rows updated: %d" % cursor.rowcount
cursor.close ()
self._conn.commit ()
def __del__(self):
self._conn.close ()

View File

@ -0,0 +1,18 @@
import json
import os
import random
import sys
class GlobalConfig:
"""Updates global configuration values"""
def __init__(self, api):
self._api = api
def update(self, key, value):
jsonresult = self._api.GET({'command': 'updateConfiguration', 'name':key,
'value':value})
if jsonresult is None:
print "Failed to update configuration"
return 1
return 0

View File

@ -0,0 +1,54 @@
import json
import os
import random
import sys
class ZoneCreator:
"""Creates a zone (and a pod and cluster for now)"""
def __init__(self, api, zonenum, dns1='192.168.10.254',
dns2='192.168.10.253', internaldns='192.168.10.254'):
self._api = api
self._zonenum = zonenum
self._zonename = "ZONE%04d"%zonenum
self._dns1 = dns1
self._dns2 = dns2
self._internaldns = internaldns
def create(self):
jsonresult = self._api.GET({'command': 'createZone', 'networktype':'Basic',
'name':self._zonename, 'dns1':self._dns1, 'dns2':self._dns2,
'internaldns1':self._internaldns})
if jsonresult is None:
print "Failed to create zone"
return 0
jsonobj = json.loads(jsonresult)
self._zoneid = jsonobj['createzoneresponse']['zone']['id']
self._zonetoken = jsonobj['createzoneresponse']['zone']['zonetoken']
print "Zone %s is created"%self._zonename
print "zone=%s"%self._zonetoken
#self.createPod()
return self._zoneid
def createPod(self):
self._podname = "POD%04d"%self._zonenum
self._clustername = "CLUSTER%04d"%self._zonenum
jsonresult = self._api.GET({'command': 'createPod', 'zoneId':self._zoneid,
'name':self._podname, 'gateway':'192.168.1.1', 'netmask':'255.255.255.0',
'startIp':'192.168.1.100', 'endIp':'192.168.1.150'})
if jsonresult is None:
print "Failed to create pod"
return 2
jsonobj = json.loads(jsonresult)
podid = jsonobj['createpodresponse']['pod']['id']
jsonresult = self._api.GET({'command': 'addCluster', 'zoneId':self._zoneid,
'clustername':self._clustername, 'podId':podid, 'hypervisor':'KVM',
'clustertype':'CloudManaged'})
if jsonresult is None:
print "Failed to create cluster"
return 3
jsonobj = json.loads(jsonresult)
clusterid = jsonobj['addclusterresponse']['cluster'][0]['id']
print "pod=%s"%podid
print "cluster=%s"%clusterid

52
tools/test/vm.py Normal file
View File

@ -0,0 +1,52 @@
import json
import os
import random
import sys
import time
#http://localhost:8080/client/api?_=1303171692177&command=listTemplates&templatefilter=featured&zoneid=1&pagesize=6&page=1&response=json&sessionkey=%2Bh3Gh4BffWpQdk4nXmcC88uEk9k%3D
#http://localhost:8080/client/api?_=1303171711292&command=deployVirtualMachine&zoneId=1&hypervisor=KVM&templateId=4&serviceOfferingId=7&response=json&sessionkey=%2Bh3Gh4BffWpQdk4nXmcC88uEk9k%3D
#http://localhost:8080/client/api?_=1303171934824&command=queryAsyncJobResult&jobId=20&response=json&sessionkey=%2Bh3Gh4BffWpQdk4nXmcC88uEk9k%3D
class VMCreator:
"""Creates a VM """
def __init__(self, api, params):
self._api = api
self._params = params
def create(self):
cmd = {'command': 'deployVirtualMachine'}
cmd.update(self._params)
jsonresult = self._api.GET(cmd)
if jsonresult is None:
print "Failed to create VM"
return 0
jsonobj = json.loads(jsonresult)
self._jobid = jsonobj['deployvirtualmachineresponse']['jobid']
self._vmid = jsonobj['deployvirtualmachineresponse']['id']
print "VM %s creation is scheduled, job=%s"%(self._vmid, self._jobid)
def poll(self, tries, wait):
jobstatus = -1
while jobstatus < 1 and tries > 0:
time.sleep(wait)
cmd = {'command': 'queryAsyncJobResult', 'jobId': self._jobid}
jsonresult = self._api.GET(cmd)
if jsonresult is None:
print "Failed to query VM creation job"
return -1
jsonobj = json.loads(jsonresult)
jobstatus = jsonobj['queryasyncjobresultresponse']['jobstatus']
print jobstatus, type(jobstatus)
tries = tries - 1
if jobstatus == 1:
jsonobj = json.loads(jsonresult)
jobresult = jsonobj['queryasyncjobresultresponse']['jobresult']
vm = jobresult['virtualmachine']
print vm
else:
print "Failed to create vm"
return jobstatus

19
tools/test/vmcreate.py Normal file
View File

@ -0,0 +1,19 @@
from apisession import ApiSession
from vm import VMCreator
#http://localhost:8080/client/api?_=1303171711292&command=deployVirtualMachine&zoneId=1&hypervisor=KVM&templateId=4&serviceOfferingId=7&response=json&sessionkey=%2Bh3Gh4BffWpQdk4nXmcC88uEk9k%3D
def create_vm():
vmcreator = VMCreator(api, {'zoneId':1, 'hypervisor':'KVM', 'templateId':4,
'serviceOfferingId':7,
'userdata':'dGhpcyBpcyBhIHRlc3QK'})
vmid = vmcreator.create()
vmcreator.poll(10, 3)
if __name__ == "__main__":
api = ApiSession('http://localhost:8080/client/api', 'admin', 'password')
api.login()
create_vm()

385
ui/cloudkit/cloudkit.jsp Normal file
View File

@ -0,0 +1,385 @@
<% long now = System.currentTimeMillis(); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>
<link rel="stylesheet" href="css/main.css" type="text/css" />
<!-- Common libraries -->
<script type="text/javascript" src="scripts/json2.js"></script>
<script type="text/javascript" src="../scripts/jquery.min.js"></script>
<script type="text/javascript" src="../scripts/jquery-ui.custom.min.js"></script>
<script type="text/javascript" src="../scripts/date.js"></script>
<script type="text/javascript" src="../scripts/jquery.cookies.js"></script>
<script type="text/javascript" src="../scripts/jquery.timers.js"></script>
<script type="text/javascript" src="../scripts/jquery.md5.js"></script>
<!-- cloud.com scripts -->
<script type="text/javascript" src="scripts/cloudkit.js?t=<%=now%>"></script>
<script type="text/javascript" src="scripts/cloudkit.hosts.js?t=<%=now%>"></script>
<!-- Favicon -->
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<title>myCloud</title>
</head>
<body>
<!-- Host template -->
<div class="db_gridrows" id="host_template" style="display:none">
<div class="db_gridcolumns" style="width:20%;">
<div class="db_gridcelltitles" id="hostname"></div>
</div>
<div class="db_gridcolumns" style="width:15%;">
<div class="db_gridcelltitles green" id="state"></div>
</div>
<div class="db_gridcolumns" style="width:15%;">
<div class="db_gridcelltitles" id="ip"></div>
</div>
<div class="db_gridcolumns" style="width:20%;">
<div class="db_gridcelltitles" id="version"></div>
</div>
<div class="db_gridcolumns" style="width:20%;">
<div class="db_gridcelltitles" id="disconnected"></div>
</div>
<div class="db_gridcolumns" style="width:10%;">
<a class="db_statistics_icon" href="#" id="host_details"></a>
<a class="db_delete_icon" style="margin-left:25px" href="#" id="host_delete"></a>
</div>
</div>
<!-- End Host template -->
<div id="main" style="display:none">
<div id="dialog_overlay" style="display:none;">
<div class="overlay_black"></div>
<!-- Statitics overlay starts here-->
<div class="overlay_dialogbox" id="dialog_host_details" style="display:none;">
<div class="overlay_dialogbox_contentarea">
<div class="overlay_dialogbox_titlearea"><h2>Host Details</h2></div>
<div class="overlay_dialogbox_content">
<div class="db_stats_gridbox">
<div class="db_stats_gridrow ">
<div class="db_stats_gridcolumns" style="width:48%;">
<div class="db_stats_gridcelltitles">Host GUID: </div>
</div>
<div class="db_stats_gridcolumns" style="width:50%;">
<div class="db_stats_gridcelltitles"><strong id="host_id"></strong></div>
</div>
</div>
<div class="db_stats_gridrow ">
<div class="db_stats_gridcolumns" style="width:48%;">
<div class="db_stats_gridcelltitles">Added Date: </div>
</div>
<div class="db_stats_gridcolumns" style="width:50%;">
<div class="db_stats_gridcelltitles"><strong id="host_added"></strong></div>
</div>
</div>
<div class="db_stats_gridrow ">
<div class="db_stats_gridcolumns" style="width:48%;">
<div class="db_stats_gridcelltitles">CPU Total: </div>
</div>
<div class="db_stats_gridcolumns" style="width:50%;">
<div class="db_stats_gridcelltitles"><strong id="host_cpu_total">4 x 2.40 GHZ</strong></div>
</div>
</div>
<div class="db_stats_gridrow ">
<div class="db_stats_gridcolumns" style="width:48%;">
<div class="db_stats_gridcelltitles">CPU Allocated:</div>
</div>
<div class="db_stats_gridcolumns" style="width:50%;">
<div class="db_stats_gridcelltitles"><strong id="host_cpu_allocated">20.83%</strong></div>
</div>
</div>
<div class="db_stats_gridrow ">
<div class="db_stats_gridcolumns" style="width:48%;">
<div class="db_stats_gridcelltitles">CPU Used: </div>
</div>
<div class="db_stats_gridcolumns" style="width:50%;">
<div class="db_stats_gridcelltitles"><strong id="host_cpu_used">0.04%</strong></div>
</div>
</div>
<div class="db_stats_gridrow ">
<div class="db_stats_gridcolumns" style="width:48%;">
<div class="db_stats_gridcelltitles">Memory Total:</div>
</div>
<div class="db_stats_gridcolumns" style="width:50%;">
<div class="db_stats_gridcelltitles"><strong id="host_mem_total">3.09 GB</strong></div>
</div>
</div>
<div class="db_stats_gridrow ">
<div class="db_stats_gridcolumns" style="width:48%;">
<div class="db_stats_gridcelltitles">Memory Allocated:</div>
</div>
<div class="db_stats_gridcolumns" style="width:50%;">
<div class="db_stats_gridcelltitles"><strong id="host_mem_allocated">2.63 GB</strong></div>
</div>
</div>
<div class="db_stats_gridrow ">
<div class="db_stats_gridcolumns" style="width:48%;">
<div class="db_stats_gridcelltitles">Memory Used:</div>
</div>
<div class="db_stats_gridcolumns" style="width:50%;">
<div class="db_stats_gridcelltitles"><strong id="host_mem_used">2.63 GB</strong></div>
</div>
</div>
<div class="db_stats_gridrow ">
<div class="db_stats_gridcolumns" style="width:48%;">
<div class="db_stats_gridcelltitles">Network Read:</div>
</div>
<div class="db_stats_gridcolumns" style="width:50%;">
<div class="db_stats_gridcelltitles"><strong id="host_net_read">4338950879.03 TB</strong></div>
</div>
</div>
<div class="db_stats_gridrow ">
<div class="db_stats_gridcolumns" style="width:48%;">
<div class="db_stats_gridcelltitles">Network Write:</div>
</div>
<div class="db_stats_gridcolumns" style="width:50%;">
<div class="db_stats_gridcelltitles"><strong id="host_net_sent">4352955092.25 TB</strong></div>
</div>
</div>
</div>
</div>
<div class="overlay_dialogbox_confirmationbox">
<div class="overlay_dialogbox_confirmationbuttonbox">
<div class="overlay_dialog_button" id="dialog_ok">OK</div>
</div>
</div>
</div>
</div>
<!-- Statitics overlay ends here-->
<!-- Delete overlay starts here-->
<div class="overlay_dialogbox" style="display:none;" id="dialog_delete_host">
<div class="overlay_dialogbox_contentarea">
<div class="overlay_dialogbox_titlearea"><h2>Confirmation</h2></div>
<div class="overlay_dialogbox_content">
<p>Please confirm that you want to remove the host, <strong id="hostname"></strong>, from your cloud.</p>
</div>
<div class="overlay_dialogbox_confirmationbox">
<div class="overlay_dialogbox_confirmationbuttonbox">
<a href="#" id="dialog_cancel">Cancel</a>
<div class="overlay_dialog_button" id="dialog_confirm">Confirm</div>
</div>
</div>
</div>
</div>
<!-- Delete overlay ends here-->
</div>
<div id="header">
<div class="logo"></div>
<div class="user_links"><p><span id="header_username"></span></p><p><a href="#" id="header_logout">Logout</a></p></div>
</div>
<div class="main_contentbg">
<div class="db_gridcontainer">
<div class="db_gridcontainer_topbox">
<div class="db_gridcontainer_topbox_left">
<h2>myCloud</h2>
<div id="search_panel" class="db_grid_searchbox">
<div class="db_grid_searchicon"></div>
<input id="search_input" class="text" type="text" />
<a id="clear_search_button" class="db_grid_search_closeicon" href="#" style="display:none;"></a>
</div>
<div id="refresh_button" class="db_gridcontainer_refreshbox"><a href="#"> Refresh </a></div>
</div>
<div class="db_gridcontainer_topbox_right">
<div class="db_grid_tabbox">
<div class="db_grid_tabs on" id="tab_hosts">Hosts</div>
</div>
<div class="db_grid_tabbox">
<div class="db_grid_tabs off" id="tab_docs">Getting Started</div>
</div>
</div>
</div>
<div class="db_tabcontent" id="tab_hosts_content" style="display:block;">
<div class="db_gridbox">
<div class="db_gridrows header">
<div class="db_gridcolumns header" style="width:20%;">
<div class="db_gridcelltitles header">Name</div>
</div>
<div class="db_gridcolumns header" style="width:15%;">
<div class="db_gridcelltitles header">State</div>
</div>
<div class="db_gridcolumns header" style="width:15%;">
<div class="db_gridcelltitles header">IP Address</div>
</div>
<div class="db_gridcolumns header" style="width:20%;">
<div class="db_gridcelltitles header">Version</div>
</div>
<div class="db_gridcolumns header" style="width:20%;">
<div class="db_gridcelltitles header">Last Disconnected</div>
</div>
<div class="db_gridcolumns header" style="width:10%;">
<div class="db_gridcelltitles header">Actions</div>
</div>
</div>
<div class="db_maingrid">
<!--Reminder for completing registrtaion starts here-->
<div class="db_gridmsgbox" style="display:none;" id="registration_complete_container">
<div class="db_gridmsgbox_content">
<p>
You have successfully added your first compute node. Please complete your registration process by clicking here: <a id="registration_complete_link" href='#'>Complete Registration</a>
</p>
</div>
</div>
<!--Reminder for completing registrtaion ends here-->
<div id="host_container">
</div>
</div>
<div class="db_grid_navigationpanel">
<div class="db_gridb_paginationbox"><p>Page <span id="page_number">N</span></p></div>
<div class="db_gridb_navbox">
<a id="prev_page_button" href="#">Prev</a>
<a id="next_page_button" href="#">Next</a>
</div>
</div>
</div>
</div>
<div class="db_tabcontent" id="tab_docs_content" style="display:none;">
<div class="db_gridbox">
<div class="db_gridrows header">
<div class="db_gridcolumns header" style="width:70%;">
<div class="db_gridcelltitles header"></div>
</div>
</div>
<div class="db_maingrid">
<div class="dbinstruction_contentarea">
<div class="dbinstruction_submenubox">
<div class="dbinstruction_submenubox_content">
<ul>
<li><a href="#gettingstarted">Getting Started</a></li>
<li><a href="#managehost">Managing Your Hosts</a></li>
</ul>
</div>
</div>
<div id="getting_started"><a name="gettingstarted"></a>
<h3>Getting Started</h3>
<div class="db_downlaodbox">
<p>Your zone token is: <strong id="zone_token"></strong></p>
<a class="db_instructiondownlaodbutton" href="#">Download Installer</a>
</div>
<h4>Contents</h4>
<p>Cloud.com's <a href="http://cloud.com/products/cloud-computing-software">CloudStack</a> agent + RightScale's <a href="http://support.rightscale.com/06-FAQs/FAQ_0178_-_What_is_inside_of_a_RightImage%3F">RightImage</a> + README</p>
<h4>System requirements</h4>
<div class="dbinstruction_bulletbox">
<ul>
<li>One or more computer hosts capable of running the software. See <a href="http://linuxhcl.com/">Linux Hardware Compatibility List</a>.</li>
<li>Ubuntu 10.04 LTS installed on each machine. Available at <a href="http://releases.ubuntu.com/">Ubuntu Releases</a>.</li>
</ul>
</div>
<h4>Adding a New Host</h4>
<p>To add a new host, install the CloudStack agent software on it.</p>
<div class="dbinstruction_bulletbox">
<ol>
<li>Click the Download button to get the CloudKit installation bundle.</li>
<li>Log in as root to the host machine where you want to install the software.
The machine must have <a href="http://releases.ubuntu.com/">Ubuntu 10.04 LTS</a> installed.
</li>
<li>Place the CloudKit installation bundle on the host and untar.
<div class="dbinstruction_bulletbox_codebox"># tar xzf &lt;Installer file name&gt;.tar.gz</div>
</li>
<li>Run the installer.
<div class="dbinstruction_bulletbox_codebox"># cd &lt;Directory of extracted installer&gt;</div>
<div class="dbinstruction_bulletbox_codebox"># sudo ./install.sh</div>
</li>
<li>At the prompt, type A to install the agent. Installation can take a few minutes.
When installation is finished, a message like "Agent installation is completed" appears.</li>
<li>After the agent installation finishes, run the setup script.</li>
<div class="dbinstruction_bulletbox_codebox"># sudo mycloud-setup-agent</div>
<li>For Management Server address, use the default if it is not localhost, or input the IP address provided to you by
Cloud.com. Currently the only Management Server IP being provided is 192.168.130.223.</li>
<li>When prompted, paste in the zone token for your cloud. You can find it at the top of this page.
The zone token is a unique code that identifies your cloud. You will need it throughout the lifetime of your cloud.
</li>
<li>Provide the name of the network interface you want to use, or (typically) accept the default.
</li>
<li>When the "Installation complete!" message appears, confirm that the new host appears in the Hosts tab of the <a href="http://216.38.159.3:8080/client/cloudkit/login.jsp ">CloudKit administration UI</a>.
You can also run this command on the host to be sure the cloud-agent software is running:
<div class="dbinstruction_bulletbox_codebox"># sudo service cloud-agent status</div>
</li>
<li>Click <a id="registration_complete_doc_link" href='#'>Complete Registration</a>.
The new host is automatically registered with RightScale as part of your CloudKit cloud.</p>
</li>
</ol>
</div>
<p><strong>You're ready to start using your new host!</strong></p>
<h4>More Information</h4>
<p style="margin-top:5px;"><a href="#">RightScale's CoudKit docs (title and URL: TBD)</a></p>
</div>
<div id="managing_host">
<h3>Managing Your Hosts</h3>
<a name="managehost"></a>
<p>Use the Hosts tab of the CloudKit administration UI to view host status or delete hosts.</p>
<p>(Want to do more than view or delete? To add a host, click the Installer tab. For other cloud management tasks, <a href="#">RightScale UI</a>.)</p>
<div class="dbinstruction_bulletbox">
<ul>
<li><strong>Host Name</strong>: IP address or fully-qualified DNS name of the host</li>
<li><strong>State:</strong></li>
<li style="margin-left:15px; display:inline;">Connecting: Request to connect to the cloud has been received and the host is attempting to comply</li>
<li style="margin-left:15px; display:inline;">Up: Host is connected and operational in the cloud</li>
<li style="margin-left:15px; display:inline;">Down: Host is connected but not operational</li>
<li style="margin-left:15px; display:inline;">Disconnected: Host is not connected to the cloud</li>
<li style="margin-left:15px; display:inline;">Updating: State of the host is in transition</li>
<li style="margin-left:15px; display:inline;">PrepareForMaintenance: Request to put the host into maintenance mode has been received</li>
<li style="margin-left:15px; display:inline;">ErrorInMaintenance: Action taken during maintenance mode has failed</li>
<li style="margin-left:15px; display:inline;">CancelMaintenance: Request to abandon maintenance activity and restore host to previous condition has been received</li>
<li style="margin-left:15px; display:inline;">Maintenance: Host is in maintenance mode and is not available for new VM instances</li>
<li style="margin-left:15px; display:inline;">Alert: Error condition exists on the host and requires attention</li>
<li style="margin-left:15px; display:inline;">Removed: Request to delete the host has been received</li>
<li><strong>Version</strong>: Version of CloudKit software installed on the host</li>
<li><strong>Last Disconnected</strong>: Timestamp recording any prior disconnection of the host from the cloud</li>
<li><strong>Action</strong></li>
<li style="margin-left:15px; display:inline;"><a class="db_statistics_icon" style="margin:-5px 5px 0 0;"></a> Statistic Icon: Click to display resource usage statistics for troubleshooting and tuning</li>
<li style="margin-left:15px; display:inline;"><a class="db_delete_icon" style="margin:-5px 5px 0 0;"></a>Delete Icon: Click to delete host; a confirmation dialog will be displayed</li>
</ul>
</div>
<h4>Adding a New Host</h4>
<p style="margin-top:5px;">To add a new host, install the CloudStack agent software on it. Click the Getting Started tab and follow the <a href="#gettingstarted">installation steps</a>.</p>
<h4>More Information</h4>
<p style="margin-top:5px;"><a href="#">RightScale's CoudKit docs (title and URL: TBD)</a></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="footer">
<div class="footer_left"><p>&copy; 2006-2011 RightScale, Inc. All rights reserved. RightScale is a registered trademark of RightScale, Inc. </p></div>
<div class="footer_right">
<a class="poweredby" href="http://cloud.com/"></a>
</div>
</div>
</div>
</body>
</html>

1551
ui/cloudkit/css/main.css Normal file

File diff suppressed because it is too large Load Diff

BIN
ui/cloudkit/images/.DS_Store vendored Normal file

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 263 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 175 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 712 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 707 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 267 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 222 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 413 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 407 B

Some files were not shown because too many files have changed in this diff Show More