/**
   * Add the up2date package to a system and a channel. Version should be specified such as "2.9.0"
   *
   * @param userIn
   * @param s
   * @param version
   * @param c
   * @return
   * @throws Exception
   */
  public static Package addUp2dateToSystemAndChannel(
      User userIn, Server s, String version, Channel c) throws Exception {

    Package p = null;
    PackageName pn = PackageFactory.lookupOrCreatePackageByName("up2date");
    if (pn != null) {
      List packages = PackageFactory.listPackagesByPackageName(pn);
      Iterator i = packages.iterator();
      while (i.hasNext()) {
        Package innerp = (Package) i.next();
        PackageEvr evr = innerp.getPackageEvr();
        if (evr != null && evr.getVersion().equals(version)) {
          p = innerp;
        }
      }
    }
    if (p == null) {
      p = PackageManagerTest.addPackageToSystemAndChannel("up2date", s, c);
      PackageEvr pevr = p.getPackageEvr();
      pevr.setEpoch("0");
      pevr.setVersion(version);
      pevr.setRelease("0");
      TestUtils.saveAndFlush(p);
      TestUtils.saveAndFlush(pevr);
    } else {
      PackageManagerTest.associateSystemToPackage(s, p);
    }

    return p;
  }
예제 #2
0
 /**
  * Looup a package provider by name
  *
  * @param name the name
  * @return the package provider
  */
 public static PackageProvider lookupPackageProvider(String name) {
   Map<String, Object> params = new HashMap<String, Object>();
   params.put("name", name);
   PackageProvider prov =
       (PackageProvider) singleton.lookupObjectByNamedQuery("PackageProvider.findByName", params);
   return prov;
 }
예제 #3
0
 /**
  * Lookup a package key object
  *
  * @param key the key to lookup
  * @return the package key
  */
 public static PackageKey lookupPackageKey(String key) {
   Map<String, Object> params = new HashMap<String, Object>();
   params.put("key", key);
   PackageKey prov =
       (PackageKey) singleton.lookupObjectByNamedQuery("PackageKey.findByKey", params);
   return prov;
 }
예제 #4
0
  /**
   * Find other packages with the same NVRE but with different arches
   *
   * @param pack the package
   * @return List of package objects
   */
  public static List<Package> findPackagesWithDifferentArch(Package pack) {
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("evr", pack.getPackageEvr());
    params.put("name", pack.getPackageName());
    params.put("arch", pack.getPackageArch());

    return singleton.listObjectsByNamedQuery("Package.findOtherArches", params);
  }
예제 #5
0
  /**
   * lookup a PackageName object based on it's name, If one does not exist, create a new one and
   * return it.
   *
   * @param pn the package name
   * @return a PackageName object that has a matching name
   */
  public static synchronized PackageName lookupOrCreatePackageByName(String pn) {
    PackageName returned = lookupPackageName(pn);

    if (returned == null) {
      PackageName newName = new PackageName();
      newName.setName(pn);
      singleton.saveObject(newName);
      return newName;
    }
    return returned;
  }
  public void testDeletePackages() throws Exception {
    // Configuration
    final int numPackagesToDelete = 50;

    // Setup
    user.addRole(RoleFactory.ORG_ADMIN);

    Set<Long> doomedPackageIds = new HashSet<Long>(numPackagesToDelete);
    for (int ii = 0; ii < numPackagesToDelete; ii++) {
      Package pack = PackageTest.createTestPackage(user.getOrg());
      doomedPackageIds.add(pack.getId());
    }

    int numPackagesBeforeDelete = PackageFactory.lookupOrphanPackages(user.getOrg()).size();
    assertTrue(numPackagesBeforeDelete >= numPackagesToDelete);

    // Test
    PackageManager.deletePackages(doomedPackageIds, user);

    // Verify
    int numPackagesAfterDelete = PackageFactory.lookupOrphanPackages(user.getOrg()).size();
    assertEquals(numPackagesBeforeDelete - numPackagesToDelete, numPackagesAfterDelete);
  }
  /**
   * Create a package with the given name and add it to the given channel. If a package by that name
   * already exists, this simply returns that package.
   *
   * @param packageName The name of the package to create.
   * @param c The channel to which to add the package
   * @return The package with that name in the channel.
   * @throws Exception
   */
  public static Package addPackageToChannel(String packageName, Channel c) throws Exception {

    PackageName pn = PackageFactory.lookupOrCreatePackageByName(packageName);
    if (pn == null) {
      pn = PackageNameTest.createTestPackageName();
      pn.setName(packageName);
    }

    Long existingId = ChannelManager.getLatestPackageEqual(c.getId(), packageName);

    if (existingId != null) {
      return PackageFactory.lookupByIdAndOrg(existingId, c.getOrg());
    }

    // existingId =
    Session session = HibernateFactory.getSession();
    Query query =
        session.createQuery(
            "from Package as "
                + "package where package.org.id = "
                + c.getOrg().getId()
                + " and package.packageName.id = "
                + pn.getId());
    List packages = query.list();
    Package retval = null;
    if (packages != null && packages.size() > 0) {
      retval = (Package) packages.get(0);
    } else {
      retval = PackageTest.createTestPackage(c.getOrg());
    }

    retval.setPackageName(pn);
    TestUtils.saveAndFlush(retval);
    PackageTest.addPackageToChannelNewestPackage(retval, c);

    return retval;
  }
