Search in sources :

Example 1 with Profile

use of org.apache.karaf.profile.Profile in project karaf by apache.

the class Builder method doGenerateAssembly.

private void doGenerateAssembly() throws Exception {
    systemDirectory = homeDirectory.resolve("system");
    etcDirectory = homeDirectory.resolve("etc");
    LOGGER.info("Generating karaf assembly: " + homeDirectory);
    //
    // Create download manager
    //
    Dictionary<String, String> props = new Hashtable<>();
    if (offline) {
        props.put(ORG_OPS4J_PAX_URL_MVN_PID + "offline", "true");
    }
    if (localRepository != null) {
        props.put(Builder.ORG_OPS4J_PAX_URL_MVN_PID + ".localRepository", localRepository);
    }
    if (mavenRepositories != null) {
        props.put(Builder.ORG_OPS4J_PAX_URL_MVN_PID + ".repositories", mavenRepositories);
    }
    MavenResolver resolver = MavenResolvers.createMavenResolver(props, ORG_OPS4J_PAX_URL_MVN_PID);
    executor = Executors.newScheduledThreadPool(8);
    manager = new CustomDownloadManager(resolver, executor, null, translatedUrls);
    this.resolver = new ResolverImpl(new Slf4jResolverLog(LOGGER));
    //
    // Unzip kars
    //
    LOGGER.info("Unzipping kars");
    Map<String, RepositoryInfo> repositories = new LinkedHashMap<>(this.repositories);
    Downloader downloader = manager.createDownloader();
    for (String kar : kars.keySet()) {
        downloader.download(kar, null);
    }
    downloader.await();
    for (String karUri : kars.keySet()) {
        Kar kar = new Kar(manager.getProviders().get(karUri).getFile().toURI());
        kar.extract(systemDirectory.toFile(), homeDirectory.toFile());
        RepositoryInfo info = kars.get(karUri);
        for (URI repositoryUri : kar.getFeatureRepos()) {
            repositories.put(repositoryUri.toString(), info);
        }
    }
    //
    // Propagate feature installation from repositories
    //
    Map<String, Stage> features = new LinkedHashMap<>(this.features);
    Map<String, Features> karRepositories = loadRepositories(manager, repositories.keySet(), false);
    for (String repo : repositories.keySet()) {
        RepositoryInfo info = repositories.get(repo);
        if (info.addAll) {
            for (Feature feature : karRepositories.get(repo).getFeature()) {
                features.put(feature.getId(), info.stage);
            }
        }
    }
    //
    // Load profiles
    //
    LOGGER.info("Loading profiles");
    allProfiles = new HashMap<>();
    for (String profilesUri : profilesUris) {
        String uri = profilesUri;
        if (uri.startsWith("jar:") && uri.contains("!/")) {
            uri = uri.substring("jar:".length(), uri.indexOf("!/"));
        }
        if (!uri.startsWith("file:")) {
            downloader = manager.createDownloader();
            downloader.download(uri, null);
            downloader.await();
            StreamProvider provider = manager.getProviders().get(uri);
            profilesUri = profilesUri.replace(uri, provider.getFile().toURI().toString());
        }
        URI profileURI = URI.create(profilesUri);
        Path profilePath;
        try {
            profilePath = Paths.get(profileURI);
        } catch (FileSystemNotFoundException e) {
            // file system does not exist, try to create it
            FileSystem fs = FileSystems.newFileSystem(profileURI, new HashMap<>(), Builder.class.getClassLoader());
            profilePath = fs.provider().getPath(profileURI);
        }
        allProfiles.putAll(Profiles.loadProfiles(profilePath));
        // Handle blacklisted profiles
        if (!blacklistedProfiles.isEmpty()) {
            if (blacklistPolicy == BlacklistPolicy.Discard) {
                // Override blacklisted profiles with empty ones
                for (String profile : blacklistedProfiles) {
                    allProfiles.put(profile, ProfileBuilder.Factory.create(profile).getProfile());
                }
            } else {
                // Remove profiles completely
                allProfiles.keySet().removeAll(blacklistedProfiles);
            }
        }
    }
    // Generate profiles
    Profile startupProfile = generateProfile(Stage.Startup, profiles, repositories, features, bundles);
    profiles.put(startupProfile.getId(), Stage.Boot);
    Profile bootProfile = generateProfile(Stage.Boot, profiles, repositories, features, bundles);
    Profile installedProfile = generateProfile(Stage.Installed, profiles, repositories, features, bundles);
    //
    // Compute overall profile
    //
    ProfileBuilder builder = ProfileBuilder.Factory.create(UUID.randomUUID().toString()).setParents(Arrays.asList(startupProfile.getId(), bootProfile.getId(), installedProfile.getId()));
    config.forEach((k, v) -> builder.addConfiguration(Profile.INTERNAL_PID, Profile.CONFIG_PREFIX + k, v));
    system.forEach((k, v) -> builder.addConfiguration(Profile.INTERNAL_PID, Profile.SYSTEM_PREFIX + k, v));
    Profile overallProfile = builder.getProfile();
    Profile overallOverlay = Profiles.getOverlay(overallProfile, allProfiles, environment);
    Profile overallEffective = Profiles.getEffective(overallOverlay, false);
    manager = new CustomDownloadManager(resolver, executor, overallEffective, translatedUrls);
    //        Hashtable<String, String> agentProps = new Hashtable<>(overallEffective.getConfiguration(ORG_OPS4J_PAX_URL_MVN_PID));
    //        final Map<String, String> properties = new HashMap<>();
    //        properties.put("karaf.default.repository", "system");
    //        InterpolationHelper.performSubstitution(agentProps, properties::get, false, false, true);
    //
    // Write config and system properties
    //
    Path configPropertiesPath = etcDirectory.resolve("config.properties");
    Properties configProperties = new Properties(configPropertiesPath.toFile());
    configProperties.putAll(overallEffective.getConfig());
    configProperties.save();
    Path systemPropertiesPath = etcDirectory.resolve("system.properties");
    Properties systemProperties = new Properties(systemPropertiesPath.toFile());
    systemProperties.putAll(overallEffective.getSystem());
    systemProperties.save();
    //
    // Download libraries
    //
    // TODO: handle karaf 2.x and 3.x libraries
    LOGGER.info("Downloading libraries");
    downloader = manager.createDownloader();
    downloadLibraries(downloader, configProperties, overallEffective.getLibraries(), "");
    downloadLibraries(downloader, configProperties, libraries, "");
    downloader.await();
    // Reformat clauses
    reformatClauses(configProperties, Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA);
    reformatClauses(configProperties, Constants.FRAMEWORK_BOOTDELEGATION);
    configProperties.save();
    //
    // Write all configuration files
    //
    LOGGER.info("Writing configurations");
    for (Map.Entry<String, byte[]> config : overallEffective.getFileConfigurations().entrySet()) {
        Path configFile = etcDirectory.resolve(config.getKey());
        LOGGER.info("   adding config file: {}", homeDirectory.relativize(configFile));
        Files.createDirectories(configFile.getParent());
        Files.write(configFile, config.getValue());
    }
    // 'improve' configuration files.
    if (propertyEdits != null) {
        KarafPropertiesEditor editor = new KarafPropertiesEditor();
        editor.setInputEtc(etcDirectory.toFile()).setOutputEtc(etcDirectory.toFile()).setEdits(propertyEdits);
        editor.run();
    }
    //
    if (!overallEffective.getOverrides().isEmpty()) {
        Path overrides = etcDirectory.resolve("override.properties");
        List<String> lines = new ArrayList<>();
        lines.add("#");
        lines.add("# Generated by the karaf assembly builder");
        lines.add("#");
        lines.addAll(overallEffective.getOverrides());
        LOGGER.info("Generating {}", homeDirectory.relativize(overrides));
        Files.write(overrides, lines, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    }
    //
    if (!blacklistedFeatures.isEmpty() || !blacklistedBundles.isEmpty()) {
        Path blacklist = etcDirectory.resolve("blacklisted.properties");
        List<String> lines = new ArrayList<>();
        lines.add("#");
        lines.add("# Generated by the karaf assembly builder");
        lines.add("#");
        if (!blacklistedFeatures.isEmpty()) {
            lines.add("");
            lines.add("# Features");
            lines.addAll(blacklistedFeatures);
        }
        if (!blacklistedBundles.isEmpty()) {
            lines.add("");
            lines.add("# Bundles");
            lines.addAll(blacklistedBundles);
        }
        LOGGER.info("Generating {}", homeDirectory.relativize(blacklist));
        Files.write(blacklist, lines, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    }
    //
    // Startup stage
    //
    Profile startupEffective = startupStage(startupProfile);
    //
    // Boot stage
    //
    Set<Feature> allBootFeatures = bootStage(bootProfile, startupEffective);
    //
    // Installed stage
    //
    installStage(installedProfile, allBootFeatures);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) Downloader(org.apache.karaf.features.internal.download.Downloader) ResolverImpl(org.apache.felix.resolver.ResolverImpl) Properties(org.apache.felix.utils.properties.Properties) URI(java.net.URI) Feature(org.apache.karaf.features.internal.model.Feature) ProfileBuilder(org.apache.karaf.profile.ProfileBuilder) Profile(org.apache.karaf.profile.Profile) LinkedHashMap(java.util.LinkedHashMap) MavenResolver(org.ops4j.pax.url.mvn.MavenResolver) Features(org.apache.karaf.features.internal.model.Features) StreamProvider(org.apache.karaf.features.internal.download.StreamProvider) Hashtable(java.util.Hashtable) KarafPropertiesEditor(org.apache.karaf.tools.utils.KarafPropertiesEditor) Kar(org.apache.karaf.kar.internal.Kar) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 2 with Profile

use of org.apache.karaf.profile.Profile in project karaf by apache.

the class Builder method startupStage.

private Profile startupStage(Profile startupProfile) throws Exception {
    LOGGER.info("Startup stage");
    //
    // Compute startup
    //
    Profile startupOverlay = Profiles.getOverlay(startupProfile, allProfiles, environment);
    Profile startupEffective = Profiles.getEffective(startupOverlay, false);
    // Load startup repositories
    LOGGER.info("   Loading repositories");
    Map<String, Features> startupRepositories = loadRepositories(manager, startupEffective.getRepositories(), false);
    //
    // Resolve
    //
    LOGGER.info("   Resolving features");
    Map<String, Integer> bundles = resolve(manager, resolver, startupRepositories.values(), startupEffective.getFeatures(), startupEffective.getBundles(), startupEffective.getOverrides(), startupEffective.getOptionals());
    //
    // Generate startup.properties
    //
    Properties startup = new Properties();
    startup.setHeader(Collections.singletonList("# Bundles to be started on startup, with startlevel"));
    Map<Integer, Set<String>> invertedStartupBundles = MapUtils.invert(bundles);
    for (Map.Entry<Integer, Set<String>> entry : new TreeMap<>(invertedStartupBundles).entrySet()) {
        String startLevel = Integer.toString(entry.getKey());
        for (String location : new TreeSet<>(entry.getValue())) {
            if (useReferenceUrls) {
                if (location.startsWith("mvn:")) {
                    location = "file:" + Parser.pathFromMaven(location);
                }
                if (location.startsWith("file:")) {
                    location = "reference:" + location;
                }
            }
            if (location.startsWith("file:") && karafVersion == KarafVersion.v24) {
                location = location.substring("file:".length());
            }
            startup.put(location, startLevel);
        }
    }
    Path startupProperties = etcDirectory.resolve("startup.properties");
    startup.save(startupProperties.toFile());
    return startupEffective;
}
Also used : EnumSet(java.util.EnumSet) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Properties(org.apache.felix.utils.properties.Properties) Profile(org.apache.karaf.profile.Profile) TreeSet(java.util.TreeSet) Features(org.apache.karaf.features.internal.model.Features) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 3 with Profile

use of org.apache.karaf.profile.Profile in project karaf by apache.

the class ProfileCopy method execute.

@Override
public Object execute() throws Exception {
    Profile profile = ProfileBuilder.Factory.createFrom(profileService.getProfile(source)).identity(target).getProfile();
    profileService.createProfile(profile);
    return null;
}
Also used : Profile(org.apache.karaf.profile.Profile)

Example 4 with Profile

use of org.apache.karaf.profile.Profile in project karaf by apache.

the class ProfileCreate method execute.

@Override
public Object execute() throws Exception {
    Profile profile = ProfileBuilder.Factory.create(profileId).setParents(parents).getProfile();
    profileService.createProfile(profile);
    return null;
}
Also used : Profile(org.apache.karaf.profile.Profile)

Example 5 with Profile

use of org.apache.karaf.profile.Profile in project karaf by apache.

the class ProfileEdit method execute.

@Override
public Object execute() throws Exception {
    if (delete) {
        set = false;
    }
    Profile profile = profileService.getRequiredProfile(profileName);
    editProfile(profile);
    return null;
}
Also used : Profile(org.apache.karaf.profile.Profile)

Aggregations

Profile (org.apache.karaf.profile.Profile)20 HashMap (java.util.HashMap)5 ArrayList (java.util.ArrayList)4 Features (org.apache.karaf.features.internal.model.Features)4 Test (org.junit.Test)4 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 Properties (org.apache.felix.utils.properties.Properties)3 Downloader (org.apache.karaf.features.internal.download.Downloader)3 Feature (org.apache.karaf.features.internal.model.Feature)3 LockHandle (org.apache.karaf.profile.LockHandle)3 IOException (java.io.IOException)2 Bundle (org.apache.karaf.features.internal.model.Bundle)2 Conditional (org.apache.karaf.features.internal.model.Conditional)2 ConfigFile (org.apache.karaf.features.internal.model.ConfigFile)2 ProfileBuilder (org.apache.karaf.profile.ProfileBuilder)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1