Skip to content

Commit bced9a6

Browse files
Edison SuEdison Su
authored andcommitted
advanced startup command
1 parent 6297820 commit bced9a6

9 files changed

Lines changed: 276 additions & 111 deletions

File tree

agent/conf/dummy.agent.properties

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
resource=com.cloud.agent.resource.DummyResource
2+
host.mac.address=06\:95\:a4\:00\:03\:ee
3+
guest.network.device=cloudbr0
4+
type=Routing
5+
local.storage.uuid=8c348eee-ea56-4e4f-9397-00f33340927a
6+
port=8250
7+
host=10.216.132.110
8+
pod=1
9+
LibvirtComputingResource.id=1
10+
cluster=1
11+
public.network.device=cloudbr0
12+
private.network.device=cloudbr0
13+
DummyResource.id=11
14+
developer=True
15+
zone=1
16+
domr.scripts.dir=scripts/network/domr/kvm
17+
workers=5

agent/scripts/_run.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#script to start multiple agents on one host
2+
num=$1
3+
port=8787
4+
while [ $num -gt 0 ]
5+
do
6+
let "port=$port + $num"
7+
java -Xrunjdwp:transport=dt_socket,address=$port,server=y,suspend=n -cp ./'*' com.cloud.agent.AgentShell &
8+
let "num=$num - 1"
9+
done

agent/src/com/cloud/agent/Agent.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ public int value() {
118118
AtomicInteger _inProgress = new AtomicInteger();
119119

120120
StartupTask _startup = null;
121+
long _startupWaitDefault = 180000;
122+
long _startupWait = _startupWaitDefault;
121123
boolean _reconnectAllowed = true;
122124
//For time sentitive task, e.g. PingTask
123125
private ThreadPoolExecutor _ugentTaskPool;
@@ -283,7 +285,7 @@ public void scheduleWatch(final Link link, final Request request, final long del
283285
s_logger.debug("Adding a watch list");
284286
}
285287
final WatchTask task = new WatchTask(link, request, this);
286-
_timer.schedule(task, delay, period);
288+
_timer.schedule(task, 0, period);
287289
_watchList.add(task);
288290
}
289291
}
@@ -316,7 +318,7 @@ public void sendStartup(Link link) {
316318
}
317319
synchronized (this) {
318320
_startup = new StartupTask(link);
319-
_timer.schedule(_startup, 180000);
321+
_timer.schedule(_startup, _startupWait);
320322
}
321323
try {
322324
link.send(request.toBytes());
@@ -780,6 +782,7 @@ public synchronized boolean cancel() {
780782
// TimerTask.cancel may fail depends on the calling context
781783
if (!cancelled) {
782784
cancelled = true;
785+
_startupWait = _startupWaitDefault;
783786
s_logger.debug("Startup task cancelled");
784787
return super.cancel();
785788
}
@@ -794,6 +797,7 @@ public synchronized void run() {
794797
}
795798
cancelled = true;
796799
_startup = null;
800+
_startupWait = _startupWaitDefault *2;
797801
reconnect(_link);
798802
}
799803
}

agent/src/com/cloud/agent/AgentShell.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.List;
3636
import java.util.Map;
3737
import java.util.Properties;
38+
import java.util.UUID;
3839

3940
import javax.naming.ConfigurationException;
4041

@@ -351,7 +352,7 @@ protected boolean parseCommand(final String[] args) throws ConfigurationExceptio
351352
if (!developer) {
352353
throw new ConfigurationException("Unable to find the guid");
353354
}
354-
_guid = MacAddress.getMacAddress().toString(":");
355+
_guid = UUID.randomUUID().toString();
355356
}
356357

