Search in sources :

Example 91 with Entry

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

the class GitPatchManagementServiceImpl method fetchPatches.

@Override
public List<PatchData> fetchPatches(URL url) throws PatchException {
    try {
        List<PatchData> patches = new ArrayList<>(1);
        File patchFile = new File(patchesDir, Long.toString(System.currentTimeMillis()) + ".patch.tmp");
        InputStream input = url.openStream();
        FileOutputStream output = new FileOutputStream(patchFile);
        ZipFile zf = null;
        try {
            IOUtils.copy(input, output);
        } finally {
            IOUtils.closeQuietly(input);
            IOUtils.closeQuietly(output);
        }
        try {
            zf = new ZipFile(patchFile);
        } catch (IOException ignored) {
            if (!FilenameUtils.getExtension(url.getFile()).equals("patch")) {
                throw new PatchException("Patch should be ZIP file or *.patch descriptor");
            }
        }
        // patchFile may "be" a patch descriptor or be a ZIP file containing descriptor
        PatchData patchData = null;
        // in case patch ZIP file has no descriptor, we'll "generate" patch data on the fly
        // no descriptor -> assume we have rollup patch or even full, new distribution
        PatchData fallbackPatchData = new PatchData(FilenameUtils.getBaseName(url.getPath()));
        fallbackPatchData.setGenerated(true);
        fallbackPatchData.setRollupPatch(true);
        fallbackPatchData.setPatchDirectory(new File(patchesDir, fallbackPatchData.getId()));
        fallbackPatchData.setPatchLocation(patchesDir);
        if (zf != null) {
            File systemRepo = getSystemRepository(karafHome, systemContext);
            try {
                List<ZipArchiveEntry> otherResources = new LinkedList<>();
                boolean skipRootDir = false;
                for (Enumeration<ZipArchiveEntry> e = zf.getEntries(); e.hasMoreElements(); ) {
                    ZipArchiveEntry entry = e.nextElement();
                    if (!skipRootDir && entry.isDirectory() && (entry.getName().startsWith("jboss-fuse-") || entry.getName().startsWith("jboss-a-mq-"))) {
                        skipRootDir = true;
                    }
                    if (entry.isDirectory() || entry.isUnixSymlink()) {
                        continue;
                    }
                    String name = entry.getName();
                    if (skipRootDir) {
                        name = name.substring(name.indexOf('/') + 1);
                    }
                    if (!name.contains("/") && name.endsWith(".patch")) {
                        // patch descriptor in ZIP's root directory
                        if (patchData == null) {
                            // load data from patch descriptor inside ZIP. This may or may not be a rollup
                            // patch
                            File target = new File(patchesDir, name);
                            extractZipEntry(zf, entry, target);
                            patchData = loadPatchData(target);
                            // ENTESB-4600: try checking the target version of the patch
                            Version version = Utils.findVersionInName(patchData.getId());
                            if (version.getMajor() == 6 && version.getMinor() == 1) {
                                throw new PatchException("Can't install patch \"" + patchData.getId() + "\", it is released for version 6.1 of the product");
                            }
                            patchData.setGenerated(false);
                            File targetDirForPatchResources = new File(patchesDir, patchData.getId());
                            patchData.setPatchDirectory(targetDirForPatchResources);
                            patchData.setPatchLocation(patchesDir);
                            target.renameTo(new File(patchesDir, patchData.getId() + ".patch"));
                            patches.add(patchData);
                        } else {
                            throw new PatchException(String.format("Multiple patch descriptors: already have patch %s and now encountered entry %s", patchData.getId(), name));
                        }
                    } else {
                        File target = null;
                        String relativeName = null;
                        if (name.startsWith("system/")) {
                            // copy to ${karaf.default.repository}
                            relativeName = name.substring("system/".length());
                            target = new File(systemRepo, relativeName);
                        } else if (name.startsWith("repository/")) {
                            // copy to ${karaf.default.repository}
                            relativeName = name.substring("repository/".length());
                            target = new File(systemRepo, relativeName);
                        } else {
                            // other files that should be applied to ${karaf.home} when the patch is installed
                            otherResources.add(entry);
                        }
                        if (target != null) {
                            // we unzip to system repository
                            extractAndTrackZipEntry(fallbackPatchData, zf, entry, target, skipRootDir);
                        }
                    }
                }
                File targetDirForPatchResources = new File(patchesDir, patchData == null ? fallbackPatchData.getId() : patchData.getId());
                // now copy non-maven resources (we should now know where to copy them)
                for (ZipArchiveEntry entry : otherResources) {
                    String name = entry.getName();
                    if (skipRootDir) {
                        name = name.substring(name.indexOf('/'));
                    }
                    File target = new File(targetDirForPatchResources, name);
                    extractAndTrackZipEntry(fallbackPatchData, zf, entry, target, skipRootDir);
                }
            } finally {
                if (zf != null) {
                    zf.close();
                }
                if (patchFile != null) {
                    patchFile.delete();
                }
            }
        } else {
            // If the file is not a zip/jar, assume it's a single patch file
            patchData = loadPatchData(patchFile);
            // no patch directory - no attached content, assuming only references to bundles
            patchData.setPatchDirectory(null);
            patchFile.renameTo(new File(patchesDir, patchData.getId() + ".patch"));
            patches.add(patchData);
        }
        if (patches.size() == 0) {
            // let's use generated patch descriptor
            File generatedPatchDescriptor = new File(patchesDir, fallbackPatchData.getId() + ".patch");
            FileOutputStream out = new FileOutputStream(generatedPatchDescriptor);
            try {
                fallbackPatchData.storeTo(out);
            } finally {
                IOUtils.closeQuietly(out);
            }
            patches.add(fallbackPatchData);
        }
        return patches;
    } catch (IOException e) {
        throw new PatchException("Unable to download patch from url " + url, e);
    }
}
Also used : PatchData(io.fabric8.patch.management.PatchData) ByteArrayInputStream(java.io.ByteArrayInputStream) JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) Version(org.osgi.framework.Version) Artifact.isSameButVersion(io.fabric8.patch.management.Artifact.isSameButVersion) EOLFixingFileOutputStream(io.fabric8.patch.management.io.EOLFixingFileOutputStream) FileOutputStream(java.io.FileOutputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) PatchException(io.fabric8.patch.management.PatchException) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Example 92 with Entry

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

