summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDevan Goodwin <dgoodwin@rm-rf.ca>2009-11-02 11:40:12 -0400
committerDevan Goodwin <dgoodwin@rm-rf.ca>2009-11-02 11:40:12 -0400
commit3474ba42b0009fabbd1540ba956223752bf715e7 (patch)
treea9d51c151d3b3766eeeca8d2a97cf9ff3416e388
parent3d84267130db79404e0396b5d0efce136b682526 (diff)
parent2239f7f0beb84ef56d7dae1a5db74d65adcedc23 (diff)
downloadcobbler-3474ba42b0009fabbd1540ba956223752bf715e7.tar.gz
cobbler-3474ba42b0009fabbd1540ba956223752bf715e7.tar.xz
cobbler-3474ba42b0009fabbd1540ba956223752bf715e7.zip
Merge branch 'cobbler4j'
-rw-r--r--.gitignore3
-rw-r--r--cobbler/clogger.py28
-rw-r--r--cobbler4j/Makefile16
-rw-r--r--cobbler4j/README18
-rw-r--r--cobbler4j/build.py27
-rw-r--r--cobbler4j/build.xml56
-rw-r--r--cobbler4j/lib/xmlrpc.jar (renamed from cobbler4j/xmlrpc.jar)bin48341 -> 48341 bytes
-rw-r--r--cobbler4j/object_base.tmpl20
-rw-r--r--cobbler4j/src/org/fedorahosted/cobbler/CobblerConnection.java (renamed from cobbler4j/CobblerConnection.java)49
-rw-r--r--cobbler4j/src/org/fedorahosted/cobbler/CobblerObject.java (renamed from cobbler4j/CobblerObject.java)43
-rw-r--r--cobbler4j/src/org/fedorahosted/cobbler/Finder.java85
-rw-r--r--cobbler4j/src/org/fedorahosted/cobbler/FinderTests.java (renamed from cobbler4j/Test.java)16
-rw-r--r--cobbler4j/src/org/fedorahosted/cobbler/ObjectType.java46
-rw-r--r--cobbler4j/src/org/fedorahosted/cobbler/XmlRpcException.java (renamed from cobbler4j/XmlRpcException.java)0
14 files changed, 333 insertions, 74 deletions
diff --git a/.gitignore b/.gitignore
index 4a347069..47f69c4e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -16,3 +16,6 @@ cobbler/webui/master.py
config/modules.conf
config/settings
config/version
+
+# Autogenerated cobbler4j classes, should never be checked in:
+cobbler4j/src/org/fedorahosted/cobbler/autogen
diff --git a/cobbler/clogger.py b/cobbler/clogger.py
index 9faea95b..eb7a5d18 100644
--- a/cobbler/clogger.py
+++ b/cobbler/clogger.py
@@ -33,16 +33,24 @@ INFO = "INFO"
class Logger:
def __init__(self, logfile="/var/log/cobbler/cobbler.log"):
+ self.logfile = None
+
# Main logfile is append mode, other logfiles not.
if not os.path.exists(logfile):
self.logfile = open(logfile, "a")
sub_process.call("chown apache %s" % logfile, shell=True)
self.logfile.close()
- if logfile.find("tasks") != -1:
- self.logfile = open(logfile, "w+")
- else:
- self.logfile = open(logfile, "a")
+ try:
+ if logfile.find("tasks") != -1:
+ self.logfile = open(logfile, "w+")
+ else:
+ self.logfile = open(logfile, "a")
+ except IOError:
+ # You likely don't have write access, this logger will just print
+ # things to stdout.
+ pass
+
def warning(self, msg):
@@ -61,12 +69,16 @@ class Logger:
self.__write(None, msg)
def __write(self, level, msg):
+
if level is not None:
- self.logfile.write("%s - %s | %s" % (time.asctime(), level, msg))
+ msg = "%s - %s | %s" % (time.asctime(), level, msg)
+
+ if self.logfile is not None:
+ self.logfile.write(msg)
+ self.logfile.write("\n")
+ self.logfile.flush()
else:
- self.logfile.write(msg)
- self.logfile.write("\n")
- self.logfile.flush()
+ print(msg)
def handle(self):
return self.logfile
diff --git a/cobbler4j/Makefile b/cobbler4j/Makefile
deleted file mode 100644
index 5e6915b8..00000000
--- a/cobbler4j/Makefile
+++ /dev/null
@@ -1,16 +0,0 @@
-all: clean build test
-
-build:
- python build.py
- ant build
-
-clean:
- -rm -rf build
- -rm CobblerDistro.java
- -rm CobblerProfile.java
- -rm CobblerSystem.java
- -rm CobblerImage.java
- -rm CobblerRepo.java
-
-test:
- ant test
diff --git a/cobbler4j/README b/cobbler4j/README
index 78485318..2cf26c84 100644
--- a/cobbler4j/README
+++ b/cobbler4j/README
@@ -5,9 +5,17 @@ Bindings for Cobbler XMLRPC to make them feel more like Java objects.
Originally derived from work done for http://fedorahosted.org/spacewalk and then
modified.
-The code is autogenerated. Run "make" to build the project.
+Some of this code is auto generated. (all classes in
+org.fedorahosted.cobbler.autogen)
+
+Requirements
+============
+
+To build: ant java-1.6.0-openjdk-devel python-cheetah
+To test: ant-junit junit4
+
+Build with: "ant"
+
+Test with: "ant test"
+
- * ant, python, and javac are required.
- * code generation requires an installed version of cobbler 2.0.2 or higher
- * to run tests ... first implement the tests, and then run them :)
- meaning, we need to write those sometime.
diff --git a/cobbler4j/build.py b/cobbler4j/build.py
index 121ec790..0bf3abcc 100644
--- a/cobbler4j/build.py
+++ b/cobbler4j/build.py
@@ -7,13 +7,23 @@ Michael DeHaan <mdehaan@redhat.com>
May Guido have mercy on your application.
"""
+# Force to build from the Python modules in this source checkout,
+# not what's installed on the system.
+import sys
+sys.path.insert(0, "../")
+
+import os.path
+import commands
+
import Cheetah.Template as Template
+
import cobbler.item_distro as cobbler_distro
import cobbler.item_profile as cobbler_profile
import cobbler.item_system as cobbler_system
import cobbler.item_repo as cobbler_repo
import cobbler.item_image as cobbler_image
-import os.path
+
+AUTOGEN_PATH = "src/org/fedorahosted/cobbler/autogen/"
# FIXME: make this also do Ruby
# FIXME: network object handling is quasi-special
@@ -21,11 +31,11 @@ import os.path
# Define what files we need to template out dynamically
# FIXME: also do this for XMLPC
OBJECT_MAP = [
- [ "Distro", "CobblerDistro", cobbler_distro.FIELDS ],
- [ "Profile", "CobblerProfile", cobbler_profile.FIELDS ],
- [ "System", "CobblerSystem", cobbler_system.FIELDS ],
- [ "Repo", "CobblerRepo", cobbler_repo.FIELDS ],
- [ "Image", "CobblerImage", cobbler_image.FIELDS ],
+ [ "Distro", "Distro", cobbler_distro.FIELDS ],
+ [ "Profile", "Profile", cobbler_profile.FIELDS ],
+ [ "System", "SystemRecord", cobbler_system.FIELDS ],
+ [ "Repo", "Repo", cobbler_repo.FIELDS ],
+ [ "Image", "Image", cobbler_image.FIELDS ],
]
# Define what variables to expose in all templates
@@ -95,10 +105,11 @@ def templatize_from_vars(objname, jclass, vars):
if objname is not None:
vars.update({
"JavaObjectType" : jclass,
- "CobblerObjectType" : objname
+ "CobblerObjectType" : objname.upper()
})
filename1 = "object_base.tmpl"
- filename2 = "%s.java" % jclass
+ filename2 = "%s%s.java" % (AUTOGEN_PATH, jclass)
+ commands.getstatusoutput("mkdir -p %s" % AUTOGEN_PATH)
print "TEMPLATING %s to %s" % (filename1, filename2)
template_to_disk(filename1, vars, filename2)
diff --git a/cobbler4j/build.xml b/cobbler4j/build.xml
index 31853387..e1eb5d17 100644
--- a/cobbler4j/build.xml
+++ b/cobbler4j/build.xml
@@ -1,12 +1,30 @@
-<project>
+<project default = "build">
- <target name="build">
+ <!-- Might be wasteful to clean everything here, but with the
+ auto-generation in play it seems wise. -->
+ <target name="init" depends="clean">
+ <path id="class.path">
+ <pathelement location="build"/>
+ <fileset dir="lib">
+ <include name="**/*.jar"/>
+ </fileset>
+ <!-- Standard location for Fedora/RHEL junit4 package. -->
+ <fileset dir="/usr/share/java/">
+ <include name="junit4.jar"/>
+ </fileset>
+ </path>
+ </target>
+
+ <!-- Auto-generate Cobbler classes when we can. -->
+ <target name="generateClasses" depends="init">
+ <exec executable="python">
+ <arg value="build.py"/>
+ </exec>
+ </target>
+
+ <target name="build" depends="generateClasses">
<mkdir dir="build"/>
- <unjar src="xmlrpc.jar" dest="build"/>
- <javac srcdir="." destdir="build" nowarn="true">
- <classpath>
- <pathelement location="build"/>
- </classpath>
+ <javac srcdir="src/" destdir="build" nowarn="true" classpathref="class.path">
</javac>
<jar destfile="cobbler4j.jar" basedir="build">
<manifest>
@@ -15,10 +33,28 @@
</jar>
</target>
- <target name="test">
- <java jar="cobbler4j.jar" fork="true">
- <arg value="http://127.0.0.1/cobbler_api"/>
+ <target name="clean">
+ <delete dir="build/" quiet="true"/>
+ <delete dir="src/org/fedorahosted/cobbler/autogen/" quiet="true"/>
+ <delete file="cobbler4j.jar" quiet="true"/>
+ </target>
+
+ <target name="test" depends="build">
+ <!--
+ <java classname="org.fedorahosted.cobbler.Test" fork="true" classpathref="class.path">
+ <arg value="http://192.168.1.1"/>
</java>
+ -->
+ <junit printsummary="yes" haltonfailure="no">
+ <formatter type="xml"/>
+ <classpath refid="class.path"/>
+ <batchtest fork="yes" todir="build/">
+ <fileset dir="src">
+ <include name="**/*Test*.java"/>
+ </fileset>
+ </batchtest>
+ </junit>
+
</target>
</project>
diff --git a/cobbler4j/xmlrpc.jar b/cobbler4j/lib/xmlrpc.jar
index 2a7c8d69..2a7c8d69 100644
--- a/cobbler4j/xmlrpc.jar
+++ b/cobbler4j/lib/xmlrpc.jar
Binary files differ
diff --git a/cobbler4j/object_base.tmpl b/cobbler4j/object_base.tmpl
index 9424d18e..eaad20fb 100644
--- a/cobbler4j/object_base.tmpl
+++ b/cobbler4j/object_base.tmpl
@@ -13,7 +13,7 @@
* in this software or its documentation.
*/
-package org.fedorahosted.cobbler;
+package org.fedorahosted.cobbler.autogen;
import java.util.ArrayList;
import java.util.HashMap;
@@ -22,6 +22,9 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
+import org.fedorahosted.cobbler.CobblerObject;
+import org.fedorahosted.cobbler.CobblerConnection;
+import org.fedorahosted.cobbler.ObjectType;
/**
* @author paji (original for spacewalk)
@@ -33,6 +36,7 @@ public class $JavaObjectType extends CobblerObject {
public ${JavaObjectType}(CobblerConnection clientIn) {
client = clientIn;
}
+ protected ${JavaObjectType} () {}
#for $val in $fields
#set $interface_specific = "False"
@@ -113,18 +117,8 @@ public class $JavaObjectType extends CobblerObject {
#end for
- public String getObjectType() {
- return "$CobblerObjectType";
- }
-
-
- /**
- * Returns a list of available $JavaObjectType objects
- * @param connection the cobbler connection
- * @return a list of $JavaObjectType objects.
- */
- public static List list(CobblerConnection connection) {
- return null;
+ public ObjectType getObjectType() {
+ return ObjectType.$CobblerObjectType;
}
}
diff --git a/cobbler4j/CobblerConnection.java b/cobbler4j/src/org/fedorahosted/cobbler/CobblerConnection.java
index a90b7cf2..e398befe 100644
--- a/cobbler4j/CobblerConnection.java
+++ b/cobbler4j/src/org/fedorahosted/cobbler/CobblerConnection.java
@@ -42,26 +42,25 @@ public class CobblerConnection {
*/
public CobblerConnection(String url, String user, String pass) {
- List auth = new LinkedList();
- auth.add(user);
- auth.add(pass);
+
+ url += "/cobbler_api";
+
try {
client = new XmlRpcClient(url, false);
}
catch (MalformedURLException e) {
throw new XmlRpcException(e);
}
- token = (String) invokeMethod("login", auth);
- }
-
+ token = (String) invokeNoTokenMethod("login", user, pass);
+ }
+
/**
* Invoke an XMLRPC method.
* @param method method to invoke
* @param args args to pass to method
* @return Object data returned.
*/
- protected Object invokeMethod(String method, List args) {
- args.add(token);
+ public Object invokeNoTokenMethod(String method, List args) {
try {
return client.invoke(method, args);
}
@@ -70,4 +69,38 @@ public class CobblerConnection {
}
}
+ /**
+ * Invoke an XMLRPC method.
+ * @param method method to invoke
+ * @param params params to pass to method
+ * @return Object data returned.
+ */
+ public Object invokeNoTokenMethod(String method, Object ... params) {
+ return invokeNoTokenMethod(method, Arrays.asList(params));
+ }
+
+
+
+ /**
+ * Invoke an XMLRPC method.
+ * @param method method to invoke
+ * @param args args to pass to method
+ * @return Object data returned.
+ */
+ protected Object invokeMethod(String method, List params) {
+ List args = new LinkedList(params);
+ args.add(token);
+ return invokeNoTokenMethod(method, args);
+ }
+
+ /**
+ * Invoke an XMLRPC method.
+ * @param method method to invoke
+ * @param args args to pass to method
+ * @return Object data returned.
+ */
+ public Object invokeMethod(String method, Object ... params) {
+ return invokeMethod(method, Arrays.asList(params));
+ }
+
}
diff --git a/cobbler4j/CobblerObject.java b/cobbler4j/src/org/fedorahosted/cobbler/CobblerObject.java
index 290965c7..01100623 100644
--- a/cobbler4j/CobblerObject.java
+++ b/cobbler4j/src/org/fedorahosted/cobbler/CobblerObject.java
@@ -34,8 +34,16 @@ import java.util.Set;
public abstract class CobblerObject {
protected String handle;
- protected HashMap dataMap = new HashMap();
+ protected Map dataMap = new HashMap();
protected CobblerConnection client;
+ static final String NAME = "name";
+ static final String UID = "uid";
+ /**
+ * @return the name
+ */
+ public String getName() {
+ return (String)dataMap.get(NAME);
+ }
/**
* Helper method used by all cobbler objects to
@@ -62,7 +70,7 @@ public abstract class CobblerObject {
//}
- protected abstract String getObjectType();
+ protected abstract ObjectType getObjectType();
/**
* look up data maps by a certain criteria
@@ -138,7 +146,36 @@ public abstract class CobblerObject {
}
public String toString() {
- return getObjectType() + dataMap.toString();
+ return getObjectType() + "\n" + dataMap.toString();
+ }
+
+ public void commit() {
+ client.invokeMethod("commit_" + getObjectType().getName() , getHandle(), dataMap);
+ }
+
+
+ protected String getHandle() {
+ if (handle == null || handle.trim().length() == 0) {
+ handle = invokeGetHandle();
+ }
+ return handle;
}
+
+ private String invokeGetHandle() {
+ return (String)client.invokeMethod("get_"+ getObjectType().getName() + "_handle", this.getName());
+ }
+
+ static CobblerObject load(ObjectType type, CobblerConnection client, Map<String, Object> dataMap) {
+ try
+ {
+ CobblerObject obj = (CobblerObject) type.getObjectClass().newInstance();
+ obj.dataMap = dataMap;
+ obj.client = client;
+ return obj;
+ }
+ catch(Exception e) {
+ throw new XmlRpcException("Class instantiation expcetion.", e);
+ }
+ }
}
diff --git a/cobbler4j/src/org/fedorahosted/cobbler/Finder.java b/cobbler4j/src/org/fedorahosted/cobbler/Finder.java
new file mode 100644
index 00000000..f27190d4
--- /dev/null
+++ b/cobbler4j/src/org/fedorahosted/cobbler/Finder.java
@@ -0,0 +1,85 @@
+/**
+ * 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 org.fedorahosted.cobbler;
+
+import java.util.List;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.HashMap;
+
+public class Finder {
+ private static final Finder INSTANCE = new Finder();
+
+ private Finder() {
+ }
+
+ public static Finder getInstance() {
+ return INSTANCE;
+ }
+
+ public List<? extends CobblerObject> findItems(CobblerConnection client,
+ ObjectType type,
+ String critera, String value) {
+ if (value == null) {
+ return null;
+ }
+
+ Map<String, String> criteria = new HashMap<String, String>();
+ criteria.put(critera, value);
+ List<Map<String, Object>> objects = (List<Map<String, Object>>)
+ client.invokeMethod(type.getName(), criteria);
+ return maps2Objects(client, type, objects);
+ }
+
+ private List <? extends CobblerObject> maps2Objects(CobblerConnection client,
+ ObjectType type, List<Map<String, Object>> maps) {
+ List<CobblerObject> ret = new LinkedList<CobblerObject>();
+ for (Map<String, Object> obj : maps) {
+ ret.add(CobblerObject.load(type, client, obj));
+ }
+ return ret;
+ }
+
+ public CobblerObject findItemById(CobblerConnection client,
+ ObjectType type,
+ String id) {
+ if (id == null) {
+ return null;
+ }
+ List <? extends CobblerObject> items = findItems(client, type, CobblerObject.UID, id);
+ if (items.isEmpty()) {
+ return null;
+ }
+ return items.get(0);
+ }
+
+ public CobblerObject findItemByName(CobblerConnection client,
+ ObjectType type, String name) {
+ if (name == null) {
+ return null;
+ }
+ List <? extends CobblerObject> items = findItems(client, type, CobblerObject.NAME, name);
+ if (items.isEmpty()) {
+ return null;
+ }
+ return items.get(0);
+ }
+
+ public List<? extends CobblerObject> listItems(CobblerConnection client, ObjectType type) {
+ List<Map<String, Object>> objects = (List<Map<String, Object>>)
+ client.invokeNoTokenMethod("get_" + type.getName()+ "s");
+ return maps2Objects(client, type, objects);
+ }
+}
diff --git a/cobbler4j/Test.java b/cobbler4j/src/org/fedorahosted/cobbler/FinderTests.java
index 85f9ad65..01971385 100644
--- a/cobbler4j/Test.java
+++ b/cobbler4j/src/org/fedorahosted/cobbler/FinderTests.java
@@ -1,10 +1,15 @@
package org.fedorahosted.cobbler;
-public class Test {
+import java.util.List;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class FinderTests {
public static final String user = "testing";
public static final String pass = "testing";
+ /*
public static void main(String[] args) {
if (args.length < 1) {
@@ -13,9 +18,14 @@ public class Test {
String endPoint = args[0];
System.out.println("Running cobbler4j tests against " + endPoint);
CobblerConnection conn = new CobblerConnection(endPoint,user,pass);
- CobblerDistro distro = new CobblerDistro(conn);
- System.out.println(distro.toString());
+ List<Distro> distros = (List<Distro>)Finder.getInstance().
+ listItems(conn, ObjectType.DISTRO);
+ System.out.println(distros.get(0));
+ }
+ */
+ @Test public void testSomething() {
+ assertTrue(true);
}
}
diff --git a/cobbler4j/src/org/fedorahosted/cobbler/ObjectType.java b/cobbler4j/src/org/fedorahosted/cobbler/ObjectType.java
new file mode 100644
index 00000000..5aeea690
--- /dev/null
+++ b/cobbler4j/src/org/fedorahosted/cobbler/ObjectType.java
@@ -0,0 +1,46 @@
+/**
+ * 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 org.fedorahosted.cobbler;
+
+// Wildcard isn't great, but better than an explicit list of objects that
+// may change.
+import org.fedorahosted.cobbler.autogen.*;
+
+public enum ObjectType {
+ DISTRO ("distro", Distro.class),
+ PROFILE ("profile", Profile.class),
+ SYSTEM ("system", SystemRecord.class),
+ IMAGE ("image", Image.class),
+ REPO ("repo", Repo.class);
+ private String name;
+ private Class clazz;
+
+ ObjectType (String nameIn, Class clazzIn) {
+ name = nameIn;
+ clazz = clazzIn;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public Class getObjectClass() {
+ return clazz;
+ }
+
+ public String toString() {
+ return getName();
+ }
+}
diff --git a/cobbler4j/XmlRpcException.java b/cobbler4j/src/org/fedorahosted/cobbler/XmlRpcException.java
index f8e8ec5a..f8e8ec5a 100644
--- a/cobbler4j/XmlRpcException.java
+++ b/cobbler4j/src/org/fedorahosted/cobbler/XmlRpcException.java