Пример #1
0
 def update_task(action, session, task, unit):
     session.state = states.SessionState.deploying
     task_info = models.Task()
     task_info.environment_id = session.environment_id
     task_info.description = dict(session.description.get('Objects'))
     task_info.action = task['action']
     status = models.Status()
     status.text = 'Action {0} is scheduled'.format(action[1]['name'])
     status.level = 'info'
     task_info.statuses.append(status)
     with unit.begin():
         unit.add(session)
         unit.add(task_info)
Пример #2
0
def report_notification(report):
    LOG.debug('Got report from orchestration ' 'engine:\n{0}'.format(report))

    report['entity_id'] = report['id']
    del report['id']

    status = models.Status()
    status.update(report)

    unit = session.get_session()
    #connect with deployment
    with unit.begin():
        running_deployment = get_last_deployment(unit, status.environment_id)
        status.task_id = running_deployment.id
        unit.add(status)
Пример #3
0
def update_task(action, session, task, unit):
    session.state = state.SessionState.deploying
    task_info = models.Task()
    task_info.environment_id = session.environment_id
    objects = session.description.get('Objects', None)
    if objects:
        task_info.description = token_sanitizer.TokenSanitizer().sanitize(
            dict(session.description.get('Objects')))
    task_info.action = task['action']
    status = models.Status()
    status.text = 'Action {0} is scheduled'.format(action)
    status.level = 'info'
    task_info.statuses.append(status)
    with unit.begin():
        unit.add(session)
        unit.add(task_info)
Пример #4
0
def report_notification(report):
    LOG.debug('Got report from orchestration '
              'engine:\n{report}'.format(report=report))

    report['entity_id'] = report.pop('id')

    status = models.Status()
    if 'timestamp' in report:
        dt = timeutils.parse_isotime(report.pop('timestamp'))
        report['created'] = dt.astimezone(pytz.utc).replace(tzinfo=None)
    status.update(report)

    unit = session.get_session()
    # connect with deployment
    with unit.begin():
        running_deployment = get_last_deployment(unit, status.environment_id)
        status.task_id = running_deployment.id
        unit.add(status)
Пример #5
0
    def process_result(context, result, environment_id):
        secure_result = token_sanitizer.TokenSanitizer().sanitize(result)
        LOG.debug('Got result from orchestration '
                  'engine:\n{0}'.format(secure_result))

        model = result['model']
        action_result = result.get('action', {})

        unit = session.get_session()
        environment = unit.query(models.Environment).get(environment_id)

        if not environment:
            LOG.warning(_LW('Environment result could not be handled, '
                            'specified environment not found in database'))
            return

        if model['Objects'] is None and model.get('ObjectsCopy', {}) is None:
            environments.EnvironmentServices.remove(environment_id)
            return

        environment.description = model
        if environment.description['Objects'] is not None:
            environment.description['Objects']['services'] = \
                environment.description['Objects'].pop('applications', [])
            # environment.networking = result.get('networking', {})
            action_name = 'Deployment'
            deleted = False
        else:
            action_name = 'Deletion'
            deleted = True
        environment.version += 1
        environment.save(unit)

        # close deployment
        deployment = get_last_deployment(unit, environment.id)
        deployment.finished = timeutils.utcnow()
        deployment.result = action_result

        num_errors = unit.query(models.Status)\
            .filter_by(level='error', task_id=deployment.id).count()
        num_warnings = unit.query(models.Status)\
            .filter_by(level='warning', task_id=deployment.id).count()

        final_status_text = action_name + ' finished'
        if num_errors:
            final_status_text += " with errors"

        elif num_warnings:
            final_status_text += " with warnings"

        status = models.Status()
        status.task_id = deployment.id
        status.text = final_status_text
        status.level = 'info'
        deployment.statuses.append(status)
        deployment.save(unit)

        # close session
        conf_session = unit.query(models.Session).filter_by(
            **{'environment_id': environment.id,
               'state': states.SessionState.DEPLOYING if not deleted
               else states.SessionState.DELETING}).first()
        if num_errors > 0:
            conf_session.state = \
                states.SessionState.DELETE_FAILURE if deleted else \
                states.SessionState.DEPLOY_FAILURE
        else:
            conf_session.state = states.SessionState.DEPLOYED
        conf_session.save(unit)

        # output application tracking information
        message = _LI('EnvId: {0} TenantId: {1} Status: {2} Apps: {3}').format(
            environment.id,
            environment.tenant_id,
            _('Failed') if num_errors + num_warnings > 0 else _('Successful'),
            ', '.join(map(
                lambda a: a['?']['type'],
                model['Objects']['services']
            ))
        )
        LOG.info(message)
