Created
August 21, 2018 19:56
-
-
Save chianingwang/fb1e2ccc3fcfdf6134912d583863ff5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import os | |
import sys | |
def parse_args(): | |
parser = argparse.ArgumentParser( | |
description='Given a list of migrations, add/edit/delete them.') | |
parser.add_argument('-a', '--action', required=True, | |
choices=['create', 'edit', 'delete'], | |
help='Action to perform') | |
parser.add_argument('accounts', | |
help='File with accounts for which migrations are ' | |
'added/removed') | |
parser.add_argument('--cluster', required=True, | |
help='Name of the cluster') | |
parser.add_argument('--credentials', | |
help='Name of the credentials to use') | |
parser.add_argument('--account-meta', required=False, type=bool, | |
default=False, | |
help='Should account metadata be overwritten by the ' | |
'source account metadata') | |
return parser.parse_args() | |
def create_migrations(accounts, propagate_metadata, cluster, credentials): | |
for account in accounts: | |
ssman.app.models.cloud_sync.CloudSyncMigration( | |
source_account=account, | |
all_source_buckets=True, | |
swift_account=account, | |
copy_account_metadata=propagate_metadata, | |
credential=credentials, | |
cluster=cluster).save() | |
def delete_migrations(accounts, propagate_metadata, cluster, credentials): | |
if accounts is None: | |
ssman.app.models.cloud_sync.CloudSyncMigration.objects.filter( | |
cluster=cluster).delete() | |
return | |
for account in accounts: | |
migration = ssman.app.models.cloud_sync.CloudSyncMigration.objects\ | |
.filter(cluster=cluster, source_account=account).first() | |
migration.delete() | |
def edit_migrations(accounts, propagate_metadata, cluster, credentials): | |
for account in accounts: | |
migration = ssman.app.models.cloud_sync.CloudSyncMigration.objects\ | |
.filter(cluster=cluster, source_account=account).first() | |
migration.copy_account_metadata = propagate_metadata | |
migration.save() | |
def import_django(): | |
# 5.x imports | |
sys.path.append('/opt/ss/deploy/current') | |
# find our psql first: | |
os.environ['PATH'] = '/opt/ss/pgsql-9.5/bin:' + os.environ['PATH'] | |
os.environ['DJANGO_SETTINGS_MODULE'] = 'ssman.settings.active' | |
os.umask(0022) | |
import django | |
django.setup() | |
global ssman | |
import ssman.app.models.cloud_sync | |
import ssman.app.models.cluster | |
def main(): | |
args = parse_args() | |
import_django() | |
cluster = ssman.app.models.cluster.Cluster.objects.filter( | |
name=args.cluster).first() | |
creds = ssman.app.models.cloud_sync.CloudSyncCredential.objects.filter( | |
name=args.credentials, cluster=cluster).first() | |
action_map = { | |
'create': create_migrations, | |
'delete': delete_migrations, | |
'edit': edit_migrations} | |
if args.accounts == '*' and args.action == 'delete': | |
delete_migrations(cluster) | |
return | |
with open(args.accounts) as fh: | |
accounts = [line.strip() for line in fh] | |
action_map[args.action](accounts, args.account_meta, cluster, creds) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment