Search in sources :

Example 6 with PlatformLayerClient

use of org.platformlayer.PlatformLayerClient in project platformlayer by platformlayer.

the class SchedulerImpl method toRunnable.

private Runnable toRunnable(final ActionTask task) {
    final PlatformLayerKey target = task.target;
    final PlatformLayerEndpointInfo endpoint = rehydrateEndpoint(task.endpoint);
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            try {
                PlatformLayerClient platformLayer = HttpPlatformLayerClient.build(httpStrategy, endpoint.getPlatformlayerBaseUrl(), endpoint.getAuthenticator(), endpoint.getProjectId(), endpoint.getTrustKeys());
                platformLayer.doAction(target, task.action);
            // TODO: Wait for task completion??
            // TODO: Link job id??
            } catch (PlatformLayerClientException e) {
                log.warn("Error running action", e);
            }
        }

        @Override
        public String toString() {
            return task.action + " on " + task.target;
        }
    };
    return runnable;
}
Also used : HttpPlatformLayerClient(org.platformlayer.HttpPlatformLayerClient) PlatformLayerClient(org.platformlayer.PlatformLayerClient) PlatformLayerClientException(org.platformlayer.PlatformLayerClientException) PlatformLayerEndpointInfo(org.platformlayer.PlatformLayerEndpointInfo) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey)

Example 7 with PlatformLayerClient

use of org.platformlayer.PlatformLayerClient in project platformlayer by platformlayer.

the class FederationMap method buildClient.

public TypedPlatformLayerClient buildClient(FederationMapping key) {
    MappedTarget target = targetMap.get(key);
    if (target == null) {
        throw new IllegalArgumentException("Unknown key: " + key);
    }
    if (target.client == null) {
        PlatformLayerClient client = HttpPlatformLayerClient.buildUsingConfiguration(httpStrategy, target.configuration);
        TypedPlatformLayerClient typedClient = new TypedPlatformLayerClient(client, mapper);
        // TODO: Save client??
        return typedClient;
    } else {
        return target.client;
    }
}
Also used : TypedPlatformLayerClient(org.platformlayer.TypedPlatformLayerClient) HttpPlatformLayerClient(org.platformlayer.HttpPlatformLayerClient) PlatformLayerClient(org.platformlayer.PlatformLayerClient) TypedPlatformLayerClient(org.platformlayer.TypedPlatformLayerClient)

Example 8 with PlatformLayerClient

use of org.platformlayer.PlatformLayerClient in project platformlayer by platformlayer.

the class SshItem method runCommand.

@Override
public Object runCommand() throws PlatformLayerClientException {
    PlatformLayerClient client = getPlatformLayerClient();
    PlatformLayerKey key = path.resolve(getContext());
    UntypedItem untypedItem = client.getItemUntyped(key, Format.XML);
    InetAddress sshAddress = findSshAddress(client, untypedItem);
    ClientAction action = null;
    if (sshAddress != null) {
        String user = "root";
        ProjectId project = key.getProject();
        if (project == null) {
            project = client.getProject();
        }
        if (project == null) {
            throw new CliException("Cannot determine project");
        }
        String projectKey = project.getKey();
        String serviceKey = "service-" + key.getServiceType().getKey();
        File sshKey = IoUtils.resolve("~/.credentials/ssh/" + projectKey + "/" + serviceKey);
        // Hmmm... user? key?
        action = new ClientAction(ClientAction.ClientActionType.SSH, user + "@" + sshAddress.getHostAddress(), sshKey.getAbsolutePath());
    }
    return action;
}
Also used : PlatformLayerClient(org.platformlayer.PlatformLayerClient) ClientAction(com.fathomdb.cli.output.ClientAction) UntypedItem(org.platformlayer.common.UntypedItem) CliException(com.fathomdb.cli.CliException) ProjectId(org.platformlayer.ids.ProjectId) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) InetAddress(java.net.InetAddress) File(java.io.File)

Example 9 with PlatformLayerClient

use of org.platformlayer.PlatformLayerClient in project platformlayer by platformlayer.

the class OpenItem method runCommand.

@Override
public Object runCommand() throws PlatformLayerClientException {
    PlatformLayerClient client = getPlatformLayerClient();
    PlatformLayerKey key = path.resolve(getContext());
    UntypedItem untypedItem = client.getItemUntyped(key, getFormat());
    List<EndpointInfo> endpointList = EndpointInfo.getEndpoints(untypedItem.getTags());
    Set<EndpointInfo> endpoints = Sets.newHashSet(endpointList);
    EndpointInfo bestEndpoint = null;
    for (EndpointInfo candidate : endpoints) {
        if (bestEndpoint == null) {
            bestEndpoint = candidate;
        } else {
            throw new IllegalArgumentException("Cannot choose between: " + bestEndpoint + " and " + candidate);
        }
    }
    ClientAction action = null;
    if (bestEndpoint != null) {
        // TODO: How do we want to do this? A new tag??
        String id = key.getServiceType().getKey() + ":" + key.getItemType().getKey();
        if (id.equals("jenkins:jenkinsService")) {
            action = new ClientAction(ClientAction.ClientActionType.BROWSER, "http://" + bestEndpoint.publicIp + ":" + bestEndpoint.publicIp);
        }
    }
    return action;
}
Also used : PlatformLayerClient(org.platformlayer.PlatformLayerClient) ClientAction(com.fathomdb.cli.output.ClientAction) UntypedItem(org.platformlayer.common.UntypedItem) EndpointInfo(org.platformlayer.core.model.EndpointInfo) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey)

