Ejemplo n.º 1
0
def main():
    '''Main entrypoint.'''

    parser = argparse.ArgumentParser()
    parser.add_argument('--mysql-config-file',
                        default=database_utils.default_config_file(),
                        help='.my.cnf file that stores credentials')
    parser.add_argument('--database', default='omegaup', help='MySQL database')
    parser.add_argument('--username',
                        default='root',
                        help='MySQL root username')
    parser.add_argument('--password', default='omegaup', help='MySQL password')
    subparsers = parser.add_subparsers(dest='command')
    subparsers.required = True

    # Commands for development.
    parser_validate = subparsers.add_parser(
        'validate', help='Validates that the versioning is sane')
    parser_validate.set_defaults(func=validate)

    parser_upgrade = subparsers.add_parser('upgrade',
                                           help='Generates the upgrade script')
    parser_upgrade.set_defaults(func=upgrade)

    args = parser.parse_args()
    auth = database_utils.authentication(config_file=args.mysql_config_file,
                                         username=args.username,
                                         password=args.password)
    args.func(args, auth)
Ejemplo n.º 2
0
def main():
    '''Main entrypoint.'''

    parser = argparse.ArgumentParser()
    parser.add_argument('--mysql-config-file',
                        default=database_utils.default_config_file(),
                        help='.my.cnf file that stores credentials')
    parser.add_argument('--database', default='omegaup', help='MySQL database')
    parser.add_argument('--username', default='root',
                        help='MySQL root username')
    parser.add_argument('--password', default='omegaup', help='MySQL password')
    subparsers = parser.add_subparsers(dest='command')
    subparsers.required = True

    # Commands for development.
    parser_validate = subparsers.add_parser(
        'validate', help='Validates that the versioning is sane')
    parser_validate.set_defaults(func=validate)

    parser_upgrade = subparsers.add_parser(
        'upgrade', help='Generates the upgrade script')
    parser_upgrade.set_defaults(func=upgrade)

    args = parser.parse_args()
    auth = database_utils.authentication(config_file=args.mysql_config_file,
                                         username=args.username,
                                         password=args.password)
    args.func(args, auth)
Ejemplo n.º 3
0
def main():
    '''Runs the linters against the chosen files.'''

    args = git_tools.parse_arguments(
        tool_description='validates schema.sql',
        extra_arguments=[
            git_tools.Argument(
                '--mysql-config-file',
                default=database_utils.default_config_file(),
                help='.my.cnf file that stores credentials'),
            git_tools.Argument(
                '--database', default='omegaup', help='MySQL database'),
            git_tools.Argument(
                '--username', default='root', help='MySQL root username'),
            git_tools.Argument(
                '--password', default='omegaup', help='MySQL password')])

    # If running in an automated environment, we can close stdin.
    # This will disable all prompts.
    if (args.continuous_integration
            or os.environ.get('CONTINUOUS_INTEGRATION') == 'true'):
        sys.stdin.close()

    validate_only = args.tool == 'validate'

    filtered_files = list(filename for filename in args.files if
                          filename.endswith('.sql'))
    if not filtered_files:
        return

    root = git_tools.root_dir()
    expected = _expected_database_schema(config_file=args.mysql_config_file,
                                         username=args.username,
                                         password=args.password,
                                         verbose=args.verbose)
    actual = git_tools.file_contents(
        args, root, 'frontend/database/schema.sql')

    if (strip_mysql_extensions(expected.strip()) != strip_mysql_extensions(
            actual.strip())):
        if validate_only:
            if git_tools.attempt_automatic_fixes(sys.argv[0], args,
                                                 filtered_files):
                sys.exit(1)
            print('%sschema.sql validation errors.%s '
                  'Please run `%s` to fix them.' % (
                      git_tools.COLORS.FAIL, git_tools.COLORS.NORMAL,
                      git_tools.get_fix_commandline(sys.argv[0], args,
                                                    filtered_files)),
                  file=sys.stderr)
        else:
            with open(os.path.join(root,
                                   'frontend/database/schema.sql'), 'wb') as f:
                f.write(expected)
            print('Files written to working directory. '
                  '%sPlease commit them before pushing.%s' % (
                      git_tools.COLORS.HEADER, git_tools.COLORS.NORMAL),
                  file=sys.stderr)
        sys.exit(1)
Ejemplo n.º 4
0
def pytest_addoption(parser):
    '''Allow configuration of test invocation.'''

    parser.addoption('--browser', action='append', type=str, dest='browsers',
                     help='The browsers that the test will run against')
    parser.addoption('--url', default=('http://localhost/' if not _CI else
                                       'http://localhost:8000/'),
                     help='The URL that the test will be run against')
    parser.addoption('--disable-headless', action='store_false',
                     dest='headless', help='Show the browser window')
    parser.addoption('--mysql-config-file',
                     default=database_utils.default_config_file(),
                     help='.my.cnf file that stores credentials')
    parser.addoption('--username', default='root', help='MySQL root username')
    parser.addoption('--password', default='omegaup', help='MySQL password')
Ejemplo n.º 5
0
def _main() -> None:
    '''Main entrypoint.'''

    parser = argparse.ArgumentParser()
    parser.add_argument('--skip-container-check',
                        action='store_true',
                        help='Skip the container check')
    parser.add_argument('--mysql-config-file',
                        default=database_utils.default_config_file(),
                        help='.my.cnf file that stores credentials')
    parser.add_argument('--database', default='omegaup', help='MySQL database')
    parser.add_argument('--hostname',
                        default=None,
                        type=str,
                        help='Hostname of the MySQL server')
    parser.add_argument('--port',
                        default=13306,
                        type=int,
                        help='Port of the MySQL server')
    parser.add_argument('--username',
                        default='root',
                        help='MySQL root username')
    parser.add_argument('--password', default='omegaup', help='MySQL password')
    subparsers = parser.add_subparsers(dest='command')
    subparsers.required = True

    # Commands for development.
    parser_validate = subparsers.add_parser(
        'validate', help='Validates that the versioning is sane')
    parser_validate.set_defaults(func=validate)

    parser_upgrade = subparsers.add_parser('upgrade',
                                           help='Generates the upgrade script')
    parser_upgrade.set_defaults(func=upgrade)

    args = parser.parse_args()

    if not args.skip_container_check:
        database_utils.check_inside_container()

    auth = database_utils.authentication(config_file=args.mysql_config_file,
                                         username=args.username,
                                         password=args.password,
                                         hostname=args.hostname,
                                         port=args.port)
    args.func(args, auth)
Ejemplo n.º 6
0
def pytest_addoption(parser):
    '''Allow configuration of test invocation.'''

    parser.addoption('--browser',
                     action='append',
                     type=str,
                     dest='browsers',
                     help='The browsers that the test will run against')
    parser.addoption(
        '--url',
        default=('http://localhost/' if not _CI else 'http://localhost:8000/'),
        help='The URL that the test will be run against')
    parser.addoption('--disable-headless',
                     action='store_false',
                     dest='headless',
                     help='Show the browser window')
    parser.addoption('--mysql-config-file',
                     default=database_utils.default_config_file(),
                     help='.my.cnf file that stores credentials')
    parser.addoption('--username', default='root', help='MySQL root username')
    parser.addoption('--password', default='omegaup', help='MySQL password')
Ejemplo n.º 7
0
def main():
    '''Runs the linters against the chosen files.'''

    args = git_tools.parse_arguments(
        tool_description='validates schema.sql',
        extra_arguments=[
            git_tools.Argument('--mysql-config-file',
                               default=database_utils.default_config_file(),
                               help='.my.cnf file that stores credentials'),
            git_tools.Argument('--database',
                               default='omegaup',
                               help='MySQL database'),
            git_tools.Argument('--username',
                               default='root',
                               help='MySQL root username'),
            git_tools.Argument('--password',
                               default='omegaup',
                               help='MySQL password')
        ])

    # If running in an automated environment, we can close stdin.
    # This will disable all prompts.
    if (args.continuous_integration
            or os.environ.get('CONTINUOUS_INTEGRATION') == 'true'):
        sys.stdin.close()

    validate_only = args.tool == 'validate'

    filtered_files = list(filename for filename in args.files
                          if filename.endswith('.sql'))
    if not filtered_files:
        return

    root = git_tools.root_dir()
    expected = _expected_database_schema(config_file=args.mysql_config_file,
                                         username=args.username,
                                         password=args.password,
                                         verbose=args.verbose)
    actual = git_tools.file_contents(args, root,
                                     'frontend/database/schema.sql')

    if (strip_mysql_extensions(expected.strip()) != strip_mysql_extensions(
            actual.strip())):
        if validate_only:
            if git_tools.attempt_automatic_fixes(sys.argv[0], args,
                                                 filtered_files):
                sys.exit(1)
            print('%sschema.sql validation errors.%s '
                  'Please run `%s` to fix them.' %
                  (git_tools.COLORS.FAIL, git_tools.COLORS.NORMAL,
                   git_tools.get_fix_commandline(sys.argv[0], args,
                                                 filtered_files)),
                  file=sys.stderr)
        else:
            with open(os.path.join(root, 'frontend/database/schema.sql'),
                      'wb') as f:
                f.write(expected)
            print('Files written to working directory. '
                  '%sPlease commit them before pushing.%s' %
                  (git_tools.COLORS.HEADER, git_tools.COLORS.NORMAL),
                  file=sys.stderr)
        sys.exit(1)
