Esempio n. 1
0
    def test_get_tron_namespaces(self, mock_ls, mock_walk):
        cluster_name = "stage"
        expected_namespaces = ["app", "foo"]
        mock_walk.return_value = [
            ("/my_soa_dir/foo", [], ["tron-stage.yaml"]),
            ("/my_soa_dir/app", [], ["tron-stage.yaml"]),
            ("my_soa_dir/woo", [], ["something-else.yaml"]),
        ]
        soa_dir = "/my_soa_dir"

        namespaces = tron_tools.get_tron_namespaces(cluster=cluster_name,
                                                    soa_dir=soa_dir)
        assert sorted(expected_namespaces) == sorted(namespaces)
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)
Esempio n. 3
0
def main():
    args = parse_args()
    namespaces = tron_tools.get_tron_namespaces(cluster=args.cluster,
                                                soa_dir=args.soa_dir)
    print("\n".join(namespaces))
Esempio n. 4
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)