diff --git a/agent/src/com/cloud/agent/resource/consoleproxy/ConsoleProxyResource.java b/agent/src/com/cloud/agent/resource/consoleproxy/ConsoleProxyResource.java index ebc86633f8c..e9bab07ffdf 100644 --- a/agent/src/com/cloud/agent/resource/consoleproxy/ConsoleProxyResource.java +++ b/agent/src/com/cloud/agent/resource/consoleproxy/ConsoleProxyResource.java @@ -113,27 +113,30 @@ public class ConsoleProxyResource extends ServerResourceBase implements ServerRe String certificate = cmd.getCertificate(); //write the cert to /etc/cloud/consoleproxy/cert/ - String strDirectoy ="/etc/cloud/consoleproxy/cert/"; - boolean dirCreated = (new File(strDirectoy)).mkdir(); - if (dirCreated) { - s_logger.info("Directory: " + strDirectoy + " created"); + String strDirectoy = null; + boolean dirCreated = false; + + strDirectoy = "/etc/cloud/consoleproxy/cert/"; + dirCreated = (new File(strDirectoy)).mkdirs(); + + if (dirCreated) + { + s_logger.info("Directory: " + strDirectoy + " created"); - //copy cert to the dir - try { + //copy cert to the dir FileWriter fstream = new FileWriter("/etc/cloud/consoleproxy/cert/customcert"); BufferedWriter out = new BufferedWriter(fstream); out.write(certificate); //Close the output stream out.close(); - }catch (Exception e){ - s_logger.warn("Unable to write file to /etc/cloud/consoleproxy/cert/ on console proxy", e); - } + success = true; } - success = true; + return new Answer(cmd, success, "Cert string in the console proxy resource status:"); }catch (Exception e) { - s_logger.error("Unable to read the cert string in console proxy resource"); + s_logger.error("Unable to read the cert string in console proxy resource",e); + success = false; } return new Answer(cmd, success, "Cert string in the console proxy resource status:"); } diff --git a/core/src/com/cloud/host/dao/HostDao.java b/core/src/com/cloud/host/dao/HostDao.java index 78eeea89bfb..b68d7755245 100644 --- a/core/src/com/cloud/host/dao/HostDao.java +++ b/core/src/com/cloud/host/dao/HostDao.java @@ -135,7 +135,9 @@ public interface HostDao extends GenericDao { long getNextSequence(long hostId); - void loadDetails(HostVO host); + void loadDetails(HostVO host); + + HostVO findConsoleProxyHost(String name, Type type); } diff --git a/core/src/com/cloud/host/dao/HostDaoImpl.java b/core/src/com/cloud/host/dao/HostDaoImpl.java index 47d3134e0b6..3acc97db6a5 100644 --- a/core/src/com/cloud/host/dao/HostDaoImpl.java +++ b/core/src/com/cloud/host/dao/HostDaoImpl.java @@ -79,6 +79,7 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao protected final SearchBuilder UnmanagedDirectConnectSearch; protected final SearchBuilder MaintenanceCountSearch; protected final SearchBuilder ClusterSearch; + protected final SearchBuilder ConsoleProxyHostSearch; protected final Attribute _statusAttr; protected final Attribute _msIdAttr; @@ -154,6 +155,11 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao ClusterSearch = createSearchBuilder(); ClusterSearch.and("cluster", ClusterSearch.entity().getClusterId(), SearchCriteria.Op.EQ); ClusterSearch.done(); + + ConsoleProxyHostSearch = createSearchBuilder(); + ConsoleProxyHostSearch.and("name", ConsoleProxyHostSearch.entity().getName(), SearchCriteria.Op.EQ); + ConsoleProxyHostSearch.and("type", ConsoleProxyHostSearch.entity().getType(), SearchCriteria.Op.EQ); + ConsoleProxyHostSearch.done(); PodSearch = createSearchBuilder(); PodSearch.and("pod", PodSearch.entity().getPodId(), SearchCriteria.Op.EQ); @@ -442,7 +448,20 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao SearchCriteria sc = DcSearch.create("dc", dcId); return listBy(sc); } - + + @Override + public HostVO findConsoleProxyHost(String name, Type type) { + SearchCriteria sc = ConsoleProxyHostSearch.create(); + sc.setParameters("name", name); + sc.setParameters("type", type); + ListhostList = listBy(sc); + + if(hostList==null || hostList.size() == 0) + return null; + else + return hostList.get(0); + } + public List listByHostPod(long podId) { SearchCriteria sc = PodSearch.create("pod", podId); return listBy(sc); diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index d1df3adbf23..bcd2a37d0e2 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -174,6 +174,7 @@ import com.cloud.dc.dao.PodVlanMapDao; import com.cloud.dc.dao.VlanDao; import com.cloud.domain.DomainVO; import com.cloud.domain.dao.DomainDao; +import com.cloud.event.EventState; import com.cloud.event.EventTypes; import com.cloud.event.EventUtils; import com.cloud.event.EventVO; @@ -6931,6 +6932,18 @@ public class ManagementServerImpl implements ManagementServer { } return version; } + + private Long saveScheduledEvent(Long userId, Long accountId, String type, String description) + { + EventVO event = new EventVO(); + event.setUserId(userId); + event.setAccountId(accountId); + event.setType(type); + event.setState(EventState.Scheduled); + event.setDescription("Scheduled async job for "+description); + event = _eventDao.persist(event); + return event.getId(); + } @Override public boolean uploadCertificate(UploadCustomCertificateCmd cmd) { @@ -6940,32 +6953,36 @@ public class ManagementServerImpl implements ManagementServer { if (certVOId!=null && certVOId!=0) { //certficate uploaded to db successfully - //get a list of all hosts from host table - List hosts = _hostDao.listAll(); + //get a list of all Console proxies from the cp table + List cpList = _consoleProxyDao.listAll(); - List consoleProxyList = new ArrayList(); - - //find the console proxies, and send the command to them - for(HostVO host : hosts) { - if(host.getType().equals(com.cloud.host.Host.Type.ConsoleProxy)){ - consoleProxyList.add(host); - } - } - - for(HostVO consoleProxy : consoleProxyList){ + for(ConsoleProxyVO cp : cpList) + { + HostVO cpHost = _hostDao.findConsoleProxyHost(cp.getName(), com.cloud.host.Host.Type.ConsoleProxy); + //now send a command to each console proxy UpdateCertificateCommand certCmd = new UpdateCertificateCommand(_certDao.findById(certVOId).getCertificate()); try { - Answer updateCertAns = _agentMgr.send(consoleProxy.getId(), certCmd); + Answer updateCertAns = _agentMgr.send(cpHost.getId(), certCmd); + + if(updateCertAns.getResult() == true) + { + //we have the cert copied over on cpvm + long eventId = saveScheduledEvent(User.UID_SYSTEM, Account.ACCOUNT_ID_SYSTEM, EventTypes.EVENT_PROXY_REBOOT, "stopping console proxy with Id: "+cp.getId()); + boolean cpReboot = _consoleProxyMgr.rebootProxy(cp.getId(), eventId); + //when cp reboots, the context will be reinit with the new cert + } } catch (AgentUnavailableException e) { s_logger.warn("Unable to send command to the console proxy resource", e); } catch (OperationTimedoutException e) { s_logger.warn("Unable to send command to the console proxy resource", e); } + } + } - return false; + return true; } @Override