Examples of ProfileBuilder


Examples of io.fabric8.api.ProfileBuilder

    }

    @Override
    public void setProfileTags(String versionId, String profileId, List<String> tags) {
        Profile profile = profileService.getRequiredProfile(versionId, profileId);
        ProfileBuilder builder = ProfileBuilder.Factory.createFrom(profile);
        builder.setTags(tags);
        profileService.updateProfile(builder.getProfile());
    }
View Full Code Here

Examples of io.fabric8.api.ProfileBuilder

    }

  private Profile getContainerProfile() {
    Version version = getVersion();
    String profileId = "#container-" + getId();
    ProfileBuilder builder = ProfileBuilder.Factory.create(profileId).version(version.getId());
    ContainerProfileOptions optionsProvider = new ContainerProfileOptions(getId(), version, dataStore);
    return builder.addOptions(optionsProvider).getProfile();
  }
View Full Code Here

Examples of io.fabric8.api.ProfileBuilder

        Version version = versionId != null ? profileService.getRequiredVersion(versionId) : fabricService.getRequiredDefaultVersion();
        Profile profile = version.getRequiredProfile(profileId);
       
        // we can only change parents to existing profiles
        Profile[] parents = FabricCommand.getExistingProfiles(fabricService, version, parentIds);
        ProfileBuilder builder = ProfileBuilder.Factory.createFrom(profile);
        for (Profile parent : parents) {
            builder.addParent(parent.getId());
        }
        profileService.updateProfile(builder.getProfile());
        return null;
    }
View Full Code Here

Examples of io.fabric8.api.ProfileBuilder

            versionId = fabricService.getDefaultVersionId();
        }

        // we can only use existing parent profiles
        Profile[] parents = FabricCommand.getExistingProfiles(fabricService, versionId, this.parents);
        ProfileBuilder builder = ProfileBuilder.Factory.create(versionId, profileId);
        for (Profile parent : parents) {
            builder.addParent(parent.getId());
        }
    profileService.createProfile(builder.getProfile());
        return null;
    }
View Full Code Here