예제 #8
0
  /**
   * Returns an InstalledPackage object, given a server and package name to lookup the latest
   * version of the package. Return null if the package doesn;t exist.
   *
   * @param name name of the package to lookup on
   * @param server server where the give package was installed.
   * @return the InstalledPackage with the given package name for the given server
   */
  public static InstalledPackage lookupByNameAndServer(String name, Server server) {
    PackageName packName = lookupPackageName(name);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("server", server);
    params.put("name", packName);

    List<InstalledPackage> original =
        singleton.listObjectsByNamedQuery("InstalledPackage.lookupByServerAndName", params);
    if (original.isEmpty()) {
      return null;
    }
    if (original.size() == 1) {
      return original.get(0);
    }
    List<InstalledPackage> packs = new LinkedList<InstalledPackage>();
    packs.addAll(original);
    Collections.sort(packs);
    return packs.get(packs.size() - 1);
  }
예제 #9
0
  public void testAddPackage() throws Exception {
    Channel c = ChannelFactoryTest.createTestChannel(user);
    Package p = PackageTest.createTestPackage(user.getOrg());
    assertNotNull(c);
    assertEquals("channel-ia32", c.getChannelArch().getLabel());
    assertNotNull(p);
    assertEquals("noarch", p.getPackageArch().getLabel());

    try {
      c.addPackage(p);
    } catch (Exception e) {
      fail("noarch should be acceptible in an ia32 channel");
    }

    try {
      PackageArch pa = PackageFactory.lookupPackageArchByLabel("x86_64");
      assertNotNull(pa);
      p.setPackageArch(pa);
      c.addPackage(p);
      fail("x86_64 is not acceptible in an ia32 channel");
    } catch (Exception e) {
      // expected.
    }
  }
예제 #10
0
 /**
  * Lookup a Package by its ID
  *
  * @param id to search for
  * @return the Package found
  */
 private static Package lookupById(Long id) {
   Map<String, Object> params = new HashMap<String, Object>();
   params.put("id", id);
   return (Package) singleton.lookupObjectByNamedQuery("Package.findById", params);
 }
