Ejemplo n.º 1
0
    def handle(self, *args, **options):
        if not args:
            raise CommandError("No snapshot name specified")
        snapshot = args[0]
        snapshot_path = join(ROOT, snapshot)
        if exists(snapshot_path):
            raise CommandError(
                "Snapshot already exists: {0}".format(snapshot_path))

        running = ensure_stopped(verbose=True)

        # rsync must be in the system PATH
        cmd = ['rsync', '-aP', '--progress', CURRENT + '/.', snapshot_path]
        print(' '.join(cmd))
        if subprocess.call(cmd) > 0:
            raise CommandError("rsync failed")

        if options['activate']:
            print('Activating the snapshot.')
            activate_clone(snapshot)

        if running:
            ensure_started(verbose=True)
            print("Server restarted in background.")

        print()
        print("Snapshot done.")

        if not options['activate']:
            print()
            print("To activate:")
            print()
            print("    ./manage.py pg_activate", snapshot)
Ejemplo n.º 2
0
    def run_from_argv(self, argv):
        running = None

        pg_cmd = bin_path('pg_dump')
        port = get_port()
        if '--help' in argv:
            cmd = [pg_cmd, '--help']
        else:
            cmd = [pg_cmd, '-p', str(port), '-h', '127.0.0.1', 'django', '-O']
            cmd.extend(argv[2:])
            running = ensure_started()
        print(' '.join(cmd))
        subprocess.call(cmd)

        if not running and running is not None:
            ensure_stopped()
Ejemplo n.º 3
0
    def handle(self, *args, **options):
        if not os.path.isdir(ROOT):
            raise CommandError(
                'Root does not exist, maybe you meant pg_init? {0}'.format(ROOT))

        previous = current_clone()

        # Name for clone
        name = '{:%Y%m%d-%H%M%S}'.format(datetime.datetime.now())
        target = join(ROOT, name)

        cmd = [bin_path('initdb'), target]
        print(' '.join(cmd))
        if subprocess.call(cmd) > 0:
            raise CommandError('Error creating database in {0}'.format(target))

        for fname in ['pg_hba.conf', 'postgresql.conf']:
            src = os.path.join(DEFAULT, fname)
            dst = os.path.join(ROOT, name, fname)
            shutil.copyfile(src, dst)

        print("Stopping server")
        ensure_stopped()

        print("New database in {0}".format(target))
        activate_clone(name)

        print("Starting server to create database 'django'")
        ensure_started()

        print("Pausing 3s so that the database can start up")
        time.sleep(3)

        print("Creating database 'django'")
        cmd = [bin_path('createdb'), '-p', str(get_port()),
               '-h', '127.0.0.1', 'django']
        print(' '.join(cmd))
        if subprocess.call(cmd) > 0:
            raise CommandError('Error creating database')

        print("Blank database {} created and activated".format(name))
        print()
        print("To revert to your previous clone:")
        print()
        print("    ./manage.py pg_activate {}".format(previous))
Ejemplo n.º 4
0
    def handle(self, *args, **options):
        if not args:
            self.list_snapshots()
            return

        snapshot = args[0]
        if snapshot == 'current':
            raise CommandError("Invalid snapshot name")

        if not exists(join(ROOT, snapshot)):
            self.list_snapshots()
            raise CommandError("Snapshot not found: {0}".format(snapshot))

        running = ensure_stopped(verbose=True)

        activate_clone(snapshot)

        if running:
            ensure_started(verbose=True)
            print("Server restarted in background.")

        print()
        print("Snapshot activated.")