/*
* This file is part of CloudEclipse.
*
* Copyright (c) 2009 Chris Matthews
* with help from Ian Bull
*
* 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 org.example.xen.publisher;
import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.internal.p2.metadata.ArtifactKey;
import org.eclipse.equinox.internal.p2.metadata.RequiredCapability;
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactDescriptor;
import org.eclipse.equinox.internal.provisional.p2.core.Version;
import org.eclipse.equinox.internal.provisional.p2.core.VersionRange;
import org.eclipse.equinox.internal.provisional.p2.metadata.IArtifactKey;
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.internal.provisional.p2.metadata.IProvidedCapability;
import org.eclipse.equinox.internal.provisional.p2.metadata.IRequiredCapability;
import org.eclipse.equinox.internal.provisional.p2.metadata.ITouchpointData;
import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory;
import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription;
import org.eclipse.equinox.p2.publisher.AbstractPublisherAction;
import org.eclipse.equinox.p2.publisher.IPublisherInfo;
import org.eclipse.equinox.p2.publisher.IPublisherResult;
import org.eclipse.equinox.spi.p2.publisher.PublisherHelper;
@SuppressWarnings("restriction")
public class XenPublishingAction extends AbstractPublisherAction {
private final String repofiles = "/home/cmatthew/workspaces/osgi/repo_files/";
public IStatus perform(IPublisherInfo publisherInfo, IPublisherResult results, IProgressMonitor monitor) {
String filename = "centos.5-3.x86.img.zip";
//Make Root IU
String time = Long.toString(System.currentTimeMillis());
InstallableUnitDescription vmIU = new InstallableUnitDescription();
vmIU.setId("org.example.xenVM");
vmIU.setVersion(Version.createOSGi(1, 0, 0, time));
vmIU.setTouchpointType(PublisherHelper.TOUCHPOINT_NATIVE);
IProvidedCapability createSelfCapability = PublisherHelper.createSelfCapability(vmIU.getId(), vmIU.getVersion());
vmIU.setCapabilities(new IProvidedCapability[] {createSelfCapability});
InstallableUnitDescription xenRootIU = new InstallableUnitDescription();
xenRootIU.setId("org.example.xenRoot");
xenRootIU.setVersion(Version.createOSGi(1, 0, 0, time));
Map<String, String> touchpointData = new HashMap<String, String>(7);
touchpointData.put("collect", "collect();");
touchpointData.put("install",
"mkdir(path:${installFolder}/image);" +
"unzip(source:@artifact, target:${installFolder}/image, buildFile:${installFolder}/build.properties);" +
"mkdir(path:${installFolder}/mountpoint);" +
"mount(options:-o loop,targetDir:${installFolder}, mountpoint:mountpoint, targetFile:image/centos.5-3.x86.img, buildFile:${installFolder}/build.properties);" +
"mount(options:-t proc,targetDir:${installFolder}, mountpoint:mountpoint/proc, targetFile:proc, buildFile:${installFolder}/build.properties);" +
//"chrootExec(chroot:${installFolder}/mountpoint/, chrootExec:ls -l /);" +
"");
xenRootIU.setTouchpointType(PublisherHelper.TOUCHPOINT_NATIVE);
ITouchpointData tpd = MetadataFactory.createTouchpointData(touchpointData);
xenRootIU.addTouchpointData(tpd);
ArtifactKey xen_image = new ArtifactKey(PublisherHelper.BINARY_ARTIFACT_CLASSIFIER, filename, Version.create("1.0.0"));
xenRootIU.setArtifacts(new IArtifactKey[] {xen_image});
File f = new File(repofiles + filename);
IArtifactDescriptor ad = PublisherHelper.createArtifactDescriptor(xen_image, f);
publishArtifact(ad, f, publisherInfo);
createSelfCapability = PublisherHelper.createSelfCapability(xenRootIU.getId(), xenRootIU.getVersion());
xenRootIU.setCapabilities(new IProvidedCapability[] {createSelfCapability});
RequiredCapability vm = new RequiredCapability(PublisherHelper.IU_NAMESPACE, "org.example.xenVM", new VersionRange(Version.create("0.0.0"), true, Version.create("9.9.9"), true), "", false, false);
xenRootIU.setRequiredCapabilities(new IRequiredCapability[] {vm});
ITouchpointData vm_touchpoint = MetadataFactory.createTouchpointData(Collections.EMPTY_MAP);
xenRootIU.addTouchpointData(vm_touchpoint);
results.addIU(makeJVM(publisherInfo), IPublisherResult.NON_ROOT);
results.addIU(makeInstallYourApp(publisherInfo), IPublisherResult.NON_ROOT);
results.addIU(MetadataFactory.createInstallableUnit(vmIU), IPublisherResult.NON_ROOT);
results.addIU(MetadataFactory.createInstallableUnit(xenRootIU), IPublisherResult.ROOT);
return Status.OK_STATUS;
}
private IInstallableUnit makeJVM(IPublisherInfo publisherInfo) {
String filename = "jre-6u15-linux-i586.rpm";
String installInstructions = "copy(source:@artifact, target:${installFolder}/mountpoint/tmp/"+filename+", overwrite:true);" +
"rpm(package:jre-6u15-linux-i586, targetDir:${installFolder}/mountpoint/tmp, chroot:${installFolder}/mountpoint/);";// +
//"umount(options:-l, targetDir:${installFolder}, mountpoint:mountpoint/proc);" +
// "umount(options:-l, targetDir:${installFolder}, mountpoint:mountpoint);" ;
InstallableUnitDescription jvmIU = new InstallableUnitDescription();
jvmIU.setId("org.example.xenjvm");
jvmIU.setVersion(Version.createOSGi(1, 0, 0, Long.toString(System.currentTimeMillis())));
jvmIU.setTouchpointType(PublisherHelper.TOUCHPOINT_NATIVE);
Map<String, String> touchpointData = new HashMap<String, String>(7);
touchpointData.put("collect", "collect();");
touchpointData.put("install", installInstructions);
ITouchpointData tpd = MetadataFactory.createTouchpointData(touchpointData);
jvmIU.addTouchpointData(tpd);
ArtifactKey jvm_rpm = new ArtifactKey(PublisherHelper.BINARY_ARTIFACT_CLASSIFIER, filename , Version.create("1.0.0"));
jvmIU.setArtifacts(new IArtifactKey[] {jvm_rpm});
File f = new File(repofiles + filename);
IArtifactDescriptor ad = PublisherHelper.createArtifactDescriptor(jvm_rpm, f);
publishArtifact(ad, f, publisherInfo);
IProvidedCapability createSelfCapability = PublisherHelper.createSelfCapability(jvmIU.getId(),jvmIU.getVersion());
jvmIU.setCapabilities(new IProvidedCapability[] {createSelfCapability});
//RequiredCapability vm = new RequiredCapability(PublisherHelper.IU_NAMESPACE, "org.example.xenVM", new VersionRange(Version.create("0.0.0"), true, Version.create("9.9.9"), true), "", false, false);
//jvmIU.setRequiredCapabilities(new IRequiredCapability[] {vm});
return MetadataFactory.createInstallableUnit(jvmIU);
}
private IInstallableUnit makeInstallYourApp(IPublisherInfo publisherInfo) {
String filename = "app";
String installInstructions = "copy(source:${installFolder}/"+filename+", target:${installFolder}/mountpoint/tmp/"+filename+", overwrite:true);" +
"chrootExec(chroot:${installFolder}/mountpoint/, chrootExec:mkdir /opt/app);" +
"chrootExec(chroot:${installFolder}/mountpoint/, chrootExec:cp -r /tmp/app/ /opt/);" +
"chrootExec(chroot:${installFolder}/mountpoint/, chrootExec:chown -R root /opt/app);" +
"chrootExec(chroot:${installFolder}/mountpoint/, chrootExec:chmod -R 777 /opt/app);" +
"chrootExec(chroot:${installFolder}/mountpoint/, chrootExec:/opt/app/install-sh.sh);"
;
InstallableUnitDescription yourAppIU = new InstallableUnitDescription();
yourAppIU.setId("org.example.yourapp");
yourAppIU.setVersion(Version.createOSGi(1, 0, 0, Long.toString(System.currentTimeMillis())));
yourAppIU.setTouchpointType(PublisherHelper.TOUCHPOINT_NATIVE);
Map<String, String> touchpointData = new HashMap<String, String>(7);
touchpointData.put("collect", "collect();");
touchpointData.put("install", installInstructions);
ITouchpointData tpd = MetadataFactory.createTouchpointData(touchpointData);
yourAppIU.addTouchpointData(tpd);
//ArtifactKey jvm_rpm = new ArtifactKey(PublisherHelper.BINARY_ARTIFACT_CLASSIFIER, filename , Version.create("1.0.0"));
//jvmIU.setArtifacts(new IArtifactKey[] {jvm_rpm});
//File f = new File(repofiles + filename);
//IArtifactDescriptor ad = PublisherHelper.createArtifactDescriptor(jvm_rpm, f);
//publishArtifact(ad, f, publisherInfo);
IProvidedCapability createSelfCapability = PublisherHelper.createSelfCapability(yourAppIU.getId(),yourAppIU.getVersion());
yourAppIU.setCapabilities(new IProvidedCapability[] {createSelfCapability});
//RequiredCapability vm = new RequiredCapability(PublisherHelper.IU_NAMESPACE, "org.example.xenVM", new VersionRange(Version.create("0.0.0"), true, Version.create("9.9.9"), true), "", false, false);
//jvmIU.setRequiredCapabilities(new IRequiredCapability[] {vm});
return MetadataFactory.createInstallableUnit(yourAppIU);
}
}