Search in sources :

Example 21 with Command

use of io.fabric8.gateway.handlers.detecting.protocol.openwire.command.Command in project fabric8 by jboss-fuse.

the class FabricGitSummaryAction method afterEachContainer.

@Override
protected void afterEachContainer(Collection<String> names) {
    System.out.printf("Scheduled git-summary command to %d containers. Awaiting response(s).\n", names.size());
    final CountDownLatch latch = new CountDownLatch(requests.size() + (masterRequest == null ? 0 : 1));
    Thread waiter = null;
    try {
        waiter = new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    String currentContainer = null;
                    try {
                        // master request
                        if (masterRequest != null && masterResult == null) {
                            currentContainer = master;
                            List<JMXResult> results = fetchResponses(master);
                            for (JMXResult result : results) {
                                if (result.getCorrelationId().equals(masterRequest.getId())) {
                                    masterResult = result;
                                    latch.countDown();
                                    break;
                                }
                            }
                        }
                        // requests for containers
                        for (Map.Entry<String, JMXRequest> req : requests.entrySet()) {
                            currentContainer = req.getKey();
                            List<JMXResult> containerResults = fetchResponses(currentContainer);
                            for (JMXResult result : containerResults) {
                                if (result.getCorrelationId().equals(req.getValue().getId())) {
                                    results.put(currentContainer, result);
                                    latch.countDown();
                                    break;
                                }
                            }
                        }
                        if ((masterRequest == null || masterResult != null) && results.size() == requests.size()) {
                            break;
                        } else {
                            // active waiting - so no need for ZK watchers, etc...
                            Thread.sleep(1000);
                        }
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        break;
                    } catch (Exception e) {
                        System.err.println("Problem occurred while fetching response from " + currentContainer + " container: " + e.getMessage());
                        // active waiting - so no need for ZK watchers, etc...
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e1) {
                        }
                    }
                }
            }
        });
        waiter.start();
        boolean finished = latch.await(timeout, TimeUnit.MILLISECONDS);
        if (!finished) {
            waiter.interrupt();
            System.out.println("Timeout waiting for git-summary response");
            return;
        }
        // before printing summary, let's check if there are out of sync containers
        Map<String, String> centralGitRepo = new HashMap<>();
        Set<String> outOfSyncContainers = new TreeSet<>();
        if (masterResult != null) {
            for (GitVersion version : ((GitVersions) masterResult.getResponse()).getVersions()) {
                centralGitRepo.put(version.getVersion(), version.getSha1());
            }
            for (String containerName : results.keySet()) {
                List<GitVersion> localRepo = ((GitVersions) results.get(containerName).getResponse()).getVersions();
                for (GitVersion version : localRepo) {
                    String ref = centralGitRepo.get(version.getVersion());
                    if (ref == null) {
                        // local container knows about version, which is not tracked in central repo
                        outOfSyncContainers.add(containerName);
                    } else if (!ref.equals(version.getSha1())) {
                        // version not in sync
                        outOfSyncContainers.add(containerName);
                    }
                }
                if (localRepo.size() != centralGitRepo.size()) {
                    // extra or not-in-sync versions handled, so this must mean some central version is not
                    // available locally
                    outOfSyncContainers.add(containerName);
                }
            }
            if (outOfSyncContainers.size() > 0) {
                System.out.println();
                System.out.println("Containers that require synchronization: " + outOfSyncContainers);
                System.out.println("Please use \"fabric:git-synchronize\" command");
            }
        } else {
            System.out.println();
            System.out.println("Can't determine synchronization status of containers - no master Git repository detected");
        }
        // now summary time
        if (masterResult != null) {
            System.out.println();
            printVersions("=== Summary for master Git repository (container: " + master + ") ===", ((GitVersions) masterResult.getResponse()).getVersions());
        }
        System.out.println();
        for (String containerName : results.keySet()) {
            printVersions("=== Summary for local Git repository (container: " + containerName + ") ===", ((GitVersions) results.get(containerName).getResponse()).getVersions());
            System.out.println();
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        waiter.interrupt();
        System.out.println("Interrupted waiting for git-summary response");
    }
}
Also used : GitVersion(io.fabric8.api.commands.GitVersion) JMXRequest(io.fabric8.api.commands.JMXRequest) HashMap(java.util.HashMap) JMXResult(io.fabric8.api.commands.JMXResult) CountDownLatch(java.util.concurrent.CountDownLatch) GitVersions(io.fabric8.api.commands.GitVersions) TreeSet(java.util.TreeSet) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 22 with Command

