Esempio n. 1
0
def main(argv=sys.argv):

    if len(argv) < 3:
        usage(argv)

    config_uri = argv[1]
    request = init_websauna(config_uri)

    if len(argv) >= 4:
        password = argv[3]
    else:
        password = getpass.getpass("Password:")
        password2 = getpass.getpass("Password (again):")

        if password != password2:
            sys.exit("Password did not match")

    admin = False
    if len(argv) == 5:
        # TODO: use optparse
        if argv[4] == "--admin":
            admin = True

    with transaction.manager:
        u = create(request, email=argv[2], username=argv[2], password=password, admin=admin)
        print("Created user #{}: {}, admin: {}".format(u.id, u.email, u.is_admin()))
Esempio n. 2
0
def main(argv=sys.argv):

    def usage(argv):
        cmd = os.path.basename(argv[0])
        print('usage: %s <config_uri> <network name>\n'
              '(example: "%s conf/production.ini testnet")' % (cmd, cmd))
        sys.exit(1)

    if len(argv) < 2:
        usage(argv)

    config_uri = argv[1]
    network_name = argv[2]

    # console_app sets up colored log output
    from websauna.system.devop.cmdline import init_websauna
    request = init_websauna(config_uri, sanity_check=True)

    services = ServiceCore.parse_network_config(request)
    one_shot = OneShot(request, network_name, services[network_name], require_unlock=False)
    one_shot.setup()

    coinbase = one_shot.web3.eth.coinbase

    pw = getpass("Give password to unlock {} on {}:".format(coinbase, network_name))

    one_shot.web3.personal.unlockAccount(coinbase, pw, 30*24*3600)

    print("Unlock complete")
    sys.exit(0)
Esempio n. 3
0
def task_app_request(task_ini_file, ini_settings):
    request = init_websauna(task_ini_file)

    if ini_settings["sqlalchemy.url"].startswith("sqlite://"):
        pytest.skip("These tests are run only for PostgreSQL database")

    return request
Esempio n. 4
0
def main(argv=sys.argv):

    if len(argv) < 3:
        usage(argv)

    config_uri = argv[1]
    request = init_websauna(config_uri)

    User = get_user_class(request.registry)
    dbsession = request.dbsession

    if len(argv) == 4:
        password = argv[3]
    else:
        password = getpass.getpass("Password:")
        password2 = getpass.getpass("Password (again):")

        if password != password2:
            sys.exit("Password did not match")

    with transaction.manager:
        u = User(email=argv[2], username=argv[2])
        u.password = password
        u.registration_source = "command_line"
        u.activated_at = now()
        dbsession.add(u)
        dbsession.flush()

        request.registry.notify(UserCreated(request, u))

        print("Created user #{}: {}, admin: {}".format(u.id, u.email, u.is_admin()))
Esempio n. 5
0
def main(argv=sys.argv):

    if len(argv) < 2:
        usage(argv)

    config_uri = argv[1]

    request = init_websauna(config_uri)

    imported_objects = OrderedDict()

    imported_objects["request"] = request
    imported_objects["dbsession"] = request.dbsession
    imported_objects["transaction"] = transaction
    imported_objects["redis"] = get_redis(request)
    imported_objects["now"] = now
    imported_objects["datetime"] = datetime

    for name, cls in Base._decl_class_registry.items():

        if name == "_sa_module_registry":
            continue

        imported_objects[name] = cls

    print("")
    print("Following classes and objects are available:")
    for var, val in imported_objects.items():
        print("{:30}: {}".format(var, str(val).replace("\n", " ").replace("\r", " ")))
    print("")

    embed(user_ns=imported_objects)
Esempio n. 6
0
def main(argv=sys.argv):

    if len(argv) < 2:
        usage(argv)

    config_uri = argv[1]
    request = init_websauna(config_uri, sanity_check=False)
    request.registry.static_asset_policy.collect_static()
    sys.exit(0)
Esempio n. 7
0
def main(argv=sys.argv):

    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]

    request = init_websauna(config_uri)
    with transaction.manager:
        engine = request.dbsession.get_bind()
        Base.metadata.create_all(engine)
Esempio n. 8
0
def main(argv=sys.argv):

    if len(argv) < 2:
        usage(argv)

    config_uri = argv[1]
    try:
        request = init_websauna(config_uri, sanity_check=True)
    except SanityCheckFailed:
        print("It did not go that well.")
        sys.exit(10)

    print("All good.")
    sys.exit(0)
Esempio n. 9
0
def main(argv=sys.argv):

    if len(argv) < 2:
        usage(argv)

    config_uri = argv[1]
    request = init_websauna(config_uri)
    engine = request.dbsession.get_bind()

    for name, cls in Base._decl_class_registry.items():

        if name == "_sa_module_registry":
            continue

        print(CreateTable(cls.__table__, bind=engine))
Esempio n. 10
0
def main(argv=sys.argv):

    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]

    request = init_websauna(config_uri)
    with transaction.manager:
        engine = request.dbsession.get_bind()

        # Always enable UUID extension for PSQL
        # TODO: Convenience for now, because we assume UUIDs, but make this somehow configurable
        engine.execute('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"')

        Base.metadata.create_all(engine)
Esempio n. 11
0
def main(argv=sys.argv):
    monkey_patch_paster_config_parser()

    if len(argv) < 2:
        usage(argv)

    config_uri = argv[1]

    # Print out the connection URL with the password masked out
    request = init_websauna(config_uri)
    url = request.registry.settings.get("sqlalchemy.url")

    engine = request.dbsession.get_bind()
    print("Connecting to {}".format(engine))

    os.system("pgcli {}".format(url))
Esempio n. 12
0
    def on_worker_init(self):
        """This method is called when a child process starts."""

        # TODO Make sanity_check True by default,
        # but make it sure we can disable it in tests,
        # because otherwise tests won't run
        self.request = init_websauna(ini_file, sanity_check=False)

        #: Associate this process as Celery app object of the environment
        self.request.registry.celery = self.app

        #: Associate request for celery app, so
        #: task executor knows about Request object
        self.app.cmdline_request = self.request

        self.register_tasks()
Esempio n. 13
0
def main(argv=sys.argv):

    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]

    request = init_websauna(config_uri)

    # Export all secrets and settings
    bash_env = create_settings_env(request.registry)

    # subprocess.check_output([DUMP_SCRIPT] + args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env)
    args = argv[2:]
    cmd = [DUMP_SCRIPT] + args
    with subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=1, env=bash_env, universal_newlines=True) as p:
        for line in p.stdout:
            print(line, end='')
Esempio n. 14
0
def main(argv=sys.argv):

    def usage(argv):
        cmd = os.path.basename(argv[0])
        print('usage: %s <config_uri>\n'
              '(example: "%s conf/production.ini")' % (cmd, cmd))
        sys.exit(1)

    if len(argv) < 2:
        usage(argv)

    config_uri = argv[1]

    # console_app sets up colored log output
    request = init_websauna(config_uri, sanity_check=True)

    run_services(request)
    sys.exit(0)
Esempio n. 15
0
def main(argv=sys.argv):
    size = 200
    tags_rel_min = 5
    tags_rel_max = 30

    config_uri = argv[1]

    request = init_websauna(config_uri)
    base_fakefactory.DB_SESSION_PROXY.session = request.dbsession

    with request.tm:
        user = get_or_create_user(request, email="[email protected]", password="qwerty")
        tags = fakefactory.TagFactory.create_batch(size=size)
        posts = fakefactory.PostFactory.create_batch(
            size=size,
            author=user,
            tags=random.sample(tags, k=random.randint(tags_rel_min, tags_rel_max)),
        )
Esempio n. 16
0
def main(argv=sys.argv):
    monkey_patch_paster_config_parser()

    if len(argv) < 2:
        usage(argv)

    config_uri = argv[1]

    # Print out the connection URL with the password masked out
    request = init_websauna(config_uri)
    url = request.registry.settings.get("sqlalchemy.url")

    engine = request.dbsession.get_bind()
    print("Connecting to {}".format(engine))

    if not which("pgcli"):
        sys.exit("pgcli not installed - please install websauna as pip install websauna[dev] to get this dependency")

    os.system("pgcli {}".format(url))
Esempio n. 17
0
def one_shot_main(argv=sys.argv):
    """Run one debug cycle"""

    def usage(argv):
        cmd = os.path.basename(argv[0])
        print('usage: %s <config_uri> <network name>\n'
              '(example: "%s conf/production.ini") ethereum' % (cmd, cmd))
        sys.exit(1)

    if len(argv) < 3:
        usage(argv)

    config_uri = argv[1]
    network_name = argv[2]

    # console_app sets up colored log output
    request = init_websauna(config_uri, sanity_check=True)
    one_shot(request, network_name)

    sys.exit(0)
Esempio n. 18
0
def main(argv=sys.argv):

    def usage(argv):
        cmd = os.path.basename(argv[0])
        print('usage: %s <config_uri>\n'
              '(example: "%s conf/production.ini")' % (cmd, cmd))
        sys.exit(1)

    if len(argv) < 2:
        usage(argv)

    config_uri = argv[1]

    # console_app sets up colored log output
    from websauna.system.devop.cmdline import init_websauna
    request = init_websauna(config_uri, sanity_check=True)

    bootstrap(request)
    print("Bootstrap complete")
    sys.exit(0)
