Example #1
0
  def execute(self, context):
    config = context.get_job_config(context.options.jobspec, context.options.config_file)
    if config.raw().has_cron_schedule():
      raise context.CommandError(
          EXIT_COMMAND_FAILURE,
          "Cron jobs may only be scheduled with \"aurora cron schedule\" command")

    api = context.get_api(config.cluster())
    resp = api.create_job(config)
    context.log_response_and_raise(resp, err_code=EXIT_COMMAND_FAILURE,
                                   err_msg="Job creation failed due to error:")
    if context.options.open_browser:
      webbrowser.open_new_tab(get_job_page(api, context.options.jobspec))
    if context.options.wait_until == "RUNNING":
      JobMonitor(api.scheduler_proxy, config.job_key()).wait_until(JobMonitor.running_or_finished)
    elif context.options.wait_until == "FINISHED":
      JobMonitor(api.scheduler_proxy, config.job_key()).wait_until(JobMonitor.terminal)
    # Check to make sure the job was created successfully.
    status_response = api.check_status(config.job_key())
    if (status_response.responseCode is not ResponseCode.OK or
        status_response.result.scheduleStatusResult.tasks is None or
        status_response.result.scheduleStatusResult.tasks == []):
      context.print_err("Error occurred while creating job %s" % context.options.jobspec)
      return EXIT_COMMAND_FAILURE
    else:
      context.print_out("Job create succeeded: job url=%s" %
                        get_job_page(api, context.options.jobspec))
    return EXIT_OK
Example #2
0
    def execute(self, context):
        config = context.get_job_config(context.options.jobspec, context.options.config_file)
        if config.raw().has_cron_schedule():
            raise context.CommandError(
                EXIT_COMMAND_FAILURE, 'Cron jobs may only be scheduled with "aurora cron schedule" command'
            )

        api = context.get_api(config.cluster())
        resp = api.create_job(config)
        context.log_response_and_raise(resp, err_code=EXIT_COMMAND_FAILURE, err_msg="Job creation failed due to error:")
        if context.options.open_browser:
            webbrowser.open_new_tab(get_job_page(api, context.options.jobspec))

        wait_until(context.options.wait_until, config.job_key(), api)

        # Check to make sure the job was created successfully.
        status_response = api.check_status(config.job_key())
        if (
            status_response.responseCode is not ResponseCode.OK
            or status_response.result.scheduleStatusResult.tasks is None
            or status_response.result.scheduleStatusResult.tasks == []
        ):
            context.print_err("Error occurred while creating job %s" % context.options.jobspec)
            return EXIT_COMMAND_FAILURE
        else:
            context.print_out("Job create succeeded: job url=%s" % get_job_page(api, context.options.jobspec))
        return EXIT_OK
Example #3
0
  def execute(self, context):
    # Check for negative max_total_failures option - negative is an error.
    # for per-instance failures, negative means "no limit", so it's allowed.
    if context.options.max_total_failures < 0:
      context.print_err("max_total_failures option must be >0, but you specified %s" %
          context.options.max_total_failures)
      return EXIT_INVALID_PARAMETER

    job = context.options.instance_spec.jobkey
    instances = (None if context.options.instance_spec.instance == ALL_INSTANCES else
        context.options.instance_spec.instance)
    if instances is not None and context.options.strict:
      context.verify_instances_option_validity(job, instances)
    api = context.get_api(job.cluster)
    config = (context.get_job_config(job, context.options.config)
        if context.options.config else None)
    updater_config = UpdaterConfig(
        context.options.batch_size,
        context.options.restart_threshold,
        context.options.watch_secs,
        context.options.max_per_instance_failures,
        context.options.max_total_failures)
    resp = api.restart(job, instances, updater_config,
        context.options.healthcheck_interval_seconds, config=config)

    context.log_response_and_raise(resp,
                                   err_msg="Error restarting job %s:" % str(job))
    context.print_out("Job %s restarted successfully" % str(job))
    if context.options.open_browser:
      webbrowser.open_new_tab(get_job_page(api, job))
    return EXIT_OK
