Пример #1
0
from time import time

def choose(random_value, percentages, default=None):
    n = 0
    for name, percentage in percentages:
        if random_value in range(n, n + percentage):
            return name
        else:
            n = n + percentage
    return default

def _now():
    return int(round(time()))

subscribe('whooshingby', 'fact', 'completed_task')
while True:
    fact = get_next_fact('whooshingby', 'completed_task')
    if not fact:
        continue

    percentages_fact = get_newest_fact('whooshingby', 'reward_percentages', {})
    reward_percentages = loads(percentages_fact['percentages'])

    random_value = (hash(fact['name']) * 61 + fact['time'] * 47) % 100
    name = choose(random_value, reward_percentages)
    if name:
        content = {'name': name, 'task_id': fact['task_id'],
                   'source': 'python', 'time': _now()}
        if not store_opinion('whooshingby', 'reward', content):
            print "Could not store reward opinion" # handle better
Пример #2
0
    subscription_facts = []
    for f in get_all_facts('kropotkin', 'factspace', {}):
        subscription_facts += get_all_facts(f['name'], 'subscription', {})

    for f in component_facts:
        if f['name'] != 'executioner':
            execute_pid(f['identifier'])
    for f in subscription_facts:
        pid = extract_local_process(f['queue'])
        if pid is not None:
            execute_pid(pid)

def execute_pid(pid):
    try:
        kill(pid, SIGTERM)
    except OSError, err:
        stderr.write('Exception when killing %d: %s\n' % (pid, str(err)))

if __name__=="__main__":
    subscribe('kropotkin', 'fact', 'stop_requested')
    while True:
        stop_fact = get_next_fact('kropotkin', 'stop_requested')
        if stop_fact and stop_fact['location'] in [THIS_MACHINE, 'all']:
            identifier = stop_fact['identifier']
            if identifier == 'all':
                execute_all()
                break
            else:
                pid = int(identifier)
                execute_pid(pid)
Пример #3
0
def get_unique_executable(files):
    executables = [f for f in files if access(f, X_OK)]
    return executables[0] if len(executables) == 1 else None

HASHBANG_TYPE = {'#!/usr/bin/python': 'python'}
EXTENSION_TYPE = {'py':   'python',
                  'js':   'javascript',
                  'html': 'html'}
def language_type(filename):
    if access(filename, X_OK):
        with open(filename, 'r') as e:
            hashbang = e.readline()[:-1]
            return HASHBANG_TYPE.get(hashbang)
    else:
        extension = filename.split('.')[-1]
        return EXTENSION_TYPE[extension]

def archive(files):
    with closing(StringIO()) as buffer:
        with taropen(mode='w', fileobj=buffer) as tar:
            for f in files:
                tar.add(f, arcname=basename(f))
        return b64encode(buffer.getvalue())

if __name__=="__main__":
    subscribe('kropotkin', 'fact', 'component_available')
    while True:
        fact = get_next_fact('kropotkin', 'component_available')
        publish(fact['location'])
Пример #4
0
#!/usr/bin/python
from base64 import b64encode
from hashlib import sha512
from json import loads
from kropotkin import get_next_fact, store_fact, subscribe
from os import urandom

def generate_salt():
    return b64encode(urandom(24))

def create_hash(salt, password):
    return sha512(salt+password).hexdigest()

subscribe('whooshingby', 'fact', 'registration_request')
while True:
    request = get_next_fact('whooshingby', 'registration_request')
    if not request:
        continue

    name = request['name']
    password = request['password']
    salt = generate_salt()
    hash = create_hash(salt, password)
    content = {'name': name, 'salt': salt, 'hash': hash}
    if not store_fact('whooshingby', 'user', content):
        stderr.write('Could not store user fact, name %s\n' % name)
Пример #5
0
               'identifier': process.pid,
               'stdout_file': stdout_file.name,
               'stderr_file': stderr_file.name}

    if store_deployed_fact is True:
        if not store_fact('kropotkin', 'component_deployed', content):
            stderr.write('Cannot store component_deployed fact for %s\n' %name)
    return content

def get_unique_executable(directory):
    nodes = listdir(directory)
    executables = [f for f in nodes if is_executable_file(join(directory, f))]
    return join(directory, executables[0]) if len(executables) == 1 else None

def is_executable_file(f):
    return (not isdir(f)) and access(f, X_OK)

def open_output_file(directory, process_name, file_name):
    basename = '.'.join([process_name, file_name, 'log'])
    filename = join(directory, basename)
    return open(filename, 'w')

if __name__=="__main__":
    subscribe('kropotkin', 'fact', 'component')
    while True:
        component_fact = get_next_fact('kropotkin', 'component')
        if component_fact['content_type'] == 'component-tar':
            name = component_fact['name']
            directory = unpack(name, component_fact['bytes'])
            deploy(name, directory)
Пример #6
0
def get_unique_executable(directory):
    nodes = listdir(directory)
    executables = [f for f in nodes if is_executable_file(join(directory, f))]
    return join(directory, executables[0]) if len(executables) == 1 else None

def is_executable_file(f):
    return (not isdir(f)) and access(f, X_OK)

MODULE_TYPES = {'python':     'python-module',
                'javascript': 'javascript-library',
                'ruby':       'ruby-gem'}
CAN_BUILD = environ['KROPOTKIN_CAN_BUILD'].split(',')

subscribe('kropotkin', 'fact', 'library_available')
while True:
    fact = get_next_fact('kropotkin', 'library_available')

    original_dir = fact['directory']
    language = fact['language']

    if language not in CAN_BUILD:
        print_stdout('Skipping library %s as language %s not buildable' \
                     % (original_dir, language))
        continue

    build_dir = mkdtemp(prefix='library-build-%s' % basename(original_dir))
    output_dir = mkdtemp(prefix='library-%s' % basename(original_dir))
    copy_all(original_dir, build_dir)

    executable = get_unique_executable(build_dir)
    if not executable:
Пример #7
0
#!/usr/bin/python
from kropotkin import get_next_fact, store_fact, subscribe
from os.path import join
from tempfile import mkdtemp

def create_factspace(name, directory):
    if not directory:
        directory = mkdtemp()

    content = {'name': name, 'directory': directory}
    if not store_fact('kropotkin', 'factspace', content):
        raise Exception("Cannot store factspace fact")

    return directory

if __name__=="__main__":
    subscribe('kropotkin', 'fact', 'factspace_wanted')
    while True:
        factspace_fact = get_next_fact('kropotkin', 'factspace_wanted')
        if factspace_fact:
            create_factspace(factspace_fact['name'],
                             factspace_fact['directory'])