use of io.fabric8.patch.management.conflicts.Resolver 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;
}
use of io.fabric8.patch.management.conflicts.Resolver in project fabric8 by jboss-fuse.
the class KarafContainerRegistration method activateInternal.
private void activateInternal() {
RuntimeProperties sysprops = runtimeProperties.get();
runtimeIdentity = sysprops.getRuntimeIdentity();
String version = sysprops.getProperty("fabric.version", ZkDefs.DEFAULT_VERSION);
String profiles = sysprops.getProperty("fabric.profiles");
try {
if (profiles != null) {
String versionNode = CONFIG_CONTAINER.getPath(runtimeIdentity);
String profileNode = CONFIG_VERSIONS_CONTAINER.getPath(version, runtimeIdentity);
createDefault(curator.get(), versionNode, version);
createDefault(curator.get(), profileNode, profiles);
}
checkAlive();
String domainsNode = CONTAINER_DOMAINS.getPath(runtimeIdentity);
Stat stat = exists(curator.get(), domainsNode);
if (stat != null) {
deleteSafe(curator.get(), domainsNode);
}
boolean openshiftEnv = Strings.notEmpty(System.getenv("OPENSHIFT_FUSE_DIR"));
ZooKeeperUtils.createDefault(curator.get(), CONTAINER_BINDADDRESS.getPath(runtimeIdentity), bootstrapConfiguration.get().getBindAddress());
ZooKeeperUtils.createDefault(curator.get(), CONTAINER_RESOLVER.getPath(runtimeIdentity), getContainerResolutionPolicy(curator.get(), runtimeIdentity));
setData(curator.get(), CONTAINER_LOCAL_HOSTNAME.getPath(runtimeIdentity), HostUtils.getLocalHostName());
if (openshiftEnv) {
setData(curator.get(), CONTAINER_LOCAL_IP.getPath(runtimeIdentity), System.getenv("OPENSHIFT_FUSE_IP"));
setData(curator.get(), CONTAINER_PUBLIC_IP.getPath(runtimeIdentity), HostUtils.getLocalIp());
} else {
setData(curator.get(), CONTAINER_LOCAL_IP.getPath(runtimeIdentity), HostUtils.getLocalIp());
}
// Mostly usable for adding values when creating containers without an existing ensemble.
for (String resolver : ZkDefs.VALID_RESOLVERS) {
String address = (String) bootstrapConfiguration.get().getConfiguration().get(resolver);
if (address != null && !address.isEmpty() && exists(curator.get(), CONTAINER_ADDRESS.getPath(runtimeIdentity, resolver)) == null) {
setData(curator.get(), CONTAINER_ADDRESS.getPath(runtimeIdentity, resolver), address);
}
}
ip = getSubstitutedData(curator.get(), getContainerPointer(curator.get(), runtimeIdentity));
setData(curator.get(), CONTAINER_IP.getPath(runtimeIdentity), ip);
if (Boolean.parseBoolean(runtimeProperties.get().getProperty("service.geoip.enabled", "false"))) {
createDefault(curator.get(), CONTAINER_GEOLOCATION.getPath(runtimeIdentity), geoLocationService.get().getGeoLocation());
}
// We are creating a dummy container object, since this might be called before the actual container is ready.
Container current = new ImmutableContainerBuilder().id(runtimeIdentity).ip(ip).build();
if (System.getProperty(SystemProperties.JAVA_RMI_SERVER_HOSTNAME) == null) {
System.setProperty(SystemProperties.JAVA_RMI_SERVER_HOSTNAME, current.getIp());
}
registerJmx(current);
registerSsh(current);
registerHttp(current);
// Set the port range values
String minimumPort = sysprops.getProperty(ZkDefs.MINIMUM_PORT);
if (minimumPort == null) {
String minPort = (String) bootstrapConfiguration.get().getConfiguration().get("minimum.port");
minimumPort = minPort;
}
String maximumPort = sysprops.getProperty(ZkDefs.MAXIMUM_PORT);
if (maximumPort == null) {
String maxPort = (String) bootstrapConfiguration.get().getConfiguration().get("maximum.port");
maximumPort = maxPort;
}
createDefault(curator.get(), CONTAINER_PORT_MIN.getPath(runtimeIdentity), minimumPort);
createDefault(curator.get(), CONTAINER_PORT_MAX.getPath(runtimeIdentity), maximumPort);
} catch (Exception e) {
LOGGER.warn("Error updating Fabric Container information. This exception will be ignored.", e);
}
}
use of io.fabric8.patch.management.conflicts.Resolver in project fabric8 by jboss-fuse.
the class ArchetypeGenerateAction method fetchArchetype.
/**
* Fetches archetype from the configured repositories
* TODO: make this code available to hawt.io/JMX too
*/
private File fetchArchetype(Archetype archetype) throws IOException {
MavenResolver resolver = MavenResolvers.createMavenResolver(new Hashtable<String, String>(), "org.ops4j.pax.url.mvn");
DownloadManager dm = DownloadManagers.createDownloadManager(resolver, Executors.newSingleThreadScheduledExecutor());
final AtomicReference<File> file = new AtomicReference<>();
String url = String.format("mvn:%s/%s/%s", archetype.groupId, archetype.artifactId, archetype.version);
Downloader downloader = dm.createDownloader();
downloader.download(url, new DownloadCallback() {
@Override
public void downloaded(StreamProvider provider) throws Exception {
file.set(provider.getFile());
}
});
// wait for download
try {
boolean init = false;
for (int i = 0; i < 2 * 60 && file.get() == null; i++) {
// dont do anything in the first 3 seconds as we likely can download it faster
if (i > 3) {
if (!init) {
System.out.print("Downloading archetype in progress: ");
init = true;
}
System.out.print(".");
}
// only sleep 0.5 sec so we can react faster
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.err.println("\nFailed to download " + archetype);
throw new IOException(e.getMessage(), e);
}
try {
downloader.await();
return file.get();
} catch (Exception e) {
System.err.println("\nFailed to download archetype within 60 seconds: " + archetype);
throw new IOException("Failed to download archetype within 60 seconds: " + archetype);
}
}
use of io.fabric8.patch.management.conflicts.Resolver in project fabric8 by jboss-fuse.
the class AbstractManagedContainer method create.
@Override
public final synchronized void create(T configuration) throws LifecycleException {
if (state != null)
throw new IllegalStateException("Cannot create container in state: " + state);
this.configuration = configuration;
// [TODO] [FABRIC-929] No connector available to access repository jboss-public-repository-group
// Remove this hack. It shouldbe possible to use the shrinkwrap maven resolver
boolean useShrinkwrap = System.getProperty("shrinkwrap.resolver") != null;
for (MavenCoordinates artefact : configuration.getMavenCoordinates()) {
File zipfile = useShrinkwrap ? shrinkwrapResolve(artefact) : localResolve(artefact);
GenericArchive archive = ShrinkWrap.createFromZipFile(GenericArchive.class, zipfile);
ExplodedExporter exporter = archive.as(ExplodedExporter.class);
File targetdir = configuration.getTargetDirectory();
if (!targetdir.isDirectory() && !targetdir.mkdirs())
throw new IllegalStateException("Cannot create target dir: " + targetdir);
if (containerHome == null) {
exporter.exportExploded(targetdir, "");
File[] childDirs = targetdir.listFiles();
if (childDirs.length != 1)
throw new IllegalStateException("Expected one child directory, but was: " + Arrays.asList(childDirs));
containerHome = childDirs[0];
} else {
exporter.exportExploded(containerHome, "");
}
}
state = State.CREATED;
try {
doConfigure(configuration);
} catch (Exception ex) {
throw new LifecycleException("Cannot configure container", ex);
}
}
use of io.fabric8.patch.management.conflicts.Resolver in project fabric8 by jboss-fuse.
the class Agent method provision.
public void provision(Map<String, Feature> allFeatures, Set<String> features, Set<String> bundles, Set<String> reqs, Set<String> overrides, Set<String> optionals, Map<String, Map<VersionRange, Map<String, String>>> metadata) throws Exception {
Callable<Map<String, Resource>> res = loadResources(manager, metadata, optionals);
// TODO: requirements should be able to be assigned to a region
Map<String, Set<String>> requirements = new HashMap<>();
for (String feature : features) {
addToMapSet(requirements, ROOT_REGION, "feature:" + feature);
}
for (String bundle : bundles) {
addToMapSet(requirements, ROOT_REGION, "bundle:" + bundle);
}
for (String req : reqs) {
addToMapSet(requirements, ROOT_REGION, "req:" + req);
}
Deployer.DeploymentRequest request = new Deployer.DeploymentRequest();
request.updateSnaphots = updateSnaphots;
request.bundleUpdateRange = bundleUpdateRange;
request.featureResolutionRange = featureResolutionRange;
request.globalRepository = new StaticRepository(res.call().values());
request.overrides = overrides;
request.requirements = requirements;
request.stateChanges = Collections.emptyMap();
request.options = options;
request.metadata = metadata;
request.bundleStartTimeout = bundleStartTimeout;
Deployer.DeploymentState dstate = new Deployer.DeploymentState();
// Service bundle
dstate.serviceBundle = serviceBundle;
// Start level
FrameworkStartLevel fsl = systemBundleContext.getBundle().adapt(FrameworkStartLevel.class);
dstate.initialBundleStartLevel = fsl.getInitialBundleStartLevel();
dstate.currentStartLevel = fsl.getStartLevel();
// Bundles
dstate.bundles = new HashMap<>();
for (Bundle bundle : systemBundleContext.getBundles()) {
dstate.bundles.put(bundle.getBundleId(), bundle);
}
// Features
dstate.features = allFeatures;
// Region -> bundles mapping
// Region -> policy mapping
dstate.bundlesPerRegion = new HashMap<>();
dstate.filtersPerRegion = new HashMap<>();
if (digraph == null) {
for (Long id : dstate.bundles.keySet()) {
addToMapSet(dstate.bundlesPerRegion, ROOT_REGION, id);
}
} else {
RegionDigraph clone = digraph.copy();
for (Region region : clone.getRegions()) {
// Get bundles
dstate.bundlesPerRegion.put(region.getName(), new HashSet<>(region.getBundleIds()));
// Get policies
Map<String, Map<String, Set<String>>> edges = new HashMap<>();
for (RegionDigraph.FilteredRegion fr : clone.getEdges(region)) {
Map<String, Set<String>> policy = new HashMap<>();
Map<String, Collection<String>> current = fr.getFilter().getSharingPolicy();
for (String ns : current.keySet()) {
for (String f : current.get(ns)) {
addToMapSet(policy, ns, f);
}
}
edges.put(fr.getRegion().getName(), policy);
}
dstate.filtersPerRegion.put(region.getName(), edges);
}
}
final State state = new State();
try {
storage.load(state);
} catch (IOException e) {
LOGGER.warn("Error loading agent state", e);
}
if (state.managedBundles.isEmpty()) {
for (Bundle b : systemBundleContext.getBundles()) {
if (b.getBundleId() != 0) {
addToMapSet(state.managedBundles, ROOT_REGION, b.getBundleId());
}
}
}
// corresponding jar url and use that one to compute the checksum of the bundle.
for (Map.Entry<Long, Bundle> entry : dstate.bundles.entrySet()) {
long id = entry.getKey();
Bundle bundle = entry.getValue();
if (id > 0 && isUpdateable(bundle) && !state.bundleChecksums.containsKey(id)) {
try {
URL url = bundle.getResource("META-INF/MANIFEST.MF");
URLConnection con = url.openConnection();
Method method = con.getClass().getDeclaredMethod("getLocalURL");
method.setAccessible(true);
String jarUrl = ((URL) method.invoke(con)).toExternalForm();
if (jarUrl.startsWith("jar:")) {
String jar = jarUrl.substring("jar:".length(), jarUrl.indexOf("!/"));
jar = new URL(jar).getFile();
long checksum = ChecksumUtils.checksumFile(new File(jar));
state.bundleChecksums.put(id, checksum);
}
} catch (Throwable t) {
LOGGER.debug("Error calculating checksum for bundle: %s", bundle, t);
}
}
}
dstate.state = state;
Set<String> prereqs = new HashSet<>();
while (true) {
try {
Deployer.DeployCallback callback = new BaseDeployCallback() {
@Override
public void phase(String message) {
Agent.this.updateStatus(message);
}
@Override
public void saveState(State newState) {
state.replace(newState);
try {
Agent.this.saveState(newState);
} catch (IOException e) {
LOGGER.warn("Error storing agent state", e);
}
}
@Override
public void provisionList(Set<Resource> resources) {
Agent.this.provisionList(resources);
}
@Override
public void restoreConfigAdminIfNeeded() {
if (configInstaller != null) {
configInstaller.restoreConfigAdminIfNeeded();
}
}
@Override
public boolean done(boolean agentStarted, List<String> urls) {
return Agent.this.done(agentStarted, urls);
}
};
// FABRIC-790, FABRIC-981 - wait for ProfileUrlHandler before attempting to load bundles (in subsystem.resolve())
// (which may be the case with bundle.xxx=blueprint:profile:xxx URLs in io.fabric8.agent PID)
// https://developer.jboss.org/message/920681 - 30 seconds is too low sometimes
// there was "url.handler.timeouts" option for agent, but it was removed during migration to karaf 4.x resolver
// LOGGER.debug("Waiting for ProfileUrlHandler");
// awaitService(URLStreamHandlerService.class, "(url.handler.protocol=profile)", 30, TimeUnit.SECONDS);
// LOGGER.debug("Waiting for ProfileUrlHandler finished");
Deployer deployer = new Deployer(manager, callback);
deployer.setDeploymentAgentId(deploymentAgentId);
deployer.deploy(dstate, request);
break;
} catch (Deployer.PartialDeploymentException e) {
if (!prereqs.containsAll(e.getMissing())) {
prereqs.addAll(e.getMissing());
} else {
throw new Exception("Deployment aborted due to loop in missing prerequisites: " + e.getMissing());
}
}
}
}
Aggregations