use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.
the class StudioClockExecutor method executeTasks.
private void executeTasks() {
for (Job job : globalTasks) {
job.execute();
}
cleanupDeletedSites();
List<String> sites = siteService.getAllCreatedSites();
for (String site : sites) {
taskExecutor.execute(new Runnable() {
@Override
public void run() {
String tasksLock = STUDIO_CLOCK_EXECUTOR_SITE_LOCK.replaceAll(PATTERN_SITE, site);
if (generalLockService.tryLock(tasksLock)) {
try {
for (SiteJob siteTask : siteTasks) {
siteTask.execute(site);
}
} finally {
generalLockService.unlock(tasksLock);
}
}
}
});
}
}
use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.
the class GitContentRepository method copyContent.
@Override
public String copyContent(String site, String fromPath, String toPath) {
String commitId = null;
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
generalLockService.lock(gitLockKey);
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX)) {
Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
String gitFromPath = helper.getGitPath(fromPath);
String gitToPath = helper.getGitPath(toPath);
try (Git git = new Git(repo)) {
Path sourcePath = Paths.get(repo.getDirectory().getParent(), fromPath);
File sourceFile = sourcePath.toFile();
Path targetPath = Paths.get(repo.getDirectory().getParent(), toPath);
File targetFile = targetPath.toFile();
// Check if we're copying a single file or whole subtree
FileUtils.copyDirectory(sourceFile, targetFile);
// The operation is done on disk, now it's time to commit
git.add().addFilepattern(gitToPath).call();
CommitCommand commitCommand = git.commit().setOnly(gitFromPath).setOnly(gitToPath).setAuthor(helper.getCurrentUserIdent()).setCommitter(helper.getCurrentUserIdent()).setMessage(helper.getCommitMessage(REPO_COPY_CONTENT_COMMIT_MESSAGE).replaceAll(PATTERN_FROM_PATH, fromPath).replaceAll(PATTERN_TO_PATH, toPath));
RevCommit commit = retryingRepositoryOperationFacade.call(commitCommand);
commitId = commit.getName();
}
}
} catch (IOException | GitAPIException | ServiceLayerException | UserNotFoundException | CryptoException e) {
logger.error("Error while copying content for site: " + site + " fromPath: " + fromPath + " toPath: " + toPath + " newName: ");
} finally {
generalLockService.unlock(gitLockKey);
}
return commitId;
}
use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.
the class GitContentRepository method pullFromRemote.
@Override
public boolean pullFromRemote(String siteId, String remoteName, String remoteBranch) throws ServiceLayerException, InvalidRemoteUrlException, CryptoException {
logger.debug("Get remote data from database for remote " + remoteName + " and site " + siteId);
Map<String, String> params = new HashMap<String, String>();
params.put("siteId", siteId);
params.put("remoteName", remoteName);
RemoteRepository remoteRepository = remoteRepositoryDAO.getRemoteRepository(params);
logger.debug("Prepare pull command");
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repo = helper.getRepository(siteId, SANDBOX);
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, siteId);
generalLockService.lock(gitLockKey);
try (Git git = new Git(repo)) {
PullResult pullResult = null;
PullCommand pullCommand = git.pull();
logger.debug("Set remote " + remoteName);
pullCommand.setRemote(remoteRepository.getRemoteName());
logger.debug("Set branch to be " + remoteBranch);
pullCommand.setRemoteBranchName(remoteBranch);
switch(remoteRepository.getAuthenticationType()) {
case NONE:
logger.debug("No authentication");
pullResult = pullCommand.call();
break;
case BASIC:
logger.debug("Basic authentication");
String hashedPassword = remoteRepository.getRemotePassword();
String password = encryptor.decrypt(hashedPassword);
pullCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(remoteRepository.getRemoteUsername(), password));
pullResult = pullCommand.call();
break;
case TOKEN:
logger.debug("Token based authentication");
String hashedToken = remoteRepository.getRemoteToken();
String token = encryptor.decrypt(hashedToken);
pullCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(token, EMPTY));
pullResult = pullCommand.call();
break;
case PRIVATE_KEY:
logger.debug("Private key authentication");
final Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
String hashedPrivateKey = remoteRepository.getRemotePrivateKey();
String privateKey = encryptor.decrypt(hashedPrivateKey);
tempKey.toFile().deleteOnExit();
pullCommand.setTransportConfigCallback(new TransportConfigCallback() {
@Override
public void configure(Transport transport) {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(getSshSessionFactory(privateKey, tempKey));
}
});
pullResult = retryingRepositoryOperationFacade.call(pullCommand);
Files.delete(tempKey);
break;
default:
throw new ServiceLayerException("Unsupported authentication type " + remoteRepository.getAuthenticationType());
}
return pullResult != null ? pullResult.isSuccessful() : false;
} catch (InvalidRemoteException e) {
logger.error("Remote is invalid " + remoteName, e);
throw new InvalidRemoteUrlException();
} catch (GitAPIException e) {
logger.error("Error while pulling from remote " + remoteName + " branch " + remoteBranch + " for site " + siteId, e);
throw new ServiceLayerException("Error while pulling from remote " + remoteName + " branch " + remoteBranch + " for site " + siteId, e);
} catch (CryptoException | IOException e) {
throw new ServiceLayerException(e);
} finally {
generalLockService.unlock(gitLockKey);
}
}
use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.
the class GitContentRepository method resetStagingRepository.
@Override
public void resetStagingRepository(String siteId) throws ServiceLayerException, CryptoException {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repo = helper.getRepository(siteId, PUBLISHED);
String stagingName = servicesConfig.getStagingEnvironment(siteId);
String liveName = servicesConfig.getLiveEnvironment(siteId);
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, siteId);
generalLockService.lock(gitLockKey);
synchronized (repo) {
try (Git git = new Git(repo)) {
logger.debug("Checkout live first becuase it is not allowed to delete checkedout branch");
CheckoutCommand checkoutCommand = git.checkout().setName(liveName);
retryingRepositoryOperationFacade.call(checkoutCommand);
logger.debug("Delete staging branch in order to reset it for site: " + siteId);
DeleteBranchCommand deleteBranchCommand = git.branchDelete().setBranchNames(stagingName).setForce(true);
retryingRepositoryOperationFacade.call(deleteBranchCommand);
logger.debug("Create new branch for staging with live HEAD as starting point");
CreateBranchCommand createBranchCommand = git.branchCreate().setName(stagingName).setStartPoint(liveName);
retryingRepositoryOperationFacade.call(createBranchCommand);
} catch (GitAPIException e) {
logger.error("Error while reseting staging environment for site: " + siteId);
throw new ServiceLayerException(e);
} finally {
generalLockService.unlock(gitLockKey);
}
}
}
use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.
the class GitContentRepository method moveContent.
@Override
public Map<String, String> moveContent(String site, String fromPath, String toPath, String newName) {
Map<String, String> toRet = new TreeMap<String, String>();
String commitId;
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
generalLockService.lock(gitLockKey);
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX)) {
Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
String gitFromPath = helper.getGitPath(fromPath);
String gitToPath;
if (StringUtils.isEmpty(newName)) {
gitToPath = helper.getGitPath(toPath);
} else {
gitToPath = helper.getGitPath(toPath + FILE_SEPARATOR + newName);
}
try (Git git = new Git(repo)) {
// Check if destination is a file, then this is a rename operation
// Perform rename and exit
Path sourcePath = Paths.get(repo.getDirectory().getParent(), gitFromPath);
File sourceFile = sourcePath.toFile();
Path targetPath = Paths.get(repo.getDirectory().getParent(), gitToPath);
File targetFile = targetPath.toFile();
if (sourceFile.getCanonicalFile().equals(targetFile.getCanonicalFile())) {
sourceFile.renameTo(targetFile);
} else {
if (targetFile.isFile()) {
if (sourceFile.isFile()) {
sourceFile.renameTo(targetFile);
} else {
// This is not a valid operation
logger.error("Invalid move operation: Trying to rename a directory to a file " + "for site: " + site + " fromPath: " + fromPath + " toPath: " + toPath + " newName: " + newName);
}
} else if (sourceFile.isDirectory()) {
// Check if we're moving a single file or whole subtree
File[] dirList = sourceFile.listFiles();
for (File child : dirList) {
if (!child.equals(sourceFile)) {
FileUtils.moveToDirectory(child, targetFile, true);
}
}
FileUtils.deleteDirectory(sourceFile);
} else {
if (sourceFile.isFile()) {
FileUtils.moveFile(sourceFile, targetFile);
} else {
FileUtils.moveToDirectory(sourceFile, targetFile, true);
}
}
}
// The operation is done on disk, now it's time to commit
AddCommand addCommand = git.add().addFilepattern(gitToPath);
retryingRepositoryOperationFacade.call(addCommand);
StatusCommand statusCommand = git.status().addPath(gitToPath);
Status gitStatus = retryingRepositoryOperationFacade.call(statusCommand);
Set<String> changeSet = gitStatus.getAdded();
for (String pathToCommit : changeSet) {
String pathRemoved = pathToCommit.replace(gitToPath, gitFromPath);
CommitCommand commitCommand = git.commit().setOnly(pathToCommit).setOnly(pathRemoved).setAuthor(helper.getCurrentUserIdent()).setCommitter(helper.getCurrentUserIdent()).setMessage(helper.getCommitMessage(REPO_MOVE_CONTENT_COMMIT_MESSAGE).replaceAll(PATTERN_FROM_PATH, fromPath).replaceAll(PATTERN_TO_PATH, toPath + (StringUtils.isNotEmpty(newName) ? newName : EMPTY)));
RevCommit commit = retryingRepositoryOperationFacade.call(commitCommand);
commitId = commit.getName();
toRet.put(pathToCommit, commitId);
}
}
}
} catch (IOException | GitAPIException | ServiceLayerException | UserNotFoundException | CryptoException e) {
logger.error("Error while moving content for site: " + site + " fromPath: " + fromPath + " toPath: " + toPath + " newName: " + newName);
} finally {
generalLockService.unlock(gitLockKey);
}
return toRet;
}
Aggregations