Search in sources :

Example 16 with Profile

use of io.fabric8.api.Profile 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 17 with Profile

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

the class OpenshiftContainerProvider method create.

@Override
public CreateOpenshiftContainerMetadata create(CreateOpenshiftContainerOptions options, CreationStateListener listener) throws Exception {
    assertValid();
    IUser user = getOrCreateConnection(options).getUser();
    IDomain domain = getOrCreateDomain(user, options);
    String cartridgeUrl = null;
    Set<String> profiles = options.getProfiles();
    String versionId = options.getVersion();
    Map<String, String> openshiftConfigOverlay = new HashMap<String, String>();
    if (profiles != null && versionId != null) {
        ProfileService profileService = fabricService.get().adapt(ProfileService.class);
        Version version = profileService.getVersion(versionId);
        if (version != null) {
            for (String profileId : profiles) {
                Profile profile = version.getRequiredProfile(profileId);
                if (profile != null) {
                    Profile overlay = profileService.getOverlayProfile(profile);
                    Map<String, String> openshiftConfig = overlay.getConfiguration(OpenShiftConstants.OPENSHIFT_PID);
                    if (openshiftConfig != null) {
                        openshiftConfigOverlay.putAll(openshiftConfig);
                    }
                }
            }
        }
        cartridgeUrl = openshiftConfigOverlay.get("cartridge");
    }
    if (cartridgeUrl == null) {
        cartridgeUrl = defaultCartridgeUrl;
    }
    String[] cartridgeUrls = cartridgeUrl.split(" ");
    LOG.info("Creating cartridges: " + cartridgeUrl);
    String standAloneCartridgeUrl = cartridgeUrls[0];
    StandaloneCartridge cartridge;
    if (standAloneCartridgeUrl.startsWith(PREFIX_CARTRIDGE_ID)) {
        cartridge = new StandaloneCartridge(standAloneCartridgeUrl.substring(PREFIX_CARTRIDGE_ID.length()));
    } else {
        cartridge = new StandaloneCartridge(new URL(standAloneCartridgeUrl));
    }
    String zookeeperUrl = fabricService.get().getZookeeperUrl();
    String zookeeperPassword = fabricService.get().getZookeeperPassword();
    Map<String, String> userEnvVars = null;
    if (!options.isEnsembleServer()) {
        userEnvVars = new HashMap<String, String>();
        userEnvVars.put("OPENSHIFT_FUSE_ZOOKEEPER_URL", zookeeperUrl);
        userEnvVars.put("OPENSHIFT_FUSE_ZOOKEEPER_PASSWORD", zookeeperPassword);
        String zkPasswordEncode = System.getProperty("zookeeper.password.encode", "true");
        userEnvVars.put("OPENSHIFT_FUSE_ZOOKEEPER_PASSWORD_ENCODE", zkPasswordEncode);
        userEnvVars.put("OPENSHIFT_FUSE_CREATED_FROM_FABRIC", "true");
    }
    String initGitUrl = null;
    int timeout = IHttpClient.NO_TIMEOUT;
    ApplicationScale scale = null;
    String containerName = options.getName();
    long t0 = System.currentTimeMillis();
    IApplication application;
    try {
        application = domain.createApplication(containerName, cartridge, scale, new GearProfile(options.getGearProfile()), initGitUrl, timeout, userEnvVars);
    } catch (OpenShiftTimeoutException e) {
        long t1;
        do {
            Thread.sleep(5000);
            domain.refresh();
            application = domain.getApplicationByName(containerName);
            if (application != null) {
                break;
            }
            t1 = System.currentTimeMillis();
        } while (t1 - t0 < TimeUnit.MILLISECONDS.convert(15, TimeUnit.MINUTES));
    }
    LOG.info("Created application " + containerName);
    // now lets add all the embedded cartridges
    List<IEmbeddableCartridge> list = new ArrayList<IEmbeddableCartridge>();
    for (int idx = 1, size = cartridgeUrls.length; idx < size; idx++) {
        String embeddedUrl = cartridgeUrls[idx];
        LOG.info("Adding embedded cartridge: " + embeddedUrl);
        if (embeddedUrl.startsWith(PREFIX_CARTRIDGE_ID)) {
            list.add(new EmbeddableCartridge(embeddedUrl.substring(PREFIX_CARTRIDGE_ID.length())));
        } else {
            list.add(new EmbeddableCartridge(new URL(embeddedUrl)));
        }
    }
    if (!list.isEmpty()) {
        application.addEmbeddableCartridges(list);
    }
    String gitUrl = application.getGitUrl();
    // in case of OpenShiftTimeoutException, application resource doesn't contain getCreationLog().
    // actually this method throws NPE
    CreateOpenshiftContainerMetadata metadata = new CreateOpenshiftContainerMetadata(domain.getId(), application.getUUID(), application.getMessages() == null ? "" : application.getCreationLog(), gitUrl);
    metadata.setContainerName(containerName);
    metadata.setCreateOptions(options);
    return metadata;
}
Also used : HashMap(java.util.HashMap) ApplicationScale(com.openshift.client.ApplicationScale) OpenShiftTimeoutException(com.openshift.client.OpenShiftTimeoutException) ArrayList(java.util.ArrayList) GearProfile(com.openshift.internal.client.GearProfile) Profile(io.fabric8.api.Profile) IGearProfile(com.openshift.client.IGearProfile) URL(java.net.URL) IDomain(com.openshift.client.IDomain) IApplication(com.openshift.client.IApplication) ProfileService(io.fabric8.api.ProfileService) Version(io.fabric8.api.Version) GearProfile(com.openshift.internal.client.GearProfile) IGearProfile(com.openshift.client.IGearProfile) IUser(com.openshift.client.IUser) EmbeddableCartridge(com.openshift.client.cartridge.EmbeddableCartridge) IEmbeddableCartridge(com.openshift.client.cartridge.IEmbeddableCartridge) StandaloneCartridge(com.openshift.client.cartridge.StandaloneCartridge) IEmbeddableCartridge(com.openshift.client.cartridge.IEmbeddableCartridge)

