Пример #1
0
    def test_lock_unreachable_network(self, m):
        def callback_prevent(url, **kwargs):
            return CallbackResult("{'message': 'Preventing lock acquired'}",
                                  status=200)

        def callback_unlock(url, **kwargs):
            return CallbackResult("{'message': 'UnLocked'}", status=200)

        m.post(self.n1.url('api_1_0.locker_prevent'),
               callback=callback_prevent)
        m.post(self.n2.url('api_1_0.locker_prevent'),
               callback=callback_prevent)
        m.post(self.n2.url('api_1_0.locker_unlock'), callback=callback_unlock)

        self.n1.route.cost = None
        self.n1.route.gate = None
        self.n1.route.proxy_server = None

        with self.assertRaises(errors.LockError) as e:
            lock(Scope.CATALOG, servers=[self.n1, self.n2], identity=ROOT)

        self.assertEqual(Scope.CATALOG, e.exception.scope)
        self.assertEqual(errors.LockError.action_map['P'], e.exception.action)
        self.assertDictEqual(
            errors.format_error_content(
                errors.LockError(
                    Scope.CATALOG,
                    action='P',
                    responses=[
                        Response(exception=errors.UnreachableDestination(
                            self.n1),
                                 server=self.n1)
                    ])), errors.format_error_content(e.exception))
        self.assertEqual(2, len(m.requests))
Пример #2
0
def _deploy_orchestration(orchestration: Orchestration,
                          var_context: 'Context',
                          hosts: t.Dict[str, t.List[Id]],
                          execution: OrchExecution,
                          lock_retries,
                          lock_delay,
                          timeout
                          ) -> Id:
    """
    Parameters
    ----------
    orchestration
        orchestration to deploy
    params
        parameters to pass to the steps

    Returns
    -------
    t.Tuple[bool, bool, t.Dict[int, dpl.CompletedProcess]]:
        tuple with 3 values. (boolean indicating if invoke process ended up successfully,
        boolean indicating if undo process ended up successfully,
        dict with all the executions). If undo process not executed, boolean set to None
    """
    rse = RegisterStepExecution(execution)
    kwargs = dict()
    kwargs['start_time'] = execution.start_time or get_now()
    cc = create_cmd_from_orchestration(orchestration, var_context, hosts=hosts, register=rse, executor=executor)

    # convert UUID into str as in_ filter does not handle UUID type
    all = [str(s) for s in hosts['all']]
    servers = Server.query.filter(Server.id.in_(all)).all()
    scope_enabled = locker_scope_enabled(Scope.ORCHESTRATION)
    if scope_enabled:
        try:
            applicant = lock.lock(Scope.ORCHESTRATION, servers, applicant=var_context.env.get('root_orch_execution_id'),
                                  retries=lock_retries, delay=lock_delay)
        except errors.LockError as e:
            kwargs.update(success=False, message=str(e))
            rse.update_orch_execution(**kwargs)
            raise
    try:
        kwargs['success'] = cc.invoke(timeout=timeout)
        if not kwargs['success'] and orchestration.undo_on_error:
            kwargs['undo_success'] = cc.undo()
        kwargs['end_time'] = get_now()
        rse.update_orch_execution(**kwargs)
    except Exception as e:
        current_app.logger.exception("Exception while executing invocation command")
        kwargs.update(success=False, message=str(e))
        rse.update_orch_execution(**kwargs)
        try:
            db.session.rollback()
        except:
            pass

    finally:
        if scope_enabled:
            lock.unlock(Scope.ORCHESTRATION, applicant=applicant, servers=servers)

    return execution.id
Пример #3
0
    def test_lock_catalog_error_on_preventing(self, m):
        def callback_prevent(url, **kwargs):
            self.assertDictEqual(
                kwargs['json'], {
                    'scope':
                    'CATALOG',
                    'applicant': [
                        str(Server.get_current().id),
                        str(self.n1.id),
                        str(self.n2.id)
                    ]
                })
            return CallbackResult("{'message': 'Preventing lock acquired'}",
                                  status=200)

        def callback_unlock(url, **kwargs):
            self.assertDictEqual(
                kwargs['json'], {
                    'scope':
                    'CATALOG',
                    'action':
                    'UNLOCK',
                    'applicant': [
                        str(Server.get_current().id),
                        str(self.n1.id),
                        str(self.n2.id)
                    ]
                })
            return CallbackResult("{'message': 'UnLocked'}", status=200)

        m.post(Server.get_current().url('api_1_0.locker_prevent'),
               callback=callback_prevent)
        m.post(self.n1.url('api_1_0.locker_prevent'),
               exception=ClientConnectionError())
        m.post(self.n2.url('api_1_0.locker_prevent'),
               callback=callback_prevent)
        m.post(Server.get_current().url('api_1_0.locker_unlock'),
               callback=callback_unlock)
        m.post(self.n2.url('api_1_0.locker_unlock'), callback=callback_unlock)

        with self.assertRaises(errors.LockError):
            lock(Scope.CATALOG, [Server.get_current(), self.n1, self.n2],
                 identity=ROOT)

        c = Locker.query.get(Scope.CATALOG)
        self.assertEqual(State.UNLOCKED, c.state)
Пример #4
0
    def test_lock_catalog(self, m):
        def callback_prevent(url, **kwargs):
            assert kwargs['json'] == {
                'scope': 'CATALOG',
                'datemark': self.datemark,
                'applicant': [Server.get_current().id, self.n1.id, self.n2.id]
            }
            return CallbackResult("{'message': 'Preventing lock acquired'}",
                                  status=200)

        def callback_lock(url, **kwargs):
            assert kwargs['json'] == {
                'scope':
                'CATALOG',
                'applicant': [
                    str(Server.get_current().id),
                    str(self.n1.id),
                    str(self.n2.id)
                ]
            }
            return CallbackResult("{'message': 'Locked'}", status=200)

        m.post(Server.get_current().url('api_1_0.locker_prevent'),
               callback=callback_prevent)
        m.post(self.n1.url('api_1_0.locker_prevent'),
               callback=callback_prevent)
        m.post(self.n2.url('api_1_0.locker_prevent'),
               callback=callback_prevent)
        m.post(Server.get_current().url('api_1_0.locker_lock'),
               callback=callback_lock)
        m.post(self.n1.url('api_1_0.locker_lock'), callback=callback_lock)
        m.post(self.n2.url('api_1_0.locker_lock'), callback=callback_lock)

        applicant = lock(Scope.CATALOG,
                         [Server.get_current(), self.n1, self.n2],
                         identity=ROOT)

        self.assertEqual(applicant,
                         [Server.get_current().id, self.n1.id, self.n2.id])
Пример #5
0
 def test_lock_no_server(self):
     with self.assertRaises(errors.NoServerToLock):
         ret = lock(Scope.ORCHESTRATION)