Beispiel #1
0
def teardown(args):
    """Teardown the resources."""
    gke = discovery.build("container", "v1")

    project = args.project
    cluster_name = args.cluster
    zone = args.zone
    util.delete_cluster(gke, cluster_name, project, zone)
    def run_test(project, zone, cluster, new_values):  # pylint: disable=too-many-locals
        # TODO([email protected]): Need to configure the notebook and test to build
        # using GCB.
        dirname = os.path.dirname(__file__)
        if not dirname:
            logging.info("__file__ doesn't apper to be absolute path.")
            dirname = os.getcwd()
        notebook_path = os.path.join(dirname, "TF on GKE.ipynb")
        logging.info("Reading notebook %s", notebook_path)
        if not os.path.exists(notebook_path):
            raise ValueError("%s does not exist" % notebook_path)

        with open(notebook_path) as hf:
            node = nbformat.read(hf, nbformat.NO_CONVERT)
        exporter = nbconvert.PythonExporter()
        raw, _ = nbconvert.export(exporter, node)

        credentials = GoogleCredentials.get_application_default()
        gke = discovery.build("container", "v1", credentials=credentials)

        lines = raw.splitlines()

        modified = replace_vars(lines, new_values)

        modified = strip_appendix(modified)

        modified = strip_unexecutable(modified)

        with tempfile.NamedTemporaryFile(suffix="notebook.py",
                                         prefix="tmpGke",
                                         mode="w",
                                         delete=False) as hf:
            code_path = hf.name
            hf.write("\n".join(modified))
        logging.info("Wrote notebook to: %s", code_path)

        with open(code_path) as hf:
            contents = hf.read()
            logging.info("Notebook contents:\n%s", contents)

        try:
            runpy.run_path(code_path)
        finally:
            logging.info("Deleting cluster; project=%s, zone=%s, name=%s",
                         project, zone, cluster)
            util.delete_cluster(gke, cluster, project, zone)