Search in sources :

Example 76 with io.fabric8.kubernetes.api.model

use of io.fabric8.kubernetes.api.model in project fabric8 by jboss-fuse.

the class JoinAction method installBundles.

public void installBundles() throws BundleException {
    BundleUtils bundleUtils = new BundleUtils(bundleContext);
    Bundle bundleFabricCommands = bundleUtils.findBundle("io.fabric8.fabric-commands");
    if (bundleFabricCommands == null) {
        bundleFabricCommands = bundleUtils.installBundle("mvn:io.fabric8/fabric-commands/" + FabricConstants.FABRIC_VERSION);
    }
    bundleFabricCommands.start();
    Bundle bundleFabricAgent = bundleUtils.findBundle("io.fabric8.fabric-agent");
    if (nonManaged && bundleFabricAgent == null) {
    // do nothing
    } else if (nonManaged && bundleFabricAgent != null) {
        bundleFabricAgent.stop();
    } else if (bundleFabricAgent == null) {
        bundleFabricAgent = bundleUtils.installBundle("mvn:io.fabric8/fabric-agent/" + FabricConstants.FABRIC_VERSION);
        bundleFabricAgent.start();
    } else {
        bundleFabricAgent.start();
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BundleUtils(io.fabric8.utils.BundleUtils)

Example 77 with io.fabric8.kubernetes.api.model

use of io.fabric8.kubernetes.api.model in project fabric8 by jboss-fuse.

the class FabricCommand method getProfiles.

/**
 * Gets (or creates) all the profiles for the given names.
 * <p/>
 * <b>Important:</b> If a profile does not already exists with the given name, then a new {@link Profile} is
 * created and returned in the list.
 *
 * @see #getExistingProfiles(io.fabric8.api.FabricService, io.fabric8.api.Version, java.util.List)
 */
public static Profile[] getProfiles(FabricService fabricService, Version version, List<String> names) {
    List<Profile> allProfiles = version.getProfiles();
    List<Profile> profiles = new ArrayList<>();
    if (names == null) {
        return new Profile[0];
    }
    for (String profileId : names) {
        Profile profile = null;
        for (Profile p : allProfiles) {
            if (profileId.equals(p.getId())) {
                profile = p;
                break;
            }
        }
        if (profile == null) {
            ProfileBuilder builder = ProfileBuilder.Factory.create(version.getId(), profileId);
            ProfileService profileService = fabricService.adapt(ProfileService.class);
            profile = profileService.createProfile(builder.getProfile());
        }
        profiles.add(profile);
    }
    return profiles.toArray(new Profile[profiles.size()]);
}
Also used : ProfileService(io.fabric8.api.ProfileService) ArrayList(java.util.ArrayList) ProfileBuilder(io.fabric8.api.ProfileBuilder) Profile(io.fabric8.api.Profile)

Example 78 with io.fabric8.kubernetes.api.model

use of io.fabric8.kubernetes.api.model in project fabric8 by jboss-fuse.

the class DataStoreBootstrapTemplate method doWith.

@Override
public void doWith(ProfileRegistry profileRegistry, DataStore dataStore) throws Exception {
    String versionId = options.getVersion();
    int minimumPort = options.getMinimumPort();
    int maximumPort = options.getMaximumPort();
    String zooKeeperServerHost = options.getBindAddress();
    int zooKeeperServerPort = options.getZooKeeperServerPort();
    int zooKeeperServerConnectionPort = options.getZooKeeperServerConnectionPort();
    int mappedPort = Ports.mapPortToRange(zooKeeperServerPort, minimumPort, maximumPort);
    CuratorFramework curator = null;
    try {
        curator = createCuratorFramework(connectionUrl, options);
        curator.start();
        curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
        LockHandle writeLock = profileRegistry.aquireWriteLock();
        try {
            // Make the import path absolute
            File importPath = new File(options.getImportPath());
            if (!importPath.isAbsolute()) {
                importPath = new File(homeDir, options.getImportPath());
            }
            // Import data into the DataStore
            if (options.isAutoImportEnabled()) {
                if (importPath.isDirectory()) {
                    profileRegistry.importFromFileSystem(importPath.getAbsolutePath());
                } else {
                    LOGGER.warn("Profile import dir does not exist: {}", importPath);
                }
            }
            // set the fabric configuration
            ZooKeeperUtils.setData(curator, ZkPath.CONFIG_DEFAULT_VERSION.getPath(), versionId);
            // Default JAAS config
            Map<String, String> jaasConfig = Collections.singletonMap("encryption.enabled", "${zk:/fabric/authentication/encryption.enabled}");
            // Default Zookeeper config
            Properties zkProps = new Properties();
            zkProps.setProperty("zookeeper.url", "${zk:" + ZkPath.CONFIG_ENSEMBLE_URL.getPath() + "}");
            zkProps.setProperty("zookeeper.password", "${zk:" + ZkPath.CONFIG_ENSEMBLE_PASSWORD.getPath() + "}");
            // Create or update default profile
            Profile defaultProfile = profileRegistry.getProfile(versionId, "default");
            if (defaultProfile == null) {
                ProfileBuilder prfBuilder = ProfileBuilder.Factory.create(versionId, "default");
                prfBuilder.addConfiguration("io.fabric8.jaas", jaasConfig);
                prfBuilder.addFileConfiguration("io.fabric8.zookeeper.properties", DataStoreUtils.toBytes(zkProps));
                Profile profile = prfBuilder.getProfile();
                if (profileRegistry.hasVersion(versionId)) {
                    profileRegistry.createProfile(profile);
                } else {
                    VersionBuilder verBuilder = VersionBuilder.Factory.create(versionId);
                    Version version = verBuilder.addProfile(profile).getVersion();
                    profileRegistry.createVersion(version);
                }
            } else {
                ProfileBuilder builder = ProfileBuilder.Factory.createFrom(defaultProfile);
                // be careful not to mess with existing *.properties files - for better "diffability"
                // during git-based patching
                Map<String, String> existingJaasConfig = builder.getConfiguration("io.fabric8.jaas");
                Map<String, String> existingZkConfig = builder.getConfiguration("io.fabric8.zookeeper");
                existingJaasConfig.putAll(jaasConfig);
                existingZkConfig.put("zookeeper.url", zkProps.getProperty("zookeeper.url"));
                existingZkConfig.put("zookeeper.password", zkProps.getProperty("zookeeper.password"));
                builder.addConfiguration("io.fabric8.jaas", existingJaasConfig);
                builder.addConfiguration("io.fabric8.zookeeper", existingZkConfig);
                profileRegistry.updateProfile(builder.getProfile());
            }
            ZooKeeperUtils.setData(curator, ZkPath.CONFIG_ENSEMBLE_URL.getPath(), "${zk:" + name + "/ip}:" + zooKeeperServerConnectionPort);
            ZooKeeperUtils.setData(curator, ZkPath.CONFIG_ENSEMBLE_PASSWORD.getPath(), PasswordEncoder.encode(options.getZookeeperPassword()));
            // Ensemble properties
            Properties ensembleProps = new Properties();
            ensembleProps.put("tickTime", String.valueOf(options.getZooKeeperServerTickTime()));
            ensembleProps.put("initLimit", String.valueOf(options.getZooKeeperServerInitLimit()));
            ensembleProps.put("syncLimit", String.valueOf(options.getZooKeeperServerSyncLimit()));
            ensembleProps.put("snapRetainCount", String.valueOf(options.getZookeeperSnapRetainCount()));
            ensembleProps.put("purgeInterval", String.valueOf(options.getZookeeperPurgeInterval()));
            ensembleProps.put("dataDir", options.getZooKeeperServerDataDir() + File.separator + "0000");
            loadPropertiesFrom(ensembleProps, importPath + "/fabric/profiles/default.profile/io.fabric8.zookeeper.server.properties");
            // Create ensemble profile
            String profileId = "fabric-ensemble-0000";
            IllegalStateAssertion.assertFalse(profileRegistry.hasProfile(versionId, profileId), "Profile already exists: " + versionId + "/" + profileId);
            ProfileBuilder ensembleProfileBuilder = ProfileBuilder.Factory.create(versionId, profileId);
            ensembleProfileBuilder.addAttribute(Profile.ABSTRACT, "true").addAttribute(Profile.HIDDEN, "true");
            ensembleProfileBuilder.addFileConfiguration("io.fabric8.zookeeper.server-0000.properties", DataStoreUtils.toBytes(ensembleProps));
            String ensembleProfileId = profileRegistry.createProfile(ensembleProfileBuilder.getProfile());
            // Ensemble server properties
            Properties serverProps = new Properties();
            serverProps.put("clientPort", String.valueOf(mappedPort));
            serverProps.put("clientPortAddress", zooKeeperServerHost);
            // Create ensemble server profile
            profileId = "fabric-ensemble-0000-1";
            IllegalStateAssertion.assertFalse(profileRegistry.hasProfile(versionId, profileId), "Profile already exists: " + versionId + "/" + profileId);
            ProfileBuilder serverProfileBuilder = ProfileBuilder.Factory.create(versionId, profileId);
            serverProfileBuilder.addAttribute(Profile.HIDDEN, "true").addAttribute(Profile.PARENTS, ensembleProfileId);
            serverProfileBuilder.addFileConfiguration("io.fabric8.zookeeper.server-0000.properties", DataStoreUtils.toBytes(serverProps));
            profileRegistry.createProfile(serverProfileBuilder.getProfile());
            ZooKeeperUtils.setData(curator, ZkPath.CONFIG_ENSEMBLES.getPath(), "0000");
            ZooKeeperUtils.setData(curator, ZkPath.CONFIG_ENSEMBLE.getPath("0000"), name);
            // configure fabric profile
            Profile fabricProfile = profileRegistry.getProfile(versionId, "fabric");
            if (fabricProfile == null) {
                ProfileBuilder builder = ProfileBuilder.Factory.create(versionId, "fabric");
                Properties agentProps = new Properties();
                agentProps.put("feature.fabric-commands", "fabric-commands");
                builder.addFileConfiguration("io.fabric8.agent.properties", DataStoreUtils.toBytes(agentProps));
                String createdId = profileRegistry.createProfile(builder.getProfile());
                fabricProfile = profileRegistry.getRequiredProfile(versionId, createdId);
            } else {
                ProfileBuilder builder = ProfileBuilder.Factory.createFrom(fabricProfile);
                Map<String, String> agentProps = builder.getConfiguration(Constants.AGENT_PID);
                agentProps.put("feature.fabric-commands", "fabric-commands");
                builder.addConfiguration(Constants.AGENT_PID, agentProps);
                String updatedId = profileRegistry.updateProfile(builder.getProfile());
                fabricProfile = profileRegistry.getRequiredProfile(versionId, updatedId);
            }
        } finally {
            writeLock.unlock();
        }
        ZooKeeperUtils.createDefault(curator, ZkPath.CONFIG_CONTAINER.getPath(name), versionId);
        StringBuilder profilesBuilder = new StringBuilder();
        Set<String> profiles = options.getProfiles();
        profilesBuilder.append("fabric").append(" ").append("fabric-ensemble-0000-1");
        for (String p : profiles) {
            if (!(p.equals("fabric") || p.equals("fabric-ensemble-0000-1"))) {
                profilesBuilder.append(" ").append(p);
            }
        }
        if (!options.isAgentEnabled()) {
            profilesBuilder.append(" ").append("unmanaged");
        }
        ZooKeeperUtils.createDefault(curator, ZkPath.CONFIG_VERSIONS_CONTAINER.getPath(versionId, name), profilesBuilder.toString());
        // outside of the profile storage area, so we'll keep these in zk
        EncryptionSupport encryption = addUsersToZookeeper(curator, options.getUsers());
        ZooKeeperUtils.createDefault(curator, "/fabric/authentication/encryption.enabled", Boolean.valueOf(encryption != null).toString());
        ZooKeeperUtils.createDefault(curator, "/fabric/authentication/domain", "karaf");
        ZooKeeperUtils.createDefault(curator, ZkPath.AUTHENTICATION_CRYPT_ALGORITHM.getPath(), "PBEWithMD5AndDES");
        ZooKeeperUtils.createDefault(curator, ZkPath.AUTHENTICATION_CRYPT_PASSWORD.getPath(), PasswordEncoder.encode(options.getZookeeperPassword()));
        // Ensure ACLs are from the beggining of the fabric tree.
        aclManager.fixAcl(curator, "/fabric", true);
    } finally {
        if (curator != null) {
            curator.close();
        }
    }
}
Also used : LockHandle(io.fabric8.api.LockHandle) Properties(java.util.Properties) ProfileBuilder(io.fabric8.api.ProfileBuilder) Profile(io.fabric8.api.Profile) CuratorFramework(org.apache.curator.framework.CuratorFramework) Version(io.fabric8.api.Version) VersionBuilder(io.fabric8.api.VersionBuilder) File(java.io.File) EncryptionSupport(org.apache.karaf.jaas.modules.encryption.EncryptionSupport)

Example 79 with io.fabric8.kubernetes.api.model

use of io.fabric8.kubernetes.api.model 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());
            }
        }
    }
}
Also used : EnumSet(java.util.EnumSet) Set(java.util.Set) MapUtils.addToMapSet(io.fabric8.agent.internal.MapUtils.addToMapSet) HashSet(java.util.HashSet) HashMap(java.util.HashMap) URL(java.net.URL) List(java.util.List) HashSet(java.util.HashSet) RegionDigraph(org.eclipse.equinox.region.RegionDigraph) Bundle(org.osgi.framework.Bundle) IOException(java.io.IOException) Method(java.lang.reflect.Method) FrameworkStartLevel(org.osgi.framework.startlevel.FrameworkStartLevel) URLConnection(java.net.URLConnection) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MultiException(io.fabric8.common.util.MultiException) Region(org.eclipse.equinox.region.Region) Collection(java.util.Collection) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File) StaticRepository(io.fabric8.agent.repository.StaticRepository)

