mirror of https://github.com/apache/cloudstack.git
more formatting changes
This commit is contained in:
parent
c11d702cac
commit
b6ba3c0e3d
File diff suppressed because it is too large
Load Diff
|
|
@ -16,31 +16,43 @@
|
|||
*
|
||||
*/
|
||||
|
||||
package com.cloud.agent;
|
||||
|
||||
package com.cloud.agent;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.cloud.utils.backoff.BackoffAlgorithm;
|
||||
|
||||
public interface IAgentShell {
|
||||
public Map<String, Object> getCmdLineProperties();
|
||||
public Properties getProperties();
|
||||
public String getPersistentProperty(String prefix, String name);
|
||||
public void setPersistentProperty(String prefix, String name, String value);
|
||||
|
||||
public String getHost();
|
||||
public String getPrivateIp();
|
||||
public int getPort();
|
||||
public int getWorkers();
|
||||
public int getProxyPort();
|
||||
public String getGuid();
|
||||
public String getZone();
|
||||
public String getPod();
|
||||
|
||||
public BackoffAlgorithm getBackoffAlgorithm();
|
||||
public int getPingRetries();
|
||||
|
||||
public void upgradeAgent(final String url);
|
||||
public String getVersion();
|
||||
}
|
||||
|
||||
public interface IAgentShell {
|
||||
public Map<String, Object> getCmdLineProperties();
|
||||
|
||||
public Properties getProperties();
|
||||
|
||||
public String getPersistentProperty(String prefix, String name);
|
||||
|
||||
public void setPersistentProperty(String prefix, String name, String value);
|
||||
|
||||
public String getHost();
|
||||
|
||||
public String getPrivateIp();
|
||||
|
||||
public int getPort();
|
||||
|
||||
public int getWorkers();
|
||||
|
||||
public int getProxyPort();
|
||||
|
||||
public String getGuid();
|
||||
|
||||
public String getZone();
|
||||
|
||||
public String getPod();
|
||||
|
||||
public BackoffAlgorithm getBackoffAlgorithm();
|
||||
|
||||
public int getPingRetries();
|
||||
|
||||
public void upgradeAgent(final String url);
|
||||
|
||||
public String getVersion();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,52 +28,51 @@ import com.cloud.utils.component.Manager;
|
|||
import com.cloud.utils.component.PluggableService;
|
||||
import com.cloud.utils.db.GenericDao;
|
||||
|
||||
|
||||
public class AgentComponentLibraryBase extends ComponentLibraryBase {
|
||||
@Override
|
||||
public Map<String, ComponentInfo<GenericDao<?, ?>>> getDaos() {
|
||||
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, 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<String, List<ComponentInfo<Adapter>>> getAdapters() {
|
||||
if (_adapters.size() == 0) {
|
||||
populateAdapters();
|
||||
}
|
||||
return _adapters;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Class<?>, Class<?>> getFactories() {
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Map<Class<?>, Class<?>> getFactories() {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected void populateManagers() {
|
||||
//addManager("StackMaidManager", StackMaidManagerImpl.class);
|
||||
}
|
||||
protected void populateManagers() {
|
||||
// addManager("StackMaidManager", StackMaidManagerImpl.class);
|
||||
}
|
||||
|
||||
protected void populateAdapters() {
|
||||
protected void populateAdapters() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected void populateServices() {
|
||||
protected void populateServices() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, ComponentInfo<PluggableService>> getPluggableServices() {
|
||||
if (_pluggableServices.size() == 0) {
|
||||
populateServices();
|
||||
}
|
||||
return _pluggableServices;
|
||||
}
|
||||
@Override
|
||||
public Map<String, ComponentInfo<PluggableService>> getPluggableServices() {
|
||||
if (_pluggableServices.size() == 0) {
|
||||
populateServices();
|
||||
}
|
||||
return _pluggableServices;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,12 +20,12 @@ package com.cloud.agent.dao;
|
|||
|
||||
import com.cloud.utils.component.Manager;
|
||||
|
||||
|
||||
/**
|
||||
* StorageDao is an abstraction layer for what the agent will use for storage.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public interface StorageComponent extends Manager {
|
||||
String get(String key);
|
||||
void persist(String key, String value);
|
||||
String get(String key);
|
||||
|
||||
void persist(String key, String value);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,98 +34,98 @@ import com.cloud.utils.PropertiesUtil;
|
|||
|
||||
/**
|
||||
* Uses Properties to implement storage.
|
||||
*
|
||||
* @config
|
||||
* {@table
|
||||
* || Param Name | Description | Values | Default ||
|
||||
* || path | path to the properties _file | String | db/db.properties ||
|
||||
* }
|
||||
*
|
||||
* @config {@table || Param Name | Description | Values | Default || || path |
|
||||
* path to the properties _file | String | db/db.properties || * }
|
||||
**/
|
||||
@Local(value={StorageComponent.class})
|
||||
@Local(value = { StorageComponent.class })
|
||||
public class PropertiesStorage implements StorageComponent {
|
||||
private static final Logger s_logger = Logger.getLogger(PropertiesStorage.class);
|
||||
Properties _properties = new Properties();
|
||||
File _file;
|
||||
String _name;
|
||||
private static final Logger s_logger = Logger
|
||||
.getLogger(PropertiesStorage.class);
|
||||
Properties _properties = new Properties();
|
||||
File _file;
|
||||
String _name;
|
||||
|
||||
@Override
|
||||
public synchronized String get(String key) {
|
||||
return _properties.getProperty(key);
|
||||
}
|
||||
@Override
|
||||
public synchronized String get(String key) {
|
||||
return _properties.getProperty(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void persist(String key, String value) {
|
||||
_properties.setProperty(key, value);
|
||||
FileOutputStream output = null;
|
||||
try {
|
||||
output = new FileOutputStream(_file);
|
||||
_properties.store(output, _name);
|
||||
output.flush();
|
||||
output.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
s_logger.error("Who deleted the file? ", e);
|
||||
} catch (IOException e) {
|
||||
s_logger.error("Uh-oh: ", e);
|
||||
} finally {
|
||||
if (output != null) {
|
||||
try {
|
||||
output.close();
|
||||
} catch (IOException e) {
|
||||
//ignore.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public synchronized void persist(String key, String value) {
|
||||
_properties.setProperty(key, value);
|
||||
FileOutputStream output = null;
|
||||
try {
|
||||
output = new FileOutputStream(_file);
|
||||
_properties.store(output, _name);
|
||||
output.flush();
|
||||
output.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
s_logger.error("Who deleted the file? ", e);
|
||||
} catch (IOException e) {
|
||||
s_logger.error("Uh-oh: ", e);
|
||||
} finally {
|
||||
if (output != null) {
|
||||
try {
|
||||
output.close();
|
||||
} catch (IOException e) {
|
||||
// ignore.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean configure(String name, Map<String, Object> params) {
|
||||
_name = name;
|
||||
String path = (String)params.get("path");
|
||||
if (path == null) {
|
||||
path = "agent.properties";
|
||||
}
|
||||
@Override
|
||||
public boolean configure(String name, Map<String, Object> params) {
|
||||
_name = name;
|
||||
String path = (String) params.get("path");
|
||||
if (path == null) {
|
||||
path = "agent.properties";
|
||||
}
|
||||
|
||||
File file = PropertiesUtil.findConfigFile(path);
|
||||
if (file == null) {
|
||||
file = new File(path);
|
||||
try {
|
||||
if (!file.createNewFile()) {
|
||||
s_logger.error("Unable to create _file: " + file.getAbsolutePath());
|
||||
return false;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
s_logger.error("Unable to create _file: " + file.getAbsolutePath(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
File file = PropertiesUtil.findConfigFile(path);
|
||||
if (file == null) {
|
||||
file = new File(path);
|
||||
try {
|
||||
if (!file.createNewFile()) {
|
||||
s_logger.error("Unable to create _file: "
|
||||
+ file.getAbsolutePath());
|
||||
return false;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
s_logger.error(
|
||||
"Unable to create _file: " + file.getAbsolutePath(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
_properties.load(new FileInputStream(file));
|
||||
_file = file;
|
||||
} catch (FileNotFoundException e) {
|
||||
s_logger.error("How did we get here? ", e);
|
||||
return false;
|
||||
} catch (IOException e) {
|
||||
s_logger.error("IOException: ", e);
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
_properties.load(new FileInputStream(file));
|
||||
_file = file;
|
||||
} catch (FileNotFoundException e) {
|
||||
s_logger.error("How did we get here? ", e);
|
||||
return false;
|
||||
} catch (IOException e) {
|
||||
s_logger.error("IOException: ", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return _name;
|
||||
}
|
||||
@Override
|
||||
public String getName() {
|
||||
return _name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean start() {
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public boolean start() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean stop() {
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public boolean stop() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,244 +15,242 @@ import org.jnetpcap.protocol.tcpip.Udp;
|
|||
|
||||
import com.cloud.agent.dhcp.DhcpSnooperImpl.DHCPState;
|
||||
|
||||
public class DhcpPacketParser implements Runnable{
|
||||
private static final Logger s_logger = Logger.getLogger(DhcpPacketParser.class);
|
||||
private enum DHCPPACKET {
|
||||
OP(0),
|
||||
HTYPE(1),
|
||||
HLEN(2),
|
||||
HOPS(3),
|
||||
XID(4),
|
||||
SECS(8),
|
||||
FLAGS(10),
|
||||
CIADDR(12),
|
||||
YIADDR(16),
|
||||
SIDADDR(20),
|
||||
GIADDR(24),
|
||||
CHADDR(28),
|
||||
SNAME(44),
|
||||
FILE(108),
|
||||
MAGIC(236),
|
||||
OPTIONS(240);
|
||||
int offset;
|
||||
DHCPPACKET(int i) {
|
||||
offset = i;
|
||||
}
|
||||
int getValue() {
|
||||
return offset;
|
||||
}
|
||||
}
|
||||
private enum DHCPOPTIONTYPE {
|
||||
PAD(0),
|
||||
MESSAGETYPE(53),
|
||||
REQUESTEDIP(50),
|
||||
END(255);
|
||||
int type;
|
||||
DHCPOPTIONTYPE(int i) {
|
||||
type = i;
|
||||
}
|
||||
int getValue() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
private enum DHCPMSGTYPE {
|
||||
DHCPDISCOVER(1),
|
||||
DHCPOFFER(2),
|
||||
DHCPREQUEST(3),
|
||||
DHCPDECLINE(4),
|
||||
DHCPACK(5),
|
||||
DHCPNAK(6),
|
||||
DHCPRELEASE(7),
|
||||
DHCPINFORM(8);
|
||||
int _type;
|
||||
DHCPMSGTYPE(int type) {
|
||||
_type = type;
|
||||
}
|
||||
int getValue() {
|
||||
return _type;
|
||||
}
|
||||
public static DHCPMSGTYPE valueOf(int type) {
|
||||
for (DHCPMSGTYPE t : values()) {
|
||||
if (type == t.getValue()) {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public class DhcpPacketParser implements Runnable {
|
||||
private static final Logger s_logger = Logger
|
||||
.getLogger(DhcpPacketParser.class);
|
||||
|
||||
private class DHCPMSG {
|
||||
DHCPMSGTYPE msgType;
|
||||
byte[] caddr;
|
||||
byte[] yaddr;
|
||||
byte[] chaddr;
|
||||
byte[] requestedIP;
|
||||
public DHCPMSG() {
|
||||
caddr = new byte[4];
|
||||
yaddr = new byte[4];
|
||||
chaddr = new byte[6];
|
||||
}
|
||||
}
|
||||
private enum DHCPPACKET {
|
||||
OP(0), HTYPE(1), HLEN(2), HOPS(3), XID(4), SECS(8), FLAGS(10), CIADDR(
|
||||
12), YIADDR(16), SIDADDR(20), GIADDR(24), CHADDR(28), SNAME(44), FILE(
|
||||
108), MAGIC(236), OPTIONS(240);
|
||||
int offset;
|
||||
|
||||
private PcapPacket _buffer;
|
||||
private int _offset;
|
||||
private int _len;
|
||||
private DhcpSnooperImpl _manager;
|
||||
DHCPPACKET(int i) {
|
||||
offset = i;
|
||||
}
|
||||
|
||||
int getValue() {
|
||||
return offset;
|
||||
}
|
||||
}
|
||||
|
||||
public DhcpPacketParser(PcapPacket buffer, int offset, int len, DhcpSnooperImpl manager) {
|
||||
_buffer = buffer;
|
||||
_offset = offset;
|
||||
_len = len;
|
||||
_manager = manager;
|
||||
}
|
||||
private int getPos(int pos) {
|
||||
return _offset + pos;
|
||||
}
|
||||
private byte getByte(int offset) {
|
||||
return _buffer.getByte(getPos(offset));
|
||||
}
|
||||
private void getByteArray(int offset, byte[] array) {
|
||||
_buffer.getByteArray(getPos(offset), array);
|
||||
}
|
||||
private long getUInt(int offset) {
|
||||
return _buffer.getUInt(getPos(offset));
|
||||
}
|
||||
private enum DHCPOPTIONTYPE {
|
||||
PAD(0), MESSAGETYPE(53), REQUESTEDIP(50), END(255);
|
||||
int type;
|
||||
|
||||
private DHCPMSG getDhcpMsg() {
|
||||
long magic = getUInt(DHCPPACKET.MAGIC.getValue());
|
||||
if (magic != 0x63538263) {
|
||||
return null;
|
||||
}
|
||||
DHCPOPTIONTYPE(int i) {
|
||||
type = i;
|
||||
}
|
||||
|
||||
DHCPMSG msg = new DHCPMSG();
|
||||
int getValue() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
int pos = DHCPPACKET.OPTIONS.getValue();
|
||||
while (pos <= _len) {
|
||||
int type = (int)getByte(pos++) & 0xff;
|
||||
private enum DHCPMSGTYPE {
|
||||
DHCPDISCOVER(1), DHCPOFFER(2), DHCPREQUEST(3), DHCPDECLINE(4), DHCPACK(
|
||||
5), DHCPNAK(6), DHCPRELEASE(7), DHCPINFORM(8);
|
||||
int _type;
|
||||
|
||||
if (type == DHCPOPTIONTYPE.END.getValue()) {
|
||||
break;
|
||||
}
|
||||
if (type == DHCPOPTIONTYPE.PAD.getValue()) {
|
||||
continue;
|
||||
}
|
||||
int len = 0;
|
||||
if (pos <= _len) {
|
||||
len = ((int)getByte(pos++)) & 0xff;
|
||||
}
|
||||
DHCPMSGTYPE(int type) {
|
||||
_type = type;
|
||||
}
|
||||
|
||||
if (type == DHCPOPTIONTYPE.MESSAGETYPE.getValue() || type == DHCPOPTIONTYPE.REQUESTEDIP.getValue()) {
|
||||
/*Read data only if needed */
|
||||
byte[] data = null;
|
||||
if ((len + pos) <= _len) {
|
||||
data = new byte[len];
|
||||
getByteArray(pos, data);
|
||||
}
|
||||
int getValue() {
|
||||
return _type;
|
||||
}
|
||||
|
||||
if (type == DHCPOPTIONTYPE.MESSAGETYPE.getValue()) {
|
||||
msg.msgType = DHCPMSGTYPE.valueOf((int)data[0]);
|
||||
} else if (type == DHCPOPTIONTYPE.REQUESTEDIP.getValue()) {
|
||||
msg.requestedIP = data;
|
||||
}
|
||||
}
|
||||
public static DHCPMSGTYPE valueOf(int type) {
|
||||
for (DHCPMSGTYPE t : values()) {
|
||||
if (type == t.getValue()) {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
pos += len;
|
||||
}
|
||||
private class DHCPMSG {
|
||||
DHCPMSGTYPE msgType;
|
||||
byte[] caddr;
|
||||
byte[] yaddr;
|
||||
byte[] chaddr;
|
||||
byte[] requestedIP;
|
||||
|
||||
if (msg.msgType == DHCPMSGTYPE.DHCPREQUEST) {
|
||||
getByteArray(DHCPPACKET.CHADDR.getValue(), msg.chaddr);
|
||||
getByteArray(DHCPPACKET.CIADDR.getValue(), msg.caddr);
|
||||
} else if (msg.msgType == DHCPMSGTYPE.DHCPACK) {
|
||||
getByteArray(DHCPPACKET.YIADDR.getValue(), msg.yaddr);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
public DHCPMSG() {
|
||||
caddr = new byte[4];
|
||||
yaddr = new byte[4];
|
||||
chaddr = new byte[6];
|
||||
}
|
||||
}
|
||||
|
||||
private String formatMacAddress(byte[] mac) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
Formatter formatter = new Formatter(sb);
|
||||
for (int i = 0; i < mac.length; i++) {
|
||||
formatter.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : "");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
private PcapPacket _buffer;
|
||||
private int _offset;
|
||||
private int _len;
|
||||
private DhcpSnooperImpl _manager;
|
||||
|
||||
private String getDestMacAddress() {
|
||||
Ethernet ether = new Ethernet();
|
||||
if (_buffer.hasHeader(ether)) {
|
||||
byte[] destMac = ether.destination();
|
||||
return formatMacAddress(destMac);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public DhcpPacketParser(PcapPacket buffer, int offset, int len,
|
||||
DhcpSnooperImpl manager) {
|
||||
_buffer = buffer;
|
||||
_offset = offset;
|
||||
_len = len;
|
||||
_manager = manager;
|
||||
}
|
||||
|
||||
private InetAddress getDHCPServerIP() {
|
||||
Ip4 ip = new Ip4();
|
||||
if (_buffer.hasHeader(ip)) {
|
||||
try {
|
||||
return InetAddress.getByAddress(ip.source());
|
||||
} catch (UnknownHostException e) {
|
||||
s_logger.debug("Failed to get dhcp server ip address: " + e.toString());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private int getPos(int pos) {
|
||||
return _offset + pos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
DHCPMSG msg = getDhcpMsg();
|
||||
private byte getByte(int offset) {
|
||||
return _buffer.getByte(getPos(offset));
|
||||
}
|
||||
|
||||
if (msg == null) {
|
||||
return;
|
||||
}
|
||||
private void getByteArray(int offset, byte[] array) {
|
||||
_buffer.getByteArray(getPos(offset), array);
|
||||
}
|
||||
|
||||
if (msg.msgType == DHCPMSGTYPE.DHCPACK) {
|
||||
InetAddress ip = null;
|
||||
try {
|
||||
ip = InetAddress.getByAddress(msg.yaddr);
|
||||
String macAddr = getDestMacAddress();
|
||||
_manager.setIPAddr(macAddr, ip, DHCPState.DHCPACKED, getDHCPServerIP());
|
||||
} catch (UnknownHostException e) {
|
||||
private long getUInt(int offset) {
|
||||
return _buffer.getUInt(getPos(offset));
|
||||
}
|
||||
|
||||
}
|
||||
} else if (msg.msgType == DHCPMSGTYPE.DHCPREQUEST) {
|
||||
InetAddress ip = null;
|
||||
if (msg.requestedIP != null) {
|
||||
try {
|
||||
ip = InetAddress.getByAddress(msg.requestedIP);
|
||||
} catch (UnknownHostException e) {
|
||||
}
|
||||
}
|
||||
if (ip == null) {
|
||||
try {
|
||||
ip = InetAddress.getByAddress(msg.caddr);
|
||||
} catch (UnknownHostException e) {
|
||||
}
|
||||
}
|
||||
private DHCPMSG getDhcpMsg() {
|
||||
long magic = getUInt(DHCPPACKET.MAGIC.getValue());
|
||||
if (magic != 0x63538263) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (ip != null) {
|
||||
String macAddr = formatMacAddress(msg.chaddr);
|
||||
_manager.setIPAddr(macAddr, ip, DHCPState.DHCPREQUESTED, null);
|
||||
}
|
||||
}
|
||||
DHCPMSG msg = new DHCPMSG();
|
||||
|
||||
}
|
||||
int pos = DHCPPACKET.OPTIONS.getValue();
|
||||
while (pos <= _len) {
|
||||
int type = (int) getByte(pos++) & 0xff;
|
||||
|
||||
private void test() {
|
||||
JPacket packet = new JMemoryPacket(Ethernet.ID,
|
||||
" 06fa 8800 00b3 0656 d200 0027 8100 001a 0800 4500 0156 64bf 0000 4011 f3f2 ac1a 6412 ac1a 649e 0043 0044 0001 0000 0001");
|
||||
Ethernet eth = new Ethernet();
|
||||
if (packet.hasHeader(eth)) {
|
||||
System.out.print(" ether:" + eth);
|
||||
}
|
||||
IEEE802dot1q vlan = new IEEE802dot1q();
|
||||
if (packet.hasHeader(vlan)) {
|
||||
System.out.print(" vlan: " + vlan);
|
||||
}
|
||||
if (type == DHCPOPTIONTYPE.END.getValue()) {
|
||||
break;
|
||||
}
|
||||
if (type == DHCPOPTIONTYPE.PAD.getValue()) {
|
||||
continue;
|
||||
}
|
||||
int len = 0;
|
||||
if (pos <= _len) {
|
||||
len = ((int) getByte(pos++)) & 0xff;
|
||||
}
|
||||
|
||||
if (packet.hasHeader(Udp.ID)) {
|
||||
System.out.print("has udp");
|
||||
}
|
||||
}
|
||||
if (type == DHCPOPTIONTYPE.MESSAGETYPE.getValue()
|
||||
|| type == DHCPOPTIONTYPE.REQUESTEDIP.getValue()) {
|
||||
/* Read data only if needed */
|
||||
byte[] data = null;
|
||||
if ((len + pos) <= _len) {
|
||||
data = new byte[len];
|
||||
getByteArray(pos, data);
|
||||
}
|
||||
|
||||
if (type == DHCPOPTIONTYPE.MESSAGETYPE.getValue()) {
|
||||
msg.msgType = DHCPMSGTYPE.valueOf((int) data[0]);
|
||||
} else if (type == DHCPOPTIONTYPE.REQUESTEDIP.getValue()) {
|
||||
msg.requestedIP = data;
|
||||
}
|
||||
}
|
||||
|
||||
pos += len;
|
||||
}
|
||||
|
||||
if (msg.msgType == DHCPMSGTYPE.DHCPREQUEST) {
|
||||
getByteArray(DHCPPACKET.CHADDR.getValue(), msg.chaddr);
|
||||
getByteArray(DHCPPACKET.CIADDR.getValue(), msg.caddr);
|
||||
} else if (msg.msgType == DHCPMSGTYPE.DHCPACK) {
|
||||
getByteArray(DHCPPACKET.YIADDR.getValue(), msg.yaddr);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
private String formatMacAddress(byte[] mac) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
Formatter formatter = new Formatter(sb);
|
||||
for (int i = 0; i < mac.length; i++) {
|
||||
formatter.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : "");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private String getDestMacAddress() {
|
||||
Ethernet ether = new Ethernet();
|
||||
if (_buffer.hasHeader(ether)) {
|
||||
byte[] destMac = ether.destination();
|
||||
return formatMacAddress(destMac);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private InetAddress getDHCPServerIP() {
|
||||
Ip4 ip = new Ip4();
|
||||
if (_buffer.hasHeader(ip)) {
|
||||
try {
|
||||
return InetAddress.getByAddress(ip.source());
|
||||
} catch (UnknownHostException e) {
|
||||
s_logger.debug("Failed to get dhcp server ip address: "
|
||||
+ e.toString());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
DHCPMSG msg = getDhcpMsg();
|
||||
|
||||
if (msg == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.msgType == DHCPMSGTYPE.DHCPACK) {
|
||||
InetAddress ip = null;
|
||||
try {
|
||||
ip = InetAddress.getByAddress(msg.yaddr);
|
||||
String macAddr = getDestMacAddress();
|
||||
_manager.setIPAddr(macAddr, ip, DHCPState.DHCPACKED,
|
||||
getDHCPServerIP());
|
||||
} catch (UnknownHostException e) {
|
||||
|
||||
}
|
||||
} else if (msg.msgType == DHCPMSGTYPE.DHCPREQUEST) {
|
||||
InetAddress ip = null;
|
||||
if (msg.requestedIP != null) {
|
||||
try {
|
||||
ip = InetAddress.getByAddress(msg.requestedIP);
|
||||
} catch (UnknownHostException e) {
|
||||
}
|
||||
}
|
||||
if (ip == null) {
|
||||
try {
|
||||
ip = InetAddress.getByAddress(msg.caddr);
|
||||
} catch (UnknownHostException e) {
|
||||
}
|
||||
}
|
||||
|
||||
if (ip != null) {
|
||||
String macAddr = formatMacAddress(msg.chaddr);
|
||||
_manager.setIPAddr(macAddr, ip, DHCPState.DHCPREQUESTED, null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void test() {
|
||||
JPacket packet = new JMemoryPacket(
|
||||
Ethernet.ID,
|
||||
" 06fa 8800 00b3 0656 d200 0027 8100 001a 0800 4500 0156 64bf 0000 4011 f3f2 ac1a 6412 ac1a 649e 0043 0044 0001 0000 0001");
|
||||
Ethernet eth = new Ethernet();
|
||||
if (packet.hasHeader(eth)) {
|
||||
System.out.print(" ether:" + eth);
|
||||
}
|
||||
IEEE802dot1q vlan = new IEEE802dot1q();
|
||||
if (packet.hasHeader(vlan)) {
|
||||
System.out.print(" vlan: " + vlan);
|
||||
}
|
||||
|
||||
if (packet.hasHeader(Udp.ID)) {
|
||||
System.out.print("has udp");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,31 +15,36 @@ import org.apache.log4j.Logger;
|
|||
import com.cloud.utils.concurrency.NamedThreadFactory;
|
||||
|
||||
public class DhcpProtocolParserServer extends Thread {
|
||||
private static final Logger s_logger = Logger.getLogger(DhcpProtocolParserServer.class);;
|
||||
protected ExecutorService _executor;
|
||||
private int dhcpServerPort = 67;
|
||||
private int bufferSize = 300;
|
||||
protected boolean _running = false;
|
||||
public DhcpProtocolParserServer(int workers) {
|
||||
_executor = new ThreadPoolExecutor(workers, 10 * workers, 1, TimeUnit.DAYS, new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory("DhcpListener"));
|
||||
_running = true;
|
||||
}
|
||||
private static final Logger s_logger = Logger
|
||||
.getLogger(DhcpProtocolParserServer.class);;
|
||||
protected ExecutorService _executor;
|
||||
private int dhcpServerPort = 67;
|
||||
private int bufferSize = 300;
|
||||
protected boolean _running = false;
|
||||
|
||||
public void run() {
|
||||
while(_running) {
|
||||
try {
|
||||
DatagramSocket dhcpSocket = new DatagramSocket(dhcpServerPort, InetAddress.getByAddress(new byte[]{0,0,0,0}));
|
||||
dhcpSocket.setBroadcast(true);
|
||||
public DhcpProtocolParserServer(int workers) {
|
||||
_executor = new ThreadPoolExecutor(workers, 10 * workers, 1,
|
||||
TimeUnit.DAYS, new LinkedBlockingQueue<Runnable>(),
|
||||
new NamedThreadFactory("DhcpListener"));
|
||||
_running = true;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
byte[] buf = new byte[bufferSize];
|
||||
DatagramPacket dgp = new DatagramPacket(buf, buf.length);
|
||||
dhcpSocket.receive(dgp);
|
||||
// _executor.execute(new DhcpPacketParser(buf));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
s_logger.debug(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
public void run() {
|
||||
while (_running) {
|
||||
try {
|
||||
DatagramSocket dhcpSocket = new DatagramSocket(dhcpServerPort,
|
||||
InetAddress.getByAddress(new byte[] { 0, 0, 0, 0 }));
|
||||
dhcpSocket.setBroadcast(true);
|
||||
|
||||
while (true) {
|
||||
byte[] buf = new byte[bufferSize];
|
||||
DatagramPacket dgp = new DatagramPacket(buf, buf.length);
|
||||
dhcpSocket.receive(dgp);
|
||||
// _executor.execute(new DhcpPacketParser(buf));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
s_logger.debug(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,18 +24,18 @@ import java.util.Map;
|
|||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.Adapter;
|
||||
|
||||
public interface DhcpSnooper extends Adapter{
|
||||
public interface DhcpSnooper extends Adapter {
|
||||
|
||||
public InetAddress getIPAddr(String macAddr, String vmName);
|
||||
public InetAddress getIPAddr(String macAddr, String vmName);
|
||||
|
||||
public InetAddress getDhcpServerIP();
|
||||
public InetAddress getDhcpServerIP();
|
||||
|
||||
public void cleanup(String macAddr, String vmName);
|
||||
public void cleanup(String macAddr, String vmName);
|
||||
|
||||
public Map<String, InetAddress> syncIpAddr();
|
||||
public Map<String, InetAddress> syncIpAddr();
|
||||
|
||||
public boolean stop();
|
||||
public boolean stop();
|
||||
|
||||
public void initializeMacTable(List<Pair<String, String>> macVmNameList);
|
||||
public void initializeMacTable(List<Pair<String, String>> macVmNameList);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
* * Copyright (C) 2011 Citrix Systems, Inc. All rights reserved
|
||||
*
|
||||
*
|
||||
*
|
||||
* This software is licensed under the GNU General Public License v3 or later.
|
||||
*
|
||||
|
|
@ -32,7 +32,6 @@
|
|||
*/
|
||||
package com.cloud.agent.dhcp;
|
||||
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
|
@ -59,269 +58,283 @@ import org.jnetpcap.protocol.tcpip.Udp;
|
|||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.concurrency.NamedThreadFactory;
|
||||
|
||||
@Local(value = { DhcpSnooper.class})
|
||||
@Local(value = { DhcpSnooper.class })
|
||||
public class DhcpSnooperImpl implements DhcpSnooper {
|
||||
private static final Logger s_logger = Logger.getLogger(DhcpSnooperImpl.class);
|
||||
public enum DHCPState {
|
||||
DHCPACKED,
|
||||
DHCPREQUESTED,
|
||||
DHCPRESET;
|
||||
}
|
||||
public class IPAddr {
|
||||
String _vmName;
|
||||
InetAddress _ip;
|
||||
DHCPState _state;
|
||||
public IPAddr(InetAddress ip, DHCPState state, String vmName) {
|
||||
_ip = ip;
|
||||
_state = state;
|
||||
_vmName = vmName;
|
||||
}
|
||||
}
|
||||
protected ExecutorService _executor;
|
||||
protected Map<String, IPAddr> _macIpMap;
|
||||
protected Map<InetAddress, String> _ipMacMap;
|
||||
private DhcpServer _server;
|
||||
protected long _timeout = 1200000;
|
||||
protected InetAddress _dhcpServerIp;
|
||||
private static final Logger s_logger = Logger
|
||||
.getLogger(DhcpSnooperImpl.class);
|
||||
|
||||
public DhcpSnooperImpl(String bridge, long timeout) {
|
||||
public enum DHCPState {
|
||||
DHCPACKED, DHCPREQUESTED, DHCPRESET;
|
||||
}
|
||||
|
||||
_timeout = timeout;
|
||||
_executor = new ThreadPoolExecutor(10, 10 * 10, 1, TimeUnit.DAYS, new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory("DhcpListener"));
|
||||
_macIpMap = new ConcurrentHashMap<String, IPAddr>();
|
||||
_ipMacMap = new ConcurrentHashMap<InetAddress, String>();
|
||||
_server = new DhcpServer(this, bridge);
|
||||
_server.start();
|
||||
}
|
||||
public class IPAddr {
|
||||
String _vmName;
|
||||
InetAddress _ip;
|
||||
DHCPState _state;
|
||||
|
||||
public IPAddr(InetAddress ip, DHCPState state, String vmName) {
|
||||
_ip = ip;
|
||||
_state = state;
|
||||
_vmName = vmName;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public InetAddress getIPAddr(String macAddr, String vmName) {
|
||||
String macAddrLowerCase = macAddr.toLowerCase();
|
||||
IPAddr addr = _macIpMap.get(macAddrLowerCase);
|
||||
if (addr == null) {
|
||||
addr = new IPAddr(null, DHCPState.DHCPRESET, vmName);
|
||||
_macIpMap.put(macAddrLowerCase, addr);
|
||||
} else {
|
||||
addr._state = DHCPState.DHCPRESET;
|
||||
}
|
||||
protected ExecutorService _executor;
|
||||
protected Map<String, IPAddr> _macIpMap;
|
||||
protected Map<InetAddress, String> _ipMacMap;
|
||||
private DhcpServer _server;
|
||||
protected long _timeout = 1200000;
|
||||
protected InetAddress _dhcpServerIp;
|
||||
|
||||
synchronized(addr) {
|
||||
try {
|
||||
addr.wait(_timeout);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
if (addr._state == DHCPState.DHCPACKED) {
|
||||
addr._state = DHCPState.DHCPRESET;
|
||||
return addr._ip;
|
||||
}
|
||||
}
|
||||
public DhcpSnooperImpl(String bridge, long timeout) {
|
||||
|
||||
return null;
|
||||
}
|
||||
_timeout = timeout;
|
||||
_executor = new ThreadPoolExecutor(10, 10 * 10, 1, TimeUnit.DAYS,
|
||||
new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory(
|
||||
"DhcpListener"));
|
||||
_macIpMap = new ConcurrentHashMap<String, IPAddr>();
|
||||
_ipMacMap = new ConcurrentHashMap<InetAddress, String>();
|
||||
_server = new DhcpServer(this, bridge);
|
||||
_server.start();
|
||||
}
|
||||
|
||||
public InetAddress getDhcpServerIP() {
|
||||
return _dhcpServerIp;
|
||||
}
|
||||
@Override
|
||||
public InetAddress getIPAddr(String macAddr, String vmName) {
|
||||
String macAddrLowerCase = macAddr.toLowerCase();
|
||||
IPAddr addr = _macIpMap.get(macAddrLowerCase);
|
||||
if (addr == null) {
|
||||
addr = new IPAddr(null, DHCPState.DHCPRESET, vmName);
|
||||
_macIpMap.put(macAddrLowerCase, addr);
|
||||
} else {
|
||||
addr._state = DHCPState.DHCPRESET;
|
||||
}
|
||||
|
||||
synchronized (addr) {
|
||||
try {
|
||||
addr.wait(_timeout);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
if (addr._state == DHCPState.DHCPACKED) {
|
||||
addr._state = DHCPState.DHCPRESET;
|
||||
return addr._ip;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanup(String macAddr, String vmName) {
|
||||
try {
|
||||
if (macAddr == null) {
|
||||
return;
|
||||
}
|
||||
_macIpMap.remove(macAddr);
|
||||
_ipMacMap.values().remove(macAddr);
|
||||
} catch (Exception e) {
|
||||
s_logger.debug("Failed to cleanup: " + e.toString());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public InetAddress getDhcpServerIP() {
|
||||
return _dhcpServerIp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, InetAddress> syncIpAddr() {
|
||||
Collection<IPAddr> ips = _macIpMap.values();
|
||||
HashMap<String, InetAddress> vmIpMap = new HashMap<String, InetAddress>();
|
||||
for(IPAddr ip : ips) {
|
||||
if (ip._state == DHCPState.DHCPACKED) {
|
||||
vmIpMap.put(ip._vmName, ip._ip);
|
||||
}
|
||||
}
|
||||
return vmIpMap;
|
||||
}
|
||||
@Override
|
||||
public void cleanup(String macAddr, String vmName) {
|
||||
try {
|
||||
if (macAddr == null) {
|
||||
return;
|
||||
}
|
||||
_macIpMap.remove(macAddr);
|
||||
_ipMacMap.values().remove(macAddr);
|
||||
} catch (Exception e) {
|
||||
s_logger.debug("Failed to cleanup: " + e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initializeMacTable(List<Pair<String, String>> macVmNameList) {
|
||||
for (Pair<String, String> macVmname : macVmNameList) {
|
||||
IPAddr ipAdrr = new IPAddr(null, DHCPState.DHCPRESET, macVmname.second());
|
||||
_macIpMap.put(macVmname.first(), ipAdrr);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public Map<String, InetAddress> syncIpAddr() {
|
||||
Collection<IPAddr> ips = _macIpMap.values();
|
||||
HashMap<String, InetAddress> vmIpMap = new HashMap<String, InetAddress>();
|
||||
for (IPAddr ip : ips) {
|
||||
if (ip._state == DHCPState.DHCPACKED) {
|
||||
vmIpMap.put(ip._vmName, ip._ip);
|
||||
}
|
||||
}
|
||||
return vmIpMap;
|
||||
}
|
||||
|
||||
protected void setIPAddr(String macAddr, InetAddress ip, DHCPState state, InetAddress dhcpServerIp) {
|
||||
String macAddrLowerCase = macAddr.toLowerCase();
|
||||
if (state == DHCPState.DHCPREQUESTED) {
|
||||
IPAddr ipAddr = _macIpMap.get(macAddrLowerCase);
|
||||
if (ipAddr == null) {
|
||||
return;
|
||||
}
|
||||
@Override
|
||||
public void initializeMacTable(List<Pair<String, String>> macVmNameList) {
|
||||
for (Pair<String, String> macVmname : macVmNameList) {
|
||||
IPAddr ipAdrr = new IPAddr(null, DHCPState.DHCPRESET,
|
||||
macVmname.second());
|
||||
_macIpMap.put(macVmname.first(), ipAdrr);
|
||||
}
|
||||
}
|
||||
|
||||
_ipMacMap.put(ip, macAddr);
|
||||
} else if (state == DHCPState.DHCPACKED) {
|
||||
_dhcpServerIp = dhcpServerIp;
|
||||
String destMac = macAddrLowerCase;
|
||||
if (macAddrLowerCase.equalsIgnoreCase("ff:ff:ff:ff:ff:ff")) {
|
||||
destMac = _ipMacMap.get(ip);
|
||||
if (destMac == null) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
protected void setIPAddr(String macAddr, InetAddress ip, DHCPState state,
|
||||
InetAddress dhcpServerIp) {
|
||||
String macAddrLowerCase = macAddr.toLowerCase();
|
||||
if (state == DHCPState.DHCPREQUESTED) {
|
||||
IPAddr ipAddr = _macIpMap.get(macAddrLowerCase);
|
||||
if (ipAddr == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
IPAddr addr = _macIpMap.get(destMac);
|
||||
if (addr != null) {
|
||||
addr._ip = ip;
|
||||
addr._state = state;
|
||||
synchronized (addr) {
|
||||
addr.notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ipMacMap.put(ip, macAddr);
|
||||
} else if (state == DHCPState.DHCPACKED) {
|
||||
_dhcpServerIp = dhcpServerIp;
|
||||
String destMac = macAddrLowerCase;
|
||||
if (macAddrLowerCase.equalsIgnoreCase("ff:ff:ff:ff:ff:ff")) {
|
||||
destMac = _ipMacMap.get(ip);
|
||||
if (destMac == null) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see com.cloud.agent.dhcp.DhcpSnooper#stop()
|
||||
*/
|
||||
@Override
|
||||
public boolean stop() {
|
||||
_executor.shutdown();
|
||||
_server.StopServer();
|
||||
return true;
|
||||
}
|
||||
IPAddr addr = _macIpMap.get(destMac);
|
||||
if (addr != null) {
|
||||
addr._ip = ip;
|
||||
addr._state = state;
|
||||
synchronized (addr) {
|
||||
addr.notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class DhcpServer extends Thread {
|
||||
private DhcpSnooperImpl _manager;
|
||||
private String _bridge;
|
||||
private Pcap _pcapedDev;
|
||||
private boolean _loop;
|
||||
public DhcpServer(DhcpSnooperImpl mgt, String bridge) {
|
||||
_manager = mgt;
|
||||
_bridge = bridge;
|
||||
_loop = true;
|
||||
}
|
||||
public void StopServer() {
|
||||
_loop = false;
|
||||
_pcapedDev.breakloop();
|
||||
_pcapedDev.close();
|
||||
}
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see com.cloud.agent.dhcp.DhcpSnooper#stop()
|
||||
*/
|
||||
@Override
|
||||
public boolean stop() {
|
||||
_executor.shutdown();
|
||||
_server.StopServer();
|
||||
return true;
|
||||
}
|
||||
|
||||
private Pcap initializePcap() {
|
||||
try {
|
||||
List<PcapIf> alldevs = new ArrayList<PcapIf>();
|
||||
StringBuilder errBuf = new StringBuilder();
|
||||
int r = Pcap.findAllDevs(alldevs, errBuf);
|
||||
if (r == Pcap.NOT_OK || alldevs.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
private class DhcpServer extends Thread {
|
||||
private DhcpSnooperImpl _manager;
|
||||
private String _bridge;
|
||||
private Pcap _pcapedDev;
|
||||
private boolean _loop;
|
||||
|
||||
PcapIf dev = null;
|
||||
for (PcapIf device : alldevs) {
|
||||
if (device.getName().equalsIgnoreCase(_bridge)) {
|
||||
dev = device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
public DhcpServer(DhcpSnooperImpl mgt, String bridge) {
|
||||
_manager = mgt;
|
||||
_bridge = bridge;
|
||||
_loop = true;
|
||||
}
|
||||
|
||||
if (dev == null) {
|
||||
s_logger.debug("Pcap: Can't find device: " + _bridge + " to listen on");
|
||||
return null;
|
||||
}
|
||||
public void StopServer() {
|
||||
_loop = false;
|
||||
_pcapedDev.breakloop();
|
||||
_pcapedDev.close();
|
||||
}
|
||||
|
||||
int snaplen = 64*1024;
|
||||
int flags = Pcap.MODE_PROMISCUOUS;
|
||||
int timeout = 10 * 1000;
|
||||
Pcap pcap = Pcap.openLive(dev.getName(), snaplen, flags, timeout, errBuf);
|
||||
if (pcap == null) {
|
||||
s_logger.debug("Pcap: Can't open " + _bridge);
|
||||
return null;
|
||||
}
|
||||
private Pcap initializePcap() {
|
||||
try {
|
||||
List<PcapIf> alldevs = new ArrayList<PcapIf>();
|
||||
StringBuilder errBuf = new StringBuilder();
|
||||
int r = Pcap.findAllDevs(alldevs, errBuf);
|
||||
if (r == Pcap.NOT_OK || alldevs.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
PcapBpfProgram program = new PcapBpfProgram();
|
||||
String expr = "dst port 68 or 67";
|
||||
int optimize = 0;
|
||||
int netmask = 0xFFFFFF00;
|
||||
if (pcap.compile(program, expr, optimize, netmask) != Pcap.OK) {
|
||||
s_logger.debug("Pcap: can't compile BPF");
|
||||
return null;
|
||||
}
|
||||
PcapIf dev = null;
|
||||
for (PcapIf device : alldevs) {
|
||||
if (device.getName().equalsIgnoreCase(_bridge)) {
|
||||
dev = device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(pcap.setFilter(program) != Pcap.OK) {
|
||||
s_logger.debug("Pcap: Can't set filter");
|
||||
return null;
|
||||
}
|
||||
return pcap;
|
||||
} catch (Exception e) {
|
||||
s_logger.debug("Failed to initialized: " + e.toString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (dev == null) {
|
||||
s_logger.debug("Pcap: Can't find device: " + _bridge
|
||||
+ " to listen on");
|
||||
return null;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
while (_loop) {
|
||||
try {
|
||||
_pcapedDev = initializePcap();
|
||||
if (_pcapedDev == null) {
|
||||
return;
|
||||
}
|
||||
int snaplen = 64 * 1024;
|
||||
int flags = Pcap.MODE_PROMISCUOUS;
|
||||
int timeout = 10 * 1000;
|
||||
Pcap pcap = Pcap.openLive(dev.getName(), snaplen, flags,
|
||||
timeout, errBuf);
|
||||
if (pcap == null) {
|
||||
s_logger.debug("Pcap: Can't open " + _bridge);
|
||||
return null;
|
||||
}
|
||||
|
||||
PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {
|
||||
public void nextPacket(PcapPacket packet, String user) {
|
||||
Udp u = new Udp();
|
||||
if (packet.hasHeader(u)) {
|
||||
int offset = u.getOffset() + u.getLength();
|
||||
_executor.execute(new DhcpPacketParser(packet, offset, u.length() - u.getLength(), _manager));
|
||||
}
|
||||
}
|
||||
};
|
||||
s_logger.debug("Starting DHCP snooping on " + _bridge);
|
||||
int retValue = _pcapedDev.loop(-1, jpacketHandler, "pcapPacketHandler");
|
||||
if ( retValue == -1) {
|
||||
s_logger.debug("Pcap: failed to set loop handler");
|
||||
} else if ( retValue == -2 && !_loop) {
|
||||
s_logger.debug("Pcap: terminated");
|
||||
return;
|
||||
}
|
||||
_pcapedDev.close();
|
||||
} catch (Exception e) {
|
||||
s_logger.debug("Pcap error:" + e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
PcapBpfProgram program = new PcapBpfProgram();
|
||||
String expr = "dst port 68 or 67";
|
||||
int optimize = 0;
|
||||
int netmask = 0xFFFFFF00;
|
||||
if (pcap.compile(program, expr, optimize, netmask) != Pcap.OK) {
|
||||
s_logger.debug("Pcap: can't compile BPF");
|
||||
return null;
|
||||
}
|
||||
|
||||
static public void main(String args[]) {
|
||||
s_logger.addAppender(new org.apache.log4j.ConsoleAppender(new org.apache.log4j.PatternLayout(), "System.out"));
|
||||
final DhcpSnooperImpl manager = new DhcpSnooperImpl("cloudbr0", 10000);
|
||||
s_logger.debug(manager.getIPAddr("02:00:4c:66:00:03", "i-2-5-VM"));
|
||||
manager.stop();
|
||||
if (pcap.setFilter(program) != Pcap.OK) {
|
||||
s_logger.debug("Pcap: Can't set filter");
|
||||
return null;
|
||||
}
|
||||
return pcap;
|
||||
} catch (Exception e) {
|
||||
s_logger.debug("Failed to initialized: " + e.toString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
public void run() {
|
||||
while (_loop) {
|
||||
try {
|
||||
_pcapedDev = initializePcap();
|
||||
if (_pcapedDev == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean configure(String name, Map<String, Object> params)
|
||||
throws ConfigurationException {
|
||||
// TODO configure timeout here
|
||||
return true;
|
||||
}
|
||||
PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {
|
||||
public void nextPacket(PcapPacket packet, String user) {
|
||||
Udp u = new Udp();
|
||||
if (packet.hasHeader(u)) {
|
||||
int offset = u.getOffset() + u.getLength();
|
||||
_executor.execute(new DhcpPacketParser(packet,
|
||||
offset, u.length() - u.getLength(),
|
||||
_manager));
|
||||
}
|
||||
}
|
||||
};
|
||||
s_logger.debug("Starting DHCP snooping on " + _bridge);
|
||||
int retValue = _pcapedDev.loop(-1, jpacketHandler,
|
||||
"pcapPacketHandler");
|
||||
if (retValue == -1) {
|
||||
s_logger.debug("Pcap: failed to set loop handler");
|
||||
} else if (retValue == -2 && !_loop) {
|
||||
s_logger.debug("Pcap: terminated");
|
||||
return;
|
||||
}
|
||||
_pcapedDev.close();
|
||||
} catch (Exception e) {
|
||||
s_logger.debug("Pcap error:" + e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean start() {
|
||||
return true;
|
||||
}
|
||||
static public void main(String args[]) {
|
||||
s_logger.addAppender(new org.apache.log4j.ConsoleAppender(
|
||||
new org.apache.log4j.PatternLayout(), "System.out"));
|
||||
final DhcpSnooperImpl manager = new DhcpSnooperImpl("cloudbr0", 10000);
|
||||
s_logger.debug(manager.getIPAddr("02:00:4c:66:00:03", "i-2-5-VM"));
|
||||
manager.stop();
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "DhcpSnooperImpl";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean configure(String name, Map<String, Object> params)
|
||||
throws ConfigurationException {
|
||||
// TODO configure timeout here
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean start() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "DhcpSnooperImpl";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,104 +21,106 @@ import com.cloud.agent.api.Command;
|
|||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.net.NetUtils;
|
||||
|
||||
@Local(value = {DhcpSnooper.class})
|
||||
@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>();
|
||||
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 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 boolean start() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "FakeDhcpSnooper";
|
||||
}
|
||||
@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 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);
|
||||
@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());
|
||||
}
|
||||
}
|
||||
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 Map<String, InetAddress> syncIpAddr() {
|
||||
return _vmIpMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean stop() {
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
public boolean stop() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initializeMacTable(List<Pair<String, String>> macVmNameList) {
|
||||
@Override
|
||||
public void initializeMacTable(List<Pair<String, String>> macVmNameList) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public InetAddress getDhcpServerIP() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public InetAddress getDhcpServerIP() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,67 +16,69 @@
|
|||
*
|
||||
*/
|
||||
|
||||
package com.cloud.agent.mockvm;
|
||||
|
||||
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;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
long i = 10;
|
||||
Long l = null;
|
||||
if (i == l) {
|
||||
System.out.print("fdfd");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,241 +16,245 @@
|
|||
*
|
||||
*/
|
||||
|
||||
package com.cloud.agent.mockvm;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
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 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>();
|
||||
|
||||
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() {
|
||||
private Map<String, Object> _params = null;
|
||||
|
||||
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);
|
||||
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) {
|
||||
|
|
@ -259,21 +263,23 @@ public class MockVmMgr implements VmMgr {
|
|||
int utilizationPercent = randSeed.nextInt() % 100;
|
||||
MockVm vm = null;
|
||||
|
||||
synchronized(this) {
|
||||
synchronized (this) {
|
||||
vm = vms.get(vmName);
|
||||
if(vm == null) {
|
||||
if (ramSize > getHostFreeMemory()) {
|
||||
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){
|
||||
if (vncPort < 0) {
|
||||
s_logger.debug("Unable to allocate VNC port");
|
||||
throw new CloudRuntimeException("Unable to allocate vnc port");
|
||||
throw new CloudRuntimeException(
|
||||
"Unable to allocate vnc port");
|
||||
}
|
||||
|
||||
vm = new MockVm(vmName, State.Running, ramSize, vmSpec.getCpus(), utilizationPercent, vncPort);
|
||||
vm = new MockVm(vmName, State.Running, ramSize,
|
||||
vmSpec.getCpus(), utilizationPercent, vncPort);
|
||||
vms.put(vmName, vm);
|
||||
}
|
||||
}
|
||||
|
|
@ -292,28 +298,28 @@ public class MockVmMgr implements VmMgr {
|
|||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(Map<String, Object> params) {
|
||||
_params = params;
|
||||
}
|
||||
@Override
|
||||
public void configure(Map<String, Object> params) {
|
||||
_params = params;
|
||||
}
|
||||
|
||||
protected Long getConfiguredProperty(String key, Long defaultValue) {
|
||||
String val = (String)_params.get(key);
|
||||
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;
|
||||
}
|
||||
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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
if (val != null) {
|
||||
Integer result = Integer.parseInt(val);
|
||||
return result;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,42 +16,51 @@
|
|||
*
|
||||
*/
|
||||
|
||||
package com.cloud.agent.mockvm;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
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 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);
|
||||
|
|
@ -60,5 +69,5 @@ public interface VmMgr {
|
|||
|
||||
public void createVif(VirtualMachineTO vmSpec, String vmName, MockVm vm);
|
||||
|
||||
public void configure(Map<String, Object> params);
|
||||
}
|
||||
public void configure(Map<String, Object> params);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,171 +44,185 @@ import com.cloud.resource.ServerResource;
|
|||
import com.cloud.storage.Storage;
|
||||
import com.cloud.storage.Storage.StoragePoolType;
|
||||
|
||||
@Local(value={ServerResource.class})
|
||||
@Local(value = { ServerResource.class })
|
||||
public class DummyResource implements ServerResource {
|
||||
String _name;
|
||||
Host.Type _type;
|
||||
boolean _negative;
|
||||
IAgentControl _agentControl;
|
||||
private Map<String, Object> _params;
|
||||
String _name;
|
||||
Host.Type _type;
|
||||
boolean _negative;
|
||||
IAgentControl _agentControl;
|
||||
private Map<String, Object> _params;
|
||||
|
||||
@Override
|
||||
public void disconnected() {
|
||||
}
|
||||
@Override
|
||||
public void disconnected() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Answer executeRequest(Command cmd) {
|
||||
if (cmd instanceof CheckNetworkCommand) {
|
||||
return new CheckNetworkAnswer((CheckNetworkCommand)cmd, true, null);
|
||||
}
|
||||
System.out.println("Received Command: " + cmd.toString());
|
||||
Answer answer = new Answer(cmd, !_negative, "response");
|
||||
System.out.println("Replying with: " + answer.toString());
|
||||
return answer;
|
||||
}
|
||||
@Override
|
||||
public Answer executeRequest(Command cmd) {
|
||||
if (cmd instanceof CheckNetworkCommand) {
|
||||
return new CheckNetworkAnswer((CheckNetworkCommand) cmd, true, null);
|
||||
}
|
||||
System.out.println("Received Command: " + cmd.toString());
|
||||
Answer answer = new Answer(cmd, !_negative, "response");
|
||||
System.out.println("Replying with: " + answer.toString());
|
||||
return answer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PingCommand getCurrentStatus(long id) {
|
||||
return new PingCommand(_type, id);
|
||||
}
|
||||
@Override
|
||||
public PingCommand getCurrentStatus(long id) {
|
||||
return new PingCommand(_type, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getType() {
|
||||
return _type;
|
||||
}
|
||||
@Override
|
||||
public Type getType() {
|
||||
return _type;
|
||||
}
|
||||
|
||||
protected String getConfiguredProperty(String key, String defaultValue) {
|
||||
String val = (String)_params.get(key);
|
||||
return val==null?defaultValue:val;
|
||||
}
|
||||
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);
|
||||
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;
|
||||
}
|
||||
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);
|
||||
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;
|
||||
|
||||
String cap = getConfiguredProperty("capabilities", "hvm");
|
||||
info.add((int)cpus);
|
||||
info.add(speed);
|
||||
info.add(ram);
|
||||
info.add(cap);
|
||||
info.add(dom0ram);
|
||||
return info;
|
||||
}
|
||||
|
||||
}
|
||||
protected void fillNetworkInformation(final StartupCommand cmd) {
|
||||
|
||||
protected void fillNetworkInformation(final StartupCommand cmd) {
|
||||
cmd.setPrivateIpAddress((String) getConfiguredProperty(
|
||||
"private.ip.address", "127.0.0.1"));
|
||||
cmd.setPrivateMacAddress((String) getConfiguredProperty(
|
||||
"private.mac.address", "8A:D2:54:3F:7C:C3"));
|
||||
cmd.setPrivateNetmask((String) getConfiguredProperty(
|
||||
"private.ip.netmask", "255.255.255.0"));
|
||||
|
||||
cmd.setPrivateIpAddress((String)getConfiguredProperty("private.ip.address", "127.0.0.1"));
|
||||
cmd.setPrivateMacAddress((String)getConfiguredProperty("private.mac.address", "8A:D2:54:3F:7C:C3"));
|
||||
cmd.setPrivateNetmask((String)getConfiguredProperty("private.ip.netmask", "255.255.255.0"));
|
||||
cmd.setStorageIpAddress((String) getConfiguredProperty(
|
||||
"private.ip.address", "127.0.0.1"));
|
||||
cmd.setStorageMacAddress((String) getConfiguredProperty(
|
||||
"private.mac.address", "8A:D2:54:3F:7C:C3"));
|
||||
cmd.setStorageNetmask((String) getConfiguredProperty(
|
||||
"private.ip.netmask", "255.255.255.0"));
|
||||
cmd.setGatewayIpAddress((String) getConfiguredProperty(
|
||||
"gateway.ip.address", "127.0.0.1"));
|
||||
|
||||
cmd.setStorageIpAddress((String)getConfiguredProperty("private.ip.address", "127.0.0.1"));
|
||||
cmd.setStorageMacAddress((String)getConfiguredProperty("private.mac.address", "8A:D2:54:3F:7C:C3"));
|
||||
cmd.setStorageNetmask((String)getConfiguredProperty("private.ip.netmask", "255.255.255.0"));
|
||||
cmd.setGatewayIpAddress((String)getConfiguredProperty("gateway.ip.address", "127.0.0.1"));
|
||||
}
|
||||
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
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 StoragePoolInfo initializeLocalStorage() {
|
||||
String hostIp = (String) getConfiguredProperty("private.ip.address",
|
||||
"127.0.0.1");
|
||||
String localStoragePath = (String) getConfiguredProperty(
|
||||
"local.storage.path", "/mnt");
|
||||
String lh = hostIp + localStoragePath;
|
||||
String uuid = UUID.nameUUIDFromBytes(lh.getBytes()).toString();
|
||||
|
||||
protected StoragePoolInfo initializeLocalStorage() {
|
||||
String hostIp = (String)getConfiguredProperty("private.ip.address", "127.0.0.1");
|
||||
String localStoragePath = (String)getConfiguredProperty("local.storage.path", "/mnt");
|
||||
String lh = hostIp + localStoragePath;
|
||||
String uuid = UUID.nameUUIDFromBytes(lh.getBytes()).toString();
|
||||
String capacity = (String) getConfiguredProperty(
|
||||
"local.storage.capacity", "1000000000");
|
||||
String available = (String) getConfiguredProperty(
|
||||
"local.storage.avail", "10000000");
|
||||
|
||||
String capacity = (String)getConfiguredProperty("local.storage.capacity", "1000000000");
|
||||
String available = (String)getConfiguredProperty("local.storage.avail", "10000000");
|
||||
return new StoragePoolInfo(uuid, hostIp, localStoragePath,
|
||||
localStoragePath, StoragePoolType.Filesystem,
|
||||
Long.parseLong(capacity), Long.parseLong(available));
|
||||
|
||||
return new StoragePoolInfo(uuid, hostIp, localStoragePath,
|
||||
localStoragePath, StoragePoolType.Filesystem,
|
||||
Long.parseLong(capacity), Long.parseLong(available));
|
||||
}
|
||||
|
||||
}
|
||||
@Override
|
||||
public StartupCommand[] initialize() {
|
||||
Map<String, VmState> changes = null;
|
||||
|
||||
@Override
|
||||
public StartupCommand[] initialize() {
|
||||
Map<String, VmState> 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);
|
||||
|
||||
final List<Object> info = getHostInfo();
|
||||
return new StartupCommand[] { cmd, sscmd };
|
||||
}
|
||||
|
||||
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);
|
||||
@Override
|
||||
public boolean configure(String name, Map<String, Object> params) {
|
||||
_name = name;
|
||||
|
||||
return new StartupCommand[]{cmd, sscmd};
|
||||
}
|
||||
String value = (String) params.get("type");
|
||||
_type = Host.Type.valueOf(value);
|
||||
|
||||
@Override
|
||||
public boolean configure(String name, Map<String, Object> params) {
|
||||
_name = name;
|
||||
value = (String) params.get("negative.reply");
|
||||
_negative = Boolean.parseBoolean(value);
|
||||
setParams(params);
|
||||
return true;
|
||||
}
|
||||
|
||||
String value = (String)params.get("type");
|
||||
_type = Host.Type.valueOf(value);
|
||||
public void setParams(Map<String, Object> _params) {
|
||||
this._params = _params;
|
||||
}
|
||||
|
||||
value = (String)params.get("negative.reply");
|
||||
_negative = Boolean.parseBoolean(value);
|
||||
setParams(params);
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public String getName() {
|
||||
return _name;
|
||||
}
|
||||
|
||||
public void setParams(Map<String, Object> _params) {
|
||||
this._params = _params;
|
||||
}
|
||||
@Override
|
||||
public boolean start() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return _name;
|
||||
}
|
||||
@Override
|
||||
public boolean stop() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean start() {
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public IAgentControl getAgentControl() {
|
||||
return _agentControl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean stop() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAgentControl getAgentControl() {
|
||||
return _agentControl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAgentControl(IAgentControl agentControl) {
|
||||
_agentControl = agentControl;
|
||||
}
|
||||
@Override
|
||||
public void setAgentControl(IAgentControl agentControl) {
|
||||
_agentControl = agentControl;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@
|
|||
*/
|
||||
package com.cloud.agent.resource.consoleproxy;
|
||||
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.FileWriter;
|
||||
|
|
@ -63,269 +62,309 @@ import com.cloud.utils.net.NetUtils;
|
|||
import com.cloud.utils.script.Script;
|
||||
|
||||
/**
|
||||
*
|
||||
* I don't want to introduce extra cross-cutting concerns into console proxy process, as it involves configurations like
|
||||
* zone/pod, agent auto self-upgrade etc. I also don't want to introduce more module dependency issues into our build system,
|
||||
* cross-communication between this resource and console proxy will be done through reflection. As a result, come out with
|
||||
* following solution to solve the problem of building a communication channel between consoole proxy and management server.
|
||||
*
|
||||
* We will deploy an agent shell inside console proxy VM, and this agent shell will launch current console proxy from within
|
||||
* this special server resource, through it console proxy can build a communication channel with management server.
|
||||
*
|
||||
*
|
||||
* I don't want to introduce extra cross-cutting concerns into console proxy
|
||||
* process, as it involves configurations like zone/pod, agent auto self-upgrade
|
||||
* etc. I also don't want to introduce more module dependency issues into our
|
||||
* build system, cross-communication between this resource and console proxy
|
||||
* will be done through reflection. As a result, come out with following
|
||||
* solution to solve the problem of building a communication channel between
|
||||
* consoole proxy and management server.
|
||||
*
|
||||
* We will deploy an agent shell inside console proxy VM, and this agent shell
|
||||
* will launch current console proxy from within this special server resource,
|
||||
* through it console proxy can build a communication channel with management
|
||||
* server.
|
||||
*
|
||||
*/
|
||||
public class ConsoleProxyResource extends ServerResourceBase implements ServerResource {
|
||||
static final Logger s_logger = Logger.getLogger(ConsoleProxyResource.class);
|
||||
public class ConsoleProxyResource extends ServerResourceBase implements
|
||||
ServerResource {
|
||||
static final Logger s_logger = Logger.getLogger(ConsoleProxyResource.class);
|
||||
|
||||
private final Properties _properties = new Properties();
|
||||
private Thread _consoleProxyMain = null;
|
||||
private final Properties _properties = new Properties();
|
||||
private Thread _consoleProxyMain = null;
|
||||
|
||||
long _proxyVmId;
|
||||
int _proxyPort;
|
||||
long _proxyVmId;
|
||||
int _proxyPort;
|
||||
|
||||
String _localgw;
|
||||
String _eth1ip;
|
||||
String _eth1mask;
|
||||
String _pubIp;
|
||||
String _localgw;
|
||||
String _eth1ip;
|
||||
String _eth1mask;
|
||||
String _pubIp;
|
||||
|
||||
@Override
|
||||
public Answer executeRequest(final Command cmd) {
|
||||
if (cmd instanceof CheckConsoleProxyLoadCommand) {
|
||||
return execute((CheckConsoleProxyLoadCommand)cmd);
|
||||
} else if(cmd instanceof WatchConsoleProxyLoadCommand) {
|
||||
return execute((WatchConsoleProxyLoadCommand)cmd);
|
||||
} else if (cmd instanceof ReadyCommand) {
|
||||
s_logger.info("Receive ReadyCommand, response with ReadyAnswer");
|
||||
return new ReadyAnswer((ReadyCommand)cmd);
|
||||
} else if(cmd instanceof CheckHealthCommand) {
|
||||
return new CheckHealthAnswer((CheckHealthCommand)cmd, true);
|
||||
} else if(cmd instanceof StartConsoleProxyAgentHttpHandlerCommand) {
|
||||
return execute((StartConsoleProxyAgentHttpHandlerCommand)cmd);
|
||||
} else {
|
||||
return Answer.createUnsupportedCommandAnswer(cmd);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public Answer executeRequest(final Command cmd) {
|
||||
if (cmd instanceof CheckConsoleProxyLoadCommand) {
|
||||
return execute((CheckConsoleProxyLoadCommand) cmd);
|
||||
} else if (cmd instanceof WatchConsoleProxyLoadCommand) {
|
||||
return execute((WatchConsoleProxyLoadCommand) cmd);
|
||||
} else if (cmd instanceof ReadyCommand) {
|
||||
s_logger.info("Receive ReadyCommand, response with ReadyAnswer");
|
||||
return new ReadyAnswer((ReadyCommand) cmd);
|
||||
} else if (cmd instanceof CheckHealthCommand) {
|
||||
return new CheckHealthAnswer((CheckHealthCommand) cmd, true);
|
||||
} else if (cmd instanceof StartConsoleProxyAgentHttpHandlerCommand) {
|
||||
return execute((StartConsoleProxyAgentHttpHandlerCommand) cmd);
|
||||
} else {
|
||||
return Answer.createUnsupportedCommandAnswer(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
private Answer execute(StartConsoleProxyAgentHttpHandlerCommand cmd) {
|
||||
launchConsoleProxy(cmd.getKeystoreBits(), cmd.getKeystorePassword());
|
||||
return new Answer(cmd);
|
||||
}
|
||||
|
||||
private void disableRpFilter() {
|
||||
try {
|
||||
FileWriter fstream = new FileWriter("/proc/sys/net/ipv4/conf/eth2/rp_filter");
|
||||
private void disableRpFilter() {
|
||||
try {
|
||||
FileWriter fstream = new FileWriter(
|
||||
"/proc/sys/net/ipv4/conf/eth2/rp_filter");
|
||||
BufferedWriter out = new BufferedWriter(fstream);
|
||||
out.write("0");
|
||||
out.close();
|
||||
} catch(IOException e) {
|
||||
s_logger.warn("Unable to disable rp_filter");
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
s_logger.warn("Unable to disable rp_filter");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean copyCertToDirectory(String certificate, String filePath) throws IOException {
|
||||
private boolean copyCertToDirectory(String certificate, String filePath)
|
||||
throws IOException {
|
||||
boolean success;
|
||||
//copy cert to the dir
|
||||
// copy cert to the dir
|
||||
FileWriter fstream = new FileWriter(filePath);
|
||||
BufferedWriter out = new BufferedWriter(fstream);
|
||||
out.write(certificate);
|
||||
//Close the output stream
|
||||
// Close the output stream
|
||||
out.close();
|
||||
success = true;
|
||||
return success;
|
||||
}
|
||||
|
||||
protected Answer execute(final CheckConsoleProxyLoadCommand cmd) {
|
||||
return executeProxyLoadScan(cmd, cmd.getProxyVmId(), cmd.getProxyVmName(), cmd.getProxyManagementIp(), cmd.getProxyCmdPort());
|
||||
}
|
||||
protected Answer execute(final CheckConsoleProxyLoadCommand cmd) {
|
||||
return executeProxyLoadScan(cmd, cmd.getProxyVmId(),
|
||||
cmd.getProxyVmName(), cmd.getProxyManagementIp(),
|
||||
cmd.getProxyCmdPort());
|
||||
}
|
||||
|
||||
protected Answer execute(final WatchConsoleProxyLoadCommand cmd) {
|
||||
return executeProxyLoadScan(cmd, cmd.getProxyVmId(), cmd.getProxyVmName(), cmd.getProxyManagementIp(), cmd.getProxyCmdPort());
|
||||
}
|
||||
protected Answer execute(final WatchConsoleProxyLoadCommand cmd) {
|
||||
return executeProxyLoadScan(cmd, cmd.getProxyVmId(),
|
||||
cmd.getProxyVmName(), cmd.getProxyManagementIp(),
|
||||
cmd.getProxyCmdPort());
|
||||
}
|
||||
|
||||
private Answer executeProxyLoadScan(final Command cmd, final long proxyVmId, final String proxyVmName, final String proxyManagementIp, final int cmdPort) {
|
||||
String result = null;
|
||||
private Answer executeProxyLoadScan(final Command cmd,
|
||||
final long proxyVmId, final String proxyVmName,
|
||||
final String proxyManagementIp, final int cmdPort) {
|
||||
String result = null;
|
||||
|
||||
final StringBuffer sb = new StringBuffer();
|
||||
sb.append("http://").append(proxyManagementIp).append(":" + cmdPort).append("/cmd/getstatus");
|
||||
final StringBuffer sb = new StringBuffer();
|
||||
sb.append("http://").append(proxyManagementIp).append(":" + cmdPort)
|
||||
.append("/cmd/getstatus");
|
||||
|
||||
boolean success = true;
|
||||
try {
|
||||
final URL url = new URL(sb.toString());
|
||||
final URLConnection conn = url.openConnection();
|
||||
boolean success = true;
|
||||
try {
|
||||
final URL url = new URL(sb.toString());
|
||||
final URLConnection conn = url.openConnection();
|
||||
|
||||
final InputStream is = conn.getInputStream();
|
||||
final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
|
||||
final StringBuilder sb2 = new StringBuilder();
|
||||
String line = null;
|
||||
try {
|
||||
while ((line = reader.readLine()) != null)
|
||||
sb2.append(line + "\n");
|
||||
result = sb2.toString();
|
||||
} catch (final IOException e) {
|
||||
success = false;
|
||||
} finally {
|
||||
try {
|
||||
is.close();
|
||||
} catch (final IOException e) {
|
||||
s_logger.warn("Exception when closing , console proxy address : " + proxyManagementIp);
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
} catch(final IOException e) {
|
||||
s_logger.warn("Unable to open console proxy command port url, console proxy address : " + proxyManagementIp);
|
||||
success = false;
|
||||
}
|
||||
final InputStream is = conn.getInputStream();
|
||||
final BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(is));
|
||||
final StringBuilder sb2 = new StringBuilder();
|
||||
String line = null;
|
||||
try {
|
||||
while ((line = reader.readLine()) != null)
|
||||
sb2.append(line + "\n");
|
||||
result = sb2.toString();
|
||||
} catch (final IOException e) {
|
||||
success = false;
|
||||
} finally {
|
||||
try {
|
||||
is.close();
|
||||
} catch (final IOException e) {
|
||||
s_logger.warn("Exception when closing , console proxy address : "
|
||||
+ proxyManagementIp);
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
s_logger.warn("Unable to open console proxy command port url, console proxy address : "
|
||||
+ proxyManagementIp);
|
||||
success = false;
|
||||
}
|
||||
|
||||
return new ConsoleProxyLoadAnswer(cmd, proxyVmId, proxyVmName, success, result);
|
||||
}
|
||||
return new ConsoleProxyLoadAnswer(cmd, proxyVmId, proxyVmName, success,
|
||||
result);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDefaultScriptsDir() {
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
protected String getDefaultScriptsDir() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return Host.Type.ConsoleProxy;
|
||||
}
|
||||
public Type getType() {
|
||||
return Host.Type.ConsoleProxy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized StartupCommand [] initialize() {
|
||||
final StartupProxyCommand cmd = new StartupProxyCommand();
|
||||
fillNetworkInformation(cmd);
|
||||
cmd.setProxyPort(_proxyPort);
|
||||
cmd.setProxyVmId(_proxyVmId);
|
||||
if(_pubIp != null)
|
||||
cmd.setPublicIpAddress(_pubIp);
|
||||
return new StartupCommand[] {cmd};
|
||||
}
|
||||
@Override
|
||||
public synchronized StartupCommand[] initialize() {
|
||||
final StartupProxyCommand cmd = new StartupProxyCommand();
|
||||
fillNetworkInformation(cmd);
|
||||
cmd.setProxyPort(_proxyPort);
|
||||
cmd.setProxyVmId(_proxyVmId);
|
||||
if (_pubIp != null)
|
||||
cmd.setPublicIpAddress(_pubIp);
|
||||
return new StartupCommand[] { cmd };
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnected() {
|
||||
}
|
||||
@Override
|
||||
public void disconnected() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public PingCommand getCurrentStatus(long id) {
|
||||
return new PingCommand(Type.ConsoleProxy, id);
|
||||
}
|
||||
@Override
|
||||
public PingCommand getCurrentStatus(long id) {
|
||||
return new PingCommand(Type.ConsoleProxy, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
|
||||
_localgw = (String)params.get("localgw");
|
||||
_eth1mask = (String)params.get("eth1mask");
|
||||
_eth1ip = (String)params.get("eth1ip");
|
||||
if (_eth1ip != null) {
|
||||
params.put("private.network.device", "eth1");
|
||||
} else {
|
||||
s_logger.warn("WARNING: eth1ip parameter is not found!");
|
||||
}
|
||||
@Override
|
||||
public boolean configure(String name, Map<String, Object> params)
|
||||
throws ConfigurationException {
|
||||
_localgw = (String) params.get("localgw");
|
||||
_eth1mask = (String) params.get("eth1mask");
|
||||
_eth1ip = (String) params.get("eth1ip");
|
||||
if (_eth1ip != null) {
|
||||
params.put("private.network.device", "eth1");
|
||||
} else {
|
||||
s_logger.warn("WARNING: eth1ip parameter is not found!");
|
||||
}
|
||||
|
||||
String eth2ip = (String) params.get("eth2ip");
|
||||
if (eth2ip != null) {
|
||||
params.put("public.network.device", "eth2");
|
||||
} else {
|
||||
s_logger.warn("WARNING: eth2ip parameter is not found!");
|
||||
}
|
||||
String eth2ip = (String) params.get("eth2ip");
|
||||
if (eth2ip != null) {
|
||||
params.put("public.network.device", "eth2");
|
||||
} else {
|
||||
s_logger.warn("WARNING: eth2ip parameter is not found!");
|
||||
}
|
||||
|
||||
super.configure(name, params);
|
||||
super.configure(name, params);
|
||||
|
||||
for(Map.Entry<String, Object> entry : params.entrySet()) {
|
||||
_properties.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
||||
_properties.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
String value = (String)params.get("premium");
|
||||
if(value != null && value.equals("premium"))
|
||||
_proxyPort = 443;
|
||||
else {
|
||||
value = (String)params.get("consoleproxy.httpListenPort");
|
||||
_proxyPort = NumbersUtil.parseInt(value, 80);
|
||||
}
|
||||
String value = (String) params.get("premium");
|
||||
if (value != null && value.equals("premium"))
|
||||
_proxyPort = 443;
|
||||
else {
|
||||
value = (String) params.get("consoleproxy.httpListenPort");
|
||||
_proxyPort = NumbersUtil.parseInt(value, 80);
|
||||
}
|
||||
|
||||
value = (String)params.get("proxy_vm");
|
||||
_proxyVmId = NumbersUtil.parseLong(value, 0);
|
||||
value = (String) params.get("proxy_vm");
|
||||
_proxyVmId = NumbersUtil.parseLong(value, 0);
|
||||
|
||||
if (_localgw != null) {
|
||||
String mgmtHost = (String) params.get("host");
|
||||
addRouteToInternalIpOrCidr(_localgw, _eth1ip, _eth1mask, mgmtHost);
|
||||
if (_localgw != null) {
|
||||
String mgmtHost = (String) params.get("host");
|
||||
addRouteToInternalIpOrCidr(_localgw, _eth1ip, _eth1mask, mgmtHost);
|
||||
|
||||
String internalDns1 = (String) params.get("internaldns1");
|
||||
if (internalDns1 == null) {
|
||||
s_logger.warn("No DNS entry found during configuration of NfsSecondaryStorage");
|
||||
} else {
|
||||
addRouteToInternalIpOrCidr(_localgw, _eth1ip, _eth1mask, internalDns1);
|
||||
}
|
||||
String internalDns1 = (String) params.get("internaldns1");
|
||||
if (internalDns1 == null) {
|
||||
s_logger.warn("No DNS entry found during configuration of NfsSecondaryStorage");
|
||||
} else {
|
||||
addRouteToInternalIpOrCidr(_localgw, _eth1ip, _eth1mask,
|
||||
internalDns1);
|
||||
}
|
||||
|
||||
String internalDns2 = (String) params.get("internaldns2");
|
||||
if (internalDns2 != null) {
|
||||
addRouteToInternalIpOrCidr(_localgw, _eth1ip, _eth1mask, internalDns2);
|
||||
}
|
||||
}
|
||||
String internalDns2 = (String) params.get("internaldns2");
|
||||
if (internalDns2 != null) {
|
||||
addRouteToInternalIpOrCidr(_localgw, _eth1ip, _eth1mask,
|
||||
internalDns2);
|
||||
}
|
||||
}
|
||||
|
||||
_pubIp = (String)params.get("public.ip");
|
||||
_pubIp = (String) params.get("public.ip");
|
||||
|
||||
value = (String)params.get("disable_rp_filter");
|
||||
if(value != null && value.equalsIgnoreCase("true")) {
|
||||
disableRpFilter();
|
||||
}
|
||||
value = (String) params.get("disable_rp_filter");
|
||||
if (value != null && value.equalsIgnoreCase("true")) {
|
||||
disableRpFilter();
|
||||
}
|
||||
|
||||
if(s_logger.isInfoEnabled())
|
||||
s_logger.info("Receive proxyVmId in ConsoleProxyResource configuration as " + _proxyVmId);
|
||||
if (s_logger.isInfoEnabled())
|
||||
s_logger.info("Receive proxyVmId in ConsoleProxyResource configuration as "
|
||||
+ _proxyVmId);
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void addRouteToInternalIpOrCidr(String localgw, String eth1ip, String eth1mask, String destIpOrCidr) {
|
||||
s_logger.debug("addRouteToInternalIp: localgw=" + localgw + ", eth1ip=" + eth1ip + ", eth1mask=" + eth1mask + ",destIp=" + destIpOrCidr);
|
||||
if (destIpOrCidr == null) {
|
||||
s_logger.debug("addRouteToInternalIp: destIp is null");
|
||||
private void addRouteToInternalIpOrCidr(String localgw, String eth1ip,
|
||||
String eth1mask, String destIpOrCidr) {
|
||||
s_logger.debug("addRouteToInternalIp: localgw=" + localgw + ", eth1ip="
|
||||
+ eth1ip + ", eth1mask=" + eth1mask + ",destIp=" + destIpOrCidr);
|
||||
if (destIpOrCidr == null) {
|
||||
s_logger.debug("addRouteToInternalIp: destIp is null");
|
||||
return;
|
||||
}
|
||||
if (!NetUtils.isValidIp(destIpOrCidr) && !NetUtils.isValidCIDR(destIpOrCidr)){
|
||||
s_logger.warn(" destIp is not a valid ip address or cidr destIp=" + destIpOrCidr);
|
||||
return;
|
||||
}
|
||||
boolean inSameSubnet = false;
|
||||
if (NetUtils.isValidIp(destIpOrCidr)) {
|
||||
if (eth1ip != null && eth1mask != null) {
|
||||
inSameSubnet = NetUtils.sameSubnet(eth1ip, destIpOrCidr, eth1mask);
|
||||
} else {
|
||||
s_logger.warn("addRouteToInternalIp: unable to determine same subnet: _eth1ip=" + eth1ip + ", dest ip=" + destIpOrCidr + ", _eth1mask=" + eth1mask);
|
||||
}
|
||||
} else {
|
||||
inSameSubnet = NetUtils.isNetworkAWithinNetworkB(destIpOrCidr, NetUtils.ipAndNetMaskToCidr(eth1ip, eth1mask));
|
||||
}
|
||||
if (inSameSubnet) {
|
||||
s_logger.debug("addRouteToInternalIp: dest ip " + destIpOrCidr + " is in the same subnet as eth1 ip " + eth1ip);
|
||||
return;
|
||||
}
|
||||
Script command = new Script("/bin/bash", s_logger);
|
||||
if (!NetUtils.isValidIp(destIpOrCidr)
|
||||
&& !NetUtils.isValidCIDR(destIpOrCidr)) {
|
||||
s_logger.warn(" destIp is not a valid ip address or cidr destIp="
|
||||
+ destIpOrCidr);
|
||||
return;
|
||||
}
|
||||
boolean inSameSubnet = false;
|
||||
if (NetUtils.isValidIp(destIpOrCidr)) {
|
||||
if (eth1ip != null && eth1mask != null) {
|
||||
inSameSubnet = NetUtils.sameSubnet(eth1ip, destIpOrCidr,
|
||||
eth1mask);
|
||||
} else {
|
||||
s_logger.warn("addRouteToInternalIp: unable to determine same subnet: _eth1ip="
|
||||
+ eth1ip
|
||||
+ ", dest ip="
|
||||
+ destIpOrCidr
|
||||
+ ", _eth1mask="
|
||||
+ eth1mask);
|
||||
}
|
||||
} else {
|
||||
inSameSubnet = NetUtils.isNetworkAWithinNetworkB(destIpOrCidr,
|
||||
NetUtils.ipAndNetMaskToCidr(eth1ip, eth1mask));
|
||||
}
|
||||
if (inSameSubnet) {
|
||||
s_logger.debug("addRouteToInternalIp: dest ip " + destIpOrCidr
|
||||
+ " is in the same subnet as eth1 ip " + eth1ip);
|
||||
return;
|
||||
}
|
||||
Script command = new Script("/bin/bash", s_logger);
|
||||
command.add("-c");
|
||||
command.add("ip route delete " + destIpOrCidr);
|
||||
command.execute();
|
||||
command.add("ip route delete " + destIpOrCidr);
|
||||
command.execute();
|
||||
command = new Script("/bin/bash", s_logger);
|
||||
command.add("-c");
|
||||
command.add("ip route add " + destIpOrCidr + " via " + localgw);
|
||||
String result = command.execute();
|
||||
if (result != null) {
|
||||
s_logger.warn("Error in configuring route to internal ip err=" + result );
|
||||
} else {
|
||||
s_logger.debug("addRouteToInternalIp: added route to internal ip=" + destIpOrCidr + " via " + localgw);
|
||||
}
|
||||
}
|
||||
command.add("ip route add " + destIpOrCidr + " via " + localgw);
|
||||
String result = command.execute();
|
||||
if (result != null) {
|
||||
s_logger.warn("Error in configuring route to internal ip err="
|
||||
+ result);
|
||||
} else {
|
||||
s_logger.debug("addRouteToInternalIp: added route to internal ip="
|
||||
+ destIpOrCidr + " via " + localgw);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return _name;
|
||||
}
|
||||
@Override
|
||||
public String getName() {
|
||||
return _name;
|
||||
}
|
||||
|
||||
private void launchConsoleProxy(final byte[] ksBits, final String ksPassword) {
|
||||
final Object resource = this;
|
||||
if(_consoleProxyMain == null) {
|
||||
private void launchConsoleProxy(final byte[] ksBits, final String ksPassword) {
|
||||
final Object resource = this;
|
||||
if (_consoleProxyMain == null) {
|
||||
_consoleProxyMain = new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
Class<?> consoleProxyClazz = Class.forName("com.cloud.consoleproxy.ConsoleProxy");
|
||||
try {
|
||||
Method method = consoleProxyClazz.getMethod("startWithContext", Properties.class, Object.class, byte[].class, String.class);
|
||||
method.invoke(null, _properties, resource, ksBits, ksPassword);
|
||||
Method method = consoleProxyClazz.getMethod(
|
||||
"startWithContext", Properties.class,
|
||||
Object.class, byte[].class, String.class);
|
||||
method.invoke(null, _properties, resource, ksBits,
|
||||
ksPassword);
|
||||
} catch (SecurityException e) {
|
||||
s_logger.error("Unable to launch console proxy due to SecurityException");
|
||||
System.exit(ExitStatus.Error.value());
|
||||
|
|
@ -350,54 +389,68 @@ public class ConsoleProxyResource extends ServerResourceBase implements ServerRe
|
|||
}, "Console-Proxy-Main");
|
||||
_consoleProxyMain.setDaemon(true);
|
||||
_consoleProxyMain.start();
|
||||
} else {
|
||||
} else {
|
||||
s_logger.error("com.cloud.consoleproxy.ConsoleProxy is already running");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean authenticateConsoleAccess(String host, String port, String vmId, String sid, String ticket) {
|
||||
ConsoleAccessAuthenticationCommand cmd = new ConsoleAccessAuthenticationCommand(host, port, vmId, sid, ticket);
|
||||
public boolean authenticateConsoleAccess(String host, String port,
|
||||
String vmId, String sid, String ticket) {
|
||||
ConsoleAccessAuthenticationCommand cmd = new ConsoleAccessAuthenticationCommand(
|
||||
host, port, vmId, sid, ticket);
|
||||
|
||||
try {
|
||||
AgentControlAnswer answer = getAgentControl().sendRequest(cmd, 10000);
|
||||
if(answer != null) {
|
||||
return ((ConsoleAccessAuthenticationAnswer)answer).succeeded();
|
||||
try {
|
||||
AgentControlAnswer answer = getAgentControl().sendRequest(cmd,
|
||||
10000);
|
||||
if (answer != null) {
|
||||
return ((ConsoleAccessAuthenticationAnswer) answer).succeeded();
|
||||
} else {
|
||||
s_logger.error("Authentication failed for vm: " + vmId + " with sid: " + sid);
|
||||
s_logger.error("Authentication failed for vm: " + vmId
|
||||
+ " with sid: " + sid);
|
||||
}
|
||||
|
||||
} catch (AgentControlChannelException e) {
|
||||
s_logger.error("Unable to send out console access authentication request due to " + e.getMessage(), e);
|
||||
s_logger.error(
|
||||
"Unable to send out console access authentication request due to "
|
||||
+ e.getMessage(), e);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void reportLoadInfo(String gsonLoadInfo) {
|
||||
ConsoleProxyLoadReportCommand cmd = new ConsoleProxyLoadReportCommand(_proxyVmId, gsonLoadInfo);
|
||||
try {
|
||||
public void reportLoadInfo(String gsonLoadInfo) {
|
||||
ConsoleProxyLoadReportCommand cmd = new ConsoleProxyLoadReportCommand(
|
||||
_proxyVmId, gsonLoadInfo);
|
||||
try {
|
||||
getAgentControl().postRequest(cmd);
|
||||
|
||||
if(s_logger.isDebugEnabled())
|
||||
s_logger.debug("Report proxy load info, proxy : " + _proxyVmId + ", load: " + gsonLoadInfo);
|
||||
if (s_logger.isDebugEnabled())
|
||||
s_logger.debug("Report proxy load info, proxy : " + _proxyVmId
|
||||
+ ", load: " + gsonLoadInfo);
|
||||
} catch (AgentControlChannelException e) {
|
||||
s_logger.error("Unable to send out load info due to " + e.getMessage(), e);
|
||||
s_logger.error(
|
||||
"Unable to send out load info due to " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ensureRoute(String address) {
|
||||
if(_localgw != null) {
|
||||
if(s_logger.isDebugEnabled())
|
||||
s_logger.debug("Ensure route for " + address + " via " + _localgw);
|
||||
public void ensureRoute(String address) {
|
||||
if (_localgw != null) {
|
||||
if (s_logger.isDebugEnabled())
|
||||
s_logger.debug("Ensure route for " + address + " via "
|
||||
+ _localgw);
|
||||
|
||||
// this method won't be called in high frequency, serialize access to script execution
|
||||
synchronized(this) {
|
||||
try {
|
||||
addRouteToInternalIpOrCidr(_localgw, _eth1ip, _eth1mask, address);
|
||||
} catch(Throwable e) {
|
||||
s_logger.warn("Unexpected exception while adding internal route to " + address, e);
|
||||
}
|
||||
// this method won't be called in high frequency, serialize access
|
||||
// to script execution
|
||||
synchronized (this) {
|
||||
try {
|
||||
addRouteToInternalIpOrCidr(_localgw, _eth1ip, _eth1mask,
|
||||
address);
|
||||
} catch (Throwable e) {
|
||||
s_logger.warn(
|
||||
"Unexpected exception while adding internal route to "
|
||||
+ address, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,17 +4,20 @@ public class KVMPhysicalDisk {
|
|||
private String path;
|
||||
private String name;
|
||||
private KVMStoragePool pool;
|
||||
|
||||
public static enum PhysicalDiskFormat {
|
||||
RAW("raw"),
|
||||
QCOW2("qcow2");
|
||||
RAW("raw"), QCOW2("qcow2");
|
||||
String format;
|
||||
|
||||
private PhysicalDiskFormat(String format) {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.format;
|
||||
}
|
||||
}
|
||||
|
||||
private PhysicalDiskFormat format;
|
||||
private long size;
|
||||
private long virtualSize;
|
||||
|
|
|
|||
|
|
@ -6,19 +6,34 @@ import com.cloud.agent.storage.KVMPhysicalDisk.PhysicalDiskFormat;
|
|||
import com.cloud.storage.Storage.StoragePoolType;
|
||||
|
||||
public interface KVMStoragePool {
|
||||
public KVMPhysicalDisk createPhysicalDisk(String name, PhysicalDiskFormat format, long size);
|
||||
public KVMPhysicalDisk createPhysicalDisk(String name,
|
||||
PhysicalDiskFormat format, long size);
|
||||
|
||||
public KVMPhysicalDisk createPhysicalDisk(String name, long size);
|
||||
|
||||
public KVMPhysicalDisk getPhysicalDisk(String volumeUuid);
|
||||
|
||||
public boolean deletePhysicalDisk(String uuid);
|
||||
|
||||
public List<KVMPhysicalDisk> listPhysicalDisks();
|
||||
|
||||
public String getUuid();
|
||||
|
||||
public long getCapacity();
|
||||
|
||||
public long getUsed();
|
||||
|
||||
public boolean refresh();
|
||||
|
||||
public boolean isExternalSnapshot();
|
||||
|
||||
public String getLocalPath();
|
||||
|
||||
public StoragePoolType getType();
|
||||
|
||||
public boolean delete();
|
||||
|
||||
PhysicalDiskFormat getDefaultFormat();
|
||||
|
||||
public boolean createFolder(String path);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
package com.cloud.agent.storage;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
|
|
@ -13,68 +14,80 @@ public class KVMStoragePoolManager {
|
|||
private StorageAdaptor _storageAdaptor;
|
||||
private KVMHAMonitor _haMonitor;
|
||||
private final Map<String, Object> _storagePools = new ConcurrentHashMap<String, Object>();
|
||||
|
||||
|
||||
private void addStoragePool(String uuid) {
|
||||
synchronized (_storagePools) {
|
||||
if (!_storagePools.containsKey(uuid)) {
|
||||
_storagePools.put(uuid, new Object());
|
||||
}
|
||||
}
|
||||
}
|
||||
synchronized (_storagePools) {
|
||||
if (!_storagePools.containsKey(uuid)) {
|
||||
_storagePools.put(uuid, new Object());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public KVMStoragePoolManager(StorageLayer storagelayer, KVMHAMonitor monitor) {
|
||||
this._storageAdaptor = new LibvirtStorageAdaptor(storagelayer);
|
||||
this._haMonitor = monitor;
|
||||
}
|
||||
|
||||
|
||||
public KVMStoragePool getStoragePool(String uuid) {
|
||||
return this._storageAdaptor.getStoragePool(uuid);
|
||||
}
|
||||
|
||||
|
||||
public KVMStoragePool getStoragePoolByURI(String uri) {
|
||||
return this._storageAdaptor.getStoragePoolByUri(uri);
|
||||
}
|
||||
|
||||
public KVMStoragePool createStoragePool(String name, String host, String path, StoragePoolType type) {
|
||||
KVMStoragePool pool = this._storageAdaptor.createStoragePool(name, host, path, type);
|
||||
|
||||
public KVMStoragePool createStoragePool(String name, String host,
|
||||
String path, StoragePoolType type) {
|
||||
KVMStoragePool pool = this._storageAdaptor.createStoragePool(name,
|
||||
host, path, type);
|
||||
if (type == StoragePoolType.NetworkFilesystem) {
|
||||
KVMHABase.NfsStoragePool nfspool = new KVMHABase.NfsStoragePool(pool.getUuid(),
|
||||
host,
|
||||
path,
|
||||
pool.getLocalPath(),
|
||||
PoolType.PrimaryStorage);
|
||||
_haMonitor.addStoragePool(nfspool);
|
||||
KVMHABase.NfsStoragePool nfspool = new KVMHABase.NfsStoragePool(
|
||||
pool.getUuid(), host, path, pool.getLocalPath(),
|
||||
PoolType.PrimaryStorage);
|
||||
_haMonitor.addStoragePool(nfspool);
|
||||
}
|
||||
addStoragePool(pool.getUuid());
|
||||
return pool;
|
||||
}
|
||||
|
||||
|
||||
public boolean deleteStoragePool(String uuid) {
|
||||
_haMonitor.removeStoragePool(uuid);
|
||||
this._storageAdaptor.deleteStoragePool(uuid);
|
||||
_storagePools.remove(uuid);
|
||||
return true;
|
||||
this._storageAdaptor.deleteStoragePool(uuid);
|
||||
_storagePools.remove(uuid);
|
||||
return true;
|
||||
}
|
||||
|
||||
public KVMPhysicalDisk createDiskFromTemplate(KVMPhysicalDisk template, String name, KVMStoragePool destPool) {
|
||||
|
||||
public KVMPhysicalDisk createDiskFromTemplate(KVMPhysicalDisk template,
|
||||
String name, KVMStoragePool destPool) {
|
||||
if (destPool.getType() == StoragePoolType.CLVM) {
|
||||
return this._storageAdaptor.createDiskFromTemplate(template, name, KVMPhysicalDisk.PhysicalDiskFormat.RAW, template.getSize(), destPool);
|
||||
return this._storageAdaptor.createDiskFromTemplate(template, name,
|
||||
KVMPhysicalDisk.PhysicalDiskFormat.RAW, template.getSize(),
|
||||
destPool);
|
||||
} else {
|
||||
return this._storageAdaptor.createDiskFromTemplate(template, name, KVMPhysicalDisk.PhysicalDiskFormat.QCOW2, template.getSize(), destPool);
|
||||
return this._storageAdaptor.createDiskFromTemplate(template, name,
|
||||
KVMPhysicalDisk.PhysicalDiskFormat.QCOW2,
|
||||
template.getSize(), destPool);
|
||||
}
|
||||
}
|
||||
|
||||
public KVMPhysicalDisk createTemplateFromDisk(KVMPhysicalDisk disk, String name, PhysicalDiskFormat format, long size, KVMStoragePool destPool) {
|
||||
return this._storageAdaptor.createTemplateFromDisk(disk, name, format, size, destPool);
|
||||
|
||||
public KVMPhysicalDisk createTemplateFromDisk(KVMPhysicalDisk disk,
|
||||
String name, PhysicalDiskFormat format, long size,
|
||||
KVMStoragePool destPool) {
|
||||
return this._storageAdaptor.createTemplateFromDisk(disk, name, format,
|
||||
size, destPool);
|
||||
}
|
||||
|
||||
public KVMPhysicalDisk copyPhysicalDisk(KVMPhysicalDisk disk, String name, KVMStoragePool destPool) {
|
||||
|
||||
public KVMPhysicalDisk copyPhysicalDisk(KVMPhysicalDisk disk, String name,
|
||||
KVMStoragePool destPool) {
|
||||
return this._storageAdaptor.copyPhysicalDisk(disk, name, destPool);
|
||||
}
|
||||
|
||||
public KVMPhysicalDisk createDiskFromSnapshot(KVMPhysicalDisk snapshot, String snapshotName, String name, KVMStoragePool destPool) {
|
||||
return this._storageAdaptor.createDiskFromSnapshot(snapshot, snapshotName, name, destPool);
|
||||
|
||||
public KVMPhysicalDisk createDiskFromSnapshot(KVMPhysicalDisk snapshot,
|
||||
String snapshotName, String name, KVMStoragePool destPool) {
|
||||
return this._storageAdaptor.createDiskFromSnapshot(snapshot,
|
||||
snapshotName, name, destPool);
|
||||
}
|
||||
|
||||
|
||||
public KVMPhysicalDisk getPhysicalDiskFromUrl(String url) {
|
||||
return this._storageAdaptor.getPhysicalDiskFromURI(url);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,5 +2,4 @@ package com.cloud.agent.storage;
|
|||
|
||||
public class KVMVirtualDisk {
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,382 +49,408 @@ import com.cloud.utils.script.OutputInterpreter;
|
|||
import com.cloud.utils.script.Script;
|
||||
|
||||
public class LibvirtStorageAdaptor implements StorageAdaptor {
|
||||
private static final Logger s_logger = Logger.getLogger(LibvirtStorageAdaptor.class);
|
||||
private StorageLayer _storageLayer;
|
||||
private String _mountPoint = "/mnt";
|
||||
private String _manageSnapshotPath;
|
||||
private static final Logger s_logger = Logger
|
||||
.getLogger(LibvirtStorageAdaptor.class);
|
||||
private StorageLayer _storageLayer;
|
||||
private String _mountPoint = "/mnt";
|
||||
private String _manageSnapshotPath;
|
||||
|
||||
public LibvirtStorageAdaptor(StorageLayer storage
|
||||
) {
|
||||
_storageLayer = storage;
|
||||
_manageSnapshotPath = Script.findScript("scripts/storage/qcow2/", "managesnapshot.sh");
|
||||
}
|
||||
public LibvirtStorageAdaptor(StorageLayer storage) {
|
||||
_storageLayer = storage;
|
||||
_manageSnapshotPath = Script.findScript("scripts/storage/qcow2/",
|
||||
"managesnapshot.sh");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean createFolder(String uuid, String path) {
|
||||
String mountPoint = _mountPoint + File.separator + uuid;
|
||||
File f = new File(mountPoint + path);
|
||||
if (!f.exists()) {
|
||||
f.mkdirs();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public boolean createFolder(String uuid, String path) {
|
||||
String mountPoint = _mountPoint + File.separator + uuid;
|
||||
File f = new File(mountPoint + path);
|
||||
if (!f.exists()) {
|
||||
f.mkdirs();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public StorageVol getVolume(StoragePool pool, String volName) {
|
||||
StorageVol vol = null;
|
||||
public StorageVol getVolume(StoragePool pool, String volName) {
|
||||
StorageVol vol = null;
|
||||
|
||||
try {
|
||||
vol = pool.storageVolLookupByName(volName);
|
||||
} catch (LibvirtException e) {
|
||||
try {
|
||||
vol = pool.storageVolLookupByName(volName);
|
||||
} catch (LibvirtException e) {
|
||||
|
||||
}
|
||||
if (vol == null) {
|
||||
storagePoolRefresh(pool);
|
||||
try {
|
||||
vol = pool.storageVolLookupByName(volName);
|
||||
}
|
||||
if (vol == null) {
|
||||
storagePoolRefresh(pool);
|
||||
try {
|
||||
vol = pool.storageVolLookupByName(volName);
|
||||
} catch (LibvirtException e) {
|
||||
throw new CloudRuntimeException(e.toString());
|
||||
}
|
||||
}
|
||||
return vol;
|
||||
}
|
||||
}
|
||||
return vol;
|
||||
}
|
||||
|
||||
public StorageVol createVolume(Connect conn, StoragePool pool, String uuid, long size, volFormat format) throws LibvirtException {
|
||||
LibvirtStorageVolumeDef volDef = new LibvirtStorageVolumeDef(UUID.randomUUID().toString(), size, format, null, null);
|
||||
s_logger.debug(volDef.toString());
|
||||
return pool.storageVolCreateXML(volDef.toString(), 0);
|
||||
}
|
||||
public StorageVol createVolume(Connect conn, StoragePool pool, String uuid,
|
||||
long size, volFormat format) throws LibvirtException {
|
||||
LibvirtStorageVolumeDef volDef = new LibvirtStorageVolumeDef(UUID
|
||||
.randomUUID().toString(), size, format, null, null);
|
||||
s_logger.debug(volDef.toString());
|
||||
return pool.storageVolCreateXML(volDef.toString(), 0);
|
||||
}
|
||||
|
||||
public StoragePool getStoragePoolbyURI(Connect conn, URI uri) throws LibvirtException {
|
||||
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";
|
||||
}
|
||||
public StoragePool getStoragePoolbyURI(Connect conn, URI uri)
|
||||
throws LibvirtException {
|
||||
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 {
|
||||
sp = conn.storagePoolLookupByUUIDString(uuid);
|
||||
} catch (LibvirtException e) {
|
||||
}
|
||||
|
||||
String targetPath = _mountPoint + File.separator + uuid;
|
||||
StoragePool sp = null;
|
||||
try {
|
||||
sp = conn.storagePoolLookupByUUIDString(uuid);
|
||||
} catch (LibvirtException e) {
|
||||
}
|
||||
if (sp == null) {
|
||||
try {
|
||||
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);
|
||||
|
||||
if (sp == null) {
|
||||
try {
|
||||
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);
|
||||
} else if (protocal.equalsIgnoreCase("DIR")) {
|
||||
_storageLayer.mkdir(targetPath);
|
||||
spd = new LibvirtStoragePoolDef(poolType.DIR, uuid, uuid,
|
||||
null, null, sourcePath);
|
||||
}
|
||||
|
||||
} 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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
if (sp == null) {
|
||||
s_logger.debug("Failed to define storage pool");
|
||||
return null;
|
||||
}
|
||||
sp.create(0);
|
||||
}
|
||||
return sp;
|
||||
} catch (LibvirtException e) {
|
||||
try {
|
||||
if (sp != null) {
|
||||
sp.undefine();
|
||||
sp.free();
|
||||
}
|
||||
} catch (LibvirtException l) {
|
||||
|
||||
return sp;
|
||||
} catch (LibvirtException e) {
|
||||
try {
|
||||
if (sp != null) {
|
||||
sp.undefine();
|
||||
sp.free();
|
||||
}
|
||||
} catch (LibvirtException l) {
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
StoragePoolInfo spi = sp.getInfo();
|
||||
if (spi.state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
|
||||
sp.create(0);
|
||||
}
|
||||
return sp;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
StoragePoolInfo spi = sp.getInfo();
|
||||
if (spi.state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
|
||||
sp.create(0);
|
||||
}
|
||||
return sp;
|
||||
}
|
||||
}
|
||||
public void storagePoolRefresh(StoragePool pool) {
|
||||
try {
|
||||
synchronized (getStoragePool(pool.getUUIDString())) {
|
||||
pool.refresh(0);
|
||||
}
|
||||
} catch (LibvirtException e) {
|
||||
|
||||
public void storagePoolRefresh(StoragePool pool) {
|
||||
try {
|
||||
synchronized (getStoragePool(pool.getUUIDString())) {
|
||||
pool.refresh(0);
|
||||
}
|
||||
} catch (LibvirtException e) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
private StoragePool createNfsStoragePool(Connect conn, String uuid,
|
||||
String host, String path) {
|
||||
String targetPath = _mountPoint + File.separator + uuid;
|
||||
LibvirtStoragePoolDef spd = new LibvirtStoragePoolDef(poolType.NETFS,
|
||||
uuid, uuid, host, path, targetPath);
|
||||
_storageLayer.mkdir(targetPath);
|
||||
StoragePool sp = null;
|
||||
try {
|
||||
s_logger.debug(spd.toString());
|
||||
sp = conn.storagePoolDefineXML(spd.toString(), 0);
|
||||
sp.create(0);
|
||||
return sp;
|
||||
} catch (LibvirtException e) {
|
||||
s_logger.debug(e.toString());
|
||||
if (sp != null) {
|
||||
try {
|
||||
sp.undefine();
|
||||
sp.free();
|
||||
} catch (LibvirtException l) {
|
||||
s_logger.debug("Failed to define nfs storage pool with: "
|
||||
+ l.toString());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private StoragePool createNfsStoragePool(Connect conn, String uuid, String host, String path) {
|
||||
String targetPath = _mountPoint + File.separator + uuid;
|
||||
LibvirtStoragePoolDef spd = new LibvirtStoragePoolDef(poolType.NETFS, uuid, uuid,
|
||||
host, path, targetPath);
|
||||
_storageLayer.mkdir(targetPath);
|
||||
StoragePool sp = null;
|
||||
try {
|
||||
s_logger.debug(spd.toString());
|
||||
sp = conn.storagePoolDefineXML(spd.toString(), 0);
|
||||
sp.create(0);
|
||||
return sp;
|
||||
} catch (LibvirtException e) {
|
||||
s_logger.debug(e.toString());
|
||||
if (sp != null) {
|
||||
try {
|
||||
sp.undefine();
|
||||
sp.free();
|
||||
} catch (LibvirtException l) {
|
||||
s_logger.debug("Failed to define nfs storage pool with: " + l.toString());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
private StoragePool CreateSharedStoragePool(Connect conn, String uuid,
|
||||
String host, String path) {
|
||||
String mountPoint = path;
|
||||
if (!_storageLayer.exists(mountPoint)) {
|
||||
return null;
|
||||
}
|
||||
LibvirtStoragePoolDef spd = new LibvirtStoragePoolDef(poolType.DIR,
|
||||
uuid, uuid, host, path, path);
|
||||
StoragePool sp = null;
|
||||
try {
|
||||
s_logger.debug(spd.toString());
|
||||
sp = conn.storagePoolDefineXML(spd.toString(), 0);
|
||||
sp.create(0);
|
||||
|
||||
private StoragePool CreateSharedStoragePool(Connect conn, String uuid, String host, String path) {
|
||||
String mountPoint = path;
|
||||
if (!_storageLayer.exists(mountPoint)) {
|
||||
return null;
|
||||
}
|
||||
LibvirtStoragePoolDef spd = new LibvirtStoragePoolDef(poolType.DIR, uuid, uuid,
|
||||
host, path, path);
|
||||
StoragePool sp = null;
|
||||
try {
|
||||
s_logger.debug(spd.toString());
|
||||
sp = conn.storagePoolDefineXML(spd.toString(), 0);
|
||||
sp.create(0);
|
||||
return sp;
|
||||
} catch (LibvirtException e) {
|
||||
s_logger.debug(e.toString());
|
||||
if (sp != null) {
|
||||
try {
|
||||
sp.undefine();
|
||||
sp.free();
|
||||
} catch (LibvirtException l) {
|
||||
s_logger.debug("Failed to define shared mount point storage pool with: "
|
||||
+ l.toString());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return sp;
|
||||
} catch (LibvirtException e) {
|
||||
s_logger.debug(e.toString());
|
||||
if (sp != null) {
|
||||
try {
|
||||
sp.undefine();
|
||||
sp.free();
|
||||
} catch (LibvirtException l) {
|
||||
s_logger.debug("Failed to define shared mount point storage pool with: " + l.toString());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
private StoragePool createCLVMStoragePool(Connect conn, String uuid,
|
||||
String host, String path) {
|
||||
|
||||
private StoragePool createCLVMStoragePool(Connect conn, String uuid, String host, String path) {
|
||||
String volgroupPath = "/dev/" + path;
|
||||
|
||||
String volgroupPath = "/dev/" + path;
|
||||
LibvirtStoragePoolDef spd = new LibvirtStoragePoolDef(poolType.LOGICAL,
|
||||
uuid, uuid, host, volgroupPath, volgroupPath);
|
||||
StoragePool sp = null;
|
||||
try {
|
||||
s_logger.debug(spd.toString());
|
||||
sp = conn.storagePoolDefineXML(spd.toString(), 0);
|
||||
sp.create(0);
|
||||
return sp;
|
||||
} catch (LibvirtException e) {
|
||||
s_logger.debug(e.toString());
|
||||
if (sp != null) {
|
||||
try {
|
||||
sp.undefine();
|
||||
sp.free();
|
||||
} catch (LibvirtException l) {
|
||||
s_logger.debug("Failed to define clvm storage pool with: "
|
||||
+ l.toString());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
LibvirtStoragePoolDef spd = new LibvirtStoragePoolDef(poolType.LOGICAL, uuid, uuid,
|
||||
host, volgroupPath, volgroupPath);
|
||||
StoragePool sp = null;
|
||||
try {
|
||||
s_logger.debug(spd.toString());
|
||||
sp = conn.storagePoolDefineXML(spd.toString(), 0);
|
||||
sp.create(0);
|
||||
return sp;
|
||||
} catch (LibvirtException e) {
|
||||
s_logger.debug(e.toString());
|
||||
if (sp != null) {
|
||||
try {
|
||||
sp.undefine();
|
||||
sp.free();
|
||||
} catch (LibvirtException l) {
|
||||
s_logger.debug("Failed to define clvm storage pool with: " + l.toString());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public StorageVol copyVolume(StoragePool destPool,
|
||||
LibvirtStorageVolumeDef destVol, StorageVol srcVol, int timeout)
|
||||
throws LibvirtException {
|
||||
StorageVol vol = destPool.storageVolCreateXML(destVol.toString(), 0);
|
||||
String srcPath = srcVol.getKey();
|
||||
String destPath = vol.getKey();
|
||||
Script.runSimpleBashScript("cp " + srcPath + " " + destPath, timeout);
|
||||
return vol;
|
||||
}
|
||||
|
||||
public StorageVol copyVolume(StoragePool destPool, LibvirtStorageVolumeDef destVol, StorageVol srcVol, int timeout) throws LibvirtException {
|
||||
StorageVol vol = destPool.storageVolCreateXML(destVol.toString(), 0);
|
||||
String srcPath = srcVol.getKey();
|
||||
String destPath = vol.getKey();
|
||||
Script.runSimpleBashScript("cp " + srcPath + " " + destPath, timeout);
|
||||
return vol;
|
||||
}
|
||||
public boolean copyVolume(String srcPath, String destPath,
|
||||
String volumeName, int timeout) throws InternalErrorException {
|
||||
_storageLayer.mkdirs(destPath);
|
||||
if (!_storageLayer.exists(srcPath)) {
|
||||
throw new InternalErrorException("volume:" + srcPath
|
||||
+ " is not exits");
|
||||
}
|
||||
String result = Script.runSimpleBashScript("cp " + srcPath + " "
|
||||
+ destPath + File.separator + volumeName, timeout);
|
||||
if (result != null) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean copyVolume(String srcPath, String destPath, String volumeName, int timeout) throws InternalErrorException{
|
||||
_storageLayer.mkdirs(destPath);
|
||||
if (!_storageLayer.exists(srcPath)) {
|
||||
throw new InternalErrorException("volume:" + srcPath + " is not exits");
|
||||
}
|
||||
String result = Script.runSimpleBashScript("cp " + srcPath + " " + destPath + File.separator + volumeName, timeout);
|
||||
if (result != null) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public LibvirtStoragePoolDef getStoragePoolDef(Connect conn,
|
||||
StoragePool pool) throws LibvirtException {
|
||||
String poolDefXML = pool.getXMLDesc(0);
|
||||
LibvirtStoragePoolXMLParser parser = new LibvirtStoragePoolXMLParser();
|
||||
return parser.parseStoragePoolXML(poolDefXML);
|
||||
}
|
||||
|
||||
public LibvirtStoragePoolDef getStoragePoolDef(Connect conn, StoragePool pool) throws LibvirtException {
|
||||
String poolDefXML = pool.getXMLDesc(0);
|
||||
LibvirtStoragePoolXMLParser parser = new LibvirtStoragePoolXMLParser();
|
||||
return parser.parseStoragePoolXML(poolDefXML);
|
||||
}
|
||||
public LibvirtStorageVolumeDef getStorageVolumeDef(Connect conn,
|
||||
StorageVol vol) throws LibvirtException {
|
||||
String volDefXML = vol.getXMLDesc(0);
|
||||
LibvirtStorageVolumeXMLParser parser = new LibvirtStorageVolumeXMLParser();
|
||||
return parser.parseStorageVolumeXML(volDefXML);
|
||||
}
|
||||
|
||||
public LibvirtStorageVolumeDef getStorageVolumeDef(Connect conn, StorageVol vol) throws LibvirtException {
|
||||
String volDefXML = vol.getXMLDesc(0);
|
||||
LibvirtStorageVolumeXMLParser parser = new LibvirtStorageVolumeXMLParser();
|
||||
return parser.parseStorageVolumeXML(volDefXML);
|
||||
}
|
||||
public StorageVol getVolumeFromURI(Connect conn, String volPath)
|
||||
throws LibvirtException, URISyntaxException {
|
||||
int index = volPath.lastIndexOf("/");
|
||||
URI volDir = null;
|
||||
StoragePool sp = null;
|
||||
StorageVol vol = null;
|
||||
try {
|
||||
volDir = new URI(volPath.substring(0, index));
|
||||
String volName = volPath.substring(index + 1);
|
||||
sp = getStoragePoolbyURI(conn, volDir);
|
||||
vol = sp.storageVolLookupByName(volName);
|
||||
return vol;
|
||||
} catch (LibvirtException e) {
|
||||
s_logger.debug("Faild to get vol path: " + e.toString());
|
||||
throw e;
|
||||
} finally {
|
||||
try {
|
||||
if (sp != null) {
|
||||
sp.free();
|
||||
}
|
||||
} catch (LibvirtException e) {
|
||||
|
||||
public StorageVol getVolumeFromURI(Connect conn, String volPath) throws LibvirtException, URISyntaxException {
|
||||
int index = volPath.lastIndexOf("/");
|
||||
URI volDir = null;
|
||||
StoragePool sp = null;
|
||||
StorageVol vol = null;
|
||||
try {
|
||||
volDir = new URI(volPath.substring(0, index));
|
||||
String volName = volPath.substring(index + 1);
|
||||
sp = getStoragePoolbyURI(conn, volDir);
|
||||
vol = sp.storageVolLookupByName(volName);
|
||||
return vol;
|
||||
} catch (LibvirtException e) {
|
||||
s_logger.debug("Faild to get vol path: " + e.toString());
|
||||
throw e;
|
||||
} finally {
|
||||
try {
|
||||
if (sp != null) {
|
||||
sp.free();
|
||||
}
|
||||
} catch (LibvirtException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
public StoragePool createFileBasedStoragePool(Connect conn,
|
||||
String localStoragePath, String uuid) {
|
||||
if (!(_storageLayer.exists(localStoragePath) && _storageLayer
|
||||
.isDirectory(localStoragePath))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public StoragePool createFileBasedStoragePool(Connect conn, String localStoragePath, String uuid) {
|
||||
if (!(_storageLayer.exists(localStoragePath) && _storageLayer.isDirectory(localStoragePath))) {
|
||||
return null;
|
||||
}
|
||||
File path = new File(localStoragePath);
|
||||
if (!(path.canWrite() && path.canRead() && path.canExecute())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
File path = new File(localStoragePath);
|
||||
if (!(path.canWrite() && path.canRead() && path.canExecute())) {
|
||||
return null;
|
||||
}
|
||||
StoragePool pool = null;
|
||||
|
||||
StoragePool pool = null;
|
||||
try {
|
||||
pool = conn.storagePoolLookupByUUIDString(uuid);
|
||||
} catch (LibvirtException e) {
|
||||
|
||||
try {
|
||||
pool = conn.storagePoolLookupByUUIDString(uuid);
|
||||
} catch (LibvirtException e) {
|
||||
}
|
||||
|
||||
}
|
||||
if (pool == null) {
|
||||
LibvirtStoragePoolDef spd = new LibvirtStoragePoolDef(poolType.DIR,
|
||||
uuid, uuid, null, null, localStoragePath);
|
||||
try {
|
||||
pool = conn.storagePoolDefineXML(spd.toString(), 0);
|
||||
pool.create(0);
|
||||
} catch (LibvirtException e) {
|
||||
if (pool != null) {
|
||||
try {
|
||||
pool.destroy();
|
||||
pool.undefine();
|
||||
} catch (LibvirtException e1) {
|
||||
}
|
||||
pool = null;
|
||||
}
|
||||
throw new CloudRuntimeException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
if (pool == null) {
|
||||
LibvirtStoragePoolDef spd = new LibvirtStoragePoolDef(poolType.DIR, uuid, uuid,
|
||||
null, null, localStoragePath);
|
||||
try {
|
||||
pool = conn.storagePoolDefineXML(spd.toString(), 0);
|
||||
pool.create(0);
|
||||
} catch (LibvirtException e) {
|
||||
if (pool != null) {
|
||||
try {
|
||||
pool.destroy();
|
||||
pool.undefine();
|
||||
} catch (LibvirtException e1) {
|
||||
}
|
||||
pool = null;
|
||||
}
|
||||
throw new CloudRuntimeException(e.toString());
|
||||
}
|
||||
}
|
||||
try {
|
||||
StoragePoolInfo spi = pool.getInfo();
|
||||
if (spi.state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
|
||||
pool.create(0);
|
||||
}
|
||||
|
||||
try {
|
||||
StoragePoolInfo spi = pool.getInfo();
|
||||
if (spi.state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
|
||||
pool.create(0);
|
||||
}
|
||||
} catch (LibvirtException e) {
|
||||
throw new CloudRuntimeException(e.toString());
|
||||
}
|
||||
|
||||
} catch (LibvirtException e) {
|
||||
throw new CloudRuntimeException(e.toString());
|
||||
}
|
||||
return pool;
|
||||
}
|
||||
|
||||
return pool;
|
||||
}
|
||||
|
||||
private void getStats(LibvirtStoragePool pool) {
|
||||
Script statsScript = new Script("/bin/bash", s_logger);
|
||||
statsScript.add("-c");
|
||||
statsScript.add("stats=$(df --total " + pool.getLocalPath() + " |grep total|awk '{print $2,$3}');echo $stats");
|
||||
final OutputInterpreter.OneLineParser statsParser = new OutputInterpreter.OneLineParser();
|
||||
String result = statsScript.execute(statsParser);
|
||||
if (result == null) {
|
||||
String stats = statsParser.getLine();
|
||||
if (stats != null && !stats.isEmpty()) {
|
||||
String sizes[] = stats.trim().split(" ");
|
||||
if (sizes.length == 2) {
|
||||
pool.setCapacity(Long.parseLong(sizes[0]) * 1024);
|
||||
pool.setUsed(Long.parseLong(sizes[1]) * 1024);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public KVMStoragePool getStoragePool(String uuid) {
|
||||
StoragePool storage = null;
|
||||
try {
|
||||
Connect conn = LibvirtConnection.getConnection();
|
||||
storage = conn.storagePoolLookupByUUIDString(uuid);
|
||||
|
||||
if (storage.getInfo().state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
|
||||
storage.create(0);
|
||||
}
|
||||
LibvirtStoragePoolDef spd = getStoragePoolDef(conn, storage);
|
||||
StoragePoolType type = null;
|
||||
if (spd.getPoolType() == LibvirtStoragePoolDef.poolType.NETFS || spd.getPoolType() == LibvirtStoragePoolDef.poolType.DIR) {
|
||||
type = StoragePoolType.Filesystem;
|
||||
} else if (spd.getPoolType() == LibvirtStoragePoolDef.poolType.LOGICAL) {
|
||||
type = StoragePoolType.CLVM;
|
||||
}
|
||||
LibvirtStoragePool pool = new LibvirtStoragePool(uuid, storage.getName(), type, this, storage);
|
||||
pool.setLocalPath(spd.getTargetPath());
|
||||
|
||||
if (pool.getType() == StoragePoolType.CLVM) {
|
||||
pool.setCapacity(storage.getInfo().capacity);
|
||||
pool.setUsed(storage.getInfo().allocation);
|
||||
} else {
|
||||
getStats(pool);
|
||||
}
|
||||
return pool;
|
||||
} catch (LibvirtException e) {
|
||||
throw new CloudRuntimeException(e.toString());
|
||||
}
|
||||
}
|
||||
private void getStats(LibvirtStoragePool pool) {
|
||||
Script statsScript = new Script("/bin/bash", s_logger);
|
||||
statsScript.add("-c");
|
||||
statsScript.add("stats=$(df --total " + pool.getLocalPath()
|
||||
+ " |grep total|awk '{print $2,$3}');echo $stats");
|
||||
final OutputInterpreter.OneLineParser statsParser = new OutputInterpreter.OneLineParser();
|
||||
String result = statsScript.execute(statsParser);
|
||||
if (result == null) {
|
||||
String stats = statsParser.getLine();
|
||||
if (stats != null && !stats.isEmpty()) {
|
||||
String sizes[] = stats.trim().split(" ");
|
||||
if (sizes.length == 2) {
|
||||
pool.setCapacity(Long.parseLong(sizes[0]) * 1024);
|
||||
pool.setUsed(Long.parseLong(sizes[1]) * 1024);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public KVMPhysicalDisk getPhysicalDisk(
|
||||
String volumeUuid, KVMStoragePool pool) {
|
||||
LibvirtStoragePool libvirtPool = (LibvirtStoragePool)pool;
|
||||
public KVMStoragePool getStoragePool(String uuid) {
|
||||
StoragePool storage = null;
|
||||
try {
|
||||
Connect conn = LibvirtConnection.getConnection();
|
||||
storage = conn.storagePoolLookupByUUIDString(uuid);
|
||||
|
||||
if (storage.getInfo().state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
|
||||
storage.create(0);
|
||||
}
|
||||
LibvirtStoragePoolDef spd = getStoragePoolDef(conn, storage);
|
||||
StoragePoolType type = null;
|
||||
if (spd.getPoolType() == LibvirtStoragePoolDef.poolType.NETFS
|
||||
|| spd.getPoolType() == LibvirtStoragePoolDef.poolType.DIR) {
|
||||
type = StoragePoolType.Filesystem;
|
||||
} else if (spd.getPoolType() == LibvirtStoragePoolDef.poolType.LOGICAL) {
|
||||
type = StoragePoolType.CLVM;
|
||||
}
|
||||
LibvirtStoragePool pool = new LibvirtStoragePool(uuid,
|
||||
storage.getName(), type, this, storage);
|
||||
pool.setLocalPath(spd.getTargetPath());
|
||||
|
||||
if (pool.getType() == StoragePoolType.CLVM) {
|
||||
pool.setCapacity(storage.getInfo().capacity);
|
||||
pool.setUsed(storage.getInfo().allocation);
|
||||
} else {
|
||||
getStats(pool);
|
||||
}
|
||||
return pool;
|
||||
} catch (LibvirtException e) {
|
||||
throw new CloudRuntimeException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public KVMPhysicalDisk getPhysicalDisk(String volumeUuid,
|
||||
KVMStoragePool pool) {
|
||||
LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
|
||||
|
||||
try {
|
||||
StorageVol vol = this.getVolume(libvirtPool.getPool(), volumeUuid);
|
||||
KVMPhysicalDisk disk;
|
||||
LibvirtStorageVolumeDef voldef = getStorageVolumeDef(libvirtPool.getPool().getConnect(), vol);
|
||||
LibvirtStorageVolumeDef voldef = getStorageVolumeDef(libvirtPool
|
||||
.getPool().getConnect(), vol);
|
||||
disk = new KVMPhysicalDisk(vol.getPath(), vol.getName(), pool);
|
||||
disk.setSize(vol.getInfo().allocation);
|
||||
disk.setVirtualSize(vol.getInfo().capacity);
|
||||
|
|
@ -443,7 +469,8 @@ public class LibvirtStorageAdaptor implements StorageAdaptor {
|
|||
}
|
||||
|
||||
@Override
|
||||
public KVMStoragePool createStoragePool(String name, String host, String path, StoragePoolType type) {
|
||||
public KVMStoragePool createStoragePool(String name, String host,
|
||||
String path, StoragePoolType type) {
|
||||
StoragePool sp = null;
|
||||
Connect conn = null;
|
||||
try {
|
||||
|
|
@ -465,7 +492,8 @@ public class LibvirtStorageAdaptor implements StorageAdaptor {
|
|||
if (sp == null) {
|
||||
if (type == StoragePoolType.NetworkFilesystem) {
|
||||
sp = createNfsStoragePool(conn, name, host, path);
|
||||
} else if (type == StoragePoolType.SharedMountPoint || type == StoragePoolType.Filesystem) {
|
||||
} else if (type == StoragePoolType.SharedMountPoint
|
||||
|| type == StoragePoolType.Filesystem) {
|
||||
sp = CreateSharedStoragePool(conn, name, host, path);
|
||||
} else if (type == StoragePoolType.CLVM) {
|
||||
sp = createCLVMStoragePool(conn, name, host, path);
|
||||
|
|
@ -479,7 +507,8 @@ public class LibvirtStorageAdaptor implements StorageAdaptor {
|
|||
}
|
||||
|
||||
LibvirtStoragePoolDef spd = getStoragePoolDef(conn, sp);
|
||||
LibvirtStoragePool pool = new LibvirtStoragePool(name, sp.getName(), type, this, sp);
|
||||
LibvirtStoragePool pool = new LibvirtStoragePool(name,
|
||||
sp.getName(), type, this, sp);
|
||||
pool.setLocalPath(spd.getTargetPath());
|
||||
|
||||
if (pool.getType() == StoragePoolType.CLVM) {
|
||||
|
|
@ -494,6 +523,7 @@ public class LibvirtStorageAdaptor implements StorageAdaptor {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteStoragePool(String uuid) {
|
||||
Connect conn = null;
|
||||
|
|
@ -522,9 +552,9 @@ public class LibvirtStorageAdaptor implements StorageAdaptor {
|
|||
}
|
||||
|
||||
@Override
|
||||
public KVMPhysicalDisk createPhysicalDisk(String name,
|
||||
KVMStoragePool pool, PhysicalDiskFormat format, long size) {
|
||||
LibvirtStoragePool libvirtPool = (LibvirtStoragePool)pool;
|
||||
public KVMPhysicalDisk createPhysicalDisk(String name, KVMStoragePool pool,
|
||||
PhysicalDiskFormat format, long size) {
|
||||
LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
|
||||
StoragePool virtPool = libvirtPool.getPool();
|
||||
LibvirtStorageVolumeDef.volFormat libvirtformat = null;
|
||||
if (format == PhysicalDiskFormat.QCOW2) {
|
||||
|
|
@ -533,11 +563,13 @@ public class LibvirtStorageAdaptor implements StorageAdaptor {
|
|||
libvirtformat = LibvirtStorageVolumeDef.volFormat.RAW;
|
||||
}
|
||||
|
||||
LibvirtStorageVolumeDef volDef = new LibvirtStorageVolumeDef(name, size, libvirtformat, null, null);
|
||||
LibvirtStorageVolumeDef volDef = new LibvirtStorageVolumeDef(name,
|
||||
size, libvirtformat, null, null);
|
||||
s_logger.debug(volDef.toString());
|
||||
try {
|
||||
StorageVol vol = virtPool.storageVolCreateXML(volDef.toString(), 0);
|
||||
KVMPhysicalDisk disk = new KVMPhysicalDisk(vol.getPath(), vol.getName(), pool);
|
||||
KVMPhysicalDisk disk = new KVMPhysicalDisk(vol.getPath(),
|
||||
vol.getName(), pool);
|
||||
disk.setFormat(format);
|
||||
disk.setSize(vol.getInfo().allocation);
|
||||
disk.setVirtualSize(vol.getInfo().capacity);
|
||||
|
|
@ -546,9 +578,10 @@ public class LibvirtStorageAdaptor implements StorageAdaptor {
|
|||
throw new CloudRuntimeException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deletePhysicalDisk(String uuid, KVMStoragePool pool) {
|
||||
LibvirtStoragePool libvirtPool = (LibvirtStoragePool)pool;
|
||||
LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
|
||||
try {
|
||||
StorageVol vol = this.getVolume(libvirtPool.getPool(), uuid);
|
||||
vol.delete(0);
|
||||
|
|
@ -563,24 +596,32 @@ public class LibvirtStorageAdaptor implements StorageAdaptor {
|
|||
public KVMPhysicalDisk createDiskFromTemplate(KVMPhysicalDisk template,
|
||||
String name, PhysicalDiskFormat format, long size,
|
||||
KVMStoragePool destPool) {
|
||||
KVMPhysicalDisk disk = destPool.createPhysicalDisk(UUID.randomUUID().toString(), format, template.getVirtualSize());
|
||||
KVMPhysicalDisk disk = destPool.createPhysicalDisk(UUID.randomUUID()
|
||||
.toString(), format, template.getVirtualSize());
|
||||
|
||||
if (format == PhysicalDiskFormat.QCOW2) {
|
||||
Script.runSimpleBashScript("qemu-img create -f " + template.getFormat() + " -b " + template.getPath() + " " + disk.getPath());
|
||||
Script.runSimpleBashScript("qemu-img create -f "
|
||||
+ template.getFormat() + " -b " + template.getPath() + " "
|
||||
+ disk.getPath());
|
||||
} else if (format == PhysicalDiskFormat.RAW) {
|
||||
Script.runSimpleBashScript("qemu-img convert -f " + template.getFormat()+ " -O raw " + template.getPath() + " " + disk.getPath());
|
||||
Script.runSimpleBashScript("qemu-img convert -f "
|
||||
+ template.getFormat() + " -O raw " + template.getPath()
|
||||
+ " " + disk.getPath());
|
||||
}
|
||||
return disk;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KVMPhysicalDisk createTemplateFromDisk(KVMPhysicalDisk disk, String name, PhysicalDiskFormat format, long size, KVMStoragePool destPool) {
|
||||
public KVMPhysicalDisk createTemplateFromDisk(KVMPhysicalDisk disk,
|
||||
String name, PhysicalDiskFormat format, long size,
|
||||
KVMStoragePool destPool) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<KVMPhysicalDisk> listPhysicalDisks(String storagePoolUuid, KVMStoragePool pool) {
|
||||
LibvirtStoragePool libvirtPool = (LibvirtStoragePool)pool;
|
||||
public List<KVMPhysicalDisk> listPhysicalDisks(String storagePoolUuid,
|
||||
KVMStoragePool pool) {
|
||||
LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
|
||||
StoragePool virtPool = libvirtPool.getPool();
|
||||
List<KVMPhysicalDisk> disks = new ArrayList<KVMPhysicalDisk>();
|
||||
try {
|
||||
|
|
@ -596,13 +637,16 @@ public class LibvirtStorageAdaptor implements StorageAdaptor {
|
|||
}
|
||||
|
||||
@Override
|
||||
public KVMPhysicalDisk copyPhysicalDisk(KVMPhysicalDisk disk,
|
||||
String name, KVMStoragePool destPool) {
|
||||
KVMPhysicalDisk newDisk = destPool.createPhysicalDisk(name, disk.getVirtualSize());
|
||||
public KVMPhysicalDisk copyPhysicalDisk(KVMPhysicalDisk disk, String name,
|
||||
KVMStoragePool destPool) {
|
||||
KVMPhysicalDisk newDisk = destPool.createPhysicalDisk(name,
|
||||
disk.getVirtualSize());
|
||||
String sourcePath = disk.getPath();
|
||||
String destPath = newDisk.getPath();
|
||||
|
||||
Script.runSimpleBashScript("qemu-img convert -f " + disk.getFormat() + " -O " + newDisk.getFormat() + " " + sourcePath + " " + destPath);
|
||||
Script.runSimpleBashScript("qemu-img convert -f " + disk.getFormat()
|
||||
+ " -O " + newDisk.getFormat() + " " + sourcePath + " "
|
||||
+ destPath);
|
||||
return newDisk;
|
||||
}
|
||||
|
||||
|
|
@ -645,7 +689,7 @@ public class LibvirtStorageAdaptor implements StorageAdaptor {
|
|||
|
||||
@Override
|
||||
public boolean refresh(KVMStoragePool pool) {
|
||||
LibvirtStoragePool libvirtPool = (LibvirtStoragePool)pool;
|
||||
LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
|
||||
StoragePool virtPool = libvirtPool.getPool();
|
||||
try {
|
||||
virtPool.refresh(0);
|
||||
|
|
@ -655,10 +699,9 @@ public class LibvirtStorageAdaptor implements StorageAdaptor {
|
|||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean deleteStoragePool(KVMStoragePool pool) {
|
||||
LibvirtStoragePool libvirtPool = (LibvirtStoragePool)pool;
|
||||
LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
|
||||
StoragePool virtPool = libvirtPool.getPool();
|
||||
try {
|
||||
virtPool.destroy();
|
||||
|
|
@ -671,6 +714,4 @@ public class LibvirtStorageAdaptor implements StorageAdaptor {
|
|||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,8 @@ public class LibvirtStoragePool implements KVMStoragePool {
|
|||
protected StorageAdaptor _storageAdaptor;
|
||||
protected StoragePool _pool;
|
||||
|
||||
public LibvirtStoragePool(String uuid, String name, StoragePoolType type, StorageAdaptor adaptor, StoragePool pool) {
|
||||
public LibvirtStoragePool(String uuid, String name, StoragePoolType type,
|
||||
StorageAdaptor adaptor, StoragePool pool) {
|
||||
this.uuid = uuid;
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
|
|
@ -74,13 +75,16 @@ public class LibvirtStoragePool implements KVMStoragePool {
|
|||
}
|
||||
|
||||
@Override
|
||||
public KVMPhysicalDisk createPhysicalDisk(String name, PhysicalDiskFormat format, long size) {
|
||||
return this._storageAdaptor.createPhysicalDisk(name, this, format, size);
|
||||
public KVMPhysicalDisk createPhysicalDisk(String name,
|
||||
PhysicalDiskFormat format, long size) {
|
||||
return this._storageAdaptor
|
||||
.createPhysicalDisk(name, this, format, size);
|
||||
}
|
||||
|
||||
@Override
|
||||
public KVMPhysicalDisk createPhysicalDisk(String name, long size) {
|
||||
return this._storageAdaptor.createPhysicalDisk(name, this, this.getDefaultFormat(), size);
|
||||
return this._storageAdaptor.createPhysicalDisk(name, this,
|
||||
this.getDefaultFormat(), size);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -10,20 +10,45 @@ import com.cloud.storage.Storage.StoragePoolType;
|
|||
public interface StorageAdaptor {
|
||||
|
||||
public KVMStoragePool getStoragePool(String uuid);
|
||||
public KVMPhysicalDisk getPhysicalDisk(String volumeUuid, KVMStoragePool pool);
|
||||
public KVMStoragePool createStoragePool(String name, String host, String path, StoragePoolType type);
|
||||
|
||||
public KVMPhysicalDisk getPhysicalDisk(String volumeUuid,
|
||||
KVMStoragePool pool);
|
||||
|
||||
public KVMStoragePool createStoragePool(String name, String host,
|
||||
String path, StoragePoolType type);
|
||||
|
||||
public boolean deleteStoragePool(String uuid);
|
||||
public KVMPhysicalDisk createPhysicalDisk(String name, KVMStoragePool pool, PhysicalDiskFormat format, long size);
|
||||
|
||||
public KVMPhysicalDisk createPhysicalDisk(String name, KVMStoragePool pool,
|
||||
PhysicalDiskFormat format, long size);
|
||||
|
||||
public boolean deletePhysicalDisk(String uuid, KVMStoragePool pool);
|
||||
public KVMPhysicalDisk createDiskFromTemplate(KVMPhysicalDisk template, String name, PhysicalDiskFormat format, long size, KVMStoragePool destPool);
|
||||
public KVMPhysicalDisk createTemplateFromDisk(KVMPhysicalDisk disk, String name, PhysicalDiskFormat format, long size, KVMStoragePool destPool);
|
||||
public List<KVMPhysicalDisk> listPhysicalDisks(String storagePoolUuid, KVMStoragePool pool);
|
||||
public KVMPhysicalDisk copyPhysicalDisk(KVMPhysicalDisk disk, String name, KVMStoragePool destPools);
|
||||
public KVMPhysicalDisk createDiskFromSnapshot(KVMPhysicalDisk snapshot, String snapshotName, String name, KVMStoragePool destPool);
|
||||
|
||||
public KVMPhysicalDisk createDiskFromTemplate(KVMPhysicalDisk template,
|
||||
String name, PhysicalDiskFormat format, long size,
|
||||
KVMStoragePool destPool);
|
||||
|
||||
public KVMPhysicalDisk createTemplateFromDisk(KVMPhysicalDisk disk,
|
||||
String name, PhysicalDiskFormat format, long size,
|
||||
KVMStoragePool destPool);
|
||||
|
||||
public List<KVMPhysicalDisk> listPhysicalDisks(String storagePoolUuid,
|
||||
KVMStoragePool pool);
|
||||
|
||||
public KVMPhysicalDisk copyPhysicalDisk(KVMPhysicalDisk disk, String name,
|
||||
KVMStoragePool destPools);
|
||||
|
||||
public KVMPhysicalDisk createDiskFromSnapshot(KVMPhysicalDisk snapshot,
|
||||
String snapshotName, String name, KVMStoragePool destPool);
|
||||
|
||||
public KVMStoragePool getStoragePoolByUri(String uri);
|
||||
|
||||
public KVMPhysicalDisk getPhysicalDiskFromURI(String uri);
|
||||
|
||||
public boolean refresh(KVMStoragePool pool);
|
||||
|
||||
public boolean deleteStoragePool(KVMStoragePool pool);
|
||||
|
||||
public boolean createFolder(String uuid, String path);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,295 +64,309 @@ import com.cloud.utils.script.Script;
|
|||
|
||||
/**
|
||||
* Serves vm data using embedded Jetty server
|
||||
*
|
||||
*
|
||||
*/
|
||||
@Local (value={VmDataServer.class})
|
||||
@Local(value = { VmDataServer.class })
|
||||
public class JettyVmDataServer implements VmDataServer {
|
||||
private static final Logger s_logger = Logger.getLogger(JettyVmDataServer.class);
|
||||
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 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 {
|
||||
public class VmDataServlet extends HttpServlet {
|
||||
|
||||
private static final long serialVersionUID = -1640031398971742349L;
|
||||
private static final long serialVersionUID = -1640031398971742349L;
|
||||
|
||||
JettyVmDataServer _vmDataServer;
|
||||
String _dataType; //userdata or meta-data
|
||||
JettyVmDataServer _vmDataServer;
|
||||
String _dataType; // userdata or meta-data
|
||||
|
||||
public VmDataServlet(JettyVmDataServer dataServer, String dataType) {
|
||||
this._vmDataServer = dataServer;
|
||||
this._dataType = dataType;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@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 metadataItem = req.getPathInfo();
|
||||
String requester = req.getRemoteAddr();
|
||||
resp.setContentType("text/html");
|
||||
resp.setStatus(HttpServletResponse.SC_OK);
|
||||
String data = null;
|
||||
if (metadataItem != null) {
|
||||
String[] path = metadataItem.split("/");
|
||||
if (path.length > 1) {
|
||||
metadataItem = path[1];
|
||||
}
|
||||
}
|
||||
|
||||
protected void handleUserData(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 data = null;
|
||||
if (metadataItem != null) {
|
||||
String[] path = metadataItem.split("/");
|
||||
if (path.length > 1) {
|
||||
metadataItem = path[1];
|
||||
}
|
||||
}
|
||||
if (metadataItem != null)
|
||||
data = _vmDataServer.getVmDataItem(requester, metadataItem);
|
||||
|
||||
if (metadataItem != null)
|
||||
data = _vmDataServer.getVmDataItem(requester, metadataItem);
|
||||
if (data != null) {
|
||||
resp.getWriter().print(data);
|
||||
} else {
|
||||
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
||||
resp.sendError(HttpServletResponse.SC_NOT_FOUND,
|
||||
"Request not found");
|
||||
}
|
||||
|
||||
if (data != null){
|
||||
resp.getWriter().print(data);
|
||||
} 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().print(
|
||||
_vmDataServer.getVmDataItem(requester, metadataItem));
|
||||
} else {
|
||||
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().print(_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");
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
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, vm);
|
||||
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);
|
||||
}
|
||||
|
||||
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, vm);
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
+ File.separator + 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;
|
||||
}
|
||||
}
|
||||
|
||||
public String getVmDataItem(String requester, String dataItem) {
|
||||
String vmName = _ipVmMap.get(requester);
|
||||
if (vmName == null){
|
||||
return null;
|
||||
}
|
||||
String vmDataFile = _vmDataDir + File.separator + vmName + File.separator + 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();
|
||||
|
||||
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 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);
|
||||
|
||||
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 });
|
||||
|
||||
_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)),
|
||||
"/*");
|
||||
|
||||
Context root = new Context(_jetty,"/latest",Context.SESSIONS);
|
||||
root.setResourceBase(_vmDataDir);
|
||||
root.addServlet(new ServletHolder(new VmDataServlet(this, USER_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);
|
||||
|
||||
ResourceHandler resource_handler = new ResourceHandler();
|
||||
resource_handler.setResourceBase("/var/lib/images/");
|
||||
_jetty.start();
|
||||
// _jetty.join();
|
||||
}
|
||||
|
||||
HandlerList handlers = new HandlerList();
|
||||
handlers.setHandlers(new Handler[] {root, resource_handler, new DefaultHandler() });
|
||||
_jetty.setHandler(handlers);
|
||||
@Override
|
||||
public boolean start() {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
_jetty.start();
|
||||
//_jetty.join();
|
||||
}
|
||||
@Override
|
||||
public boolean stop() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean start() {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
public String getName() {
|
||||
return "JettyVmDataServer";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean stop() {
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public Answer handleVmDataCommand(VmDataCommand cmd) {
|
||||
String vmDataDir = _vmDataDir + File.separator + cmd.getVmName();
|
||||
|
||||
Script.runSimpleBashScript("rm -rf " + vmDataDir);
|
||||
_fs.mkdirs(vmDataDir);
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "JettyVmDataServer";
|
||||
}
|
||||
for (String[] item : cmd.getVmData()) {
|
||||
try {
|
||||
_fs.create(vmDataDir, item[1]);
|
||||
String vmDataFile = vmDataDir + File.separator + item[1];
|
||||
byte[] data;
|
||||
if (item[2] != null) {
|
||||
if (item[1].equals("user-data")) {
|
||||
data = Base64.decodeBase64(item[2]);
|
||||
} else {
|
||||
data = item[2].getBytes();
|
||||
}
|
||||
if (data != null && data.length > 0) {
|
||||
FileOutputStream writer = new FileOutputStream(
|
||||
vmDataFile);
|
||||
writer.write(data);
|
||||
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 Answer handleVmDataCommand(VmDataCommand cmd) {
|
||||
String vmDataDir = _vmDataDir + File.separator + cmd.getVmName();
|
||||
@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);
|
||||
}
|
||||
|
||||
Script.runSimpleBashScript("rm -rf " + vmDataDir);
|
||||
_fs.mkdirs(vmDataDir);
|
||||
}
|
||||
|
||||
for (String [] item : cmd.getVmData()) {
|
||||
try {
|
||||
_fs.create(vmDataDir, item[1]);
|
||||
String vmDataFile = vmDataDir + File.separator + item[1];
|
||||
byte[] data;
|
||||
if (item[2] != null) {
|
||||
if (item[1].equals("user-data")) {
|
||||
data = Base64.decodeBase64(item[2]);
|
||||
} else {
|
||||
data = item[2].getBytes();
|
||||
}
|
||||
if (data != null && data.length > 0) {
|
||||
FileOutputStream writer = new FileOutputStream(vmDataFile);
|
||||
writer.write(data);
|
||||
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;
|
||||
Script.runSimpleBashScript("rm -rf " + vmDataDir);
|
||||
}
|
||||
@Override
|
||||
public void handleVmStopped(String vmName) {
|
||||
String vmDataDir = _vmDataDir + File.separator + vmName;
|
||||
Script.runSimpleBashScript("rm -rf " + vmDataDir);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,15 +24,15 @@ 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
|
||||
*
|
||||
* 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 Answer handleVmDataCommand(VmDataCommand cmd);
|
||||
|
||||
public void handleVmStarted(VirtualMachineTO vm);
|
||||
public void handleVmStarted(VirtualMachineTO vm);
|
||||
|
||||
public void handleVmStopped(String vmName);
|
||||
public void handleVmStopped(String vmName);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue