תזמון הרצה של Notebook

בדף הזה מוסבר איך לתזמן הפעלה של מחברת ב-Colab Enterprise.

סקירה כללית

אתם יכולים לתזמן מחברת כך שתופעל מיד פעם אחת, או לפי לו"ז חוזר.

כשמתזמנים את ההרצה של המחברת, בוחרים תבנית של זמן ריצה. ‫Colab Enterprise משתמש בתבנית זמן הריצה הזו כדי ליצור את זמן הריצה שבו מופעל ה-notebook.

לסביבת זמן הריצה נדרשות הרשאות ספציפיות כדי להריץ את הקוד של המחברת ולגשת לשירותים ולממשקי API. Google Cloud

  • אם הפעלתם את פרטי הכניסה של משתמשי הקצה בהגדרת תבנית זמן הריצה, זמן הריצה ישתמש בהרשאות שמשויכות לפרטי הכניסה שלכם כמשתמש.

  • אם פרטי הכניסה של משתמש הקצה לא מופעלים, צריך לציין חשבון שירות כשמתזמנים את הפעלת המחברת. ‫Colab Enterprise משתמש בפרטי הכניסה של חשבון השירות הזה כדי להריץ את המחברת.

מידע נוסף זמין במאמר בנושא התפקידים הנדרשים להפעלת מחברת.

אחרי ש-Colab Enterprise מסיים את ההרצה של קובץ ה-notebook, התוצאות מאוחסנות בקטגוריה של Cloud Storage שאפשר לשתף.

מגבלות

זמני הריצה של Colab Enterprise משתמשים במכסה של Compute Engine. מהן מכסות ההקצאה של Compute Engine?

לפני שמתחילים

  1. נכנסים לחשבון Google Cloud . אם אתם משתמשים חדשים ב- Google Cloud, צרו חשבון כדי שתוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Agent Platform, Dataform, and Compute Engine APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Agent Platform, Dataform, and Compute Engine APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

התפקידים שנדרשים כדי לתזמן את ההרצה של ה-notebook

כדי לקבל את ההרשאות שדרושות לתזמון הרצה של מחברת ב-Colab Enterprise, צריך לבקש מהאדמין להקצות לכם את תפקידי ה-IAM הבאים בפרויקט:

להסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.

יכול להיות שאפשר לקבל את ההרשאות הנדרשות גם באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש.

התפקידים שנדרשים להפעלת ה-notebook

למשתמש שמריץ את ה-notebook צריכות להיות הרשאות ספציפיות. הגורם המבצע הוא חשבון המשתמש שלכם או חשבון שירות שאתם מציינים, כפי שמתואר בסקירה הכללית.

כדי לקבל את ההרשאות שדרושות להרצת מחברת ב-Colab Enterprise, צריך לבקש מהאדמין להקצות לכם את תפקידי ה-IAM הבאים:

להסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.

התפקידים המוגדרים מראש האלה כוללים את ההרשאות שנדרשות להרצת מחברת ב-Colab Enterprise. כדי לראות בדיוק אילו הרשאות נדרשות, אפשר להרחיב את הקטע ההרשאות הנדרשות:

ההרשאות הנדרשות

כדי להריץ מחברת ב-Colab Enterprise, צריך את ההרשאות הבאות:

  • dataform.locations.list ב-Notebook
  • dataform.repositories.computeAccessTokenStatus ב-Notebook
  • dataform.repositories.fetchHistory ב-Notebook
  • dataform.repositories.fetchRemoteBranches ב-Notebook
  • dataform.repositories.get ב-Notebook
  • dataform.repositories.getIamPolicy ב-Notebook
  • dataform.repositories.list ב-Notebook
  • dataform.repositories.queryDirectoryContents ב-Notebook
  • dataform.repositories.readFile ב-Notebook
  • logging.logEntries.create בפרויקט
  • logging.logEntries.route בפרויקט
  • monitoring.metricDescriptors.create בפרויקט
  • monitoring.metricDescriptors.get בפרויקט
  • monitoring.metricDescriptors.list בפרויקט
  • monitoring.monitoredResourceDescriptors.get בפרויקט
  • monitoring.monitoredResourceDescriptors.list בפרויקט
  • monitoring.timeSeries.create בפרויקט
  • resourcemanager.projects.get בפרויקט
  • resourcemanager.projects.list בפרויקט
  • storage.buckets.get ב-Notebook
  • storage.managedFolders.create ב-Notebook
  • storage.managedFolders.delete ב-Notebook
  • storage.managedFolders.get ב-Notebook
  • storage.managedFolders.list ב-Notebook
  • storage.multipartUploads.abort ב-Notebook
  • storage.multipartUploads.create ב-Notebook
  • storage.multipartUploads.list ב-Notebook
  • storage.multipartUploads.listParts ב-Notebook
  • storage.objects.create ב-Notebook
  • storage.objects.delete ב-Notebook
  • storage.objects.get ב-Notebook
  • storage.objects.list ב-Notebook
  • storage.objects.restore ב-Notebook
  • storage.objects.setRetention ב-Notebook

יכול להיות שתקבלו את ההרשאות האלה באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש אחרים.

שימוש בהרצות מתוזמנות של מחברות ברשת VPC משותפת

כדי להשתמש בהפעלות מתוזמנות של מחברות ברשת VPC משותפת, צריך להעניק הרשאות נוספות. איך משתמשים ב-Colab Enterprise ברשת VPC משותפת

הפעלה של מחברת פעם אחת

כדי להריץ מחברת פעם אחת, אפשר להשתמש במסוף Google Cloud , ב-Google Cloud CLI, בספריית הלקוח של Python של Agent Platform או ב-Terraform.

המסוף

  1. נכנסים לדף My notebooks של Colab Enterprise במסוף Google Cloud .

    לתיקיות שלי

  2. בתפריט Region (אזור), בוחרים את האזור שבו נמצא ה-Notebook.

  3. לצד מחברת, לוחצים על התפריט פעולות במחברת ובוחרים באפשרות תזמון.

  4. בשדה שם לוח הזמנים, מזינים שם ללוח הזמנים.

  5. לוחצים על הרשימה Runtime template (תבנית זמן ריצה) ובוחרים תבנית זמן ריצה. תבנית זמן הריצה קובעת את המפרטים של זמן הריצה שמפעיל את המחברת.

  6. בקטע Run schedule (תזמון הרצה), בוחרים באפשרות One-off (חד-פעמי) כדי להריץ את ה-notebook מיד אחרי ששולחים את ההרצה.

  7. לצד השדה Cloud Storage output location (מיקום הפלט ב-Cloud Storage), לוחצים על Browse (עיון) כדי לפתוח את תיבת הדו-שיח Select folder (בחירת תיקייה).

  8. בוחרים קטגוריה של Cloud Storage. אפשר גם ללחוץ על  Create new bucket (יצירת מאגר חדש) ולהשלים את תיבת הדו-שיח כדי ליצור מאגר.

  9. אם בחרתם בתבנית של זמן ריצה בלי להפעיל את פרטי הכניסה של משתמש הקצה, בתיבת הדו-שיח יופיע השדה חשבון שירות. בשדה Service account, מזינים את כתובת האימייל של חשבון השירות.

  10. לוחצים על שליחה.

    הפעלת ה-notebook מתחילה באופן מיידי.

gcloud

לפני השימוש בנתוני הפקודה הבאים, צריך להחליף את הנתונים הבאים:

  • DISPLAY_NAME: השם המוצג של הפעלת המחברת.
  • NOTEBOOK_RUNTIME_TEMPLATE: תבנית זמן הריצה של המחברת שמציינת את הגדרות המחשוב של זמן הריצה.
  • NOTEBOOK_URI: ה-URI של Cloud Storage של המחברת להפעלה.
  • OUTPUT_URI: המיקום ב-Cloud Storage שבו רוצים לאחסן את התוצאות.
  • USER_EMAIL: כתובת האימייל בחשבון המשתמש שמציינת את הגישה של הפעלת המחברת למשאבי Google Cloud .
  • PROJECT_ID: מזהה הפרויקט.
  • REGION: האזור שבו המחברת תפעל.

מריצים את הפקודה הבאה:

‫Linux,‏ macOS או Cloud Shell

gcloud colab executions create --display-name="DISPLAY_NAME" \
    --notebook-runtime-template=NOTEBOOK_RUNTIME_TEMPLATE \
    --gcs-notebook-uri=NOTEBOOK_URI \
    --gcs-output-uri=OUTPUT_URI \
    --user-email=USER_EMAIL \
    --project=PROJECT_ID \
    --region=REGION

‏Windows (PowerShell)

gcloud colab executions create --display-name="DISPLAY_NAME" `
    --notebook-runtime-template=NOTEBOOK_RUNTIME_TEMPLATE `
    --gcs-notebook-uri=NOTEBOOK_URI `
    --gcs-output-uri=OUTPUT_URI `
    --user-email=USER_EMAIL `
    --project=PROJECT_ID `
    --region=REGION

Windows‏ (cmd.exe)

gcloud colab executions create --display-name="DISPLAY_NAME" ^
    --notebook-runtime-template=NOTEBOOK_RUNTIME_TEMPLATE ^
    --gcs-notebook-uri=NOTEBOOK_URI ^
    --gcs-output-uri=OUTPUT_URI ^
    --user-email=USER_EMAIL ^
    --project=PROJECT_ID ^
    --region=REGION

מידע נוסף על ניהול הפעלות של מחברות Colab Enterprise משורת הפקודה זמין במסמכי התיעוד של ה-CLI של gcloud.

Python

לפני שמנסים את הדוגמה הזו, צריך להתקין את Agent Platform SDK ל-Python. ספריית הלקוח של Python ל-Agent Platform מותקנת כשמתקינים את Agent Platform SDK ל-Python. מידע נוסף מופיע במאמרי העזרה של ה-API.

כדי להריץ את דוגמת הקוד הבאה, צריך את מזהה מאגר Dataform של המחברת. כדי לקבל את מזהה המאגר של המחברת, אפשר להשתמש ב-method ‏list_repositories של Dataform.

from google.cloud import aiplatform_v1

PROJECT_ID = "my-project"
LOCATION = "us-central1"
REPOSITORY_ID = "b223577f-a3fb-482c-a22c-0658c6602598"
TEMPLATE_ID = "6524523989455339520"

API_ENDPOINT = f"{LOCATION}-aiplatform.googleapis.com"
PARENT = f"projects/{PROJECT_ID}/locations/{LOCATION}"

notebook_service_client = aiplatform_v1.NotebookServiceClient(client_options = {
    "api_endpoint": API_ENDPOINT,
})

operation = notebook_service_client.create_notebook_execution_job(parent=PARENT, notebook_execution_job={
    "display_name": "my-execution-job",

    # Specify a NotebookRuntimeTemplate to source compute configuration from
    "notebook_runtime_template_resource_name": f"projects/{PROJECT_ID}/locations/{LOCATION}/notebookRuntimeTemplates/{TEMPLATE_ID}",

    # Specify a Colab Enterprise notebook to run
    "dataform_repository_source": {
        "dataform_repository_resource_name": f"projects/{PROJECT_ID}/locations/{LOCATION}/repositories/{REPOSITORY_ID}",
    },

    # Specify a Cloud Storage bucket to store output artifacts
    "gcs_output_uri": "gs://my-bucket/",

    # Specify the identity that runs the notebook
    "execution_user": "{EMAIL}",

    # Run as the service account instead
    # "service_account": "my-service-account",
})
print("Waiting for operation to complete...")
result = operation.result()

Terraform

כדי ללמוד איך להחיל הגדרות ב-Terraform או להסיר אותן, ראו פקודות בסיסיות ב-Terraform. למידע נוסף, ראו את מאמרי העזרה לספקים של Terraform.

בדוגמה הבאה נעשה שימוש במשאב Terraform‏ google_colab_notebook_execution כדי להריץ notebook של Colab Enterprise.

resource "google_colab_runtime_template" "my_runtime_template" {
  provider = google-beta
  name = "{{index $.Vars "runtime_template_name"}}"
  display_name = "Runtime template"
  location = "us-central1"

  machine_spec {
    machine_type     = "e2-standard-4"
  }

  network_spec {
    enable_internet_access = true
  }
}

resource "google_storage_bucket" "output_bucket" {
  provider = google-beta
  name          = "{{index $.Vars "bucket"}}"
  location      = "US"
  force_destroy = true
  uniform_bucket_level_access = true
}

resource "google_storage_bucket_object" "notebook" {
  provider = google-beta
  name   = "hello_world.ipynb"
  bucket = google_storage_bucket.output_bucket.name
  content = <<EOF
    {
      "cells": [
        {
          "cell_type": "code",
          "execution_count": null,
          "metadata": {},
          "outputs": [],
          "source": [
            "print(\"Hello, World!\")"
          ]
        }
      ],
      "metadata": {
        "kernelspec": {
          "display_name": "Python 3",
          "language": "python",
          "name": "python3"
        },
        "language_info": {
          "codemirror_mode": {
            "name": "ipython",
            "version": 3
          },
          "file_extension": ".py",
          "mimetype": "text/x-python",
          "name": "python",
          "nbconvert_exporter": "python",
          "pygments_lexer": "ipython3",
          "version": "3.8.5"
        }
      },
      "nbformat": 4,
      "nbformat_minor": 4
    }
    EOF
}

resource "google_colab_notebook_execution" "{{$.PrimaryResourceId}}" {
  provider = google-beta
  notebook_execution_job_id = "{{index $.Vars "notebook_execution_job_id"}}"
  display_name = "Notebook execution full"
  location = "us-central1"

  execution_timeout = "86400s"
  gcs_notebook_source {
  uri = "gs://${google_storage_bucket_object.notebook.bucket}/${google_storage_bucket_object.notebook.name}"
  generation = google_storage_bucket_object.notebook.generation
  }

  service_account = "{{index $.TestEnvVars "service_account"}}"

  gcs_output_uri = "gs://${google_storage_bucket.output_bucket.name}"
  notebook_runtime_template_resource_name = "projects/${google_colab_runtime_template.my_runtime_template.project}/locations/${google_colab_runtime_template.my_runtime_template.location}/notebookRuntimeTemplates/${google_colab_runtime_template.my_runtime_template.name}"

  depends_on = [
    google_storage_bucket_object.notebook,
    google_storage_bucket.output_bucket,
    google_colab_runtime_template.my_runtime_template,
  ]

}

אפשר לראות את התוצאות של הפעלות מחברות שהושלמו בדף ההפעלות.

תזמון הרצה של Notebook

כדי לתזמן הפעלה של מחברת, אפשר להשתמש במסוף Google Cloud , ב-CLI של gcloud, בספריית הלקוח של Python של Agent Platform או ב-Terraform.

המסוף

  1. נכנסים לדף My notebooks של Colab Enterprise במסוף Google Cloud .

    לתיקיות שלי

  2. בתפריט Region (אזור), בוחרים את האזור שבו נמצא ה-Notebook.

  3. לצד מחברת, לוחצים על התפריט פעולות במחברת ובוחרים באפשרות תזמון.

  4. בשדה שם לוח הזמנים, מזינים שם ללוח הזמנים.

  5. לוחצים על הרשימה Runtime template (תבנית זמן ריצה) ובוחרים תבנית זמן ריצה. תבנית זמן הריצה קובעת את המפרטים של זמן הריצה שמפעיל את המחברת.

  6. בקטע Run schedule (תזמון הפעלה), בוחרים באפשרות Recurring (חוזרת) כדי לתזמן את הפעלת ה-notebook במרווח זמן ספציפי.

  7. משלימים את תיבת הדו-שיח של קביעת הפגישה.

  8. לצד השדה Cloud Storage output location (מיקום הפלט ב-Cloud Storage), לוחצים על Browse (עיון) כדי לפתוח את תיבת הדו-שיח Select folder (בחירת תיקייה).

  9. בוחרים קטגוריה של Cloud Storage. אפשר גם ללחוץ על  Create new bucket (יצירת מאגר חדש) ולהשלים את תיבת הדו-שיח כדי ליצור מאגר.

  10. אם בחרתם בתבנית של זמן ריצה בלי להפעיל את פרטי הכניסה של משתמש הקצה, בתיבת הדו-שיח יופיע השדה חשבון שירות. בשדה Service account, מזינים את כתובת האימייל של חשבון השירות.

  11. לוחצים על שליחה.

    הפעלות מתוזמנות של מחברות מתחילות אוטומטית לפי לוח הזמנים שהגדרתם.

gcloud

לפני השימוש בנתוני הפקודה הבאים, צריך להחליף את הנתונים הבאים:

  • DISPLAY_NAME: השם המוצג של לוח הזמנים.
  • CRON_SCHEDULE: לוח הזמנים שהגדרתם, בפורמט unix-cron. לדוגמה, 00 19 * * MON פירושו שבועי ביום שני, בשעה 19:00 לפי שעון גריניץ' (GMT).
  • NOTEBOOK_RUN_NAME: השם המוצג של הפעלות המחברת שנוצרו לפי לוח הזמנים הזה.
  • NOTEBOOK_RUNTIME_TEMPLATE: תבנית זמן הריצה של המחברת שמציינת את הגדרות המחשוב של זמן הריצה.
  • NOTEBOOK_URI: ה-URI של Cloud Storage של המחברת להפעלה.
  • OUTPUT_URI: המיקום ב-Cloud Storage שבו רוצים לאחסן את התוצאות.
  • USER_EMAIL: כתובת האימייל בחשבון המשתמש שמציינת את הגישה של הפעלת המחברת למשאבי Google Cloud .
  • PROJECT_ID: מזהה הפרויקט.
  • REGION: האזור שבו יופעל לוח הזמנים.

מריצים את הפקודה הבאה:

‫Linux,‏ macOS או Cloud Shell

gcloud colab schedules create --display-name="DISPLAY_NAME" \
    --cron-schedule=CRON_SCHEDULE \
    --execution-display-name=NOTEBOOK_RUN_NAME \
    --notebook-runtime-template=NOTEBOOK_RUNTIME_TEMPLATE \
    --gcs-notebook-uri=NOTEBOOK_URI \
    --gcs-output-uri=OUTPUT_URI \
    --user-email=USER_EMAIL \
    --project=PROJECT_ID \
    --region=REGION

‏Windows (PowerShell)

gcloud colab schedules create --display-name="DISPLAY_NAME" `
    --cron-schedule=CRON_SCHEDULE `
    --execution-display-name=NOTEBOOK_RUN_NAME `
    --notebook-runtime-template=NOTEBOOK_RUNTIME_TEMPLATE `
    --gcs-notebook-uri=NOTEBOOK_URI `
    --gcs-output-uri=OUTPUT_URI `
    --user-email=USER_EMAIL `
    --project=PROJECT_ID `
    --region=REGION

Windows‏ (cmd.exe)

gcloud colab schedules create --display-name="DISPLAY_NAME" ^
    --cron-schedule=CRON_SCHEDULE ^
    --execution-display-name=NOTEBOOK_RUN_NAME ^
    --notebook-runtime-template=NOTEBOOK_RUNTIME_TEMPLATE ^
    --gcs-notebook-uri=NOTEBOOK_URI ^
    --gcs-output-uri=OUTPUT_URI ^
    --user-email=USER_EMAIL ^
    --project=PROJECT_ID ^
    --region=REGION

לקבלת מידע נוסף על יצירת לוחות זמנים של מחברות Colab Enterprise משורת הפקודה, אפשר לעיין במסמכי התיעוד של ה-CLI של gcloud.

Python

לפני שמנסים את הדוגמה הזו, צריך להתקין את Agent Platform SDK ל-Python. ספריית הלקוח של Python ל-Agent Platform מותקנת כשמתקינים את Agent Platform SDK ל-Python. מידע נוסף מופיע במאמרי העזרה של ה-API.

כדי להריץ את דוגמת הקוד הבאה, צריך את מזהה מאגר Dataform של המחברת. כדי לקבל את מזהה המאגר של המחברת, אפשר להשתמש ב-method ‏list_repositories של Dataform.

from google.cloud import aiplatform_v1

PROJECT_ID = "my-project"
LOCATION = "us-central1"
REPOSITORY_ID = "b223577f-a3fb-482c-a22c-0658c6602598"
TEMPLATE_ID = "6524523989455339520"

API_ENDPOINT = f"{LOCATION}-aiplatform.googleapis.com"
PARENT = f"projects/{PROJECT_ID}/locations/{LOCATION}"

schedules_service_client = aiplatform_v1.ScheduleServiceClient(client_options = {
    "api_endpoint": API_ENDPOINT,
})

schedule = schedules_service_client.create_schedule(parent=PARENT, schedule={
    "display_name": "my-notebook-schedule",

    # Time specification. TZ is optional.
    # cron = "* * * * *" to run it in the next minute.
    "cron": "TZ=America/Los_Angeles * * * * *",

    # How many runs the schedule will trigger before it becomes COMPLETED.
    # A Schedule in COMPLETED state will not trigger any more runs.
    "max_run_count": 1,
    "max_concurrent_run_count": 1,

    "create_notebook_execution_job_request": {
      "parent": PARENT,
      "notebook_execution_job": {
        "display_name": "my-execution-job",

        # Specify a NotebookRuntimeTemplate to source compute configuration from
        "notebook_runtime_template_resource_name": f"projects/{PROJECT_ID}/locations/{LOCATION}/notebookRuntimeTemplates/{TEMPLATE_ID}",

        # Specify a Colab Enterprise notebook to run
        "dataform_repository_source": {
            "dataform_repository_resource_name": f"projects/{PROJECT_ID}/locations/{LOCATION}/repositories/{REPOSITORY_ID}",
        },

        # Specify a Cloud Storage bucket to store output artifacts
        "gcs_output_uri": "gs://my-bucket/",


        # Specify the identity that runs the notebook
        "execution_user": "{EMAIL}",

        # Run as the service account instead
        # "service_account": "my-service-account",
    }
  }
})

Terraform

כדי ללמוד איך להחיל הגדרות ב-Terraform או להסיר אותן, ראו פקודות בסיסיות ב-Terraform. למידע נוסף, ראו את מאמרי העזרה לספקים של Terraform.

בדוגמה הבאה נעשה שימוש במשאב Terraform‏ google_colab_schedule כדי לתזמן הפעלה של notebook ב-Colab Enterprise.

resource "google_colab_runtime_template" "my_runtime_template" {
  provider = google-beta
  name = "{{index $.Vars "runtime_template_name"}}"
  display_name = "Runtime template"
  location = "us-central1"

  machine_spec {
    machine_type     = "e2-standard-4"
  }

  network_spec {
    enable_internet_access = true
  }
}

resource "google_storage_bucket" "output_bucket" {
  provider = google-beta
  name          = "{{index $.Vars "bucket"}}"
  location      = "US"
  force_destroy = true
  uniform_bucket_level_access = true
}

resource "google_secret_manager_secret" "secret" {
  provider = google-beta
  secret_id = "{{index $.Vars "secret"}}"
  replication {
    auto {}
  }
}

resource "google_secret_manager_secret_version" "secret_version" {
  provider = google-beta
  secret = google_secret_manager_secret.secret.id
  secret_data = "secret-data"
}

resource "google_dataform_repository" "dataform_repository" {
  provider = google-beta
  name = "{{index $.Vars "dataform_repository"}}"
  display_name = "dataform_repository"
  npmrc_environment_variables_secret_version = google_secret_manager_secret_version.secret_version.id
  kms_key_name = "{{index $.Vars "key_name"}}"

  labels = {
    label_foo1 = "label-bar1"
  }

  git_remote_settings {
      url = "https://github.com/OWNER/REPOSITORY.git"
      default_branch = "main"
      authentication_token_secret_version = google_secret_manager_secret_version.secret_version.id
  }

  workspace_compilation_overrides {
    default_database = "database"
    schema_suffix = "_suffix"
    table_prefix = "prefix_"
  }

}

resource "google_colab_schedule" "{{$.PrimaryResourceId}}" {
  provider = google-beta
  display_name = "{{index $.Vars "display_name"}}"
  location = "{{index $.TestEnvVars "location"}}"
  allow_queueing = true
  max_concurrent_run_count = 2
  cron = "TZ=America/Los_Angeles * * * * *"
  max_run_count = 5
  start_time = "{{index $.Vars "start_time"}}"
  end_time = "{{index $.Vars "end_time"}}"

  desired_state = "ACTIVE"

  create_notebook_execution_job_request {
    notebook_execution_job {
      display_name = "Notebook execution"
      execution_timeout = "86400s"

      dataform_repository_source {
        commit_sha = "randomsha123"
        dataform_repository_resource_name = "projects/{{index $.TestEnvVars "project_id"}}/locations/{{index $.TestEnvVars "location"}}/repositories/${google_dataform_repository.dataform_repository.name}"
      }

      notebook_runtime_template_resource_name = "projects/${google_colab_runtime_template.my_runtime_template.project}/locations/${google_colab_runtime_template.my_runtime_template.location}/notebookRuntimeTemplates/${google_colab_runtime_template.my_runtime_template.name}"

      gcs_output_uri = "gs://${google_storage_bucket.output_bucket.name}"
      service_account = "{{index $.TestEnvVars "service_account"}}"
    }
  }

  depends_on = [
    google_colab_runtime_template.my_runtime_template,
    google_storage_bucket.output_bucket,
    google_secret_manager_secret_version.secret_version,
    google_dataform_repository.dataform_repository,
  ]
}

במסוף Google Cloud , אפשר לראות את לוחות הזמנים בדף Schedules. אפשר לראות את התוצאות של הפעלות מחברות שהושלמו בדף ההפעלות.

הצגת תוצאות

כדי לראות את תוצאות ההרצה של מחברת, אפשר להשתמש במסוף Google Cloud , ב-CLI של gcloud או בספריית הלקוח של Python של Agent Platform.

המסוף

  1. במסוף Google Cloud , נכנסים לדף Executions של Colab Enterprise.

    כניסה לדף Executions

  2. לצד ההרצה של ה-Notebook שרוצים לראות את התוצאות שלה, לוחצים על הצגת התוצאה.

    ב-Colab Enterprise, התוצאה של הפעלת המחברת נפתחת בכרטיסייה חדשה.

  3. כדי לראות את התוצאה, לוחצים על הכרטיסייה.

gcloud

לפני השימוש בנתוני הפקודה הבאים, צריך להחליף את הנתונים הבאים:

  • PROJECT_ID: מזהה הפרויקט.
  • REGION: האזור שבו נמצאים תוצאות ההרצה של המחברת.
  • SCHEDULE_NAME: השם של לוח הזמנים שעבורו רוצים להציג את התוצאות. כדי לראות תוצאות מכל לוחות הזמנים, משמיטים את הדגל --filter.

מריצים את הפקודה הבאה:

‫Linux,‏ macOS או Cloud Shell

gcloud colab executions list --project=PROJECT_ID \
    --region=REGION \
    --filter="scheduleResourceName:SCHEDULE_NAME"

‏Windows (PowerShell)

gcloud colab executions list --project=PROJECT_ID `
    --region=REGION `
    --filter="scheduleResourceName:SCHEDULE_NAME"

Windows‏ (cmd.exe)

gcloud colab executions list --project=PROJECT_ID ^
    --region=REGION ^
    --filter="scheduleResourceName:SCHEDULE_NAME"

מידע נוסף על הצגת רשימה של הפעלות מחברת Colab Enterprise משורת הפקודה זמין במסמכי התיעוד של ה-CLI של gcloud.

Python

לפני שמנסים את הדוגמה הזו, צריך להתקין את Agent Platform SDK ל-Python. ספריית הלקוח של Python ל-Agent Platform מותקנת כשמתקינים את Agent Platform SDK ל-Python. מידע נוסף מופיע במאמרי העזרה של ה-API.

כדי להריץ את דוגמת הקוד הבאה, צריך את מזהה מאגר Dataform של המחברת. כדי לקבל את מזהה המאגר של המחברת, אפשר להשתמש ב-method ‏list_repositories של Dataform.

from google.cloud import aiplatform_v1

PROJECT_ID = "my-project"
LOCATION = "us-central1"

API_ENDPOINT = f"{LOCATION}-aiplatform.googleapis.com"
PARENT = f"projects/{PROJECT_ID}/locations/{LOCATION}"

notebook_service_client = aiplatform_v1.NotebookServiceClient(client_options = {
    "api_endpoint": API_ENDPOINT,
})

notebook_execution_jobs = notebook_service_client.list_notebook_execution_jobs(parent=PARENT)
notebook_execution_jobs

מחיקת תוצאות

כדי למחוק תוצאה מאחת מהרצות המחברת, אפשר להשתמש במסוף Google Cloud או ב-CLI של gcloud.

המסוף

  1. במסוף Google Cloud , נכנסים לדף Executions של Colab Enterprise.

    כניסה לדף Executions

  2. בוחרים את ההרצה של ה-Notebook שרוצים למחוק את התוצאה שלה.

  3. לוחצים על  מחיקה.

  4. כדי לאשר את המחיקה, לוחצים על אישור.

gcloud

לפני השימוש בנתוני הפקודה הבאים, צריך להחליף את הנתונים הבאים:

  • NOTEBOOK_RUN_ID: המזהה של ההרצה של מחברת ה-Notebook שרוצים למחוק.
  • PROJECT_ID: מזהה הפרויקט.
  • REGION: האזור שבו נמצאת ההרצה של המחברת.

מריצים את הפקודה הבאה:

‫Linux,‏ macOS או Cloud Shell

gcloud colab executions delete NOTEBOOK_RUN_ID \
    --project=PROJECT_ID \
    --region=REGION

‏Windows (PowerShell)

gcloud colab executions delete NOTEBOOK_RUN_ID `
    --project=PROJECT_ID `
    --region=REGION

Windows‏ (cmd.exe)

gcloud colab executions delete NOTEBOOK_RUN_ID ^
    --project=PROJECT_ID ^
    --region=REGION

למידע נוסף על מחיקת הפעלות של מחברות Colab Enterprise משורת הפקודה, אפשר לעיין במסמכי התיעוד של ה-CLI של gcloud.

שיתוף תוצאות של הפעלת נוטבוק

כדי לשתף את תוצאות ההרצה של מחברת, צריך לתת גישה לקטגוריה של Cloud Storage שמכילה את ההרצה של המחברת. מתן הגישה הזו מעניק למשתמשים גם גישה לכל משאב אחר באותה קטגוריה של Cloud Storage (ראו שיקולי אבטחה).

מידע נוסף מופיע במאמר שיתוף ושיתוף פעולה ב-Cloud Storage.

שיקולי אבטחה

תוצאות ההרצה של המחברת מאוחסנות כקבצים של מחברת (IPYNB) בקטגוריה של Cloud Storage. כשמעניקים גישה לקטגוריה הזו, חשוב לשים לב לנקודות הבאות:

  • כל מי שיש לו גישה למאגר יכול לראות את הקוד של קובץ ה-Notebook ואת התוצאות של הרצת ה-Notebook.

  • כל מי שיש לו אפשרות לשנות את התוכן של ה-bucket יכול לשנות את התוכן של קובץ המחברת.

אם לוח הזמנים מוגדר לשימוש בהרשאות אישיות, רק המשתמש שצוין יכול לשנות את לוח הזמנים או להפעיל אותו.

אם התזמון מוגדר לשימוש בחשבון שירות, רק משתמשים עם ההרשאה iam.serviceAccounts.actAs בחשבון השירות יכולים לשנות את התזמון או להפעיל אותו.

הצגת פרטים על לוח הזמנים

תוכלו לראות מידע על לוח זמנים, כולל:

  • הקטגוריה של Cloud Storage שבה התוצאות מאוחסנות לפי לוח הזמנים.
  • שעת ההתחלה ושעת הסיום.
  • התדירות.

כדי לראות את פרטי התזמון, אפשר להשתמש במסוף Google Cloud או ב-CLI של gcloud.

המסוף

  1. במסוף Google Cloud , נכנסים לדף Schedules של Colab Enterprise.

    מעבר ללוחות זמנים

  2. לוחצים על שם התזמון.

    ייפתח הדף פרטי התזמון.

  3. כדי לחזור לדף לוחות זמנים, לוחצים על  חזרה לדף הקודם.

gcloud

לפני השימוש בנתוני הפקודה הבאים, צריך להחליף את הנתונים הבאים:

  • SCHEDULE: מזהה לוח הזמנים.
  • PROJECT_ID: מזהה הפרויקט.
  • REGION: האזור שבו נמצא לוח הזמנים.

מריצים את הפקודה הבאה:

‫Linux,‏ macOS או Cloud Shell

gcloud colab schedules describe SCHEDULE \
    --project=PROJECT_ID \
    --region=REGION

‏Windows (PowerShell)

gcloud colab schedules describe SCHEDULE `
    --project=PROJECT_ID `
    --region=REGION

Windows‏ (cmd.exe)

gcloud colab schedules describe SCHEDULE ^
    --project=PROJECT_ID ^
    --region=REGION

מידע נוסף על הצגת לוחות זמנים של Colab Enterprise משורת הפקודה זמין במסמכי התיעוד של ה-CLI של gcloud.

