def remount(): if reactive.is_state('postgresql.cluster.is_running'): # Attempting this while PostgreSQL is live would be really, really bad. service.stop() old_data_dir = postgresql.data_dir() new_data_dir = os.path.join(external_volume_mount, 'postgresql', postgresql.version(), 'main') backup_data_dir = '{}-{}'.format(old_data_dir, int(time.time())) if os.path.isdir(new_data_dir): hookenv.log('Remounting existing database at {}'.format(new_data_dir), WARNING) else: status_set('maintenance', 'Migrating data from {} to {}'.format(old_data_dir, new_data_dir)) helpers.makedirs(new_data_dir, mode=0o770, user='******', group='postgres') try: rsync_cmd = ['rsync', '-av', old_data_dir + '/', new_data_dir + '/'] hookenv.log('Running {}'.format(' '.join(rsync_cmd)), DEBUG) subprocess.check_call(rsync_cmd) except subprocess.CalledProcessError: status_set('blocked', 'Failed to sync data from {} to {}' ''.format(old_data_dir, new_data_dir)) return os.replace(old_data_dir, backup_data_dir) os.symlink(new_data_dir, old_data_dir) fix_perms(new_data_dir) reactive.remove_state('postgresql.storage.needs_remount')
def migrate_pgdata(): ''' Copy the data from /var/lib/postgresql/9.x/main to the new path and replace the original PGDATA with a symlink. Note that the original may already be a symlink, either from the block storage broker or manual changes by admins. ''' if reactive.is_state('postgresql.cluster.is_running'): # Attempting this while PostgreSQL is live would be really, really bad. service.stop() old_data_dir = postgresql.data_dir() new_data_dir = unitdata.kv().get(pgdata_path_key) backup_data_dir = '{}-{}'.format(old_data_dir, int(time.time())) if os.path.isdir(new_data_dir): # This never happens with Juju storage, at least with 2.0, # because we have no way of reusing old partitions. hookenv.log('Remounting existing database at {}'.format(new_data_dir), WARNING) else: status_set('maintenance', 'Migrating data from {} to {}'.format(old_data_dir, new_data_dir)) helpers.makedirs(new_data_dir, mode=0o770, user='******', group='postgres') try: rsync_cmd = ['rsync', '-av', old_data_dir + '/', new_data_dir + '/'] hookenv.log('Running {}'.format(' '.join(rsync_cmd)), DEBUG) subprocess.check_call(rsync_cmd, universal_newlines=True) except subprocess.CalledProcessError: status_set('blocked', 'Failed to sync data from {} to {}' ''.format(old_data_dir, new_data_dir)) return os.replace(old_data_dir, backup_data_dir) os.symlink(new_data_dir, old_data_dir) fix_perms(new_data_dir) reactive.set_state('postgresql.storage.pgdata.migrated')
def remount(): if reactive.is_state("postgresql.cluster.is_running"): # Attempting this while PostgreSQL is live would be really, really bad. service.stop() old_data_dir = postgresql.data_dir() new_data_dir = os.path.join(external_volume_mount, "postgresql", postgresql.version(), "main") backup_data_dir = "{}-{}".format(old_data_dir, int(time.time())) if os.path.isdir(new_data_dir): hookenv.log("Remounting existing database at {}".format(new_data_dir), WARNING) else: status_set( "maintenance", "Migrating data from {} to {}".format(old_data_dir, new_data_dir), ) helpers.makedirs(new_data_dir, mode=0o770, user="******", group="postgres") try: rsync_cmd = [ "rsync", "-av", old_data_dir + "/", new_data_dir + "/" ] hookenv.log("Running {}".format(" ".join(rsync_cmd)), DEBUG) subprocess.check_call(rsync_cmd) except subprocess.CalledProcessError: status_set( "blocked", "Failed to sync data from {} to {}" "".format(old_data_dir, new_data_dir), ) return os.replace(old_data_dir, backup_data_dir) os.symlink(new_data_dir, old_data_dir) fix_perms(new_data_dir) reactive.remove_state("postgresql.storage.needs_remount")