Branch 'devel' - cas-admin caslib/core.py
by Adam Stokes
cas-admin | 4 ++--
caslib/core.py | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
New commits:
commit 2c60d214423253989027afd1fb061372ac50a423
Author: adam stokes <astokes(a)fedoraproject.org>
Date: Mon May 3 19:48:52 2010 -0400
function
diff --git a/cas-admin b/cas-admin
index 131f9dd..b61e2c8 100755
--- a/cas-admin
+++ b/cas-admin
@@ -18,6 +18,7 @@
""" cas-admin - fingerprinting utility for cas
"""
+import sys
if sys.version_info[:2] < (2,4):
raise SystemExit("Python >= 2.4 required")
try:
@@ -30,7 +31,6 @@ except:
import os
import ConfigParser
import optparse
-import sys
import datetime
import paramiko
@@ -73,7 +73,7 @@ def queryLocalRpms(path):
cmd = "find -L %s -iregex %s" % (settings["KERNELS"], settings["RPMFILTER"])
results = casexecute(cmd, True)
count = 0
- for rpm in results:
+ for rpm in results.split():
localRpms.append(rpm)
genprint("(found) %-5d kernels(s)" % (count,))
count = count + 1
diff --git a/caslib/core.py b/caslib/core.py
index 9ae13d7..212f757 100644
--- a/caslib/core.py
+++ b/caslib/core.py
@@ -31,7 +31,7 @@ def casexecute(cmd, output=False):
- `cmd`: shell command to run
- `output`: return std out
"""
- stdout, stderr = Popen(cmd, shell=True, bufsize=-1).communicate()
+ (stdout, stderr) = Popen(cmd, shell=True, bufsize=-1, stdout=PIPE, stderr=PIPE).communicate()
if output:
return stdout
else:
14 years
Branch 'devel' - cas-admin
by Adam Stokes
cas-admin | 1 -
1 file changed, 1 deletion(-)
New commits:
commit a1421fecded51f5c316caa6367e53fc0f179fc82
Author: adam stokes <astokes(a)fedoraproject.org>
Date: Mon May 3 19:35:54 2010 -0400
function
diff --git a/cas-admin b/cas-admin
index 65c8933..131f9dd 100755
--- a/cas-admin
+++ b/cas-admin
@@ -212,4 +212,3 @@ if __name__=="__main__":
# Got all the pre-reqs out of the way; now build RPM list
queryLocalRpms(settings["DEBUGS"])
- return True
14 years
Branch 'devel' - cas-admin
by Adam Stokes
cas-admin | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
New commits:
commit dffbf5e873821a95c8695191e9a190dc1d6f6bef
Author: adam stokes <astokes(a)fedoraproject.org>
Date: Mon May 3 19:34:53 2010 -0400
function
diff --git a/cas-admin b/cas-admin
index cbe84bd..65c8933 100755
--- a/cas-admin
+++ b/cas-admin
@@ -186,7 +186,7 @@ if __name__=="__main__":
if os.getuid() is not 0:
raise RuntimeError, "You must be root(0), instead you are id(%d)" % (os.getuid())
- genprint(_("Starting requirement processing and database creation."))
+ genprint("Starting requirement processing and database creation.")
if not os.path.isdir(os.path.dirname(settings["DATABASE"])):
os.makedirs(os.path.dirname(settings["DATABASE"]))
if not os.path.isdir(settings["DEBUGS"]):
@@ -194,7 +194,7 @@ if __name__=="__main__":
# if autopurge is enabled lets clean up some stale data
if AUTOPURGE:
- genprint(_('Autopurge enabled, purging stale data'))
+ genprint('Autopurge enabled, purging stale data')
purgeData(int(PURGELIMIT))
if purgeData:
@@ -205,9 +205,6 @@ if __name__=="__main__":
purgeDataDays = PURGELIMIT
genprint("Beginning Purge going back %s day(s)" % (purgeDataDays,))
purgeData(int(purgeDataDays))
- return True
- else:
- return False
if queryServer:
genprint("Generating accessible CAS server list")
@@ -215,3 +212,4 @@ if __name__=="__main__":
# Got all the pre-reqs out of the way; now build RPM list
queryLocalRpms(settings["DEBUGS"])
+ return True
14 years
Branch 'devel' - cas-admin
by Adam Stokes
cas-admin | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
New commits:
commit 5e65e2b552f1316315d45757e51cfdefafccc662
Author: adam stokes <astokes(a)fedoraproject.org>
Date: Mon May 3 19:32:25 2010 -0400
function
diff --git a/cas-admin b/cas-admin
index 18e2ed2..cbe84bd 100755
--- a/cas-admin
+++ b/cas-admin
@@ -203,7 +203,7 @@ if __name__=="__main__":
if ans=='Y' or ans=='y':
if not purgeDataDays:
purgeDataDays = PURGELIMIT
- genprint(_("Beginning Purge going back %s day(s)" % (purgeDataDays,)))
+ genprint("Beginning Purge going back %s day(s)" % (purgeDataDays,))
purgeData(int(purgeDataDays))
return True
else:
14 years
Branch 'devel' - cas-admin
by Adam Stokes
cas-admin | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
New commits:
commit 1542eefe9ce1b3f9ad7fb5020c7d70f94f5836bf
Author: adam stokes <astokes(a)fedoraproject.org>
Date: Mon May 3 19:30:57 2010 -0400
fix return function
diff --git a/cas-admin b/cas-admin
index 149d289..18e2ed2 100755
--- a/cas-admin
+++ b/cas-admin
@@ -203,9 +203,9 @@ if __name__=="__main__":
if ans=='Y' or ans=='y':
if not purgeDataDays:
purgeDataDays = PURGELIMIT
- genprint(_("Beginning Purge going back %s day(s)" % (purgeDataDays,)))
- purgeData(int(purgeDataDays))
- return True
+ genprint(_("Beginning Purge going back %s day(s)" % (purgeDataDays,)))
+ purgeData(int(purgeDataDays))
+ return True
else:
return False
14 years
Branch 'devel' - cas.spec
by Adam Stokes
cas.spec | 1 +
1 file changed, 1 insertion(+)
New commits:
commit a1df77cb354fd36a41bd1fee177639a38b6bb5aa
Author: adam stokes <astokes(a)fedoraproject.org>
Date: Mon May 3 19:28:51 2010 -0400
spec
diff --git a/cas.spec b/cas.spec
index 6f69940..f712db5 100644
--- a/cas.spec
+++ b/cas.spec
@@ -48,6 +48,7 @@ rm -rf ${RPM_BUILD_ROOT}
%{_bindir}/cas-admin
%{python_sitelib}/*
%{_mandir}/man1/*
+%{_mandir}/man5/*
%{_datadir}/%{name}
%dir %{_var}/lib/cas/snippets/
%config(noreplace) %{_var}/lib/cas/snippets/*
14 years
Branch 'devel' - 2 commits - cas-admin caslib/core.py caslib/db.py cas.spec
by Adam Stokes
cas-admin | 359 ++++++++++++++++++++++++++-------------------------------
cas.spec | 12 +
caslib/core.py | 13 ++
caslib/db.py | 211 +++++++++++----------------------
4 files changed, 262 insertions(+), 333 deletions(-)
New commits:
commit 17a1681db3593315e473337e1ca37c857cb5e428
Merge: ded0b7a... 2fb868c...
Author: adam stokes <astokes(a)fedoraproject.org>
Date: Mon May 3 16:17:29 2010 -0400
Merge branch 'devel' of ssh://git.fedorahosted.org/git/cas into devel
commit ded0b7a2f759be192aa271eacf977110a5670e61
Author: adam stokes <astokes(a)fedoraproject.org>
Date: Mon May 3 16:17:16 2010 -0400
rework cas-admin to use sqlalchemy interface
diff --git a/cas-admin b/cas-admin
index bd2bd43..149d289 100755
--- a/cas-admin
+++ b/cas-admin
@@ -1,42 +1,50 @@
#!/usr/bin/python
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see <http://www.gnu.org/licenses/>.
+# cas-admin
+# CAS Administration
+# Copyright (C) 2010 Adam Stokes
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
""" cas-admin - fingerprinting utility for cas
"""
+if sys.version_info[:2] < (2,4):
+ raise SystemExit("Python >= 2.4 required")
+try:
+ from caslib.db import *
+except:
+ raise SystemExit('Can not interface with database, please check configuration settings' \
+ 'and try again')
+
+# Must have requirements now loaded -- we can continue
import os
import ConfigParser
import optparse
import sys
-import urlparse
import datetime
import paramiko
-from caslib.core import CoreBase
-from caslib.util import UtilBase, Logging, genprint
+from caslib.core import CoreBase, casexecute
+from caslib.util import UtilBase, genprint
from caslib.rpmutils import RPMBase
-from caslib.db import CasStorage, CasStorageException
if sys.version_info[:2] < (2,6):
- from caslib.cas_subprocess import Popen, PIPE, call
import caslib.cas_shutil as shutil
else:
- from subprocess import Popen, PIPE, call
import shutil
-if sys.version_info[:2] < (2,4):
- raise SystemExit("Python >= 2.4 required")
-
+# Setup session for db interaction
+session = Session()
# Read in configuration
config = ConfigParser.ConfigParser()
config.read("/etc/cas.conf")
@@ -54,39 +62,32 @@ BUFFERSIZE=None
if config.has_option("advanced", "buffersize"):
BUFFERSIZE=config.get("advanced", "buffersize")
-class CasDatabaseHandler(object):
- def __init__(self, logger):
- self.casLog = logger
- self.util = UtilBase()
- # setup database connection
- self.first_run = False
- if not os.path.isfile(settings["DATABASE"]):
- self.first_run = True
- self.db = CasStorage(settings["DATABASE"])
- self.db.connect()
- if self.first_run:
- self.db.buildTable()
-
- def run(self):
- # Uses emacs regex -- see `man find`
- # TODO: rework kernel filtering to expand scope
- # beyond just debuginfo rpms.
- cmd = ["find", "-L", settings["KERNELS"], "-iregex", settings["RPMFILTER"]]
- pipe = Popen(cmd, stdout=PIPE, stderr=PIPE)
- # setup count for kernels found, mainly for console output
- count = 0
- # create list of rpms from `cmd`
- for line in pipe.stdout:
- self.db.addDebuginfoRPM(line.strip())
- genprint("(found) %-5d kernel(s)" % (count,))
- count = count + 1
- # query database for debuginfo rpms
- rpms = self.db.getAllDebuginfoRPM()
- for id, rpm in rpms:
- # temporary storage path in form of DEBUGS/COUNT
- dst = os.path.join(settings["DEBUGS"], str(count))
- if not os.path.isdir(dst):
- os.makedirs(dst)
+def queryLocalRpms(path):
+ """ query debug kernels located on the filesystem
+
+ Arguments:
+ - `path`: filesystem location of debug kernels
+ """
+ localRpms = []
+ # Uses emacs regex -- see `man find`
+ cmd = "find -L %s -iregex %s" % (settings["KERNELS"], settings["RPMFILTER"])
+ results = casexecute(cmd, True)
+ count = 0
+ for rpm in results:
+ localRpms.append(rpm)
+ genprint("(found) %-5d kernels(s)" % (count,))
+ count = count + 1
+
+ dst = os.path.join(settings["DEBUGS"], str(count))
+ if not os.path.isdir(dst):
+ os.makedirs(dst)
+
+ for rpm in localRpms:
+ rpm_IN_DB = session.query(RPM).filter_by(rpmPath=rpm)
+ if not rpm_IN_DB:
+ # add RPM to database
+ rpm_record = RPM(rpm)
+ session.add(rpm_record)
rpmTool = RPMBase()
genprint("(extracting) %-50s" % (os.path.basename(rpm),))
results = rpmTool.extract(rpm, dst)
@@ -98,147 +99,119 @@ class CasDatabaseHandler(object):
debugKernel = os.path.normpath(vmlinux)
timestamp = stamper.timestamp(debugKernel, BUFFERSIZE)
# add rpm id, debug, timestamp to database
- self.db.addTimestamp(id, debugKernel, timestamp)
- # Cleanup extracted debugs
- shutil.rmtree(dst)
- self.casLog.info("Timestamp database built.")
+ timestamp_record = Timestamp(timestamp, debugKernel, rpm_record.rpmId)
+ session.add(timestamp_record)
+ # commit to database
+ session.commit()
+ # Cleanup extracted debugs
+ shutil.rmtree(dst)
+ genprint("CAS Database generation complete.")
+ return
+
+def queryServers():
+ """ query accessible servers
+ """
+ host = None
+ port = 22
+ hostname_count = 0
+ ssh_obj = paramiko.SSHClient()
+ ssh_obj.load_host_keys(os.path.expanduser("~/.ssh/known_hosts"))
+ for server in ssh_obj.get_host_keys().keys():
+ server = server.strip()
+ # here we test for ports and apply accordingly
+ if ':' in server:
+ server, port = server.split(':')
+ # ssh keys place [host] when defining servers with ports
+ server = server[1:-1]
+ try:
+ ssh_obj.connect(server, port=port, username=settings["CASUSER"])
+ except paramiko.AuthenticationException, e:
+ raise SystemExit("Failed to connect: %s" % (e,))
+ stdin, stdout, stderr = ssh_obj.exec_command("/bin/uname -m")
+ if stderr:
+ genprint(stderr)
+ # clean up arch string
+ for i in stdout.readlines():
+ arch = i.strip()
+ server_record = Server(server, port, arch)
+ session.add(server_record)
+ hostname_count = hostname_count + 1
+ genprint("Server database built with %d server(s) added." % (hostname_count,))
+ session.commit()
return
-class CasServerHandler(object):
- def __init__(self, logger):
- self.casLog = logger
- self.util = UtilBase()
- # setup database connection
- self.first_run = False
- if not os.path.isfile(settings["DATABASE"]):
- self.first_run = True
- self.db = CasStorage(settings["DATABASE"])
- self.db.connect()
- if self.first_run:
- self.db.buildTable()
-
- def run(self):
- host = None
- port = 22
- hostname_count = 0
- ssh_obj = paramiko.SSHClient()
- ssh_obj.load_host_keys(os.path.expanduser("~/.ssh/known_hosts"))
- for server in ssh_obj.get_host_keys().keys():
- server = server.strip()
- # here we test for ports and apply accordingly
- if ':' in server:
- server, port = server.split(':')
- # ssh keys place [host] when defining servers with ports
- server = server[1:-1]
- try:
- ssh_obj.connect(server, port=port, username=settings["CASUSER"])
- except paramiko.AuthenticationException, e:
- raise SystemExit(self.casLog.debug("Failed to connect to %s: %s" % (server,e)))
- stdin, stdout, stderr = ssh_obj.exec_command("/bin/uname -m")
- if stderr:
- self.casLog.debug(stderr)
- # clean up arch string
- for i in stdout.readlines():
- arch = i.strip()
- self.db.addServer(server, port, arch)
- hostname_count = hostname_count + 1
- self.casLog.info("Server database built with %d server(s) added." % (hostname_count,))
- return
-
-class PurgeHandler(object):
- def __init__(self, purgeDays, logger):
- self.purgeDataDays = purgeDays
- self.casLog = logger
- self.util = UtilBase()
- self.todaysDate = datetime.date.today()
-
- def run(self):
- # create date of timedelta
- cutOffDate = self.todaysDate - datetime.timedelta(days=self.purgeDataDays)
- self.casLog.debug(cutOffDate)
- # Start of purging data
- for root, dirs, files in os.walk(settings["WORKDIRECTORY"]):
- for d in dirs:
- dirpath = os.path.join(root,d)
- # pull out date from directory structure and trim it to
- # (year, month, day)
- searchDate = self.util.regexSearch('(\d{4})\.(\d+)\.(\d+)', dirpath)
- if searchDate:
- self.casLog.debug('found %s' % (searchDate,))
- (year, month, day) = searchDate.split('.')
- # create our datetime object so we can do some arithmetic
- dirDate = datetime.date(int(year), int(month), int(day))
- self.casLog.debug(dirDate)
- if dirDate < cutOffDate:
- self.casLog.debug('Should purge old directories, %s' % (dirpath,))
- rmtree(dirpath)
-
-class CasAdminApplication(object):
- def __init__(self, args):
- self.parse_options(args)
- self.casLog = Logging("/var/log","cas-admin", settings["DEBUGLEVEL"])
-
- def parse_options(self, args):
- parser = optparse.OptionParser(usage="cas-admin [opts] args")
- parser.add_option("-b","--build", dest="buildDB",
- help="Build CAS DB", action="store_true",
- default=False)
- parser.add_option("-s","--server", dest="server_init",
- help="Build SERVER DB", action="store_true",
- default=False)
- parser.add_option("-p","--purge", dest="purgeData",
- help="Purge files default 90 days, customize with -d",
- action="store_true", default=False)
- parser.add_option("-d","--days", dest="purgeDataDays",
- help="Set how many days back to purge data")
- (self.opts, args) = parser.parse_args()
- self.buildDB = self.opts.buildDB
- self.server_init = self.opts.server_init
- self.purgeData = self.opts.purgeData
- self.purgeDataDays = self.opts.purgeDataDays
-
- def run(self):
- """ Make sure necessary directories and configuration is setup
- prior to running the fingerprint
- """
- if os.getuid() is not 0:
- raise RuntimeError, "You must be root(0), instead you are id(%d)" % (os.getuid())
- if not os.path.isdir(os.path.dirname(settings["DATABASE"])):
- os.makedirs(os.path.dirname(settings["DATABASE"]))
- if not os.path.isdir(settings["DEBUGS"]):
- os.makedirs(settings["DEBUGS"])
-
- # if autopurge is enabled lets clean up some stale data
- if AUTOPURGE:
- self.casLog.debug('Autopurge enabled, purging stale data')
- purgeHandler = PurgeHandler(int(PURGELIMIT), self.casLog).run()
-
- if self.purgeData:
- ans = raw_input("You are about to purge data, is this what you " \
- "really want to do? [Y/y/N/n]: ")
- if ans=='Y' or ans=='y':
- if not self.purgeDataDays:
- self.purgeDataDays = PURGELIMIT
- self.casLog.info("Beginning Purge going back %s day(s)" % (self.purgeDataDays,))
- purgeHandler = PurgeHandler(int(self.purgeDataDays), self.casLog).run()
- raise SystemExit(self.casLog.info("Purge finished"))
- else:
- raise SystemExit(self.casLog.info("Purge cancelled"))
-
- if self.buildDB:
- self.casLog.info("Starting CAS DB instance.")
- dbHandler = CasDatabaseHandler(self.casLog).run()
- elif self.server_init:
- self.casLog.info("Building CAS Server DB instance.")
- if not os.path.isfile(os.path.expanduser("~/.ssh/known_hosts")):
- raise SystemExit(self.casLog.info("Unable to read ssh hosts keys, " \
- "please make sure ssh client is configured properly"))
- else:
- serverHandler = CasServerHandler(self.casLog).run()
+def purgeData(days):
+ """ purge data from filesystem older then `days`
+
+ Arguments:
+ - `days`: amount of days to go back when purging data
+ """
+ util = UtilBase()
+ todaysDate = datetime.date.today()
+ # create date of timedelta
+ cutOffDate = todaysDate - datetime.timedelta(days=days)
+ # Start of purging data
+ for root, dirs, files in os.walk(settings["WORKDIRECTORY"]):
+ for d in dirs:
+ dirpath = os.path.join(root,d)
+ # pull out date from directory structure and trim it to
+ # (year, month, day)
+ searchDate = util.regexSearch('(\d{4})\.(\d+)\.(\d+)', dirpath)
+ if searchDate:
+ genprint('found %s' % (searchDate,))
+ (year, month, day) = searchDate.split('.')
+ # create our datetime object so we can do some arithmetic
+ dirDate = datetime.date(int(year), int(month), int(day))
+ if dirDate < cutOffDate:
+ genprint('Should purge old directories, %s' % (dirpath,))
+ rmtree(dirpath)
+
+if __name__=="__main__":
+ parser = optparse.OptionParser(usage="cas-admin [opts] args")
+ parser.add_option("--server", dest="queryServer",
+ help="Add remote CAS servers",
+ action="store_true", default=False)
+ parser.add_option("-p","--purge", dest="purgeData",
+ help="Purge files default 90 days, customize with -d",
+ action="store_true", default=False)
+ parser.add_option("-d","--days", dest="purgeDataDays",
+ help="Set how many days back to purge data")
+ (opts, args) = parser.parse_args()
+ purgeData = opts.purgeData
+ purgeDataDays = opts.purgeDataDays
+ queryServer = opts.queryServer
+
+ """initialize CAS requirements
+ """
+ if os.getuid() is not 0:
+ raise RuntimeError, "You must be root(0), instead you are id(%d)" % (os.getuid())
+
+ genprint(_("Starting requirement processing and database creation."))
+ if not os.path.isdir(os.path.dirname(settings["DATABASE"])):
+ os.makedirs(os.path.dirname(settings["DATABASE"]))
+ if not os.path.isdir(settings["DEBUGS"]):
+ os.makedirs(settings["DEBUGS"])
+
+ # if autopurge is enabled lets clean up some stale data
+ if AUTOPURGE:
+ genprint(_('Autopurge enabled, purging stale data'))
+ purgeData(int(PURGELIMIT))
+
+ if purgeData:
+ ans = raw_input(_("You are about to purge data, is this what you " \
+ "really want to do? [Y/y/N/n]: "))
+ if ans=='Y' or ans=='y':
+ if not purgeDataDays:
+ purgeDataDays = PURGELIMIT
+ genprint(_("Beginning Purge going back %s day(s)" % (purgeDataDays,)))
+ purgeData(int(purgeDataDays))
+ return True
else:
- raise SystemExit(self.casLog.info("Missing options, please run with --help."))
+ return False
-if __name__=="__main__":
- app = CasAdminApplication(sys.argv[1:])
- sys.exit(app.run())
+ if queryServer:
+ genprint("Generating accessible CAS server list")
+ queryServers()
+ # Got all the pre-reqs out of the way; now build RPM list
+ queryLocalRpms(settings["DEBUGS"])
diff --git a/cas.spec b/cas.spec
index e40ffe6..6f69940 100644
--- a/cas.spec
+++ b/cas.spec
@@ -2,7 +2,7 @@
Name: cas
Summary: Tool to analyze and configure core file environment
-Version: 0.17
+Version: 0.18
Release: 0%{?dist}
Source0: https://fedorahosted.org/releases/c/a/cas/%{name}-%{version}.tar.gz
License: GPLv3+
@@ -12,15 +12,15 @@ BuildArch: noarch
Url: http://fedorahosted.org/cas
BuildRequires: python-devel
%if 0%{?rhel}
-Requires: python-sqlite python-paramiko
-%else
-Requires: python-paramiko
+Requires: python-sqlite
%endif
+Requires: python-paramiko
Requires: xz
Requires: crash
Requires: python-cherrypy
Requires: python-simplejson
Requires: python-urlgrabber
+Requires: python-sqlalchemy
%description
CAS provides a user the ability to configure an environment for core analysis
@@ -54,6 +54,10 @@ rm -rf ${RPM_BUILD_ROOT}
%doc AUTHORS LICENSE README PKG-INFO doc/*
%changelog
+* Mon May 3 2010 Adam Stokes <ajs at redhat dot com> - 0.18
+- Rewrote database interface using sqlalchemy
+- Reworked cas-admin to interface with new database format
+
* Mon Apr 26 2010 Adam Stokes <ajs at redhat dot com> - 0.17
- Add cas.conf man page
- Fix typo in 'smtphost' configuration parameter
diff --git a/caslib/core.py b/caslib/core.py
index a489b0b..9ae13d7 100644
--- a/caslib/core.py
+++ b/caslib/core.py
@@ -24,6 +24,19 @@ else:
class CoreException(Exception):
pass
+def casexecute(cmd, output=False):
+ """ wrapper to subprocess for running external applications
+
+ Arguments:
+ - `cmd`: shell command to run
+ - `output`: return std out
+ """
+ stdout, stderr = Popen(cmd, shell=True, bufsize=-1).communicate()
+ if output:
+ return stdout
+ else:
+ return
+
class CoreBase(object):
def __init__(self):
self.util = UtilBase()
diff --git a/caslib/db.py b/caslib/db.py
index 43582b2..8321fa3 100644
--- a/caslib/db.py
+++ b/caslib/db.py
@@ -1,136 +1,75 @@
-import os
-import sys
-
-if sys.version_info[:2] > (2,4):
- import sqlite3 as sqlite
-else:
- try:
- import sqlite
- except ImportError:
- raise SystemExit('Unable to determine sqlite, please make sure it is installed.')
-
-class CasStorageException(Exception): pass
-
-class CasStorage(object):
- def __init__(self, database):
- """ setup database connection and return db cursor for
- traversing database """
- self.db = database
- self.conn = None
- self.cursor = None
-
- def connect(self):
- """ execute connection """
- try:
- self.conn = sqlite.connect(self.db)
- self.cursor = self.conn.cursor()
- except:
- raise CasStorageException('Cannot connect to database')
- return
-
- def commit(self):
- self.conn.commit()
- return
-
- def buildTable(self):
- # We want to add jobs to a database just in case
- # there is no structured form of purging old data.
- self.cursor.execute("""create table jobs (
- job_id integer primary key autoincrement,
- identifier integer,
- date text,
- email text)
- """)
- self.cursor.execute("""create table debuginfo (
- debug_id integer primary key autoincrement,
- rpm text)
- """)
- self.cursor.execute("""create table timestamp (
- timestamp_id integer primary key autoincrement,
- debugpath text,
- timestamp text,
- debug_id integer)
- """)
- self.cursor.execute("""create table server (
- server_id integer primary key autoincrement,
- arch text,
- port text,
- hostname text)
- """)
- self.commit()
- return
-
- # DEBUGINFO METHODS
- def addDebuginfoRPM(self, debuginfo):
- debuginfo = (debuginfo,)
- self.cursor.execute('SELECT * from debuginfo where rpm="%s"' % debuginfo)
- if not self.cursor.fetchone():
- self.cursor.execute('INSERT into debuginfo(rpm) values("%s")' % debuginfo)
- self.commit()
- return
-
- def getAllDebuginfoRPM(self):
- self.cursor.execute("SELECT * FROM debuginfo")
- return self.cursor.fetchall()
-
- # TIMESTAMP METHODS
- def addTimestamp(self, id, debug, timestamp):
- """ build relation to debuginfo rpm and add debug path, timestamp """
- values = (id, debug, timestamp)
- self.cursor.execute('SELECT * FROM timestamp where timestamp="%s"' % (timestamp,))
- if not self.cursor.fetchone():
- self.cursor.execute('''INSERT into timestamp (debug_id,debugpath,
- timestamp) values(%d,"%s","%s")''' % values)
- self.commit()
- return
-
- def getTimestampDebug(self, timestamp):
- """ return timestamp based on extracted core timestamp """
- values = (timestamp,)
- self.cursor.execute('''SELECT rpm,debugpath
- FROM debuginfo, timestamp
- where timestamp.timestamp LIKE "%%%s%%"
- AND debuginfo.debug_id = timestamp.debug_id''' % values)
- return self.cursor.fetchone()
-
- # JOB METHODS
- def getAllJobs(self):
- """ return all current running jobs """
- self.cursor.execute('SELECT * FROM jobs')
- return self.cursor.fetchall()
-
- def getJobById(self, id):
- """ single job """
- self.cursor.execute('SELECT * FROM jobs where id=%d' % (id,))
- return self.cursor.fetchone()
-
- def getJobRange(self, days):
- """ provides jobs based on creation date from
- $days back
- """
- pass
-
- def addJob(self, identifier, date, email):
- """ add job to db """
- if email is None:
- email = "cas@localhost"
- values = (str(date), int(identifier), str(email))
- self.cursor.execute('''INSERT into jobs (date,identifier,email)
- values ("%s",%d, "%s")''' % values)
- self.commit()
- return
-
- # SERVER METHODS
- def addServer(self, hostname, port, arch):
- """ add server/arch to db """
- values = (hostname, port, arch)
- self.cursor.execute('select * from server where hostname="%s"' % (hostname,))
- if not self.cursor.fetchone():
- self.cursor.execute('''INSERT into server (hostname, arch, port)
- values ("%s","%s","%s")''' % values)
- self.commit()
- return
-
- def getServers(self):
- self.cursor.execute('select hostname, port, arch from server')
- return self.cursor.fetchall()
+# cas-db library
+# Interface to CAS database
+# Copyright (C) 2010 Adam Stokes
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Mapper Object
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
+Base = declarative_base()
+class RPM(Base):
+ __tablename__ = 'rpms'
+ rpmId = Column(Integer, primary_key=True)
+ rpmPath = Column(String(255))
+
+ def __init__(self, rpmPath):
+ self.rpmPath = rpmPath
+
+ def __repr__(self):
+ return "<RPM('%s')>" % (self.rpmPath,)
+
+class Timestamp(Base):
+ __tablename__ = 'timestamps'
+ stampId = Column(Integer, primary_key=True)
+ stampKey = Column(String(255))
+ debugPath = Column(String(255))
+ rpmId = Column(Integer, ForeignKey("rpms.rpmId"))
+
+ def __init__(self, stampKey, debugPath, rpmId):
+ self.stampKey = stampKey
+ self.debugPath = debugPath
+ self.rpmId = rpmId
+
+ def __repr__(self):
+ return "<Timestamp('%s','%s','%s')>" % (self.stampKey, self.debugPath, self.rpmId)
+
+class Server(Base):
+ __tablename__ = 'servers'
+ serverId = Column(Integer, primary_key=True)
+ hostname = Column(String(100))
+ port = Column(String(10))
+ arch = Column(String(40))
+
+ def __init__(self, hostname, port, arch):
+ self.hostname = hostname
+ self.port = port
+ self.arch = arch
+
+ def __repr__(self):
+ return "<Server('%s','%s','%s')>" % (self.hostname, self.port, self.arch)
+
+from sqlalchemy import create_engine
+from sqlalchemy.orm import sessionmaker
+# Read cas.conf for database path
+import ConfigParser
+config = ConfigParser.RawConfigParser()
+config.read("/etc/cas.conf")
+try:
+ dbpath = config.get('settings','database')
+except:
+ raise
+engine = create_engine('sqlite:///%s' % (dbpath,))
+Session = sessionmaker(bind=engine)
+Base.metadata.create_all(engine)
14 years