Ejemplo n.º 8
0
def main() -> None:
    '''Main entrypoint.'''

    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--mysql-config-file',
        default=database_utils.default_config_file(),
        help='.my.cnf file that stores credentials')
    parser.add_argument(
        '--username', default='root', help='MySQL root username')
    parser.add_argument('--password', default='omegaup', help='MySQL password')
    parser.add_argument('--verbose', action='store_true')
    parser.add_argument(
        '--aws-username',
        default='omegaup-rds-deploy',
        help='The name of the AWS user to change the RDS timeout')
    parser.add_argument(
        '--aws-rds-region',
        default='us-east-1',
        help='The region of the RDS database')
    parser.add_argument(
        '--aws-rds-parameter-group-name',
        default='omegaup-frontend',
        help='The name of the Parameter Group name')
    parser.add_argument(
        '--lower-timeout',
        default='mysql',
        choices=('no', 'mysql', 'aws'),
        help='Temporarily lower the wait timeout.')
    parser.add_argument(
        '--kill-other-connections',
        action='store_true',
        help='Kill all connections to MySQL.')
    subparsers = parser.add_subparsers(dest='command')
    subparsers.required = True

    # Commands for puppet.
    parser_exists = subparsers.add_parser(
        'exists', help='Checks if the migration table exists')
    parser_exists.set_defaults(func=exists)

    parser_latest = subparsers.add_parser(
        'latest', help='Checks if the database is at the latest revision')
    parser_latest.set_defaults(func=latest)

    parser_migrate = subparsers.add_parser(
        'migrate', help='Migrates the database to the latest revision')
    parser_migrate.add_argument(
        '--noop',
        action='store_true',
        help=('Only print scripts that would be '
              'installed'))
    parser_migrate.add_argument(
        '--development-environment',
        dest='development_environment',
        action='store_true',
        help='Installs scripts flagged as for testing')
    parser_migrate.add_argument(
        '--databases',
        default='omegaup,omegaup-test',
        help='Comma-separated list of databases')
    parser_migrate.add_argument(
        '--limit', type=int, help='Last revision to include')
    parser_migrate.set_defaults(func=migrate)

    # Commands for development.
    parser_validate = subparsers.add_parser(
        'validate', help='Validates that the versioning is sane')
    parser_validate.set_defaults(func=validate)

    parser_ensure = subparsers.add_parser(
        'ensure', help='Ensures that the migration table exists')
    parser_ensure.set_defaults(func=ensure)

    parser_reset = subparsers.add_parser(
        'reset', help='Resets the migration table to a particular revision')
    parser_reset.add_argument(
        'revision', help='The desired revision', type=int)
    parser_reset.set_defaults(func=reset)

    parser_revision = subparsers.add_parser(
        'revision', help='Gets the current revision')
    parser_revision.set_defaults(func=print_revision)

    parser_purge = subparsers.add_parser(
        'purge', help='Start from scratch - Drop & Create empty databases')
    parser_purge.add_argument(
        '--databases',
        default=('omegaup,omegaup-test,'
                 '_omegaup_metadata'),
        help='Comma-separated list of databases')
    parser_purge.set_defaults(func=purge)

    parser_schema = subparsers.add_parser(
        'schema',
        help=('Show the database schema. Does not actually '
              'read/write from the database'))
    parser_schema.add_argument(
        '--limit', type=int, help='Last revision to include')
    parser_schema.set_defaults(func=schema)

    args = parser.parse_args()

    if args.verbose:
        logging.getLogger().setLevel('DEBUG')

    auth = database_utils.authentication(
        config_file=args.mysql_config_file,
        username=args.username,
        password=args.password)
    args.func(args, auth)
