Search in sources :

Example 41 with OpsException

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();
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) UnmarshalException(javax.xml.bind.UnmarshalException) JaxbHelper(org.platformlayer.xml.JaxbHelper) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 42 with OpsException

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);
            }
        }
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) KvmInstance(org.platformlayer.service.cloud.direct.ops.kvm.KvmInstance) HostPolicy(org.platformlayer.core.model.HostPolicy) IOException(java.io.IOException) File(java.io.File) LxcInstanceController(org.platformlayer.service.cloud.direct.ops.lxc.LxcInstanceController)

Example 43 with OpsException

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);
        }
    }
}
Also used : DirectCloud(org.platformlayer.service.cloud.direct.model.DirectCloud) MachineProvider(org.platformlayer.ops.machines.MachineProvider) OpsException(org.platformlayer.ops.OpsException) Command(org.platformlayer.ops.Command) CloudImage(org.platformlayer.ops.images.CloudImage) File(java.io.File) ImageStore(org.platformlayer.ops.images.ImageStore) ImageFormat(org.platformlayer.ops.images.ImageFormat) Handler(org.platformlayer.ops.Handler)

Example 44 with OpsException

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);
// }
// }
}
Also used : OpsException(org.platformlayer.ops.OpsException) SshAuthorizedKey(org.platformlayer.ops.ssh.SshAuthorizedKey) OpsProvider(org.platformlayer.ops.OpsProvider) PublicKey(java.security.PublicKey) IOException(java.io.IOException) TagChanges(org.platformlayer.core.model.TagChanges) ConfigureSshd(org.platformlayer.ops.bootstrap.ConfigureSshd) GoogleComputeMachine(org.platformlayer.service.cloud.google.ops.compute.GoogleComputeMachine) GoogleCloudInstance(org.platformlayer.service.cloud.google.model.GoogleCloudInstance)

Example 45 with OpsException

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);
    }
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) OpsException(org.platformlayer.ops.OpsException) Instance(com.google.api.services.compute.model.Instance) IOException(java.io.IOException)

Aggregations

OpsException (org.platformlayer.ops.OpsException)142 IOException (java.io.IOException)39 File (java.io.File)19 ItemBase (org.platformlayer.core.model.ItemBase)19 RepositoryException (org.platformlayer.RepositoryException)18 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)17 Handler (org.platformlayer.ops.Handler)17 Tag (org.platformlayer.core.model.Tag)16 Command (org.platformlayer.ops.Command)16 Machine (org.platformlayer.ops.Machine)13 TagChanges (org.platformlayer.core.model.TagChanges)11 OpsTarget (org.platformlayer.ops.OpsTarget)11 TimeoutException (java.util.concurrent.TimeoutException)10 OpenstackException (org.openstack.client.OpenstackException)10 OpsContext (org.platformlayer.ops.OpsContext)10 X509Certificate (java.security.cert.X509Certificate)9 InetAddress (java.net.InetAddress)8 ProjectId (org.platformlayer.ids.ProjectId)8 ProcessExecution (org.platformlayer.ops.process.ProcessExecution)8 List (java.util.List)7