Example 10 with PlatformLayerClient

use of org.platformlayer.PlatformLayerClient in project platformlayer by platformlayer.

the class PutItem method runCommand.

@Override
public Object runCommand() throws PlatformLayerClientException, JSONException {
    PlatformLayerClient client = getPlatformLayerClient();
    if (json == null) {
        InputStream stream = new NoCloseInputStream(System.in);
        byte[] data;
        try {
            data = ByteStreams.toByteArray(stream);
            json = new String(data, Charsets.UTF_8);
        } catch (IOException e) {
            throw new CliException("Error reading stdin", e);
        }
    }
    JSONObject jsonObject = new JSONObject(json);
    PlatformLayerKey parentKey = null;
    if (parent != null || !tags.isEmpty()) {
        JSONObject tagsObject = null;
        if (jsonObject.has("tags")) {
            tagsObject = jsonObject.getJSONObject("tags");
        } else {
            tagsObject = new JSONObject();
            jsonObject.put("tags", tagsObject);
        }
        JSONArray tagsArray;
        if (tagsObject.has("tags")) {
            tagsArray = tagsObject.getJSONArray("tags");
        } else {
            tagsArray = new JSONArray();
            tagsObject.put("tags", tagsArray);
        }
        if (parent != null) {
            parentKey = parent.resolve(getContext());
            Tag parentTag = Tag.buildParentTag(parentKey);
            JSONObject jsonTag = new JSONObject();
            jsonTag.put("key", parentTag.getKey());
            jsonTag.put("value", parentTag.getValue());
            tagsArray.put(jsonTag);
        }
        for (String tag : tags) {
            int equalsIndex = tag.indexOf('=');
            if (equalsIndex == -1) {
                throw new CliException("Expected tagname=tagvalue");
            }
            String tagName = tag.substring(0, equalsIndex);
            String tagValue = tag.substring(equalsIndex + 1);
            JSONObject jsonTag = new JSONObject();
            jsonTag.put("key", tagName);
            jsonTag.put("value", tagValue);
            tagsArray.put(jsonTag);
        }
    }
    PlatformLayerKey key = path.resolve(getContext());
    boolean wrap = false;
    String data;
    if (wrap) {
        JSONObject wrapped = new JSONObject();
        wrapped.put(key.getItemType().getKey(), jsonObject);
        data = wrapped.toString();
    } else {
        data = jsonObject.toString();
    }
    UntypedItem retval = client.putItem(key, data, Format.JSON);
    return retval;
}
Also used : UntypedItem(org.platformlayer.common.UntypedItem) NoCloseInputStream(com.fathomdb.io.NoCloseInputStream) InputStream(java.io.InputStream) JSONArray(org.json.JSONArray) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) IOException(java.io.IOException) PlatformLayerClient(org.platformlayer.PlatformLayerClient) NoCloseInputStream(com.fathomdb.io.NoCloseInputStream) CliException(com.fathomdb.cli.CliException) JSONObject(org.json.JSONObject) Tag(org.platformlayer.core.model.Tag)

Aggregations

PlatformLayerClient (org.platformlayer.PlatformLayerClient)36 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)21 UntypedItem (org.platformlayer.common.UntypedItem)7 TypedPlatformLayerClient (org.platformlayer.TypedPlatformLayerClient)4 PlatformLayerCliContext (org.platformlayer.client.cli.PlatformLayerCliContext)4 JobData (org.platformlayer.jobs.model.JobData)4 CliException (com.fathomdb.cli.CliException)3 InputStream (java.io.InputStream)3 HttpPlatformLayerClient (org.platformlayer.HttpPlatformLayerClient)3 UntypedItemXml (org.platformlayer.UntypedItemXml)3 Tag (org.platformlayer.core.model.Tag)3 ProjectId (org.platformlayer.ids.ProjectId)3 JobExecutionList (org.platformlayer.jobs.model.JobExecutionList)3 ClientAction (com.fathomdb.cli.output.ClientAction)2 NoCloseInputStream (com.fathomdb.io.NoCloseInputStream)2 IOException (java.io.IOException)2 JSONObject (org.codehaus.jettison.json.JSONObject)2 JSONObject (org.json.JSONObject)2 EndpointInfo (org.platformlayer.core.model.EndpointInfo)2 Link (org.platformlayer.core.model.Link)2