Ejemplo n.º 9
0
def main():
    '''Main entrypoint.'''

    parser = argparse.ArgumentParser()
    parser.add_argument('--mysql-config-file',
                        default=database_utils.default_config_file(),
                        help='.my.cnf file that stores credentials')
    parser.add_argument('--username',
                        default='root',
                        help='MySQL root username')
    parser.add_argument('--password', default='omegaup', help='MySQL password')
    subparsers = parser.add_subparsers(dest='command')
    subparsers.required = True

    # Commands for puppet.
    parser_exists = subparsers.add_parser(
        'exists', help='Checks if the migration table exists')
    parser_exists.set_defaults(func=exists)

    parser_latest = subparsers.add_parser(
        'latest', help='Checks if the database is at the latest revision')
    parser_latest.set_defaults(func=latest)

    parser_migrate = subparsers.add_parser(
        'migrate', help='Migrates the database to the latest revision')
    parser_migrate.add_argument('--noop',
                                action='store_true',
                                help=('Only print scripts that would be '
                                      'installed'))
    parser_migrate.add_argument('--development-environment',
                                dest='development_environment',
                                action='store_true',
                                help='Installs scripts flagged as for testing')
    parser_migrate.add_argument('--databases',
                                default='omegaup,omegaup-test',
                                help='Comma-separated list of databases')
    parser_migrate.add_argument('--limit',
                                type=int,
                                help='Last revision to include')
    parser_migrate.set_defaults(func=migrate)

    # Commands for development.
    parser_ensure = subparsers.add_parser(
        'ensure', help='Ensures that the migration table exists')
    parser_ensure.set_defaults(func=ensure)

    parser_reset = subparsers.add_parser(
        'reset', help='Resets the migration table to a particular revision')
    parser_reset.add_argument('revision',
                              help='The desired revision',
                              type=int)
    parser_reset.set_defaults(func=reset)

    parser_revision = subparsers.add_parser('revision',
                                            help='Gets the current revision')
    parser_revision.set_defaults(func=print_revision)

    parser_purge = subparsers.add_parser(
        'purge', help='Start from scratch - Drop & Create empty databases')
    parser_purge.add_argument('--databases',
                              default=('omegaup,omegaup-test,'
                                       '_omegaup_metadata'),
                              help='Comma-separated list of databases')
    parser_purge.set_defaults(func=purge)

    parser_schema = subparsers.add_parser(
        'schema',
        help=('Show the database schema. Does not actually '
              'read/write from the database'))
    parser_schema.add_argument('--limit',
                               type=int,
                               help='Last revision to include')
    parser_schema.set_defaults(func=schema)

    args = parser.parse_args()
    auth = database_utils.authentication(config_file=args.mysql_config_file,
                                         username=args.username,
                                         password=args.password)
    args.func(args, auth)
