Search in sources :

Example 76 with Repository

use of io.fabric8.agent.model.Repository in project fabric8 by jboss-fuse.

the class ServiceImplTest method testLoadWithoutRanges.

@Test
public void testLoadWithoutRanges() throws IOException, GitAPIException {
    BundleContext bundleContext = createMock(BundleContext.class);
    ComponentContext componentContext = createMock(ComponentContext.class);
    Bundle sysBundle = createMock(Bundle.class);
    BundleContext sysBundleContext = createMock(BundleContext.class);
    Bundle bundle = createMock(Bundle.class);
    Bundle bundle2 = createMock(Bundle.class);
    FrameworkWiring wiring = createMock(FrameworkWiring.class);
    GitPatchRepository repository = createMock(GitPatchRepository.class);
    // 
    // Create a new service, download a patch
    // 
    expect(componentContext.getBundleContext()).andReturn(bundleContext);
    expect(bundleContext.getBundle(0)).andReturn(sysBundle).anyTimes();
    expect(sysBundle.getBundleContext()).andReturn(sysBundleContext).anyTimes();
    expect(sysBundleContext.getProperty(Service.NEW_PATCH_LOCATION)).andReturn(storage.toString()).anyTimes();
    expect(repository.getManagedPatch(anyString())).andReturn(null).anyTimes();
    expect(repository.findOrCreateMainGitRepository()).andReturn(null).anyTimes();
    expect(sysBundleContext.getProperty("karaf.default.repository")).andReturn("system").anyTimes();
    expect(sysBundleContext.getProperty("karaf.home")).andReturn(karaf.getCanonicalPath()).anyTimes();
    expect(sysBundleContext.getProperty("karaf.base")).andReturn(karaf.getCanonicalPath()).anyTimes();
    expect(sysBundleContext.getProperty("karaf.name")).andReturn("root").anyTimes();
    expect(sysBundleContext.getProperty("karaf.instances")).andReturn(karaf.getCanonicalPath() + "/instances").anyTimes();
    expect(sysBundleContext.getProperty("karaf.data")).andReturn(karaf.getCanonicalPath() + "/data").anyTimes();
    replay(componentContext, sysBundleContext, sysBundle, bundleContext, bundle, repository);
    PatchManagement pm = new GitPatchManagementServiceImpl(bundleContext);
    ((GitPatchManagementServiceImpl) pm).setGitPatchRepository(repository);
    ServiceImpl service = new ServiceImpl();
    setField(service, "patchManagement", pm);
    service.activate(componentContext);
    PatchData pd = PatchData.load(getClass().getClassLoader().getResourceAsStream("test1.patch"));
    assertEquals(2, pd.getBundles().size());
    assertTrue(pd.getRequirements().isEmpty());
}
Also used : GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) PatchData(io.fabric8.patch.management.PatchData) ComponentContext(org.osgi.service.component.ComponentContext) Bundle(org.osgi.framework.Bundle) PatchManagement(io.fabric8.patch.management.PatchManagement) GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) GitPatchRepository(io.fabric8.patch.management.impl.GitPatchRepository) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 77 with Repository

use of io.fabric8.agent.model.Repository in project fabric8 by jboss-fuse.

the class FabricPatchServiceImpl method install.

@Override
public PatchResult install(final Patch patch, boolean simulation, final String versionId, boolean upload, final String username, final String password, final ProfileUpdateStrategy strategy) throws IOException {
    // we start from the same state as in standalone mode - after successful patch:add
    // we have other things to do in fabric env however:
    // 1. check prerequisites
    // 2. we don't care about current state of framework - it'll be managed by fabric-agent and we don't
    // necessary install a patch for this container we're in
    // 3. we don't do patchManagement.beginInstallation / patchManagement.commitInstallation here
    // this will be done later - after updated fabric-agent is started
    // 4. we don't have to analyze bundles/features/repositories updates - these will be handled simply by
    // updating profiles in specified version
    PatchKind kind = patch.getPatchData().isRollupPatch() ? PatchKind.ROLLUP : PatchKind.NON_ROLLUP;
    if (kind == PatchKind.NON_ROLLUP) {
        throw new UnsupportedOperationException("patch:fabric-install should be used for Rollup patches only");
    }
    String currentContainersVersionId = fabricService.getCurrentContainer().getVersionId();
    if (!simulation && versionId.equals(currentContainersVersionId)) {
        throw new UnsupportedOperationException("Can't install Rollup patch in current version. Please install" + " this patch in new version and then upgrade existing container(s)");
    }
    fabricService.adapt(ProfileService.class).getRequiredVersion(versionId);
    // just a list of new bundle locations - in fabric the updatable version depends on the moment we
    // apply the new version to existing containers.
    List<BundleUpdate> bundleUpdatesInThisPatch = bundleUpdatesInPatch(patch);
    Presentation.displayBundleUpdates(bundleUpdatesInThisPatch, true);
    PatchResult result = new PatchResult(patch.getPatchData(), simulation, System.currentTimeMillis(), bundleUpdatesInThisPatch, null);
    if (!simulation) {
        // update profile definitions stored in Git. We don't update ${karaf.home}/fabric, becuase it is used
        // only once - when importing profiles during fabric:create.
        // when fabric is already available, we have to update (Git) repository information
        GitOperation operation = new GitOperation() {

            @Override
            public Object call(Git git, GitContext context) throws Exception {
                // we can't pass git reference to patch-management
                // because patch-management private-packages git library
                // but we can leverage the write lock we have
                GitHelpers.checkoutBranch(git, versionId);
                // let's get back in history to the point before user changes (profile-edits), but not earlier
                // than last R patch
                String patchBranch = patchManagement.findLatestPatchRevision(git.getRepository().getDirectory(), versionId);
                // now install profiles from patch just like there were no user changes
                patchManagement.installProfiles(git.getRepository().getDirectory(), versionId, patch, strategy);
                // and finally we have to merge user and patch changes to profiles.
                patchManagement.mergeProfileChanges(patch, git.getRepository().getDirectory(), versionId, patchBranch);
                context.commitMessage("Installing rollup patch \"" + patch.getPatchData().getId() + "\"");
                return null;
            }
        };
        gitDataStore.gitOperation(new GitContext().requireCommit().setRequirePush(true), operation, null);
        if (upload) {
            PatchManagement.UploadCallback callback = new PatchManagement.UploadCallback() {

                @Override
                public void doWithUrlConnection(URLConnection connection) throws ProtocolException {
                    if (connection instanceof HttpURLConnection) {
                        ((HttpURLConnection) connection).setRequestMethod("PUT");
                    }
                    if (username != null && password != null) {
                        connection.setRequestProperty("Authorization", "Basic " + Base64Encoder.encode(username + ":" + password));
                    }
                }
            };
            patchManagement.uploadPatchArtifacts(patch.getPatchData(), fabricService.getMavenRepoUploadURI(), callback);
        }
    }
    return result;
}
Also used : PatchKind(io.fabric8.patch.management.PatchKind) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) ProfileService(io.fabric8.api.ProfileService) GitOperation(io.fabric8.git.internal.GitOperation) Git(org.eclipse.jgit.api.Git) HttpURLConnection(java.net.HttpURLConnection) GitContext(io.fabric8.api.GitContext) PatchManagement(io.fabric8.patch.management.PatchManagement) PatchResult(io.fabric8.patch.management.PatchResult) BundleUpdate(io.fabric8.patch.management.BundleUpdate)

Example 78 with Repository

use of io.fabric8.agent.model.Repository in project fabric8 by jboss-fuse.

the class Presentation method displayFeatureUpdates.

/**
 * Displays a table with installed (<code>install == true</code>) or rolledback features
 * @param featureUpdates
 * @param install is this displayed during patch install (<code>true</code>) or rollback (<code>false</code>)
 */
public static void displayFeatureUpdates(Collection<FeatureUpdate> featureUpdates, boolean install) {
    Set<String> toKeep = new TreeSet<>();
    Set<String> toRemove = new TreeSet<>();
    Set<String> toAdd = new TreeSet<>();
    for (FeatureUpdate fu : featureUpdates) {
        if (install) {
            toRemove.add(fu.getPreviousRepository());
        } else if (fu.getNewRepository() != null) {
            toRemove.add(fu.getNewRepository());
        }
        if (fu.getNewRepository() != null) {
            toAdd.add(install ? fu.getNewRepository() : fu.getPreviousRepository());
        } else if (install) {
            // keep old only during installation, when rolling back rollup patch, we don't want to keep
            // any new repository
            toKeep.add(fu.getPreviousRepository());
        }
    }
    toRemove.removeAll(toKeep);
    System.out.printf("========== Repositories to remove (%d):%n", toRemove.size());
    for (String repo : toRemove) {
        System.out.println(" - " + repo);
    }
    System.out.printf("========== Repositories to add (%d):%n", toAdd.size());
    for (String repo : toAdd) {
        System.out.println(" - " + repo);
    }
    System.out.printf("========== Repositories to keep (%d):%n", toKeep.size());
    for (String repo : toKeep) {
        System.out.println(" - " + repo);
    }
    System.out.printf("========== Features to (%s):%n", install ? "update" : "downgrade");
    int l1 = "[name]".length();
    int l2 = "[version]".length();
    int l3 = install ? "[new version]".length() : "[previous version]".length();
    Map<String, FeatureUpdate> map = new TreeMap<>();
    for (FeatureUpdate fu : featureUpdates) {
        if (fu.getName() == null) {
            continue;
        }
        if (fu.getName().length() > l1) {
            l1 = fu.getName().length();
        }
        if (install) {
            if (fu.getPreviousVersion().length() > l2) {
                l2 = fu.getPreviousVersion().length();
            }
            if (fu.getNewVersion() != null) {
                if (fu.getNewVersion().length() > l3) {
                    l3 = fu.getNewVersion().length();
                }
            }
        } else {
            if (fu.getNewVersion() != null) {
                if (fu.getNewVersion().length() > l2) {
                    l2 = fu.getNewVersion().length();
                }
            }
            if (fu.getPreviousVersion().length() > l3) {
                l3 = fu.getPreviousVersion().length();
            }
        }
        map.put(fu.getName(), fu);
    }
    System.out.printf("%-" + l1 + "s   %-" + l2 + "s   %-" + l3 + "s%n", "[name]", "[version]", install ? "[new version]" : "[previous version]");
    for (FeatureUpdate fu : map.values()) {
        System.out.printf("%-" + l1 + "s   %-" + l2 + "s   %-" + l3 + "s%n", fu.getName(), install ? fu.getPreviousVersion() : fu.getNewVersion() == null ? "<reinstall>" : fu.getNewVersion(), install ? fu.getNewVersion() == null ? "<reinstall>" : fu.getNewVersion() : fu.getPreviousVersion());
    }
    System.out.flush();
}
Also used : TreeSet(java.util.TreeSet) TreeMap(java.util.TreeMap) FeatureUpdate(io.fabric8.patch.management.FeatureUpdate)

Example 79 with Repository

use of io.fabric8.agent.model.Repository in project fabric8 by jboss-fuse.

the class CreateAction method doExecute.

