示例#1
0
def main():
    parser = get_c7n_mailer_parser()
    args = parser.parse_args()
    mailer_config = get_and_validate_mailer_config(args)
    args_dict = vars(args)
    logger = get_logger(debug=args_dict.get('debug', False))

    module_dir = path.dirname(path.abspath(__file__))
    default_templates = [
        path.abspath(path.join(module_dir, 'msg-templates')),
        path.abspath(path.join(module_dir, '..', 'msg-templates')),
        path.abspath('.')
    ]
    templates = args_dict.get('templates', None)
    if templates:
        default_templates.append(
            path.abspath(path.expanduser(path.expandvars(templates))))

    mailer_config['templates_folders'] = default_templates

    provider = get_provider(mailer_config)
    if args_dict.get('update_lambda'):
        if args_dict.get('debug'):
            print(
                '\n** --debug is only supported with --run, not --update-lambda **\n'
            )
            return
        if args_dict.get('max_num_processes'):
            print('\n** --max-num-processes is only supported '
                  'with --run, not --update-lambda **\n')
            return

        if provider == Providers.Azure:
            azure_deploy.provision(mailer_config)
        elif provider == Providers.AWS:
            deploy.provision(mailer_config,
                             functools.partial(session_factory, mailer_config))

    if args_dict.get('run'):
        max_num_processes = args_dict.get('max_num_processes')

        # Select correct processor
        if provider == Providers.Azure:
            processor = MailerAzureQueueProcessor(mailer_config, logger)
        elif provider == Providers.AWS:
            aws_session = session_factory(mailer_config)
            processor = MailerSqsQueueProcessor(mailer_config, aws_session,
                                                logger)

        # Execute
        if max_num_processes:
            run_mailer_in_parallel(processor, max_num_processes)
        else:
            processor.run()
示例#2
0
def start_c7n_mailer(logger, config, auth_file):
    try:
        logger.info('c7n_mailer starting...')
        session = Session(authorization_file=auth_file,
                          resource=RESOURCE_STORAGE)
        mailer_azure_queue_processor = MailerAzureQueueProcessor(
            config, logger, session=session)
        mailer_azure_queue_processor.run()
    except Exception as e:
        logger.exception(
            "Error starting mailer MailerAzureQueueProcessor(). \n Error: %s \n"
            % (e))
示例#3
0
    def test_run(self, mock_get_messages, mock_delete, mock_client,
                 mock_process):
        mock_get_messages.side_effect = [[self.compressed_message], []]
        mock_client.return_value = (None, None)
        mock_process.return_value = True

        # Run the 'run' method
        azure_processor = MailerAzureQueueProcessor(MAILER_CONFIG_AZURE,
                                                    logger)
        azure_processor.run(False)

        self.assertEqual(2, mock_get_messages.call_count)
        self.assertEqual(1, mock_process.call_count)
        mock_delete.assert_called()