Search in sources :

Example 16 with Handler

use of org.platformlayer.ops.Handler in project platformlayer by platformlayer.

the class GitServerAssignment method handler.

@Handler
public void handler(GitRepository model) throws Exception {
    PlatformLayerKey assignedTo = Tag.ASSIGNED_TO.findUnique(model.getTags());
    if (OpsContext.isConfigure()) {
        if (assignedTo == null) {
            List<GitService> gitServices = platformLayer.listItems(GitService.class);
            if (gitServices.size() == 0) {
                throw new OpsException("No git service found");
            }
            GitService gitService = RandomChooser.chooseRandom(gitServices);
            if (gitService == null) {
                throw new IllegalStateException();
            }
            assignedTo = gitService.getKey();
            platformLayer.addTag(model.getKey(), Tag.ASSIGNED_TO.build(assignedTo));
        }
    }
    GitService gitService = null;
    if (assignedTo != null) {
        gitService = platformLayer.getItem(assignedTo, GitService.class);
    }
    if (OpsContext.isDelete()) {
        if (gitService == null) {
            log.info("Deleting, but not assigned to a server; nothing to do");
            getRecursionState().setPreventRecursion(true);
            return;
        }
    }
    if (gitService == null) {
        throw new OpsException("No git servers found");
    }
    if (gitService.getState() != ManagedItemState.ACTIVE) {
        throw new OpsException("Server not yet active: " + gitService);
    }
    Machine machine = instances.findMachine(gitService);
    if (machine == null) {
        throw new OpsException("Server machine not found:" + gitService);
    }
    SshKey sshKey = service.getSshKey();
    OpsTarget target = machine.getTarget(sshKey);
    getRecursionState().pushChildScope(OpsTarget.class, target);
}
Also used : SshKey(org.platformlayer.ops.helpers.SshKey) OpsException(org.platformlayer.ops.OpsException) OpsTarget(org.platformlayer.ops.OpsTarget) GitService(org.platformlayer.service.git.model.GitService) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) Machine(org.platformlayer.ops.Machine) Handler(org.platformlayer.ops.Handler)

Example 17 with Handler

use of org.platformlayer.ops.Handler 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 18 with Handler

use of org.platformlayer.ops.Handler in project platformlayer by platformlayer.

the class EnsureFirewallIngress method handler.

@Handler
public void handler(GoogleCloud cloud, GoogleComputeMachine machine) throws OpsException {
    GoogleComputeClient client = googleComputeClientFactory.getComputeClient(cloud);
    // Find the public address, although the Google Cloud firewall may be blocking it
    publicAddress = machine.getNetworkPoint().getBestAddress(NetworkPoint.forPublicInternet());
    String serverLink = machine.getServerSelfLink();
    List<Firewall> rules = client.getInstanceFirewallRules(serverLink);
    Firewall matchingRule = findMatchingRule(rules);
    if (OpsContext.isConfigure()) {
        if (matchingRule == null) {
            Firewall rule = new Firewall();
            rule.setSourceRanges(Arrays.asList("0.0.0.0/0"));
            rule.setName("pl-" + UUID.randomUUID().toString());
            Allowed allowed = new Allowed();
            allowed.setIPProtocol("tcp");
            allowed.setPorts(Arrays.asList("" + model.publicPort));
            rule.setAllowed(Arrays.asList(allowed));
            rule.setNetwork(client.buildNetworkUrl("default"));
            client.createFirewallRule(rule);
        }
    }
    if (OpsContext.isDelete()) {
        if (matchingRule != null) {
            client.deleteFirewallRule(matchingRule);
        }
    }
}
Also used : GoogleComputeClient(org.platformlayer.service.cloud.google.ops.compute.GoogleComputeClient) Allowed(com.google.api.services.compute.model.Firewall.Allowed) Firewall(com.google.api.services.compute.model.Firewall) Handler(org.platformlayer.ops.Handler)

Example 19 with Handler

use of org.platformlayer.ops.Handler in project platformlayer by platformlayer.

the class MountCgroups method handler.