Пример #6
0
    def test_report_env_stats(self, mock_notifier):
        now = timeutils.utcnow()
        later = now + dt.timedelta(minutes=1)

        session = db_session.get_session()

        environment1 = models.Environment(
            name='test_environment1', tenant_id='test_tenant_id1',
            version=2, id='test_env_id_1',
            created=now,
            updated=later,
            description={
                'Objects': {
                    'applications': ['app1'],
                    'services': ['service1']
                }
            }
        )
        environment2 = models.Environment(
            name='test_environment2', tenant_id='test_tenant_id2',
            version=1, id='test_env_id_2',
            created=now,
            updated=later,
            description={
                'Objects': {
                    'applications': ['app2'],
                    'services': ['service3']
                }
            }
        )
        environment3 = models.Environment(
            name='test_environment3', tenant_id='test_tenant_id2',
            version=1, id='test_env_id_3',
            created=now,
            updated=later,
            description={}
        )

        session_1 = models.Session(
            environment=environment1, user_id='test_user_id',
            description={},
            state=states.SessionState.DEPLOYED,
            version=1
        )

        session_2 = models.Session(
            environment=environment2, user_id='test_user_id',
            description={},
            state=states.SessionState.DEPLOYED,
            version=0
        )

        session_3 = models.Session(
            environment=environment3, user_id='test_user_id',
            description={},
            state=states.SessionState.DEPLOY_FAILURE,
            version=1
        )

        task_1 = models.Task(
            id='task_id_1',
            environment=environment1,
            description={},
            created=now,
            started=now,
            updated=later,
            finished=later
        )

        task_2 = models.Task(
            id='task_id_2',
            environment=environment2,
            description={},
            created=now,
            started=now,
            updated=later,
            finished=later
        )

        task_3 = models.Task(
            id='task_id_3',
            environment=environment3,
            description={},
            created=now,
            started=now,
            updated=later,
            finished=later
        )

        status_1 = models.Status(
            id='status_id_1',
            task_id='task_id_1',
            text='Deployed',
            level='info'
        )

        status_2 = models.Status(
            id='status_id_2',
            task_id='task_id_2',
            text='Deployed',
            level='info'
        )

        status_3 = models.Status(
            id='status_id_3',
            task_id='task_id_3',
            text='Something was wrong',
            level='error'
        )

        session.add_all([environment1, environment2, environment3])
        session.add_all([session_1, session_2, session_3])
        session.add_all([task_1, task_2, task_3])
        session.add_all([status_1, status_2, status_3])

        session.flush()

        self.service.report_env_stats()

        self.assertEqual(mock_notifier.call_count, 2)

        dict_env_1 = {'version': 2,
                      'updated': later,
                      'tenant_id': u'test_tenant_id1',
                      'created': now,
                      'description_text': u'',
                      'status': 'ready',
                      'id': u'test_env_id_1',
                      'name': u'test_environment1'}

        dict_env_2 = {'version': 1,
                      'updated': later,
                      'tenant_id': u'test_tenant_id2',
                      'created': now,
                      'description_text': u'',
                      'status': 'ready',
                      'id': u'test_env_id_2',
                      'name': u'test_environment2'}

        calls = [mock.call('environment.exists', dict_env_1),
                 mock.call('environment.exists', dict_env_2)]

        mock_notifier.assert_has_calls(calls)
