Exemple #1
0
    def __init__(self, config):
        self.config = config
        self.handler = RequestHandler(config)
        self.instance = self._random_string()
        self.session = Store('session', {})

        self.ci = None
        self.environment = None
Exemple #2
0
    def setUp(self):
        self.config = MagicMock()

        self.current_time = time.time()
        fake_time = MagicMock()
        fake_time.time = MagicMock(side_effect=lambda: self.current_time)
        self.update_cache = Store('version-check-cache', {},
                                  tempfile.mkdtemp(), False)
        self.update_cache._defaults = UPDATE_CHECKER_CACHE._defaults
        self.command = CliInitCommand(self.config, False, False, False,
                                      'token', 'token', 'token',
                                      self.update_cache, fake_time)
Exemple #3
0
from cli.internal.utils.store import Store
from cli.version import __version__

LOG_PROTOCOL_TRACE = 4

AUTH = Store('auth', {'api_key': None, 'id_token': None, 'access_token': None})

ENDPOINTS = Store(
    'endpoints', {
        'client_id':
        'QLWpUwYOOcLlAJsmyQhQMXyeWn6RZpoc',
        'auth_url':
        'https://bymason.auth0.com/oauth/ro',
        'user_info_url':
        'https://bymason.auth0.com/userinfo',
        'platform_url_base':
        'https://platform.bymason.com',
        'api_url_base':
        'https://platform.bymason.com/api',
        'projects_url_path':
        '/dashboard/projects',
        'registry_signed_url_path':
        '/registry/signedurl',
        'registry_artifact_url_path':
        '/registry/artifacts',
        'builder_url_path':
        '/tracker/builder',
        'deploy_url_path':
        '/deploy',
        'console_create_url_path':
        '/controller/create',
Exemple #4
0
class StoreTest(unittest.TestCase):
    def setUp(self):
        runner = CliRunner()
        with runner.isolated_filesystem():
            self.store = Store('test', {'default': True}, os.path.abspath(''),
                               False)

    def test__getset__field_can_be_retrieved(self):
        self.store['key'] = 'value'

        self.assertEqual(self.store['key'], 'value')

    def test__getset__default_can_be_retrieved(self):
        self.assertEqual(self.store['default'], True)

    def test__getset__default_can_be_overwritten(self):
        self.store['default'] = False

        self.assertEqual(self.store['default'], False)

    def test__restore__garbage_file_is_ignored(self):
        self._write_data('foobar')
        self.store['key'] = 'value'

        self.store.restore()

        self.assertDictEqual(self.store._fields, {'key': 'value'})

    def test__restore__existing_fields_are_overwritten(self):
        self._write_data({'key': 'new value'})
        self.store['key'] = 'value'

        self.store.restore()

        self.assertDictEqual(self.store._fields, {'key': 'new value'})

    def test__restore__new_fields_are_merged(self):
        self._write_data({'otherKey': 'other value'})
        self.store['key'] = 'value'

        self.store.restore()

        self.assertDictEqual(self.store._fields, {
            'key': 'value',
            'otherKey': 'other value'
        })

    def test__restore__fields_are_initialized(self):
        self._write_data({'key': 'value'})

        self.store.restore()

        self.assertDictEqual(self.store._fields, {'key': 'value'})

    def test__save__fields_are_stored(self):
        self.store['key'] = 'value'

        self.store.save()
        self.store.clear()
        self.store.restore()

        self.assertDictEqual(self.store._fields, {'key': 'value'})

    def test__save__defaults_are_ignored(self):
        self.store['default'] = False

        self.store.save()
        self.store.clear()
        self.store.restore()

        self.assertDictEqual(self.store._fields, {'default': False})

    def test__clear__fields_are_wiped(self):
        self.store['key'] = 'value'

        self.store.clear()

        self.assertDictEqual(self.store._fields, {})

    def test__clear__defaults_are_ignored(self):
        self.store['key'] = 'value'

        self.store.clear()

        self.assertDictEqual(self.store._defaults, {'default': True})

    def _write_data(self, data):
        os.makedirs(os.path.dirname(self.store._file), exist_ok=True)
        with open(self.store._file, 'w') as f:
            if type(data) is dict:
                yaml.safe_dump(data, f)
            else:
                f.write(data)
Exemple #5
0
 def setUp(self):
     runner = CliRunner()
     with runner.isolated_filesystem():
         self.store = Store('test', {'default': True}, os.path.abspath(''),
                            False)
Exemple #6
0
class MasonAnalytics:
    def __init__(self, config):
        self.config = config
        self.handler = RequestHandler(config)
        self.instance = self._random_string()
        self.session = Store('session', {})

        self.ci = None
        self.environment = None

    def log_event(self, command=None, duration_seconds=None, exception=None):
        self._compute_environment()

        headers = {
            'Content-Type': 'application/json'
        }
        payload = {
            'property': 'mason-cli',
            'event': 'invoke',
            'environment': self.environment,
            'cli': {
                'version': __version__,
                'command': command,
                'instance': self.instance,
                'session': self._sanitized_session(),
                'log_level': self.config.logger.level,
                'exception': exception.__class__.__name__ if exception else None,
                'flags': list({k: v for k, v in
                               click.get_current_context().params.items() if v}.keys()),
                'ci': self.ci,
                'duration': duration_seconds
            }
        }

        url = build_url(self.config.endpoints_store, 'analytics_url')
        try:
            self.handler.post(url, headers=headers, json=payload)
        except Exception as e:
            self.config.logger.debug(e)

    def log_config(self, config):
        self._compute_environment()

        try:
            mapped_configs = self._mapped_config(config)
        except Exception as e:
            self.config.logger.debug(e)
            return

        headers = {
            'Content-Type': 'application/json'
        }
        payload = {
            'property': 'configs',
            'event': 'register',
            'environment': self.environment,
            'configs2': mapped_configs
        }

        url = build_url(self.config.endpoints_store, 'analytics_url')
        try:
            self.handler.post(url, headers=headers, json=payload)
        except Exception as e:
            self.config.logger.debug(e)

    def _compute_environment(self):
        if self.ci and self.environment:
            return

        self.ci = True if os.getenv("CI") else False

        sample_url = build_url(self.config.endpoints_store, 'deploy_url')
        if 'development' in sample_url:
            self.environment = 'development'
        elif 'staging' in sample_url:
            self.environment = 'staging'
        else:
            self.environment = 'production'

    def _sanitized_session(self):
        current_time = int(time.time())

        last_used = self.session['last_used']
        if last_used and current_time - last_used < 900:  # 15 minutes
            self.session['last_used'] = current_time
            self.session.save()

            return self.session['id']
        else:
            id = self._random_string()

            self.session['last_used'] = current_time
            self.session['id'] = id
            self.session.save()

            return id

    @staticmethod
    def _mapped_config(config):
        mapped_config = copy.deepcopy(config)

        mapped_config['customerApps'] = []
        for app in mapped_config.get('apps') or []:
            mapped_config['customerApps'].append({
                'name': app.get('package_name'),
                'version': str(app.get('version_code'))
            })
        mapped_config.pop('apps', None)

        anim = (mapped_config.get('media') or {}).get('bootanimation')
        if anim:
            mapped_config['bootanimation'] = [{
                'name': anim.get('name'),
                'version': str(anim.get('version'))
            }]
        splash = (mapped_config.get('media') or {}).get('splash')
        if splash:
            mapped_config['splash'] = [{
                'name': splash.get('name'),
                'version': str(splash.get('version'))
            }]
        mapped_config.pop('media', None)

        os = mapped_config.get('os') or {}
        os_configs = os.get('configurations') or {}
        mapped_config['name'] = os.get('name')
        mapped_config['version'] = str(os.get('version'))
        MasonAnalytics._map_os_config_items(mapped_config, os_configs, 'mason-management')
        MasonAnalytics._map_os_config_items(mapped_config, os_configs, 'mason-core')
        MasonAnalytics._map_os_config_items(mapped_config, os_configs, 'mason-fota')
        MasonAnalytics._map_os_config_items(mapped_config, os_configs, 'mason-app-updater')
        MasonAnalytics._map_os_config_items(mapped_config, os_configs, 'android')
        MasonAnalytics._map_os_config_items(mapped_config, os_configs, 'settings')
        MasonAnalytics._map_os_config_items(mapped_config, os_configs, 'systemui')
        mapped_config.pop('os', None)

        return mapped_config

    @staticmethod
    def _map_os_config_items(mapped_config, os_configs, name):
        items = (os_configs.get(name) or {}).items()

        name = name.replace('-', '_')
        mapped_config[name] = []
        for (key, val) in items:
            mapped_config[name].append({
                'key': key,
                'value': str(val)
            })

    @staticmethod
    def _random_string():
        letters = string.ascii_lowercase
        return ''.join(random.choice(letters) for i in range(32))