/*
* This file is part of CloudEclipse.
*
* Copyright (c) 2009 Chris Matthews
*
* CloudEclipse is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CloudEclipse is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CloudEclipse. If not, see <http://www.gnu.org/licenses/>.
*
*/
package ca.uvic.cs.cloud.cloudwizard;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
public class CloudBuildRunner extends org.eclipse.core.runtime.jobs.Job {
private BuildSettingsStore bs;
public CloudBuildRunner(BuildSettingsStore bs) {
super("Building Cloud Image");
this.bs = bs;
}
@Override
protected IStatus run(IProgressMonitor monitor) {
if(monitor == null)
monitor = new NullProgressMonitor();
monitor.beginTask("Building Cloud Image", IProgressMonitor.UNKNOWN);
if(monitor.isCanceled())
throw new OperationCanceledException();
prepareWorkingDir();
doWork();
monitor.done();
return Status.OK_STATUS;
}
private IStatus prepareWorkingDir() {
String installDir = bs.getValue(bs.INSTALL_PATH);
String execDir = bs.getValue(bs.EXEC_PATH);
File f = new File(installDir);
if (f.exists() && !f.isDirectory()) {
return new Status(IStatus.ERROR, Exporter.PLUGIN_ID, "Cannot Create Install Directory.");
}
if (!f.exists()) {
f.mkdirs();
}
File appDir = new File(installDir + "/app/");
appDir.mkdir();
Process p = null;
try {
String s = "cp -r " + execDir+ "/. " + appDir.getAbsolutePath()+"/";
System.out.println(s);
p = Runtime.getRuntime().exec(s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
p.waitFor();
if (p.exitValue() != 0) {
return new Status(IStatus.ERROR, Exporter.PLUGIN_ID, "Cannot copy program files.");
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
String s = "cp " + bs.getStoreFile()+" " + f.getAbsolutePath()+"/";
System.out.println(s);
p = Runtime.getRuntime().exec(s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
p.waitFor();
if (p.exitValue() != 0) {
return new Status(IStatus.ERROR, Exporter.PLUGIN_ID, "Cannot copy configure files.");
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Status.OK_STATUS;
}
private IStatus doWork() {
String installDir = bs.getValue(bs.INSTALL_PATH);
Process p = null;
try {
String s = "java -Xms40m -Xmx256m -Declipse.p2.data.area="+installDir+"/p2 -jar /home/cmatthew/bin/eclipse-head2/eclipse/plugins/org.eclipse.equinox.launcher_1.0.200.v20090520.jar " +
" -application org.eclipse.equinox.p2.director " +
" -console " +
"-metadataRepository file:/p2 " +
"-artifactRepository file:/p2 " +
"-installIU org.example.xenRoot " +
"-installIU org.example.yourapp " +
"-installIU org.example.xenjvm " +
"-destination " + installDir + " " +
"-bundlepool " + installDir;
System.out.println(s);
p = Runtime.getRuntime().exec(s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
p.waitFor();
readOffStream(p.getErrorStream(), true);
readOffStream(p.getInputStream(), true);
if (p.exitValue() != 0) {
return new Status(IStatus.ERROR, Exporter.PLUGIN_ID, "Cannot copy program files.");
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Status.OK_STATUS;
}
public static void readOffStream(InputStream inputStream, boolean show) {
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream));
try {
String output = null;
while ((output = reader.readLine()) != null) {
if (show) {
System.out.println(output);
}
}
} catch (IOException e) {
// ignore
} finally {
try {
reader.close();
} catch (IOException e) {
// ignore
}
}
}
}