Ejemplo n.º 1
0
def find_machine_state(conn):
    """
    locate this machine's state in the database.
    """
    try:
        verify_db_machine_state(conn)  # First make sure our DB tables are all in order.
        result = r.db('wanwipe').table('machine_state').get_all(get_boot_id(), index='boot_id').run(conn)
        for document in result:  # Look over the returned documents. There should only be one, boot_id is unique.
            print("{}: BaseDB: machine_state query found a matching document: {}".format(dt.isoformat(dt.now()), document), file=sys.stderr)
            if document.get('machine_id') == get_dbus_machine_id():  # Found a current state for this machine.
                return document.get('id')  # Return the current state, skipping the below.
        print("{}: BaseDB: couldn't find any machine_states. Creating new state.".format(dt.isoformat(dt.now())), file=sys.stderr)  # We didn't return above, so...
        return create_machine_state(conn)  # Just create a machine state and return it if none exists.
    except RqlRuntimeError as kaboom:
        print("{}: BaseDB: machine_state lookup failed somehow: {}".format(dt.isoformat(dt.now()), kaboom), file=sys.stderr)
Ejemplo n.º 2
0
def create_machine_state(conn):
    """
    create this machine's base state in the database.
    """
    machine_id = get_dbus_machine_id()
    boot_id = get_boot_id()
    my_ip = get_global_ip()

    try:
        inserted = r.db('wanwipe').table('machine_state').insert({
            'machine_id': machine_id, 'boot_id': boot_id,
            'ip': my_ip, 'updated_at': r.now()
        }).run(conn)
        print("{}: BaseDB: machine_state created: {}".format(dt.isoformat(dt.now()), inserted['generated_keys'][0]), file=sys.stderr)
        return inserted['generated_keys'][0]
    except RqlRuntimeError as kaboom:
        print("{}: BaseDB: machine_state creation failed somehow: {}".format(dt.isoformat(dt.now()), kaboom), file=sys.stderr)