the class Utils method extractZipEntry.

/**
 * Exctracts ZIP entry into target file. Sets correct file permissions if found in ZIP entry.
 * @param zip
 * @param entry
 * @param target
 * @throws IOException
 */
public static void extractZipEntry(ZipFile zip, ZipArchiveEntry entry, File target) throws IOException {
    target.getParentFile().mkdirs();
    FileOutputStream targetOutputStream = new FileOutputStream(target);
    IOUtils.copyLarge(zip.getInputStream(entry), targetOutputStream);
    IOUtils.closeQuietly(targetOutputStream);
    if (Files.getFileAttributeView(target.toPath(), PosixFileAttributeView.class) != null) {
        Files.setPosixFilePermissions(target.toPath(), getPermissionsFromUnixMode(target, entry.getUnixMode()));
    }
}
Also used : EOLFixingFileOutputStream(io.fabric8.patch.management.io.EOLFixingFileOutputStream) FileOutputStream(java.io.FileOutputStream) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 93 with Entry

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

the class FabricServiceImpl method substituteConfigurations.

/**
 * Performs substitution to configuration based on the registered {@link PlaceholderResolver} instances.
 */
public Map<String, Map<String, String>> substituteConfigurations(final Map<String, Map<String, String>> configurations) {
    final Map<String, PlaceholderResolver> resolversSnapshot = new HashMap<String, PlaceholderResolver>(placeholderResolvers);
    // Check that all resolvers are available
    Set<String> requiredSchemes = getSchemesForProfileConfigurations(configurations);
    Set<String> availableSchemes = resolversSnapshot.keySet();
    if (!availableSchemes.containsAll(requiredSchemes)) {
        StringBuilder sb = new StringBuilder();
        sb.append("Missing Placeholder Resolvers:");
        for (String scheme : requiredSchemes) {
            if (!availableSchemes.contains(scheme)) {
                sb.append(" ").append(scheme);
            }
        }
        throw new FabricException(sb.toString());
    }
    final Map<String, Map<String, String>> mutableConfigurations = new HashMap<>();
    for (Entry<String, Map<String, String>> entry : configurations.entrySet()) {
        String key = entry.getKey();
        Map<String, String> value = new HashMap<>(entry.getValue());
        mutableConfigurations.put(key, value);
    }
    final FabricService fabricService = this;
    for (Map.Entry<String, Map<String, String>> entry : mutableConfigurations.entrySet()) {
        final String pid = entry.getKey();
        Map<String, String> props = entry.getValue();
        Map<String, String> original = new HashMap<>(props);
        for (Map.Entry<String, String> e : original.entrySet()) {
            final String key = e.getKey();
            final String value = e.getValue();
            try {
                props.put(key, InterpolationHelper.substVars(value, key, null, props, new InterpolationHelper.SubstitutionCallback() {

                    public String getValue(String toSubstitute) {
                        if (toSubstitute != null && toSubstitute.contains(":")) {
                            String scheme = toSubstitute.substring(0, toSubstitute.indexOf(":"));
                            return resolversSnapshot.get(scheme).resolve(fabricService, mutableConfigurations, pid, key, toSubstitute);
                        }
                        return substituteBundleProperty(toSubstitute, bundleContext);
                    }
                }));
            } catch (EncryptionOperationNotPossibleException exception) {
                LOGGER.warn("Error resolving " + key, exception);
            }
        }
    }
    return mutableConfigurations;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) FabricException(io.fabric8.api.FabricException) EncryptionOperationNotPossibleException(org.jasypt.exceptions.EncryptionOperationNotPossibleException) FabricService(io.fabric8.api.FabricService) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) PlaceholderResolver(io.fabric8.api.PlaceholderResolver)

