Search in sources :

Example 76 with Entry

use of io.fabric8.maven.docker.config.CopyConfiguration.Entry in project fabric8 by jboss-fuse.

the class ProfileMetadata method getPidMetaTypeObject.

@Override
public MetaTypeObjectDTO getPidMetaTypeObject(String versionId, String profileId, final String pid) throws Exception {
    final AtomicReference<MetaTypeObjectDTO> answer = new AtomicReference<>(null);
    MetadataHandler handler = new MetadataHandler() {

        @Override
        public void invoke(MetaData metadata, Properties resources) {
            Map<String, Object> map = metadata.getDesignates();
            Map<String, Object> objects = metadata.getObjectClassDefinitions();
            Set<Map.Entry<String, Object>> entries = map.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                String aPid = entry.getKey();
                Object value = objects.get(aPid);
                if (Objects.equal(pid, aPid) && value instanceof OCD) {
                    OCD ocd = (OCD) value;
                    answer.set(createMetaTypeObjectDTO(resources, ocd));
                }
            }
        }
    };
    findMetadataForProfile(versionId, profileId, handler);
    return answer.get();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Properties(java.util.Properties) OCD(org.apache.felix.metatype.OCD) JarEntry(java.util.jar.JarEntry) MetaData(org.apache.felix.metatype.MetaData) Map(java.util.Map) HashMap(java.util.HashMap) MetaTypeObjectDTO(io.fabric8.api.jmx.MetaTypeObjectDTO)

Example 77 with Entry

use of io.fabric8.maven.docker.config.CopyConfiguration.Entry in project fabric8 by jboss-fuse.

the class ProfileWatcherImpl method run.

public void run() {
    assertValid();
    LOG.debug("Profile watcher thread started");
    int oldCounter = -1;
    SortedSet<String> oldActiveProfiles = null;
    Map<File, Long> localChecksums = new HashMap<File, Long>();
    Map<File, Long> localModified = new HashMap<File, Long>();
    Set<Profile> refreshProfiles = new HashSet<Profile>();
    ProfileService profileService = fabricService.get().adapt(ProfileService.class);
    while (running.get() && watchURLs.size() > 0) {
        SortedSet<String> currentActiveProfiles = getCurrentActiveProfileVersions();
        if (profileArtifacts == null || oldCounter != counter.get() || oldActiveProfiles == null || !oldActiveProfiles.equals(currentActiveProfiles)) {
            oldCounter = counter.get();
            oldActiveProfiles = currentActiveProfiles;
            try {
                LOG.debug("Reloading the currently active profile artifacts");
                profileArtifacts = findProfileArifacts();
            } catch (Exception e) {
                LOG.error("Failed to get profiles artifacts: " + e, e);
            }
        }
        // lets refresh profiles on the next loop; so we've time to finish uploading/modifying files
        for (Profile profile : refreshProfiles) {
            LOG.info("Refreshing profile: " + profile);
            Profiles.refreshProfile(fabricService.get(), profile);
        }
        refreshProfiles.clear();
        if (profileArtifacts != null) {
            List<File> localRepositories = new LinkedList<>();
            if (mavenResolver.get().getLocalRepository() != null) {
                localRepositories.add(mavenResolver.get().getLocalRepository());
            }
            if (mavenResolver.get().getDefaultRepositories() != null) {
                for (LocalRepository repository : mavenResolver.get().getDefaultRepositories()) {
                    localRepositories.add(repository.getBasedir());
                }
            }
            Set<Map.Entry<ProfileVersionKey, Map<String, Parser>>> entries = profileArtifacts.entrySet();
            for (Map.Entry<ProfileVersionKey, Map<String, Parser>> entry : entries) {
                ProfileVersionKey key = entry.getKey();
                Map<String, Parser> artifactMap = entry.getValue();
                // lets find a container for the profile
                Profile profile = key.getProfile();
                Properties checksums = findProfileChecksums(fabricService.get(), profile);
                if (checksums != null) {
                    Set<Map.Entry<String, Parser>> artifactMapEntries = artifactMap.entrySet();
                    for (Map.Entry<String, Parser> artifactMapEntry : artifactMapEntries) {
                        String location = artifactMapEntry.getKey();
                        Parser parser = artifactMapEntry.getValue();
                        if (isSnapshot(parser) || wildCardMatch(location)) {
                            Object value = checksums.get(location);
                            if (value == null) {
                                value = checksums.get(JavaContainers.removeUriPrefixBeforeMaven(location));
                            }
                            Long checksum = null;
                            if (value instanceof Number) {
                                checksum = ((Number) value).longValue();
                            } else if (value instanceof String) {
                                checksum = Long.parseLong((String) value);
                            }
                            if (checksum == null) {
                                if (missingChecksums.add(location)) {
                                    LOG.warn("Could not find checksum for location " + location);
                                }
                            } else {
                                File file = null;
                                for (File localRepository : localRepositories) {
                                    File _file = new File(localRepository.getPath() + File.separator + parser.getArtifactPath());
                                    if (_file.isFile()) {
                                        file = _file;
                                        break;
                                    }
                                }
                                if (!file.exists()) {
                                    LOG.info("Ignoring file " + file.getPath() + " as it does not exist");
                                } else {
                                    // lets use a cache of last modified times to avoid having to continuously
                                    // recalculate the checksum on each file
                                    Long oldModfied = localModified.get(file);
                                    long modified = file.lastModified();
                                    if (oldModfied == null || modified != oldModfied) {
                                        localModified.put(file, modified);
                                        Long fileChecksum = getFileChecksum(file);
                                        if (fileChecksum != null && !fileChecksum.equals(checksum)) {
                                            // lets keep track of local checksums in case we've already started the upload process
                                            // and it takes the profile a little while to respond to uploaded jars and to
                                            // refreshed profiles
                                            Long localChecksum = localChecksums.get(file);
                                            if (localChecksum == null || !localChecksum.equals(fileChecksum)) {
                                                localChecksums.put(file, fileChecksum);
                                                LOG.info("Checksums don't match for " + location + ", container: " + checksum + " and local file: " + fileChecksum);
                                                LOG.info("Updated version of " + location + " detected in " + file);
                                                if (isUpload()) {
                                                    uploadFile(location, parser, file);
                                                }
                                                refreshProfiles.add(profile);
                                            }
                                        }
                                    }
                                }
                            }
                        } else {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Ignoring " + location);
                            }
                        }
                    }
                }
            }
        }
        try {
            Thread.sleep(interval);
        } catch (InterruptedException ex) {
            running.set(false);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Profile watcher thread stopped");
    }
}
Also used : HashMap(java.util.HashMap) Properties(java.util.Properties) Profile(io.fabric8.api.Profile) HashSet(java.util.HashSet) LocalRepository(org.eclipse.aether.repository.LocalRepository) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) Parser(io.fabric8.maven.util.Parser) ProfileService(io.fabric8.api.ProfileService) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap)

Example 78 with Entry

use of io.fabric8.maven.docker.config.CopyConfiguration.Entry in project fabric8 by jboss-fuse.

the class DeploymentAgent method updateStatus.