use of io.fabric8.gateway.handlers.detecting.protocol.openwire.command.Command in project fabric8 by jboss-fuse.

the class FabricGitSynchronizeAction method performContainerAction.

@Override
protected void performContainerAction(String queuePath, String containerName) throws Exception {
    String command = map(new JMXRequest().withObjectName("io.fabric8:type=Fabric").withMethod("gitSynchronize").withDelay(randomDelay).withParam(Boolean.class, allowPush));
    curator.create().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath(queuePath, PublicStringSerializer.serialize(command));
}
Also used : JMXRequest(io.fabric8.api.commands.JMXRequest)

Example 23 with Command

use of io.fabric8.gateway.handlers.detecting.protocol.openwire.command.Command in project fabric8 by jboss-fuse.

the class FabricCreateCommandTest method deployment.

@Deployment
@StartLevelAware(autostart = true)
public static Archive<?> deployment() {
    final JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "create-command-test.jar");
    archive.addClasses(PasswordEncoder.class, Base64Encoder.class);
    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 24 with Command

use of io.fabric8.gateway.handlers.detecting.protocol.openwire.command.Command in project fabric8 by jboss-fuse.

the class CreateAction method doExecute.

protected Object doExecute() throws Exception {
    String adminRole = "admin";
    String administratorRole = "administrator";
    String superUserRole = "superuser";
    boolean adminExists = hasRole(adminRole, superUserRole, administratorRole);
    if (!adminExists) {
        System.out.println("The fabric:create command can be executed only by admin user");
        return null;
    }
    Path propsPath = runtimeProperties.getConfPath().resolve("users.properties");
    Properties userProps = new Properties(propsPath.toFile());
    if (!adminExists && !usersPropertiesFileContainsRole(userProps, adminRole, superUserRole, administratorRole)) {
        System.out.println("The etc/user.properties file must contain at least one admin user, if no other admin exists");
        return null;
    }
    // prevent creating fabric if already created
    ServiceReference<FabricService> sref = bundleContext.getServiceReference(FabricService.class);
    FabricService fabricService = sref != null ? bundleContext.getService(sref) : null;
    if (!force && (fabricService != null && fabricService.getCurrentContainer().isEnsembleServer())) {
        System.out.println("Current container " + fabricService.getCurrentContainerName() + " is already in the current fabric ensemble. Cannot create fabric.");
        System.out.println("You can use the --force option, if you want to force re-create the fabric.");
        return null;
    }
    Configuration bootConfiguration = configAdmin.getConfiguration(BootstrapConfiguration.COMPONENT_PID, null);
    Dictionary<String, Object> bootProperties = bootConfiguration.getProperties();
    if (bootProperties == null) {
        bootProperties = new Hashtable<>();
    }
    String runtimeIdentity = runtimeProperties.getRuntimeIdentity();
    CreateEnsembleOptions.Builder<?> builder = CreateEnsembleOptions.builder().zooKeeperServerTickTime(zooKeeperTickTime).zooKeeperServerInitLimit(zooKeeperInitLimit).zooKeeperServerSyncLimit(zooKeeperSyncLimit).zooKeeperServerDataDir(zooKeeperDataDir).zookeeperSnapRetainCount(zookeeperSnapRetainCount).zookeeperPurgeInterval(zookeeperPurgeInterval).fromRuntimeProperties(runtimeProperties).bootstrapTimeout(bootstrapTimeout).waitForProvision(waitForProvisioning).autoImportEnabled(!noImport).clean(clean);
    builder.version(version);
    if (containers == null || containers.isEmpty()) {
        containers = Arrays.asList(runtimeIdentity);
    }
    if (!noImport && importDir != null) {
        builder.autoImportEnabled(true);
        builder.importPath(importDir);
    }
    if (globalResolver != null) {
        if (!isInEnum(globalResolver, ResolverPolicyEnum.class)) {
            System.out.println("The globalResolver value must be one of the following: localip, localhostname, publicip, publichostname, manualip");
            return null;
        }
        builder.globalResolver(globalResolver);
        bootProperties.put(ZkDefs.GLOBAL_RESOLVER_PROPERTY, globalResolver);
    }
    if (resolver != null) {
        if (!isInEnum(resolver, ResolverPolicyEnum.class)) {
            System.out.println("The resolver value must be one of the following: localip, localhostname, publicip, publichostname, manualip");
            return null;
        }
        builder.resolver(resolver);
        bootProperties.put(ZkDefs.LOCAL_RESOLVER_PROPERTY, resolver);
    }
    if (manualIp != null) {
        builder.manualIp(manualIp);
        bootProperties.put(ZkDefs.MANUAL_IP, manualIp);
    }
    if (bindAddress != null) {
        if (!bindAddress.contains(":")) {
            builder.bindAddress(bindAddress);
            bootProperties.put(ZkDefs.BIND_ADDRESS, bindAddress);
        } else {
            String[] parts = bindAddress.split(":");
            builder.bindAddress(parts[0]);
            builder.zooKeeperServerPort(Integer.parseInt(parts[1]));
            bootProperties.put(ZkDefs.BIND_ADDRESS, parts[0]);
        }
    }
    if (zooKeeperServerPort > 0) {
        // --zookeeper-server-port option has higher priority than
        // CreateEnsembleOptions.ZOOKEEPER_SERVER_PORT and CreateEnsembleOptions.ZOOKEEPER_SERVER_CONNECTION_PORT
        // system/runtime properties
        builder.setZooKeeperServerPort(zooKeeperServerPort);
        builder.setZooKeeperServerConnectionPort(zooKeeperServerPort);
    } else {
        int shiftedPort = Ports.mapPortToRange(2181, minimumPort, maximumPort);
        builder.setZooKeeperServerPort(shiftedPort);
        builder.setZooKeeperServerConnectionPort(shiftedPort);
    }
    // Configure External Git Repository.
    if (externalGitUrl != null) {
        builder.dataStoreProperty(Constants.GIT_REMOTE_URL, externalGitUrl);
    }
    if (externalGitUser != null) {
        builder.dataStoreProperty(GIT_REMOTE_USER, externalGitUser);
    }
    if (externalGitPassword != null) {
        builder.dataStoreProperty(GIT_REMOTE_PASSWORD, externalGitPassword);
    }
    if ((externalGitUrl != null) || (externalGitUser != null) || (externalGitPassword != null)) {
        Configuration configuration = configAdmin.getConfiguration(Constants.DATASTORE_PID);
        Dictionary<String, Object> existingProperties = configuration.getProperties();
        Map<String, String> changedProperties = builder.getDataStoreProperties();
        for (String key : changedProperties.keySet()) {
            existingProperties.put(key, changedProperties.get(key));
        }
        configuration.update(existingProperties);
    }
    if (profiles != null && profiles.size() > 0) {
        builder.profiles(profiles);
    }
    if (nonManaged) {
        builder.agentEnabled(false);
    } else {
        builder.agentEnabled(true);
    }
    builder.minimumPort(minimumPort);
    builder.maximumPort(maximumPort);
    bootProperties.put(ZkDefs.MINIMUM_PORT, String.valueOf(minimumPort));
    bootProperties.put(ZkDefs.MAXIMUM_PORT, String.valueOf(maximumPort));
    newUser = newUser != null ? newUser : ShellUtils.retrieveFabricUser(session);
    newUserPassword = newUserPassword != null ? newUserPassword : ShellUtils.retrieveFabricUserPassword(session);
    if (userProps.isEmpty()) {
        String[] credentials = promptForNewUser(newUser, newUserPassword);
        newUser = credentials[0];
        newUserPassword = credentials[1];
    } else {
        if (newUser == null || newUserPassword == null) {
            newUser = "" + userProps.keySet().iterator().next();
            newUserPassword = "" + userProps.get(newUser);
            if (newUserPassword.contains(ROLE_DELIMITER)) {
                newUserPassword = newUserPassword.substring(0, newUserPassword.indexOf(ROLE_DELIMITER));
            }
        }
        String passwordWithroles = userProps.get(newUser);
        if (passwordWithroles != null && passwordWithroles.contains(ROLE_DELIMITER)) {
            String[] infos = passwordWithroles.split(",");
            String oldUserRole = "";
            if (newUserIsAdmin(infos)) {
                newUserRole = "_g_:admin";
                oldUserRole = newUserRole;
            }
            for (int i = 1; i < infos.length; i++) {
                if (infos[i].trim().startsWith(BackingEngine.GROUP_PREFIX)) {
                    // it's a group reference
                    String groupInfo = (String) userProps.get(infos[i].trim());
                    if (groupInfo != null) {
                        String[] roles = groupInfo.split(",");
                        for (int j = 1; j < roles.length; j++) {
                            if (!roles[j].trim().equals(oldUserRole)) {
                                if (!newUserRole.isEmpty()) {
                                    newUserRole = newUserRole + ROLE_DELIMITER + roles[j].trim();
                                } else {
                                    newUserRole = roles[j].trim();
                                }
                            }
                        }
                    }
                } else {
                    // it's an user reference
                    if (!infos[i].trim().equals(oldUserRole)) {
                        if (!newUserRole.isEmpty()) {
                            newUserRole = newUserRole + ROLE_DELIMITER + infos[i].trim();
                        } else {
                            newUserRole = infos[i].trim();
                        }
                    }
                }
            }
        }
    }
    if (Strings.isNullOrEmpty(newUser)) {
        System.out.println("No user specified. Cannot create a new fabric ensemble.");
        return null;
    }
    StringBuilder sb = new StringBuilder();
    // session is unset when this is called from FMC
    if (session != null) {
        ShellUtils.storeFabricCredentials(session, newUser, newUserPassword);
    }
    if (generateZookeeperPassword) {
    // do nothing use the generated password.
    } else if (zookeeperPassword == null) {
        zookeeperPassword = PasswordEncoder.decode(runtimeProperties.getProperty(CreateEnsembleOptions.ZOOKEEPER_PASSWORD, PasswordEncoder.encode(newUserPassword)));
        builder.zookeeperPassword(zookeeperPassword);
    } else {
        builder.zookeeperPassword(zookeeperPassword);
    }
    OsgiUtils.updateCmConfigurationAndWait(bundleContext, bootConfiguration, bootProperties, bootstrapTimeout, TimeUnit.MILLISECONDS);
    String roleToAssign = newUserRole.isEmpty() ? "_g_:admin" : newUserRole;
    CreateEnsembleOptions options = builder.users(userProps).withUser(newUser, newUserPassword, roleToAssign).build();
    if (containers.size() == 1 && containers.contains(runtimeIdentity)) {
        bootstrap.create(options);
    } else {
        ServiceProxy<ZooKeeperClusterService> serviceProxy = ServiceProxy.createServiceProxy(bundleContext, ZooKeeperClusterService.class);
        try {
            serviceProxy.getService().createCluster(containers, options);
        } finally {
            serviceProxy.close();
        }
    }
    ShellUtils.storeZookeeperPassword(session, options.getZookeeperPassword());
    if (zookeeperPassword == null && !generateZookeeperPassword) {
        sb.append("Zookeeper password: (reusing users ").append(newUser).append(" password:").append(options.getZookeeperPassword()).append(")\n");
        sb.append("(You can use the --zookeeper-password / --generate-zookeeper-password option to specify one.)\n");
    } else if (generateZookeeperPassword) {
        sb.append("Generated zookeeper password:").append(options.getZookeeperPassword());
    }
    System.out.println(sb.toString());
    if (!nonManaged && !waitForProvisioning) {
        System.out.println("It may take a couple of seconds for the container to provision...");
        System.out.println("You can use the --wait-for-provisioning option, if you want this command to block until the container is provisioned.");
    }
    return null;
}
Also used : Path(java.nio.file.Path) Configuration(org.osgi.service.cm.Configuration) BootstrapConfiguration(io.fabric8.zookeeper.bootstrap.BootstrapConfiguration) ResolverPolicyEnum(io.fabric8.boot.commands.support.ResolverPolicyEnum) Properties(org.apache.felix.utils.properties.Properties)