Examples of io.fabric8.api.ProfileBuilder

        }
       
        String pidName = getBrokerPID(brokerName);
        // lets check we have a config value

        ProfileBuilder builder;
       
        // create a profile if it doesn't exist
        Map<String, String> config = null;
        boolean create = !version.hasProfile(profileId);
        if (create) {
            builder = ProfileBuilder.Factory.create(versionId, profileId);
            if (parentProfile != null) {
                builder.addParent(parentProfile.getId());
            }
        } else {
            Profile profile = version.getRequiredProfile(profileId);
            builder = ProfileBuilder.Factory.createFrom(profile);
            config = new HashMap<>(builder.getConfiguration(pidName));
        }
       
        Map<String, String> parentProfileConfig = parentProfile.getConfiguration(MQ_PID_TEMPLATE);
        if (config == null) {
            config = new HashMap<>(parentProfileConfig);
        }

        if (configs != null && "true".equals(configs.get("ssl"))) {

            // Only generate the keystore file if it does not exist.
            // [TOOD] Fix direct data access! This should be part of the ProfileBuilder
            byte[] keystore  = builder.getFileConfiguration("keystore.jks");
            if( keystore==null ) {
                try {

                    String host = configs.get("keystore.cn");
                    if( host == null ) {
                        host = configs.get(GROUP);
                        if( host == null ) {
                            host = "localhost";
                        }
                        configs.put("keystore.cn", host);
                    }
                    String password = configs.get("keystore.password");
                    if( password == null ) {
                        password = generatePassword(8);
                        configs.put("keystore.password", password);
                    }

                    File keystoreFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
                    keystoreFile.delete();
                    LOG.info("Generating ssl keystore...");
                    int rc = system("keytool", "-genkey",
                            "-storetype", "JKS",
                            "-storepass", password,
                            "-keystore", keystoreFile.getCanonicalPath(),
                            "-keypass", password,
                            "-alias", host,
                            "-keyalg", "RSA",
                            "-keysize", "4096",
                            "-dname", String.format("cn=%s", host),
                            "-validity", "3650");

                    if(rc!=0) {
                      throw new IOException("keytool failed with exit code: "+rc);
                    }

                    keystore = Files.readBytes(keystoreFile);
                    keystoreFile.delete();
                    LOG.info("Keystore generated");

                    builder.addFileConfiguration("keystore.jks", keystore);
                    configs.put("keystore.file", "profile:keystore.jks");

                } catch (IOException e) {
                    LOG.info("Failed to generate keystore.jks: "+e, e);
                }

            }

            // [TOOD] Fix direct data access! This should be part of the ProfileBuilder
            byte[] truststore = builder.getFileConfiguration("truststore.jks");
            if (truststore == null) {
               try {
                    String password = configs.get("truststore.password");
                    if( password == null ) {
                        password = configs.get("keystore.password");
                        configs.put("truststore.password", password);
                    }

                    File keystoreFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
                    Files.writeToFile(keystoreFile, keystore);

                    File certFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
                    certFile.delete();

                    LOG.info("Exporting broker certificate to create truststore.jks");
                    int rc = system("keytool", "-exportcert", "-rfc",
                            "-keystore", keystoreFile.getCanonicalPath(),
                            "-storepass", configs.get("keystore.password"),
                            "-alias",  configs.get("keystore.cn"),
                            "--file", certFile.getCanonicalPath());

                    keystoreFile.delete();
                    if(rc!=0) {
                      throw new IOException("keytool failed with exit code: "+rc);
                    }

                    LOG.info("Creating truststore.jks");
                    File truststoreFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
                    truststoreFile.delete();
                    rc = system("keytool", "-importcert", "-noprompt",
                            "-keystore", truststoreFile.getCanonicalPath(),
                            "-storepass", password,
                            "--file", certFile.getCanonicalPath());
                    certFile.delete();
                    if(rc!=0) {
                      throw new IOException("keytool failed with exit code: "+rc);
                    }

                    truststore = Files.readBytes(truststoreFile);
                    truststoreFile.delete();
                   
                    builder.addFileConfiguration("truststore.jks", truststore);
                    configs.put("truststore.file", "profile:truststore.jks");

                } catch (IOException e) {
                    LOG.info("Failed to generate truststore.jks due: " + e.getMessage(), e);
                }
            }
        }

        config.put("broker-name", brokerName);
        if (configs != null) {
            config.putAll(configs);
        }

        // lets check we've a bunch of config values inherited from the template
        String[] propertiesToDefault = { CONFIG_URL, STANDBY_POOL, CONNECTORS };
        for (String key : propertiesToDefault) {
            if (config.get(key) == null) {
                String defaultValue = parentProfileConfig.get(key);
                if (Strings.isNotBlank(defaultValue)) {
                    config.put(key, defaultValue);
                }
            }
        }
       
        builder.addConfiguration(pidName, config);
        Profile profile = builder.getProfile();
        return create ? profileService.createProfile(profile) : profileService.updateProfile(profile);
    }
View Full Code Here

