void configureInternal(Map<String, ?> conf) throws Exception { configuration = configurer.configure(conf, this); if (Strings.isNullOrBlank(runtimeId)) { throw new IllegalArgumentException("Runtime id must not be null or empty."); } if (Strings.isNullOrBlank(localResolver)) { localResolver = globalResolver; } String decodedZookeeperPassword = null; Properties userProps = new Properties(); try { userProps.load(new File(confDir, "users.properties")); } catch (IOException e) { LOGGER.warn("Failed to load users from etc/users.properties. No users will be imported.", e); } if (Strings.isNotBlank(zookeeperPassword)) { decodedZookeeperPassword = PasswordEncoder.decode(zookeeperPassword); } else if (userProps.containsKey(DEFAULT_ADMIN_USER)) { String passwordAndRole = userProps.getProperty(DEFAULT_ADMIN_USER).trim(); decodedZookeeperPassword = passwordAndRole.substring(0, passwordAndRole.indexOf(ROLE_DELIMITER)); } else { decodedZookeeperPassword = PasswordEncoder.encode(CreateEnsembleOptions.generatePassword()); } if (userProps.isEmpty()) { userProps.put( DEFAULT_ADMIN_USER, decodedZookeeperPassword + ROLE_DELIMITER + DEFAULT_ADMIN_ROLE); } options = CreateEnsembleOptions.builder() .bindAddress(bindAddress) .agentEnabled(agentAutoStart) .ensembleStart(ensembleAutoStart) .zookeeperPassword(decodedZookeeperPassword) .zooKeeperServerPort(zookeeperServerPort) .zooKeeperServerConnectionPort(zookeeperServerConnectionPort) .autoImportEnabled(profilesAutoImport) .importPath(profilesAutoImportPath) .resolver(localResolver) .globalResolver(globalResolver) .users(userProps) .profiles(profiles) .version(version) .build(); }
@Override public String getZookeeperPassword() { assertValid(); String rawZookeeperPassword = getZookeeperInfo("zookeeper.password"); if (rawZookeeperPassword != null) { return PasswordEncoder.decode(rawZookeeperPassword); } else { return null; } }
/** * Creates the environment variables for the given container options using the profiles specified * in the options to figure out what environment variables to use. */ public static Map<String, String> getEnvironmentVariables( FabricService service, CreateContainerBasicOptions options) { Set<String> profileIds = options.getProfiles(); String versionId = options.getVersion(); String zookeeperUrl = service.getZookeeperUrl(); String zookeeperUser = service.getZooKeeperUser(); String zookeeperPasswordRaw = service.getZookeeperPassword(); String zookeeperPassword = zookeeperPasswordRaw; if (zookeeperPassword != null) { zookeeperPassword = PasswordEncoder.encode(zookeeperPassword); } String localIp = service.getCurrentContainer().getLocalIp(); if (!Strings.isNullOrBlank(localIp)) { int idx = zookeeperUrl.lastIndexOf(':'); if (idx > 0) { localIp += zookeeperUrl.substring(idx); } zookeeperUrl = localIp; } Map<String, String> envVarsOverlay = Profiles.getOverlayConfiguration( service, profileIds, versionId, EnvironmentVariables.ENVIRONMENT_VARIABLES_PID); String containerName = options.getName(); envVarsOverlay.put(EnvironmentVariables.KARAF_NAME, containerName); envVarsOverlay.put(EnvironmentVariables.CONTAINER_NAME, containerName); if (!options.isEnsembleServer()) { if (!envVarsOverlay.containsKey(EnvironmentVariables.ZOOKEEPER_URL)) { envVarsOverlay.put(EnvironmentVariables.ZOOKEEPER_URL, zookeeperUrl); } if (!envVarsOverlay.containsKey(EnvironmentVariables.ZOOKEEPER_USER)) { envVarsOverlay.put(EnvironmentVariables.ZOOKEEPER_USER, zookeeperUser); } if (!envVarsOverlay.containsKey(EnvironmentVariables.ZOOKEEPER_PASSWORD)) { envVarsOverlay.put(EnvironmentVariables.ZOOKEEPER_PASSWORD, zookeeperPassword); } if (!envVarsOverlay.containsKey(EnvironmentVariables.ZOOKEEPER_PASSWORD_RAW)) { envVarsOverlay.put(EnvironmentVariables.ZOOKEEPER_PASSWORD_RAW, zookeeperPasswordRaw); } if (!envVarsOverlay.containsKey(EnvironmentVariables.ZOOKEEPER_PASSWORD_ENCODE)) { String zkPasswordEncode = System.getProperty("zookeeper.password.encode", "true"); envVarsOverlay.put(EnvironmentVariables.ZOOKEEPER_PASSWORD_ENCODE, zkPasswordEncode); } } return envVarsOverlay; }
/** Creates ZooKeeper client configuration. */ public void createZooKeeeperClientConfig(String connectionUrl, CreateEnsembleOptions options) throws IOException { Dictionary<String, Object> properties = new Hashtable<String, Object>(); if (options.isAutoImportEnabled()) { loadPropertiesFrom( properties, options.getImportPath() + "/fabric/profiles/default.profile/io.fabric8.zookeeper.properties"); } properties.put("zookeeper.url", connectionUrl); properties.put( "zookeeper.timeout", System.getProperties().containsKey("zookeeper.timeout") ? System.getProperties().getProperty("zookeeper.timeout") : "30000"); properties.put("fabric.zookeeper.pid", Constants.ZOOKEEPER_CLIENT_PID); properties.put("zookeeper.password", PasswordEncoder.encode(options.getZookeeperPassword())); Configuration config = configAdmin.get().getConfiguration(Constants.ZOOKEEPER_CLIENT_PID, null); config.update(properties); }
protected Object doExecute() throws Exception { // 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) .fromRuntimeProperties(runtimeProperties) .bootstrapTimeout(bootstrapTimeout) .waitForProvision(waitForProvisioning) .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) { builder.globalResolver(globalResolver); bootProperties.put(ZkDefs.GLOBAL_RESOLVER_PROPERTY, globalResolver); } if (resolver != 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); } // Configure External Git Repository. if (externalGitUrl != null) { builder.dataStoreProperty(GIT_REMOTE_URL, externalGitUrl); } if (externalGitUser != null) { builder.dataStoreProperty(GIT_REMOTE_USER, externalGitUser); } if (externalGitPassword != null) { builder.dataStoreProperty(GIT_REMOTE_PASSWORD, externalGitPassword); } if (profiles != null && profiles.size() > 0) { builder.profiles(profiles); } if (nonManaged) { builder.agentEnabled(false); } else { builder.agentEnabled(true); } builder.minimumPort(minimumPort); builder.minimumPort(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); Path propsPath = runtimeProperties.getConfPath().resolve("users.properties"); Properties userProps = new Properties(propsPath.toFile()); 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 = 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)) { newUserRole = newUserRole + ROLE_DELIMITER + roles[j].trim(); } } } } else { // it's an user reference if (!infos[i].trim().equals(oldUserRole)) { newUserRole = newUserRole + ROLE_DELIMITER + 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); } bootConfiguration.update(bootProperties); CreateEnsembleOptions options = builder.users(userProps).withUser(newUser, newUserPassword, newUserRole).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:"******")\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:"******"Using specified zookeeper password:"******"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; }
@Override public CreateContainerMetadata[] createContainers( CreateContainerOptions options, CreationStateListener listener) { assertValid(); try { final ContainerProvider provider = getProvider(options.getProviderType()); if (provider == null) { throw new FabricException( "Unable to find a container provider supporting '" + options.getProviderType() + "'"); } String originalName = options.getName(); if (originalName == null || originalName.length() == 0) { throw new FabricException("A name must be specified when creating containers"); } if (listener == null) { listener = new NullCreationStateListener(); } ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); Map optionsMap = mapper.readValue(mapper.writeValueAsString(options), Map.class); String versionId = options.getVersion() != null ? options.getVersion() : getDataStore().getDefaultVersion(); Set<String> profileIds = options.getProfiles(); if (profileIds == null || profileIds.isEmpty()) { profileIds = new LinkedHashSet<String>(); profileIds.add("default"); } optionsMap.put("version", versionId); optionsMap.put("profiles", profileIds); optionsMap.put("number", 0); final List<CreateContainerMetadata> metadatas = new CopyOnWriteArrayList<CreateContainerMetadata>(); int orgNumber = options.getNumber(); int number = Math.max(orgNumber, 1); final CountDownLatch latch = new CountDownLatch(number); for (int i = 1; i <= number; i++) { String containerName; if (orgNumber >= 1) { containerName = originalName + i; } else { containerName = originalName; } optionsMap.put("name", containerName); // Check if datastore configuration has been specified and fallback to current container // settings. if (!hasValidDataStoreProperties(optionsMap)) { optionsMap.put("dataStoreProperties", getDataStore().getDataStoreProperties()); } Class cl = options .getClass() .getClassLoader() .loadClass(options.getClass().getName() + "$Builder"); CreateContainerBasicOptions.Builder builder = (CreateContainerBasicOptions.Builder) mapper.readValue(mapper.writeValueAsString(optionsMap), cl); // We always want to pass the obfuscated version of the password to the container provider. builder = (CreateContainerBasicOptions.Builder) builder.zookeeperPassword(PasswordEncoder.encode(getZookeeperPassword())); final CreateContainerOptions containerOptions = builder.build(); final CreationStateListener containerListener = listener; new Thread("Creating container " + containerName) { public void run() { try { getDataStore().createContainerConfig(containerOptions); CreateContainerMetadata metadata = provider.create(containerOptions, containerListener); if (metadata.isSuccess()) { Container parent = containerOptions.getParent() != null ? getContainer(containerOptions.getParent()) : null; // An ensemble server can be created without an existing ensemble. // In this case container config will be created by the newly created container. // TODO: We need to make sure that this entries are somehow added even to ensemble // servers. if (!containerOptions.isEnsembleServer()) { getDataStore().createContainerConfig(metadata); } ContainerImpl container = new ContainerImpl(parent, metadata.getContainerName(), FabricServiceImpl.this); metadata.setContainer(container); LOGGER.info( "The container " + metadata.getContainerName() + " has been successfully created"); } else { LOGGER.info( "The creation of the container " + metadata.getContainerName() + " has failed", metadata.getFailure()); } metadatas.add(metadata); } catch (Throwable t) { CreateContainerBasicMetadata metadata = new CreateContainerBasicMetadata(); metadata.setCreateOptions(containerOptions); metadata.setFailure(t); metadatas.add(metadata); getDataStore().deleteContainer(containerOptions.getName()); } finally { latch.countDown(); } } }.start(); } if (!latch.await(15, TimeUnit.MINUTES)) { throw new FabricException("Timeout waiting for container creation"); } return metadatas.toArray(new CreateContainerMetadata[metadatas.size()]); } catch (Exception e) { LOGGER.error("Failed to create containers " + e, e); throw FabricException.launderThrowable(e); } }