/** * This method updates the password of all hosts in a given cluster. */ @Override @DB public boolean updateClusterPassword(final UpdateHostPasswordCmd command) { if (command.getClusterId() == null) { throw new InvalidParameterValueException("You should provide a cluster id."); } final ClusterVO cluster = ApiDBUtils.findClusterById(command.getClusterId()); if (cluster == null || !supportedHypervisors.contains(cluster.getHypervisorType())) { throw new InvalidParameterValueException("This operation is not supported for this hypervisor type"); } return updateHostsInCluster(command); }
private boolean updateHostsInCluster(final UpdateHostPasswordCmd command) { // get all the hosts in this cluster final List<HostVO> hosts = _resourceMgr.listAllHostsInCluster(command.getClusterId()); Transaction.execute(new TransactionCallbackNoReturn() { @Override public void doInTransactionWithoutResult(final TransactionStatus status) { for (final HostVO h : hosts) { if (s_logger.isDebugEnabled()) { s_logger.debug("Changing password for host name = " + h.getName()); } // update password for this host final DetailVO nv = _detailsDao.findDetail(h.getId(), ApiConstants.USERNAME); if (nv.getValue().equals(command.getUsername())) { final DetailVO nvp = _detailsDao.findDetail(h.getId(), ApiConstants.PASSWORD); nvp.setValue(DBEncryptionUtil.encrypt(command.getPassword())); _detailsDao.persist(nvp); } else { // if one host in the cluster has diff username then // rollback to maintain consistency throw new InvalidParameterValueException("The username is not same for all hosts, please modify passwords for individual hosts."); } } } }); return true; }
@Override public void execute() { if (getClusterId() == null) { _mgr.updateHostPassword(this); _resourceService.updateHostPassword(this); } else { _mgr.updateClusterPassword(this); _resourceService.updateClusterPassword(this); } setResponseObject(new SuccessResponse(getCommandName())); } }
@Override public boolean updateClusterPassword(final UpdateHostPasswordCmd command) { final boolean shouldUpdateHostPasswd = command.getUpdatePasswdOnHost(); // get agents for the cluster final List<HostVO> hosts = listAllHostsInCluster(command.getClusterId()); for (final HostVO host : hosts) { try { final Boolean result = propagateResourceEvent(host.getId(), ResourceState.Event.UpdatePassword); if (result != null) { return result; } } catch (final AgentUnavailableException e) { s_logger.error("Agent is not availbale!", e); } if (shouldUpdateHostPasswd) { final boolean isUpdated = doUpdateHostPassword(host.getId()); if (!isUpdated) { throw new CloudRuntimeException("CloudStack failed to update the password of the Host with UUID / ID ==> " + host.getUuid() + " / " + host.getId() + ". Please make sure you are still able to connect to your hosts."); } } } return true; }