예제 #11
0
 /**
  * List all package keys
  *
  * @return list of package key objects
  */
 public static List<PackageKey> listPackageKeys() {
   Map<String, Object> params = new HashMap<String, Object>();
   List<PackageKey> prov = singleton.listObjectsByNamedQuery("PackageKey.listKeys", params);
   return prov;
 }
  /** {@inheritDoc} */
  public ActionForward execute(
      ActionMapping mapping,
      ActionForm formIn,
      HttpServletRequest request,
      HttpServletResponse response) {

    RequestContext requestContext = new RequestContext(request);
    User user = requestContext.getLoggedInUser();

    // If this is an easy one and we have the pid
    if (request.getParameter("pid") != null) {
      long pid = requestContext.getRequiredParam("pid");
      Package pkg = PackageFactory.lookupByIdAndUser(pid, user);

      // show permission error if pid is invalid like we did before
      if (pkg == null) {
        throw new PermissionException("Invalid pid");
      }

      if (pkg instanceof Patch) {
        request.setAttribute("type", "patch");
        request.setAttribute(PACKAGE_NAME, pkg.getPackageName().getName());
        request.setAttribute(
            "readme_url", DownloadManager.getPatchReadmeDownloadPath((Patch) pkg, user));
      } else if (pkg instanceof PatchSet) {
        request.setAttribute("type", "patchset");
        request.setAttribute(PACKAGE_NAME, pkg.getNameEvra());
        request.setAttribute(
            "readme_url", DownloadManager.getPatchSetReadmeDownloadPath((PatchSet) pkg, user));
      } else {
        request.setAttribute("type", "rpm");
        request.setAttribute(PACKAGE_NAME, pkg.getFilename());
        if (!pkg.getPackageKeys().isEmpty()) {
          request.setAttribute(PACKAGE_KEY, pkg.getPackageKeys().iterator().next().getKey());
        }
        boolean isDebug = pkg.getPackageName().getName().contains("debuginfo");

        request.setAttribute("isDebuginfo", isDebug);
        if (!isDebug) {
          Package debugPkg = PackageManager.findDebugInfo(user, pkg);
          String ftpUrl = PackageManager.generateFtpDebugPath(pkg);
          if (debugPkg != null) {
            request.setAttribute(
                "debugUrl", DownloadManager.getPackageDownloadPath(debugPkg, user));
          } else if (ftpUrl != null) {
            request.setAttribute("debugUrl", ftpUrl);
            request.setAttribute("debugFtp", true);
          }
        }
      }

      if (DownloadManager.isFileAvailable(pkg.getPath())) {
        request.setAttribute("url", DownloadManager.getPackageDownloadPath(pkg, user));
      }

      List<PackageSource> src = PackageFactory.lookupPackageSources(pkg);

      if (!src.isEmpty() && DownloadManager.isFileAvailable(src.get(0).getPath())) {
        request.setAttribute(
            "srpm_url", DownloadManager.getPackageSourceDownloadPath(pkg, src.get(0), user));
        request.setAttribute("srpm_path", src.get(0).getFile());
      }

      request.setAttribute("pack", pkg);
      // description can be null.
      if (pkg.getDescription() != null) {
        request.setAttribute("description", pkg.getDescription().replace("\n", "<BR>\n"));
      } else {
        request.setAttribute("description", pkg.getDescription());
      }
      request.setAttribute("packArches", PackageFactory.findPackagesWithDifferentArch(pkg));
      request.setAttribute("pid", pid);

      return mapping.findForward("default");
    } else { // we have to guess
      PackageListItem item = PackageListItem.parse(request.getParameter("id_combo"));
      Package pkg;
      long nameId = item.getIdOne();
      long evrId = item.getIdTwo();
      long archId = 0;
      if (item.getIdThree() != null) {
        archId = item.getIdThree();
      }

      Long cid = requestContext.getParamAsLong("cid");
      Long sid = requestContext.getParamAsLong("sid");
      if (cid != null) {
        pkg = PackageManager.guestimatePackageByChannel(cid, nameId, evrId, user.getOrg());

      } else if (sid != null) {
        pkg = PackageManager.guestimatePackageBySystem(sid, nameId, evrId, archId, user.getOrg());

      } else {
        throw new BadParameterException("pid, cid, or sid");
      }

      // show permission error if pid is invalid like we did before
      if (pkg == null) {
        throw new NoSuchPackageException();
      }

      Map params = new HashMap();
      params.put("pid", pkg.getId());
      return getStrutsDelegate().forwardParams(mapping.findForward("package"), params);
    }
  }
예제 #13
0
 /**
  * Store the package provider.
  *
  * @param prov The object we are commiting.
  */
 public static void save(PackageProvider prov) {
   singleton.saveObject(prov);
 }
예제 #14
0
 /**
  * list package providers
  *
  * @return list of package providers
  */
 public static List<PackageProvider> listPackageProviders() {
   Map<String, Object> params = new HashMap<String, Object>();
   List<PackageProvider> list =
       singleton.listObjectsByNamedQuery("PackageProvider.listProviders", params);
   return list;
 }
예제 #15
0
 /**
  * Store the package delta.
  *
  * @param delta The object we are commiting.
  */
 public static void save(PackageDelta delta) {
   singleton.saveObject(delta);
 }