Example #4
0
  def execute(self, context):
    job = context.options.instance_spec.jobkey
    instances_arg = context.options.instance_spec.instance
    if instances_arg == ALL_INSTANCES:
      raise context.CommandError(EXIT_INVALID_PARAMETER,
          "The instances list cannot be omitted in a kill command!; "
          "use killall to kill all instances")

    if context.has_count_or_percentage_sla_policy(job):
      context.print_out("WARNING: Killing instances without updating SlaPolicy.")

    if context.options.strict:
      context.get_active_instances_or_raise(job, instances_arg)
    api = context.get_api(job.cluster)
    config = context.get_job_config_optional(job, context.options.config)
    if context.options.no_batching:
      resp = api.kill_job(job, instances_arg, config=config, message=context.options.message)
      context.log_response_and_raise(resp)
      wait_result = self.wait_kill_tasks(context, api.scheduler_proxy, job, instances_arg)
      if wait_result is not EXIT_OK:
        return wait_result
    else:
      self.kill_in_batches(context, job, instances_arg, config)
    if context.options.open_browser:
      webbrowser.open_new_tab(get_job_page(api, job))
    context.print_out("Job kill succeeded")
    return EXIT_OK
Example #5
0
 def execute(self, context):
     job = context.options.instance_spec.jobkey
     instances_arg = context.options.instance_spec.instance
     if instances_arg == ALL_INSTANCES:
         raise context.CommandError(
             EXIT_INVALID_PARAMETER,
             "The instances list cannot be omitted in a kill command!; "
             "use killall to kill all instances")
     if context.options.strict:
         context.get_active_instances_or_raise(job, instances_arg)
     api = context.get_api(job.cluster)
     config = context.get_job_config_optional(job, context.options.config)
     if context.options.no_batching:
         resp = api.kill_job(job, instances_arg, config=config)
         context.log_response_and_raise(resp)
         wait_result = self.wait_kill_tasks(context, api.scheduler_proxy,
                                            job, instances_arg)
         if wait_result is not EXIT_OK:
             return wait_result
     else:
         self.kill_in_batches(context, job, instances_arg, config)
     if context.options.open_browser:
         webbrowser.open_new_tab(get_job_page(api, job))
     context.print_out("Job kill succeeded")
     return EXIT_OK
Example #6
0
    def execute(self, context):
        # Check for negative max_total_failures option - negative is an error.
        # for per-instance failures, negative means "no limit", so it's allowed.
        if context.options.max_total_failures < 0:
            context.print_err(
                "max_total_failures option must be >0, but you specified %s" % context.options.max_total_failures
            )
            return EXIT_INVALID_PARAMETER

        job = context.options.instance_spec.jobkey
        instances = (
            None if context.options.instance_spec.instance == ALL_INSTANCES else context.options.instance_spec.instance
        )
        if instances is not None and context.options.strict:
            context.get_active_instances_or_raise(job, instances)
        api = context.get_api(job.cluster)
        config = context.get_job_config_optional(job, context.options.config)
        restart_settings = RestartSettings(
            batch_size=context.options.batch_size,
            watch_secs=context.options.watch_secs,
            max_per_instance_failures=context.options.max_per_instance_failures,
            max_total_failures=context.options.max_total_failures,
            health_check_interval_seconds=context.options.healthcheck_interval_seconds,
        )
        resp = api.restart(job, instances, restart_settings, config=config)

        context.log_response_and_raise(resp, err_msg="Error restarting job %s:" % str(job))
        context.print_out("Job %s restarted successfully" % str(job))
        if context.options.open_browser:
            webbrowser.open_new_tab(get_job_page(api, job))
        return EXIT_OK
Example #7
0
 def execute(self, context):
   api = context.get_api(context.options.jobspec.cluster)
   config = (context.get_job_config(context.options.jobspec, context.options.config)
       if context.options.config else None)
   resp = api.start_cronjob(context.options.jobspec, config=config)
   context.log_response_and_raise(resp,
       err_msg=("Error starting cron job %s:" % context.options.jobspec))
   if context.options.open_browser:
     webbrowser.open_new_tab(get_job_page(api, context.options.jobspec))
   return EXIT_OK
Example #8
0
 def execute(self, context):
     api = context.get_api(context.options.jobspec.cluster)
     config = (context.get_job_config(context.options.jobspec,
                                      context.options.config)
               if context.options.config else None)
     resp = api.start_cronjob(context.options.jobspec, config=config)
     context.log_response_and_raise(resp,
                                    err_msg=("Error starting cron job %s:" %
                                             context.options.jobspec))
     if context.options.open_browser:
         webbrowser.open_new_tab(get_job_page(api, context.options.jobspec))
     return EXIT_OK