private void updateStatus(FabricService fs, String status, Throwable result, boolean force) /*=false*/
{
    if (!force && System.currentTimeMillis() < lastStatusUpdate + UPDATE_INTERVAL) {
        return;
    }
    lastStatusUpdate = System.currentTimeMillis();
    try {
        provisioningStatus = status;
        provisioningError = result;
        if (fs != null) {
            fabricNotAvailableLogged = false;
            Container container = fs.getCurrentContainer();
            String e;
            if (result == null) {
                e = null;
            } else {
                StringWriter sw = new StringWriter();
                result.printStackTrace(new PrintWriter(sw));
                e = sw.toString();
            }
            if (provisionList != null) {
                Set<String> uris = new TreeSet<>();
                for (Resource res : provisionList) {
                    uris.add(getUri(res));
                }
                container.setProvisionList(new ArrayList<>(uris));
            }
            container.setProvisionResult(status);
            container.setProvisionException(e);
            java.util.Properties provisionChecksums = new java.util.Properties();
            for (Map.Entry<Long, Long> entry : state.bundleChecksums.entrySet()) {
                Bundle bundle = systemBundleContext.getBundle(entry.getKey());
                String location = bundle.getLocation();
                provisionChecksums.put(location, entry.getValue().toString());
            }
            /*
                putAllProperties(provisionChecksums, libChecksums);
                putAllProperties(provisionChecksums, endorsedChecksums);
                putAllProperties(provisionChecksums, extensionChecksums);
*/
            container.setProvisionChecksums(provisionChecksums);
        } else {
            if (!fabricNotAvailableLogged) {
                fabricNotAvailableLogged = true;
                LOGGER.info("Unable to set provisioning status as FabricService is not available");
            }
        }
    } catch (Throwable e) {
        LOGGER.warn("Unable to set provisioning result");
    }
}
Also used : Bundle(org.osgi.framework.Bundle) Resource(org.osgi.resource.Resource) Properties(org.apache.felix.utils.properties.Properties) Container(io.fabric8.api.Container) StringWriter(java.io.StringWriter) TreeSet(java.util.TreeSet) Map(java.util.Map) HashMap(java.util.HashMap) PrintWriter(java.io.PrintWriter)

Example 79 with Entry

use of io.fabric8.maven.docker.config.CopyConfiguration.Entry in project fabric8 by jboss-fuse.

the class DeploymentAgent method handleRestartJvmFlag.

/**
 * Adds support for a directive to force a restart upon the first assignment of a specific profile to a container.
 * It creates an entry in zk so that a subsequent modification to the same profile, will not trigger a jvm restart.
 * The behavior is useful for situation when a profile provision .jars in lib/ folder, that are picked up only at
 * jvm boot time.
 *
 * @param profile
 * @param restart
 * @return
 */