357358
return true;
@@ -529,8 +530,12 @@ private void run(String[] args) {
529530
init(args);
530531

531532
String instance = getProperty(null, "instance");
532-
if (instance == null) {
533-
instance = "";
533+
if (instance == null) {
534+
if (Boolean.parseBoolean(getProperty(null, "developer"))) {
535+
instance = UUID.randomUUID().toString();
536+
} else {
537+
instance = "";
538+
}
534539
} else {
535540
instance += ".";
536541
}

agent/src/com/cloud/agent/resource/DummyResource.java

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,50 @@
1717
*/
1818
package com.cloud.agent.resource;
1919

20+
import java.util.ArrayList;
21+
import java.util.HashMap;
22+
import java.util.List;
2023
import java.util.Map;
24+
import java.util.UUID;
2125

2226
import javax.ejb.Local;
2327

2428
import com.cloud.agent.IAgentControl;
2529
import com.cloud.agent.api.Answer;
30+
import com.cloud.agent.api.CheckNetworkAnswer;
31+
import com.cloud.agent.api.CheckNetworkCommand;
2632
import com.cloud.agent.api.Command;
2733
import com.cloud.agent.api.PingCommand;
2834
import com.cloud.agent.api.StartupCommand;
35+
import com.cloud.agent.api.StartupRoutingCommand;
36+
import com.cloud.agent.api.StartupStorageCommand;
37+
import com.cloud.agent.api.StoragePoolInfo;
38+
import com.cloud.agent.api.StartupRoutingCommand.VmState;
2939
import com.cloud.host.Host;
3040
import com.cloud.host.Host.Type;
41+
import com.cloud.hypervisor.Hypervisor.HypervisorType;
42+
import com.cloud.network.Networks.RouterPrivateIpStrategy;
3143
import com.cloud.resource.ServerResource;
44+
import com.cloud.storage.Storage;
45+
import com.cloud.storage.Storage.StoragePoolType;
3246

3347
@Local(value={ServerResource.class})
3448
public class DummyResource implements ServerResource {
3549
String _name;
3650
Host.Type _type;
3751
boolean _negative;
3852
IAgentControl _agentControl;
53+
private Map<String, Object> _params;
3954

4055
@Override
4156
public void disconnected() {
4257
}
4358

4459
@Override
4560
public Answer executeRequest(Command cmd) {
61+
if (cmd instanceof CheckNetworkCommand) {
62+
return new CheckNetworkAnswer((CheckNetworkCommand)cmd, true, null);
63+
}
4664
System.out.println("Received Command: " + cmd.toString());
4765
Answer answer = new Answer(cmd, !_negative, "response");
4866
System.out.println("Replying with: " + answer.toString());
@@ -58,10 +76,98 @@ public PingCommand getCurrentStatus(long id) {
5876
public Type getType() {
5977
return _type;
6078
}
79+
80+
protected String getConfiguredProperty(String key, String defaultValue) {
81+
String val = (String)_params.get(key);
82+
return val==null?defaultValue:val;
83+
}
84+
85+
protected Long getConfiguredProperty(String key, Long defaultValue) {
86+
String val = (String)_params.get(key);
87+
88+
if (val != null) {
89+
Long result = Long.parseLong(val);
90+
return result;
91+
}
92+
return defaultValue;
93+
}
94+
95+
protected List<Object> getHostInfo() {
96+
final ArrayList<Object> info = new ArrayList<Object>();
97+
long speed = getConfiguredProperty("cpuspeed", 4000L) ;
98+
long cpus = getConfiguredProperty("cpus", 4L);
99+
long ram = getConfiguredProperty("memory", 16000L*1024L*1024L);
100+
long dom0ram = Math.min(ram/10, 768*1024*1024L);
101+
102+
103+
String cap = getConfiguredProperty("capabilities", "hvm");
104+
info.add((int)cpus);
105+
info.add(speed);
106+
info.add(ram);
107+
info.add(cap);
108+
info.add(dom0ram);
109+
return info;
110+
111+
}
112+
113+
protected void fillNetworkInformation(final StartupCommand cmd) {
114+
115+
cmd.setPrivateIpAddress((String)getConfiguredProperty("private.ip.address", "127.0.0.1"));
116+
cmd.setPrivateMacAddress((String)getConfiguredProperty("private.mac.address", "8A:D2:54:3F:7C:C3"));
117+
cmd.setPrivateNetmask((String)getConfiguredProperty("private.ip.netmask", "255.255.255.0"));
118+
119+
cmd.setStorageIpAddress((String)getConfiguredProperty("private.ip.address", "127.0.0.1"));
120+
cmd.setStorageMacAddress((String)getConfiguredProperty("private.mac.address", "8A:D2:54:3F:7C:C3"));
121+
cmd.setStorageNetmask((String)getConfiguredProperty("private.ip.netmask", "255.255.255.0"));
122+
cmd.setGatewayIpAddress((String)getConfiguredProperty("gateway.ip.address", "127.0.0.1"));
123+
124+
}
125+
126+
private Map<String, String> getVersionStrings() {
127+
Map<String, String> result = new HashMap<String, String>();
128+
String hostOs = (String) _params.get("Host.OS");
129+
String hostOsVer = (String) _params.get("Host.OS.Version");
130+
String hostOsKernVer = (String) _params.get("Host.OS.Kernel.Version");
131+
result.put("Host.OS", hostOs==null?"Fedora":hostOs);
132+
result.put("Host.OS.Version", hostOsVer==null?"14":hostOsVer);
133+
result.put("Host.OS.Kernel.Version", hostOsKernVer==null?"2.6.35.6-45.fc14.x86_64":hostOsKernVer);
134+
return result;
135+
}
136+
137+
protected StoragePoolInfo initializeLocalStorage() {
138+
String hostIp = (String)getConfiguredProperty("private.ip.address", "127.0.0.1");
139+
String localStoragePath = (String)getConfiguredProperty("local.storage.path", "/mnt");
140+
String lh = hostIp + localStoragePath;
141+
String uuid = UUID.nameUUIDFromBytes(lh.getBytes()).toString();
142+
143+
String capacity = (String)getConfiguredProperty("local.storage.capacity", "1000000000");
144+
String available = (String)getConfiguredProperty("local.storage.avail", "10000000");
145+
146+
return new StoragePoolInfo(uuid, hostIp, localStoragePath,
147+
localStoragePath, StoragePoolType.Filesystem,
148+
Long.parseLong(capacity), Long.parseLong(available));
61149

150+
}
151+
62152
@Override
63153
public StartupCommand[] initialize() {
64-
return new StartupCommand[] {new StartupCommand(Host.Type.Storage)};
154+
Map<String, VmState> changes = null;
155+
156+
157+
final List<Object> info = getHostInfo();
158+
159+
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);
160+
fillNetworkInformation(cmd);
161+
cmd.getHostDetails().putAll(getVersionStrings());
162+
cmd.setCluster(getConfiguredProperty("cluster", "1"));
163+
StoragePoolInfo pi = initializeLocalStorage();
164+
StartupStorageCommand sscmd = new StartupStorageCommand();
165+
sscmd.setPoolInfo(pi);
166+
sscmd.setGuid(pi.getUuid());
167+
sscmd.setDataCenter((String)_params.get("zone"));
168+
sscmd.setResourceType(Storage.StorageResourceType.STORAGE_POOL);
169+
170+
return new StartupCommand[]{cmd, sscmd};
65171
}
66172

67173
@Override
@@ -73,9 +179,13 @@ public boolean configure(String name, Map<String, Object> params) {
73179

74180
value = (String)params.get("negative.reply");
75181
_negative = Boolean.parseBoolean(value);
76-
182+
setParams(params);
77183
return true;
78184
}
185+
186+
public void setParams(Map<String, Object> _params) {
187+
this._params = _params;
188+
}
79189

80190
@Override
81191
public String getName() {

api/src/com/cloud/agent/api/ReadyCommand.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package com.cloud.agent.api;
1919

2020
public class ReadyCommand extends Command {
21-
21+
private String _details;
2222
public ReadyCommand() {
2323
super();
2424
}
@@ -30,6 +30,14 @@ public ReadyCommand(Long dcId) {
3030
this.dcId = dcId;
3131
}
3232

33+
public void setDetails(String details) {
34+
_details = details;
35+
}
36+
37+
public String getDetails() {
38+
return _details;
39+
}
40+
3341
public Long getDataCenterId() {
3442
return dcId;
3543
}

0 commit comments

Comments
 (0)