Example #9
0
 def execute(self, context):
   job = context.options.jobspec
   api = context.get_api(job.cluster)
   if context.options.no_batching:
     resp = api.kill_job(job, None)
     context.log_response_and_raise(resp)
     wait_result = self.wait_kill_tasks(context, api.scheduler_proxy, job)
     if wait_result is not EXIT_OK:
       return wait_result
   else:
     self.kill_in_batches(context, job, None)
   if context.options.open_browser:
     webbrowser.open_new_tab(get_job_page(api, job))
   context.print_out("Job killall succeeded")
   return EXIT_OK
Example #10
0
  def execute(self, context):
    api = context.get_api(context.options.jobspec.cluster)
    config = context.get_job_config(context.options.jobspec, context.options.config_file)
    if not config.raw().has_cron_schedule():
      raise context.CommandError(
          EXIT_COMMAND_FAILURE,
          "Non-cron jobs may only be created with \"aurora job create\" command")

    resp = api.schedule_cron(config)
    context.log_response_and_raise(resp,
        err_msg=("Error scheduling cron job %s:" % context.options.jobspec))

    context.print_out("Cron job scheduled, status can be viewed at %s"
        % get_job_page(api, context.options.jobspec))

    return EXIT_OK
Example #11
0
    def execute(self, context):
        job = context.options.task_instance.jobkey
        instance = context.options.task_instance.instance
        count = context.options.instance_count

        active = context.get_active_instances_or_raise(job, [instance])
        start = max(list(active)) + 1

        api = context.get_api(job.cluster)
        resp = api.add_instances(job, instance, count)
        context.log_response_and_raise(resp)

        wait_until(context.options.wait_until, job, api, range(start, start + count))

        if context.options.open_browser:
            webbrowser.open_new_tab(get_job_page(api, job))

        return EXIT_OK
Example #12
0
  def execute(self, context):
    job = context.options.task_instance.jobkey
    instance = context.options.task_instance.instance
    count = context.options.instance_count

    active = context.get_active_instances_or_raise(job, [instance])
    start = max(list(active)) + 1

    api = context.get_api(job.cluster)
    resp = api.add_instances(job, instance, count)
    context.log_response_and_raise(resp)

    wait_until(context.options.wait_until, job, api, range(start, start + count))

    if context.options.open_browser:
      webbrowser.open_new_tab(get_job_page(api, job))

    return EXIT_OK
Example #13
0
    def execute(self, context):
        api = context.get_api(context.options.jobspec.cluster)
        config = context.get_job_config(context.options.jobspec,
                                        context.options.config_file)
        if not config.raw().has_cron_schedule():
            raise context.CommandError(
                EXIT_COMMAND_FAILURE,
                "Non-cron jobs may only be created with \"aurora job create\" command"
            )

        resp = api.schedule_cron(config)
        context.log_response_and_raise(
            resp,
            err_msg=("Error scheduling cron job %s:" %
                     context.options.jobspec))

        context.print_out("Cron job scheduled, status can be viewed at %s" %
                          get_job_page(api, context.options.jobspec))

        return EXIT_OK
Example #14
0
  def test_successful_schedule(self):
    mock_context = FakeAuroraCommandContext()
    with contextlib.nested(
        patch('apache.aurora.client.cli.cron.CronNoun.create_context', return_value=mock_context)):

      api = mock_context.get_api('west')
      api.schedule_cron.return_value = self.create_simple_success_response()
      with temporary_file() as fp:
        fp.write(self.get_valid_cron_config())
        fp.flush()
        cmd = AuroraCommandLine()
        cmd.execute(['cron', 'schedule', self.TEST_JOBSPEC, fp.name])

      # Now check that the right API calls got made.
      # Check that create_job was called exactly once, with an AuroraConfig parameter.
      assert api.schedule_cron.call_count == 1
      assert isinstance(api.schedule_cron.call_args[0][0], AuroraConfig)

      # The last text printed out to the user should contain a url to the job
      assert get_job_page(api, self.TEST_JOBKEY) in mock_context.out[-1]
