Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing gcd.sh as a local resource with a remote version #76

Merged
merged 6 commits into from
May 27, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
Expand All @@ -31,13 +32,17 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Locale;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

Expand All @@ -52,8 +57,18 @@ public class LocalGcdHelper {

public static final String DEFAULT_PROJECT_ID = "projectid1";
public static final int PORT = 8080;
private static final String GCD = "gcd-head";
private static final String GCD_LOC = '/' + GCD + ".zip";
private static final String GCD_VERSION = "gcd-v1beta2-rev1-2.1.2b";

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

private static final String GCD_FILENAME = GCD_VERSION + ".zip";
private static final URL GCD_URL;

static {
try {
GCD_URL = new URL("http://storage.googleapis.com/gcd/tools/" + GCD_FILENAME);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}


private static class ProcessStreamReader extends Thread {

Expand Down Expand Up @@ -107,7 +122,14 @@ public void start() throws IOException, InterruptedException {
File gcdFolder = gcdPath.toFile();
gcdFolder.deleteOnExit();

This comment was marked as spam.

This comment was marked as spam.


try (ZipInputStream zipIn = new ZipInputStream(getClass().getResourceAsStream(GCD_LOC))) {
File gcdZipFile = new File(System.getProperty("java.io.tmpdir"), GCD_FILENAME);
if (!gcdZipFile.exists()) {
ReadableByteChannel rbc = Channels.newChannel(GCD_URL.openStream());
FileOutputStream fos = new FileOutputStream(gcdZipFile);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
}
try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(gcdZipFile))) {
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
File filePath = new File(gcdFolder, entry.getName());
Expand All @@ -120,27 +142,41 @@ public void start() throws IOException, InterruptedException {
entry = zipIn.getNextEntry();
}
}

File datasetFolder = new File(gcdFolder, GCD + '/' + projectId);
File datasetFolder = new File(gcdFolder, GCD_VERSION + '/' + projectId);
deleteRecurse(datasetFolder.toPath());

// TODO: if System.getProperty("os.name").startsWith("Windows") use cmd.exe /c and gcd.cmd
Process temp = new ProcessBuilder()
ProcessBuilder processBuilder = new ProcessBuilder()
.redirectErrorStream(true)
.directory(new File(gcdFolder, GCD))
.redirectOutput(new File("/dev/null"))
.command("bash", "gcd.sh", "create", "-d", projectId, projectId)
.start();
.directory(new File(gcdFolder, GCD_VERSION));
if (isWindows()) {
processBuilder.command("cmd", "/C", "gcd.cmd", "create", "-p", projectId, projectId);
processBuilder.redirectOutput(new File("NULL:"));
} else {
processBuilder.redirectOutput(new File("/dev/null"));

This comment was marked as spam.

This comment was marked as spam.

processBuilder.command("bash", "gcd.sh", "create", "-p", projectId, projectId);
}

Process temp = processBuilder.start();
temp.waitFor();

temp = new ProcessBuilder()
.directory(new File(gcdFolder, GCD))
.redirectErrorStream(true)
.command("bash", "gcd.sh", "start", "--testing", "--allow_remote_shutdown", projectId)
.start();
processBuilder = new ProcessBuilder()
.directory(new File(gcdFolder, GCD_VERSION))
.redirectErrorStream(true);
if (isWindows()) {
processBuilder.command("cmd", "/C", "gcd.cmd", "start", "--testing",
"--allow_remote_shutdown", projectId);
} else {
processBuilder.command("bash", "gcd.sh", "start", "--testing", "--allow_remote_shutdown",
projectId);
}
temp = processBuilder.start();
processReader = ProcessStreamReader.start(temp, "Dev App Server is now running");
}

private static boolean isWindows() {
return System.getProperty("os.name").toLowerCase(Locale.ENGLISH).indexOf("windows") > -1;
}

private static void extractFile(ZipInputStream zipIn, File filePath) throws IOException {
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) {
byte[] bytesIn = new byte[1024];
Expand Down
Binary file not shown.