예제 #16
0
  /**
   * Lookup package sources for a particular package
   *
   * @param pack the package associated with the package sources
   * @return the list of package source objects
   */
  public static List<PackageSource> lookupPackageSources(Package pack) {
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("pack", pack);

    return singleton.listObjectsByNamedQuery("PackageSource.findByPackage", params);
  }
예제 #17
0
 /**
  * Lookup a PackageArch by its id.
  *
  * @param id package arch label id sought.
  * @return the PackageArch whose id matches the given id.
  */
 public static PackageArch lookupPackageArchById(Long id) {
   Map<String, Object> params = new HashMap<String, Object>();
   params.put("id", id);
   return (PackageArch) singleton.lookupObjectByNamedQuery("PackageArch.findById", params, true);
 }
예제 #18
0
 /**
  * Lookup a PackageArch by its label.
  *
  * @param label package arch label sought.
  * @return the PackageArch whose label matches the given label.
  */
 public static PackageArch lookupPackageArchByLabel(String label) {
   Map<String, Object> params = new HashMap<String, Object>();
   params.put("label", label);
   return (PackageArch)
       singleton.lookupObjectByNamedQuery("PackageArch.findByLabel", params, true);
 }
  // Check to make sure up2date is 2.9.0
  protected ValidatorError validateUp2dateVersion() {
    Server hostServer = getHostServer();
    List packages = PackageManager.systemPackageList(hostServer.getId(), null);
    if (packages != null) {
      log.debug("    packages.size() : " + packages.size());
    }
    // PackageListItem
    Iterator i = packages.iterator();
    String up2dateepoch = null;
    String up2dateversion = null;
    String up2daterelease = null;

    while (i.hasNext()) {
      PackageListItem pli = (PackageListItem) i.next();
      if (pli.getName().equals("yum-rhn-plugin")) {
        // found yum-rhn-plugin - returning
        return null;
      }
      if (pli.getName().equals("zypp-plugin-spacewalk")) {
        // found zypp-plugin-spacewalk - returning
        return null;
      }

      if (pli.getName().equals("up2date")) {
        log.debug("    found up2date ...");
        up2dateepoch = pli.getEpoch();
        up2dateversion = pli.getVersion();
        up2daterelease = pli.getRelease();

        log.debug("    e: " + up2dateepoch + " v: " + up2dateversion + " r : " + up2daterelease);
      }
    }

    if (up2dateepoch == null && up2dateversion == null && up2daterelease == null) {
      Object[] args = new Object[2];
      args[0] = hostServer.getId();
      args[1] = hostServer.getName();
      return new ValidatorError("kickstart.schedule.noup2date", args);
    }

    up2dateepoch = up2dateepoch == null ? "0" : up2dateepoch;
    up2dateversion = up2dateversion == null ? "0" : up2dateversion;
    up2daterelease = up2daterelease == null ? "0" : up2daterelease;

    int comp =
        PackageManager.verCmp(
            up2dateepoch, up2dateversion, up2daterelease, "0", UP2DATE_VERSION, "0");
    log.debug("    Got back comp from verCmp: " + comp);
    if (comp < 0) {
      Long packageId =
          PackageManager.getServerNeededUpdatePackageByName(hostServer.getId(), "up2date");
      if (packageId == null) {
        Object[] args = new Object[2];
        args[0] = UP2DATE_VERSION;
        args[1] = up2dateversion;
        return new ValidatorError("kickstart.schedule.noup2dateinchannel", args);
      }
      Package p = PackageFactory.lookupByIdAndUser(packageId, this.user);

      Map evrmap = new HashMap();
      evrmap.put("name_id", p.getPackageName().getId());
      evrmap.put("evr_id", p.getPackageEvr().getId());
      evrmap.put("arch_id", p.getPackageArch().getId());
      packagesToInstall.add(evrmap);
    } else {
      return null;
    }

    return null;
  }
예제 #20
0
 /**
  * Lookup a package key type by label
  *
  * @param label the label of the type
  * @return the key type
  */
 public static PackageKeyType lookupKeyTypeByLabel(String label) {
   Map<String, Object> params = new HashMap<String, Object>();
   params.put("label", label);
   return (PackageKeyType)
       singleton.lookupObjectByNamedQuery("PackageKeyType.findByLabel", params);
 }