Examples of io.fabric8.api.ProfileBuilder

            ensembleProperties.put("dataDir", options.getZooKeeperServerDataDir() + File.separator + newClusterId);
           
            // create new ensemble
            String ensembleProfileId = "fabric-ensemble-" + newClusterId;
            IllegalStateAssertion.assertFalse(profileRegistry.get().hasProfile(versionId, ensembleProfileId), "Profile already exists: " + versionId + "/" + ensembleProfileId);
            ProfileBuilder ensembleProfileBuilder = ProfileBuilder.Factory.create(versionId, ensembleProfileId);
            ensembleProfileBuilder.addAttribute(Profile.ABSTRACT, "true").addAttribute(Profile.HIDDEN, "true");

            int index = 1;
            String connectionUrl = "";
            String realConnectionUrl = "";
            String containerList = "";
            List<Profile> memberProfiles = new ArrayList<>();
            for (String container : containers) {
                String ip = getSubstitutedPath(curator.get(), ZkPath.CONTAINER_IP.getPath(container));

                String minimumPort = String.valueOf(Ports.MIN_PORT_NUMBER);
                String maximumPort = String.valueOf(Ports.MAX_PORT_NUMBER);
                String bindAddress = "0.0.0.0";

                if (exists(curator.get(), ZkPath.CONTAINER_PORT_MIN.getPath(container)) != null) {
                    minimumPort = getSubstitutedPath(curator.get(), ZkPath.CONTAINER_PORT_MIN.getPath(container));
                }

                if (exists(curator.get(), ZkPath.CONTAINER_PORT_MAX.getPath(container)) != null) {
                    maximumPort = getSubstitutedPath(curator.get(), ZkPath.CONTAINER_PORT_MAX.getPath(container));
                }

                if (exists(curator.get(), ZkPath.CONTAINER_BINDADDRESS.getPath(container)) != null) {
                    bindAddress = getSubstitutedPath(curator.get(), ZkPath.CONTAINER_BINDADDRESS.getPath(container));
                }

                // Ensemble member properties
                Properties memberProperties = new Properties();
                String memberPropertiesName = "io.fabric8.zookeeper.server-" + newClusterId + ".properties";
                String port1 = publicPort(container, Integer.toString(findPort(usedPorts, ip, mapPortToRange(Ports.DEFAULT_ZOOKEEPER_SERVER_PORT, minimumPort, maximumPort))));
                if (containers.size() > 1) {
                    String port2 = publicPort(container, Integer.toString(findPort(usedPorts, ip, mapPortToRange(Ports.DEFAULT_ZOOKEEPER_PEER_PORT, minimumPort, maximumPort))));
                    String port3 = publicPort(container, Integer.toString(findPort(usedPorts, ip, mapPortToRange(Ports.DEFAULT_ZOOKEEPER_ELECTION_PORT, minimumPort, maximumPort))));
                    ensembleProperties.put("server." + Integer.toString(index), "${zk:" + container + "/ip}:" + port2 + ":" + port3);
                    memberProperties.put("server.id", Integer.toString(index));
                }
                memberProperties.put("clientPort", port1);
                memberProperties.put("clientPortAddress", bindAddress);

                // Create ensemble member profile
                String memberProfileId = "fabric-ensemble-" + newClusterId + "-" + index;
                IllegalStateAssertion.assertFalse(profileRegistry.get().hasProfile(versionId, memberProfileId), "Profile already exists: " + versionId + "/" + memberProfileId);
                ProfileBuilder memberProfileBuilder = ProfileBuilder.Factory.create(versionId, memberProfileId);
                memberProfileBuilder.addAttribute(Profile.HIDDEN, "true").addAttribute(Profile.PARENTS, ensembleProfileId);
                memberProfileBuilder.addFileConfiguration(memberPropertiesName, DataStoreUtils.toBytes(memberProperties));
                memberProfiles.add(memberProfileBuilder.getProfile());

                if (connectionUrl.length() > 0) {
                    connectionUrl += ",";
                    realConnectionUrl += ",";
                }
                connectionUrl += "${zk:" + container + "/ip}:" + port1;
                realConnectionUrl += ip + ":" + port1;
                if (containerList.length() > 0) {
                    containerList += ",";
                }
                containerList += container;
                index++;
            }

            LockHandle writeLock = profileRegistry.get().aquireWriteLock();
            try {
                // Create the ensemble profile
                ensembleProfileBuilder.addFileConfiguration(ensemblePropertiesName, DataStoreUtils.toBytes(ensembleProperties));
                Profile ensembleProfile = ensembleProfileBuilder.getProfile();
                LOGGER.info("Creating parent ensemble profile: {}", ensembleProfile);
                profileRegistry.get().createProfile(ensembleProfile);
               
                // Create the member profiles
                for (Profile memberProfile : memberProfiles) {
                    LOGGER.info("Creating member ensemble profile: {}", memberProfile);
                    profileRegistry.get().createProfile(memberProfile);
                }
            } finally {
                writeLock.unlock();
            }
           
            index = 1;
            for (String container : containers) {
                // add this container to the ensemble
                List<String> profiles = new LinkedList<String>(dataStore.get().getContainerProfiles(container));
                profiles.add("fabric-ensemble-" + newClusterId + "-" + Integer.toString(index));
                LOGGER.info("Assigning member ensemble profile with id: {} to {}.", ensembleProfileId + "-" + index, container);
                dataStore.get().setContainerProfiles(container, profiles);
                index++;
            }

            Profile defaultProfile = profileRegistry.get().getRequiredProfile(versionId, "default");
            Map<String, String> zkConfig = defaultProfile.getConfiguration(Constants.ZOOKEEPER_CLIENT_PID);
            if (oldClusterId != null) {
                Properties properties = DataStoreUtils.toProperties(zkConfig);
                properties.put("zookeeper.url", getSubstitutedData(curator.get(), realConnectionUrl));
                properties.put("zookeeper.password", options.getZookeeperPassword());
                CuratorFramework dst = CuratorFrameworkFactory.builder().connectString(realConnectionUrl).retryPolicy(new RetryOneTime(500))
                        .aclProvider(aclProvider.get()).authorization("digest", ("fabric:" + options.getZookeeperPassword()).getBytes()).sessionTimeoutMs(30000)
                        .connectionTimeoutMs((int) options.getMigrationTimeout()).build();
                dst.start();
                try {
                    long t0 = System.currentTimeMillis();
                    LOGGER.info("Waiting for ensemble {} to become ready.", newClusterId);
                    if (!dst.getZookeeperClient().blockUntilConnectedOrTimedOut()) {
                        throw new EnsembleModificationFailed("Timed out connecting to new ensemble.", EnsembleModificationFailed.Reason.TIMEOUT);
                    }
                    LOGGER.info("Copying data from the old ensemble to the new one");
                    copy(curator.get(), dst, "/fabric");
                    setData(dst, ZkPath.CONFIG_ENSEMBLES.getPath(), newClusterId);
                    setData(dst, ZkPath.CONFIG_ENSEMBLE.getPath(newClusterId), containerList);

                    // Perform cleanup when the new datastore has been registered.
                    final AtomicReference<DataStore> result = new AtomicReference<DataStore>();
                    runtimeProperties.get().putRuntimeAttribute(DataStoreTemplate.class, new DataStoreTemplate() {
                        @Override
                        public void doWith(ProfileRegistry profileRegistry, DataStore dataStore) {
                            synchronized (result) {
                                result.set(dataStore);
                                result.notifyAll();
                            }
                        }
                    });

                    LOGGER.info("Migrating containers to the new ensemble using url {}.", connectionUrl);
                    setData(dst, ZkPath.CONFIG_ENSEMBLE_PASSWORD.getPath(), PasswordEncoder.encode(options.getZookeeperPassword()));
                    setData(dst, ZkPath.CONFIG_ENSEMBLE_URL.getPath(), connectionUrl);
                    curator.get().inTransaction()
                            .setData().forPath(ZkPath.CONFIG_ENSEMBLE_PASSWORD.getPath(),  PasswordEncoder.encode(options.getZookeeperPassword()).getBytes(Charsets.UTF_8))
                            .and()
                            .setData().forPath(ZkPath.CONFIG_ENSEMBLE_URL.getPath(), connectionUrl.getBytes(Charsets.UTF_8))
                            .and().commit();

                    // Wait until all containers switched
                    boolean allStarted = false;
                    while (!allStarted && System.currentTimeMillis() - t0 < options.getMigrationTimeout()) {
                        allStarted = true;
                        for (Container container : allContainers) {
                            allStarted &= exists(dst, ZkPath.CONTAINER_ALIVE.getPath(container.getId())) != null;
                        }
                        if (!allStarted) {
                            Thread.sleep(1000);
                        }
                    }
                    if (!allStarted) {
                        throw new EnsembleModificationFailed("Timeout waiting for containers to join the new ensemble", EnsembleModificationFailed.Reason.TIMEOUT);
                    }
                    LOGGER.info("Migration successful. Cleaning up");
                    // Wait until the new datastore has been registered
                    synchronized (result) {
                        if (result.get() == null) {
                            result.wait();
                        }
                    }
                    // Remove old profiles
                    for (String container : oldContainers) {
                        cleanUpEnsembleProfiles(result.get(), container, oldClusterId);
                    }

                } finally {
                    dst.close();
                }
            } else {
                ProfileBuilder builder = ProfileBuilder.Factory.createFrom(defaultProfile);
                zkConfig = new HashMap<>(zkConfig);
                zkConfig.put("zookeeper.password", "${zk:" + ZkPath.CONFIG_ENSEMBLE_PASSWORD.getPath() + "}");
                zkConfig.put("zookeeper.url", "${zk:" + ZkPath.CONFIG_ENSEMBLE_URL.getPath() + "}");
                builder.addConfiguration(Constants.ZOOKEEPER_CLIENT_PID, zkConfig);
                profileRegistry.get().updateProfile(builder.getProfile());
            }
        } catch (Exception e) {
            throw EnsembleModificationFailed.launderThrowable(e);
        }
    }
