Search in sources :

Example 1 with DependencyDTO

use of io.fabric8.deployer.dto.DependencyDTO in project fabric8 by jboss-fuse.

the class JavaContainers method addMavenDependencies.

protected static void addMavenDependencies(Map<String, Parser> artifacts, DependencyDTO dependency) throws MalformedURLException {
    String url = dependency.toBundleUrlWithType();
    Parser parser = Parser.parsePathWithSchemePrefix(url);
    String scope = dependency.getScope();
    if (!artifacts.containsKey(url) && !artifacts.containsValue(parser) && !(Objects.equal("test", scope))) {
        LOGGER.debug("Adding url: " + url + " parser: " + parser);
        artifacts.put(url, parser);
    }
    List<DependencyDTO> children = dependency.getChildren();
    if (children != null) {
        for (DependencyDTO child : children) {
            addMavenDependencies(artifacts, child);
        }
    }
}
Also used : DependencyDTO(io.fabric8.deployer.dto.DependencyDTO) Parser(io.fabric8.maven.util.Parser)

Example 2 with DependencyDTO

use of io.fabric8.deployer.dto.DependencyDTO in project fabric8 by jboss-fuse.

the class ProjectDeployerImpl method resolveProfileDeployments.

private DeployResults resolveProfileDeployments(ProjectRequirements requirements, FabricService fabric, Profile profile, ProfileBuilder builder, boolean mergeWithOldProfile) throws Exception {
    DependencyDTO rootDependency = requirements.getRootDependency();
    ProfileService profileService = fabricService.get().adapt(ProfileService.class);
    if (!mergeWithOldProfile) {
        // If we're not merging with the old profile, then create a dummy empty profile to merge with instead.
        ProfileBuilder emptyProfile = ProfileBuilder.Factory.create(profile.getVersion(), profile.getId()).setOverlay(true);
        profile = emptyProfile.getProfile();
    }
    if (rootDependency != null) {
        // as a hack lets just add this bundle in
        LOG.info("Got root: " + rootDependency);
        List<String> parentIds = profile.getParentIds();
        Profile overlay = profileService.getOverlayProfile(profile);
        String bundleUrl = rootDependency.toBundleUrlWithType();
        LOG.info("Using resolver to add extra features and bundles on " + bundleUrl);
        List<String> features = new ArrayList<String>(profile.getFeatures());
        List<String> bundles = new ArrayList<String>(profile.getBundles());
        List<String> optionals = new ArrayList<String>(profile.getOptionals());
        if (requirements.getFeatures() != null) {
            features.addAll(requirements.getFeatures());
        }
        if (requirements.getBundles() != null) {
            bundles.addAll(requirements.getBundles());
        }
        bundles.add(bundleUrl);
        LOG.info("Adding bundle: " + bundleUrl);
        // TODO we maybe should detect a karaf based container in a nicer way than this?
        boolean isKarafContainer = parentIds.contains("karaf") || parentIds.contains("containers-karaf");
        boolean addBundleDependencies = Objects.equal("bundle", rootDependency.getType()) || isKarafContainer;
        if (addBundleDependencies && requirements.isUseResolver()) {
            // lets build up a list of all current active features and bundles along with all discovered features
            List<Feature> availableFeatures = new ArrayList<Feature>();
            addAvailableFeaturesFromProfile(availableFeatures, fabric, overlay);
            Set<String> currentBundleLocations = new HashSet<>();
            currentBundleLocations.addAll(bundles);
            // lets add the current features
            DownloadManager downloadManager = DownloadManagers.createDownloadManager(fabric, executorService);
            Set<Feature> currentFeatures = AgentUtils.getFeatures(fabric, downloadManager, overlay);
            addBundlesFromProfile(currentBundleLocations, overlay);
            List<String> parentProfileIds = requirements.getParentProfiles();
            if (parentProfileIds != null) {
                for (String parentProfileId : parentProfileIds) {
                    Profile parentProfile = profileService.getProfile(profile.getVersion(), parentProfileId);
                    Profile parentOverlay = profileService.getOverlayProfile(parentProfile);
                    Set<Feature> parentFeatures = AgentUtils.getFeatures(fabric, downloadManager, parentOverlay);
                    currentFeatures.addAll(parentFeatures);
                    addAvailableFeaturesFromProfile(availableFeatures, fabric, parentOverlay);
                    addBundlesFromProfile(currentBundleLocations, parentOverlay);
                }
            }
            // lets add all known features from the known repositories
            for (DependencyDTO dependency : rootDependency.getChildren()) {
                if ("test".equals(dependency.getScope()) || "provided".equals(dependency.getScope())) {
                    continue;
                }
                if ("jar".equals(dependency.getType())) {
                    String match = getAllServiceMixBundles().get(dependency.getGroupId() + ":" + dependency.getArtifactId() + ":" + dependency.getVersion());
                    if (match != null) {
                        LOG.info("Replacing artifact " + dependency + " with servicemix bundle " + match);
                        String[] parts = match.split(":");
                        dependency.setGroupId(parts[0]);
                        dependency.setArtifactId(parts[1]);
                        dependency.setVersion(parts[2]);
                        dependency.setType("bundle");
                    }
                }
                String prefix = dependency.toBundleUrlWithoutVersion();
                Feature feature = findFeatureWithBundleLocationPrefix(currentFeatures, prefix);
                if (feature != null) {
                    LOG.info("Feature is already is in the profile " + feature.getId() + " for " + dependency.toBundleUrl());
                } else {
                    feature = findFeatureWithBundleLocationPrefix(availableFeatures, prefix);
                    if (feature != null) {
                        String name = feature.getName();
                        if (features.contains(name)) {
                            LOG.info("Feature is already added " + name + " for " + dependency.toBundleUrl());
                        } else {
                            LOG.info("Found a matching feature for bundle " + dependency.toBundleUrl() + ": " + feature.getId());
                            features.add(name);
                        }
                    } else {
                        String bundleUrlWithType = dependency.toBundleUrlWithType();
                        String foundBundleUri = findBundleUri(currentBundleLocations, prefix);
                        if (foundBundleUri != null) {
                            LOG.info("Bundle already included " + foundBundleUri + " for " + bundleUrlWithType);
                        } else {
                            boolean ignore = false;
                            String bundleWithoutMvnPrefix = getMavenCoords(bundleUrlWithType);
                            for (String ignoreBundlePrefix : RESOLVER_IGNORE_BUNDLE_PREFIXES) {
                                if (bundleWithoutMvnPrefix.startsWith(ignoreBundlePrefix)) {
                                    ignore = true;
                                    break;
                                }
                            }
                            if (ignore) {
                                LOG.info("Ignoring bundle: " + bundleUrlWithType);
                            } else {
                                boolean optional = dependency.isOptional();
                                LOG.info("Adding " + (optional ? "optional " : "") + " bundle: " + bundleUrlWithType);
                                if (optional) {
                                    optionals.add(bundleUrlWithType);
                                } else {
                                    bundles.add(bundleUrlWithType);
                                }
                            }
                        }
                    }
                }
            }
            // Modify the profile through the {@link ProfileBuilder}
            builder.setOptionals(optionals).setFeatures(features);
        }
        builder.setBundles(bundles);
    }
    profile = profileService.updateProfile(builder.getProfile());
    Integer minimumInstances = requirements.getMinimumInstances();
    if (minimumInstances != null) {
        FabricRequirements fabricRequirements = fabricService.get().getRequirements();
        ProfileRequirements profileRequirements = fabricRequirements.getOrCreateProfileRequirement(profile.getId());
        profileRequirements.setMinimumInstances(minimumInstances);
        fabricRequirements.setVersion(requirements.getVersion());
        fabricService.get().setRequirements(fabricRequirements);
    }
    // lets find a hawtio profile and version
    String profileUrl = findHawtioUrl(fabric);
    if (profileUrl == null) {
        profileUrl = "/";
    }
    if (!profileUrl.endsWith("/")) {
        profileUrl += "/";
    }
    String profilePath = Profiles.convertProfileIdToPath(profile.getId());
    profileUrl += "index.html#/wiki/branch/" + profile.getVersion() + "/view/fabric/profiles/" + profilePath;
    return new DeployResults(profile, profileUrl);
}
Also used : ProfileRequirements(io.fabric8.api.ProfileRequirements) DeployResults(io.fabric8.deployer.dto.DeployResults) ArrayList(java.util.ArrayList) DependencyDTO(io.fabric8.deployer.dto.DependencyDTO) ProfileBuilder(io.fabric8.api.ProfileBuilder) Feature(io.fabric8.agent.model.Feature) DownloadManager(io.fabric8.agent.download.DownloadManager) Profile(io.fabric8.api.Profile) ProfileService(io.fabric8.api.ProfileService) FabricRequirements(io.fabric8.api.FabricRequirements) HashSet(java.util.HashSet)