Example 18 with Profile

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

the class DeploymentUpdater method updateDeployment.

public void updateDeployment(Git git, File baseDir, CredentialsProvider credentials) throws Exception {
    Set<String> bundles = new LinkedHashSet<String>();
    Set<Feature> features = new LinkedHashSet<Feature>();
    Profile overlayProfile = container.getOverlayProfile();
    Profile effectiveProfile = Profiles.getEffectiveProfile(fabricService, overlayProfile);
    bundles.addAll(effectiveProfile.getBundles());
    AgentUtils.addFeatures(features, fabricService, downloadManager, effectiveProfile);
    if (copyFilesIntoGit) {
        copyDeploymentsIntoGit(git, baseDir, bundles, features);
    } else {
        addDeploymentsIntoPom(git, baseDir, effectiveProfile, bundles, features);
    }
    // now lets do a commit
    String message = "updating deployment";
    git.commit().setMessage(message).call();
    enableDeployDirectory(git, baseDir);
    String branch = GitHelpers.currentBranch(git);
    LOG.info("Pushing deployment changes to branch " + branch + " credentials " + credentials + " for container " + container.getId());
    try {
        Iterable<PushResult> results = git.push().setCredentialsProvider(credentials).setRefSpecs(new RefSpec(branch)).setOutputStream(new LoggingOutputStream(LOG)).call();
        /*
            for (PushResult result : results) {
                LOG.info(result.getMessages());
            }
*/
        LOG.info("Pushed deployment changes to branch " + branch + " for container " + container.getId());
    } catch (GitAPIException e) {
        LOG.error("Failed to push deployment changes to branch " + branch + " for container " + container.getId() + ". Reason: " + e, e);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RefSpec(org.eclipse.jgit.transport.RefSpec) LoggingOutputStream(io.fabric8.common.util.LoggingOutputStream) PushResult(org.eclipse.jgit.transport.PushResult) Feature(io.fabric8.agent.model.Feature) Profile(io.fabric8.api.Profile)

Example 19 with Profile

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

the class OpenShiftDeployAgent method onConfigurationChanged.

protected void onConfigurationChanged() {
    LOGGER.info("Configuration has changed; so checking the Fabric managed Java cartridges on OpenShift are up to date");
    Container[] containers = fabricService.get().getContainers();
    for (Container container : containers) {
        Profile effectiveProfile = Profiles.getEffectiveProfile(fabricService.get(), container.getOverlayProfile());
        Map<String, String> openshiftConfiguration = effectiveProfile.getConfiguration(OpenShiftConstants.OPENSHIFT_PID);
        if (openshiftConfiguration != null) {
            DeploymentUpdater deployTask = null;
            try {
                deployTask = createDeployTask(container, openshiftConfiguration);
            } catch (MalformedURLException e) {
                LOGGER.error("Failed to create DeploymentUpdater. " + e, e);
            }
            if (deployTask != null && OpenShiftUtils.isFabricManaged(openshiftConfiguration)) {
                String containerId = container.getId();
                IOpenShiftConnection connection = OpenShiftUtils.createConnection(container);
                CreateOpenshiftContainerOptions options = OpenShiftUtils.getCreateOptions(container);
                if (connection == null || options == null) {
                    LOGGER.warn("Ignoring container which has no openshift connection or options. connection: " + connection + " options: " + options);
                } else {
                    try {
                        IApplication application = OpenShiftUtils.getApplication(container, connection);
                        if (application != null) {
                            final String gitUrl = application.getGitUrl();
                            if (gitUrl != null) {
                                LOGGER.info("Git URL is " + gitUrl);
                                final CartridgeGitRepository repo = new CartridgeGitRepository(containerId);
                                final List<IOpenShiftSSHKey> sshkeys = application.getDomain().getUser().getSSHKeys();
                                final CredentialsProvider credentials = new CredentialsProvider() {

                                    @Override
                                    public boolean supports(CredentialItem... items) {
                                        return true;
                                    }

                                    @Override
                                    public boolean isInteractive() {
                                        return true;
                                    }

                                    @Override
                                    public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {
                                        LOGGER.info("Credential request " + uri + " items " + Arrays.asList(items));
                                        int i = -1;
                                        for (CredentialItem item : items) {
                                            if (item instanceof CredentialItem.StringType) {
                                                CredentialItem.StringType stringType = (CredentialItem.StringType) item;
                                                int idx = ++i < sshkeys.size() ? i : 0;
                                                if (idx < sshkeys.size()) {
                                                    IOpenShiftSSHKey sshKey = sshkeys.get(idx);
                                                    String passphrase = sshKey.getPublicKey();
                                                    LOGGER.info("For item " + item + " index " + i + " using passphrase: " + passphrase);
                                                    stringType.setValue(passphrase);
                                                } else {
                                                    LOGGER.warn("No ssh keys we can pass into git!");
                                                }
                                                continue;
                                            } else {
                                                LOGGER.warn("Unknown CredentialItem " + item);
                                            }
                                        }
                                        return true;
                                    }
                                };
                                final DeploymentUpdater finalDeployTask = deployTask;
                                SshSessionFactoryUtils.useOpenShiftSessionFactory(new Callable<Object>() {

                                    @Override
                                    public Object call() throws Exception {
                                        repo.cloneOrPull(gitUrl, credentials);
                                        finalDeployTask.updateDeployment(repo.getGit(), repo.getLocalRepo(), credentials);
                                        return null;
                                    }
                                });
                            }
                        }
                    } catch (Exception e) {
                        LOGGER.error("Failed to update container " + containerId + ". Reason: " + e, e);
                    } finally {
                        OpenShiftUtils.close(connection);
                    }
                }
            }
        }
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) MalformedURLException(java.net.MalformedURLException) CreateOpenshiftContainerOptions(io.fabric8.openshift.CreateOpenshiftContainerOptions) CredentialItem(org.eclipse.jgit.transport.CredentialItem) UnsupportedCredentialItem(org.eclipse.jgit.errors.UnsupportedCredentialItem) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) IOpenShiftSSHKey(com.openshift.client.IOpenShiftSSHKey) Profile(io.fabric8.api.Profile) MalformedURLException(java.net.MalformedURLException) Container(io.fabric8.api.Container) IApplication(com.openshift.client.IApplication) IOpenShiftConnection(com.openshift.client.IOpenShiftConnection)

Example 20 with Profile

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

the class OpenShiftPomDeployerTest method doTest.

protected void doTest(String folder, String[] artifactUrls, String[] repoUrls, String expectedCamelDependencyScope, String expectedHawtioDependencyScope) throws Exception {
    File sourceDir = new File(baseDir, "src/test/resources/" + folder);
    assertDirectoryExists(sourceDir);
    File pomSource = new File(sourceDir, "pom.xml");
    assertFileExists(pomSource);
    File outputDir = new File(baseDir, "target/" + getClass().getName() + "/" + folder);
    outputDir.mkdirs();
    assertDirectoryExists(outputDir);
    File pom = new File(outputDir, "pom.xml");
    Files.copy(pomSource, pom);
    assertFileExists(pom);
    git = Git.init().setDirectory(outputDir).setGitDir(new File(outputDir, ".git")).call();
    assertDirectoryExists(new File(outputDir, ".git"));
    git.add().addFilepattern("pom.xml").call();
    git.commit().setMessage("Initial import").call();
    // now we have the git repo setup; lets run the update
    OpenShiftPomDeployer deployer = new OpenShiftPomDeployer(git, outputDir, deployDir, webAppDir);
    System.out.println("About to update the pom " + pom + " with artifacts: " + Arrays.asList(artifactUrls));
    List<Parser> artifacts = new ArrayList<Parser>();
    for (String artifactUrl : artifactUrls) {
        artifacts.add(Parser.parsePathWithSchemePrefix(artifactUrl));
    }
    List<MavenRepositoryURL> repos = new ArrayList<MavenRepositoryURL>();
    for (String repoUrl : repoUrls) {
        repos.add(new MavenRepositoryURL(repoUrl));
    }
    deployer.update(artifacts, repos);
    System.out.println("Completed the new pom is: ");
    System.out.println(Files.toString(pom));
    Document xml = XmlUtils.parseDoc(pom);
    Element plugins = assertXPathElement(xml, "project/profiles/profile[id = 'openshift']/build/plugins");
    Element cleanExecution = assertXPathElement(plugins, "plugin[artifactId = 'maven-clean-plugin']/executions/execution[id = 'fuse-fabric-clean']");
    Element dependencySharedExecution = assertXPathElement(plugins, "plugin[artifactId = 'maven-dependency-plugin']/executions/execution[id = 'fuse-fabric-deploy-shared']");
    Element dependencyWebAppsExecution = assertXPathElement(plugins, "plugin[artifactId = 'maven-dependency-plugin']/executions/execution[id = 'fuse-fabric-deploy-webapps']");
    Element warPluginWarName = xpath("plugin[artifactId = 'maven-war-plugin']/configuration/warName").element(plugins);
    if (warPluginWarName != null) {
        String warName = warPluginWarName.getTextContent();
        System.out.println("WarName is now:  " + warName);
        assertTrue("Should not have ROOT war name", !"ROOT".equals(warName));
    }
    Element dependencies = assertXPathElement(xml, "project/dependencies");
    Element repositories = assertXPathElement(xml, "project/repositories");
    for (Parser artifact : artifacts) {
        // lets check there's only 1 dependency for group & artifact and it has the right version
        String group = groupId(artifact);
        String artifactId = artifact.getArtifact();
        Element dependency = assertSingleDependencyForGroupAndArtifact(dependencies, group, artifactId);
        Element version = assertXPathElement(dependency, "version");
        assertEquals("Version", artifact.getVersion(), version.getTextContent());
    }
    // lets check we either preserve scope, add provided or don't add a scope if there's none present in the underlying pom
    assertDependencyScope(dependencies, "org.apache.camel", "camel-core", expectedCamelDependencyScope);
    assertDependencyScope(dependencies, "org.drools", "drools-wb-distribution-wars", "provided");
    assertDependencyScope(dependencies, "io.hawt", "hawtio-web", expectedHawtioDependencyScope);
    assertRepositoryUrl(repositories, "https://maven.repository.redhat.com/ga/");
    assertRepositoryUrl(repositories, "https://repo.fusesource.com/nexus/content/groups/ea/");
}
Also used : OpenShiftPomDeployer(io.fabric8.openshift.agent.OpenShiftPomDeployer) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) MavenRepositoryURL(io.fabric8.maven.util.MavenRepositoryURL) Document(org.w3c.dom.Document) File(java.io.File) Parser(io.fabric8.maven.util.Parser)

Aggregations

Profile (io.fabric8.api.Profile)125 Version (io.fabric8.api.Version)50 Container (io.fabric8.api.Container)49 ArrayList (java.util.ArrayList)37 ProfileBuilder (io.fabric8.api.ProfileBuilder)34 Test (org.junit.Test)32 FabricService (io.fabric8.api.FabricService)28 HashMap (java.util.HashMap)25 File (java.io.File)24 ProfileService (io.fabric8.api.ProfileService)23 Map (java.util.Map)22 IOException (java.io.IOException)19 ProfileRequirements (io.fabric8.api.ProfileRequirements)13 HashSet (java.util.HashSet)12 GitVersion (io.fabric8.api.commands.GitVersion)11 FabricRequirements (io.fabric8.api.FabricRequirements)10 LinkedList (java.util.LinkedList)9 TreeMap (java.util.TreeMap)9 LockHandle (io.fabric8.api.LockHandle)8 Parser (io.fabric8.maven.util.Parser)8