use of org.craftercms.studio.api.v2.utils.StudioConfiguration.SANDBOX_PATH in project studio by craftercms.
the class StudioClusterSandboxRepoSyncTask method addRemotes.
protected void addRemotes(String siteId, List<ClusterMember> clusterNodes) throws InvalidRemoteUrlException, ServiceLayerException {
Map<String, String> existingRemotes = remotesMap.get(siteId);
logger.debug("Add cluster members as remotes to local sandbox repository");
for (ClusterMember member : clusterNodes) {
if (existingRemotes != null && existingRemotes.containsKey(member.getGitRemoteName())) {
continue;
}
try {
if (existingRemotes == null) {
existingRemotes = new HashMap<String, String>();
remotesMap.put(siteId, existingRemotes);
}
String remoteUrl = member.getGitUrl().replace("{siteId}", siteId) + "/" + studioConfiguration.getProperty(SANDBOX_PATH);
addRemoteRepository(siteId, member, remoteUrl);
existingRemotes.put(member.getGitRemoteName(), StringUtils.EMPTY);
} catch (IOException e) {
logger.error("Failed to open repository for site " + siteId, e);
}
}
}
use of org.craftercms.studio.api.v2.utils.StudioConfiguration.SANDBOX_PATH in project studio by craftercms.
the class StudioClusterSandboxRepoSyncTask method createSiteFromRemote.
protected boolean createSiteFromRemote(String siteId, long localNodeId, List<ClusterSiteRecord> clusterSiteRecords) 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 = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, siteId);
if (generalLockService.tryLock(gitLockKey)) {
try {
Optional<ClusterSiteRecord> csr = clusterSiteRecords.stream().filter(x -> StringUtils.equals(x.getState(), STATE_CREATED) && !(x.getClusterNodeId() == localNodeId)).findFirst();
if (csr.isPresent()) {
ClusterMember remoteNode = clusterDao.getMemberById(csr.get().getClusterNodeId());
logger.debug("Cloning " + SANDBOX + " repository for site " + siteId + " from " + remoteNode.getLocalAddress());
// prepare a new folder for the cloned repository
Path siteSandboxPath = buildRepoPath(siteId);
File localPath = siteSandboxPath.toFile();
// then clone
logger.debug("Cloning from " + remoteNode.getGitUrl() + " to " + localPath);
CloneCommand cloneCommand = Git.cloneRepository();
Git cloneResult = null;
try {
if (localPath.exists()) {
FileUtils.forceDelete(localPath);
}
final Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
logger.debug("Add user credentials if provided");
studioClusterUtils.configureAuthenticationForCommand(remoteNode, cloneCommand, tempKey);
String cloneUrl = remoteNode.getGitUrl().replace("{siteId}", siteId);
cloneUrl = cloneUrl + "/" + studioConfiguration.getProperty(SANDBOX_PATH);
logger.debug("Executing clone command");
Git gitClone = cloneResult = cloneCommand.setURI(cloneUrl).setRemote(remoteNode.getGitRemoteName()).setDirectory(localPath).setCloneAllBranches(true).call();
Files.deleteIfExists(tempKey);
cloned = validateRepository(gitClone.getRepository());
} 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, e);
} finally {
if (cloneResult != null) {
cloneResult.close();
}
}
}
} finally {
generalLockService.unlock(gitLockKey);
}
} else {
logger.debug("Failed to get lock " + gitLockKey);
}
return cloned;
}
Aggregations