use of org.craftercms.studio.api.v2.dal.ClusterMember in project studio by craftercms.
the class StudioClusterSandboxRepoSyncTask method addRemoteRepository.
private void addRemoteRepository(String siteId, ClusterMember member, String remoteUrl) throws IOException, InvalidRemoteUrlException, ServiceLayerException {
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repo = builder.setGitDir(buildRepoPath(siteId).resolve(GIT_ROOT).toFile()).readEnvironment().findGitDir().build();
try (Git git = new Git(repo)) {
Config storedConfig = repo.getConfig();
Set<String> remotes = storedConfig.getSubsections(CONFIG_SECTION_REMOTE);
if (remotes.contains(member.getGitRemoteName().replaceFirst(CLUSTER_NODE_REMOTE_NAME_PREFIX, ""))) {
try {
removeRemote(git, member.getGitRemoteName().replaceFirst(CLUSTER_NODE_REMOTE_NAME_PREFIX, ""));
} catch (GitAPIException e) {
logger.debug("Error while cleaning up remote repository for site " + siteId, e);
}
}
if (remotes.contains(member.getGitRemoteName())) {
logger.debug("Remote " + member.getGitRemoteName() + " already exists for SANDBOX repo for site " + siteId);
String storedRemoteUrl = storedConfig.getString(CONFIG_SECTION_REMOTE, member.getGitRemoteName(), CONFIG_PARAMETER_URL);
if (!StringUtils.equals(storedRemoteUrl, remoteUrl)) {
RemoteSetUrlCommand remoteSetUrlCommand = git.remoteSetUrl();
remoteSetUrlCommand.setName(member.getGitRemoteName());
remoteSetUrlCommand.setUri(new URIish(remoteUrl));
remoteSetUrlCommand.call();
}
} else {
logger.debug("Add " + member.getLocalAddress() + " as remote to SANDBOX");
RemoteAddCommand remoteAddCommand = git.remoteAdd();
remoteAddCommand.setName(member.getGitRemoteName());
remoteAddCommand.setUri(new URIish(remoteUrl));
remoteAddCommand.call();
}
} catch (URISyntaxException e) {
logger.error("Remote URL is invalid " + remoteUrl, e);
throw new InvalidRemoteUrlException();
} catch (GitAPIException e) {
logger.error("Error while adding remote " + member.getGitRemoteName() + " (url: " + remoteUrl + ") for site " + siteId, e);
throw new ServiceLayerException("Error while adding remote " + member.getGitRemoteName() + " (url: " + remoteUrl + ") for site " + siteId, e);
}
}
use of org.craftercms.studio.api.v2.dal.ClusterMember 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;
}
use of org.craftercms.studio.api.v2.dal.ClusterMember in project studio by craftercms.
the class GitContentRepository method insertClusterRemoteRepository.
@RetryingOperation
public void insertClusterRemoteRepository(RemoteRepository remoteRepository) {
HierarchicalConfiguration<ImmutableNode> registrationData = studioConfiguration.getSubConfig(CLUSTERING_NODE_REGISTRATION);
if (registrationData != null && !registrationData.isEmpty()) {
String localAddress = registrationData.getString(CLUSTER_MEMBER_LOCAL_ADDRESS);
ClusterMember member = clusterDao.getMemberByLocalAddress(localAddress);
if (member != null) {
clusterDao.addClusterRemoteRepository(member.getId(), remoteRepository.getId());
}
}
}
use of org.craftercms.studio.api.v2.dal.ClusterMember in project studio by craftercms.
the class ClusterNodeRegistrationImpl method init.
public void init() {
logger.debug("Autoregister cluster if cluster node is configured");
HierarchicalConfiguration<ImmutableNode> registrationData = getConfiguration();
ClusterMember clusterMember = new ClusterMember();
if (registrationData != null && !registrationData.isEmpty()) {
try {
logger.debug("Collect and populate data for cluster node registration");
clusterMember.setLocalAddress(registrationData.getString(CLUSTER_MEMBER_LOCAL_ADDRESS));
Path path = Paths.get(studioConfiguration.getProperty(REPO_BASE_PATH), studioConfiguration.getProperty(SITES_REPOS_PATH));
String authenticationType = registrationData.getString(CLUSTER_MEMBER_AUTHENTICATION_TYPE);
String username = registrationData.getString(CLUSTER_MEMBER_USERNAME);
String password = registrationData.getString(CLUSTER_MEMBER_PASSWORD);
String token = registrationData.getString(CLUSTER_MEMBER_TOKEN);
String privateKey = registrationData.getString(CLUSTER_MEMBER_PRIVATE_KEY);
String gitUrl = studioConfiguration.getProperty(CLUSTERING_SYNC_URL_FORMAT);
if (StringUtils.isEmpty(username)) {
gitUrl = gitUrl.replace("{username}@", "");
} else {
gitUrl = gitUrl.replace("{username}", username);
}
gitUrl = gitUrl.replace("{localAddress}", clusterMember.getLocalAddress()).replace("{absolutePath}", path.toAbsolutePath().normalize().toString()) + "/{siteId}";
clusterMember.setGitUrl(gitUrl);
clusterMember.setState(ClusterMember.State.ACTIVE);
clusterMember.setGitRemoteName(getGitRemoteName(clusterMember));
clusterMember.setGitAuthType(authenticationType.toLowerCase());
clusterMember.setGitUsername(username);
if (StringUtils.isEmpty(password)) {
clusterMember.setGitPassword(password);
} else {
String hashedPassword = encryptor.encrypt(password);
clusterMember.setGitPassword(hashedPassword);
}
if (StringUtils.isEmpty(token)) {
clusterMember.setGitToken(token);
} else {
String hashedToken = encryptor.encrypt(token);
clusterMember.setGitToken(hashedToken);
}
if (StringUtils.isEmpty(privateKey)) {
clusterMember.setGitPrivateKey(privateKey);
} else {
String hashedPrivateKey = encryptor.encrypt(privateKey);
clusterMember.setGitPrivateKey(hashedPrivateKey);
}
clusterMember.setAvailable(1);
logger.debug("Register cluster member");
registerClusterNode(clusterMember);
} catch (CryptoException e) {
logger.error("Failed to register cluster member");
}
}
}
use of org.craftercms.studio.api.v2.dal.ClusterMember in project studio by craftercms.
the class ClusterManagementController method getAllMembers.
@GetMapping("/api/2/cluster")
public ResponseBody getAllMembers() throws ServiceLayerException {
List<ClusterMember> clusterMembers = clusterManagementService.getAllMemebers();
ResponseBody responseBody = new ResponseBody();
ResultList<ClusterMember> result = new ResultList<ClusterMember>();
result.setEntities(RESULT_KEY_CLUSTER_MEMBERS, clusterMembers);
result.setResponse(ApiResponse.OK);
responseBody.setResult(result);
return responseBody;
}
Aggregations