use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.
the class DirectInstanceController method addChildren.
@Override
protected void addChildren() throws OpsException {
HostPolicy hostPolicy = model.hostPolicy;
if (hostPolicy == null) {
hostPolicy = new HostPolicy();
}
if (hostPolicy.allowRunInContainer) {
// TODO: The variable initialization probably doesn't belong here
LxcInstanceController lxc = injected(LxcInstanceController.class);
String id = model.getId();
lxc.id = id;
lxc.instanceDir = new File(DirectCloudUtils.LXC_BASE_DIR, id);
lxc.minimumMemoryMB = model.minimumMemoryMb;
addChild(lxc);
if (model.publicPorts != null) {
log.info("Ignoring model.publicPorts: " + model.publicPorts);
// for (int publicPort : model.publicPorts) {
// PublicPorts publicPortForward = injected(PublicPorts.class);
// publicPortForward.publicPort = publicPort;
// publicPortForward.backendPort = publicPort;
// publicPortForward.backendItem = model;
// lxc.addChild(publicPortForward);
// }
}
} else {
// TODO: The variable initialization probably doesn't belong here
KvmInstance kvm = injected(KvmInstance.class);
String id = model.getId();
kvm.id = id;
kvm.instanceDir = new File(DirectCloudUtils.KVM_BASE_DIR, id);
kvm.owner = model.getKey();
kvm.minimumMemoryMB = model.minimumMemoryMb;
kvm.recipeId = model.recipeId;
try {
kvm.sshPublicKey = OpenSshUtils.readSshPublicKey(model.sshPublicKey);
} catch (IOException e) {
throw new OpsException("Error deserializing SSH key", e);
}
addChild(kvm);
// TODO: Remove this... it's only supposed to be a hint
if (model.publicPorts != null) {
for (int publicPort : model.publicPorts) {
throw new UnsupportedOperationException();
//
// PublicPorts publicPortForward = injected(PublicPorts.class);
// publicPortForward.publicPort = publicPort;
// publicPortForward.backendItem = model;
// kvm.addChild(publicPortForward);
}
}
}
}
use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.
the class DownloadImage method handler.
@Handler
public void handler(OpsTarget target) throws OpsException {
if (target.getFilesystemInfoFile(imageFile) == null) {
DirectCloud cloud = OpsContext.get().getInstance(DirectCloud.class);
if (cloud == null) {
throw new IllegalStateException("Cloud instance not found");
}
ImageStore imageStore = cloudHelpers.getImageStore(cloud);
MachineProvider machineProvider = providers.toInterface(cloud, MachineProvider.class);
CloudImage imageInfo = imageFactory.getOrCreateImageId(machineProvider, imageFormats, recipeKey);
if (imageStore == null) {
throw new OpsException("Image store not configured");
}
String fileName = imageInfo.getId() + ".image." + imageInfo.getFormat().name();
// TODO: We don't need rawImage; delete or just request a read-only version
File rawImage = new File(IMAGES_DIR, fileName);
target.mkdir(IMAGES_DIR);
// TODO: Caching / reuse ... need to check md5 though
imageStore.bringToMachine(imageInfo.getId(), target, rawImage);
ImageFormat imageFormat = imageInfo.getFormat();
switch(imageFormat) {
case Tar:
{
target.mkdir(imageFile);
target.executeCommand(Command.build("cd {0}; tar jxf {1}", imageFile, rawImage).setTimeout(TimeSpan.FIVE_MINUTES));
break;
}
case DiskRaw:
{
Command expand = Command.build("gunzip -c {0} | cp --sparse=always /proc/self/fd/0 {1}", rawImage, imageFile);
target.executeCommand(expand.setTimeout(TimeSpan.FIVE_MINUTES));
break;
}
case DiskQcow2:
{
Command expand = Command.build("cp {0} {1}", rawImage, imageFile);
target.executeCommand(expand.setTimeout(TimeSpan.FIVE_MINUTES));
break;
}
default:
throw new OpsException("Unknown image format: " + imageFormat);
}
}
}
use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.
the class GoogleCloudInstanceController method addChildren.
@Override
protected void addChildren() throws OpsException {
final GoogleCloudInstance model = OpsContext.get().getInstance(GoogleCloudInstance.class);
PublicKey rootPublicKey;
try {
rootPublicKey = OpenSshUtils.readSshPublicKey(model.sshPublicKey);
} catch (IOException e) {
throw new OpsException("Cannot read SSH key");
}
CloudInstanceMapper instance;
{
instance = injected(CloudInstanceMapper.class);
instance.instance = model;
addChild(instance);
}
{
SshAuthorizedKey authorizeRoot = instance.addChild(SshAuthorizedKey.class);
authorizeRoot.user = "root";
authorizeRoot.publicKey = rootPublicKey;
}
{
instance.addChild(ConfigureSshd.class);
}
{
OpsProvider<TagChanges> tagChanges = new OpsProvider<TagChanges>() {
@Override
public TagChanges get() {
GoogleComputeMachine machine = OpsContext.get().getInstance(GoogleComputeMachine.class);
TagChanges tagChanges = new TagChanges();
tagChanges.addTags.add(Tag.INSTANCE_KEY.build(model.getKey()));
tagChanges.addTags.addAll(machine.buildAddressTags());
return tagChanges;
}
};
instance.addChild(Tagger.build(model, tagChanges));
}
// Note: We can't bootstrap an instance, because we can't log in to it,
// because the public key is not our service's public key
// if (model.publicPorts != null) {
// for (int publicPort : model.publicPorts) {
// PublicPorts publicPortForward = injected(PublicPorts.class);
// publicPortForward.port = publicPort;
// publicPortForward.backendItem = model;
// kvm.addChild(publicPortForward);
// }
// }
}
use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.
the class GoogleComputeClient method findInstanceByName.
public Instance findInstanceByName(String name) throws OpsException {
try {
log.debug("Retrieving instance by name: " + name);
Instance instance = compute.instances().get(projectId, name).execute();
return instance;
} catch (GoogleJsonResponseException e) {
if (e.getStatusCode() == 404) {
return null;
}
throw new OpsException("Error getting instance", e);
} catch (IOException e) {
throw new OpsException("Error getting instance", e);
}
}
use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.
the class PlatformlayerBackedPool method release.
@Override
public void release(PlatformLayerKey owner, T item) throws OpsException {
for (int i = 0; i < 10; i++) {
ItemBase resource = platformLayer.getItem(resourceKey);
List<Assignment> assignments = findAssignments(resource);
String key = adapter.toKey(item);
Assignment assigned = Assignment.find(assignments, key, subkey);
if (assigned == null) {
throw new OpsException("Resource not assigned");
}
if (!assigned.item.equals(owner.getUrl())) {
throw new OpsException("Resource not held");
}
Tag assignmentTag = assigned.asTag();
TagChanges tagChanges = new TagChanges();
tagChanges.removeTags.add(assignmentTag);
if (null != platformLayer.changeTags(resourceKey, tagChanges, resource.getVersion())) {
return;
}
if (!TimeSpan.ONE_SECOND.doSafeSleep()) {
break;
}
}
// List<PlatformLayerKey> assignedTo = Tag.POOL_ASSIGNMENT.build(t) Tag.ASSIGNED_TO.find(item);
// if (!assignedTo.contains(holder)) {
// throw new OpsException("Resource not owned");
// }
//
// platformLayer.deleteItem(item.getKey());
}
Aggregations