Created
November 24, 2014 07:42
-
-
Save cschwede/e862bb2c7045f6075083 to your computer and use it in GitHub Desktop.
Sample how to find and access object and container data on a Swift storage node.
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 (c) 2014 Christian Schwede <[email protected]> | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | |
# implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
import os | |
import cPickle as pickle | |
import sqlite3 | |
import xattr | |
def _get_acc_cont_obj(filename): | |
obj_fd = open(filename) | |
metadata = '' | |
key = 0 | |
try: | |
while True: | |
metadata += xattr.getxattr( | |
obj_fd, '%s%s' % ("user.swift.metadata", (key or ''))) | |
key += 1 | |
except IOError: | |
pass | |
obj_fd.close() | |
return pickle.loads(metadata).get('name') | |
def _get_object_names(filename): | |
conn = sqlite3.connect(filename) | |
c = conn.cursor() | |
c.execute('SELECT account, container FROM container_stat;') | |
account, container = c.fetchone() | |
objects = [] | |
for obj in c.execute('SELECT name FROM object;'): | |
objname = obj[0] | |
if objname: | |
name = "/%s/%s/%s" % (account, container, objname) | |
if unicode(name): | |
name = name.encode('utf8') | |
objects.append(name) | |
return objects | |
if __name__ == "__main__": | |
for root, _dirs, files in os.walk(".", followlinks=True): | |
if "quarantined" in root: | |
continue | |
for filename in files: | |
fullname = os.path.join(root, filename) | |
if fullname.split('.')[-1] == "data": | |
objname = _get_acc_cont_obj(fullname) | |
# Do something here with the object data file, for example: | |
print fullname, objname | |
if fullname.split('.')[-1] == "db": | |
objects = _get_object_names(fullname) | |
# Do something here with the container data, for example: | |
print fullname, objects |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment