more changes for the console proxy custom certificate part; some code cleanup as well as event generation with console proxy reboot logic

This commit is contained in:
abhishek 2010-10-25 17:51:39 -07:00
parent f0fd34c5b4
commit 822ee2d899
4 changed files with 68 additions and 27 deletions

View File

@ -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:");
}

View File

@ -135,7 +135,9 @@ public interface HostDao extends GenericDao<HostVO, Long> {
long getNextSequence(long hostId);
void loadDetails(HostVO host);
void loadDetails(HostVO host);
HostVO findConsoleProxyHost(String name, Type type);
}

View File

@ -79,6 +79,7 @@ public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao
protected final SearchBuilder<HostVO> UnmanagedDirectConnectSearch;
protected final SearchBuilder<HostVO> MaintenanceCountSearch;
protected final SearchBuilder<HostVO> ClusterSearch;
protected final SearchBuilder<HostVO> ConsoleProxyHostSearch;
protected final Attribute _statusAttr;
protected final Attribute _msIdAttr;
@ -154,6 +155,11 @@ public class HostDaoImpl extends GenericDaoBase<HostVO, Long> 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<HostVO, Long> implements HostDao
SearchCriteria<HostVO> sc = DcSearch.create("dc", dcId);
return listBy(sc);
}
@Override
public HostVO findConsoleProxyHost(String name, Type type) {
SearchCriteria<HostVO> sc = ConsoleProxyHostSearch.create();
sc.setParameters("name", name);
sc.setParameters("type", type);
List<HostVO>hostList = listBy(sc);
if(hostList==null || hostList.size() == 0)
return null;
else
return hostList.get(0);
}
public List<HostVO> listByHostPod(long podId) {
SearchCriteria<HostVO> sc = PodSearch.create("pod", podId);
return listBy(sc);

View File

@ -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<HostVO> hosts = _hostDao.listAll();
//get a list of all Console proxies from the cp table
List<ConsoleProxyVO> cpList = _consoleProxyDao.listAll();
List<HostVO> consoleProxyList = new ArrayList<HostVO>();
//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