Search in sources :

Example 6 with Agent

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

the class FabricConfigAdminBridge method updateInternal.

/**
 * Method scheduled to run in separate thread - so be careful, as we may be running in deactivated SCR
 * component.
 * @throws Exception
 */
private synchronized void updateInternal() throws Exception {
    try {
        Container currentContainer = fabricService.get().getCurrentContainer();
        if (currentContainer == null) {
            LOGGER.warn("No current container yet so cannot update!");
            return;
        }
        Profile overlayProfile = null;
        try {
            overlayProfile = currentContainer.getOverlayProfile();
        } catch (RuntimeException e) {
            LOGGER.warn("No profile data yet so cannot update!");
            return;
        }
        Profile effectiveProfile = Profiles.getEffectiveProfile(fabricService.get(), overlayProfile);
        Map<String, Map<String, String>> configurations = effectiveProfile.getConfigurations();
        List<Configuration> zkConfigs = asList(configAdmin.get().listConfigurations("(" + FABRIC_ZOOKEEPER_PID + "=*)"));
        // Process all configurations but agent
        for (String pid : configurations.keySet()) {
            if (!pid.equals(Constants.AGENT_PID)) {
                Hashtable<String, Object> c = new Hashtable<String, Object>(configurations.get(pid));
                if (!updateConfig(zkConfigs, pid, c)) {
                    return;
                }
            }
        }
        // Process agent configuration last
        for (String pid : configurations.keySet()) {
            if (pid.equals(Constants.AGENT_PID)) {
                Hashtable<String, Object> c = new Hashtable<String, Object>(configurations.get(pid));
                c.put(Profile.HASH, String.valueOf(effectiveProfile.getProfileHash()));
                if (!updateConfig(zkConfigs, pid, c)) {
                    return;
                }
            }
        }
        for (Configuration config : zkConfigs) {
            LOGGER.info("Deleting configuration {}", config.getPid());
            fabricService.get().getPortService().unregisterPort(fabricService.get().getCurrentContainer(), config.getPid());
            if (!isValid()) {
                return;
            }
            config.delete();
        }
        // end of update
        Configuration fcab = configAdmin.get().getConfiguration(Constants.CONFIGADMIN_BRIDGE_PID, null);
        Hashtable<String, String> props = new Hashtable<>();
        props.put("lastUpdate", Long.toString(new Date().getTime()));
        fcab.update(props);
    } catch (IllegalStateException e) {
        handleException(e);
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) Profile(io.fabric8.api.Profile) Date(java.util.Date) Container(io.fabric8.api.Container) Map(java.util.Map)

Example 7 with Agent

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

the class CloudContainerInstallationTask method install.

public CreateJCloudsContainerMetadata install() {
    LoginCredentials credentials = nodeMetadata.getCredentials();
    // For some cloud providers return do not allow shell access to root, so the user needs to be overrided.
    if (!Strings.isNullOrEmpty(options.getUser()) && credentials != null) {
        credentials = credentials.toBuilder().user(options.getUser()).build();
    } else {
        credentials = nodeMetadata.getCredentials();
    }
    String id = nodeMetadata.getId();
    Set<String> publicAddresses = nodeMetadata.getPublicAddresses();
    // Make a copy of the addresses, because we don't want to return back a guice implementation of Set.
    Set<String> copyOfPublicAddresses = new HashSet<String>();
    for (String publicAddress : publicAddresses) {
        copyOfPublicAddresses.add(publicAddress);
    }
    CreateJCloudsContainerMetadata jCloudsContainerMetadata = new CreateJCloudsContainerMetadata();
    jCloudsContainerMetadata.setCreateOptions(options);
    jCloudsContainerMetadata.setNodeId(nodeMetadata.getId());
    jCloudsContainerMetadata.setContainerName(containerName);
    jCloudsContainerMetadata.setPublicAddresses(copyOfPublicAddresses);
    jCloudsContainerMetadata.setHostname(nodeMetadata.getHostname());
    if (credentials != null) {
        jCloudsContainerMetadata.setIdentity(credentials.identity);
        jCloudsContainerMetadata.setCredential(credentials.credential);
    }
    String publicAddress = "";
    Properties addresses = new Properties();
    if (publicAddresses != null && !publicAddresses.isEmpty()) {
        publicAddress = publicAddresses.iterator().next();
        addresses.put(ZkDefs.PUBLIC_IP, publicAddress);
    }
    options.getSystemProperties().put(ContainerProviderUtils.ADDRESSES_PROPERTY_KEY, addresses);
    options.getMetadataMap().put(containerName, jCloudsContainerMetadata);
    // Setup firwall for node
    try {
        FirewallManager firewallManager = firewallManagerFactory.getFirewallManager(computeService);
        if (firewallManager.isSupported()) {
            listener.onStateChange("Configuring firewall.");
            String source = getOriginatingIp();
            Rule httpRule = Rule.create().source("0.0.0.0/0").destination(nodeMetadata).port(8181);
            firewallManager.addRules(httpRule);
            if (source != null) {
                Rule jmxRule = Rule.create().source(source).destination(nodeMetadata).ports(44444, 1099);
                Rule sshRule = Rule.create().source(source).destination(nodeMetadata).port(8101);
                Rule zookeeperRule = Rule.create().source(source).destination(nodeMetadata).port(2181);
                firewallManager.addRules(jmxRule, sshRule, zookeeperRule);
            }
            // where firewall configuration is shared among nodes of the same groups, e.g. EC2.
            if (!Strings.isNullOrEmpty(publicAddress)) {
                Rule zookeeperFromTargetRule = Rule.create().source(publicAddress + "/32").destination(nodeMetadata).port(2181);
                firewallManager.addRule(zookeeperFromTargetRule);
            }
        } else {
            listener.onStateChange(String.format("Skipping firewall configuration. Not supported for provider %s", options.getProviderName()));
        }
    } catch (FirewallNotSupportedOnProviderException e) {
        LOGGER.warn("Firewall manager not supported. Firewall will have to be manually configured.");
    } catch (IOException e) {
        LOGGER.warn("Could not lookup originating ip. Firewall will have to be manually configured.", e);
    } catch (Throwable t) {
        LOGGER.warn("Failed to setup firewall", t);
    }
    try {
        String script = buildInstallAndStartScript(containerName, options);
        listener.onStateChange(String.format("Installing fabric agent on container %s. It may take a while...", containerName));
        ExecResponse response = null;
        String uploadPath = "/tmp/fabric8-karaf-" + FabricConstants.FABRIC_VERSION + ".zip";
        URL distributionURL = options.getProxyUri().resolve("io/fabric8/fabric8-karaf/" + FabricConstants.FABRIC_VERSION + "/fabric8-karaf-" + FabricConstants.FABRIC_VERSION + ".zip").toURL();
        try {
            if (options.doUploadDistribution()) {
                uploadToNode(computeService.getContext(), nodeMetadata, credentials, distributionURL, uploadPath);
            }
            if (credentials != null) {
                response = computeService.runScriptOnNode(id, script, templateOptions.overrideLoginCredentials(credentials).runAsRoot(false));
            } else {
                response = computeService.runScriptOnNode(id, script, templateOptions);
            }
        } catch (AuthorizationException ex) {
            throw new Exception("Failed to connect to the container via ssh.");
        } catch (SshException ex) {
            throw new Exception("Failed to connect to the container via ssh.");
        }
        if (response != null && response.getOutput() != null) {
            if (response.getOutput().contains(ContainerProviderUtils.FAILURE_PREFIX)) {
                jCloudsContainerMetadata.setFailure(new Exception(ContainerProviderUtils.parseScriptFailure(response.getOutput())));
            }
            String overridenResolverValue = ContainerProviderUtils.parseResolverOverride(response.getOutput());
            if (overridenResolverValue != null) {
                jCloudsContainerMetadata.setOverridenResolver(overridenResolverValue);
                listener.onStateChange("Overriding resolver to " + overridenResolverValue + ".");
            }
        } else {
            jCloudsContainerMetadata.setFailure(new Exception("No response received for fabric install script."));
        }
    } catch (Throwable t) {
        jCloudsContainerMetadata.setFailure(t);
    }
    // Cleanup addresses.
    options.getSystemProperties().clear();
    return jCloudsContainerMetadata;
}
Also used : FirewallManager(io.fabric8.service.jclouds.firewall.FirewallManager) ExecResponse(org.jclouds.compute.domain.ExecResponse) AuthorizationException(org.jclouds.rest.AuthorizationException) FirewallNotSupportedOnProviderException(io.fabric8.service.jclouds.firewall.FirewallNotSupportedOnProviderException) IOException(java.io.IOException) SshException(org.jclouds.ssh.SshException) Properties(java.util.Properties) URL(java.net.URL) AuthorizationException(org.jclouds.rest.AuthorizationException) FirewallNotSupportedOnProviderException(io.fabric8.service.jclouds.firewall.FirewallNotSupportedOnProviderException) IOException(java.io.IOException) SshException(org.jclouds.ssh.SshException) LoginCredentials(org.jclouds.domain.LoginCredentials) Rule(io.fabric8.service.jclouds.firewall.Rule) HashSet(java.util.HashSet)

Example 8 with Agent

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

the class ExtendedJoinTest method testJoinAndAddToEnsemble.

/**
 * This is a test for FABRIC-353.
 */
@Test
@Ignore
public void testJoinAndAddToEnsemble() throws Exception {
    System.err.println(CommandSupport.executeCommand("fabric:create --force --clean -n --wait-for-provisioning"));
    // System.out.println(executeCommand("shell:info"));
    // System.out.println(executeCommand("fabric:info"));
    // System.out.println(executeCommand("fabric:profile-list"));
    BundleContext moduleContext = ServiceLocator.getSystemContext();
    ServiceProxy<FabricService> fabricProxy = ServiceProxy.createServiceProxy(moduleContext, FabricService.class);
    try {
        FabricService fabricService = fabricProxy.getService();
        AdminService adminService = ServiceLocator.awaitService(AdminService.class);
        String version = System.getProperty("fabric.version");
        System.out.println(CommandSupport.executeCommand("admin:create --featureURL mvn:io.fabric8/fabric8-karaf/" + version + "/xml/features --feature fabric-git --feature fabric-agent --feature fabric-boot-commands basic_cnt_f"));
        System.out.println(CommandSupport.executeCommand("admin:create --featureURL mvn:io.fabric8/fabric8-karaf/" + version + "/xml/features --feature fabric-git --feature fabric-agent --feature fabric-boot-commands basic_cnt_g"));
        try {
            System.out.println(CommandSupport.executeCommand("admin:start basic_cnt_f"));
            System.out.println(CommandSupport.executeCommand("admin:start basic_cnt_g"));
            ProvisionSupport.instanceStarted(Arrays.asList("basic_cnt_f", "basic_cnt_g"), ProvisionSupport.PROVISION_TIMEOUT);
            System.out.println(CommandSupport.executeCommand("admin:list"));
            String joinCommand = "fabric:join -f --zookeeper-password " + fabricService.getZookeeperPassword() + " " + fabricService.getZookeeperUrl();
            String response = "";
            for (int i = 0; i < 10 && !response.contains("true"); i++) {
                response = CommandSupport.executeCommand("ssh:ssh -l karaf -P karaf -p " + adminService.getInstance("basic_cnt_f").getSshPort() + " localhost " + WAIT_FOR_JOIN_SERVICE);
                Thread.sleep(1000);
            }
            response = "";
            for (int i = 0; i < 10 && !response.contains("true"); i++) {
                response = CommandSupport.executeCommand("ssh:ssh -l karaf -P karaf -p " + adminService.getInstance("basic_cnt_g").getSshPort() + " localhost " + WAIT_FOR_JOIN_SERVICE);
                Thread.sleep(1000);
            }
            System.err.println(CommandSupport.executeCommand("ssh:ssh -l karaf -P karaf -p " + adminService.getInstance("basic_cnt_f").getSshPort() + " localhost " + joinCommand));
            System.err.println(CommandSupport.executeCommand("ssh:ssh -l karaf -P karaf -p " + adminService.getInstance("basic_cnt_g").getSshPort() + " localhost " + joinCommand));
            ProvisionSupport.containersExist(Arrays.asList("basic_cnt_f", "basic_cnt_g"), ProvisionSupport.PROVISION_TIMEOUT);
            Container cntF = fabricService.getContainer("basic_cnt_f");
            Container cntG = fabricService.getContainer("basic_cnt_g");
            ProvisionSupport.containerStatus(Arrays.asList(cntF, cntG), "success", ProvisionSupport.PROVISION_TIMEOUT);
            EnsembleSupport.addToEnsemble(fabricService, cntF, cntG);
            System.out.println(CommandSupport.executeCommand("fabric:container-list"));
            EnsembleSupport.removeFromEnsemble(fabricService, cntF, cntG);
            System.out.println(CommandSupport.executeCommand("fabric:container-list"));
        } finally {
            System.out.println(CommandSupport.executeCommand("admin:stop basic_cnt_f"));
            System.out.println(CommandSupport.executeCommand("admin:stop basic_cnt_g"));
        }
    } finally {
        fabricProxy.close();
    }
}
Also used : Container(io.fabric8.api.Container) AdminService(org.apache.karaf.admin.AdminService) FabricService(io.fabric8.api.FabricService) BundleContext(org.osgi.framework.BundleContext) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with Agent

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

the class DeploymentAgentTest method deployment.

@Deployment
@StartLevelAware(autostart = true)
public static Archive<?> deployment() {
    final JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "deployment-agent-test.jar");
    archive.addPackage(CommandSupport.class.getPackage());
    archive.setManifest(new Asset() {

        @Override
        public InputStream openStream() {
            OSGiManifestBuilder builder = OSGiManifestBuilder.newInstance();
            builder.addBundleManifestVersion(2);
            builder.addBundleSymbolicName(archive.getName());
            builder.addBundleVersion("1.0.0");
            builder.addImportPackages(ServiceLocator.class, FabricService.class);
            builder.addImportPackages("io.fabric8.git");
            builder.addImportPackages(AbstractCommand.class, Action.class);
            builder.addImportPackage("org.apache.felix.service.command;status=provisional");
            builder.addImportPackages(ConfigurationAdmin.class, ServiceTracker.class, Logger.class);
            return builder.openStream();
        }
    });
    return archive;
}
Also used : Action(org.apache.felix.gogo.commands.Action) ServiceTracker(org.osgi.util.tracker.ServiceTracker) InputStream(java.io.InputStream) AbstractCommand(org.apache.felix.gogo.commands.basic.AbstractCommand) Logger(org.slf4j.Logger) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) ServiceLocator(io.fabric8.api.gravia.ServiceLocator) FabricService(io.fabric8.api.FabricService) OSGiManifestBuilder(org.jboss.osgi.metadata.OSGiManifestBuilder) Asset(org.jboss.shrinkwrap.api.asset.Asset) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) CommandSupport(io.fabric8.itests.support.CommandSupport) StartLevelAware(org.jboss.arquillian.osgi.StartLevelAware) Deployment(org.jboss.arquillian.container.test.api.Deployment)

