Permalink
Browse files

revert accidential push back to 3f89370

  • Loading branch information...
1 parent 0089f6f commit b20eaf8392162ab3e5f726d6d7ae7e01006690b5 @ryanbrainard ryanbrainard committed Feb 24, 2012
View
@@ -1 +1 @@
-/target
+target
View
@@ -2,54 +2,10 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
- <groupId>com.heroku</groupId>
- <artifactId>shareappworker</artifactId>
+ <groupId>org.example</groupId>
+ <artifactId>herokujavasample</artifactId>
<version>1.0-SNAPSHOT</version>
- <repositories>
- <repository>
- <id>mavencentral</id>
- <name>mavencentral</name>
- <url>http://repo1.maven.org/maven2</url>
- </repository>
- </repositories>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>com.heroku.api</groupId>
- <artifactId>heroku-api</artifactId>
- <version>0.2</version>
- </dependency>
- <dependency>
- <groupId>com.heroku.api</groupId>
- <artifactId>heroku-json-gson</artifactId>
- <version>0.2</version>
- </dependency>
- <dependency>
- <groupId>com.heroku.api</groupId>
- <artifactId>heroku-http-apache</artifactId>
- <version>0.2</version>
- </dependency>
- <dependency>
- <groupId>org.eclipse.jgit</groupId>
- <artifactId>org.eclipse.jgit</artifactId>
- <version>1.2.0.201112221803-r</version>
- </dependency>
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.0.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-io</artifactId>
- <version>1.3.2</version>
- </dependency>
- </dependencies>
+
<build>
<plugins>
<plugin>
@@ -60,9 +16,13 @@
<assembleDirectory>target</assembleDirectory>
<programs>
<program>
- <mainClass>com.heroku.ShareAppWorker</mainClass>
+ <mainClass>WorkerProcess</mainClass>
<name>worker</name>
</program>
+ <program>
+ <mainClass>OneOffProcess</mainClass>
+ <name>oneoff</name>
+ </program>
</programs>
</configuration>
<executions>
@@ -1,20 +0,0 @@
-package com.heroku;
-
-import com.heroku.api.App;
-
-
-public class AppCloneRequest {
- public AppCloneRequest(String emailAddress, String gitUrl,String status) {
- super();
- this.emailAddress = emailAddress;
- this.gitUrl = gitUrl;
- this.status=status;
- }
- public String emailAddress;
- public String gitUrl;
- public String id;
- public String status;
- public String appName;
- public String appUrl;
- public String appGitUrl;;
-}
@@ -1,57 +0,0 @@
-package com.heroku;
-
-
-import org.apache.commons.codec.EncoderException;
-import org.apache.commons.codec.binary.Base64;
-import org.apache.commons.codec.binary.StringUtils;
-import org.apache.http.HttpResponse;
-import org.apache.http.NameValuePair;
-import org.apache.http.auth.AuthScope;
-import org.apache.http.auth.UsernamePasswordCredentials;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.entity.UrlEncodedFormEntity;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.client.methods.HttpUriRequest;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.message.BasicNameValuePair;
-
-import java.io.IOException;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.List;
-
-public class EmailHelper {
-
- public static void sendEmailViaMailGun(String senderEmail, String receiverEmail, String subject, String body) throws IOException, URISyntaxException, EncoderException {
-
- if (System.getenv("MAILGUN_API_KEY") == null) {
- throw new RuntimeException("MAILGUN_API_KEY environment variable not found");
- }
-
- if (System.getenv("MAILGUN_SMTP_LOGIN") == null) {
- throw new RuntimeException("MAILGUN_SMTP_LOGIN environment variable not found");
- }
-
- String mailGunApp = System.getenv("MAILGUN_SMTP_LOGIN").split("@")[1];
-
- DefaultHttpClient httpClient = new DefaultHttpClient();
-
- List<NameValuePair> formPairs = new ArrayList<NameValuePair>();
- formPairs.add(new BasicNameValuePair("from", senderEmail));
- formPairs.add(new BasicNameValuePair("to", receiverEmail));
- formPairs.add(new BasicNameValuePair("subject", subject));
- formPairs.add(new BasicNameValuePair("text", body));
-
- UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formPairs);
-
- HttpPost httpPost = new HttpPost("https://api.mailgun.net/v2/" + mailGunApp + "/messages");
- httpPost.setEntity(urlEncodedFormEntity);
-
- String encoding = new String(Base64.encodeBase64(StringUtils.getBytesUtf8("api:" + System.getenv("MAILGUN_API_KEY"))));
- httpPost.setHeader("Authorization", "Basic " + encoding);
-
- HttpResponse response = httpClient.execute(httpPost);
- }
-
-}
@@ -1,191 +0,0 @@
-package com.heroku;
-
-import com.heroku.api.Heroku;
-import com.heroku.api.HerokuAPI;
-import com.heroku.api.connection.HttpClientConnection;
-import com.heroku.api.App;
-import com.heroku.api.request.key.KeyAdd;
-import com.heroku.api.request.key.KeyRemove;
-import com.heroku.api.request.login.BasicAuthLogin;
-import com.heroku.api.request.sharing.SharingAdd;
-import com.heroku.api.request.sharing.SharingRemove;
-import com.heroku.api.request.sharing.SharingTransfer;
-import com.heroku.api.response.Unit;
-import com.jcraft.jsch.JSch;
-import com.jcraft.jsch.JSchException;
-import com.jcraft.jsch.KeyPair;
-import org.apache.commons.io.FileUtils;
-import org.eclipse.jgit.api.CloneCommand;
-import org.eclipse.jgit.api.Git;
-import org.eclipse.jgit.api.errors.*;
-import org.eclipse.jgit.lib.Repository;
-import org.eclipse.jgit.lib.RepositoryBuilder;
-
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.UUID;
-
-public class HerokuAppSharingHelper {
-
- private static final String SSH_KEY_COMMENT = "share@heroku";
-
- String emailAddress;
- String gitUrl;
-
- public Throwable exception;
-
- public HerokuAppSharingHelper(String emailAddress, String gitUrl) {
- this.emailAddress = emailAddress;
- this.gitUrl = gitUrl;
- }
-
- public App cloneApp() throws Exception {
- App app = null;
-
- try {
- HttpClientConnection herokuConnection = new HttpClientConnection(new BasicAuthLogin(System.getenv("HEROKU_USERNAME"), System.getenv("HEROKU_PASSWORD")));
- HerokuAPI herokuAPI = new HerokuAPI(herokuConnection);
- // create an app on heroku (using heroku credentials specified in ${HEROKU_USERNAME} / ${HEROKU_PASSWORD}
- app = herokuAPI.createApp(new App().on(Heroku.Stack.Cedar));
- log("APPCRT","Got API Key for super User");
- if (!app.getCreateStatus().equals("complete")) {
- throw new RuntimeException("Could not create the Heroku app");
- }
- log("APIKEY","Got API Key for super User");
- // write the public key to a file
- String fakeUserHome = System.getProperty("java.io.tmpdir") + File.separator + UUID.randomUUID().toString();
-
- File fakeUserHomeSshDir = new File(fakeUserHome + File.separator + ".ssh");
- fakeUserHomeSshDir.mkdirs();
- log("FLSCRT",String.format("Created tmp directory at %s",fakeUserHomeSshDir.getAbsolutePath()));
- JSch jsch = new JSch();
- KeyPair keyPair = KeyPair.genKeyPair(jsch, KeyPair.RSA);
- keyPair.writePrivateKey(fakeUserHomeSshDir.getAbsolutePath() + File.separator + "id_rsa");
- keyPair.writePublicKey(fakeUserHomeSshDir.getAbsolutePath() + File.separator + "id_rsa.pub", SSH_KEY_COMMENT);
- log("SSHKEY",String.format("Wrote private/public key pair to file system"));
-
- ByteArrayOutputStream publicKeyOutputStream = new ByteArrayOutputStream();
- keyPair.writePublicKey(publicKeyOutputStream, SSH_KEY_COMMENT);
- publicKeyOutputStream.close();
- String sshPublicKey = new String(publicKeyOutputStream.toByteArray());
-
- File knownHostsFile = new File("src/main/resources/known_hosts");
- FileUtils.copyFileToDirectory(knownHostsFile, fakeUserHomeSshDir);
- // add the key pair to ${HEROKU_USERNAME}
- KeyAdd keyAdd = new KeyAdd(sshPublicKey);
- Unit keyAddResponse = herokuConnection.execute(keyAdd);
- log("ADDKEY",String.format("Added Keys to account"));
-
- if (keyAddResponse == null) {
- throw new RuntimeException("Could not add an ssh key to the user");
- }
-
- URI sourceRepoUri = new URI(gitUrl);
-
- // git clone a repo specified in sourceRepoUri to local disk
- File tmpDir = new File(System.getProperty("java.io.tmpdir") + File.separator + "src" +
- File.separator + sourceRepoUri.getHost() + File.separator + sourceRepoUri.getPath());
-
- Git gitRepo = null;
-
- if (!tmpDir.exists()) {
- CloneCommand cloneCommand = new CloneCommand();
- cloneCommand.setURI(sourceRepoUri.toString());
- cloneCommand.setDirectory(tmpDir);
- gitRepo = cloneCommand.call();
- } else {
- File tmpGitDir = new File(tmpDir.getAbsolutePath() + File.separator + ".git");
- Repository repository = new RepositoryBuilder().setGitDir(tmpGitDir).build();
- gitRepo = new Git(repository);
- gitRepo.pull().call();
- }
- log("GITCLN",String.format("Cloned Git Repo"));
-
- // git push the heroku repo
-
- gitRepo.getRepository().getFS().setUserHome(new File(fakeUserHome));
- gitRepo.push().setRemote(app.getGitUrl()).call();
-
- log("GITPSH",String.format("Pushed to Heroku remote Git repo:%s",app.getGitUrl()));
- // share the app with the provided email
- SharingAdd sharingAdd = new SharingAdd(app.getName(), emailAddress);
- Unit sharingAddResponse = herokuConnection.execute(sharingAdd);
-
- if (sharingAddResponse == null) {
- throw new RuntimeException("Could not add " + emailAddress + " as a collaborator");
- }
-
- log("APPSHR",String.format("Added '%s' as collaborator",emailAddress));
- // transfer the app to the provided email
- SharingTransfer sharingTransfer = new SharingTransfer(app.getName(), emailAddress);
- Unit sharingTransferResponse = herokuConnection.execute(sharingTransfer);
- log("APPOWN",String.format("Made '%s' the owner",emailAddress));
-
- if (sharingTransferResponse == null) {
- throw new RuntimeException("Could not transfer the app to " + emailAddress);
- }
-
- // remove ${HEROKU_USERNAME} as collaborator
- SharingRemove sharingRemove = new SharingRemove(app.getName(), System.getenv("HEROKU_USERNAME"));
- Unit sharingRemoveResponse = herokuConnection.execute(sharingRemove);
- log("CLBRMV",String.format("Removed '%s' from collaborators",System.getenv("HEROKU_USERNAME")));
-
- if (sharingRemoveResponse == null) {
- throw new RuntimeException("Could remove " + System.getenv("HEROKU_USERNAME") + " from the app");
- }
-
- // remove the key pair from ${HEROKU_USERNAME}
- KeyRemove keyRemove = new KeyRemove(SSH_KEY_COMMENT);
- Unit keyRemoveResponse = herokuConnection.execute(keyRemove);
-
- if (keyRemoveResponse == null) {
- throw new RuntimeException("Could not remove ssh key");
- }
- log("KEYRMV",String.format("Removed public keys"));
-
- // cleanup the fakeUserHome
- new File(fakeUserHome).delete();
- log("FLSDEL",String.format("Deleted Temporary file system"));
-
- return app;
-
- } catch (IOException e) {
- log("APPERR","Exception when sharing an app:"+e.getMessage());
- throw new RuntimeException(e);
- } catch (URISyntaxException e) {
- log("APPERR","Exception when sharing an app:"+e.getMessage());
- throw new RuntimeException(e);
- } catch (InvalidConfigurationException e) {
- log("APPERR","Exception when sharing an app:"+e.getMessage());
- throw new RuntimeException(e);
- } catch (InvalidRemoteException e) {
- log("APPERR","Exception when sharing an app:"+e.getMessage());
- throw new RuntimeException(e);
- } catch (WrongRepositoryStateException e) {
- log("APPERR","Exception when sharing an app:"+e.getMessage());
- throw new RuntimeException(e);
- } catch (CanceledException e) {
- log("APPERR","Exception when sharing an app:"+e.getMessage());
- throw new RuntimeException(e);
- } catch (RefNotFoundException e) {
- log("APPERR","Exception when sharing an app:"+e.getMessage());
- throw new RuntimeException(e);
- } catch (DetachedHeadException e) {
- log("APPERR","Exception when sharing an app:"+e.getMessage());
- throw new RuntimeException(e);
- } catch (JSchException e) {
- log("APPERR","Exception when sharing an app:"+e.getMessage());
- throw new RuntimeException(e);
- }
- }
-
-
- private void log(String event,String logMsg){
- System.out.println(String.format("[Requested By:%s] - %s : %s",this.emailAddress,event,logMsg));
- }
-}
@@ -1,28 +0,0 @@
-package com.heroku;
-
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import org.apache.commons.pool.impl.GenericObjectPool.Config;
-
-import redis.clients.jedis.JedisPool;
-import redis.clients.jedis.Protocol;
-
-public class JedisPoolFactory {
-
- protected final Pattern HEROKU_REDISTOGO_URL_PATTERN = Pattern.compile("^redis://([^:]*):([^@]*)@([^:]*):([^/]*)(/)?");
-
- protected static JedisPool pool;
-
- public JedisPool getPool() {
- if (JedisPoolFactory.pool == null) {
- Config config = new Config();
- config.testOnBorrow = true;
- Matcher matcher = HEROKU_REDISTOGO_URL_PATTERN.matcher(System.getenv("REDISTOGO_URL"));
- matcher.matches();
- JedisPoolFactory.pool = new JedisPool(config, matcher.group(3), Integer.parseInt(matcher.group(4)), Protocol.DEFAULT_TIMEOUT, matcher.group(2));
- }
- return JedisPoolFactory.pool;
- }
-
-}
Oops, something went wrong.

0 comments on commit b20eaf8

Please sign in to comment.