View Full Code Here

Examples of io.fabric8.api.ProfileBuilder

            parentProfile = version.getRequiredProfile(parentProfileName);
        }
        if (group == null || profileId == null)
            return parentProfile;
       
        ProfileBuilder builder;
       
        // create a profile if it doesn't exist
        boolean create = !version.hasProfile(profileId);
        if (create) {
            builder = ProfileBuilder.Factory.create(versionId, profileId);
        } else {
            Profile profile = version.getRequiredProfile(profileId);
            builder = ProfileBuilder.Factory.createFrom(profile);
        }

        // set the parent if its specified
        if (parentProfile != null) {
            builder.addParent(parentProfile.getId());
        }

        Map<String, String> config = builder.getConfiguration(MQ_CONNECTION_FACTORY_PID);
        config = config != null ? new HashMap<>(config) : new HashMap<String, String>();
        config.put(GROUP, group);
        builder.addConfiguration(MQ_CONNECTION_FACTORY_PID, config);
       
        Profile profile = builder.getProfile();
        return create ? profileService.createProfile(profile) : profileService.updateProfile(profile);
    }
View Full Code Here

Examples of io.fabric8.api.ProfileBuilder

        return Profiles.getEffectiveProfile(fabricService.get(), profileService.getOverlayProfile(versionProfile));
    }

  private Profile getVersionProfile(Version version) {
    String profileId = "#version-" + version.getId();
    ProfileBuilder builder = ProfileBuilder.Factory.create(profileId).version(version.getId());
    VersionProfileOptionsProvider optionsProvider = new VersionProfileOptionsProvider(version);
    return builder.addOptions(optionsProvider).getProfile();
  }
