private ActionMessages processForm(
      User user, Server server, DynaActionForm f, HttpServletRequest request) {
    if (log.isDebugEnabled()) {
      log.debug("Processing form.");
    }

    // scheduleScriptRun takes a list of servers
    List servers = new ArrayList();
    servers.add(server);

    ActionMessages msgs = new ActionMessages();

    Boolean submitted = (Boolean) f.get("submitted");
    String runBefore = (String) f.get("run_script");
    String username = (String) f.get("username");
    String group = (String) f.get("group");
    Long timeout = (Long) f.get("timeout");
    String script = (String) f.get("script");
    String sessionSetLabel = (String) f.get("session_set_label");
    String mode = (String) f.get("mode");

    if (log.isDebugEnabled()) {
      log.debug("submitted [" + submitted + "]");
      log.debug("runBefore [" + runBefore + "]");
      log.debug("username [" + username + "]");
      log.debug("group [" + group + "]");
      log.debug("timeout [" + timeout + "]");
      log.debug("script [" + script + "]");
      log.debug("mode [" + mode + "]");
    }

    // The earliest time to perform the action.
    Date earliest = getStrutsDelegate().readDatePicker(f, "date", DatePicker.YEAR_RANGE_POSITIVE);

    if (BEFORE.equals(runBefore)) {
      ScriptActionDetails sad = ActionManager.createScript(username, group, timeout, script);
      ScriptRunAction sra = ActionManager.scheduleScriptRun(user, servers, "", sad, earliest);
      List<Map<String, Long>> packs = getPackages(user, request, sessionSetLabel);
      int numPackages = packs.size();
      PackageAction pa = schedulePackageAction(user, server, packs, mode, earliest);
      pa.setPrerequisite(sra);
      ActionManager.storeAction(pa);
      showMessages(msgs, pa, server, numPackages, mode);
      showRemoteCommandMsg(msgs, true, mode);
    } else {
      List<Map<String, Long>> packs = getPackages(user, request, sessionSetLabel);
      int numPackages = packs.size();
      PackageAction pa = schedulePackageAction(user, server, packs, mode, earliest);
      ScriptActionDetails sad = ActionManager.createScript(username, group, timeout, script);
      ScriptRunAction sra = ActionManager.scheduleScriptRun(user, servers, "", sad, earliest);
      sra.setPrerequisite(pa);
      ActionManager.storeAction(sra);
      showMessages(msgs, sra, server, numPackages, mode);
      showRemoteCommandMsg(msgs, false, mode);
    }
    SessionSetHelper.obliterate(request, sessionSetLabel);
    return msgs;
  }
  private PackageAction syncToVictim(
      RequestContext requestContext, Long sid, Set pkgIdCombos, String option) {

    PackageAction pa = null;
    Date time = new Date(requestContext.getParamAsLong("time"));

    if (isProfileSync(requestContext)) {
      Long prid = requestContext.getRequiredParam("prid");

      pa =
          ProfileManager.syncToProfile(
              requestContext.getCurrentUser(), sid, prid, pkgIdCombos, option, time);

      if (pa == null) {
        createMessage(requestContext.getRequest(), "message.nopackagestosync");
        return null;
      }

      List args = new ArrayList();
      args.add(sid.toString());
      args.add(pa.getId().toString());
      args.add(requestContext.lookupAndBindServer().getName());
      args.add(
          ProfileManager.lookupByIdAndOrg(prid, requestContext.getCurrentUser().getOrg())
              .getName());

      createMessage(requestContext.getRequest(), "message.syncpackages", args);
    } else if (isSystemSync(requestContext)) {
      Long sid1 = requestContext.getRequiredParam("sid_1");
      pa =
          ProfileManager.syncToSystem(
              requestContext.getCurrentUser(), sid, sid1, pkgIdCombos, option, time);

      if (pa == null) {
        createMessage(requestContext.getRequest(), "message.nopackagestosync");
        return null;
      }

      List args = new ArrayList();
      args.add(sid.toString());
      args.add(pa.getId().toString());
      args.add(requestContext.lookupAndBindServer().getName());
      args.add(SystemManager.lookupByIdAndUser(sid1, requestContext.getCurrentUser()).getName());

      createMessage(requestContext.getRequest(), "message.syncpackages", args);
    }

    addHardwareMessage(pa, requestContext);

    return pa;
  }
예제 #3
0
 protected void addHardwareMessage(PackageAction pa, RequestContext rctx) {
   // If we scheduled a hardware refresh too
   if (pa != null && pa.getPrerequisite() != null) {
     // NOTE: Hardware refresh has been scheduled for
     // cascade.sfbay.redhat.com to be run before the
     // package profile sync.  This is required to verify that the
     // system has the ability to compare packages.
     List hwargs = new ArrayList();
     hwargs.add(rctx.lookupAndBindServer().getId().toString());
     hwargs.add(pa.getPrerequisite().toString());
     hwargs.add(rctx.lookupAndBindServer().getName());
     createMessage(rctx.getRequest(), "message.hardwarerefresh", hwargs);
   }
 }
예제 #4
0
  public void testCreatePackageUpdateActionWithName() throws Exception {
    User usr = UserTestUtils.findNewUser("testUser", "testOrg" + this.getClass().getSimpleName());

    PackageAction testAction =
        (PackageAction) ActionFactoryTest.createAction(usr, ActionFactory.TYPE_PACKAGES_UPDATE);
    PackageActionDetailsTest.createTestDetailsWithName(usr, testAction);
    ActionFactory.save(testAction);
    flushAndEvict(testAction);
    /** Get action back out of db and make sure it committed correctly */
    Action same = ActionFactory.lookupById(testAction.getId());
    assertTrue(same instanceof PackageAction);
    PackageAction sameAction = (PackageAction) same;

    assertNotNull(sameAction.getDetails());
    assertEquals(sameAction.getDetails().size(), 2);
    assertNotNull(sameAction.getDetails().toArray()[0]);
    assertNotNull(sameAction.getDetails().toArray()[1]);
    assertEquals(sameAction.getName(), testAction.getName());
    assertEquals(sameAction.getId(), testAction.getId());
  }
예제 #5
0
  public void testCreatePackageRemoveAction() throws Exception {
    User user = UserTestUtils.findNewUser("testUser", "testOrg" + this.getClass().getSimpleName());
    Server srvr = ServerFactoryTest.createTestServer(user);

    ServerAction sa = new ServerAction();
    sa.setStatus(ActionFactory.STATUS_QUEUED);
    sa.setRemainingTries(new Long(10));
    sa.setServer(srvr);
    log.debug("Creating PackageRemoveAction.");
    PackageAction pra =
        (PackageAction) ActionFactory.createAction(ActionFactory.TYPE_PACKAGES_REMOVE);
    pra.setOrg(user.getOrg());
    pra.setName("Package Removal");
    pra.addServerAction(sa);
    sa.setParentAction(pra);
    log.debug("Committing PackageRemoveAction.");
    ActionFactory.save(pra);

    PackageAction result = (PackageAction) ActionFactory.lookupById(pra.getId());

    assertEquals(pra, result);
  }
예제 #6
0
  /**
   * Test fetching a PackageAction
   *
   * @throws Exception
   */
  public void testLookupPackageAction() throws Exception {

    Action newA =
        ActionFactoryTest.createAction(
            UserTestUtils.createUser(
                "testUser", UserTestUtils.createOrg("testOrg" + this.getClass().getSimpleName())),
            ActionFactory.TYPE_PACKAGES_VERIFY);
    assertNotNull(newA.getId());
    assertTrue(newA instanceof PackageAction);
    PackageAction p = (PackageAction) newA;
    assertNotNull(p.getDetails());
    assertEquals(p.getDetails().size(), 1);
    PackageActionDetails firstDetail = (PackageActionDetails) p.getDetails().toArray()[0];

    /** Make sure PackageEvr was set & committed correctly */
    Set details = p.getDetails();
    Iterator ditr = details.iterator();
    while (ditr.hasNext()) {
      PackageActionDetails detail = (PackageActionDetails) ditr.next();
      assertNotNull(detail.getEvr().getId());
    }

    User user = UserTestUtils.findNewUser("TEST USER", "TEST ORG");
    Server testserver = ServerFactoryTest.createTestServer(user);

    PackageActionResult result = new PackageActionResult();
    result.setServer(testserver);
    result.setDetails(firstDetail);
    result.setResultCode(new Long(42));
    result.setCreated(new Date());
    result.setModified(new Date());

    firstDetail.addResult(result);

    ActionFactory.save(p);

    PackageAction p2 = (PackageAction) ActionFactory.lookupById(p.getId());

    assertNotNull(p2.getDetails());
    assertEquals(p2.getDetails().size(), 1);
    assertNotNull(p2.getDetails().toArray()[0]);
    firstDetail = (PackageActionDetails) p2.getDetails().toArray()[0];
    assertNotNull(firstDetail.getResults());
    assertEquals(firstDetail.getResults().size(), 1);
  }
  /**
   * Executes the appropriate PackageAction
   *
   * @param mapping ActionMapping
   * @param formIn ActionForm
   * @param request ServletRequest
   * @param response ServletResponse
   * @return The ActionForward to go to next.
   */
  public ActionForward executePackageAction(
      ActionMapping mapping,
      ActionForm formIn,
      HttpServletRequest request,
      HttpServletResponse response) {

    RequestContext requestContext = new RequestContext(request);
    StrutsDelegate strutsDelegate = getStrutsDelegate();

    Long sid = requestContext.getRequiredParam("sid");
    User user = requestContext.getCurrentUser();
    // updateList(newactions, user.getId());

    List<Map<String, Long>> data = PackageListItem.toKeyMaps(getDataResult(request));
    int numPackages = data.size();

    // Archive the actions
    Server server = SystemManager.lookupByIdAndUser(sid, user);

    // The earliest time to perform the action.
    DynaActionForm dynaActionForm = (DynaActionForm) formIn;
    Date earliest =
        getStrutsDelegate().readDatePicker(dynaActionForm, "date", DatePicker.YEAR_RANGE_POSITIVE);

    // The action chain to append this action to, if any
    ActionChain actionChain = ActionChainHelper.readActionChain(dynaActionForm, user);

    PackageAction pa = schedulePackageAction(formIn, requestContext, data, earliest, actionChain);

    // Remove the actions from the users set
    SessionSetHelper.obliterate(request, getDecl(sid));

    ActionMessages msgs = new ActionMessages();

    if (actionChain == null) {
      /**
       * If there was only one action archived, display the "action" archived message, else display
       * the "actions" archived message.
       */
      if (numPackages == 1) {
        msgs.add(
            ActionMessages.GLOBAL_MESSAGE,
            new ActionMessage(
                getMessageKeyForOne(),
                LocalizationService.getInstance().formatNumber(numPackages),
                pa.getId().toString(),
                sid.toString(),
                StringUtil.htmlifyText(server.getName())));
      } else {
        msgs.add(
            ActionMessages.GLOBAL_MESSAGE,
            new ActionMessage(
                getMessageKeyForMany(),
                LocalizationService.getInstance().formatNumber(numPackages),
                pa.getId().toString(),
                sid.toString(),
                StringUtil.htmlifyText(server.getName())));
      }
    } else {
      msgs.add(
          ActionMessages.GLOBAL_MESSAGE,
          new ActionMessage(
              "message.addedtoactionchain",
              actionChain.getId(),
              StringUtil.htmlifyText(actionChain.getLabel())));
    }

    strutsDelegate.saveMessages(request, msgs);
    Map params = new HashMap();
    processParamMap(formIn, request, params);
    return strutsDelegate.forwardParams(mapping.findForward(RhnHelper.CONFIRM_FORWARD), params);
  }