Пример #7
0
    def process_result(context, result, environment_id):
        secure_result = token_sanitizer.TokenSanitizer().sanitize(result)
        LOG.debug('Got result from orchestration '
                  'engine:\n{0}'.format(secure_result))

        unit = session.get_session()
        environment = unit.query(models.Environment).get(environment_id)

        if not environment:
            LOG.warning(
                _('Environment result could not be handled, specified '
                  'environment was not found in database'))
            return

        if result['Objects'] is None and result.get('ObjectsCopy', {}) is None:
            environments.EnvironmentServices.remove(environment_id)
            return

        environment.description = result
        if environment.description['Objects'] is not None:
            environment.description['Objects']['services'] = \
                environment.description['Objects'].pop('applications', [])
            # environment.networking = result.get('networking', {})
            action_name = 'Deployment'
            deleted = False
        else:
            action_name = 'Deletion'
            deleted = True
        environment.version += 1
        environment.save(unit)

        #close deployment
        deployment = get_last_deployment(unit, environment.id)
        deployment.finished = timeutils.utcnow()

        num_errors = unit.query(models.Status)\
            .filter_by(level='error', task_id=deployment.id).count()
        num_warnings = unit.query(models.Status)\
            .filter_by(level='warning', task_id=deployment.id).count()

        final_status_text = action_name + ' finished'
        if num_errors:
            final_status_text += " with errors"

        elif num_warnings:
            final_status_text += " with warnings"

        status = models.Status()
        status.task_id = deployment.id
        status.text = final_status_text
        status.level = 'info'
        deployment.statuses.append(status)
        deployment.save(unit)

        #close session
        conf_session = unit.query(models.Session).filter_by(
            **{
                'environment_id': environment.id,
                'state': 'deploying' if not deleted else 'deleting'
            }).first()
        if num_errors > 0:
            conf_session.state = \
                sessions.SessionState.DELETE_FAILURE if deleted else \
                sessions.SessionState.DEPLOY_FAILURE
        else:
            conf_session.state = sessions.SessionState.DEPLOYED
        conf_session.save(unit)
Пример #8
0
    def process_result(context, result, environment_id):
        secure_result = token_sanitizer.TokenSanitizer().sanitize(result)
        LOG.debug('Got result from orchestration '
                  'engine:\n{result}'.format(result=secure_result))

        model = result['model']
        action_result = result.get('action', {})

        unit = session.get_session()
        environment = unit.query(models.Environment).get(environment_id)

        if not environment:
            LOG.warning('Environment result could not be handled, '
                        'specified environment not found in database')
            return

        if model['Objects'] is None and model.get('ObjectsCopy', {}) is None:
            environments.EnvironmentServices.remove(environment_id)
            return

        environment.description = model
        if environment.description['Objects'] is not None:
            environment.description['Objects']['services'] = \
                environment.description['Objects'].pop('applications', [])
            action_name = 'Deployment'
            deleted = False
        else:
            action_name = 'Deletion'
            deleted = True
        environment.version += 1
        environment.save(unit)

        # close deployment
        deployment = get_last_deployment(unit, environment.id)
        deployment.finished = timeutils.utcnow()
        deployment.result = action_result

        num_errors = unit.query(models.Status)\
            .filter_by(level='error', task_id=deployment.id).count()
        num_warnings = unit.query(models.Status)\
            .filter_by(level='warning', task_id=deployment.id).count()

        final_status_text = action_name + ' finished'
        if num_errors:
            final_status_text += " with errors"

        elif num_warnings:
            final_status_text += " with warnings"

        status = models.Status()
        status.task_id = deployment.id
        status.text = final_status_text
        status.level = 'info'
        deployment.statuses.append(status)
        deployment.save(unit)

        # close session
        conf_session = unit.query(models.Session).filter_by(
            **{
                'environment_id':
                environment.id,
                'state':
                states.SessionState.DEPLOYING if not deleted else states.
                SessionState.DELETING
            }).first()
        if num_errors > 0 or result['action'].get('isException'):
            conf_session.state = \
                states.SessionState.DELETE_FAILURE if deleted else \
                states.SessionState.DEPLOY_FAILURE
        else:
            conf_session.state = states.SessionState.DEPLOYED
        conf_session.save(unit)

        # output application tracking information
        services = []
        objects = model['Objects']
        if objects:
            services = objects.get('services')
        if num_errors + num_warnings > 0:
            LOG.warning('EnvId: {env_id} TenantId: {tenant_id} Status: '
                        'Failed Apps: {services}'.format(
                            env_id=environment.id,
                            tenant_id=environment.tenant_id,
                            services=services))
        else:
            LOG.info('EnvId: {env_id} TenantId: {tenant_id} Status: '
                     'Successful Apps: {services}'.format(
                         env_id=environment.id,
                         tenant_id=environment.tenant_id,
                         services=services))
            if action_name == 'Deployment':
                env = environment.to_dict()
                env["deployment_started"] = deployment.started
                env["deployment_finished"] = deployment.finished
                status_reporter.get_notifier().report('environment.deploy.end',
                                                      env)