Example #15
0
  def execute(self, context):
    job = context.options.task_instance.jobkey
    instance = context.options.task_instance.instance
    count = context.options.instance_count

    active = context.get_active_instances_or_raise(job, [instance])
    start = max(list(active)) + 1

    if context.has_count_or_percentage_sla_policy(job):
      context.print_out("WARNING: Adding instances without updating SlaPolicy.")

    api = context.get_api(job.cluster)
    resp = api.add_instances(job, instance, count)
    context.log_response_and_raise(resp)

    wait_until(context.options.wait_until, job, api, range(start, start + count))

    if context.options.open_browser:
      webbrowser.open_new_tab(get_job_page(api, job))

    return EXIT_OK
Example #16
0
    def execute(self, context):
        # Check for negative max_total_failures option - negative is an error.
        # for per-instance failures, negative means "no limit", so it's allowed.
        if context.options.max_total_failures < 0:
            context.print_err(
                "max_total_failures option must be >0, but you specified %s" %
                context.options.max_total_failures)
            return EXIT_INVALID_PARAMETER

        if context.options.restart_threshold:
            context.print_out(
                "WARNING: '--restart-threshold' option is no longer supported and will be "
                "removed in the next release.")

        job = context.options.instance_spec.jobkey
        instances = (None
                     if context.options.instance_spec.instance == ALL_INSTANCES
                     else context.options.instance_spec.instance)
        if instances is not None and context.options.strict:
            context.get_active_instances_or_raise(job, instances)
        api = context.get_api(job.cluster)
        config = context.get_job_config_optional(job, context.options.config)
        restart_settings = RestartSettings(
            batch_size=context.options.batch_size,
            watch_secs=context.options.watch_secs,
            max_per_instance_failures=context.options.
            max_per_instance_failures,
            max_total_failures=context.options.max_total_failures,
            health_check_interval_seconds=context.options.
            healthcheck_interval_seconds)
        resp = api.restart(job, instances, restart_settings, config=config)

        context.log_response_and_raise(resp,
                                       err_msg="Error restarting job %s:" %
                                       str(job))
        context.print_out("Job %s restarted successfully" % str(job))
        if context.options.open_browser:
            webbrowser.open_new_tab(get_job_page(api, job))
        return EXIT_OK
Example #17
0
 def execute(self, context):
   job = context.options.instance_spec.jobkey
   instances_arg = context.options.instance_spec.instance
   if instances_arg == ALL_INSTANCES:
     raise context.CommandError(EXIT_INVALID_PARAMETER,
         "The instances list cannot be omitted in a kill command!; "
         "use killall to kill all instances")
   if context.options.strict:
     context.verify_instances_option_validity(job, instances_arg)
   api = context.get_api(job.cluster)
   if context.options.no_batching:
     resp = api.kill_job(job, instances_arg)
     context.log_response_and_raise(resp)
     wait_result = self.wait_kill_tasks(context, api.scheduler_proxy, job, instances_arg)
     if wait_result is not EXIT_OK:
       return wait_result
   else:
     self.kill_in_batches(context, job, instances_arg)
   if context.options.open_browser:
     webbrowser.open_new_tab(get_job_page(api, job))
   context.print_out("Job kill succeeded")
   return EXIT_OK
Example #18
0
    def test_successful_schedule(self):
        mock_context = FakeAuroraCommandContext()
        with contextlib.nested(
                patch('apache.aurora.client.cli.cron.CronNoun.create_context',
                      return_value=mock_context)):

            api = mock_context.get_api('west')
            api.schedule_cron.return_value = self.create_simple_success_response(
            )
            with temporary_file() as fp:
                fp.write(self.get_valid_cron_config())
                fp.flush()
                cmd = AuroraCommandLine()
                cmd.execute(['cron', 'schedule', self.TEST_JOBSPEC, fp.name])

            # Now check that the right API calls got made.
            # Check that create_job was called exactly once, with an AuroraConfig parameter.
            assert api.schedule_cron.call_count == 1
            assert isinstance(api.schedule_cron.call_args[0][0], AuroraConfig)

            # The last text printed out to the user should contain a url to the job
            assert get_job_page(api, self.TEST_JOBKEY) in mock_context.out[-1]