/[shmookey]/portconf/Installer.py


UCC Code Repository

Contents of /portconf/Installer.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 116 - (show annotations) (download) (as text)
Thu May 29 09:02:14 2008 UTC (12 years, 6 months ago) by shmookey
File MIME type: text/x-python
File size: 2999 byte(s)
sessioning at an application level now works, just need to add it to the web interface through cookies

1 ''' Installer.py - installation tools for PACT.
2 '''
3
4 import pgdb
5 import sys, os, pickle
6 import commands
7
8 class DBHostNotFound (Exception): pass
9 class DBNameNotFound (Exception): pass
10 class DBLoginFailed (Exception): pass
11 class NeedDBName (Exception): pass
12 class NeedDBUser (Exception): pass
13 class NeedDBPass (Exception): pass
14 class InvalidDatabaseSettings (Exception): pass
15 class DatabaseError (Exception): pass
16
17 def GetConf ():
18 try:
19 confile = open ("conf.pkl", "rb+")
20 conf = pickle.load (confile)
21 confile.close ()
22 except IOError:
23 conf = {}
24 except EOFError:
25 conf = {}
26 return conf
27
28 def SaveConf (conf):
29 confile = open ("conf.pkl", "wb+")
30 pickle.dump (conf, confile)
31 confile.close ()
32
33 def Set (name, value):
34 conf = GetConf ()
35 conf [name] = value
36 SaveConf (conf)
37 if not name == "dbpass":
38 return conf[name]
39 else: return "******"
40
41 def Get (name):
42 conf = GetConf ()
43 return conf [name]
44
45 # Fill unfilled values with defaults if available, otherwise raise an exception.
46 # Then do the installation.
47 def Test ():
48 conf = GetConf ()
49 if not conf.has_key ("pyciscopath"):
50 conf ["pyciscopath"] = os.path.abspath (os.path.dirname (sys.argv[0]) + "/pycisco/")
51 if not conf.has_key ("pyaaapath"):
52 conf ["pyaaapath"] = os.path.abspath (os.path.dirname (sys.argv[0]) + "/pyaaa/")
53 if not conf.has_key ("dbhost"):
54 conf ["dbhost"] = "localhost"
55 if not conf.has_key ("dbport"):
56 conf ["dbport"] = "5432"
57 if not conf.has_key ("dbpref"):
58 conf ["dbpref"] = "pact"
59 if not conf.has_key ("dbname"):
60 raise NeedDBName ()
61 if not conf.has_key ("dbuser"):
62 raise NeedDBUser ()
63 if not conf.has_key ("dbpass"):
64 raise NeedDBPass ()
65 SaveConf (conf)
66
67 def Install ():
68 Test ()
69 conf = GetConf ()
70 try:
71 db = pgdb.connect (host = conf["dbhost"] + ":" + conf["dbport"], user = conf["dbuser"], password = conf["dbpass"], database = conf["dbname"])
72 except pgdb.InternalError:
73 raise InvalidDatabaseSettings ()
74 cur = db.cursor ()
75 sqlfile = open ("sql/postgre.sql", 'r')
76 try:
77 for command in ''.join (sqlfile.readlines ()).split ("#"): cur.execute (command.replace ("TABLEPREFIX", conf["dbpref"]))
78 db.commit ()
79 except pgdb.DatabaseError:
80 raise DatabaseError ()
81 return True
82
83 def Uninstall ():
84 Test ()
85 conf = GetConf ()
86 try:
87 db = pgdb.connect (host = conf["dbhost"] + ":" + conf["dbport"], user = conf["dbuser"], password = conf["dbpass"], database = conf["dbname"])
88 pref = conf["dbpref"]
89 except pgdb.InternalError:
90 raise InvalidDatabaseSettings ()
91 cur = db.cursor ()
92 sqlfile = open ("sql/postgre.sql", 'r')
93 try:
94 cur.execute ("DROP TABLE %s_users" % pref)
95 cur.execute ("DROP TABLE %s_permissions" % pref)
96 cur.execute ("DROP TABLE %s_memberships" % pref)
97 cur.execute ("DROP TABLE %s_groups" % pref)
98 cur.execute ("DROP TABLE %s_sessions" % pref)
99 cur.execute ("DROP TABLE %s_parameters" % pref)
100 cur.execute ("DROP TABLE %s_NetworkDevice" % pref)
101 db.commit ()
102 except pgdb.DatabaseError:
103 raise DatabaseError ()
104 commands.getoutput ("rm conf.pkl")
105 return True

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26