use of org.craftercms.studio.api.v1.ebus.DeploymentItem in project studio by craftercms.
the class GitContentRepository method publish.
@RetryingOperation
@Override
public void publish(String site, String sandboxBranch, List<DeploymentItemTO> deploymentItems, String environment, String author, String comment) throws DeploymentException {
if (CollectionUtils.isEmpty(deploymentItems)) {
return;
}
String commitId = EMPTY;
String gitLockKey = SITE_PUBLISHED_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
generalLockService.lock(gitLockKey);
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repo = helper.getRepository(site, PUBLISHED);
boolean repoCreated = false;
if (Objects.isNull(repo)) {
helper.createPublishedRepository(site, sandboxBranch);
repo = helper.getRepository(site, PUBLISHED);
repoCreated = Objects.nonNull(repo);
}
String path = EMPTY;
String sandboxBranchName = sandboxBranch;
if (StringUtils.isEmpty(sandboxBranchName)) {
sandboxBranchName = studioConfiguration.getProperty(REPO_SANDBOX_BRANCH);
}
synchronized (repo) {
try (Git git = new Git(repo)) {
String inProgressBranchName = environment + IN_PROGRESS_BRANCH_NAME_SUFFIX;
// fetch "origin/master"
logger.debug("Fetch from sandbox for site " + site);
FetchCommand fetchCommand = git.fetch();
retryingRepositoryOperationFacade.call(fetchCommand);
// checkout master and pull from sandbox
logger.debug("Checkout published/master branch for site " + site);
try {
// First delete it in case it already exists (ignored if does not exist)
String currentBranch = repo.getBranch();
if (currentBranch.endsWith(IN_PROGRESS_BRANCH_NAME_SUFFIX)) {
ResetCommand resetCommand = git.reset().setMode(HARD);
retryingRepositoryOperationFacade.call(resetCommand);
}
Ref ref = repo.exactRef(R_HEADS + sandboxBranchName);
boolean createBranch = (ref == null);
CheckoutCommand checkoutCommand = git.checkout().setName(sandboxBranchName).setCreateBranch(createBranch);
retryingRepositoryOperationFacade.call(checkoutCommand);
logger.debug("Delete in-progress branch, in case it was not cleaned up for site " + site);
DeleteBranchCommand deleteBranchCommand = git.branchDelete().setBranchNames(inProgressBranchName).setForce(true);
retryingRepositoryOperationFacade.call(deleteBranchCommand);
PullCommand pullCommand = git.pull().setRemote(DEFAULT_REMOTE_NAME).setRemoteBranchName(sandboxBranchName).setStrategy(THEIRS);
retryingRepositoryOperationFacade.call(pullCommand);
} catch (RefNotFoundException e) {
logger.error("Failed to checkout published master and to pull content from sandbox for site " + site, e);
throw new DeploymentException("Failed to checkout published master and to pull content from " + "sandbox for site " + site);
}
// checkout environment branch
logger.debug("Checkout environment branch " + environment + " for site " + site);
try {
CheckoutCommand checkoutCommand = git.checkout().setName(environment);
retryingRepositoryOperationFacade.call(checkoutCommand);
} catch (RefNotFoundException e) {
logger.info("Not able to find branch " + environment + " for site " + site + ". Creating new branch");
// create new environment branch
// it will start as empty orphan branch
CheckoutCommand checkoutCommand = git.checkout().setOrphan(true).setForceRefUpdate(true).setStartPoint(sandboxBranchName).setUpstreamMode(TRACK).setName(environment);
retryingRepositoryOperationFacade.call(checkoutCommand);
// remove any content to create empty branch
RmCommand rmcmd = git.rm();
File[] toDelete = repo.getWorkTree().listFiles();
for (File toDel : toDelete) {
if (!repo.getDirectory().equals(toDel) && !StringUtils.equals(toDel.getName(), DOT_GIT_IGNORE)) {
rmcmd.addFilepattern(toDel.getName());
}
}
retryingRepositoryOperationFacade.call(rmcmd);
CommitCommand commitCommand = git.commit().setMessage(helper.getCommitMessage(REPO_INITIAL_COMMIT_COMMIT_MESSAGE)).setAllowEmpty(true);
retryingRepositoryOperationFacade.call(commitCommand);
}
// Create in progress branch
try {
// Create in progress branch
logger.debug("Create in-progress branch for site " + site);
CheckoutCommand checkoutCommand = git.checkout().setCreateBranch(true).setForceRefUpdate(true).setStartPoint(environment).setUpstreamMode(TRACK).setName(inProgressBranchName);
retryingRepositoryOperationFacade.call(checkoutCommand);
} catch (GitAPIException e) {
// TODO: DB: Error ?
logger.error("Failed to create in-progress published branch for site " + site);
}
Set<String> deployedCommits = new HashSet<String>();
Set<String> deployedPackages = new HashSet<String>();
logger.debug("Checkout deployed files started.");
AddCommand addCommand = git.add();
for (DeploymentItemTO deploymentItem : deploymentItems) {
commitId = deploymentItem.getCommitId();
path = helper.getGitPath(deploymentItem.getPath());
if (Objects.isNull(commitId) || !commitIdExists(site, PUBLISHED, commitId)) {
if (contentExists(site, path)) {
if (Objects.isNull(commitId)) {
logger.warn("Commit ID is NULL for content " + path + ". Was the git repo reset at some point?");
} else {
logger.warn("Commit ID " + commitId + " does not exist for content " + path + ". Was the git repo reset at some point?");
}
logger.info("Publishing content from HEAD for " + path);
commitId = getRepoLastCommitId(site);
} else {
logger.warn("Skipping file " + path + " because commit id is null");
continue;
}
}
logger.debug("Checking out file " + path + " from commit id " + commitId + " for site " + site);
CheckoutCommand checkout = git.checkout();
checkout.setStartPoint(commitId).addPath(path);
retryingRepositoryOperationFacade.call(checkout);
if (deploymentItem.isMove()) {
if (!StringUtils.equals(deploymentItem.getPath(), deploymentItem.getOldPath())) {
String oldPath = helper.getGitPath(deploymentItem.getOldPath());
RmCommand rmCommand = git.rm().addFilepattern(oldPath).setCached(false);
retryingRepositoryOperationFacade.call(rmCommand);
cleanUpMoveFolders(git, oldPath);
}
}
if (deploymentItem.isDelete()) {
String deletePath = helper.getGitPath(deploymentItem.getPath());
boolean isPage = deletePath.endsWith(FILE_SEPARATOR + INDEX_FILE);
RmCommand rmCommand = git.rm().addFilepattern(deletePath).setCached(false);
retryingRepositoryOperationFacade.call(rmCommand);
Path parentToDelete = Paths.get(path).getParent();
deleteParentFolder(git, parentToDelete, isPage);
}
deployedCommits.add(commitId);
String packageId = deploymentItem.getPackageId();
if (StringUtils.isNotEmpty(packageId)) {
deployedPackages.add(deploymentItem.getPackageId());
}
addCommand.addFilepattern(path);
objectMetadataManager.updateLastPublishedDate(site, deploymentItem.getPath(), ZonedDateTime.now(UTC));
}
logger.debug("Checkout deployed files completed.");
// commit all deployed files
String commitMessage = studioConfiguration.getProperty(REPO_PUBLISHED_COMMIT_MESSAGE);
logger.debug("Get Author Ident started.");
User user = userServiceInternal.getUserByIdOrUsername(-1, author);
PersonIdent authorIdent = helper.getAuthorIdent(user);
logger.debug("Get Author Ident completed.");
logger.debug("Git add all published items started.");
retryingRepositoryOperationFacade.call(addCommand);
logger.debug("Git add all published items completed.");
commitMessage = commitMessage.replace("{username}", author);
commitMessage = commitMessage.replace("{datetime}", ZonedDateTime.now(UTC).format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HHmmssSSSX")));
commitMessage = commitMessage.replace("{source}", "UI");
commitMessage = commitMessage.replace("{message}", comment);
StringBuilder sb = new StringBuilder();
for (String c : deployedCommits) {
sb.append(c).append(" ");
}
StringBuilder sbPackage = new StringBuilder();
for (String p : deployedPackages) {
sbPackage.append(p).append(" ");
}
commitMessage = commitMessage.replace("{commit_id}", sb.toString().trim());
commitMessage = commitMessage.replace("{package_id}", sbPackage.toString().trim());
logger.debug("Git commit all published items started.");
String prologue = studioConfiguration.getProperty(REPO_COMMIT_MESSAGE_PROLOGUE);
String postscript = studioConfiguration.getProperty(REPO_COMMIT_MESSAGE_POSTSCRIPT);
StringBuilder sbCommitMessage = new StringBuilder();
if (StringUtils.isNotEmpty(prologue)) {
sbCommitMessage.append(prologue).append("\n\n");
}
sbCommitMessage.append(commitMessage);
if (StringUtils.isNotEmpty(postscript)) {
sbCommitMessage.append("\n\n").append(postscript);
}
CommitCommand commitCommand = git.commit().setMessage(sbCommitMessage.toString()).setAuthor(authorIdent);
RevCommit revCommit = retryingRepositoryOperationFacade.call(commitCommand);
logger.debug("Git commit all published items completed.");
int commitTime = revCommit.getCommitTime();
// tag
ZonedDateTime tagDate2 = Instant.ofEpochSecond(commitTime).atZone(UTC);
ZonedDateTime publishDate = ZonedDateTime.now(UTC);
String tagName2 = tagDate2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HHmmssSSSX")) + "_published_on_" + publishDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HHmmssSSSX"));
logger.debug("Get Author Ident started.");
PersonIdent authorIdent2 = helper.getAuthorIdent(user);
logger.debug("Get Author Ident completed.");
logger.debug("Git tag started.");
TagCommand tagCommand = git.tag().setTagger(authorIdent2).setName(tagName2).setMessage(commitMessage);
retryingRepositoryOperationFacade.call(tagCommand);
logger.debug("Git tag completed.");
// checkout environment
logger.debug("Checkout environment " + environment + " branch for site " + site);
CheckoutCommand checkoutCommand = git.checkout().setName(environment);
retryingRepositoryOperationFacade.call(checkoutCommand);
Ref branchRef = repo.findRef(inProgressBranchName);
// merge in-progress branch
logger.debug("Merge in-progress branch into environment " + environment + " for site " + site);
MergeCommand mergeCommand = git.merge().setCommit(true).include(branchRef);
retryingRepositoryOperationFacade.call(mergeCommand);
// clean up
logger.debug("Delete in-progress branch (clean up) for site " + site);
DeleteBranchCommand deleteBranchCommand = git.branchDelete().setBranchNames(inProgressBranchName).setForce(true);
retryingRepositoryOperationFacade.call(deleteBranchCommand);
git.close();
if (repoCreated) {
siteService.setPublishedRepoCreated(site);
}
}
}
} catch (Exception e) {
logger.error("Error when publishing site " + site + " to environment " + environment, e);
throw new DeploymentException("Error when publishing site " + site + " to environment " + environment + " [commit ID = " + commitId + "]");
} finally {
generalLockService.unlock(gitLockKey);
}
}
use of org.craftercms.studio.api.v1.ebus.DeploymentItem in project studio by craftercms.
the class EnvironmentDeployer method onEnvironmentDeploymentEvent.
@EventListener(EVENT_PUBLISH_TO_ENVIRONMENT)
public void onEnvironmentDeploymentEvent(DeploymentEventContext context) {
List<DeploymentItem> items = context.getItems();
List<DeploymentItemTO> deploymentItems = new ArrayList<DeploymentItemTO>();
for (DeploymentItem item : items) {
DeploymentItemTO deploymentItem = new DeploymentItemTO();
deploymentItem.setSite(item.getSite());
deploymentItem.setPath(item.getPath());
deploymentItem.setCommitId(item.getCommitId());
deploymentItem.setPackageId(item.getPackageId());
}
try {
contentRepository.publish(context.getSite(), StringUtils.EMPTY, deploymentItems, context.getEnvironment(), context.getAuthor(), context.getComment());
} catch (DeploymentException e) {
logger.error("Error when publishing site " + context.getSite() + " to environment " + context.getEnvironment(), e);
}
}
use of org.craftercms.studio.api.v1.ebus.DeploymentItem in project studio by craftercms.
the class StudioPublisherTask method processPublishingRequest.
private void processPublishingRequest(String siteId, String environment, PublishRequest item, List<DeploymentItemTO> completeDeploymentItemList, Set<String> processedPaths) throws ServiceLayerException, DeploymentException {
List<DeploymentItemTO> missingDependencies = new ArrayList<DeploymentItemTO>();
Set<String> missingDependenciesPaths = new HashSet<String>();
String statusMessage;
try {
List<DeploymentItemTO> deploymentItemList = new ArrayList<DeploymentItemTO>();
logger.debug("Processing [{0}] content item for site \"{1}\"", item.getPath(), siteId);
DeploymentItemTO deploymentItem = publishingManager.processItem(item);
if (deploymentItem != null) {
deploymentItemList.add(deploymentItem);
}
logger.debug("Processing COMPLETE [{0}] content item for site \"{1}\"", item.getPath(), siteId);
if (isMandatoryDependenciesCheckEnabled()) {
logger.debug("Processing Mandatory Deps [{0}] content item for site " + "\"{1}\"", item.getPath(), siteId);
missingDependencies.addAll(publishingManager.processMandatoryDependencies(item, processedPaths, missingDependenciesPaths));
logger.debug("Processing Mandatory Dependencies COMPLETE [{0}]" + " content item for site \"{1}\"", item.getPath(), siteId);
}
deploymentItemList.addAll(missingDependencies);
completeDeploymentItemList.addAll(deploymentItemList);
} catch (DeploymentException err) {
logger.error("Error while executing deployment to environment store for site \"{0}\",", err, siteId);
publishingManager.markItemsReady(siteId, environment, Arrays.asList(item));
siteService.enablePublishing(siteId, false);
statusMessage = studioConfiguration.getProperty(JOB_DEPLOY_CONTENT_TO_ENVIRONMENT_STATUS_MESSAGE_ERROR);
siteService.updatePublishingStatusMessage(siteId, ERROR, statusMessage);
throw err;
} catch (Exception err) {
logger.error("Unexpected error while executing deployment to environment " + "store for site \"{0}\", ", err, siteId);
publishingManager.markItemsReady(siteId, environment, Arrays.asList(item));
siteService.enablePublishing(siteId, false);
statusMessage = studioConfiguration.getProperty(JOB_DEPLOY_CONTENT_TO_ENVIRONMENT_STATUS_MESSAGE_ERROR);
siteService.updatePublishingStatusMessage(siteId, ERROR, statusMessage);
throw err;
}
}
use of org.craftercms.studio.api.v1.ebus.DeploymentItem in project studio by craftercms.
the class PublishingManagerImpl method processItem.
@Override
public DeploymentItemTO processItem(PublishRequest item) throws DeploymentException, SiteNotFoundException {
if (item == null) {
throw new DeploymentException("Cannot process item, item is null.");
}
DeploymentItemTO deploymentItem = new DeploymentItemTO();
deploymentItem.setSite(item.getSite());
deploymentItem.setPath(item.getPath());
deploymentItem.setCommitId(item.getCommitId());
deploymentItem.setPackageId(item.getPackageId());
String site = item.getSite();
String path = item.getPath();
String oldPath = item.getOldPath();
String environment = item.getEnvironment();
String action = item.getAction();
String user = item.getUser();
String liveEnvironment = LIVE_ENVIRONMENT;
if (servicesConfig.isStagingEnvironmentEnabled(site)) {
liveEnvironment = servicesConfig.getLiveEnvironment(site);
}
boolean isLive = false;
if (StringUtils.isNotEmpty(liveEnvironment)) {
if (liveEnvironment.equals(environment)) {
isLive = true;
}
} else if (StringUtils.equalsIgnoreCase(LIVE_ENVIRONMENT, item.getEnvironment()) || StringUtils.equalsIgnoreCase(PRODUCTION_ENVIRONMENT, environment)) {
isLive = true;
}
if (StringUtils.equals(action, PublishRequest.Action.DELETE)) {
if (oldPath != null && oldPath.length() > 0) {
contentService.deleteContent(site, oldPath, user);
boolean hasRenamedChildren = false;
if (oldPath.endsWith(FILE_SEPARATOR + DmConstants.INDEX_FILE)) {
if (contentService.contentExists(site, oldPath.replace(FILE_SEPARATOR + DmConstants.INDEX_FILE, ""))) {
// TODO: SJ: This bypasses the Content Service, fix
RepositoryItem[] children = contentRepository.getContentChildren(site, oldPath.replace(FILE_SEPARATOR + DmConstants.INDEX_FILE, ""));
if (children.length > 1) {
hasRenamedChildren = true;
}
}
if (!hasRenamedChildren) {
deleteFolder(site, oldPath.replace(FILE_SEPARATOR + DmConstants.INDEX_FILE, ""), user);
}
}
deploymentItem.setMove(true);
deploymentItem.setOldPath(oldPath);
objectMetadataManager.clearRenamed(site, path);
}
boolean haschildren = false;
if (item.getPath().endsWith(FILE_SEPARATOR + DmConstants.INDEX_FILE)) {
if (contentService.contentExists(site, path.replace(FILE_SEPARATOR + DmConstants.INDEX_FILE, ""))) {
// TODO: SJ: This bypasses the Content Service, fix
RepositoryItem[] children = contentRepository.getContentChildren(site, path.replace(FILE_SEPARATOR + DmConstants.INDEX_FILE, ""));
if (children.length > 1) {
haschildren = true;
}
}
}
if (contentService.contentExists(site, path)) {
contentService.deleteContent(site, path, user);
if (!haschildren) {
deleteFolder(site, path.replace(FILE_SEPARATOR + DmConstants.INDEX_FILE, ""), user);
}
}
deploymentItem.setDelete(true);
} else {
if (StringUtils.equals(action, PublishRequest.Action.MOVE)) {
deploymentItem.setMove(true);
deploymentItem.setOldPath(oldPath);
if (oldPath != null && oldPath.length() > 0) {
if (isLive) {
objectMetadataManager.clearRenamed(site, path);
}
}
}
ItemMetadata itemMetadata = objectMetadataManager.getProperties(site, path);
if (itemMetadata == null) {
if (contentService.contentExists(site, path)) {
LOGGER.warn("Content item: '" + site + "':'" + path + "' doesn't exists in " + "the database, but does exist in git. This may cause problems " + "in the environment: '" + environment + "'");
} else {
LOGGER.warn("Content item: '" + site + "':'" + path + "' cannot be published. " + "Content does not exist in git nor in the database. Skipping...");
return null;
}
} else {
ContentItemTO contentItem = contentService.getContentItem(site, path);
if (isLive) {
// should consider what should be done if this does not work.
// Currently the method will bail and the item is stuck in processing.
LOGGER.debug("Environment is live, transition item to LIVE state {0}:{1}", site, path);
// check if commit id from workflow and from object state match
if (Objects.isNull(itemMetadata.getCommitId()) || itemMetadata.getCommitId().equals(item.getCommitId())) {
objectStateService.transition(site, contentItem, TransitionEvent.DEPLOYMENT);
}
} else {
objectStateService.transition(site, contentItem, TransitionEvent.SAVE);
}
itemMetadata.setSubmittedBy(StringUtils.EMPTY);
itemMetadata.setSendEmail(0);
itemMetadata.setSubmittedForDeletion(0);
itemMetadata.setSubmissionComment(StringUtils.EMPTY);
itemMetadata.setSubmittedToEnvironment(StringUtils.EMPTY);
itemMetadata.setLaunchDate(null);
objectMetadataManager.updateObjectMetadata(itemMetadata);
}
String blacklistConfig = studioConfiguration.getProperty(CONFIGURATION_PUBLISHING_BLACKLIST_REGEX);
if (StringUtils.isNotEmpty(blacklistConfig) && ContentUtils.matchesPatterns(item.getPath(), Arrays.asList(StringUtils.split(blacklistConfig, ",")))) {
LOGGER.debug("File " + item.getPath() + " of the site " + site + " will not be published because it " + "matches the configured publishing blacklist regex patterns.");
markItemsCompleted(site, item.getEnvironment(), Arrays.asList(item));
deploymentItem = null;
}
}
return deploymentItem;
}
Aggregations