Example 3 with DependencyDTO

use of io.fabric8.deployer.dto.DependencyDTO in project fabric8 by jboss-fuse.

the class ProjectDeployerTest method testProfileDeploy.

@Test
@Ignore("[FABRIC-1110] Mocked test makes invalid assumption on the implementation")
public void testProfileDeploy() throws Exception {
    String groupId = "foo";
    String artifactId = "bar";
    String expectedProfileId = groupId + "-" + artifactId;
    String versionId = "1.0";
    ProjectRequirements requirements = new ProjectRequirements();
    DependencyDTO rootDependency = new DependencyDTO();
    requirements.setRootDependency(rootDependency);
    rootDependency.setGroupId(groupId);
    rootDependency.setArtifactId(artifactId);
    rootDependency.setVersion("1.0.0");
    List<String> parentProfileIds = Arrays.asList("karaf");
    List<String> features = Arrays.asList("cxf", "war");
    requirements.setParentProfiles(parentProfileIds);
    requirements.setFeatures(features);
    projectDeployer.deployProject(requirements);
    // now we should have a profile created
    Profile profile = assertProfileInFabric(expectedProfileId, versionId);
    assertBundleCount(profile, 1);
    assertEquals("parent ids", parentProfileIds, profile.getParentIds());
    assertFeatures(profile, features);
    String requirementsFileName = "dependencies/" + groupId + "/" + artifactId + "-requirements.json";
    byte[] jsonData = profile.getFileConfiguration(requirementsFileName);
    assertNotNull("should have found some JSON for: " + requirementsFileName, jsonData);
    String json = new String(jsonData);
    LOG.info("Got JSON: " + json);
    // lets replace the version, parent, features
    rootDependency.setVersion("1.0.1");
    projectDeployer.deployProject(requirements);
    profile = assertProfileInFabric(expectedProfileId, versionId);
    assertBundleCount(profile, 1);
    // now lets make a new version
    expectedProfileId = "cheese";
    versionId = "1.2";
    requirements.setVersion(versionId);
    requirements.setProfileId(expectedProfileId);
    parentProfileIds = Arrays.asList("default");
    features = Arrays.asList("camel", "war");
    requirements.setParentProfiles(parentProfileIds);
    requirements.setFeatures(features);
    projectDeployer.deployProject(requirements);
    profile = assertProfileInFabric(expectedProfileId, versionId);
    assertBundleCount(profile, 1);
    assertEquals("parent ids", parentProfileIds, profile.getParentIds());
    assertFeatures(profile, features);
// assertProfileMetadata();
}
Also used : DependencyDTO(io.fabric8.deployer.dto.DependencyDTO) ProjectRequirements(io.fabric8.deployer.dto.ProjectRequirements) Profile(io.fabric8.api.Profile) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with DependencyDTO

