-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
name: gravel-common | ||
requires-apt: python2.7 python-gdbm python-yaml | ||
requires-apt: python2.7 python-tdb python-yaml |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import graveldb | ||
|
||
class Screw(graveldb.Table('Screw', '/tmp')): | ||
default = lambda self: graveldb.Object(size=1) | ||
|
||
s = Screw('foo') | ||
with s: | ||
s.data.size += 1 | ||
s.save() | ||
|
||
s = Screw('foo') | ||
print s.data.size |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import shelve | ||
import tdb | ||
import os | ||
|
||
class TDBShelf(shelve.Shelf): | ||
def __init__(self, path): | ||
self.db = tdb.open(path, flags=os.O_RDWR | os.O_CREAT) | ||
shelve.Shelf.__init__(self, self.db) | ||
|
||
def lock_all(self): | ||
self.db.lock_all() | ||
|
||
def unlock_all(self): | ||
self.db.unlock_all() | ||
|
||
def Table(name, path): | ||
return type(name, (_Table,), {'table': TDBShelf(path + '/' + name)}) | ||
|
||
class _Table(object): | ||
def __init__(self, name): | ||
self.name = name | ||
self.data = self.table.get(name, self.default()) | ||
|
||
def save(self): | ||
self.table[self.name] = self.data | ||
|
||
def __enter__(self): | ||
self.table.lock_all() | ||
|
||
def __exit__(self, *args): | ||
self.table.unlock_all() | ||
|
||
default = lambda self: Object() | ||
|
||
class Object(object): | ||
def __init__(self, **kwargs): | ||
for k, v in kwargs.items(): | ||
setattr(self, k, v) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import tempfile | ||
import os | ||
|
||
def reformat_ssh_key(data): | ||
''' Returns data as a well formed ssh-rsa key ''' | ||
split = data.split(None, 2) | ||
if len(split) < 2: | ||
raise ValueError('Not enough values in SSH key.') | ||
|
||
if len(split) == 2: | ||
split = split + ['',] | ||
|
||
type, value, author = split | ||
|
||
if type != 'ssh-rsa': | ||
raise ValueError('Wrong key type.') | ||
|
||
data = value.decode('base64').encode('base64').replace('\n', '') | ||
author = urllib.quote(author.strip()) | ||
|
||
return '%s %s %s' % (type, data, author) | ||
|
||
def get_ssh_key_fingerprint(data): | ||
key = reformat_ssh_key(data).split(None, 2)[1] | ||
fp_plain = hashlib.md5(key).hexdigest() | ||
return ':'.join( a + b for a,b in zip(fp_plain[::2], fp_plain[1::2]) ) | ||
|
||
def write_authorized_keys(keys): | ||
''' | ||
Replaces autogenerated part of authorized_keys. | ||
Command from keys is not escaped in any way, but keys are!!! | ||
''' | ||
AUTOGEN_START = '### START AUTOGENERATED ###\n' | ||
AUTOGEN_END = '### END AUTOGENERATED ###\n' | ||
path = os.path.expandsuer('~/.ssh/authorized_keys') | ||
out = tempfile.NamedTemporaryFile(delete=False) | ||
|
||
if os.path.exists(path): | ||
autogen = False | ||
for line in open(path): | ||
if line == AUTOGEN_START: | ||
autogen = True | ||
elif line == AUTOGEN_END: | ||
autogen = False | ||
elif not autogen: | ||
out.write(line) | ||
|
||
out.write(AUTOGEN_START) | ||
for command, key in keys: | ||
escaped_key = reformat_ssh_key(key) | ||
out.write('command="%s",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty %s\n' | ||
% (command, escaped_key)) | ||
out.write(AUTOGEN_END) | ||
out.close() | ||
os.rename(out.name, path) |