java/code/src/com/redhat/rhn/domain/task/Task.hbm.xml
| 9
java/code/src/com/redhat/rhn/domain/task/TaskFactory.java
| 21 ++
java/code/src/com/redhat/rhn/domain/task/test/TaskTest.java
| 15 +
java/code/src/com/redhat/rhn/manager/BaseTransactionCommand.java
| 75 +++++++
java/code/src/com/redhat/rhn/manager/errata/cache/UpdateErrataCacheCommand.java
| 41 ---
java/code/src/com/redhat/rhn/manager/satellite/UpgradeCommand.java
| 105 ++++++++++
java/code/src/com/redhat/rhn/manager/satellite/test/UpgradeCommandTest.java
| 59 +++++
java/code/src/com/redhat/rhn/webapp/RhnServletListener.java
| 12 +
8 files changed, 299 insertions(+), 38 deletions(-)
New commits:
commit 8f94b49ce1dd35671f627bd4161e6355631d00f1
Author: Mike McCune<mmcc...@redhat.com>
Date: Tue Jul 21 14:22:17 2009 -0700
512814 - adding spot to add 'upgrade' logic to our startup of tomact.
Now coders can add an entry to rhnTaskQueue to indicate to our Java code
that we want to run a set of business logic during an upgrade.
diff --git a/java/code/src/com/redhat/rhn/domain/task/Task.hbm.xml
b/java/code/src/com/redhat/rhn/domain/task/Task.hbm.xml
index 4b32495..276b5e8 100644
--- a/java/code/src/com/redhat/rhn/domain/task/Task.hbm.xml
+++ b/java/code/src/com/redhat/rhn/domain/task/Task.hbm.xml
@@ -33,4 +33,13 @@ PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
and task_data = :data and earliest = :earliest]]>
</query>
+<sql-query name="Task.lookupByNameLike">
+<![CDATA[SELECT {t.*}
+ FROM rhnTaskQueue {t}
+ WHERE t.task_name like :namelike
+ ]]>
+<return alias="t" class="com.redhat.rhn.domain.task.Task" />
+</sql-query>
+
+
</hibernate-mapping>
diff --git a/java/code/src/com/redhat/rhn/domain/task/TaskFactory.java
b/java/code/src/com/redhat/rhn/domain/task/TaskFactory.java
index 70c71f1..50149eb 100644
--- a/java/code/src/com/redhat/rhn/domain/task/TaskFactory.java
+++ b/java/code/src/com/redhat/rhn/domain/task/TaskFactory.java
@@ -74,6 +74,16 @@ public class TaskFactory extends HibernateFactory {
}
/**
+ * Remove a completed Task from the queue.
+ *
+ * @param taskIn to remove
+ */
+ public static void remove(Task taskIn) {
+ singleton.removeObject(taskIn);
+ }
+
+
+ /**
* Lookups up a task.
* @param org The org containing the task
* @param name The name of the task
@@ -126,4 +136,15 @@ public class TaskFactory extends HibernateFactory {
return c.list();
}
+
+ /**
+ * Lookup a list of Tasks who's name start with passed in param
+ * @param nameIn to lookup
+ * @return List of Tasks or null if not found.
+ */
+ public static List getTaskListByNameLike(String nameIn) {
+ return
HibernateFactory.getSession().getNamedQuery("Task.lookupByNameLike")
+ .setString("namelike", nameIn + "%")
+ .list();
+ }
}
diff --git a/java/code/src/com/redhat/rhn/domain/task/test/TaskTest.java
b/java/code/src/com/redhat/rhn/domain/task/test/TaskTest.java
index 2d9333f..c4559d1 100644
--- a/java/code/src/com/redhat/rhn/domain/task/test/TaskTest.java
+++ b/java/code/src/com/redhat/rhn/domain/task/test/TaskTest.java
@@ -24,6 +24,8 @@ import com.redhat.rhn.testing.UserTestUtils;
import org.hibernate.Session;
+import java.util.List;
+
/**
* TaskTest
* @version $Rev$
@@ -61,4 +63,17 @@ public class TaskTest extends RhnBaseTestCase {
t3.setName("foo");
assertFalse("t2 should not be equal to t3", t2.equals(t3));
}
+
+ public void testLookupNameLike() throws Exception {
+ Org org = UserTestUtils.findNewOrg("testOrg");
+ String testname = "task_object_unit_test_" + TestUtils.randomString();
+ Long testdata = new Long(42);
+ Task t = TaskFactory.createTask(org, testname, testdata);
+
+ List lookedup =
TaskFactory.getTaskListByNameLike("task_object_unit_test_");
+ assertNotNull(lookedup);
+ assertTrue(lookedup.size()> 0);
+ assertTrue(lookedup.get(0) != null);
+ assertTrue(lookedup.get(0) instanceof Task);
+ }
}
diff --git a/java/code/src/com/redhat/rhn/manager/BaseTransactionCommand.java
b/java/code/src/com/redhat/rhn/manager/BaseTransactionCommand.java
new file mode 100644
index 0000000..1f47b46
--- /dev/null
+++ b/java/code/src/com/redhat/rhn/manager/BaseTransactionCommand.java
@@ -0,0 +1,75 @@
+/**
+ * Copyright (c) 2009 Red Hat, Inc.
+ *
+ * This software is licensed to you under the GNU General Public License,
+ * version 2 (GPLv2). There is NO WARRANTY for this software, express or
+ * implied, including the implied warranties of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
+ * along with this software; if not, see
+ * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
+ *
+ * Red Hat trademarks are not licensed under GPLv2. No permission is
+ * granted to use or replicate Red Hat trademarks that are incorporated
+ * in this software or its documentation.
+ */
+package com.redhat.rhn.manager;
+
+import com.redhat.rhn.common.hibernate.HibernateFactory;
+
+import org.apache.log4j.Logger;
+import org.hibernate.HibernateException;
+
+/**
+ * BaseTransactionCommand - simple baseclass to hold logic for handling tx
+ *
+ * @version $Rev$
+ */
+public class BaseTransactionCommand {
+
+ private Logger log;
+
+ /**
+ * Constructor
+ * @param logIn to use.
+ */
+ public BaseTransactionCommand(Logger logIn) {
+ log = logIn;
+ }
+
+ protected void handleTransaction() {
+ boolean committed = false;
+
+ try {
+ HibernateFactory.commitTransaction();
+ committed = true;
+
+ if (log.isDebugEnabled()) {
+ log.debug("Transaction committed");
+ }
+ }
+ catch (HibernateException e) {
+ log.error("Rolling back transaction", e);
+ }
+ finally {
+ try {
+ if (!committed) {
+ try {
+ if (log.isDebugEnabled()) {
+ log.debug("Rolling back transaction");
+ }
+ HibernateFactory.rollbackTransaction();
+ }
+ catch (HibernateException e) {
+ final String msg = "Additional error during rollback";
+ log.warn(msg, e);
+ }
+ }
+ }
+ finally {
+ // cleanup the session
+ HibernateFactory.closeSession();
+ }
+ }
+ }
+
+}
diff --git
a/java/code/src/com/redhat/rhn/manager/errata/cache/UpdateErrataCacheCommand.java
b/java/code/src/com/redhat/rhn/manager/errata/cache/UpdateErrataCacheCommand.java
index fdff195..547b78b 100644
---
a/java/code/src/com/redhat/rhn/manager/errata/cache/UpdateErrataCacheCommand.java
+++
b/java/code/src/com/redhat/rhn/manager/errata/cache/UpdateErrataCacheCommand.java
@@ -25,12 +25,12 @@ import com.redhat.rhn.domain.org.Org;
import com.redhat.rhn.domain.org.OrgFactory;
import com.redhat.rhn.domain.server.Server;
import com.redhat.rhn.frontend.dto.ErrataCacheDto;
+import com.redhat.rhn.manager.BaseTransactionCommand;
import com.redhat.rhn.manager.action.ActionManager;
import com.redhat.rhn.manager.errata.ErrataManager;
import com.redhat.rhn.manager.system.SystemManager;
import org.apache.log4j.Logger;
-import org.hibernate.HibernateException;
import java.util.ArrayList;
import java.util.HashMap;
@@ -43,7 +43,7 @@ import java.util.Map;
* UpdateErrataCacheCommand
* @version $Rev$
*/
-public class UpdateErrataCacheCommand {
+public class UpdateErrataCacheCommand extends BaseTransactionCommand {
private static Logger log = Logger
.getLogger(UpdateErrataCacheCommand.class);
@@ -51,6 +51,7 @@ public class UpdateErrataCacheCommand {
* Default constructor
*/
public UpdateErrataCacheCommand() {
+ super(log);
}
/**
@@ -280,42 +281,6 @@ public class UpdateErrataCacheCommand {
return retval;
}
-
- protected void handleTransaction() {
- boolean committed = false;
-
- try {
- HibernateFactory.commitTransaction();
- committed = true;
-
- if (log.isDebugEnabled()) {
- log.debug("Transaction committed");
- }
- }
- catch (HibernateException e) {
- log.error("Rolling back transaction", e);
- }
- finally {
- try {
- if (!committed) {
- try {
- if (log.isDebugEnabled()) {
- log.debug("Rolling back transaction");
- }
- HibernateFactory.rollbackTransaction();
- }
- catch (HibernateException e) {
- final String msg = "Additional error during rollback";
- log.warn(msg, e);
- }
- }
- }
- finally {
- // cleanup the session
- HibernateFactory.closeSession();
- }
- }
- }
private void processServer(Long serverId, Org org) {
Server server = SystemManager.lookupByIdAndOrg(serverId, org);
diff --git a/java/code/src/com/redhat/rhn/manager/satellite/UpgradeCommand.java
b/java/code/src/com/redhat/rhn/manager/satellite/UpgradeCommand.java
new file mode 100644
index 0000000..f2fd91f
--- /dev/null
+++ b/java/code/src/com/redhat/rhn/manager/satellite/UpgradeCommand.java
@@ -0,0 +1,105 @@
+/**
+ * Copyright (c) 2009 Red Hat, Inc.
+ *
+ * This software is licensed to you under the GNU General Public License,
+ * version 2 (GPLv2). There is NO WARRANTY for this software, express or
+ * implied, including the implied warranties of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
+ * along with this software; if not, see
+ * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
+ *
+ * Red Hat trademarks are not licensed under GPLv2. No permission is
+ * granted to use or replicate Red Hat trademarks that are incorporated
+ * in this software or its documentation.
+ */
+package com.redhat.rhn.manager.satellite;
+
+import com.redhat.rhn.common.hibernate.HibernateFactory;
+import com.redhat.rhn.domain.kickstart.KickstartData;
+import com.redhat.rhn.domain.kickstart.KickstartFactory;
+import com.redhat.rhn.domain.kickstart.KickstartSession;
+import com.redhat.rhn.domain.task.Task;
+import com.redhat.rhn.domain.task.TaskFactory;
+import com.redhat.rhn.manager.BaseTransactionCommand;
+import com.redhat.rhn.manager.kickstart.KickstartSessionCreateCommand;
+
+import org.apache.log4j.Logger;
+
+import java.util.List;
+
+
+/**
+ * Class responsible for executing one-time upgrade logic
+ *
+ * @version $Rev$
+ */
+public class UpgradeCommand extends BaseTransactionCommand {
+
+ /**
+ * Logger for this class
+ */
+ private static Logger log = Logger.getLogger(UpgradeCommand.class);
+
+ public static final String UPGRADE_TASK_NAME = "upgrade_satellite_";
+ public static final String UPGRADE_KS_PROFILES =
+ UPGRADE_TASK_NAME + "kickstart_profiles";
+
+ /**
+ * Constructor
+ */
+ public UpgradeCommand() {
+ super(log);
+ }
+
+
+ /**
+ * Excute the upgrade step
+ */
+ public void store() {
+ try {
+ HibernateFactory.getSession().beginTransaction();
+ List upgradeTasks =
TaskFactory.getTaskListByNameLike(UPGRADE_TASK_NAME);
+ // Loop over upgrade tasks and execute the steps.
+ for (int i = 0; i< upgradeTasks.size(); i++) {
+ Task t = (Task) upgradeTasks.get(i);
+ // Use WARN because we want this logged.
+ if (t != null) {
+ log.warn("got upgrade task: " + t.getName());
+ if (t.getName().equals(UPGRADE_KS_PROFILES)) {
+ processKickstartProfiles();
+ TaskFactory.remove(t);
+ }
+ }
+ }
+ }
+ catch (Exception e) {
+ log.error("Problem upgrading!", e);
+ HibernateFactory.rollbackTransaction();
+
+ }
+ finally {
+ handleTransaction();
+ }
+ }
+
+ private void processKickstartProfiles() {
+ // Use WARN here because we want this operation logged.
+ log.warn("Processing ks profiles.");
+ List allKickstarts = KickstartFactory.listAllKickstartData();
+ for (int i = 0; i< allKickstarts.size(); i++) {
+ KickstartData ksdata = (KickstartData) allKickstarts.get(i);
+ KickstartSession ksession =
+
KickstartFactory.lookupDefaultKickstartSessionForKickstartData(ksdata);
+ if (ksession == null) {
+ log.warn("Kickstart does not have a session: id: " +
ksdata.getId() +
+ " label: " + ksdata.getLabel());
+ KickstartSessionCreateCommand kcmd = new
KickstartSessionCreateCommand(
+ ksdata.getOrg(), ksdata);
+ kcmd.store();
+ log.warn("Created kickstart session and key");
+ }
+
+ }
+ }
+
+}
diff --git
a/java/code/src/com/redhat/rhn/manager/satellite/test/UpgradeCommandTest.java
b/java/code/src/com/redhat/rhn/manager/satellite/test/UpgradeCommandTest.java
new file mode 100644
index 0000000..4175fb5
--- /dev/null
+++
b/java/code/src/com/redhat/rhn/manager/satellite/test/UpgradeCommandTest.java
@@ -0,0 +1,59 @@
+/**
+ * Copyright (c) 2009 Red Hat, Inc.
+ *
+ * This software is licensed to you under the GNU General Public License,
+ * version 2 (GPLv2). There is NO WARRANTY for this software, express or
+ * implied, including the implied warranties of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
+ * along with this software; if not, see
+ * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
+ *
+ * Red Hat trademarks are not licensed under GPLv2. No permission is
+ * granted to use or replicate Red Hat trademarks that are incorporated
+ * in this software or its documentation.
+ */
+package com.redhat.rhn.manager.satellite.test;
+
+import com.redhat.rhn.domain.kickstart.KickstartData;
+import com.redhat.rhn.domain.kickstart.KickstartFactory;
+import com.redhat.rhn.domain.kickstart.KickstartSession;
+import com.redhat.rhn.domain.task.Task;
+import com.redhat.rhn.domain.task.TaskFactory;
+import com.redhat.rhn.frontend.action.kickstart.test.KickstartTestHelper;
+import com.redhat.rhn.manager.satellite.UpgradeCommand;
+import com.redhat.rhn.testing.BaseTestCaseWithUser;
+
+import java.util.List;
+
+
+public class UpgradeCommandTest extends BaseTestCaseWithUser {
+
+ public void testUpgradeProfiles() throws Exception {
+ KickstartData ksd = KickstartTestHelper.createTestKickStart(user);
+
+ KickstartSession ksession =
+
KickstartFactory.lookupDefaultKickstartSessionForKickstartData(ksd);
+ assertNull(ksession);
+ TaskFactory.createTask(user.getOrg(),
+ UpgradeCommand.UPGRADE_KS_PROFILES, new Long(0));
+
+ List l = TaskFactory.getTaskListByNameLike(
+ UpgradeCommand.UPGRADE_KS_PROFILES);
+ assertTrue(l.get(0) instanceof Task);
+
+ // UpgradeCommand its its own transaction so we gotta commit.
+ commitAndCloseSession();
+
+ UpgradeCommand cmd = new UpgradeCommand();
+ cmd.store();
+
+ // Check to see if the upgrade command created the default profile.
+ ksession =
+
KickstartFactory.lookupDefaultKickstartSessionForKickstartData(ksd);
+ assertNotNull(ksession);
+
+ assertNull(TaskFactory.getTaskListByNameLike(
+ UpgradeCommand.UPGRADE_KS_PROFILES));
+
+ }
+}
diff --git a/java/code/src/com/redhat/rhn/webapp/RhnServletListener.java
b/java/code/src/com/redhat/rhn/webapp/RhnServletListener.java
index 94a7925..c81ebeb 100644
--- a/java/code/src/com/redhat/rhn/webapp/RhnServletListener.java
+++ b/java/code/src/com/redhat/rhn/webapp/RhnServletListener.java
@@ -16,6 +16,7 @@ package com.redhat.rhn.webapp;
import com.redhat.rhn.common.hibernate.HibernateFactory;
import com.redhat.rhn.common.messaging.MessageQueue;
+import com.redhat.rhn.manager.satellite.UpgradeCommand;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
@@ -109,6 +110,17 @@ public class RhnServletListener implements
ServletContextListener {
startHibernate();
logStart("Hibernate");
+
+ log.debug("Starting upgrade check");
+ executeUpgradeStep();
+ }
+
+ private void executeUpgradeStep() {
+ log.debug("calling UpgradeCommand.");
+ UpgradeCommand cmd = new UpgradeCommand();
+ cmd.store();
+ log.debug("UpgradeCommand done.");
+
}
/** {...@inheritdoc} */
_______________________________________________
spacewalk-commits mailing list
spacewalk-comm...@lists.fedorahosted.org
https://fedorahosted.org/mailman/listinfo/spacewalk-commits