class Command(BaseGoCommand): help = "Manage Go accounts." option_list = BaseGoCommand.option_list + ( make_command_option( 'create_billing_account', help=( "Create a billing account table entry for the user if" "there isn't an entry already.")), make_option( '--email-address', dest='email_address', help="Act on the given user."), make_option( '--all', dest='all_accounts', action='store_true', default=False, help='Act on all accounts.'), make_option('--dry-run', dest='dry_run', action='store_true', default=False, help='Just pretend to act.'), ) def _get_user_apis(self): if self.options.get("email_address"): return [self.mk_user_api(self.options["email_address"])] if self.options.get("all_accounts"): return self.mk_all_user_apis() raise CommandError( "Please specify either --email-address or --all-users") def _apply_to_accounts(self, func, dry_run=None): if dry_run is None: dry_run = self.options.get('dry_run') for user, user_api in self._get_user_apis(): self.stdout.write( "Performing %s on account %s ...\n" % ( func.__name__, user_details_as_string(user) )) if not dry_run: func(user_api) self.stdout.write("done.\n") def handle_command_create_billing_account(self, *args, **options): def create_billing_account(user_api): account_key = user_api.user_account_key user = get_user_by_account_key(account_key) _, created = Account.objects.get_or_create( user=user, account_number=account_key) if created: self.stdout.write( " Created billing account.\n") else: self.stdout.write( " Billing account already exists.\n") self._apply_to_accounts(create_billing_account)
def setup_dummy_command(self): def handle_command_dummy(*args, **options): def dummy(user_api): self.command.stdout.write(" pom-pom-pom ...\n") self.command._apply_to_accounts(dummy) self.command.handle_command_dummy = handle_command_dummy self.command.option_list = self.command.option_list + ( make_command_option("dummy"), )
def setup_dummy_command(self): def handle_command_dummy(*args, **options): def dummy(user_api): self.command.stdout.write(" pom-pom-pom ...\n") self.command._apply_to_accounts(dummy) self.command.handle_command_dummy = handle_command_dummy self.command.option_list = self.command.option_list + ( make_command_option("dummy"),)
class Command(BaseGoCommand): # TODO Use riak instead of redis for maintaining the list of accounts # for which metric collection should be disabled encoding = 'utf-8' help = "Disable or re-enable metric collection for a Vumi Go user" option_list = BaseGoCommand.option_list + ( make_email_option(), make_command_option( 'list', help='List the Vumi Go user accounts that currently have ' 'metric collection disabled'), make_command_option( 'enable', help='Enable metric collection for the Vumi Go user'), make_command_option( 'disable', help='Disable metric collection for the Vumi Go user'), ) def handle_command_enable(self, *args, **options): _user, user_api = self.mk_user_api(options=options) user_account_key = user_api.user_account_key self.vumi_api.redis.srem('disabled_metrics_accounts', user_account_key) def handle_command_disable(self, *args, **options): _user, user_api = self.mk_user_api(options=options) user_account_key = user_api.user_account_key self.vumi_api.redis.sadd('disabled_metrics_accounts', user_account_key) def handle_command_list(self, *args, **options): acc_keys = self.vumi_api.redis.smembers('disabled_metrics_accounts') if not acc_keys: self.stderr.write('No accounts have metric collection disabled.\n') else: for i, acc_key in enumerate(acc_keys): user = get_user_by_account_key(acc_key) output = u"%s. %s\n" % (i, user_details_as_string(user)) self.stdout.write(output.encode(self.encoding))
class Command(BaseGoAccountCommand): help = "Manage conversations." option_list = BaseGoAccountCommand.option_list + ( make_command_option( 'list', help='List the active conversations in this account.'), make_command_option('show', help='Display a conversation'), make_command_option('show_config', help='Display the config for a conversation'), make_command_option('start', help='Start a conversation'), make_command_option('stop', help='Stop a conversation'), make_command_option('archive', help='Archive a conversation'), make_command_option('export', help='Export a conversation definition'), make_command_option('import', help='Import a conversation definition'), make_option('--conversation-key', dest='conversation_key', help='The conversation key'), make_option('--file', dest='file', help='The file to import')) def get_conversation(self, options): if 'conversation_key' not in options: raise CommandError('Please specify a conversation key') conversation = self.user_api.get_wrapped_conversation( options['conversation_key']) if conversation is None: raise CommandError('Conversation does not exist') return conversation def handle_command_list(self, *args, **options): conversations = self.user_api.active_conversations() conversations.sort(key=lambda c: c.created_at) for i, c in enumerate(conversations): self.stdout.write("%d. %s (type: %s, key: %s)\n" % (i, c.name, c.conversation_type, c.key)) def handle_command_show(self, *args, **options): conversation = self.get_conversation(options) self.stdout.write(pformat(conversation.get_data())) self.stdout.write("\n") def handle_command_show_config(self, *args, **options): conversation = self.get_conversation(options) self.stdout.write(pformat(conversation.config)) self.stdout.write("\n") def handle_command_start(self, *apps, **options): conversation = self.get_conversation(options) if conversation.archived(): raise CommandError('Archived conversations cannot be started') if conversation.running() or conversation.stopping(): raise CommandError('Conversation already running') self.stdout.write("Starting conversation...\n") conversation.start() self.stdout.write("Conversation started\n") def handle_command_stop(self, *apps, **options): conversation = self.get_conversation(options) if conversation.archived(): raise CommandError('Archived conversations cannot be stopped') if conversation.stopped(): raise CommandError('Conversation already stopped') self.stdout.write("Stopping conversation...\n") conversation.stop_conversation() self.stdout.write("Conversation stopped\n") def handle_command_archive(self, *apps, **options): conversation = self.get_conversation(options) if conversation.archived(): raise CommandError('Archived conversations cannot be archived') if not conversation.stopped(): raise CommandError('Only stopped conversations can be archived') self.stdout.write("Archiving conversation...\n") conversation.archive_conversation() self.stdout.write("Conversation archived\n") def handle_command_export(self, *apps, **options): conversation = self.get_conversation(options) self.stdout.write(json.dumps(conversation.get_data())) def load_file(self, options): if 'file' not in options: raise CommandError('Please specify a file to load.') file_name = options['file'] return open(file_name, 'r') def handle_command_import(self, *apps, **options): raw_conv_data = json.load(self.load_file(options)) allowed_keys = [ 'conversation_type', 'description', 'name', 'config', ] new_conv_data = dict([(key, raw_conv_data[key]) for key in allowed_keys]) conversation_type = new_conv_data['conversation_type'] if conversation_type not in config.configured_conversation_types(): raise CommandError('Invalid conversation_type: %s' % (conversation_type, )) conversation = self.user_api.new_conversation(**new_conv_data) self.stdout.write(json.dumps(conversation.get_data()))
class Command(BaseGoAccountCommand): help = "Manage routers." option_list = BaseGoAccountCommand.option_list + ( make_command_option('list', help='List the active routers in this account.'), make_command_option('show', help='Display a router'), make_command_option('show_config', help='Display the config for a router'), make_command_option('start', help='Start a router'), make_command_option('stop', help='Stop a router'), make_command_option('archive', help='Archive a router'), make_option('--router-key', dest='router_key', help='The router key'), ) def get_router(self, options): if 'router_key' not in options: raise CommandError('Please specify a router key') router = self.user_api.get_router(options['router_key']) if router is None: raise CommandError('Router does not exist') return router def handle_command_list(self, *args, **options): routers = self.user_api.active_routers() routers.sort(key=lambda c: c.created_at) for i, c in enumerate(routers): self.stdout.write("%d. %s (type: %s, key: %s)\n" % (i, c.name, c.router_type, c.key)) def handle_command_show(self, *args, **options): router = self.get_router(options) self.stdout.write(pformat(router.get_data())) self.stdout.write("\n") def handle_command_show_config(self, *args, **options): router = self.get_router(options) self.stdout.write(pformat(router.config)) self.stdout.write("\n") def handle_command_start(self, *apps, **options): router = self.get_router(options) if router.archived(): raise CommandError('Archived routers cannot be started') if router.running() or router.stopping(): raise CommandError('Router already running') self.stdout.write("Starting router...\n") rapi = self.user_api.get_router_api(router.router_type, router.key) rapi.start_router() self.stdout.write("Router started\n") def handle_command_stop(self, *apps, **options): router = self.get_router(options) if router.archived(): raise CommandError('Archived routers cannot be stopped') if router.stopped(): raise CommandError('Router already stopped') self.stdout.write("Stopping router...\n") rapi = self.user_api.get_router_api(router.router_type, router.key) rapi.stop_router() self.stdout.write("Router stopped\n") def handle_command_archive(self, *apps, **options): router = self.get_router(options) if router.archived(): raise CommandError('Archived routers cannot be archived') if not router.stopped(): raise CommandError('Only stopped routers can be archived') self.stdout.write("Archiving router...\n") rapi = self.user_api.get_router_api(router.router_type, router.key) rapi.archive_router() self.stdout.write("Router archived\n")
class Command(BaseGoCommand): help = "Manage the message cache." option_list = BaseGoCommand.option_list + ( make_command_option('reconcile', help='Reconcile the message cache.'), make_command_option('switch_to_counters', help='Switch cache to counters.'), make_option('--email-address', dest='email_address', help="Act on the given user's batches."), make_option('--conversation-key', dest='conversation_key', help='Act on the given conversation.'), make_option('--active-conversations', dest='active_conversations', action='store_true', default=False, help='Act on all active conversations.'), make_option('--archived-conversations', dest='archived_conversations', action='store_true', default=False, help='Act on all archived conversations.'), make_option('--dry-run', dest='dry_run', action='store_true', default=False, help='Just pretend to act.'), ) def _get_user_apis(self): if self.options.get("email_address"): return [self.mk_user_api(self.options["email_address"])] return self.mk_all_user_apis() def _get_batches(self, user_api): batches = set() if self.options.get('conversation_key'): conv = user_api.get_conversation(self.options['conversation_key']) batches.add(conv.batch.key) if self.options.get('active_conversations'): batches.update(conv.batch.key for conv in user_api.active_conversations()) if self.options.get('archived_conversations'): batches.update(conv.batch.key for conv in user_api.finished_conversations()) return list(batches) def _apply_to_batches(self, func, dry_run=None): if dry_run is None: dry_run = self.options.get('dry_run') for user, user_api in self._get_user_apis(): batches = self._get_batches(user_api) if not batches: continue self.stdout.write("Processing account %s ...\n" % user_details_as_string(user)) for batch_id in sorted(batches): self.stdout.write(" Performing %s on batch %s ...\n" % (func.__name__, batch_id)) if not dry_run: func(user_api, batch_id) self.stdout.write("done.\n") def handle_command_reconcile(self, *args, **options): def reconcile(user_api, batch_id): user_api.api.mdb.reconcile_cache(batch_id) self._apply_to_batches(reconcile) def handle_command_switch_to_counters(self, *args, **options): def switch_to_counters(user_api, batch_id): user_api.api.mdb.cache.switch_to_counters(batch_id) self._apply_to_batches(switch_to_counters)