Ejemplo n.º 1
0
def launch_operation():
    data = request.get_json()
    operation = pickle.loads(
        base64.b64decode(data['operation'].encode('ascii')))
    var_context = pickle.loads(
        base64.b64decode(data['var_context'].encode('ascii')))
    params = pickle.loads(base64.b64decode(data['params'].encode('ascii')))
    orch_exec_json = data['orch_execution']
    orch_exec = OrchExecution.from_json(orch_exec_json)

    se = StepExecution(id=var_context.env.get('step_execution_id'),
                       server=g.server,
                       step_id=data.get('step_id'),
                       orch_execution=orch_exec,
                       params=params,
                       start_time=get_now())
    db.session.add_all([orch_exec, se])
    db.session.commit()
    future = executor.submit(run_command_and_callback,
                             operation,
                             params,
                             var_context,
                             g.source,
                             se,
                             data['event_id'],
                             identity=get_jwt_identity(),
                             timeout=data.get('timeout', None))
    try:
        r = future.result(1)
    except concurrent.futures.TimeoutError:
        return {}, 204
    except Exception as e:
        current_app.logger.exception(
            f"Exception got when executing step {data.get('step_id')}. See logs for more information"
        )
        return {
            'error':
            f"Exception got when executing step {data.get('step_id')}. See logs for more information"
        }, 500
    else:
        return r, 200
Ejemplo n.º 2
0
    def test_to_from_json(self):
        start = dt.datetime(2019, 4, 1, tzinfo=dt.timezone.utc)
        end = dt.datetime(2019, 4, 2, tzinfo=dt.timezone.utc)
        u = User('user', id='cccccccc-1234-5678-1234-56781234ccc1')
        db.session.add(u)
        o = Orchestration('run_orch', 1, id='eeeeeeee-1234-5678-1234-56781234eee1')
        s = o.add_step(undo=False,
                       action_template=ActionTemplate('orchestration', 1, ActionType.ORCHESTRATION, code=''))

        co = Orchestration('child', 1, id='eeeeeeee-1234-5678-1234-56781234eee2')
        cs = o.add_step(undo=False, action_template=ActionTemplate('action', 1, ActionType.SHELL, code=''))

        db.session.add_all([o, s, co, cs])

        oe = OrchExecution(id='bbbbbbbb-1234-5678-1234-56781234bbb1', start_time=start,
                           end_time=end,
                           target={'all': [str(self.me.id), str(self.remote.id)], 'backend': self.remote.name},
                           params={'params': 'content'},
                           executor=u,
                           orchestration=o
                           )
        se = StepExecution(id='aaaaaaaa-1234-5678-1234-56781234aaa1', start_time=start, end_time=end, step=s,
                           orch_execution_id=oe.id,
                           params={'orchestration_id': 'eeeeeeee-1234-5678-1234-56781234eee2'}, rc=None, success=True,
                           server=self.remote)

        coe = OrchExecution(id='bbbbbbbb-1234-5678-1234-56781234bbb2', start_time=start,
                            end_time=end,
                            target={'all': [str(self.me.id), str(self.remote.id)], 'backend': self.remote.name},
                            params={'params': 'content'},
                            executor=u,
                            orchestration=o,
                            parent_step_execution_id=se.id
                            )
        cse = StepExecution(id='aaaaaaaa-1234-5678-1234-56781234aaa2', start_time=start, end_time=end, step=s,
                            orch_execution_id=coe.id,
                            params={'param': 'data'}, rc=0, success=True, server=self.remote,
                            )

        se.child_orch_execution_id = coe.id
        db.session.add_all([oe, se, coe, cse])
        db.session.commit()
        self.assertDictEqual(dict(id=str(oe.id),
                                  start_time=start.strftime(defaults.DATETIME_FORMAT),
                                  end_time=end.strftime(defaults.DATETIME_FORMAT),
                                  target={'all': [str(self.me.id), str(self.remote.id)], 'backend': self.remote.name},
                                  params={'params': 'content'}, orchestration_id=str(o.id),
                                  success=None, undo_success=None,
                                  executor_id='cccccccc-1234-5678-1234-56781234ccc1',
                                  message=None),
                             oe.to_json())

        self.assertDictEqual(dict(id=str(oe.id),
                                  start_time=start.strftime(defaults.DATETIME_FORMAT),
                                  end_time=end.strftime(defaults.DATETIME_FORMAT),
                                  target={'all': [str(self.me), str(self.remote)], 'backend': self.remote.name},
                                  params={'params': 'content'},
                                  orchestration={'id': 'eeeeeeee-1234-5678-1234-56781234eee1',
                                                 'name': 'run_orch',
                                                 'version': 1},
                                  success=None, undo_success=None,
                                  executor='user',
                                  message=None),
                             oe.to_json(human=True))

        dumped = oe.to_json(add_step_exec=True)
        self.assertEqual(1, len(dumped['steps']))
        self.assertEqual('aaaaaaaa-1234-5678-1234-56781234aaa1', dumped['steps'][0]['id'])
        self.assertIn('orch_execution', dumped['steps'][0])

        o_e_json = oe.to_json()
        db.session.commit()
        del oe

        # load existent object
        smashed = OrchExecution.from_json(o_e_json)

        self.assertEqual('bbbbbbbb-1234-5678-1234-56781234bbb1', smashed.id)
        self.assertEqual(start, smashed.start_time)
        self.assertEqual(end, smashed.end_time)
        self.assertEqual(o.id, smashed.orchestration.id)
        self.assertDictEqual({'all': [str(self.me.id), str(self.remote.id)], 'backend': self.remote.name},
                             smashed.target)
        self.assertDictEqual({'params': 'content'}, smashed.params)
        self.assertEqual(User.get_by_name('user'), smashed.executor)
        self.assertEqual(None, smashed.service)
        self.assertEqual(None, smashed.success)
        self.assertEqual(None, smashed.undo_success)

        # load new object and insert into database
        new_obj = OrchExecution.from_json(dict(id='bbbbbbbb-1234-5678-1234-56781234bbb3',
                                               start_time=start.strftime(defaults.DATETIME_FORMAT),
                                               end_time=end.strftime(defaults.DATETIME_FORMAT),
                                               target={'all': [str(self.me.id), str(self.remote.id)],
                                                       'backend': self.remote.name},
                                               params={'params': 'content'},
                                               orchestration_id=str(o.id),
                                               service_id=None,
                                               success=None, undo_success=None,
                                               executor_id='cccccccc-1234-5678-1234-56781234ccc1'))
        db.session.add(new_obj)
        db.session.commit()