השהיה, המשך או מחיקה של תזמון

כדי להשהות, להמשיך או למחוק לוח זמנים, אפשר להשתמש במסוף Google Cloud , ב-CLI של gcloud או ב-Terraform.

המסוף

  1. במסוף Google Cloud , נכנסים לדף Schedules של Colab Enterprise.

    מעבר ללוחות זמנים

  2. בוחרים לוח זמנים.

  3. לוחצים על  השהיה,  המשך, או על  מחיקה.

gcloud

לפני השימוש בנתוני הפקודה הבאים, צריך להחליף את הנתונים הבאים:

  • ACTION: אחד מהערכים pause,‏ resume או delete.
  • SCHEDULE_ID: מזהה לוח הזמנים.
  • PROJECT_ID: מזהה הפרויקט.
  • REGION: האזור שבו נמצא לוח הזמנים.

מריצים את הפקודה הבאה:

‫Linux,‏ macOS או Cloud Shell

gcloud colab schedules ACTION SCHEDULE_ID \
    --project=PROJECT_ID \
    --region=REGION

‏Windows (PowerShell)

gcloud colab schedules ACTION SCHEDULE_ID `
    --project=PROJECT_ID `
    --region=REGION

Windows‏ (cmd.exe)

gcloud colab schedules ACTION SCHEDULE_ID ^
    --project=PROJECT_ID ^
    --region=REGION

לקבלת מידע נוסף על ניהול לוחות זמנים של Colab Enterprise משורת הפקודה, אפשר לעיין במסמכי התיעוד של ה-CLI של gcloud.

Terraform

כדי ללמוד איך להחיל הגדרות ב-Terraform או להסיר אותן, ראו פקודות בסיסיות ב-Terraform. למידע נוסף, ראו את מאמרי העזרה לספקים של Terraform.

בדוגמה הבאה נעשה שימוש במשאב Terraform‏ google_colab_schedule כדי להשהות או להפעיל מחדש תזמון.

כדי להשתמש בדוגמה הזו, משנים את הערך של desired_state לפי ההנחיות הבאות:

  • PAUSED כדי להשהות את התזמון
  • ACTIVE כדי להמשיך את התזמון
resource "google_colab_runtime_template" "my_runtime_template" {
  name = "{{index $.Vars "runtime_template_name"}}"
  display_name = "Runtime template"
  location = "us-central1"

  machine_spec {
    machine_type     = "e2-standard-4"
  }

  network_spec {
    enable_internet_access = true
  }
}

resource "google_storage_bucket" "output_bucket" {
  name          = "{{index $.Vars "bucket"}}"
  location      = "US"
  force_destroy = true
  uniform_bucket_level_access = true
}

resource "google_storage_bucket_object" "notebook" {
  name   = "hello_world.ipynb"
  bucket = google_storage_bucket.output_bucket.name
  content = <<EOF
    {
      "cells": [
        {
          "cell_type": "code",
          "execution_count": null,
          "metadata": {},
          "outputs": [],
          "source": [
            "print(\"Hello, World!\")"
          ]
        }
      ],
      "metadata": {
        "kernelspec": {
          "display_name": "Python 3",
          "language": "python",
          "name": "python3"
        },
        "language_info": {
          "codemirror_mode": {
            "name": "ipython",
            "version": 3
          },
          "file_extension": ".py",
          "mimetype": "text/x-python",
          "name": "python",
          "nbconvert_exporter": "python",
          "pygments_lexer": "ipython3",
          "version": "3.8.5"
        }
      },
      "nbformat": 4,
      "nbformat_minor": 4
    }
    EOF
}

resource "google_colab_schedule" "{{$.PrimaryResourceId}}" {
  display_name = "{{index $.Vars "display_name"}}"
  location = "{{index $.TestEnvVars "location"}}"
  max_concurrent_run_count = 2
  cron = "TZ=America/Los_Angeles * * * * *"

  desired_state = "PAUSED"

  create_notebook_execution_job_request {
    notebook_execution_job {
      display_name = "Notebook execution"
      gcs_notebook_source {
        uri = "gs://${google_storage_bucket_object.notebook.bucket}/${google_storage_bucket_object.notebook.name}"
        generation = google_storage_bucket_object.notebook.generation
      }

      notebook_runtime_template_resource_name = "projects/${google_colab_runtime_template.my_runtime_template.project}/locations/${google_colab_runtime_template.my_runtime_template.location}/notebookRuntimeTemplates/${google_colab_runtime_template.my_runtime_template.name}"
      gcs_output_uri = "gs://${google_storage_bucket.output_bucket.name}"
      service_account = "{{index $.TestEnvVars "service_account"}}"
      }
  }

  depends_on = [
    google_colab_runtime_template.my_runtime_template,
    google_storage_bucket.output_bucket,
  ]
}

המאמרים הבאים