View Full Code Here

Examples of io.fabric8.api.ProfileBuilder

    }

    private void editProfile(Profile profile) throws Exception {
        boolean editInLine = false;

        ProfileBuilder builder = ProfileBuilder.Factory.createFrom(profile);
       
        if (delete || remove) {
            editInLine = true;
        }

        if (features != null && features.length > 0) {
            editInLine = true;
            handleFeatures(builder, features, profile);
        }
        if (repositories != null && repositories.length > 0) {
            editInLine = true;
            handleFeatureRepositories(builder, repositories, profile);
        }
        if (libs != null && libs.length > 0) {
            editInLine = true;
            handleLibraries(builder, libs, profile, "lib", LIB_PREFIX);
        }
        if (endorsed != null && endorsed.length > 0) {
            editInLine = true;
            handleLibraries(builder, endorsed, profile, "endorsed lib", ENDORSED_PREFIX);
        }
        if (extension != null && extension.length > 0) {
            editInLine = true;
            handleLibraries(builder, extension, profile, "extension lib", EXT_PREFIX);
        }
        if (bundles != null && bundles.length > 0) {
            editInLine = true;
            handleBundles(builder, bundles, profile);
        }
        if (fabs != null && fabs.length > 0) {
            editInLine = true;
            handleFabs(builder, fabs, profile);
        }
        if (overrides != null && overrides.length > 0) {
            editInLine = true;
            handleOverrides(builder, overrides, profile);
        }

        if (pidProperties != null && pidProperties.length > 0) {
            editInLine = handlePid(builder, pidProperties, profile);
        }

        if (systemProperties != null && systemProperties.length > 0) {
            editInLine = true;
            handleSystemProperties(builder, systemProperties, profile);
        }

        if (configProperties != null && configProperties.length > 0) {
            editInLine = true;
            handleConfigProperties(builder, configProperties, profile);
        }

        if (!editInLine) {
            resource = resource != null ? resource : "io.fabric8.agent.properties";
            //If a single pid has been selected, but not a key value has been specified or import has been selected,
            //then open the resource in the editor.
            if (pidProperties != null && pidProperties.length == 1) {
                resource = pidProperties[0] + ".properties";
            }
            openInEditor(profile, resource);
        }
       
        profileService.updateProfile(builder.getProfile());
    }
View Full Code Here

Examples of io.fabric8.api.ProfileBuilder

                }
                String profileId = parts[0];
                String versionId = parts[1];
                String resource = parts[2];
                Profile profile = profileRegistry.getRequiredProfile(versionId, profileId);
                ProfileBuilder builder = ProfileBuilder.Factory.createFrom(profile);
                builder.addFileConfiguration(resource, content.getBytes());
                profileRegistry.updateProfile(builder.getProfile());
            } catch (Exception e) {
                return false;
            }
            return true;
        }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.