def test_get_messages(): assert not get_messages((), _CONTEXT_NAME) messages = get_messages((_MESSAGE_TYPE, ), _CONTEXT_NAME) assert messages is not None and not messages test_message() messages = get_messages((_MESSAGE_TYPE, ), _CONTEXT_NAME) assert messages and len(messages) == 1
def fetch_upgrade_report_messages(context_id, renderer): """ :param context_id: ID to identify the needed messages :type context_id: str :param renderer: How messages will be rendered (e.g html or plaintext) :type context_id: str :return: All upgrade messages of type "Report" withing the given context """ report_msgs = get_messages(names=['Report'], context=context_id) if report_msgs: messages_to_print = [] for message in report_msgs: topic = message.get('topic') phase = message.get('phase') data = json.loads(message.get('message').get('data')) merged_data = {'topic': topic, 'phase': phase} merged_data.update({ 'audience': data['audience'], 'title': data['title'], 'flags': data['flags'], 'severity': data['severity'] }) detail = _flatten_dict(json.loads(data['detail'])) merged_data.update(detail) template = Template(data['renderers'].get(renderer)) messages_to_print.append(template.render(merged_data)) return messages_to_print return None
def save(self): self._crumbs['run_id'] = os.environ.get('LEAPP_EXECUTION_ID', 'N/A') messages = get_messages(('IPUConfig',), self._crumbs['run_id']) versions = json.loads((messages or [{}])[0].get('message', {}).get( 'data', '{}')).get('version', {'target': 'N/A', 'source': 'N/A'}) self._crumbs['target_os'] = 'Red Hat Enterprise Linux {target}'.format(**versions) self._crumbs['source_os'] = 'Red Hat Enterprise Linux {source}'.format(**versions) self._crumbs['activity_ended'] = datetime.datetime.utcnow().isoformat() + 'Z' self._crumbs['env'] = {k: v for k, v in os.environ.items() if k.startswith('LEAPP_')} try: with open('/etc/migration-results', 'a+') as crumbs: crumbs.seek(0) doc = {'activities': []} try: content = json.load(crumbs) if isinstance(content, dict): if isinstance(content.get('activities', None), list): doc = content except JSONDecodeError: # Expected to happen when /etc/migration-results is still empty or does not yet exist pass doc['activities'].append(self._crumbs) crumbs.seek(0) crumbs.truncate() json.dump(doc, crumbs, indent=2, sort_keys=True) crumbs.write('\n') except OSError: sys.stderr.write('WARNING: Could not write to /etc/migration-results\n')
def fetch_upgrade_report_messages(context_id): """ :param context_id: ID to identify the needed messages :type context_id: str :return: All upgrade messages of type "Report" withing the given context """ report_msgs = get_messages(names=['Report'], context=context_id) or [] messages = [] for message in report_msgs: data = message['message']['data'] # We need to be able to uniquely identify each message so we compute # a hash of: context UUID, message ID in the database and hash of the # data section of the message itself sha256 = hashlib.sha256() sha256.update(message['message']['hash'].encode('utf-8')) sha256.update(message['context'].encode('utf-8')) sha256.update(str(message['id']).encode('utf-8')) envelope = { 'timeStamp': message['stamp'], 'hostname': message['hostname'], 'actor': message['actor'], 'id': sha256.hexdigest() } report = json.loads(json.loads(data).get('report')) report.update(envelope) messages.append(report) return messages
def fetch_upgrade_report_raw(context_id, renderers=True): """ :param context_id: ID to identify the needed messages :type context_id: str :param renderers: whether copy or skip renderers field of original message :type boolean :return: A list of upgrade messages of type "Report" withing the given context, each message is a dict of raw data. """ report_msgs = get_messages(names=['Report'], context=context_id) or [] messages = [] for message in report_msgs: data = json.loads(message.get('message').get('data')) merged_data = { 'topic': message.get('topic'), 'phase': message.get('phase'), 'audience': data['audience'], 'title': data['title'], 'flags': data['flags'], 'severity': data['severity'] } if renderers: merged_data['renderers'] = data['renderers'] detail = _flatten_dict(json.loads(data['detail'])) merged_data.update(detail) messages.append(merged_data) return messages
def restore_leapp_env_vars(context): """ Restores leapp environment variables from the `IPUConfig` message. """ messages = get_messages(('IPUConfig',), context) leapp_env_vars = json.loads((messages or [{}])[0].get('message', {}).get('data', '{}')).get('leapp_env_vars', {}) for entry in leapp_env_vars: os.environ[entry['name']] = entry['value']
def test_run_workflow(repository_dir): # We need the workflow to be created already if not repository_dir.join('workflows/test.py').check(file=True): test_new_workflow(repository_dir) with pytest.raises(CalledProcessError): check_call(['snactor', 'workflow', 'run', 'Test']) with repository_dir.as_cwd(): check_call(['snactor', 'workflow', 'run', 'Test']) check_call(['snactor', '--debug', 'workflow', 'run', 'Test']) check_call(['snactor', 'workflow', '--debug', 'run', 'Test']) check_call(['snactor', 'workflow', 'run', '--debug', 'Test']) test_clear_messages(repository_dir) connection = audit.create_connection('.leapp/leapp.db') assert len( audit.get_messages( names=('TestModel', ), context=context.last_snactor_context(connection), connection=connection)) == 0 check_call( ['snactor', 'workflow', 'run', '--debug', '--save-output', 'Test']) assert len( audit.get_messages( names=('TestModel', ), context=context.last_snactor_context(connection), connection=connection)) == 1 check_call(['snactor', 'workflow', 'run', 'Test']) assert len( audit.get_messages( names=('TestModel', ), context=context.last_snactor_context(connection), connection=connection)) == 1 check_call(['snactor', 'workflow', 'run', '--save-output', 'Test']) assert len( audit.get_messages( names=('TestModel', ), context=context.last_snactor_context(connection), connection=connection)) == 2
def _perform_load(self, consumes): context = os.environ.get('LEAPP_EXECUTION_ID', 'TESTING-CONTEXT') self._data = get_messages([consume.__name__ for consume in consumes], context)