Example #1
0
 def test_get_tron_client(self, mock_client, mock_system_tron_config):
     result = tron_tools.get_tron_client()
     assert mock_system_tron_config.return_value.get_url.call_count == 1
     mock_client.assert_called_once_with(
         mock_system_tron_config.return_value.get_url.return_value
     )
     assert result == mock_client.return_value
def main():
    args = parse_args()
    log_level = logging.DEBUG if args.verbose else logging.INFO
    logging.basicConfig(level=log_level)

    if args.all_namespaces:
        if args.services:
            log.error('Do not pass service names with --all flag')
            sys.exit(1)

        try:
            services = tron_tools.get_tron_namespaces_for_cluster()
        except Exception as e:
            log.error('Failed to list tron namespaces: {error}'.format(
                error=str(e), ))
            sys.exit(1)
    else:
        services = args.services

    if not services:
        log.warning("No namespaces found")
        sys.exit(0)

    client = tron_tools.get_tron_client()

    updated = []
    failed = []
    skipped = []

    for service in services:
        try:
            new_config = tron_tools.create_complete_config(
                service=service,
                soa_dir=args.soa_dir,
            )
            if client.update_namespace(service, new_config):
                updated.append(service)
                log.debug(f'Updated {service}')
            else:
                skipped.append(service)
                log.debug(f'Skipped {service}')
        except Exception as e:
            log.error(f'Update for {service} failed: {str(e)}')
            log.debug(f'Exception while updating {service}', exc_info=1)
            failed.append(service)

    skipped_report = skipped if args.verbose else len(skipped)
    log.info(
        f'Updated following namespaces: {updated}, '
        f'failed: {failed}, skipped: {skipped_report}', )

    sys.exit(1 if failed else 0)
def tron_instance_status(
    instance_status: Mapping[str, Any], service: str, instance: str, verbose: int
) -> Mapping[str, Any]:
    status: Dict[str, Any] = {}
    client = tron_tools.get_tron_client()
    short_job, action = instance.split(".")
    job = f"{service}.{short_job}"
    job_content = client.get_job_content(job=job)

    try:
        latest_run_id = client.get_latest_job_run_id(job_content=job_content)
        if latest_run_id is None:
            action_run = {"state": "Hasn't run yet (no job run id found)"}
        else:
            action_run = client.get_action_run(
                job=job, action=action, run_id=latest_run_id
            )
    except Exception as e:
        action_run = {"state": f"Failed to get latest run info: {e}"}

    # job info
    status["job_name"] = short_job
    status["job_status"] = job_content["status"]
    status["job_schedule"] = "{} {}".format(
        job_content["scheduler"]["type"], job_content["scheduler"]["value"]
    )
    status["job_url"] = (
        tron_tools.get_tron_dashboard_for_cluster(settings.cluster) + f"#job/{job}"
    )

    if action:
        status["action_name"] = action
    if action_run.get("state"):
        status["action_state"] = action_run["state"]
    if action_run.get("start_time"):
        status["action_start_time"] = action_run["start_time"]
    if action_run.get("raw_command"):
        status["action_raw_command"] = action_run["raw_command"]
    if action_run.get("stdout"):
        status["action_stdout"] = "\n".join(action_run["stdout"])
    if action_run.get("stderr"):
        status["action_stderr"] = "\n".join(action_run["stderr"])
    if action_run.get("command"):
        status["action_command"] = action_run["command"]

    return status
Example #4
0
def tron_instance_status(
    instance_status: Mapping[str, Any],
    service: str,
    instance: str,
    verbose: bool,
) -> Mapping[str, Any]:
    status: Dict[str, Any] = {}
    client = tron_tools.get_tron_client()
    short_job, action = instance.split('.')
    job = f"{service}.{short_job}"

    job_content = client.get_job_content(job=job)
    latest_run_id = client.get_latest_job_run_id(job_content=job_content)
    action_run = client.get_action_run(job=job,
                                       action=action,
                                       run_id=latest_run_id)

    # job info
    status['job_name'] = short_job
    status['job_status'] = job_content['status']
    status['job_schedule'] = '{} {}'.format(job_content['scheduler']['type'],
                                            job_content['scheduler']['value'])
    status['job_url'] = tron_tools.get_tron_dashboard_for_cluster(
        settings.cluster) + f'#job/{job}'

    if action:
        status['action_name'] = action
    if action_run['state']:
        status['action_state'] = action_run['state']
    if action_run['start_time']:
        status['action_start_time'] = action_run['start_time']
    if action_run['raw_command']:
        status['action_raw_command'] = action_run['raw_command']
    if action_run['stdout']:
        status['action_stdout'] = '\n'.join(action_run['stdout'])
    if action_run['stderr']:
        status['action_stderr'] = '\n'.join(action_run['stderr'])
    if action_run['command']:
        status['action_command'] = action_run['command']

    return status
def main():
    args = parse_args()

    cluster = tron_tools.load_tron_config().get_cluster_name()
    client = tron_tools.get_tron_client()
    namespaces = client.list_namespaces()
    expected_namespaces = tron_tools.get_tron_namespaces(cluster=cluster,
                                                         soa_dir=args.soa_dir)
    to_delete = set(namespaces) - set(expected_namespaces) - {"MASTER"}

    if not to_delete:
        paasta_print("No Tron namespaces to remove")
        sys.exit(0)

    if args.dry_run:
        paasta_print("Dry run, would have removed namespaces:\n  " +
                     "\n  ".join(to_delete))
        sys.exit(0)

    successes = []
    errors = []
    for namespace in to_delete:
        try:
            client.update_namespace(namespace, "")
            successes.append(namespace)
        except Exception as e:
            errors.append((namespace, e))

    if successes:
        paasta_print("Successfully removed namespaces:\n",
                     "\n  ".join(successes))

    if errors:
        paasta_print("Failed to remove namespaces:\n  " + "\n  ".join([
            "{namespace}: {error}".format(namespace=namespace,
                                          error=str(error))
            for namespace, error in errors
        ]))
        sys.exit(1)