protected boolean handleRestartJvmFlag(Profile profile, AtomicBoolean restart) {
    boolean result = false;
    List<String> profilesRequiringRestart = new ArrayList<>();
    ServiceReference<CuratorFramework> curatorServiceReference = systemBundleContext.getServiceReference(CuratorFramework.class);
    ServiceReference<FabricService> fabricServiceReference = systemBundleContext.getServiceReference(FabricService.class);
    if (curatorServiceReference != null && fabricServiceReference != null) {
        CuratorFramework curator = systemBundleContext.getService(curatorServiceReference);
        FabricService fs = systemBundleContext.getService(fabricServiceReference);
        String currentContainerName = fs.getCurrentContainerName();
        List<String> activeProfiles = fs.getCurrentContainer().getProfileIds();
        // check for jvm restart requests
        Map<String, String> agentProperties = profile.getConfiguration("io.fabric8.agent");
        Map<String, String> jvmRestartEntries = new HashMap<>();
        for (String key : agentProperties.keySet()) {
            if (key.startsWith("io.fabric8.agent.forceOneTimeJVMRestart")) {
                jvmRestartEntries.put(key, agentProperties.get(key));
                LOGGER.info("Found a profile carrying a one-time JVM restart request: {}", key);
            }
        }
        // clean old entries
        String basePath = ZkPath.CONTAINER_PROVISION_RESTART.getPath(currentContainerName);
        try {
            if (ZooKeeperUtils.exists(curator, basePath) != null) {
                List<String> zkPaths = ZooKeeperUtils.getAllChildren(curator, ZkPath.CONTAINER_PROVISION_RESTART.getPath(currentContainerName));
                for (String zkPath : zkPaths) {
                    String[] split = zkPath.split("/");
                    String prof = split[split.length - 1];
                    if (!activeProfiles.contains(prof)) {
                        LOGGER.info("Deleting old JVM restart request status: {}", zkPath);
                        ZooKeeperUtils.delete(curator, zkPath);
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.error("Unable to check ZK connection", e);
        }
        for (String key : jvmRestartEntries.keySet()) {
            String[] split = key.split("\\.");
            String profileForcingRestart = split[split.length - 1];
            try {
                String zkPath = ZkPath.CONTAINER_PROVISION_RESTART_PROFILES.getPath(currentContainerName, profileForcingRestart);
                Stat exists = exists(curator, zkPath);
                if (exists == null) {
                    ZooKeeperUtils.create(curator, zkPath);
                    profilesRequiringRestart.add(profileForcingRestart);
                    result = true;
                }
            } catch (Exception e) {
                LOGGER.error("Unable to check ZK connection", e);
            }
        }
    }
    if (result) {
        System.setProperty("karaf.restart.jvm", "true");
        restart.set(true);
        LOGGER.warn("Profiles {} scheduled a JVM restart request. Automated JVM restart support is not universally available. If your jvm doesn't support it you are required to manually restart the container that has just been assigned the profile.", profilesRequiringRestart);
        try {
            bundleContext.getBundle(0).stop();
        } catch (BundleException e) {
            LOGGER.error("Error when forcing a JVM restart", e);
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConfigurationException(org.osgi.service.cm.ConfigurationException) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) CuratorFramework(org.apache.curator.framework.CuratorFramework) Stat(org.apache.zookeeper.data.Stat) FabricService(io.fabric8.api.FabricService) BundleException(org.osgi.framework.BundleException)

Example 80 with Entry

use of io.fabric8.maven.docker.config.CopyConfiguration.Entry in project fabric8 by jboss-fuse.

the class ProfileDownloader method downloadProfile.

/**
 * Downloads the bundles, features and FABs for this profile.
 */
public void downloadProfile(Profile profile) throws Exception {
    if (listener != null) {
        listener.beforeDownloadProfile(profile);
    }
    ProfileService profileService = fabricService.adapt(ProfileService.class);
    if (!profile.isOverlay()) {
        profile = profileService.getOverlayProfile(profile);
    }
    DownloadManager downloadManager = DownloadManagers.createDownloadManager(fabricService, executorService);
    Set<String> bundles = new LinkedHashSet<String>();
    Set<Feature> features = new LinkedHashSet<Feature>();
    addMavenBundles(fabricService, profile, bundles, profile.getBundles());
    addMavenBundles(fabricService, profile, bundles, profile.getFabs());
    AgentUtils.addFeatures(features, fabricService, downloadManager, profile);
    Map<String, File> files = AgentUtils.downloadBundles(downloadManager, features, bundles, Collections.<String>emptySet(), !isDownloadFilesFromProfile());
    Set<Map.Entry<String, File>> entries = files.entrySet();
    for (Map.Entry<String, File> entry : entries) {
        String name = entry.getKey();
        File file = entry.getValue();
        if (processedFiles.add(file)) {
            String fileName = file.getName();
            String mvnCoords = getMavenCoords(name);
            File destFile;
            if (mvnCoords != null) {
                Parser parser = new Parser(mvnCoords);
                destFile = new File(target, parser.getArtifactPath());
            } else {
                destFile = new File(target, fileName);
            }
            if (force || !destFile.exists()) {
                LOG.info("Copying file: " + file + " to: " + destFile.getCanonicalPath());
                Files.copy(file, destFile);
                if (listener != null) {
                    listener.onCopyDone(profile, destFile);
                }
            }
        }
    }
    if (listener != null) {
        listener.afterDownloadProfile(profile);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Feature(io.fabric8.agent.model.Feature) Parser(io.fabric8.maven.util.Parser) ProfileService(io.fabric8.api.ProfileService) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Map (java.util.Map)89 HashMap (java.util.HashMap)57 IOException (java.io.IOException)31 File (java.io.File)30 ArrayList (java.util.ArrayList)26 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)16 List (java.util.List)14 Properties (java.util.Properties)14 HashSet (java.util.HashSet)10 Entry (io.fabric8.maven.docker.config.CopyConfiguration.Entry)9 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)9 Test (org.junit.Test)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 LinkedHashMap (java.util.LinkedHashMap)8 TreeMap (java.util.TreeMap)8 Resource (io.fabric8.kubernetes.client.dsl.Resource)7 FileInputStream (java.io.FileInputStream)7 Set (java.util.Set)7 Profile (io.fabric8.api.Profile)6 ProfileService (io.fabric8.api.ProfileService)5