-
-
Save hansen-m/58667f370047af92f634 to your computer and use it in GitHub Desktop.
BES Archiver Utility (Example for using python-besapi), will create a full BES file console export at the current working directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# Copyright 2014 The Pennsylvania State University. | |
# | |
""" | |
BESArchiver.py | |
Created by Matt Hansen ([email protected]) on 2014-05-27. | |
Easily export content using the BES (BigFix) REST API | |
""" | |
import os | |
import string | |
import besapi | |
from ConfigParser import SafeConfigParser | |
""" | |
# Example file contents of besapi.conf | |
# Place at /etc/besapi.conf, ~/besapi.conf or ./besapi.conf | |
[besapi] | |
BES_ROOT_SERVER = https://your.bes.rootserver.org:52311 | |
BES_USER_NAME = YOUR_USERNAME_HERE | |
BES_PASSWORD = YOUR_PASSWORD_HERE | |
[besarchiver] | |
VERBOSE = False | |
""" | |
# Sanitize Text | |
def sani_txt(*args): | |
""" Clean arbitrary text for safe file system usage.""" | |
valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits) | |
sani_args = [] | |
for arg in args: | |
sani_args.append(''.join(c for c in | |
str(arg).replace('/', '-') | |
if c in valid_chars)) | |
return tuple(sani_args) | |
# Read Config File | |
CONFPARSER = SafeConfigParser({'VERBOSE': 'True'}) | |
CONFPARSER.read(['/etc/besapi.conf', | |
os.path.expanduser('~/besapi.conf'), | |
'besapi.conf']) | |
BES_ROOT_SERVER = CONFPARSER.get('besapi', 'BES_ROOT_SERVER') | |
BES_USER_NAME = CONFPARSER.get('besapi', 'BES_USER_NAME') | |
BES_PASSWORD = CONFPARSER.get('besapi', 'BES_PASSWORD') | |
if 'besarchiver' in CONFPARSER.sections(): | |
VERBOSE = CONFPARSER.getboolean('besarchiver', 'VERBOSE') | |
else: | |
VERBOSE = True | |
# Create API Connection | |
B = besapi.BESConnection(BES_USER_NAME, BES_PASSWORD, BES_ROOT_SERVER) | |
# Iterate Over All Sites | |
for site in B.get('sites').besobj.iterchildren(): | |
resource = site.attrib['Resource'].replace('http://', 'https://') | |
# Iterate Over All Site Content | |
content = B.get(resource + '/content') | |
if content.request.status_code == 200: | |
print "Archiving %d items from %s..." % ( | |
content().countchildren(), site.Name) | |
for task in content().iterchildren(): | |
if VERBOSE: | |
print "{%s} (%s) [%s] %s - %s " % ( | |
site.Name, | |
task.tag, | |
task.ID, | |
task.Name.text.encode("ascii", "ignore"), | |
task.attrib['LastModified']) | |
# Get Specific Content | |
content = B.get(task.attrib['Resource'].replace( | |
'http://', 'https://')) | |
# Write Content to Disk | |
if content: | |
if not os.path.exists("%s/%s" % sani_txt(site.Name, task.tag)): | |
os.makedirs("%s/%s" % sani_txt(site.Name, task.tag)) | |
with open("%s/%s/%s - %s.bes" % | |
sani_txt(site.Name, | |
task.tag, | |
task.ID, | |
task.Name.text.encode("ascii", "ignore")) | |
, "wb") as bes_file: | |
bes_file.write(content.text.encode('utf-8')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This functionality has been moved into BESAPI wrapper itself: https://github.com/jgstew/besapi/blob/master/src/besapi/besapi.py#L642-L656