Example 10 with Agent

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

the class DeploymentAgentTest method testFeatureRepoResolution.

@Test
@SuppressWarnings("unchecked")
public void testFeatureRepoResolution() throws Exception {
    CommandSupport.executeCommand("fabric:create --force --clean -n --wait-for-provisioning");
    // We are just want to use a feature repository that is not part of the distribution.
    CommandSupport.executeCommand("fabric:profile-create --parent feature-camel test-profile");
    CommandSupport.executeCommand("fabric:version-create --parent 1.0 1.1");
    CommandSupport.executeCommand("fabric:profile-edit --repository mvn:io.fabric8.examples.fabric-camel-dosgi/features/" + System.getProperty("fabric.version") + "/xml/features test-profile 1.1");
    CommandSupport.executeCommand("fabric:profile-edit --feature fabric-dosgi test-profile 1.1");
    // We remove all repositories from agent config but the maven central to rely on the fabric-maven-proxy.
    // Also remove local repository
    CommandSupport.executeCommand("fabric:profile-edit --pid io.fabric8.agent/org.ops4j.pax.url.mvn.repositories=http://repo1.maven.org/maven2@id=m2central default 1.1");
    CommandSupport.executeCommand("fabric:profile-edit --pid test-profile 1.1");
    BundleContext moduleContext = ServiceLocator.getSystemContext();
    ServiceProxy<FabricService> fabricProxy = ServiceProxy.createServiceProxy(moduleContext, FabricService.class);
    try {
        FabricService fabricService = fabricProxy.getService();
        Set<Container> containers = ContainerBuilder.create().withName("smoke_cnt_a").withProfiles("test-profile").assertProvisioningResult().build(fabricService);
        try {
            // We want to remove all repositories from fabric-agent.
            for (Container container : containers) {
                CommandSupport.executeCommand("fabric:container-upgrade 1.1 " + container.getId());
                System.out.flush();
            }
            ProvisionSupport.provisioningSuccess(containers, ProvisionSupport.PROVISION_TIMEOUT);
            CommandSupport.executeCommand("fabric:container-list");
            for (Container container : containers) {
                CommandSupport.executeCommand("fabric:container-connect -u admin -p admin " + container.getId() + " osgi:list");
                CommandSupport.executeCommand("fabric:container-connect -u admin -p admin " + container.getId() + " config:proplist --pid org.ops4j.pax.url.mvn");
                System.out.flush();
            }
        } finally {
            ContainerBuilder.stop(fabricService, containers);
        }
    } finally {
        fabricProxy.close();
    }
}
Also used : Container(io.fabric8.api.Container) FabricService(io.fabric8.api.FabricService) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)8 FabricService (io.fabric8.api.FabricService)7 Container (io.fabric8.api.Container)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Test (org.junit.Test)6 Bundle (org.osgi.framework.Bundle)5 BundleContext (org.osgi.framework.BundleContext)5 Profile (io.fabric8.api.Profile)4 File (java.io.File)4 BundleException (org.osgi.framework.BundleException)4 MultiException (io.fabric8.common.util.MultiException)3 FileInputStream (java.io.FileInputStream)3 List (java.util.List)3 Set (java.util.Set)3 ConfigurationException (org.osgi.service.cm.ConfigurationException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 DownloadManager (io.fabric8.agent.download.DownloadManager)2 Downloader (io.fabric8.agent.download.Downloader)2