diff options
author | Rob Austein <sra@hactrn.net> | 2016-04-27 22:28:36 +0000 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2016-04-27 22:28:36 +0000 |
commit | bdc3a6ef48a3ebbd44b4f51d274a21b24aac8eb9 (patch) | |
tree | 65024e21ccc4e6186a47e0a8404e6adadd08c579 /potpourri | |
parent | bf96f5f690cb418ed00f77a32f692015b03a4969 (diff) | |
parent | 319916e90e1b1f6328effbc7cd9acf74c38c1842 (diff) |
Pull ca-{pickle,unpickle}.py from trunk/.
svn path=/branches/tk705/; revision=6396
Diffstat (limited to 'potpourri')
-rwxr-xr-x | potpourri/ca-pickle.py | 95 | ||||
-rwxr-xr-x | potpourri/ca-unpickle.py | 26 |
2 files changed, 121 insertions, 0 deletions
diff --git a/potpourri/ca-pickle.py b/potpourri/ca-pickle.py new file mode 100755 index 00000000..92741853 --- /dev/null +++ b/potpourri/ca-pickle.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python + +# $Id$ + +""" +Package up state of an old (pre-rpkidb, pre-pubdb, pre-Django 1.8) +RPKI CA installation as a Python pickle database, for later re-loading +into a more recent version of the code using a companion script. +""" + +import os +import sys +import cPickle +import argparse +import subprocess +import rpki.config +import rpki.version +import rpki.autoconf + +from rpki.mysql_import import MySQLdb, _mysql_exceptions + +parser = argparse.ArgumentParser(description = __doc__) +parser.add_argument("-c", "--config", + help = "specify alternate location for rpki.conf") +parser.add_argument("-p", "--protocol", + choices = (0, 1, 2), type = int, default = 2, + help = "pickling protocol to use") +parser.add_argument("output", + help = "output file") +args = parser.parse_args() + +cfg = rpki.config.parser(args.config) + +databases = {} + +for section in ("rpkid", "irdbd", "pubd"): + db = MySQLdb.connect(db = cfg.get(section = section, option = "sql-database"), + user = cfg.get(section = section, option = "sql-username"), + passwd = cfg.get(section = section, option = "sql-password")) + tables = {} + + cur = db.cursor() + cur.execute("SHOW TABLES") + table_names = tuple(row[0] for row in cur.fetchall()) + cur.close() + + cur = db.cursor(MySQLdb.cursors.DictCursor) + for name in table_names: + cur.execute("SELECT * FROM " + name) + tables[name] = cur.fetchall() + cur.close() + + db.close() + + databases[section] = tables + +filenames = [cfg.filename] +raw_config = {} +cooked_config = {} + +for section in cfg.cfg.sections(): + for option in cfg.cfg.options(section): + raw_config[section, option] = cfg.cfg.get(section = section, option = option) + cooked_config[section, option] = cfg.get(section = section, option = option) + if os.path.isfile(cooked_config[section, option]): + filenames.append(cooked_config[section, option]) + +for i, fn in enumerate(filenames): + filenames[i] = os.path.abspath(fn) + +files = {} + +for filename in filenames: + with open(filename, "rb") as f: + files[filename] = f.read() + +world = dict( + version = rpki.version.VERSION, + rpki_conf = filenames[0], + databases = databases, + files = files, + raw_config = raw_config, + cooked_config = cooked_config) + +xz = subprocess.Popen( + ("xz", "-C", "sha256"), + stdin = subprocess.PIPE, + stdout = os.open(args.output, os.O_WRONLY | os.O_CREAT, 0600)) + +cPickle.dump(world, xz.stdin, args.protocol) + +xz.stdin.close() + +if xz.wait() != 0: + sys.exit("XZ pickling failed with code {}".format(xz.returncode)) diff --git a/potpourri/ca-unpickle.py b/potpourri/ca-unpickle.py new file mode 100755 index 00000000..dbbe3e8a --- /dev/null +++ b/potpourri/ca-unpickle.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python + +# $Id$ + +""" +Unpickle CA state packaged by ca-pickle. + +This version is a stub, and exists only to test ca-pickle. +""" + +import sys +import cPickle +import argparse +import subprocess + +parser = argparse.ArgumentParser(description = __doc__) +parser.add_argument("input", help = "input file") +args = parser.parse_args() + +xzcat = subprocess.Popen(("xzcat", args.input), stdout = subprocess.PIPE) +world = cPickle.load(xzcat.stdout) +if xzcat.wait() != 0: + sys.exit("XZ unpickling failed with code {}".format(xz.returncode)) + +print "import datetime" +print "world =", repr(world) |