use of io.fabric8.deployer.dto.DependencyDTO in project fabric8 by jboss-fuse.

the class DeployToProfileMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (isIgnoreProject())
        return;
    try {
        ProjectRequirements requirements = new ProjectRequirements();
        if (isIncludeArtifact()) {
            DependencyDTO rootDependency = loadRootDependency();
            requirements.setRootDependency(rootDependency);
        }
        configureRequirements(requirements);
        // validate requirements
        if (requirements.getProfileId() != null) {
            // make sure the profile id is a valid name
            FabricValidations.validateProfileName(requirements.getProfileId());
        }
        boolean newUserAdded = false;
        fabricServer = mavenSettings.getServer(serverId);
        // we may have username and password from jolokiaUrl
        String jolokiaUsername = null;
        String jolokiaPassword = null;
        try {
            URL url = new URL(jolokiaUrl);
            String s = url.getUserInfo();
            if (Strings.isNotBlank(s) && s.indexOf(':') > 0) {
                int idx = s.indexOf(':');
                jolokiaUsername = s.substring(0, idx);
                jolokiaPassword = s.substring(idx + 1);
                customUsernameAndPassword = true;
            }
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("Option jolokiaUrl is invalid due " + e.getMessage());
        }
        // jolokia url overrides username/password configured in maven settings
        if (jolokiaUsername != null) {
            if (fabricServer == null) {
                fabricServer = new Server();
            }
            getLog().info("Using username: " + jolokiaUsername + " and password from provided jolokiaUrl option");
            fabricServer.setId(serverId);
            fabricServer.setUsername(jolokiaUsername);
            fabricServer.setPassword(jolokiaPassword);
        }
        if (fabricServer == null) {
            boolean create = false;
            if (mavenSettings.isInteractiveMode() && mavenSettingsWriter != null) {
                System.out.println("Maven settings file: " + mavenSettingsFile.getAbsolutePath());
                System.out.println();
                System.out.println();
                System.out.println("There is no <server> section in your ~/.m2/settings.xml file for the server id: " + serverId);
                System.out.println();
                System.out.println("You can enter the username/password now and have the settings.xml updated or you can do this by hand if you prefer.");
                System.out.println();
                while (true) {
                    String value = readInput("Would you like to update the settings.xml file now? (y/n): ").toLowerCase();
                    if (value.startsWith("n")) {
                        System.out.println();
                        System.out.println();
                        break;
                    } else if (value.startsWith("y")) {
                        create = true;
                        break;
                    }
                }
                if (create) {
                    System.out.println("Please let us know the login details for this server: " + serverId);
                    System.out.println();
                    String userName = readInput("Username: ");
                    String password = readPassword("Password: ");
                    String password2 = readPassword("Repeat Password: ");
                    while (!password.equals(password2)) {
                        System.out.println("Passwords do not match, please try again.");
                        password = readPassword("Password: ");
                        password2 = readPassword("Repeat Password: ");
                    }
                    System.out.println();
                    fabricServer = new Server();
                    fabricServer.setId(serverId);
                    fabricServer.setUsername(userName);
                    fabricServer.setPassword(password);
                    mavenSettings.addServer(fabricServer);
                    if (mavenSettingsFile.exists()) {
                        int counter = 1;
                        while (true) {
                            File backupFile = new File(mavenSettingsFile.getAbsolutePath() + ".backup-" + counter++ + ".xml");
                            if (!backupFile.exists()) {
                                System.out.println("Copied original: " + mavenSettingsFile.getAbsolutePath() + " to: " + backupFile.getAbsolutePath());
                                Files.copy(mavenSettingsFile, backupFile);
                                break;
                            }
                        }
                    }
                    Map<String, Object> config = new HashMap<String, Object>();
                    mavenSettingsWriter.write(mavenSettingsFile, config, mavenSettings);
                    System.out.println("Updated settings file: " + mavenSettingsFile.getAbsolutePath());
                    System.out.println();
                    newUserAdded = true;
                }
            }
        }
        if (fabricServer == null) {
            String message = "No <server> element can be found in ~/.m2/settings.xml for the server <id>" + serverId + "</id> so we cannot connect to fabric8!\n\n" + "Please add the following to your ~/.m2/settings.xml file (using the correct user/password values):\n\n" + "<servers>\n" + "  <server>\n" + "    <id>" + serverId + "</id>\n" + "    <username>admin</username>\n" + "    <password>admin</password>\n" + "  </server>\n" + "</servers>\n";
            getLog().error(message);
            throw new MojoExecutionException(message);
        }
        // now lets invoke the mbean
        J4pClient client = createJolokiaClient();
        if (upload) {
            uploadDeploymentUnit(client, newUserAdded || customUsernameAndPassword);
        } else {
            getLog().info("Uploading to the fabric8 maven repository is disabled");
        }
        DeployResults results = uploadRequirements(client, requirements);
        if (results != null) {
            uploadReadMeFile(client, results);
            uploadProfileConfigurations(client, results);
            refreshProfile(client, results);
        }
    } catch (MojoExecutionException e) {
        throw e;
    } catch (Exception e) {
        throw new MojoExecutionException("Error executing", e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) DeployResults(io.fabric8.deployer.dto.DeployResults) Server(org.apache.maven.settings.Server) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) J4pClient(org.jolokia.client.J4pClient) DependencyDTO(io.fabric8.deployer.dto.DependencyDTO) URL(java.net.URL) MalformedObjectNameException(javax.management.MalformedObjectNameException) J4pRemoteException(org.jolokia.client.exception.J4pRemoteException) ArtifactDeploymentException(org.apache.maven.artifact.deployer.ArtifactDeploymentException) J4pConnectException(org.jolokia.client.exception.J4pConnectException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) J4pException(org.jolokia.client.exception.J4pException) ProjectRequirements(io.fabric8.deployer.dto.ProjectRequirements) File(java.io.File)