Ejemplo n.º 10
0
def main() -> None:
    '''Main entrypoint.'''

    parser = argparse.ArgumentParser()
    parser.add_argument('--skip-container-check',
                        action='store_true',
                        help='Skip the container check')
    parser.add_argument('--mysql-config-file',
                        default=database_utils.default_config_file(),
                        help='.my.cnf file that stores credentials')
    parser.add_argument('--hostname',
                        default=None,
                        type=str,
                        help='Hostname of the MySQL server')
    parser.add_argument('--port',
                        default=13306,
                        type=int,
                        help='Port of the MySQL server')
    parser.add_argument('--username',
                        default='root',
                        help='MySQL root username')
    parser.add_argument('--password', default='omegaup', help='MySQL password')
    parser.add_argument('--aws-rds-parameter-group-name',
                        default='omegaup-frontend',
                        help=('The name of the Parameter Group name. '
                              'Required with --lower-timeout and --aws.'))
    parser.add_argument('--lower-timeout',
                        action='store_true',
                        help='Temporarily lower the wait timeout.')
    parser.add_argument('--kill-blocking-connections',
                        action='store_true',
                        help='Kill all connections that hold a lock.')
    parser.add_argument('--aws',
                        action='store_true',
                        help='Use AWS-specific commands.')
    subparsers = parser.add_subparsers(dest='command')
    subparsers.required = True

    # Commands for puppet.
    parser_exists = subparsers.add_parser(
        'exists', help='Checks if the migration table exists')
    parser_exists.set_defaults(func=exists)

    parser_latest = subparsers.add_parser(
        'latest', help='Checks if the database is at the latest revision')
    parser_latest.set_defaults(func=latest)

    parser_migrate = subparsers.add_parser(
        'migrate', help='Migrates the database to the latest revision')
    parser_migrate.add_argument('--noop',
                                action='store_true',
                                help=('Only print scripts that would be '
                                      'installed'))
    parser_migrate.add_argument('--development-environment',
                                dest='development_environment',
                                action='store_true',
                                help='Installs scripts flagged as for testing')
    parser_migrate.add_argument('--databases',
                                default='omegaup,omegaup-test',
                                help='Comma-separated list of databases')
    parser_migrate.add_argument('--limit',
                                type=int,
                                help='Last revision to include')
    parser_migrate.set_defaults(func=migrate)

    # Commands for development.
    parser_validate = subparsers.add_parser(
        'validate', help='Validates that the versioning is sane')
    parser_validate.set_defaults(func=validate)

    parser_ensure = subparsers.add_parser(
        'ensure', help='Ensures that the migration table exists')
    parser_ensure.set_defaults(func=ensure)

    parser_reset = subparsers.add_parser(
        'reset', help='Resets the migration table to a particular revision')
    parser_reset.add_argument('revision',
                              help='The desired revision',
                              type=int)
    parser_reset.set_defaults(func=reset)

    parser_revision = subparsers.add_parser('revision',
                                            help='Gets the current revision')
    parser_revision.set_defaults(func=print_revision)

    parser_purge = subparsers.add_parser(
        'purge', help='Start from scratch - Drop & Create empty databases')
    parser_purge.add_argument('--databases',
                              default=('omegaup,omegaup-test,'
                                       '_omegaup_metadata'),
                              help='Comma-separated list of databases')
    parser_purge.set_defaults(func=purge)

    parser_schema = subparsers.add_parser(
        'schema',
        help=('Show the database schema. Does not actually '
              'read/write from the database'))
    parser_schema.add_argument('--limit',
                               type=int,
                               help='Last revision to include')
    parser_schema.set_defaults(func=schema)

    lib.logs.configure_parser(parser)

    args = parser.parse_args()
    lib.logs.init(parser.prog, args)

    if not args.skip_container_check:
        database_utils.check_inside_container()

    auth = database_utils.authentication(config_file=args.mysql_config_file,
                                         username=args.username,
                                         password=args.password,
                                         hostname=args.hostname,
                                         port=args.port)
    args.func(args, auth)