Example #6
0
def main():
    args = parse_args()

    client = tron_tools.get_tron_client()
    namespaces = client.list_namespaces()
    expected_namespaces = tron_tools.get_tron_namespaces_for_cluster(
        soa_dir=args.soa_dir)
    to_delete = set(namespaces) - set(expected_namespaces)

    if not to_delete:
        paasta_print('No Tron namespaces to remove')
        sys.exit(0)

    if args.dry_run:
        paasta_print('Dry run, would have removed namespaces:\n  ' +
                     '\n  '.join(to_delete))
        sys.exit(0)

    successes = []
    errors = []
    for namespace in to_delete:
        try:
            client.update_namespace(namespace, '')
            successes.append(namespace)
        except Exception as e:
            errors.append((namespace, e))

    if successes:
        paasta_print('Successfully removed namespaces:\n',
                     '\n  '.join(successes))

    if errors:
        paasta_print(
            'Failed to remove namespaces:\n  ' + '\n  '.join([
                '{namespace}: {error}'.format(namespace=namespace,
                                              error=str(error))
                for namespace, error in errors
            ], ), )
        sys.exit(1)
Example #7
0
def tron_instance_status(instance_status: Mapping[str, Any], service: str,
                         instance: str, verbose: int) -> Mapping[str, Any]:
    status: Dict[str, Any] = {}
    client = tron_tools.get_tron_client()
    short_job, action = instance.split(".")
    job = f"{service}.{short_job}"

    job_content = client.get_job_content(job=job)
    latest_run_id = client.get_latest_job_run_id(job_content=job_content)
    action_run = client.get_action_run(job=job,
                                       action=action,
                                       run_id=latest_run_id)

    # job info
    status["job_name"] = short_job
    status["job_status"] = job_content["status"]
    status["job_schedule"] = "{} {}".format(job_content["scheduler"]["type"],
                                            job_content["scheduler"]["value"])
    status["job_url"] = (
        tron_tools.get_tron_dashboard_for_cluster(settings.cluster) +
        f"#job/{job}")

    if action:
        status["action_name"] = action
    if action_run["state"]:
        status["action_state"] = action_run["state"]
    if action_run["start_time"]:
        status["action_start_time"] = action_run["start_time"]
    if action_run["raw_command"]:
        status["action_raw_command"] = action_run["raw_command"]
    if action_run["stdout"]:
        status["action_stdout"] = "\n".join(action_run["stdout"])
    if action_run["stderr"]:
        status["action_stderr"] = "\n".join(action_run["stderr"])
    if action_run["command"]:
        status["action_command"] = action_run["command"]

    return status
Example #8
0
def main():
    args = parse_args()
    log_level = logging.DEBUG if args.verbose else logging.INFO
    logging.basicConfig(level=log_level)

    if not args.cluster:
        args.cluster = tron_tools.load_tron_config().get_cluster_name()

    if args.all_namespaces:
        if args.services:
            log.error("Do not pass service names with --all flag")
            sys.exit(1)

        try:
            services = tron_tools.get_tron_namespaces(cluster=args.cluster,
                                                      soa_dir=args.soa_dir)
        except Exception as e:
            log.error(
                "Failed to list tron namespaces: {error}".format(error=str(e)))
            sys.exit(1)
    else:
        services = args.services

    if not services:
        log.warning("No namespaces found")
        sys.exit(0)

    if not args.dry_run:
        client = tron_tools.get_tron_client()

    updated = []
    failed = []
    skipped = []

    master_config = tron_tools.create_complete_master_config(
        cluster=args.cluster, soa_dir=args.soa_dir)
    if args.dry_run:
        log.info(f"Would update {MASTER_NAMESPACE} to:")
        log.info(f"{master_config}")
        updated.append(MASTER_NAMESPACE)
    else:
        if client.update_namespace(MASTER_NAMESPACE, master_config):
            updated.append(MASTER_NAMESPACE)
            log.debug(f"Updated {MASTER_NAMESPACE}")
        else:
            skipped.append(MASTER_NAMESPACE)
            log.debug(f"Skipped {MASTER_NAMESPACE}")

    for service in sorted(services):
        try:
            new_config = tron_tools.create_complete_config(
                cluster=args.cluster, service=service, soa_dir=args.soa_dir)
            if args.dry_run:
                log.info(f"Would update {service} to:")
                log.info(f"{new_config}")
                updated.append(service)
            else:
                if client.update_namespace(service, new_config):
                    updated.append(service)
                    log.debug(f"Updated {service}")
                else:
                    skipped.append(service)
                    log.debug(f"Skipped {service}")
        except Exception as e:
            log.error(f"Update for {service} failed: {str(e)}")
            log.debug(f"Exception while updating {service}", exc_info=1)
            failed.append(service)

    skipped_report = skipped if args.verbose else len(skipped)
    log.info(f"Updated following namespaces: {updated}, "
             f"failed: {failed}, skipped: {skipped_report}")

    sys.exit(1 if failed else 0)