Example 94 with Entry

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

the class MQManager method addMasterSlaveStatus.

protected void addMasterSlaveStatus(List<MQBrokerStatusDTO> answer) throws Exception {
    Map<String, Map<String, MQBrokerStatusDTO>> groupMap = new HashMap<String, Map<String, MQBrokerStatusDTO>>();
    for (MQBrokerStatusDTO status : answer) {
        String key = status.getGroup();
        Map<String, MQBrokerStatusDTO> list = groupMap.get(key);
        if (list == null) {
            list = new HashMap<String, MQBrokerStatusDTO>();
            groupMap.put(key, list);
        }
        String statusPath = String.format("%s/%s", status.getContainer(), status.getBrokerName());
        list.put(statusPath, status);
    }
    CuratorFramework curator = getCurator();
    // now lets check the cluster status for each group
    Set<Map.Entry<String, Map<String, MQBrokerStatusDTO>>> entries = groupMap.entrySet();
    for (Map.Entry<String, Map<String, MQBrokerStatusDTO>> entry : entries) {
        String group = entry.getKey();
        Map<String, MQBrokerStatusDTO> containerMap = entry.getValue();
        String groupPath = ZkPath.MQ_CLUSTER.getPath(group);
        List<String> children = getChildrenSafe(curator, groupPath);
        for (String child : children) {
            String childPath = groupPath + "/" + child;
            byte[] data = curator.getData().forPath(childPath);
            if (data != null && data.length > 0) {
                String text = new String(data).trim();
                if (!text.isEmpty()) {
                    ObjectMapper mapper = new ObjectMapper();
                    Map<String, Object> map = mapper.readValue(data, HashMap.class);
                    String id = stringValue(map, "id", "container");
                    if (id != null) {
                        String container = stringValue(map, "container", "agent");
                        String statusPath = String.format("%s/%s", container, id);
                        MQBrokerStatusDTO containerStatus = containerMap.get(statusPath);
                        if (containerStatus != null) {
                            Boolean master = null;
                            List services = listValue(map, "services");
                            if (services != null) {
                                if (!services.isEmpty()) {
                                    List<String> serviceTexts = new ArrayList<String>();
                                    for (Object service : services) {
                                        String serviceText = getSubstitutedData(curator, service.toString());
                                        if (Strings.isNotBlank(serviceText)) {
                                            serviceTexts.add(serviceText);
                                        }
                                        containerStatus.setServices(serviceTexts);
                                    }
                                    master = Boolean.TRUE;
                                } else {
                                    master = Boolean.FALSE;
                                }
                            } else {
                                master = Boolean.FALSE;
                            }
                            containerStatus.setMaster(master);
                        }
                    }
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MQBrokerStatusDTO(io.fabric8.api.jmx.MQBrokerStatusDTO) CuratorFramework(org.apache.curator.framework.CuratorFramework) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 95 with Entry

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

the class MQManager method createOrUpdateProfile.

/**
 * Creates or updates the broker profile for the given DTO and updates the requirements so that the
 * minimum number of instances of the profile is updated
 */
public static Profile createOrUpdateProfile(MQBrokerConfigDTO dto, FabricService fabricService, RuntimeProperties runtimeProperties) throws IOException {
    FabricRequirements requirements = fabricService.getRequirements();
    MQService mqService = createMQService(fabricService, runtimeProperties);
    Map<String, String> configuration = new HashMap<String, String>();
    List<String> properties = dto.getProperties();
    String version = dto.version();
    boolean changeInCurrentVersion = requirements.getVersion().equals(dto.getVersion());
    if (properties != null) {
        for (String entry : properties) {
            String[] parts = entry.split("=", 2);
            if (parts.length == 2) {
                configuration.put(parts[0], parts[1]);
            } else {
                configuration.put(parts[0], "");
            }
        }
    }
    String data = dto.getData();
    String profileName = dto.profile();
    try {
        FabricValidations.validateProfileName(profileName);
    } catch (IllegalArgumentException e) {
        // we do not want exception in the server log, so print the error message to the console
        System.out.println(e.getMessage());
        return null;
    }
    String brokerName = dto.getBrokerName();
    if (data == null) {
        // lets use a relative path so we work on any karaf container
        data = "${runtime.data}" + brokerName;
    }
    configuration.put(DATA, data);
    for (Map.Entry<String, String> port : dto.getPorts().entrySet()) {
        configuration.put(port.getKey() + "-port", port.getValue());
    }
    BrokerKind kind = dto.kind();
    configuration.put(KIND, kind.toString());
    String config = dto.getConfigUrl();
    if (config != null) {
        configuration.put(CONFIG_URL, mqService.getConfig(version, config));
    }
    String group = dto.getGroup();
    if (group != null) {
        configuration.put(GROUP, group);
    }
    Maps.setStringValues(configuration, NETWORKS, dto.getNetworks());
    String networksUserName = dto.getNetworksUserName();
    if (networksUserName != null) {
        configuration.put(NETWORK_USER_NAME, networksUserName);
    }
    String networksPassword = dto.getNetworksPassword();
    if (networksPassword != null) {
        configuration.put(NETWORK_PASSWORD, networksPassword);
    }
    String parentProfile = dto.getParentProfile();
    if (parentProfile != null) {
        configuration.put(PARENT, parentProfile);
    }
    Boolean ssl = dto.getSsl();
    if (ssl != null) {
        configuration.put(SSL, ssl.toString());
    }
    Integer replicas = dto.getReplicas();
    if (replicas != null) {
        configuration.put(REPLICAS, replicas.toString());
    }
    Integer minInstances = dto.getMinimumInstances();
    if (minInstances != null) {
        configuration.put(MINIMUM_INSTANCES, minInstances.toString());
    }
    Profile profile = mqService.createOrUpdateMQProfile(version, profileName, brokerName, configuration, dto.kind().equals(BrokerKind.Replicated));
    String profileId = profile.getId();
    ProfileRequirements profileRequirement = requirements.getOrCreateProfileRequirement(profileId);
    Integer minimumInstances = profileRequirement.getMinimumInstances();
    // lets reload the DTO as we may have inherited some values from the parent profile
    List<MQBrokerConfigDTO> list = createConfigDTOs(mqService, profile);
    // lets assume 2 required instances for master/slave unless folks use
    // N+1 or replicated
    int requiredInstances = 2;
    if (list.size() == 1) {
        MQBrokerConfigDTO loadedDTO = list.get(0);
        requiredInstances = loadedDTO.requiredInstances();
    } else {
        // assume N+1 broker as there's more than one broker in the profile; so lets set the required size to N+1
        requiredInstances = list.size() + 1;
    }
    if (changeInCurrentVersion && (minimumInstances == null || minimumInstances.intValue() < requiredInstances)) {
        profileRequirement.setMinimumInstances(requiredInstances);
        fabricService.setRequirements(requirements);
    }
    String clientProfile = dto.clientProfile();
    if (Strings.isNotBlank(clientProfile)) {
        String clientParentProfile = dto.getClientParentProfile();
        if (Strings.isNullOrBlank(clientParentProfile)) {
            clientParentProfile = "mq-client-base";
        }
        mqService.createOrUpdateMQClientProfile(version, clientProfile, group, clientParentProfile);
    }
    return profile;
}
Also used : MQService(io.fabric8.api.MQService) ProfileRequirements(io.fabric8.api.ProfileRequirements) BrokerKind(io.fabric8.api.jmx.BrokerKind) HashMap(java.util.HashMap) Profile(io.fabric8.api.Profile) MQBrokerConfigDTO(io.fabric8.api.jmx.MQBrokerConfigDTO) FabricRequirements(io.fabric8.api.FabricRequirements) Map(java.util.Map) HashMap(java.util.HashMap)

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