예제 #1
0
def mkreq(req):
  # This function fits the old style where TaskRequest was stored first, before
  # TaskToRun and TaskResultSummary.
  task_request.init_new_request(req, True)
  req.key = task_request.new_request_key()
  req.put()
  return req
예제 #2
0
  def make_task_request(
      self, service_account, service_account_token, try_number=1):
    now = utils.utcnow()
    args = {
      'created_ts': now,
      'manual_tags': [u'tag:1'],
      'name': 'Request with %s' % service_account,
      'priority': 50,
      'task_slices': [
        task_request.TaskSlice(
            expiration_secs=60,
            properties=task_request.TaskProperties(
                command=[u'command1'],
                dimensions_data={u'pool': [u'default']},
                execution_timeout_secs=24*60*60)),
      ],
      'user': '******',
    }
    req = task_request.TaskRequest(**args)
    task_request.init_new_request(req, True)
    req.key = task_request.new_request_key()
    req.service_account = service_account
    req.service_account_token = service_account_token
    req.put()

    summary_key = task_pack.request_key_to_result_summary_key(req.key)
    run_result_key = task_pack.result_summary_key_to_run_result_key(
        summary_key, try_number)
    return task_pack.pack_run_result_key(run_result_key)
예제 #3
0
def _gen_request(**kwargs):
    """Returns an initialized task_request.TaskRequest."""
    now = utils.utcnow()
    args = {
        # Don't be confused, this is not part of the API. This code is constructing
        # a DB entity, not a swarming_rpcs.NewTaskRequest.
        u'created_ts':
        now,
        u'manual_tags': [u'tag:1'],
        u'name':
        u'yay',
        u'priority':
        50,
        u'task_slices': [
            task_request.TaskSlice(expiration_secs=60,
                                   properties=_gen_properties(),
                                   wait_for_capacity=False),
        ],
        u'user':
        u'Jesus',
    }
    args.update(kwargs)
    ret = task_request.TaskRequest(**args)
    task_request.init_new_request(ret, True, task_request.TEMPLATE_AUTO)
    return ret
예제 #4
0
def _gen_request_slices(**kwargs):
  """Creates a TaskRequest."""
  now = utils.utcnow()
  args = {
    u'created_ts': now,
    u'manual_tags': [u'tag:1'],
    u'name': u'Request name',
    u'priority': 50,
    u'user': u'Jesus',
  }
  args.update(kwargs)
  req = task_request.TaskRequest(**args)
  task_request.init_new_request(req, True, task_request.TEMPLATE_AUTO)
  return req
예제 #5
0
def _gen_request_slices(**kwargs):
  """Creates a TaskRequest."""
  now = utils.utcnow()
  args = {
    u'created_ts': now,
    u'manual_tags': [u'tag:1'],
    u'name': u'Request name',
    u'priority': 50,
    u'task_slices': [
      task_request.TaskSlice(expiration_secs=30, properties=_gen_properties()),
    ],
    u'user': u'Jesus',
  }
  args.update(kwargs)
  # Note that ndb model constructor accepts dicts for structured properties.
  req = task_request.TaskRequest(**args)
  task_request.init_new_request(req, True)
  return req
예제 #6
0
def _gen_request(properties=None):
  """Creates a TaskRequest."""
  now = utils.utcnow()
  args = {
    'created_ts': now,
    'manual_tags': [u'tag:1'],
    'name': 'Request name',
    'priority': 50,
    'task_slices': [
      task_request.TaskSlice(
          expiration_secs=60,
          properties=properties or _gen_properties()),
    ],
    'user': '******',
  }
  req = task_request.TaskRequest(**args)
  task_request.init_new_request(req, True, task_request.TEMPLATE_AUTO)
  return req
예제 #7
0
def _gen_request_slice(**kwargs):
    """Creates a TaskRequest."""
    now = utils.utcnow()
    args = {
        'created_ts':
        now,
        'manual_tags': [u'tag:1'],
        'name':
        'Request name',
        'priority':
        50,
        'task_slices': [
            task_request.TaskSlice(expiration_secs=60,
                                   properties=_gen_properties()),
        ],
        'user':
        '******',
    }
    args.update(kwargs)
    ret = task_request.TaskRequest(**args)
    task_request.init_new_request(ret, True)
    ret.key = task_request.new_request_key()
    ret.put()
    return ret
예제 #8
0
    def new(self, request):
        """Creates a new task.

    The task will be enqueued in the tasks list and will be executed at the
    earliest opportunity by a bot that has at least the dimensions as described
    in the task request.
    """
        sb = (request.properties.secret_bytes
              if request.properties is not None else None)
        if sb is not None:
            request.properties.secret_bytes = "HIDDEN"
        logging.debug('%s', request)
        if sb is not None:
            request.properties.secret_bytes = sb

        try:
            request_obj, secret_bytes = message_conversion.new_task_request_from_rpc(
                request, utils.utcnow())
            for index in xrange(request_obj.num_task_slices):
                apply_server_property_defaults(
                    request_obj.task_slice(index).properties)
            task_request.init_new_request(
                request_obj, acl.can_schedule_high_priority_tasks())
            # We need to call the ndb.Model pre-put check earlier because the
            # following checks assume that the request itself is valid and could crash
            # otherwise.
            request_obj._pre_put_hook()
        except (datastore_errors.BadValueError, TypeError, ValueError) as e:
            logging.exception(
                'Here\'s what was wrong in the user new task request:')
            raise endpoints.BadRequestException(e.message)

        # Make sure the caller is actually allowed to schedule the task before
        # asking the token server for a service account token.
        task_scheduler.check_schedule_request_acl(request_obj)

        # If request_obj.service_account is an email, contact the token server to
        # generate "OAuth token grant" (or grab a cached one). By doing this we
        # check that the given service account usage is allowed by the token server
        # rules at the time the task is posted. This check is also performed later
        # (when running the task), when we get the actual OAuth access token.
        if service_accounts.is_service_account(request_obj.service_account):
            if not service_accounts.has_token_server():
                raise endpoints.BadRequestException(
                    'This Swarming server doesn\'t support task service accounts '
                    'because Token Server URL is not configured')
            max_lifetime_secs = request_obj.max_lifetime_secs
            try:
                # Note: this raises AuthorizationError if the user is not allowed to use
                # the requested account or service_accounts.InternalError if something
                # unexpected happens.
                duration = datetime.timedelta(seconds=max_lifetime_secs)
                request_obj.service_account_token = (
                    service_accounts.get_oauth_token_grant(
                        service_account=request_obj.service_account,
                        validity_duration=duration))
            except service_accounts.InternalError as exc:
                raise endpoints.InternalServerErrorException(exc.message)

        # If the user only wanted to evaluate scheduling the task, but not actually
        # schedule it, return early without a task_id.
        if request.evaluate_only:
            request_obj._pre_put_hook()
            return swarming_rpcs.TaskRequestMetadata(
                request=message_conversion.task_request_to_rpc(request_obj))

        try:
            result_summary = task_scheduler.schedule_request(
                request_obj, secret_bytes)
        except (datastore_errors.BadValueError, TypeError, ValueError) as e:
            raise endpoints.BadRequestException(e.message)

        previous_result = None
        if result_summary.deduped_from:
            previous_result = message_conversion.task_result_to_rpc(
                result_summary, False)

        return swarming_rpcs.TaskRequestMetadata(
            request=message_conversion.task_request_to_rpc(request_obj),
            task_id=task_pack.pack_result_summary_key(result_summary.key),
            task_result=previous_result)