Esempio n. 19
0
def main(argv=sys.argv):

    def usage(argv):
        cmd = os.path.basename(argv[0])
        print('usage: %s <config_uri> <network name>\n'
              '(example: "%s conf/production.ini")' % (cmd, cmd))
        sys.exit(1)

    if len(argv) < 2:
        usage(argv)

    config_uri = argv[1]

    # console_app sets up colored log output
    from websauna.system.devop.cmdline import init_websauna
    request = init_websauna(config_uri, sanity_check=True)

    # Get list of configured networks
    services = ServiceCore.parse_network_config(request)
    redis = get_redis(request)

    for network_name in services.keys():
        # Update each network separately and have a lock to ensure we don't
        # accidentally do two overlapping update runs
        # https://pypi.python.org/pypi/python-redis-lock
        lock = redis_lock.Lock(redis, "network-update-lock-{}".format(network_name))

        if not lock.acquire(blocking=False):
            # This network is still procesing pending operations from the previous task run
            print("Lock {} is blocked, reseting".format(network_name))
            lock.reset()
        else:
            lock.release()

    print("Unlock complete")
    sys.exit(0)
Esempio n. 20
0
def run_alembic(package:str):
    """Alembic env.py script entry point for Websauna application.

    Initialize the application, load models and pass control to Alembic migration handler.

    :param package: String of the Python package name whose model the migration concerns. E.g. the name of the current Websauna package.
    """
    global logger
    global version_table

    current_package = package

    # this is the Alembic Config object, which provides
    # access to the values within the .ini file in use.
    config = context.config

    # This was -c passed to ws-alembic command
    config_file = config.config_file_name

    setup_logging(config_file)

    # Load the WSGI application, etc.
    request = init_websauna(config_file)
    engine = request.dbsession.get_bind()

    # Delay logger creation until we have initialized the logging system
    logger = logging.getLogger(__name__)

    # Extract database connection URL from the settings
    url = request.registry.settings["sqlalchemy.url"]

    allowed_packages = parse_allowed_packages(current_package)

    #: Pull out MetaData instance for the system
    target_metadata = get_sqlalchemy_metadata(allowed_packages)

    # Each package needs to maintain its own alembic_history table
    version_table = get_migration_table_name(allowed_packages, current_package)

    def include_object(object, name, type_, reflected, compare_to):
        """
        """

        # Try to figure out smartly table from different object types
        if type_ in ("index", "column", "foreign_key_constraint"):
            table_name = object.table.name
        elif type_ == "table":
            table_name = object.name
        else:
            raise RuntimeError("Don't know how to check type for migration inclusion list: {}".format(type_))

        model = get_class_by_table_name(table_name)
        if not model:
            # Don't know what's this... let's skip
            print("No model available", object, type_, table_name)
            return False

        module = model.__module__

        # XXX: This is not very beautiful check but works for now
        return consider_module_for_migration(module, allowed_packages)

    if context.is_offline_mode():
        run_migrations_offline(url, target_metadata, version_table, include_object)
    else:
        logger.info("Starting online migration engine on database connection {} version history table {}".format(engine, version_table))
        run_migrations_online(engine, target_metadata, version_table, include_object)

    # TODO: If a migration file is written, post-edit it and add websauna import

    logger.info("All done")
Esempio n. 21
0
def tutorial_req(request):
    """Construct a WSGI app with tutorial models and admins loaded."""
    ini_file = os.path.join(HERE, "tutorial-test.ini")
    request = init_websauna(ini_file)
    return request
Esempio n. 22
0
def cache_app(request):
    """Construct a WSGI app with static asset caching enabled."""
    request = init_websauna(STATIC_CONF_FILE)
    return request.app
        # No API docs for Jinja builtins
        if qual.startswith("jinja2."):
            qual = None

        filters.append((name, qual, doc, heading))

    filters = sorted(filters, key=lambda x: x[0])
    return filters


def find_vars():
    vars = websauna.system.core.vars
    result = []
    for name, func in vars._template_variables.items():
        qual = fullname(func)
        doc = strip_indent(func.__doc__)
        heading = "-" * len(name)
        result.append((name, qual, doc, heading))
    result = sorted(result, key=lambda x: x[0])
    return result


request = init_websauna("../websauna/conf/development.ini")

modules = {}
modules["filters"] = find_filters(request)
modules["vars"] = find_vars()

print(template.render(dict(modules=modules)))
            qual = qual[len("builtins"):]

        # No API docs for Jinja builtins
        if qual.startswith("jinja2."):
            qual = None

        filters.append((name, qual, doc, heading))

    filters = sorted(filters, key=lambda x: x[0])
    return filters


def find_vars():
    vars = websauna.system.core.vars
    result = []
    for name, func in vars._template_variables.items():
        qual = fullname(func)
        doc = strip_indent(func.__doc__)
        heading = "-" * len(name)
        result.append((name, qual, doc, heading))
    result = sorted(result, key=lambda x: x[0])
    return result


request = init_websauna("../development.ini")

modules = {}
modules["filters"] = find_filters(request)
modules["vars"] = find_vars()

print(template.render(dict(modules=modules)))
Esempio n. 25
0
def task_app_request(task_ini_file):
    request = init_websauna(task_ini_file)
    return request