Пример #1
0
    def configure(self):
        helper.update_config(
            self.scalr_config.get('dbqueue_event', {}), self.config)
        helper.validate_config(self.config)
        socket.setdefaulttimeout(self.config['instances_connection_timeout'])

        self._db = dbmanager.ScalrDB(self.config['connections']['mysql'])
        self._pool = helper.GPool(pool_size=self.config['pool_size'])

        proxy_settings = helper.get_proxy_settings(self.scalr_config, 'system.webhooks')
        if proxy_settings:
            self.proxy = {
                'http': proxy_settings['url'],
                'https': proxy_settings['url']
            }
Пример #2
0
def test_get_proxy_settings_webhooks():
    scalr_config = {
        'connections': {
            'proxy': {
                'host': 'host',
                'port': 10,
                'type': 0,
                'user': '******',
                'pass': '******',
            },
        },
        'system': {
            'webhooks': {
                'use_proxy': None
            }
        }
    }
    cases = [
        {
            'section': ('system.webhooks', ),
            'use_proxy': (True, 'yes'),
            'use_on': ('both', 'scalr'),
            'result': {
                'host': scalr_config['connections']['proxy']['host'],
                'port': scalr_config['connections']['proxy']['port'],
                'type': scalr_config['connections']['proxy']['type'],
                'user': scalr_config['connections']['proxy']['user'],
                'pass': scalr_config['connections']['proxy']['pass'],
                'scheme': 'http',
                'url': 'http://*****:*****@host:10'
            }
        },
    ]
    for case in cases:
        for section in case['section']:
            config = scalr_config.copy()
            for use_proxy in case['use_proxy']:
                config['system']['webhooks']['use_proxy'] = use_proxy
                for use_on in case['use_on']:
                    config['connections']['proxy']['use_on'] = use_on
                    result = helper.get_proxy_settings(config, section)
                    if result:
                        case['result']['use_on'] = use_on
                    assert case['result'] == result, result
Пример #3
0
def test_get_proxy_settings_webhooks():
    scalr_config = {
        'connections': {
            'proxy': {
                'host': 'host',
                'port': 10,
                'type': 0,
                'user': '******',
                'pass': '******',
            },
        },
        'system': {
            'webhooks': {'use_proxy': None}
        }
    }
    cases = [
        {
            'section': ('system.webhooks',),
            'use_proxy': (True, 'yes'),
            'use_on': ('both', 'scalr'),
            'result': {
                'host': scalr_config['connections']['proxy']['host'],
                'port': scalr_config['connections']['proxy']['port'],
                'type': scalr_config['connections']['proxy']['type'],
                'user': scalr_config['connections']['proxy']['user'],
                'pass': scalr_config['connections']['proxy']['pass'],
                'scheme': 'http',
                'url': 'http://*****:*****@host:10'
            }
        },
    ]
    for case in cases:
        for section in case['section']:
            config = scalr_config.copy()
            for use_proxy in case['use_proxy']:
                config['system']['webhooks']['use_proxy'] = use_proxy
                for use_on in case['use_on']:
                    config['connections']['proxy']['use_on'] = use_on
                    result = helper.get_proxy_settings(config, section)
                    if result:
                        case['result']['use_on'] = use_on
                    assert case['result'] == result, result
Пример #4
0
def test_get_proxy_settings_default():
    scalr_config = {
        'connections': {
            'proxy': {
                'host': 'host',
                'type': 0,
            },
        }
    }
    cases = [
        {
            'section': ('ec2', 'aws'),
            'use_proxy': (True, 'yes'),
            'use_on': ('both', 'scalr'),
            'result': {
                'host': scalr_config['connections']['proxy']['host'],
                'user': None,
                'pass': None,
                'port': 3128,
                'type': scalr_config['connections']['proxy']['type'],
                'scheme': 'http',
                'url': 'http://host:3128'
            }
        },
    ]
    for case in cases:
        for section in case['section']:
            if section == 'ec2':
                section = 'aws'
            config = scalr_config.copy()
            for use_proxy in case['use_proxy']:
                if section != 'nonexistent':
                    config[section] = {}
                    config[section]['use_proxy'] = use_proxy
                for use_on in case['use_on']:
                    config['connections']['proxy']['use_on'] = use_on
                    result = helper.get_proxy_settings(config, section)
                    if result:
                        case['result']['use_on'] = use_on
                    assert case['result'] == result, result
Пример #5
0
def test_get_proxy_settings_default():
    scalr_config = {
        'connections': {
            'proxy': {
                'host': 'host',
                'type': 0,
            },
        }
    }
    cases = [
        {
            'section': ('ec2', 'aws'),
            'use_proxy': (True, 'yes'),
            'use_on': ('both', 'scalr'),
            'result': {
                'host': scalr_config['connections']['proxy']['host'],
                'user': None,
                'pass': None,
                'port': 3128,
                'type': scalr_config['connections']['proxy']['type'],
                'scheme': 'http',
                'url': 'http://host:3128'
            }
        },
    ]
    for case in cases:
        for section in case['section']:
            if section == 'ec2':
                section = 'aws'
            config = scalr_config.copy()
            for use_proxy in case['use_proxy']:
                if section != 'nonexistent':
                    config[section] = {}
                    config[section]['use_proxy'] = use_proxy
                for use_on in case['use_on']:
                    config['connections']['proxy']['use_on'] = use_on
                    result = helper.get_proxy_settings(config, section)
                    if result:
                        case['result']['use_on'] = use_on
                    assert case['result'] == result, result
Пример #6
0
 def set_proxy(self):
     self.proxy_settings = {
         platform: helper.get_proxy_settings(self.scalr_config, platform) for platform in analytics.PLATFORMS
     }
Пример #7
0
 def set_proxy(self):
     self.proxy_settings = {
         platform: helper.get_proxy_settings(self.scalr_config, platform)
         for platform in analytics.PLATFORMS
     }
Пример #8
0
    def configure(self):
        enabled = self.scalr_config.get('analytics', {}).get('enabled', False)
        if not enabled:
            sys.stdout.write('Analytics is disabled. Exit\n')
            sys.exit(0)

        utcnow = datetime.datetime.utcnow().replace(microsecond=0)
        if self.args['--date-from']:
            dtime_from = datetime.datetime.strptime(self.args['--date-from'], '%Y-%m-%d')
            if not self.args['--recalculate']:
                if self.args['--billing'] in ('aws-detailed-billing', 'azure'):
                    three_months_ago = utcnow + datetime.timedelta(days=-119)
                    assert_msg = 'Processing is not supported for dtime-from more than four months ago'
                    assert dtime_from > three_months_ago, assert_msg
                else:
                    two_weeks_ago = utcnow + datetime.timedelta(days=-14)
                    assert_msg = 'Processing is not supported for dtime-from more than two weeks ago'
                    assert dtime_from > two_weeks_ago, assert_msg
            self.config['dtime_from'] = dtime_from
        if self.args['--date-to']:
            dtime_to = datetime.datetime.strptime(self.args['--date-to'], '%Y-%m-%d')
            self.config['dtime_to'] = dtime_to

        helper.update_config(
            self.scalr_config.get('analytics', {}).get('connections', {}).get('scalr', {}),
            self.config['connections']['mysql'])
        helper.update_config(
            self.scalr_config.get('analytics', {}).get('connections', {}).get('analytics', {}),
            self.config['connections']['analytics'])
        helper.update_config(
            self.scalr_config.get('analytics', {}).get('processing', {}),
            self.config)

        if self.args['--billing']:
            self.config['billing'] = (self.args['--billing'],)
        if self.args['--platform']:
            assert_msg = "--platform option supported only for 'poller' billing type"
            assert 'poller' in self.config['billing'], assert_msg
            self.config['platform'] = (self.args['--platform'],)

        helper.validate_config(self.config)

        crypto_key_path = os.path.join(os.path.dirname(self.args['--config']), '.cryptokey')
        self.config['crypto_key'] = cryptotool.read_key(crypto_key_path)
        self.config['azure_app_client_id'] = self.scalr_config.get('azure', {}).get('app_client_id')
        self.config['azure_app_secret_key'] = self.scalr_config.get('azure', {}).get('app_secret_key')

        self.scalr_db = dbmanager.ScalrDB(self.config['connections']['mysql'])
        self.analytics_db = dbmanager.ScalrDB(self.config['connections']['analytics'])
        self.analytics = analytics.Analytics(self.scalr_db, self.analytics_db)

        if self.args['--year']:
            # XXX pymysql.connect issue
            time.sleep(0)
            quarters_calendar = self.analytics.get_quarters_calendar()
            year = int(self.args['--year'])
            quarter = int(self.args['--quarter'] or 1)
            dtime_from, dtime_to = quarters_calendar.dtime_for_quarter(quarter, year=year)
            self.config['dtime_from'] = dtime_from
            self.config['dtime_to'] = min(utcnow, dtime_to)

        self.config['proxy']['aws'] = helper.get_proxy_settings(self.scalr_config, 'aws')
        self.config['proxy']['azure'] = helper.get_proxy_settings(self.scalr_config, 'azure')

        socket.setdefaulttimeout(self.config['instances_connection_timeout'])
Пример #9
0
    def configure(self):
        enabled = self.scalr_config.get('analytics', {}).get('enabled', False)
        if not enabled:
            sys.stdout.write('Analytics is disabled. Exit\n')
            sys.exit(0)

        utcnow = datetime.datetime.utcnow().replace(microsecond=0)
        if self.args['--date-from']:
            dtime_from = datetime.datetime.strptime(self.args['--date-from'],
                                                    '%Y-%m-%d')
            if not self.args['--recalculate']:
                if self.args['--billing'] in ('aws-detailed-billing', 'azure'):
                    three_months_ago = utcnow + datetime.timedelta(days=-119)
                    assert_msg = 'Processing is not supported for dtime-from more than four months ago'
                    assert dtime_from > three_months_ago, assert_msg
                else:
                    two_weeks_ago = utcnow + datetime.timedelta(days=-14)
                    assert_msg = 'Processing is not supported for dtime-from more than two weeks ago'
                    assert dtime_from > two_weeks_ago, assert_msg
            self.config['dtime_from'] = dtime_from
        if self.args['--date-to']:
            dtime_to = datetime.datetime.strptime(self.args['--date-to'],
                                                  '%Y-%m-%d')
            self.config['dtime_to'] = dtime_to

        helper.update_config(
            self.scalr_config.get('analytics', {}).get('connections',
                                                       {}).get('scalr', {}),
            self.config['connections']['mysql'])
        helper.update_config(
            self.scalr_config.get('analytics',
                                  {}).get('connections',
                                          {}).get('analytics', {}),
            self.config['connections']['analytics'])
        helper.update_config(
            self.scalr_config.get('analytics', {}).get('processing', {}),
            self.config)

        if self.args['--billing']:
            self.config['billing'] = (self.args['--billing'], )
        if self.args['--platform']:
            assert_msg = "--platform option supported only for 'poller' billing type"
            assert 'poller' in self.config['billing'], assert_msg
            self.config['platform'] = (self.args['--platform'], )

        helper.validate_config(self.config)

        crypto_key_path = os.path.join(os.path.dirname(self.args['--config']),
                                       '.cryptokey')
        self.config['crypto_key'] = cryptotool.read_key(crypto_key_path)
        self.config['azure_app_client_id'] = self.scalr_config.get(
            'azure', {}).get('app_client_id')
        self.config['azure_app_secret_key'] = self.scalr_config.get(
            'azure', {}).get('app_secret_key')

        self.scalr_db = dbmanager.ScalrDB(self.config['connections']['mysql'])
        self.analytics_db = dbmanager.ScalrDB(
            self.config['connections']['analytics'])
        self.analytics = analytics.Analytics(self.scalr_db, self.analytics_db)

        if self.args['--year']:
            # XXX pymysql.connect issue
            time.sleep(0)
            quarters_calendar = self.analytics.get_quarters_calendar()
            year = int(self.args['--year'])
            quarter = int(self.args['--quarter'] or 1)
            dtime_from, dtime_to = quarters_calendar.dtime_for_quarter(
                quarter, year=year)
            self.config['dtime_from'] = dtime_from
            self.config['dtime_to'] = min(utcnow, dtime_to)

        self.config['proxy']['aws'] = helper.get_proxy_settings(
            self.scalr_config, 'aws')
        self.config['proxy']['azure'] = helper.get_proxy_settings(
            self.scalr_config, 'azure')

        socket.setdefaulttimeout(self.config['instances_connection_timeout'])