use of org.platformlayer.ops.OpsException in project platformlayer by platformlayer.
the class SmartDeserialization method deserialize.
public static <T> T deserialize(Class<T> c, InputStream is) throws OpsException {
// TODO: Auto-detect XML, JSON, others?
String data;
try {
data = IoUtils.readAll(is);
} catch (IOException e) {
throw new OpsException("Error reading data", e);
}
Format format = null;
for (int i = 0; i < data.length(); i++) {
char firstChar = data.charAt(i);
switch(firstChar) {
case ' ':
continue;
case '<':
format = Format.XML;
break;
case '{':
format = Format.JSON;
break;
default:
{
if (Character.isLetter(firstChar)) {
format = Format.PROPERTIES;
} else {
throw new IllegalArgumentException("Unhandled character: " + ((int) firstChar));
}
break;
}
}
if (format != null) {
break;
}
}
if (format == null) {
throw new IllegalStateException("Could not determine format");
}
if (format == Format.XML) {
JaxbHelper jaxb = JaxbHelper.get(c);
try {
return jaxb.deserialize(new StringReader(data), c);
} catch (UnmarshalException e) {
throw new OpsException("Error deserializing item", e);
}
} else {
throw new UnsupportedOperationException();
}
}
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);
}
}
Aggregations