Example 80 with io.fabric8.kubernetes.api.model

use of io.fabric8.kubernetes.api.model in project fabric8 by jboss-fuse.

the class MavenProxyResolutionTest method releaseIsAvailableInRemoteRepositoryUpdatingRelease.

@Test
public void releaseIsAvailableInRemoteRepositoryUpdatingRelease() throws IOException, InvalidMavenArtifactRequest {
    File remoteRepository = initFileRepository("rr");
    MavenResolver resolver = new ResolverBuilder().withRemoteRepositories(Collections.singletonList(remoteRepository)).withReleaseUpdates().withUpdatePolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS).build();
    MavenDownloadProxyServlet servlet = new MavenDownloadProxyServlet(resolver, runtime, null, 1, 0);
    mvnInstall(remoteRepository, "io.fabric8.test", "universalis-api", "0.1.0", at("10:00"), "a");
    File file = servlet.download("io/fabric8/test/universalis-api/0.1.0/universalis-api-0.1.0.jar");
    // first resolution
    assertThat(FileUtils.readFileToString(file), equalTo("a"));
    // don't do that, it's not proper use of maven. But sometimes we just have another deployment to public repository...
    mvnInstall(remoteRepository, "io.fabric8.test", "universalis-api", "0.1.0", at("11:00"), "b");
    // second resolution
    file = servlet.download("io/fabric8/test/universalis-api/0.1.0/universalis-api-0.1.0.jar");
    assertThat("Artifact will be updated though it's not proper usage of Maven", FileUtils.readFileToString(file), equalTo("b"));
}
Also used : MavenResolver(io.fabric8.maven.MavenResolver) File(java.io.File) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)29 FabricService (io.fabric8.api.FabricService)26 File (java.io.File)22 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)18 InputStream (java.io.InputStream)16 Logger (org.slf4j.Logger)16 ServiceLocator (io.fabric8.api.gravia.ServiceLocator)15 CommandSupport (io.fabric8.itests.support.CommandSupport)15 Deployment (org.jboss.arquillian.container.test.api.Deployment)15 StartLevelAware (org.jboss.arquillian.osgi.StartLevelAware)15 OSGiManifestBuilder (org.jboss.osgi.metadata.OSGiManifestBuilder)15 Asset (org.jboss.shrinkwrap.api.asset.Asset)15 JavaArchive (org.jboss.shrinkwrap.api.spec.JavaArchive)15 ServiceTracker (org.osgi.util.tracker.ServiceTracker)15 IOException (java.io.IOException)14 Action (org.apache.felix.gogo.commands.Action)14 AbstractCommand (org.apache.felix.gogo.commands.basic.AbstractCommand)14 MavenResolver (io.fabric8.maven.MavenResolver)11 Container (io.fabric8.api.Container)10 BundleContext (org.osgi.framework.BundleContext)9