Ejemplo n.º 11
0
def main() -> None:
    '''Runs the linters against the chosen files.'''

    args = git_tools.parse_arguments(
        tool_description='validates schema.sql',
        extra_arguments=[
            git_tools.Argument('--mysql-config-file',
                               default=database_utils.default_config_file(),
                               help='.my.cnf file that stores credentials'),
            git_tools.Argument('--database',
                               default='omegaup',
                               help='MySQL database'),
            git_tools.Argument('--hostname',
                               default=None,
                               type=str,
                               help='Hostname of the MySQL server'),
            git_tools.Argument('--username',
                               default='root',
                               help='MySQL root username'),
            git_tools.Argument('--password',
                               default='omegaup',
                               help='MySQL password')
        ])

    validate_only = args.tool == 'validate'

    filtered_files = list(filename for filename in args.files
                          if filename.endswith('.sql'))

    root = git_tools.root_dir()
    if not _check_mutually_exclusive_schema_modifications(
            args=args,
            root=root,
    ):
        sys.exit(1)
    if 'frontend/database/dao_schema.sql' in filtered_files:
        filtered_files.remove('frontend/database/dao_schema.sql')
    if not filtered_files:
        return

    expected = _expected_database_schema(config_file=args.mysql_config_file,
                                         username=args.username,
                                         password=args.password,
                                         hostname=args.hostname,
                                         verbose=args.verbose)
    actual = git_tools.file_contents(args, root, _SCHEMA_FILENAME)

    expected_contents = strip_mysql_extensions(expected.strip())
    actual_contents = strip_mysql_extensions(actual.strip())

    if expected_contents != actual_contents:
        if validate_only:
            if git_tools.attempt_automatic_fixes(sys.argv[0], args,
                                                 filtered_files):
                sys.exit(1)
            sys.stderr.writelines(
                difflib.unified_diff(
                    actual_contents.decode('utf-8').splitlines(keepends=True),
                    expected_contents.decode('utf-8').splitlines(
                        keepends=True),
                    fromfile=_SCHEMA_FILENAME,
                    tofile=_SCHEMA_FILENAME))
            print('%sschema.sql validation errors.%s '
                  'Please run `%s` to fix them.' %
                  (git_tools.COLORS.FAIL, git_tools.COLORS.NORMAL,
                   git_tools.get_fix_commandline(args, filtered_files)),
                  file=sys.stderr)
        else:
            with open(os.path.join(root, 'frontend/database/schema.sql'),
                      'wb') as f:
                f.write(expected)
            print('Files written to working directory. '
                  '%sPlease commit them before pushing.%s' %
                  (git_tools.COLORS.HEADER, git_tools.COLORS.NORMAL),
                  file=sys.stderr)
        sys.exit(1)
Ejemplo n.º 12
0
def main():
    '''Main entrypoint.'''

    parser = argparse.ArgumentParser()
    parser.add_argument('--mysql-config-file',
                        default=database_utils.default_config_file(),
                        help='.my.cnf file that stores credentials')
    parser.add_argument('--username', default='root',
                        help='MySQL root username')
    parser.add_argument('--password', default='omegaup', help='MySQL password')
    subparsers = parser.add_subparsers(dest='command')
    subparsers.required = True

    # Commands for puppet.
    parser_exists = subparsers.add_parser(
        'exists', help='Checks if the migration table exists')
    parser_exists.set_defaults(func=exists)

    parser_latest = subparsers.add_parser(
        'latest', help='Checks if the database is at the latest revision')
    parser_latest.set_defaults(func=latest)

    parser_migrate = subparsers.add_parser(
        'migrate', help='Migrates the database to the latest revision')
    parser_migrate.add_argument('--noop', action='store_true',
                                help=('Only print scripts that would be '
                                      'installed'))
    parser_migrate.add_argument('--development-environment',
                                dest='development_environment',
                                action='store_true',
                                help='Installs scripts flagged as for testing')
    parser_migrate.add_argument('--databases', default='omegaup,omegaup-test',
                                help='Comma-separated list of databases')
    parser_migrate.add_argument('--limit', type=int,
                                help='Last revision to include')
    parser_migrate.set_defaults(func=migrate)

    # Commands for development.
    parser_ensure = subparsers.add_parser(
        'ensure', help='Ensures that the migration table exists')
    parser_ensure.set_defaults(func=ensure)

    parser_reset = subparsers.add_parser(
        'reset',
        help='Resets the migration table to a particular revision')
    parser_reset.add_argument('revision', help='The desired revision',
                              type=int)
    parser_reset.set_defaults(func=reset)

    parser_revision = subparsers.add_parser(
        'revision', help='Gets the current revision')
    parser_revision.set_defaults(func=print_revision)

    parser_purge = subparsers.add_parser(
        'purge', help='Start from scratch - Drop & Create empty databases')
    parser_purge.add_argument('--databases',
                              default=('omegaup,omegaup-test,'
                                       '_omegaup_metadata'),
                              help='Comma-separated list of databases')
    parser_purge.set_defaults(func=purge)

    parser_schema = subparsers.add_parser(
        'schema', help=('Show the database schema. Does not actually '
                        'read/write from the database'))
    parser_schema.add_argument('--limit', type=int,
                               help='Last revision to include')
    parser_schema.set_defaults(func=schema)

    args = parser.parse_args()
    auth = database_utils.authentication(config_file=args.mysql_config_file,
                                         username=args.username,
                                         password=args.password)
    args.func(args, auth)