HostVO pxe = pxes.get(0);
/*
* prepare() will check if current account has right for creating
* template
*/
TemplateAdapter adapter = _adapters.get(TemplateAdapterType.BareMetal.getName());
Long userId = UserContext.current().getCallerUserId();
userId = (userId == null ? User.UID_SYSTEM : userId);
AccountVO account = _accountDao.findById(vm.getAccountId());
try {
TemplateProfile tmplProfile;
tmplProfile = adapter.prepare(false, userId, cmd.getTemplateName(), cmd.getDisplayText(), cmd.getBits(), false, false, cmd.getUrl(), cmd.isPublic(), cmd.isFeatured(), false,
"BareMetal", cmd.getOsTypeId(), pxe.getDataCenterId(), HypervisorType.BareMetal, account.getAccountName(), account.getDomainId(), "0", true, cmd.getDetails());
if (!_pxeMgr.prepareCreateTemplate(_pxeMgr.getPxeServerType(pxe), pxe.getId(), vm, cmd.getUrl())) {
throw new Exception("Prepare PXE boot file for host " + hostId + " failed");
}
IpmISetBootDevCommand setBootDev = new IpmISetBootDevCommand(IpmISetBootDevCommand.BootDev.pxe);
Answer ans = _agentMgr.send(hostId, setBootDev);
if (!ans.getResult()) {
throw new Exception("Set host " + hostId + " to PXE boot failed");
}
IpmiBootorResetCommand boot = new IpmiBootorResetCommand();
ans = _agentMgr.send(hostId, boot);
if (!ans.getResult()) {
throw new Exception("Boot/Reboot host " + hostId + " failed");
}
VMTemplateVO tmpl = adapter.create(tmplProfile);
s_logger.debug("Create baremetal template for host " + hostId + " successfully, template id:" + tmpl.getId());
return tmpl;
} catch (Exception e) {
s_logger.debug("Create baremetal tempalte for host " + hostId + " failed", e);
throw new CloudRuntimeException(e.getMessage());