protected Object doExecute() throws Exception {
    String adminRole = "admin";
    String administratorRole = "administrator";
    String superUserRole = "superuser";
    boolean adminExists = hasRole(adminRole, superUserRole, administratorRole);
    if (!adminExists) {
        System.out.println("The fabric:create command can be executed only by admin user");
        return null;
    }
    Path propsPath = runtimeProperties.getConfPath().resolve("users.properties");
    Properties userProps = new Properties(propsPath.toFile());
    if (!adminExists && !usersPropertiesFileContainsRole(userProps, adminRole, superUserRole, administratorRole)) {
        System.out.println("The etc/user.properties file must contain at least one admin user, if no other admin exists");
        return null;
    }
    // prevent creating fabric if already created
    ServiceReference<FabricService> sref = bundleContext.getServiceReference(FabricService.class);
    FabricService fabricService = sref != null ? bundleContext.getService(sref) : null;
    if (!force && (fabricService != null && fabricService.getCurrentContainer().isEnsembleServer())) {
        System.out.println("Current container " + fabricService.getCurrentContainerName() + " is already in the current fabric ensemble. Cannot create fabric.");
        System.out.println("You can use the --force option, if you want to force re-create the fabric.");
        return null;
    }
    Configuration bootConfiguration = configAdmin.getConfiguration(BootstrapConfiguration.COMPONENT_PID, null);
    Dictionary<String, Object> bootProperties = bootConfiguration.getProperties();
    if (bootProperties == null) {
        bootProperties = new Hashtable<>();
    }
    String runtimeIdentity = runtimeProperties.getRuntimeIdentity();
    CreateEnsembleOptions.Builder<?> builder = CreateEnsembleOptions.builder().zooKeeperServerTickTime(zooKeeperTickTime).zooKeeperServerInitLimit(zooKeeperInitLimit).zooKeeperServerSyncLimit(zooKeeperSyncLimit).zooKeeperServerDataDir(zooKeeperDataDir).zookeeperSnapRetainCount(zookeeperSnapRetainCount).zookeeperPurgeInterval(zookeeperPurgeInterval).fromRuntimeProperties(runtimeProperties).bootstrapTimeout(bootstrapTimeout).waitForProvision(waitForProvisioning).autoImportEnabled(!noImport).clean(clean);
    builder.version(version);
    if (containers == null || containers.isEmpty()) {
        containers = Arrays.asList(runtimeIdentity);
    }
    if (!noImport && importDir != null) {
        builder.autoImportEnabled(true);
        builder.importPath(importDir);
    }
    if (globalResolver != null) {
        if (!isInEnum(globalResolver, ResolverPolicyEnum.class)) {
            System.out.println("The globalResolver value must be one of the following: localip, localhostname, publicip, publichostname, manualip");
            return null;
        }
        builder.globalResolver(globalResolver);
        bootProperties.put(ZkDefs.GLOBAL_RESOLVER_PROPERTY, globalResolver);
    }
    if (resolver != null) {
        if (!isInEnum(resolver, ResolverPolicyEnum.class)) {
            System.out.println("The resolver value must be one of the following: localip, localhostname, publicip, publichostname, manualip");
            return null;
        }
        builder.resolver(resolver);
        bootProperties.put(ZkDefs.LOCAL_RESOLVER_PROPERTY, resolver);
    }
    if (manualIp != null) {
        builder.manualIp(manualIp);
        bootProperties.put(ZkDefs.MANUAL_IP, manualIp);
    }
    if (bindAddress != null) {
        if (!bindAddress.contains(":")) {
            builder.bindAddress(bindAddress);
            bootProperties.put(ZkDefs.BIND_ADDRESS, bindAddress);
        } else {
            String[] parts = bindAddress.split(":");
            builder.bindAddress(parts[0]);
            builder.zooKeeperServerPort(Integer.parseInt(parts[1]));
            bootProperties.put(ZkDefs.BIND_ADDRESS, parts[0]);
        }
    }
    if (zooKeeperServerPort > 0) {
        // --zookeeper-server-port option has higher priority than
        // CreateEnsembleOptions.ZOOKEEPER_SERVER_PORT and CreateEnsembleOptions.ZOOKEEPER_SERVER_CONNECTION_PORT
        // system/runtime properties
        builder.setZooKeeperServerPort(zooKeeperServerPort);
        builder.setZooKeeperServerConnectionPort(zooKeeperServerPort);
    } else {
        int shiftedPort = Ports.mapPortToRange(2181, minimumPort, maximumPort);
        builder.setZooKeeperServerPort(shiftedPort);
        builder.setZooKeeperServerConnectionPort(shiftedPort);
    }
    // Configure External Git Repository.
    if (externalGitUrl != null) {
        builder.dataStoreProperty(Constants.GIT_REMOTE_URL, externalGitUrl);
    }
    if (externalGitUser != null) {
        builder.dataStoreProperty(GIT_REMOTE_USER, externalGitUser);
    }
    if (externalGitPassword != null) {
        builder.dataStoreProperty(GIT_REMOTE_PASSWORD, externalGitPassword);
    }
    if ((externalGitUrl != null) || (externalGitUser != null) || (externalGitPassword != null)) {
        Configuration configuration = configAdmin.getConfiguration(Constants.DATASTORE_PID);
        Dictionary<String, Object> existingProperties = configuration.getProperties();
        Map<String, String> changedProperties = builder.getDataStoreProperties();
        for (String key : changedProperties.keySet()) {
            existingProperties.put(key, changedProperties.get(key));
        }
        configuration.update(existingProperties);
    }
    if (profiles != null && profiles.size() > 0) {
        builder.profiles(profiles);
    }
    if (nonManaged) {
        builder.agentEnabled(false);
    } else {
        builder.agentEnabled(true);
    }
    builder.minimumPort(minimumPort);
    builder.maximumPort(maximumPort);
    bootProperties.put(ZkDefs.MINIMUM_PORT, String.valueOf(minimumPort));
    bootProperties.put(ZkDefs.MAXIMUM_PORT, String.valueOf(maximumPort));
    newUser = newUser != null ? newUser : ShellUtils.retrieveFabricUser(session);
    newUserPassword = newUserPassword != null ? newUserPassword : ShellUtils.retrieveFabricUserPassword(session);
    if (userProps.isEmpty()) {
        String[] credentials = promptForNewUser(newUser, newUserPassword);
        newUser = credentials[0];
        newUserPassword = credentials[1];
    } else {
        if (newUser == null || newUserPassword == null) {
            newUser = "" + userProps.keySet().iterator().next();
            newUserPassword = "" + userProps.get(newUser);
            if (newUserPassword.contains(ROLE_DELIMITER)) {
                newUserPassword = newUserPassword.substring(0, newUserPassword.indexOf(ROLE_DELIMITER));
            }
        }
        String passwordWithroles = userProps.get(newUser);
        if (passwordWithroles != null && passwordWithroles.contains(ROLE_DELIMITER)) {
            String[] infos = passwordWithroles.split(",");
            String oldUserRole = "";
            if (newUserIsAdmin(infos)) {
                newUserRole = "_g_:admin";
                oldUserRole = newUserRole;
            }
            for (int i = 1; i < infos.length; i++) {
                if (infos[i].trim().startsWith(BackingEngine.GROUP_PREFIX)) {
                    // it's a group reference
                    String groupInfo = (String) userProps.get(infos[i].trim());
                    if (groupInfo != null) {
                        String[] roles = groupInfo.split(",");
                        for (int j = 1; j < roles.length; j++) {
                            if (!roles[j].trim().equals(oldUserRole)) {
                                if (!newUserRole.isEmpty()) {
                                    newUserRole = newUserRole + ROLE_DELIMITER + roles[j].trim();
                                } else {
                                    newUserRole = roles[j].trim();
                                }
                            }
                        }
                    }
                } else {
                    // it's an user reference
                    if (!infos[i].trim().equals(oldUserRole)) {
                        if (!newUserRole.isEmpty()) {
                            newUserRole = newUserRole + ROLE_DELIMITER + infos[i].trim();
                        } else {
                            newUserRole = infos[i].trim();
                        }
                    }
                }
            }
        }
    }
    if (Strings.isNullOrEmpty(newUser)) {
        System.out.println("No user specified. Cannot create a new fabric ensemble.");
        return null;
    }
    StringBuilder sb = new StringBuilder();
    // session is unset when this is called from FMC
    if (session != null) {
        ShellUtils.storeFabricCredentials(session, newUser, newUserPassword);
    }
    if (generateZookeeperPassword) {
    // do nothing use the generated password.
    } else if (zookeeperPassword == null) {
        zookeeperPassword = PasswordEncoder.decode(runtimeProperties.getProperty(CreateEnsembleOptions.ZOOKEEPER_PASSWORD, PasswordEncoder.encode(newUserPassword)));
        builder.zookeeperPassword(zookeeperPassword);
    } else {
        builder.zookeeperPassword(zookeeperPassword);
    }
    OsgiUtils.updateCmConfigurationAndWait(bundleContext, bootConfiguration, bootProperties, bootstrapTimeout, TimeUnit.MILLISECONDS);
    String roleToAssign = newUserRole.isEmpty() ? "_g_:admin" : newUserRole;
    CreateEnsembleOptions options = builder.users(userProps).withUser(newUser, newUserPassword, roleToAssign).build();
    if (containers.size() == 1 && containers.contains(runtimeIdentity)) {
        bootstrap.create(options);
    } else {
        ServiceProxy<ZooKeeperClusterService> serviceProxy = ServiceProxy.createServiceProxy(bundleContext, ZooKeeperClusterService.class);
        try {
            serviceProxy.getService().createCluster(containers, options);
        } finally {
            serviceProxy.close();
        }
    }
    ShellUtils.storeZookeeperPassword(session, options.getZookeeperPassword());
    if (zookeeperPassword == null && !generateZookeeperPassword) {
        sb.append("Zookeeper password: (reusing users ").append(newUser).append(" password:").append(options.getZookeeperPassword()).append(")\n");
        sb.append("(You can use the --zookeeper-password / --generate-zookeeper-password option to specify one.)\n");
    } else if (generateZookeeperPassword) {
        sb.append("Generated zookeeper password:").append(options.getZookeeperPassword());
    }
    System.out.println(sb.toString());
    if (!nonManaged && !waitForProvisioning) {
        System.out.println("It may take a couple of seconds for the container to provision...");
        System.out.println("You can use the --wait-for-provisioning option, if you want this command to block until the container is provisioned.");
    }
    return null;
}
Also used : Path(java.nio.file.Path) Configuration(org.osgi.service.cm.Configuration) BootstrapConfiguration(io.fabric8.zookeeper.bootstrap.BootstrapConfiguration) ResolverPolicyEnum(io.fabric8.boot.commands.support.ResolverPolicyEnum) Properties(org.apache.felix.utils.properties.Properties)

