Ejemplo n.º 1
0
def _backup(backupname, user, database):
    """Backs up the database with pg_dump."""

    # We have some restrictions on the backupname
    if re.match('[a-z0-9_-]+$', backupname) is None:
        click.secho('Invalid backupname.', fg='red')
        sys.exit(1)

    # The file must not exist
    filename = os.path.join(BACKUP_DIR, backupname)
    if os.path.isfile(filename):
        click.secho('File %s exists.' % filename, fg='red')
        sys.exit(1)

    params = ['pg_dump', '-h', SOCKET_DIR, '-O', '-x', '-U', user, database]

    with open(filename, 'w') as f, running_db():
        ret = subprocess.call(
            params, stdout=f, preexec_fn=setuser(USER_NAME))

    os.chown(filename, USER_ID, GROUP_ID)

    if ret == 0:
        click.secho('Successful backup: %s' % filename, fg='green')
    else:
        try:
            os.remove(filename)
        except:
            pass
        click.secho('Backup (%s) failed' % filename, fg='red')
        sys.exit(1)
Ejemplo n.º 2
0
def start():
    cwd = '/var/lib/data'
    texfile = 'input/test.tex'
    output_directory = 'output'
    cmd = ['lualatex', '--output-directory=%s' % output_directory, texfile]
    preexec_fn = setuser(USER_NAME)
    subprocess.call(cmd, preexec_fn=preexec_fn, cwd=cwd)
Ejemplo n.º 3
0
def _backup(backupname):
    """Backs up the database with pg_dump."""
    # We have some restrictions on the backupname
    if re.match('[a-z0-9_-]+$', backupname) is None:
        click.secho('Invalid backupname.', fg='red')
        sys.exit(1)

    # The file must not exist
    filename = os.path.join(BACKUP_DIR, backupname)
    if os.path.isfile(filename):
        click.secho('File %s exists.' % filename, fg='red')
        sys.exit(1)

    params = ['pg_dump', '-h', SOCKET_DIR, '-O', '-x', '-U', 'postgres',
              'django']
    with open(filename, 'w') as f, running_db():
        ret = subprocess.call(params, stdout=f, preexec_fn=setuser('postgres'))

    uid, gid, _ = id('postgres')
    os.chown(filename, uid, gid)

    if ret == 0:
        click.secho('Successful backup: %s' % filename, fg='green')
    else:
        try:
            os.remove(filename)
        except:
            pass
        click.secho('Backup (%s) failed' % filename, fg='red')
        sys.exit(1)
Ejemplo n.º 4
0
def running_db():
    """
    Starts and stops postgres (if it is not running) so the block
    inside the with statement can execute command against it.
    """

    subproc = None
    if not os.path.isfile(os.path.join(PGDATA, 'postmaster.pid')):
        setpostgresuser = setuser('postgres')
        subproc = subprocess.Popen(
            start_postgres,
            preexec_fn=setpostgresuser,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)

        click.echo('Waiting for database to start...')
        time.sleep(1)

    try:
        yield
    finally:
        if subproc:
            subproc.send_signal(signal.SIGTERM)
            click.echo('Waiting for database to stop...')
            subproc.wait()
Ejemplo n.º 5
0
def test():
    rc = subprocess.call(
        [
            'py.test',
            # '--cov-report=',
            '--cov-append',
            '--cov=pygmentslexerbabylon'
        ],
        preexec_fn=setuser(USER_NAME))
    sys.exit(rc)
Ejemplo n.º 6
0
def repair():
    """Rapair stale lock file (postmaster.pid) situations."""
    subproc = subprocess.Popen(
        START_POSTGRES,
        preexec_fn=setuser(USER_NAME),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)

    success = False
    for i in range(10):
        logline = subproc.stderr.readline()
        if logline.find(b'ready to accept connections') > -1:
            success = True
            break

    subproc.send_signal(signal.SIGTERM)
    subproc.wait()

    if success:
        click.echo('Success.')
    else:
        click.echo('Could not repair :(')
Ejemplo n.º 7
0
def running_db():
    """
    Starts and stops postgres (if it is not running) so the block
    inside the with statement can execute command against it.
    """

    subproc = None
    if not os.path.isfile(os.path.join(PGDATA, 'postmaster.pid')):
        click.echo('Starting the dtabase...')
        subproc = subprocess.Popen(
            START_POSTGRES,
            preexec_fn=setuser(USER_NAME),
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)

        click.echo('Waiting for database to start...')
        while True:
            logline = subproc.stderr.readline()
            if logline.find(b'ready to accept connections') > -1:
                break

    try:
        conn = psycopg2.connect(CONN_STR)
    except:
        click.echo('postmaster.pid existed or the database '
                   'started, but could not connect.')
        raise Exception('Database could not be started.')
    else:
        conn.close()

    try:
        yield
    finally:
        if subproc:
            subproc.send_signal(signal.SIGTERM)
            click.echo('Waiting for database to stop...')
            subproc.wait()
Ejemplo n.º 8
0
def running_db():
    """
    Starts and stops postgres (if it is not running) so the block
    inside the with statement can execute command against it.
    """

    subproc = None
    if not os.path.isfile(os.path.join(PGDATA, 'postmaster.pid')):
        setpostgresuser = setuser('postgres')
        subproc = subprocess.Popen(start_postgres,
                                   preexec_fn=setpostgresuser,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)

        click.echo('Waiting for database to start...')
        time.sleep(1)

    try:
        yield
    finally:
        if subproc:
            subproc.send_signal(signal.SIGTERM)
            click.echo('Waiting for database to stop...')
            subproc.wait()
Ejemplo n.º 9
0
def coverase():
    subprocess.call(['coverage', 'erase'], preexec_fn=setuser(USER_NAME))
Ejemplo n.º 10
0
def covreport():
    subprocess.call(['coverage', 'html'], preexec_fn=setuser(USER_NAME))