Repository: brooklyn-server Updated Branches: refs/heads/master 7a4ca1c1f -> f43e8380e
Adds karaf application-list & enity-info commands `brooklyn:application-list` Display a list of deployed applications and their state `brooklyn:entity-info <id>` Display detailed information about a single entity Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/01f8c452 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/01f8c452 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/01f8c452 Branch: refs/heads/master Commit: 01f8c452e3d0975726824edab8e704c27d46933a Parents: 7a4ca1c Author: Mark McKenna <[email protected]> Authored: Fri Aug 18 19:02:02 2017 +0100 Committer: Mark McKenna <[email protected]> Committed: Mon Aug 21 14:05:36 2017 +0100 ---------------------------------------------------------------------- .../karaf/commands/ApplicationList.java | 57 ++++++++ .../apache/brooklyn/karaf/commands/Catalog.java | 44 ------ .../brooklyn/karaf/commands/EntityInfo.java | 141 +++++++++++++++++++ .../commands/completers/EntityIdCompleter.java | 42 ++++++ 4 files changed, 240 insertions(+), 44 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/01f8c452/karaf/commands/src/main/java/org/apache/brooklyn/karaf/commands/ApplicationList.java ---------------------------------------------------------------------- diff --git a/karaf/commands/src/main/java/org/apache/brooklyn/karaf/commands/ApplicationList.java b/karaf/commands/src/main/java/org/apache/brooklyn/karaf/commands/ApplicationList.java new file mode 100644 index 0000000..12054da --- /dev/null +++ b/karaf/commands/src/main/java/org/apache/brooklyn/karaf/commands/ApplicationList.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.karaf.commands; + +import org.apache.brooklyn.api.mgmt.ManagementContext; +import org.apache.brooklyn.core.entity.Attributes; +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.lifecycle.Reference; +import org.apache.karaf.shell.api.action.lifecycle.Service; +import org.apache.karaf.shell.support.table.ShellTable; + +@Command(scope = "brooklyn", name = "application-list", description = "Lists all applications") +@Service +public class ApplicationList implements Action { + + @Reference + private ManagementContext managementContext; + + @Override + public Object execute() throws Exception { + System.out.println(); + ShellTable table = new ShellTable(); + table.column("id"); + table.column("name"); + table.column("up"); + table.column("state"); + table.column("expected state"); + + managementContext.getApplications().forEach((application -> table.addRow().addContent( + application.getApplicationId(), + application.getDisplayName(), + application.sensors().get(Attributes.SERVICE_UP).toString(), + application.sensors().get(Attributes.SERVICE_STATE_ACTUAL).toString(), + application.sensors().get(Attributes.SERVICE_STATE_EXPECTED).toString() + ))); + table.print(System.out, true); + System.out.println(); + return null; + } +} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/01f8c452/karaf/commands/src/main/java/org/apache/brooklyn/karaf/commands/Catalog.java ---------------------------------------------------------------------- diff --git a/karaf/commands/src/main/java/org/apache/brooklyn/karaf/commands/Catalog.java b/karaf/commands/src/main/java/org/apache/brooklyn/karaf/commands/Catalog.java deleted file mode 100644 index 406df8f..0000000 --- a/karaf/commands/src/main/java/org/apache/brooklyn/karaf/commands/Catalog.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.brooklyn.karaf.commands; - -import org.apache.karaf.shell.api.action.Action; -import org.apache.karaf.shell.api.action.Argument; -import org.apache.karaf.shell.api.action.Command; -import org.apache.karaf.shell.api.action.Option; -import org.apache.karaf.shell.api.action.lifecycle.Service; - -@Command(scope = "brooklyn", name = "catalog", description = "Manage the local brooklyn catalog") -@Service -public class Catalog implements Action { - - @Option(name = "-o", aliases = { "--option" }, description = "An option to the command", required = false, multiValued = false) - private String option; - - @Argument(name = "argument", description = "Argument to the command", required = false, multiValued = false) - private String argument; - - @Override - public Object execute() throws Exception { - System.out.println("Executing command catalog"); - System.out.println("Option: " + option); - System.out.println("Argument: " + argument); - return null; - } -} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/01f8c452/karaf/commands/src/main/java/org/apache/brooklyn/karaf/commands/EntityInfo.java ---------------------------------------------------------------------- diff --git a/karaf/commands/src/main/java/org/apache/brooklyn/karaf/commands/EntityInfo.java b/karaf/commands/src/main/java/org/apache/brooklyn/karaf/commands/EntityInfo.java new file mode 100644 index 0000000..efade8d --- /dev/null +++ b/karaf/commands/src/main/java/org/apache/brooklyn/karaf/commands/EntityInfo.java @@ -0,0 +1,141 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.karaf.commands; + +import org.apache.brooklyn.api.entity.Entity; +import org.apache.brooklyn.api.mgmt.ManagementContext; +import org.apache.brooklyn.api.sensor.AttributeSensor; +import org.apache.brooklyn.core.entity.Attributes; +import org.apache.brooklyn.core.mgmt.BrooklynTags; +import org.apache.brooklyn.karaf.commands.completers.EntityIdCompleter; +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Argument; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.Completion; +import org.apache.karaf.shell.api.action.Option; +import org.apache.karaf.shell.api.action.lifecycle.Reference; +import org.apache.karaf.shell.api.action.lifecycle.Service; +import org.apache.karaf.shell.support.ansi.SimpleAnsi; +import org.apache.karaf.shell.support.table.ShellTable; + +import java.util.Collections; +import java.util.Optional; + +@Command(scope = "brooklyn", name = "entity-info", description = "Display entity info") +@Service +public class EntityInfo implements Action { + + @Reference + private ManagementContext managementContext; + + @Option(name = "-a", aliases = {"--all"}, description = "Display all information") + private Boolean displayAll = false; + + @Option(name = "-c", aliases = {"--children"}, description = "Display child information") + private Boolean displayChildren = false; + + @Option(name = "-s", aliases = {"--sensors"}, description = "Display sensor information") + private Boolean displaySensors = false; + + @Option(name = "--cfg", aliases = {"--config"}, description = "Display config information") + private Boolean displayConfig = false; + + @Option(name = "--blueprint", description = "Display blueprint") + private Boolean displayBlueprint = false; + + @Argument(index = 0, name = "id", description = "The entity id", required = true) + @Completion(EntityIdCompleter.class) + private String id = null; + + @Override + public Object execute() throws Exception { + Optional<Entity> entity = Optional.ofNullable(managementContext.getEntityManager().getEntity(id)); + if(!entity.isPresent()){ + System.err.println(String.format("Entity [%s] not found", id)); + return null; + } + printHeader("Basic Information"); + final ShellTable infoTable = new ShellTable(); + infoTable.column("").bold(); + infoTable.column(""); + infoTable.noHeaders(); + infoTable.addRow().addContent("Application Id", entity.get().getApplicationId()); + infoTable.addRow().addContent("Application Name", entity.get().getApplication().getDisplayName()); + infoTable.addRow().addContent("Name", entity.get().getDisplayName()); + infoTable.addRow().addContent("Id", id); + infoTable.addRow().addContent("UP", entity.get().sensors().get(Attributes.SERVICE_UP)); + infoTable.addRow().addContent("State", entity.get().sensors().get(Attributes.SERVICE_STATE_ACTUAL)); + infoTable.addRow().addContent("Expected State", entity.get().sensors().get(Attributes.SERVICE_STATE_EXPECTED)); + infoTable.print(System.out, true); + if (displayChildren || displayAll) { + printHeader("Child Information"); + final ShellTable childrenTable = new ShellTable(); + childrenTable.column("id"); + childrenTable.column("name"); + + entity.get().getChildren().forEach(child -> childrenTable.addRow().addContent(child.getId(), child.getDisplayName())); + + childrenTable.print(System.out, true); + } + if (displaySensors || displayAll) { + printHeader("Sensor Information"); + final ShellTable sensorTable = new ShellTable(); + sensorTable.column("name"); + sensorTable.column("value"); + sensorTable.column("description"); + + entity.get().getEntityType().getSensors().stream() + .filter(AttributeSensor.class::isInstance).map(AttributeSensor.class::cast) + .forEach(sensor -> sensorTable.addRow().addContent(sensor.getName(), entity.get().getAttribute(sensor), sensor.getDescription())); + + sensorTable.print(System.out, true); + } + + if (displayConfig || displayAll) { + printHeader("Config Information"); + final ShellTable configTable = new ShellTable(); + configTable.column("name"); + configTable.column("value"); + configTable.column("description"); + + entity.get().getEntityType().getConfigKeys().stream() + .forEach(configKey -> configTable.addRow().addContent(configKey.getName(), entity.get().getConfig(configKey), configKey.getDescription())); + configTable.print(System.out, true); + } + + if (displayBlueprint || displayAll) { + final Optional<String> bluePrint = Optional.ofNullable(BrooklynTags.findFirst(BrooklynTags.YAML_SPEC_KIND, entity.get().tags().getTags())) + .map(BrooklynTags.NamedStringTag::getContents); + if (bluePrint.isPresent()) { + printHeader("Blueprint Information"); + System.out.println("---"); + System.out.print(bluePrint.get()); + System.out.println("..."); + } + } + + System.out.println(); + return null; + } + + private void printHeader(String header) { + System.out.println("\n" + SimpleAnsi.COLOR_CYAN + SimpleAnsi.INTENSITY_BOLD + header + SimpleAnsi.INTENSITY_NORMAL + SimpleAnsi.COLOR_DEFAULT); + System.out.println(SimpleAnsi.INTENSITY_BOLD + String.join("", Collections.nCopies(header.length(), "-")) + SimpleAnsi.INTENSITY_NORMAL); + } +} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/01f8c452/karaf/commands/src/main/java/org/apache/brooklyn/karaf/commands/completers/EntityIdCompleter.java ---------------------------------------------------------------------- diff --git a/karaf/commands/src/main/java/org/apache/brooklyn/karaf/commands/completers/EntityIdCompleter.java b/karaf/commands/src/main/java/org/apache/brooklyn/karaf/commands/completers/EntityIdCompleter.java new file mode 100644 index 0000000..0ac5b8e --- /dev/null +++ b/karaf/commands/src/main/java/org/apache/brooklyn/karaf/commands/completers/EntityIdCompleter.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.karaf.commands.completers; + +import org.apache.brooklyn.api.mgmt.ManagementContext; +import org.apache.karaf.shell.api.action.lifecycle.Reference; +import org.apache.karaf.shell.api.action.lifecycle.Service; +import org.apache.karaf.shell.api.console.CommandLine; +import org.apache.karaf.shell.api.console.Completer; +import org.apache.karaf.shell.api.console.Session; +import org.apache.karaf.shell.support.completers.StringsCompleter; +import java.util.List; + +@Service +public class EntityIdCompleter implements Completer { + + @Reference + private ManagementContext managementContext; + + @Override + public int complete(final Session session, final CommandLine commandLine, final List<String> candidates) { + final StringsCompleter delegate = new StringsCompleter(); + managementContext.getEntityManager().getEntities().forEach(entity -> delegate.getStrings().add(entity.getId())); + return delegate.complete(session, commandLine, candidates); + } +}