Example 25 with Command

use of io.fabric8.gateway.handlers.detecting.protocol.openwire.command.Command in project fabric8 by jboss-fuse.

the class AutoScaleController method autoScaleProfile.

private void autoScaleProfile(FabricService service, final ContainerAutoScaler autoScaler, FabricRequirements requirements, ProfileRequirements profileRequirement, AutoScaleStatus status) {
    final String profile = profileRequirement.getProfile();
    Integer minimumInstances = profileRequirement.getMinimumInstances();
    Integer maximumInstances = profileRequirement.getMaximumInstances();
    String requirementsVersion = requirements.getVersion();
    final String version = Strings.isNotBlank(requirementsVersion) ? requirementsVersion : service.getDefaultVersionId();
    if (maximumInstances != null || minimumInstances != null) {
        if (maximumInstances != null) {
            List<Container> containers = Containers.aliveAndSuccessfulContainersForProfile(version, profile, service);
            int count = containers.size();
            int delta = count - maximumInstances;
            if (delta > 0) {
                stopContainers(containers, autoScaler, requirements, profileRequirement, status, delta);
            }
        }
        if (minimumInstances != null) {
            // lets check if we need to provision more
            List<Container> containers = Containers.aliveOrPendingContainersForProfile(version, profile, service);
            int count = containers.size();
            int delta = minimumInstances - count;
            try {
                AutoScaleProfileStatus profileStatus = status.profileStatus(profile);
                if (delta < 0) {
                    FabricService fs = this.fabricService.get();
                    if (fs != null) {
                        profileStatus.destroyingContainer();
                        for (int i = delta; i < 0; i++) {
                            while (!containers.isEmpty()) {
                                Container container = containers.remove(0);
                                if (container.getId().startsWith("auto_")) {
                                    fs.destroyContainer(container);
                                    break;
                                }
                            }
                        }
                    }
                } else if (delta > 0) {
                    if (AutoScalers.requirementsSatisfied(service, version, requirements, profileRequirement, status)) {
                        profileStatus.creatingContainer();
                        final AutoScaleRequest command = new AutoScaleRequest(service, version, profile, delta, requirements, profileRequirement, status);
                        new Thread("Creating container for " + command.getProfile()) {

                            @Override
                            public void run() {
                                try {
                                    autoScaler.createContainers(command);
                                } catch (Exception e) {
                                    LOGGER.error("Failed to create container of profile: " + profile + ". Caught: " + e, e);
                                }
                            }
                        }.start();
                    }
                } else {
                    profileStatus.provisioned();
                }
            } catch (Exception e) {
                LOGGER.error("Failed to auto-scale " + profile + ". Caught: " + e, e);
            }
        }
    }
}
Also used : Container(io.fabric8.api.Container) FabricService(io.fabric8.api.FabricService) AutoScaleProfileStatus(io.fabric8.api.AutoScaleProfileStatus) AutoScaleRequest(io.fabric8.api.AutoScaleRequest)

Aggregations

IOException (java.io.IOException)9 File (java.io.File)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Container (io.fabric8.api.Container)5 JMXRequest (io.fabric8.api.commands.JMXRequest)4 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 FabricService (io.fabric8.api.FabricService)3 ContainerCreateConfig (io.fabric8.maven.docker.access.ContainerCreateConfig)3 Arguments (io.fabric8.maven.docker.config.Arguments)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Properties (java.util.Properties)3 TemplatedResource (com.netflix.spinnaker.halyard.core.resource.v1.TemplatedResource)2 ArtifactService (com.netflix.spinnaker.halyard.deploy.services.v1.ArtifactService)2 GenerateService (com.netflix.spinnaker.halyard.deploy.services.v1.GenerateService)2 SpinnakerRuntimeSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings)2 Container (io.fabric8.kubernetes.api.model.Container)2