Example 80 with Repository

use of io.fabric8.agent.model.Repository in project fabric8 by jboss-fuse.

the class ProjectDeployerImpl method addAvailableFeaturesFromProfile.

protected void addAvailableFeaturesFromProfile(Collection<Feature> allFeatures, FabricService fabric, Profile overlay) throws Exception {
    for (String repoUriWithExpressions : overlay.getRepositories()) {
        String repoUri = VersionPropertyPointerResolver.replaceVersions(fabric, overlay.getConfigurations(), repoUriWithExpressions);
        Repository repo = new Repository(URI.create(repoUri));
        repo.load();
        allFeatures.addAll(Arrays.asList(repo.getFeatures()));
    }
}
Also used : Repository(io.fabric8.agent.model.Repository)

Aggregations

File (java.io.File)49 Test (org.junit.Test)32 Git (org.eclipse.jgit.api.Git)30 GitPatchRepository (io.fabric8.patch.management.impl.GitPatchRepository)27 IOException (java.io.IOException)24 GitPatchManagementServiceImpl (io.fabric8.patch.management.impl.GitPatchManagementServiceImpl)20 HashMap (java.util.HashMap)15 ObjectId (org.eclipse.jgit.lib.ObjectId)13 MalformedURLException (java.net.MalformedURLException)11 Map (java.util.Map)11 ArrayList (java.util.ArrayList)10 PatchException (io.fabric8.patch.management.PatchException)9 RevCommit (org.eclipse.jgit.revwalk.RevCommit)8 Bundle (org.osgi.framework.Bundle)8 Version (org.osgi.framework.Version)8 MavenResolver (io.fabric8.maven.MavenResolver)7 HashSet (java.util.HashSet)7 Repository (io.fabric8.agent.model.Repository)6 BundleContext (org.osgi.framework.BundleContext)6 Feature (io.fabric8.agent.model.Feature)5