Search in sources :

Example 1 with ContainerTemplate

use of io.fabric8.service.ContainerTemplate in project fabric8 by jboss-fuse.

the class ChildContainerProvider method createKarafContainerController.

private ChildContainerController createKarafContainerController() {
    return new ChildContainerController() {

        @Override
        public CreateChildContainerMetadata create(final CreateChildContainerOptions options, final CreationStateListener listener) {
            final Container parent = fabricService.get().getContainer(options.getParent());
            ContainerTemplate containerTemplate = new ContainerTemplate(parent, options.getJmxUser(), options.getJmxPassword(), false);
            return containerTemplate.execute(new ContainerTemplate.AdminServiceCallback<CreateChildContainerMetadata>() {

                public CreateChildContainerMetadata doWithAdminService(AdminServiceMBean adminService) throws Exception {
                    return doCreateKaraf(adminService, options, listener, parent);
                }
            });
        }

        @Override
        public void start(final Container container) {
            getContainerTemplateForChild(container).execute(new ContainerTemplate.AdminServiceCallback<Object>() {

                public Object doWithAdminService(AdminServiceMBean adminService) throws Exception {
                    // update jvm options if they have have changed
                    CreateChildContainerMetadata metadata = (CreateChildContainerMetadata) container.getMetadata();
                    CreateChildContainerOptions createOptions = metadata.getCreateOptions();
                    String jvmOpts = createOptions.getJvmOpts();
                    TabularData instances = adminService.getInstances();
                    Collection<CompositeDataSupport> values = (Collection<CompositeDataSupport>) instances.values();
                    for (CompositeDataSupport o : values) {
                        if (container.getId().equals(o.get("Name"))) {
                            if (o.containsKey("JavaOpts")) {
                                String oldJavaOpts = (String) o.get("JavaOpts");
                                StringBuilder stringBuilder = ChildContainerProvider.buildJvmOpts(createOptions, fabricService.get());
                                String extendendJvmOpts = stringBuilder.toString();
                                if (jvmOpts != null && !extendendJvmOpts.equals(oldJavaOpts)) {
                                    adminService.changeJavaOpts(container.getId(), extendendJvmOpts);
                                }
                            }
                            break;
                        }
                    }
                    adminService.startInstance(container.getId(), null);
                    return null;
                }
            });
        }

        @Override
        public void stop(final Container container) {
            getContainerTemplateForChild(container).execute(new ContainerTemplate.AdminServiceCallback<Object>() {

                public Object doWithAdminService(AdminServiceMBean adminService) throws Exception {
                    String prevProvisionResult = container.getProvisionResult();
                    container.setProvisionResult(Container.PROVISION_STOPPING);
                    try {
                        adminService.stopInstance(container.getId());
                        container.setProvisionResult(Container.PROVISION_STOPPED);
                    } catch (Throwable t) {
                        container.setProvisionResult(prevProvisionResult);
                        LOG.error("Failed to stop container: " + container.getId(), t);
                        throw t;
                    }
                    return null;
                }
            });
        }

        @Override
        public void destroy(final Container container) {
            getContainerTemplateForChild(container).execute(new ContainerTemplate.AdminServiceCallback<Object>() {

                public Object doWithAdminService(AdminServiceMBean adminService) throws Exception {
                    adminService.destroyInstance(container.getId());
                    return null;
                }
            });
        }
    };
}
Also used : CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) AdminServiceMBean(org.apache.karaf.admin.management.AdminServiceMBean) TabularData(javax.management.openmbean.TabularData) ContainerTemplate(io.fabric8.service.ContainerTemplate) Collection(java.util.Collection)

Example 2 with ContainerTemplate

use of io.fabric8.service.ContainerTemplate in project fabric8 by jboss-fuse.

the class ZooKeeperClusterServiceImpl method publicPort.

private String publicPort(String containerName, final String port) {
    FabricService fabric = fabricService.get();
    Container container = fabric.getContainer(containerName);
    String user = ZooKeeperUtils.getContainerLogin(runtimeProperties.get());
    String password = "";
    try {
        Properties containerTokens = ZooKeeperUtils.getContainerTokens(obtainValid(curator));
        password = containerTokens.getProperty(user);
    } catch (Exception e) {
        LOGGER.error("Unable to get temp ZK user/pass for administrative purposes", e);
    }
    ContainerTemplate containerTemplate = new ContainerTemplate(container, user, password, false);
    return containerTemplate.execute(new JmxTemplateSupport.JmxConnectorCallback<String>() {

        @Override
        public String doWithJmxConnector(JMXConnector connector) throws Exception {
            return connector.getMBeanServerConnection().invoke(new ObjectName("io.fabric8:type=Fabric"), "getPublicPortOnCurrentContainer", new Object[] { new Integer(port) }, new String[] { "int" }).toString();
        }
    });
}
Also used : ContainerTemplate(io.fabric8.service.ContainerTemplate) JmxTemplateSupport(io.fabric8.service.JmxTemplateSupport) JMXConnector(javax.management.remote.JMXConnector) Properties(java.util.Properties) ObjectName(javax.management.ObjectName)

Aggregations

ContainerTemplate (io.fabric8.service.ContainerTemplate)2 JmxTemplateSupport (io.fabric8.service.JmxTemplateSupport)1 Collection (java.util.Collection)1 Properties (java.util.Properties)1 ObjectName (javax.management.ObjectName)1 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)1 TabularData (javax.management.openmbean.TabularData)1 JMXConnector (javax.management.remote.JMXConnector)1 AdminServiceMBean (org.apache.karaf.admin.management.AdminServiceMBean)1