if (serviceCtxt == null) {
String msg = "Not a registered service: domain - " + domainName
+ ", sub domain - " + subDomainName;
log.fatal(msg);
throw new CloudControllerException(msg);
}
// load Cartridge
serviceCtxt.setCartridge(loadCartridge(serviceCtxt.getCartridgeType(),
serviceCtxt.getPayload(), dataHolder
.getCartridges()));
if (serviceCtxt.getCartridge() == null) {
String msg = "There's no registered Cartridge found. Domain - "
+ domainName + ", sub domain - " + subDomainName;
log.fatal(msg);
throw new CloudControllerException(msg);
}
if (serviceCtxt.getCartridge().getIaases().isEmpty()) {
String msg = "There's no registered IaaSes found for Cartridge type: "
+ serviceCtxt.getCartridge().getType();
log.fatal(msg);
throw new CloudControllerException(msg);
}
// sort the IaasProviders according to scale up order
Collections.sort(serviceCtxt.getCartridge().getIaases(),
IaasProviderComparator.ascending(IaasProviderComparator
.getComparator(IaasProviderComparator.SCALE_UP_SORT)));
for (IaasProvider iaas : serviceCtxt.getCartridge().getIaases()) {
IaasContext ctxt = null;
if ((ctxt = serviceCtxt.getIaasContext(iaas.getType())) == null) {
ctxt = serviceCtxt.addIaasContext(iaas.getType());
}
if (iaas.getMaxInstanceLimit() > dataHolder.getActiveInstanceCount(iaas.getType())) {
try {
iaas.getIaas().setDynamicPayload(iaas);
// get the ComputeService
computeService = iaas.getComputeService();
// corresponding Template
template = iaas.getTemplate();
if (template == null) {
String msg = "Failed to start an instance in "
+ iaas.getType()
+ ". Reason : Template is null. You have not specify a matching service "
+ "element in the configuration file of Autoscaler.\n Hence, will try to "
+ "start in another IaaS if available.";
log.error(msg);
continue;
}
// set instance name as the host name
// template.getOptions().userMetadata("Name",
// serviceCtxt.getHostName());
// template.getOptions().as(TemplateOptions.class).userMetadata("Name",
// serviceCtxt.getHostName());
// generate the group id from domain name and sub domain
// name.
// Should have lower-case ASCII letters, numbers, or dashes.
// Should have a length between 3-15
String str = domainName.concat("-" + subDomainName)
.substring(0, 10);
String group = str.replaceAll("[^a-z0-9-]", "");
NodeMetadata node;
// create and start a node
Set<? extends NodeMetadata> nodes = computeService
.createNodesInGroup(group, 1, template);
node = nodes.iterator().next();
String autoAssignIpProp = iaas
.getProperty(CloudControllerConstants.AUTO_ASSIGN_IP_PROPERTY);
// acquire the lock
lock.lock();
try {
// reset ip
ip = "";
// default behavior is autoIpAssign=false
if (autoAssignIpProp == null
|| (autoAssignIpProp != null && autoAssignIpProp
.equals("false"))) {
// allocate an IP address - manual IP assigning mode
ip = iaas.getIaas().associateAddress(iaas, node);
}
if (ip.isEmpty()
&& node.getPublicAddresses() != null
&& node.getPublicAddresses().iterator()
.hasNext()) {
ip = node.getPublicAddresses().iterator().next();
}
// if not public IP is assigned, we're using private IP
if (ip.isEmpty()
&& node.getPrivateAddresses() != null
&& node.getPrivateAddresses().iterator()
.hasNext()) {
ip = node.getPrivateAddresses().iterator().next();
}
if (node.getId() == null) {
String msg = "Node id of the starting instance is null.\n"
+ node.toString();
log.fatal(msg);
throw new CloudControllerException(msg);
}
// add node ID
ctxt.addNodeId(node.getId());
ctxt.addNodeToPublicIp(node.getId(), ip);