@Handler
public void handler() throws OpsException, IOException {
    // TODO: Only if not installed
    OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
    File cgroupsFile = new File("/cgroup");
    FilesystemInfo info = target.getFilesystemInfoFile(cgroupsFile);
    if (info != null) {
        // TODO: Better idempotency
        return;
    }
    String fstabLine = "\ncgroup\t/cgroup\tcgroup\tdefaults\t0\t0";
    File fstabFile = new File("/etc/fstab");
    String fstab = target.readTextFile(fstabFile);
    fstab += fstabLine;
    FileUpload.upload(target, fstabFile, fstab);
    target.mkdir(cgroupsFile);
    Command mountCommand = Command.build("mount cgroup");
    target.executeCommand(mountCommand);
// mkdir /cgroup
// echo -e "cgroup\t/cgroup\tcgroup\tdefaults\t0\t0" >> /etc/fstab
// mount cgroup
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) FilesystemInfo(org.platformlayer.ops.filesystem.FilesystemInfo) Command(org.platformlayer.ops.Command) File(java.io.File) Handler(org.platformlayer.ops.Handler)

Example 20 with Handler

use of org.platformlayer.ops.Handler in project platformlayer by platformlayer.

the class CloudInstanceMapper method doOperation.

@Handler
public void doOperation() throws OpsException, IOException {
    Tag tag = Tag.build(Tag.ASSIGNED, instance.getKey().getUrl());
    List<DirectHost> hosts = Lists.newArrayList(platformLayer.listItems(DirectHost.class, TagFilter.byTag(tag)));
    if (hosts.size() > 1) {
        // Huh?
        throw new OpsException("Multiple hosts already assigned");
    }
    DirectHost host;
    if (hosts.isEmpty()) {
        if (OpsContext.isDelete()) {
            host = null;
        } else {
            if (createInstance) {
                DirectCloudHost cloudHost = cloudMap.pickHost(instance);
                host = cloudHost.getModel();
                platformLayer.addTag(host.getKey(), tag);
            } else {
                throw new OpsException("Instance not yet assigned");
            }
        }
    } else {
        host = hosts.get(0);
    }
    RecursionState recursion = getRecursionState();
    if (host != null) {
        this.cloud = platformLayer.getItem(host.cloud, DirectCloud.class);
        this.hostTarget = directHelpers.toTarget(host);
        recursion.pushChildScope(cloud);
        recursion.pushChildScope(host);
        recursion.pushChildScope(hostTarget);
    } else {
        if (!OpsContext.isDelete()) {
            throw new IllegalStateException();
        }
        log.info("No host set; won't recurse in");
        recursion.setPreventRecursion(true);
    }
}
Also used : DirectCloud(org.platformlayer.service.cloud.direct.model.DirectCloud) DirectHost(org.platformlayer.service.cloud.direct.model.DirectHost) OpsException(org.platformlayer.ops.OpsException) DirectCloudHost(org.platformlayer.service.cloud.direct.ops.cloud.DirectCloudHost) Tag(org.platformlayer.core.model.Tag) Handler(org.platformlayer.ops.Handler)

Aggregations

Handler (org.platformlayer.ops.Handler)58 File (java.io.File)21 Command (org.platformlayer.ops.Command)17 OpsException (org.platformlayer.ops.OpsException)17 OpsTarget (org.platformlayer.ops.OpsTarget)17 Tag (org.platformlayer.core.model.Tag)8 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)5 SshKey (org.platformlayer.ops.helpers.SshKey)5 Machine (org.platformlayer.ops.Machine)4 PublicKey (java.security.PublicKey)3 Action (org.platformlayer.core.model.Action)3 EndpointInfo (org.platformlayer.core.model.EndpointInfo)3 ItemBase (org.platformlayer.core.model.ItemBase)3 Tags (org.platformlayer.core.model.Tags)3 MachineCreationRequest (org.platformlayer.ops.MachineCreationRequest)3 Map (java.util.Map)2 OpenstackComputeClient (org.openstack.client.common.OpenstackComputeClient)2 SecurityGroup (org.openstack.model.compute.SecurityGroup)2 Server (org.openstack.model.compute.Server)2 PlatformLayerClientException (org.platformlayer.PlatformLayerClientException)2