Example 5 with DependencyDTO

use of io.fabric8.deployer.dto.DependencyDTO in project fabric8 by jboss-fuse.

the class AbstractProfileMojo method buildFrom.

private DependencyDTO buildFrom(DependencyNode node) {
    Artifact artifact = node.getArtifact();
    if (artifact != null) {
        DependencyDTO answer = new DependencyDTO();
        answer.setGroupId(artifact.getGroupId());
        answer.setArtifactId(artifact.getArtifactId());
        if (artifact.isSnapshot() && !uniqueVersion) {
            answer.setVersion(artifact.getBaseVersion());
        } else {
            answer.setVersion(artifact.getVersion());
        }
        answer.setClassifier(artifact.getClassifier());
        String scope = artifact.getScope();
        answer.setScope(scope);
        answer.setType(artifact.getType());
        // so lets ignore this for the maven project's artifact
        if (artifact.getClassifier() == null && "jar".equals(artifact.getType())) {
            if (project.getArtifact().equals(artifact)) {
                getLog().debug("Ignoring bundle check on the maven project artifact: " + artifact + " as this causes issues with the maven-install-plugin and we can assume the project packaging is accurate");
            } else {
                try {
                    ArtifactResolutionRequest request = new ArtifactResolutionRequest();
                    request.setArtifact(artifact);
                    request.setRemoteRepositories(remoteRepositories);
                    request.setLocalRepository(localRepository);
                    resolver.resolve(request);
                    JarInputStream jis = new JarInputStream(new FileInputStream(artifact.getFile()));
                    Manifest man = jis.getManifest();
                    String bsn = man.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
                    if (bsn != null) {
                        answer.setType("bundle");
                    } else {
                    // Try to find a matching servicemix bundle for it
                    /*
                        Map<String, String> bundles = getAllServiceMixBundles();
                        getLog().debug("Trying to find a matching bundle for " + artifact);
                        String match = bundles.get(artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion());
                        if (match != null) {
                            String[] parts = match.split(":");
                            answer.setGroupId(parts[0]);
                            answer.setArtifactId(parts[1]);
                            answer.setVersion(parts[2]);
                            getLog().info("Replacing artifact " + artifact + " with servicemix bundle " + match);
                        }
                        */
                    }
                } catch (Exception e) {
                    getLog().debug("Error checking artifact type for " + artifact, e);
                }
            }
        }
        answer.setOptional(artifact.isOptional());
        String type = answer.getType();
        if (type != null && type.equals("pom")) {
            getLog().debug("Ignoring pom.xml for " + answer);
            return null;
        }
        int state = node.getState();
        if (state != DependencyNode.INCLUDED) {
            getLog().debug("Ignoring " + node);
            return null;
        }
        if (isWarProject()) {
            if (scope != null && !scope.equals("provided")) {
                getLog().debug("WAR packaging so ignoring non-provided scope " + scope + " for " + node);
                return null;
            }
        }
        List children = node.getChildren();
        for (Object child : children) {
            if (child instanceof DependencyNode) {
                DependencyNode childNode = (DependencyNode) child;
                if (childNode.getState() == DependencyNode.INCLUDED) {
                    String childScope = childNode.getArtifact().getScope();
                    if (!"test".equals(childScope) && !"provided".equals(childScope)) {
                        DependencyDTO childDTO = buildFrom(childNode);
                        if (childDTO != null) {
                            answer.addChild(childDTO);
                        }
                    } else {
                        getLog().debug("Ignoring artifact " + childNode.getArtifact() + " with scope " + childScope);
                    }
                }
            }
        }
        return answer;
    }
    return null;
}
Also used : JarInputStream(java.util.jar.JarInputStream) ArtifactResolutionRequest(org.apache.maven.artifact.resolver.ArtifactResolutionRequest) DependencyNode(org.apache.maven.shared.dependency.tree.DependencyNode) DependencyDTO(io.fabric8.deployer.dto.DependencyDTO) List(java.util.List) ArrayList(java.util.ArrayList) Manifest(java.util.jar.Manifest) Artifact(org.apache.maven.artifact.Artifact) FileInputStream(java.io.FileInputStream) DependencyTreeBuilderException(org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Aggregations

DependencyDTO (io.fabric8.deployer.dto.DependencyDTO)8 ProjectRequirements (io.fabric8.deployer.dto.ProjectRequirements)4 IOException (java.io.IOException)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 MojoFailureException (org.apache.maven.plugin.MojoFailureException)3 Profile (io.fabric8.api.Profile)2 DeployResults (io.fabric8.deployer.dto.DeployResults)2 File (java.io.File)2 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 DownloadManager (io.fabric8.agent.download.DownloadManager)1 Feature (io.fabric8.agent.model.Feature)1 FabricRequirements (io.fabric8.api.FabricRequirements)1 ProfileBuilder (io.fabric8.api.ProfileBuilder)1 ProfileRequirements (io.fabric8.api.ProfileRequirements)1 ProfileService (io.fabric8.api.ProfileService)1 Parser (io.fabric8.maven.util.Parser)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1