use of org.craftercms.studio.api.v2.dal.ClusterMember in project studio by craftercms.
the class RepositoryManagementServiceInternalImpl method isRemovableRemote.
private boolean isRemovableRemote(String siteId, String remoteName) {
boolean toRet = true;
if (StringUtils.startsWith(remoteName, CLUSTER_NODE_REMOTE_NAME_PREFIX)) {
RemoteRepository remoteRepository = getRemoteRepository(siteId, remoteName);
List<ClusterMember> clusterMembers = getClusterMembersByRemoteName(remoteName);
toRet = !(Objects.isNull(remoteRepository) && CollectionUtils.isNotEmpty(clusterMembers));
}
return toRet;
}
use of org.craftercms.studio.api.v2.dal.ClusterMember in project studio by craftercms.
the class ClusterManagementServiceImpl method removeMembers.
@Override
@HasPermission(type = DefaultPermission.class, action = "delete_cluster")
public boolean removeMembers(List<Long> memberIds) throws SiteNotFoundException {
List<ClusterMember> members = getAllMemebers();
boolean toRet = clusterManagementServiceInternal.removeMembers(memberIds);
if (toRet) {
SiteFeed siteFeed = siteService.getSite(studioConfiguration.getProperty(CONFIGURATION_GLOBAL_SYSTEM_SITE));
AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
auditLog.setSiteId(siteFeed.getId());
auditLog.setOperation(OPERATION_REMOVE_CLUSTER_NODE);
auditLog.setActorId(securityService.getCurrentUser());
auditLog.setPrimaryTargetId(siteFeed.getSiteId());
auditLog.setPrimaryTargetType(TARGET_TYPE_CLUSTER_NODE);
auditLog.setPrimaryTargetValue(siteFeed.getName());
List<AuditLogParameter> paramters = new ArrayList<AuditLogParameter>();
for (ClusterMember m : members) {
AuditLogParameter paramter = new AuditLogParameter();
paramter.setTargetId(Long.toString(m.getId()));
paramter.setTargetType(TARGET_TYPE_CLUSTER_NODE);
paramter.setTargetValue(m.getLocalAddress());
paramters.add(paramter);
}
auditLog.setParameters(paramters);
auditServiceInternal.insertAuditLog(auditLog);
}
return toRet;
}
use of org.craftercms.studio.api.v2.dal.ClusterMember in project studio by craftercms.
the class ClusterNodeRegistrationImpl method registerClusterNode.
@Override
public boolean registerClusterNode(ClusterMember clusterMember) {
ClusterMember existingRecord = clusterDao.getMemberByLocalAddress(clusterMember.getLocalAddress());
int result = 0;
if (Objects.isNull(existingRecord)) {
result = clusterDao.addMember(clusterMember);
} else {
clusterMember.setId(existingRecord.getId());
result = clusterDao.updateMember(clusterMember);
}
return result > 0;
}
use of org.craftercms.studio.api.v2.dal.ClusterMember in project studio by craftercms.
the class StudioClusterUtils method cloneGlobalRepository.
public boolean cloneGlobalRepository(List<ClusterMember> clusterNodes) throws CryptoException, ServiceLayerException {
// Clone from the first node in the cluster (it doesn't matter which one to clone from, so pick the first)
// we will eventually to catch up to the latest
boolean cloned = false;
int idx = 0;
String gitLockKey = GLOBAL_REPOSITORY_GIT_LOCK;
if (generalLockService.tryLock(gitLockKey)) {
try {
while (!cloned && idx < clusterNodes.size()) {
ClusterMember remoteNode = clusterNodes.get(idx++);
logger.debug("Cloning global repository from " + remoteNode.getLocalAddress());
// prepare a new folder for the cloned repository
Path siteSandboxPath = Paths.get(studioConfiguration.getProperty(StudioConfiguration.REPO_BASE_PATH), studioConfiguration.getProperty(StudioConfiguration.GLOBAL_REPO_PATH));
File localPath = siteSandboxPath.toFile();
localPath.delete();
// then clone
logger.debug("Cloning from " + remoteNode.getGitUrl() + " to " + localPath);
CloneCommand cloneCommand = Git.cloneRepository();
Git cloneResult = null;
try {
final Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
logger.debug("Add user credentials if provided");
configureAuthenticationForCommand(remoteNode, cloneCommand, tempKey);
String cloneUrl = remoteNode.getGitUrl().replace("/sites/{siteId}", "/global");
logger.debug("Executing clone command");
cloneResult = cloneCommand.setURI(cloneUrl).setRemote(remoteNode.getGitRemoteName()).setDirectory(localPath).setCloneAllBranches(true).call();
Files.deleteIfExists(tempKey);
cloned = true;
} catch (InvalidRemoteException e) {
logger.error("Invalid remote repository: " + remoteNode.getGitRemoteName() + " (" + remoteNode.getGitUrl() + ")", e);
} catch (TransportException e) {
if (StringUtils.endsWithIgnoreCase(e.getMessage(), "not authorized")) {
logger.error("Bad credentials or read only repository: " + remoteNode.getGitRemoteName() + " (" + remoteNode.getGitUrl() + ")", e);
} else {
logger.error("Remote repository not found: " + remoteNode.getGitRemoteName() + " (" + remoteNode.getGitUrl() + ")", e);
}
} catch (GitAPIException | IOException e) {
logger.error("Error while creating repository for site with path" + siteSandboxPath.toString(), e);
} finally {
if (cloneResult != null) {
cloneResult.close();
}
}
}
} finally {
generalLockService.unlock(gitLockKey);
}
} else {
logger.debug("Failed to get lock " + gitLockKey);
}
return cloned;
}
use of org.craftercms.studio.api.v2.dal.ClusterMember in project studio by craftercms.
the class StudioClusterGlobalRepoSyncTask method updateContent.
protected void updateContent(List<ClusterMember> clusterNodes) throws IOException, CryptoException, ServiceLayerException {
logger.debug("Update global repo");
Path siteSandboxPath = Paths.get(studioConfiguration.getProperty(StudioConfiguration.REPO_BASE_PATH), studioConfiguration.getProperty(StudioConfiguration.GLOBAL_REPO_PATH)).resolve(GIT_ROOT);
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repo = builder.setGitDir(siteSandboxPath.toFile()).readEnvironment().findGitDir().build();
try (Git git = new Git(repo)) {
logger.debug("Update content from each active cluster memeber");
for (ClusterMember remoteNode : clusterNodes) {
updateBranch(git, remoteNode);
}
} catch (GitAPIException e) {
logger.error("Error while syncing cluster node global repo content", e);
}
}
Aggregations