Cloud integrations - deploy artifacts to git clouds

This commit is contained in:
Michael Golubev
2013-11-26 23:52:59 +01:00
parent ab741becab
commit 036cc96b37
7 changed files with 322 additions and 75 deletions
@@ -12,7 +12,7 @@
<orderEntry type="module" module-name="debugger-impl" />
<orderEntry type="module" module-name="execution-openapi" />
<orderEntry type="module" module-name="lang-impl" />
<orderEntry type="module" module-name="remote-servers-impl" scope="RUNTIME" />
<orderEntry type="module" module-name="remote-servers-impl" />
</component>
</module>
@@ -0,0 +1,69 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.remoteServer.impl.util;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.packaging.artifacts.Artifact;
import com.intellij.remoteServer.agent.util.CloudGitApplication;
import com.intellij.remoteServer.configuration.deployment.ArtifactDeploymentSource;
import com.intellij.remoteServer.configuration.deployment.DeploymentSource;
import com.intellij.remoteServer.util.DeploymentSourceHandler;
import com.intellij.remoteServer.util.ServerRuntimeException;
import java.io.File;
/**
* @author michael.golubev
*/
public abstract class ArtifactDeploymentSourceHandlerBase implements DeploymentSourceHandler {
private static final Logger LOG = Logger.getInstance("#" + ArtifactDeploymentSourceHandlerBase.class.getName());
private Artifact myArtifact;
private File myRepositoryRootFile;
protected File getArtifactFile() {
return new File(myArtifact.getOutputFilePath());
}
@Override
public File getRepositoryRootFile() {
return myRepositoryRootFile;
}
@Override
public boolean init(DeploymentSource deploymentSource) throws ServerRuntimeException {
if (!(deploymentSource instanceof ArtifactDeploymentSource)) {
return false;
}
ArtifactDeploymentSource artifactDeploymentSource = (ArtifactDeploymentSource)deploymentSource;
Artifact artifact = artifactDeploymentSource.getArtifact();
if (artifact == null) {
throw new ServerRuntimeException("Artifact not found " + artifactDeploymentSource.getArtifactPointer().getArtifactName());
}
String outputPath = artifact.getOutputPath();
LOG.assertTrue(outputPath != null, "Artifact output path not found");
myRepositoryRootFile = new File(outputPath, "/deploy");
if (!myRepositoryRootFile.exists()) {
if (!myRepositoryRootFile.mkdir()) {
throw new ServerRuntimeException("Unable to create deploy folder");
}
}
myArtifact = artifact;
return true;
}
}
@@ -0,0 +1,33 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.remoteServer.util;
import com.intellij.remoteServer.agent.util.CloudGitApplication;
import com.intellij.remoteServer.configuration.deployment.DeploymentSource;
import java.io.File;
/**
* @author michael.golubev
*/
public interface DeploymentSourceHandler {
boolean init(DeploymentSource deploymentSource) throws ServerRuntimeException;
File getRepositoryRootFile();
CloudGitApplication deploy() throws ServerRuntimeException;
}
@@ -0,0 +1,59 @@
package com.intellij.remoteServer.util;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.module.ModulePointerManager;
import com.intellij.openapi.project.Project;
import com.intellij.remoteServer.ServerType;
import com.intellij.remoteServer.configuration.ServerConfiguration;
import com.intellij.remoteServer.configuration.deployment.DeploymentConfiguration;
import com.intellij.remoteServer.configuration.deployment.DeploymentConfigurator;
import com.intellij.remoteServer.configuration.deployment.DeploymentSource;
import com.intellij.remoteServer.impl.configuration.deployment.ModuleDeploymentSourceImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author michael.golubev
*/
public abstract class CloudGitDeploymentConfiguratorBase<D extends DeploymentConfiguration, S extends ServerConfiguration>
extends DeploymentConfigurator<D, S> {
private final Project myProject;
private ServerType<S> myServerType;
public CloudGitDeploymentConfiguratorBase(Project project, ServerType<S> serverType) {
myProject = project;
myServerType = serverType;
}
@Nullable
public static CloudGitDeploymentSourceHandlerProvider getDeploymentSourceHandlerProvider(ServerType<?> serverType) {
for (CloudGitDeploymentSourceHandlerProvider provider : CloudGitDeploymentSourceHandlerProvider.EP_NAME.getExtensions()) {
if (provider.getServerType() == serverType) {
return provider;
}
}
return null;
}
@NotNull
@Override
public List<DeploymentSource> getAvailableDeploymentSources() {
if (myProject.isDefault()) return Collections.emptyList();
List<DeploymentSource> result = new ArrayList<DeploymentSource>();
ModulePointerManager pointerManager = ModulePointerManager.getInstance(myProject);
for (Module module : ModuleManager.getInstance(myProject).getModules()) {
result.add(new ModuleDeploymentSourceImpl(pointerManager.create(module)));
}
CloudGitDeploymentSourceHandlerProvider provider = getDeploymentSourceHandlerProvider(myServerType);
if (provider != null) {
result.addAll(provider.getDeploymentSources(myProject));
}
return result;
}
}
@@ -12,7 +12,9 @@ import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.remoteServer.ServerType;
import com.intellij.remoteServer.agent.util.CloudGitAgent;
import com.intellij.remoteServer.agent.util.CloudGitAgentDeployment;
import com.intellij.remoteServer.agent.util.CloudGitApplication;
@@ -38,6 +40,8 @@ import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author michael.golubev
@@ -53,6 +57,7 @@ public abstract class CloudGitDeploymentRuntime<DC extends CloudDeploymentNameCo
private final Project myProject;
private final GitRepositoryManager myGitRepositoryManager;
private final Git myGit;
private DeploymentSourceHandler mySourceHandler;
private final VirtualFile myContentRoot;
private final File myContentRootFile;
@@ -76,7 +81,8 @@ public abstract class CloudGitDeploymentRuntime<DC extends CloudDeploymentNameCo
@Nullable DeploymentLogManager logManager,
CloudDeploymentNameProvider deploymentNameProvider,
String remoteName,
String cloudName) throws ServerRuntimeException {
String cloudName,
ServerType<?> serverType) throws ServerRuntimeException {
myConfiguration = serverConfiguration;
myTasksExecutor = taskExecutor;
myLogManager = logManager;
@@ -84,25 +90,31 @@ public abstract class CloudGitDeploymentRuntime<DC extends CloudDeploymentNameCo
myRemoteName = remoteName;
myCloudName = cloudName;
List<DeploymentSourceHandler> handlers = new ArrayList<DeploymentSourceHandler>();
handlers.add(new ModuleDeploymentSourceHandler());
CloudGitDeploymentSourceHandlerProvider handlerProvider
= CloudGitDeploymentConfiguratorBase.getDeploymentSourceHandlerProvider(serverType);
if (handlerProvider != null) {
handlers.addAll(handlerProvider.getHandlers(this));
}
DeploymentSource deploymentSource = task.getSource();
if (!(deploymentSource instanceof ModuleDeploymentSource)) {
throw new ServerRuntimeException("Module deployment source is the only supported");
for (DeploymentSourceHandler sourceHandler : handlers) {
if (sourceHandler.init(deploymentSource)) {
mySourceHandler = sourceHandler;
break;
}
}
if (mySourceHandler == null) {
throw new ServerRuntimeException("Unknown deployment source");
}
ModuleDeploymentSource moduleDeploymentSource = (ModuleDeploymentSource)deploymentSource;
Module module = moduleDeploymentSource.getModule();
if (module == null) {
throw new ServerRuntimeException("Module not found: " + moduleDeploymentSource.getModulePointer().getModuleName());
}
VirtualFile contentRoot = moduleDeploymentSource.getContentRoot();
LOG.assertTrue(contentRoot != null, "Content root is not found");
myContentRoot = contentRoot;
File contentRootFile = moduleDeploymentSource.getFile();
LOG.assertTrue(contentRootFile != null, "Content root file is not found");
File contentRootFile = mySourceHandler.getRepositoryRootFile();
myContentRootFile = contentRootFile;
VirtualFile contentRoot = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(myContentRootFile);
LOG.assertTrue(contentRoot != null, "Repository root is not found");
myContentRoot = contentRoot;
myProject = task.getProject();
myGitRepositoryManager = GitUtil.getRepositoryManager(myProject);
myGit = ServiceManager.getService(Git.class);
@@ -142,59 +154,7 @@ public abstract class CloudGitDeploymentRuntime<DC extends CloudDeploymentNameCo
}
public void deploy() throws ServerRuntimeException {
VirtualFile contentRoot = getContentRoot();
CloudGitApplication application = findApplication();
if (application == null) {
application = createApplication();
}
GitRepository repository = findRepository();
if (repository == null) {
myLoggingHandler.println("Initializing git repository...");
GitCommandResult gitInitResult = getGit().init(getProject(), contentRoot, createGitLineHandlerListener());
checkGitResult(gitInitResult);
refreshApplicationRepository();
repository = getRepository();
}
GitRemote gitRemote = GitUtil.findRemoteByName(repository, getRemoteName());
if (gitRemote == null) {
addGitRemote(application);
}
else if (!gitRemote.getUrls().contains(application.getGitUrl())) {
resetGitRemote(application);
}
try {
GitSimpleHandler handler = new GitSimpleHandler(getProject(), contentRoot, GitCommand.ADD);
handler.setSilent(false);
handler.addParameters(".");
handler.run();
}
catch (VcsException e) {
throw new ServerRuntimeException(e);
}
try {
if (GitUtil.hasLocalChanges(true, getProject(), contentRoot)) {
GitSimpleHandler handler = new GitSimpleHandler(getProject(), contentRoot, GitCommand.COMMIT);
handler.setSilent(false);
handler.addParameters("-a");
handler.addParameters("-m", "Deploy");
handler.endOptions();
handler.run();
}
}
catch (VcsException e) {
throw new ServerRuntimeException(e);
}
repository.update();
pushApplication(getRemoteName(), application.getGitUrl());
CloudGitApplication application = mySourceHandler.deploy();
if (myLogManager != null) {
LoggingHandler loggingHandler = myLogManager.getMainLoggingHandler();
@@ -239,6 +199,38 @@ public abstract class CloudGitDeploymentRuntime<DC extends CloudDeploymentNameCo
return findApplication() != null;
}
public CloudGitApplication findOrCreateApplication() throws ServerRuntimeException {
CloudGitApplication application = findApplication();
if (application == null) {
application = createApplication();
}
return application;
}
public void addOrResetGitRemote(CloudGitApplication application, GitRepository repository) throws ServerRuntimeException {
GitRemote gitRemote = GitUtil.findRemoteByName(repository, getRemoteName());
if (gitRemote == null) {
addGitRemote(application);
}
else if (!gitRemote.getUrls().contains(application.getGitUrl())) {
resetGitRemote(application);
}
}
public GitRepository findOrCreateRepository() throws ServerRuntimeException {
GitRepository repository = findRepository();
if (repository == null) {
myLoggingHandler.println("Initializing git repository...");
GitCommandResult gitInitResult = getGit().init(getProject(), getContentRoot(), createGitLineHandlerListener());
checkGitResult(gitInitResult);
refreshApplicationRepository();
repository = getRepository();
}
return repository;
}
public void downloadExistingApplication() throws ServerRuntimeException {
CloudGitApplication application = findApplication();
if (application == null) {
@@ -259,7 +251,7 @@ public abstract class CloudGitDeploymentRuntime<DC extends CloudDeploymentNameCo
return myApplicationName;
}
private Git getGit() {
protected Git getGit() {
return myGit;
}
@@ -267,7 +259,7 @@ public abstract class CloudGitDeploymentRuntime<DC extends CloudDeploymentNameCo
return myContentRoot;
}
private File getContentRootFile() {
protected File getContentRootFile() {
return myContentRootFile;
}
@@ -335,9 +327,9 @@ public abstract class CloudGitDeploymentRuntime<DC extends CloudDeploymentNameCo
GitInit.refreshAndConfigureVcsMappings(myProject, getContentRoot(), getContentRootFile().getAbsolutePath());
}
protected void pushApplication(String remoteName, String gitUrl) throws ServerRuntimeException {
protected void pushApplication(@NotNull CloudGitApplication application) throws ServerRuntimeException {
GitCommandResult gitPushResult
= getGit().push(getRepository(), remoteName, gitUrl, "master:master", createGitLineHandlerListener());
= getGit().push(getRepository(), getRemoteName(), application.getGitUrl(), "master:master", createGitLineHandlerListener());
checkGitResult(gitPushResult);
}
@@ -362,6 +354,34 @@ public abstract class CloudGitDeploymentRuntime<DC extends CloudDeploymentNameCo
repository.update();
}
protected void add() throws ServerRuntimeException {
try {
GitSimpleHandler handler = new GitSimpleHandler(getProject(), myContentRoot, GitCommand.ADD);
handler.setSilent(false);
handler.addParameters(".");
handler.run();
}
catch (VcsException e) {
throw new ServerRuntimeException(e);
}
}
protected void commit() throws ServerRuntimeException {
try {
if (GitUtil.hasLocalChanges(true, getProject(), myContentRoot)) {
GitSimpleHandler handler = new GitSimpleHandler(getProject(), myContentRoot, GitCommand.COMMIT);
handler.setSilent(false);
handler.addParameters("-a");
handler.addParameters("-m", "Deploy");
handler.endOptions();
handler.run();
}
}
catch (VcsException e) {
throw new ServerRuntimeException(e);
}
}
protected void performRemoteGitTask(final GitLineHandler handler, String title) throws ServerRuntimeException {
final GitTask task = new GitTask(myProject, handler, title);
task.setProgressAnalyzer(new GitStandardProgressAnalyzer());
@@ -435,7 +455,7 @@ public abstract class CloudGitDeploymentRuntime<DC extends CloudDeploymentNameCo
return confirmed.get();
}
private String getRemoteName() {
protected String getRemoteName() {
return myRemoteName;
}
@@ -516,4 +536,44 @@ public abstract class CloudGitDeploymentRuntime<DC extends CloudDeploymentNameCo
performRemoteGitTask(handler, CloudBundle.getText("cloning.existing.application", getCloudName()));
}
}
public class ModuleDeploymentSourceHandler implements DeploymentSourceHandler {
private File myRepositoryRootFile;
@Override
public File getRepositoryRootFile() {
return myRepositoryRootFile;
}
@Override
public boolean init(DeploymentSource deploymentSource) throws ServerRuntimeException {
if (!(deploymentSource instanceof ModuleDeploymentSource)) {
return false;
}
ModuleDeploymentSource moduleDeploymentSource = (ModuleDeploymentSource)deploymentSource;
Module module = moduleDeploymentSource.getModule();
if (module == null) {
throw new ServerRuntimeException("Module not found: " + moduleDeploymentSource.getModulePointer().getModuleName());
}
File contentRootFile = moduleDeploymentSource.getFile();
LOG.assertTrue(contentRootFile != null, "Content root file is not found");
myRepositoryRootFile = contentRootFile;
return true;
}
@Override
public CloudGitApplication deploy() throws ServerRuntimeException {
CloudGitApplication application = findOrCreateApplication();
GitRepository repository = findOrCreateRepository();
addOrResetGitRemote(application, repository);
add();
commit();
repository.update();
pushApplication(application);
return application;
}
}
}
@@ -0,0 +1,24 @@
package com.intellij.remoteServer.util;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.project.Project;
import com.intellij.remoteServer.ServerType;
import com.intellij.remoteServer.configuration.deployment.DeploymentSource;
import java.util.Collection;
import java.util.List;
/**
* @author michael.golubev
*/
public interface CloudGitDeploymentSourceHandlerProvider {
ExtensionPointName<CloudGitDeploymentSourceHandlerProvider> EP_NAME
= ExtensionPointName.create("Git4Idea.remoteServer.CloudGitDeploymentSourceHandlerProvider");
ServerType<?> getServerType();
Collection<DeploymentSource> getDeploymentSources(Project project);
List<DeploymentSourceHandler> getHandlers(CloudGitDeploymentRuntime<?, ?, ?> deploymentRuntime);
}
+3 -1
View File
@@ -193,6 +193,8 @@
<extensionPoints>
<extensionPoint qualifiedName="Git4Idea.GitHttpAuthDataProvider" interface="git4idea.jgit.GitHttpAuthDataProvider"/>
</extensionPoints>
<extensionPoint qualifiedName="Git4Idea.remoteServer.CloudGitDeploymentSourceHandlerProvider"
interface="com.intellij.remoteServer.util.CloudGitDeploymentSourceHandlerProvider"/>
</extensionPoints>
</idea-plugin>