Commits

Aldrin Leal  committed 1b0708a

Improving Fast-Deploy

  • Participants
  • Parent commits a5e5322
  • Branches fastdeploy

Comments (0)

Files changed (1)

File beanstalk-maven-plugin/src/main/java/br/com/ingenieux/mojo/beanstalk/bundle/FastDeployMojo.java

 
 import java.io.File;
 import java.util.Date;
+import java.util.ListIterator;
 
 import org.eclipse.jgit.api.AddCommand;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.PushCommand;
 import org.eclipse.jgit.api.Status;
 import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.lib.RepositoryBuilder;
 import org.eclipse.jgit.lib.TextProgressMonitor;
 import org.eclipse.jgit.transport.RefSpec;
 import org.jfrog.maven.annomojo.annotations.MojoGoal;
 
 import br.com.ingenieux.mojo.beanstalk.AbstractNeedsEnvironmentMojo;
 
+import com.amazonaws.services.elasticbeanstalk.model.ApplicationVersionDescription;
+import com.amazonaws.services.elasticbeanstalk.model.DescribeApplicationVersionsRequest;
+import com.amazonaws.services.elasticbeanstalk.model.DescribeApplicationVersionsResult;
+
 /**
  * Uploads a packed war file to Amazon S3 for further Deployment.
  */
 	@MojoParameter(expression = "${project.build.directory}/${project.build.finalName}")
 	File sourceDirectory;
 
+	@MojoParameter(expression = "${beanstalk.stagingDirectory}", description = "Git Staging Dir (should not be under target/)", defaultValue = "${project.basedir}/tmp-git-deployment-staging")
+	File stagingDirectory;
+
+	@MojoParameter(expression = "${beanstalk.useStagingDirectory}", description = "Use Staging Directory?", defaultValue = "false")
+	boolean useStagingDirectory = false;
+
 	@Override
 	protected void configure() {
 		try {
 
 	@Override
 	protected Object executeInternal() throws Exception {
-		File gitRepo = new File(sourceDirectory, ".git");
-
-		Git git = null;
-
-		if (!gitRepo.exists()) {
-			git = Git.init().setDirectory(sourceDirectory).call();
-		} else {
-			git = Git.open(gitRepo);
-		}
+		Git git = getGitRepo();
 
 		Status status = git.status().call();
 
 
 			return null;
 		}
-		
+
 		// Asks for Existing Files to get added
 		git.add().setUpdate(true).addFilepattern(".").call();
 
 		// Now as for any new files (untracked)
-		
+
 		AddCommand addCommand = git.add();
 
 		if (!status.getUntracked().isEmpty()) {
 			// Ignore
 		}
 
-		String gitVersion = "git-" + commitId;
+		String applicationVersionId = null;
 
-		getLog().info("Deployed version " + gitVersion);
+		{
+			String gitVersionBase = "git-" + commitId;
+			DescribeApplicationVersionsResult describeApplicationVersions = getService()
+					.describeApplicationVersions(
+							new DescribeApplicationVersionsRequest()
+									.withApplicationName(applicationName));
+			boolean found = false;
+
+			ListIterator<ApplicationVersionDescription> versions = describeApplicationVersions
+					.getApplicationVersions().listIterator();
+			
+			while (! found) {
+				ApplicationVersionDescription curVer = versions.next();
+				
+				found = curVer.getVersionLabel().startsWith(gitVersionBase);
+				
+				if (found) {
+					applicationVersionId = curVer.getVersionLabel();
+				}
+			}
+		}
+
+		getLog().info("Deployed version " + applicationVersionId);
 
 		return null;
 	}
+
+	private Git getGitRepo() throws Exception {
+		Git git = null;
+		
+		if (!useStagingDirectory) {
+			File gitRepo = new File(sourceDirectory, ".git");
+
+			if (!gitRepo.exists()) {
+				git = Git.init().setDirectory(sourceDirectory).call();
+			} else {
+				git = Git.open(gitRepo);
+			}
+		} else {
+			File gitRepo = stagingDirectory;
+			Repository r = null;
+			
+			RepositoryBuilder b = new RepositoryBuilder().setGitDir(stagingDirectory).setWorkTree(sourceDirectory);
+
+			if (!gitRepo.exists()) {
+				gitRepo.getParentFile().mkdirs();
+				
+				r = b.build();
+				
+				r.create();
+			} else {
+				r = b.build();
+			}
+			
+			git = Git.wrap(r);
+		}
+		
+		return git;
+	}
 }