Crea ed esegui un job che utilizza volumi di archiviazione

Questo documento spiega come creare ed eseguire un job Batch che utilizza uno o più volumi di archiviazione esterni. Le opzioni di archiviazione esterna includono un disco permanente nuovo o esistente, nuovi SSD locali, bucket Cloud Storage esistenti e un Network File System (NFS) esistente, ad esempio una condivisione file di Filestore.

Indipendentemente dall'aggiunta di volumi di archiviazione esterni, ogni VM di Compute Engine per un job dispone di un disco di avvio, che fornisce spazio di archiviazione per l'immagine del sistema operativo (OS) e le istruzioni del job. Per informazioni sulla configurazione del disco di avvio per un job, consulta invece Panoramica dell'ambiente del sistema operativo della VM.

Prima di iniziare

  1. Se non hai mai utilizzato Batch, consulta Inizia a utilizzare Batch e abilita Batch completando i prerequisiti per progetti e utenti.
  2. Per ottenere le autorizzazioni necessarie per creare un job, chiedi all'amministratore di concederti i seguenti ruoli IAM:

    Per saperne di più sulla concessione dei ruoli, consulta Gestisci l'accesso a progetti, cartelle e organizzazioni.

    Potresti anche riuscire a ottenere le autorizzazioni richieste tramite i ruoli personalizzati o altri ruoli predefiniti.

Crea un job che utilizza i volumi di archiviazione

Facoltativamente, un job può utilizzare uno o più dei seguenti tipi di volumi di archiviazione esterni. Per saperne di più su tutti i tipi di volumi di archiviazione e sulle differenze e le limitazioni di ciascuno, consulta la documentazione relativa alle opzioni di archiviazione delle VM di Compute Engine.

Puoi consentire a un job di utilizzare ogni volume di archiviazione includendolo nella definizione del job e specificando il relativo percorso di montaggio (mountPath) nei tuoi eseguibili. Per scoprire come creare un job che utilizza i volumi di archiviazione, consulta una o più delle seguenti sezioni:

Utilizza un disco permanente

Un job che utilizza Persistent Disk presenta le seguenti limitazioni:

  • Tutti i Persistent Disk: consulta le limitazioni per tutti i Persistent Disk.

  • Dischi permanenti nuovi ed esistenti: ogni disco permanente in un job può essere nuovo (definito e creato con il job) o esistente (già creato nel progetto e specificato nel job). Per utilizzare un disco permanente, questo deve essere formattato e montato sulle VM del job, che devono trovarsi nella stessa località del disco permanente. Batch monta tutti i dischi permanenti inclusi in un job e formatta tutti i nuovi dischi permanenti, ma devi formattare e smontare tutti i dischi permanenti esistenti che vuoi che un job utilizzi.

    Le opzioni di posizione, le opzioni di formato e le opzioni di montaggio supportate variano tra i Persistent Disk nuovi ed esistenti, come descritto nella seguente tabella:

    Nuovi Persistent Disk Dischi permanenti esistenti
    Opzioni formato

    Il disco permanente viene formattato automaticamente con un file system ext4.

    Devi formattare il Persistent Disk per utilizzare un file system ext4 prima di utilizzarlo per un job.

    Opzioni di montaggio

    Tutte le opzioni sono supportate.

    Sono supportate tutte le opzioni tranne la scrittura. Ciò è dovuto alle limitazioni della modalità multi-writer.

    Prima di utilizzarlo per un job, devi scollegare il Persistent Disk da tutte le VM a cui è collegato.

    Opzioni per località

    Puoi creare solo Persistent Disk a livello di zona.

    Puoi selezionare qualsiasi località per il job. I dischi permanenti vengono creati nella zona in cui viene eseguito il progetto.

    Puoi selezionare Persistent Disk a livello di zona e di regione.


    Per utilizzare un disco, le risorse di Compute Engine per un lavoro devono essere create solo nelle posizioni che contengono tutti i Persistent Disk del lavoro. Ad esempio, per un Persistent Disk a livello di zona, la località del job deve essere la zona del disco; per un disco permanente a livello di regione, la località del job deve essere la regione del disco o, se specifichi le zone, una o entrambe le zone specifiche in cui si trova il disco permanente a livello di regione. Per saperne di più, consulta Località.

  • Template di istanza: se vuoi utilizzare un template di istanza VM durante la creazione di questo job, devi collegare tutti i Persistent Disk per questo job nel template di istanza. In caso contrario, se non vuoi utilizzare un template di istanza, devi collegare i Persistent Disk direttamente nella definizione del job.

Puoi creare un job che utilizza un disco permanente utilizzando la consoleGoogle Cloud , gcloud CLI, l'API Batch, C++, Go, Java, Node.js o Python.

Console

Utilizzando la console Google Cloud , l'esempio seguente crea un job che esegue uno script per leggere un file da un disco permanente a livello di zona esistente che si trova nella zona us-central1-a. Lo script di esempio presuppone che il job abbia un disco permanente a livello di zona esistente che contiene un file di testo denominato example.txt nella directory root.

(Facoltativo) Crea un disco permanente a livello di zona di esempio

Se vuoi creare un disco permanente a livello di zona da utilizzare per eseguire lo script di esempio, segui questi passaggi prima di creare il job:

  1. Collega un nuovo disco permanente vuoto denominato example-disk a una VM Linux nella zona us-central1-a, quindi esegui i comandi sulla VM per formattare e montare il disco. Per istruzioni, consulta Aggiungi un disco permanente alla tua VM.

    Non disconnetterti ancora dalla VM.

  2. Per creare example.txt su disco permanente, esegui questi comandi sulla VM:

    1. Per cambiare la directory di lavoro corrente impostandola sulla directory root del disco permanente, digita il seguente comando:

      cd VM_MOUNT_PATH
      

      Sostituisci VM_MOUNT_PATH con il percorso della directory in cui è stato montato il disco permanente su questa VM nel passaggio precedente, ad esempio /mnt/disks/example-disk.

    2. Premi Enter.

    3. Per creare e definire un file denominato example.txt, digita il seguente comando:

      cat > example.txt
      
    4. Premi Enter.

    5. Digita i contenuti del file. Ad esempio, digita Hello world!.

    6. Per salvare il file, premi Ctrl+D (o Command+D su macOS).

    Al termine, puoi disconnetterti dalla VM.

  3. Scollega il disco permanente dalla VM.

Crea un job che utilizzi il disco permanente a livello di zona esistente

Per creare un job che utilizza i Persistent Disk a livello di zona esistenti utilizzando la consoleGoogle Cloud , segui questi passaggi:

  1. Nella console Google Cloud , vai alla pagina Elenco job.

    Vai all'elenco dei job

  2. Fai clic su Crea. Viene visualizzata la pagina Crea job batch. Nel riquadro a sinistra, è selezionata la pagina Dettagli job.

  3. Configura la pagina Dettagli job:

    1. (Facoltativo) Nel campo Nome job, personalizza il nome del job.

      Ad esempio, inserisci example-disk-job.

    2. Configura la sezione Dettagli attività:

      1. Nella finestra Nuovo eseguibile, aggiungi almeno uno script o un container per l'esecuzione di questo job.

        Ad esempio, per eseguire uno script che stampa i contenuti di un file denominato example.txt e che si trova nella directory root del disco permanente utilizzato da questo job, segui questi passaggi:

        1. Seleziona la casella di controllo Script. Viene visualizzato un campo.

        2. Nel campo, inserisci il seguente script:

          echo "Here is the content of the example.txt file in the persistent disk."
          cat MOUNT_PATH/example.txt
          

          Sostituisci MOUNT_PATH con il percorso in cui prevedi di montare il disco permanente sulle VM per questo job, ad esempio /mnt/disks/example-disk.

        3. Fai clic su Fine.

      2. Nel campo Numero di attività, inserisci il numero di attività per questo job.

        Ad esempio, inserisci 1 (impostazione predefinita).

      3. Nel campo Parallelismo, inserisci il numero di attività da eseguire contemporaneamente.

        Ad esempio, inserisci 1 (impostazione predefinita).

  4. Configura la pagina Specifiche delle risorse:

    1. Nel riquadro a sinistra, fai clic su Specifiche delle risorse. Viene visualizzata la pagina Specifiche delle risorse.

    2. Seleziona la località per questo job. Per utilizzare un disco permanente a livello di zona esistente, le VM di un job devono trovarsi nella stessa zona.

      1. Nel campo Regione, seleziona una regione.

        Ad esempio, per utilizzare il disco permanente a livello di zona di esempio, seleziona us-central1 (Iowa) (predefinito).

      2. Nel campo Zona, seleziona una zona.

        Ad esempio, seleziona us-central1-a (Iowa).

  5. Configura la pagina Configurazioni aggiuntive:

    1. Nel riquadro a sinistra, fai clic su Configurazioni aggiuntive. Si apre la pagina Configurazioni aggiuntive.

    2. Per ogni disco permanente a livello di zona esistente che vuoi montare in questo job, segui questi passaggi:

      1. Nella sezione Volume di archiviazione, fai clic su Aggiungi nuovo volume. Viene visualizzata la finestra Nuovo volume.

      2. Nella finestra Nuovo volume, segui questi passaggi:

        1. Nella sezione Tipo di volume, seleziona Persistent Disk (predefinito).

        2. Nell'elenco Disco, seleziona un disco permanente a livello di zona esistente che vuoi montare su questo job. Il disco deve trovarsi nella stessa zona del job.

          Ad esempio, seleziona il disco permanente a livello di zona esistente che hai preparato, che si trova nella zona us-central1-a e contiene il file example.txt.

        3. (Facoltativo) Se vuoi rinominare questo disco permanente a livello di zona, procedi nel seguente modo:

          1. Seleziona Personalizza il nome del dispositivo.

          2. Nel campo Nome dispositivo, inserisci il nuovo nome per il disco.

        4. Nel campo Percorso di montaggio, inserisci il percorso di montaggio (MOUNT_PATH) per questo disco permanente:

          Ad esempio, inserisci quanto segue:

          /mnt/disks/EXISTING_PERSISTENT_DISK_NAME
          

          Sostituisci EXISTING_PERSISTENT_DISK_NAME con il nome del disco. Se hai rinominato il disco permanente a livello di zona, utilizza il nuovo nome.

          Ad esempio, sostituisci EXISTING_PERSISTENT_DISK_NAME con example-disk.

        5. Fai clic su Fine.

  6. (Facoltativo) Configura gli altri campi per questo job.

  7. (Facoltativo) Per esaminare la configurazione del job, fai clic su Anteprima nel riquadro a sinistra.

  8. Fai clic su Crea.

Nella pagina Dettagli job viene visualizzato il job che hai creato.

gcloud

Utilizzando gcloud CLI, l'esempio seguente crea un job che collega e monta un disco permanente esistente e un nuovo disco permanente. Il job ha tre attività, ciascuna delle quali esegue uno script per creare un file nel nuovo disco permanente denominato output_task_TASK_INDEX.txt dove TASK_INDEX è l'indice di ogni attività: 0, 1 e 2.

Per creare un job che utilizza i Persistent Disk con gcloud CLI, utilizza il comando gcloud batch jobs submit. Nel file di configurazione JSON del job, specifica i Persistent Disk nel campo instances e monta il disco permanente nel campo volumes.

  1. Crea un file JSON.

    • Se non utilizzi un template di istanza per questo job, crea un file JSON con i seguenti contenuti:

      {
          "allocationPolicy": {
              "instances": [
                  {
                      "policy": {
                          "disks": [
                              {
                                  "deviceName": "EXISTING_PERSISTENT_DISK_NAME",
                                  "existingDisk": "projects/PROJECT_ID/EXISTING_PERSISTENT_DISK_LOCATION/disks/EXISTING_PERSISTENT_DISK_NAME"
                              },
                              {
                                  "newDisk": {
                                      "sizeGb": NEW_PERSISTENT_DISK_SIZE,
                                      "type": "NEW_PERSISTENT_DISK_TYPE"
                                  },
                                  "deviceName": "NEW_PERSISTENT_DISK_NAME"
                              }
                          ]
                      }
                  }
              ],
              "location": {
                  "allowedLocations": [
                      "EXISTING_PERSISTENT_DISK_LOCATION"
                  ]
              }
          },
          "taskGroups": [
              {
                  "taskSpec": {
                      "runnables": [
                          {
                              "script": {
                                  "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/disks/NEW_PERSISTENT_DISK_NAME/output_task_${BATCH_TASK_INDEX}.txt"
                              }
                          }
                      ],
                      "volumes": [
                          {
                              "deviceName": "NEW_PERSISTENT_DISK_NAME",
                              "mountPath": "/mnt/disks/NEW_PERSISTENT_DISK_NAME",
                              "mountOptions": "rw,async"
                          },
                          {
      
                              "deviceName": "EXISTING_PERSISTENT_DISK_NAME",
                              "mountPath": "/mnt/disks/EXISTING_PERSISTENT_DISK_NAME"
                          }
                      ]
                  },
                  "taskCount":3
              }
          ],
          "logsPolicy": {
              "destination": "CLOUD_LOGGING"
          }
      }
      

      Sostituisci quanto segue:

      • PROJECT_ID: l'ID progetto del tuo progetto.
      • EXISTING_PERSISTENT_DISK_NAME: il nome di un disco permanente esistente.
      • EXISTING_PERSISTENT_DISK_LOCATION: la posizione di un disco permanente esistente. Per ogni disco permanente a livello di zona esistente, la località del job deve essere la zona del disco; per ogni disco permanente a livello di regione esistente, la località del job deve essere la regione del disco o, se specifichi le zone, una o entrambe le zone specifiche in cui si trova il disco permanente a livello di regione. Scopri di più sul campo allowedLocations.

      • NEW_PERSISTENT_DISK_SIZE: le dimensioni del nuovo disco permanente in GB. Le dimensioni consentite dipendono dal tipo di disco permanente, ma il minimo è spesso 10 GB (10) e il massimo è spesso 64 TB (64000).

      • NEW_PERSISTENT_DISK_TYPE: il tipo di disco del nuovo disco permanente, pd-standard, pd-balanced, pd-ssd o pd-extreme. Il tipo di disco predefinito per i Persistent Disk non di avvio è pd-standard.

      • NEW_PERSISTENT_DISK_NAME: il nome del nuovo disco permanente.

    • Se utilizzi un template di istanza VM per questo job, crea un file JSON come mostrato in precedenza, ma sostituisci il campo instances con quanto segue:

      "instances": [
          {
              "instanceTemplate": "INSTANCE_TEMPLATE_NAME"
          }
      ],
      

      dove INSTANCE_TEMPLATE_NAME è il nome del template di istanza per questo job. Per un job che utilizza Persistent Disk, questo template di istanza deve definire e collegare i Persistent Disk che vuoi che vengano utilizzati dal job. In questo esempio, il template deve definire e collegare un nuovo disco permanente denominato NEW_PERSISTENT_DISK_NAME e collegare un disco permanente esistente denominato EXISTING_PERSISTENT_DISK_NAME.

  2. Esegui questo comando:

    gcloud batch jobs submit JOB_NAME \
      --location LOCATION \
      --config JSON_CONFIGURATION_FILE
    

    Sostituisci quanto segue:

    • JOB_NAME: il nome del job.

    • LOCATION: la località del job.

    • JSON_CONFIGURATION_FILE: il percorso di un file JSON con i dettagli di configurazione del job.

API

Utilizzando l'API Batch, l'esempio seguente crea un job che collega e monta un disco permanente esistente e un nuovo disco permanente. Il job ha tre attività, ciascuna delle quali esegue uno script per creare un file nel nuovo disco permanente denominato output_task_TASK_INDEX.txt, dove TASK_INDEX è l'indice di ciascuna attività: 0, 1 e 2.

Per creare un job che utilizza i Persistent Disk utilizzando l'API Batch, utilizza il metodo jobs.create. Nella richiesta, specifica i Persistent Disk nel campo instances e monta il disco permanente nel campo volumes.

  • Se non utilizzi un template di istanza per questo job, effettua la seguente richiesta:

    POST https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs?job_id=JOB_NAME
    
    {
        "allocationPolicy": {
            "instances": [
                {
                    "policy": {
                        "disks": [
                            {
                                "deviceName": "EXISTING_PERSISTENT_DISK_NAME",
                                "existingDisk": "projects/PROJECT_ID/EXISTING_PERSISTENT_DISK_LOCATION/disks/EXISTING_PERSISTENT_DISK_NAME"
                            },
                            {
                                "newDisk": {
                                    "sizeGb": NEW_PERSISTENT_DISK_SIZE,
                                    "type": "NEW_PERSISTENT_DISK_TYPE"
                                },
                                "deviceName": "NEW_PERSISTENT_DISK_NAME"
                            }
                        ]
                    }
                }
            ],
            "location": {
                "allowedLocations": [
                    "EXISTING_PERSISTENT_DISK_LOCATION"
                ]
            }
        },
        "taskGroups": [
            {
                "taskSpec": {
                    "runnables": [
                        {
                            "script": {
                                "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/disks/NEW_PERSISTENT_DISK_NAME/output_task_${BATCH_TASK_INDEX}.txt"
                            }
                        }
                    ],
                    "volumes": [
                        {
                            "deviceName": "NEW_PERSISTENT_DISK_NAME",
                            "mountPath": "/mnt/disks/NEW_PERSISTENT_DISK_NAME",
                            "mountOptions": "rw,async"
                        },
                        {
    
                            "deviceName": "EXISTING_PERSISTENT_DISK_NAME",
                            "mountPath": "/mnt/disks/EXISTING_PERSISTENT_DISK_NAME"
                        }
                    ]
                },
                "taskCount":3
            }
        ],
        "logsPolicy": {
            "destination": "CLOUD_LOGGING"
        }
    }
    

    Sostituisci quanto segue:

    • PROJECT_ID: l'ID progetto del tuo progetto.
    • LOCATION: la località del job.
    • JOB_NAME: il nome del job.
    • EXISTING_PERSISTENT_DISK_NAME: il nome di un disco permanente esistente.
    • EXISTING_PERSISTENT_DISK_LOCATION: la posizione di un disco permanente esistente. Per ogni disco permanente a livello di zona esistente, la località del job deve essere la zona del disco; per ogni disco permanente a livello di regione esistente, la località del job deve essere la regione del disco o, se specifichi le zone, una o entrambe le zone specifiche in cui si trova il disco permanente a livello di regione. Scopri di più sul campo allowedLocations.

    • NEW_PERSISTENT_DISK_SIZE: le dimensioni del nuovo disco permanente in GB. Le dimensioni consentite dipendono dal tipo di disco permanente, ma il minimo è spesso 10 GB (10) e il massimo è spesso 64 TB (64000).

    • NEW_PERSISTENT_DISK_TYPE: il tipo di disco del nuovo disco permanente, pd-standard, pd-balanced, pd-ssd o pd-extreme. Il tipo di disco predefinito per i Persistent Disk non di avvio è pd-standard.

    • NEW_PERSISTENT_DISK_NAME: il nome del nuovo disco permanente.

  • Se utilizzi un template di istanza VM per questo job, crea un file JSON come mostrato in precedenza, ma sostituisci il campo instances con quanto segue:

    "instances": [
        {
            "instanceTemplate": "INSTANCE_TEMPLATE_NAME"
        }
    ],
    ...
    

    Dove INSTANCE_TEMPLATE_NAME è il nome del template di istanza per questo job. Per un job che utilizza Persistent Disk, questo template di istanza deve definire e collegare i Persistent Disk che vuoi che vengano utilizzati dal job. In questo esempio, il template deve definire e collegare un nuovo disco permanente denominato NEW_PERSISTENT_DISK_NAME e collegare un disco permanente esistente denominato EXISTING_PERSISTENT_DISK_NAME.

C++

Per creare un job batch che utilizzi Persistent Disk nuovi o esistenti utilizzando le librerie client di Cloud per C++, utilizza la funzione CreateJob e includi quanto segue:

  • Per collegare i Persistent Disk alle VM per un job, includi uno dei seguenti elementi:
    • Se non utilizzi un template di istanza VM per questo job, utilizza il metodo set_remote_path.
    • Se utilizzi un template di istanza VM per questo job, utilizza il metodo set_instance_template.
  • Per montare i Persistent Disk nel job, utilizza il campo volumes con i campi deviceName e mountPath. Per i nuovi Persistent Disk, utilizza anche il campo mountOptions per abilitare la scrittura.

Per un esempio di codice di un caso d'uso simile, consulta Utilizza un bucket Cloud Storage.

Vai

Per creare un job batch che utilizza Persistent Disk nuovi o esistenti utilizzando le librerie client di Cloud per Go, utilizza la funzione CreateJob e includi quanto segue:

import (
	"context"
	"fmt"
	"io"

	batch "cloud.google.com/go/batch/apiv1"
	"cloud.google.com/go/batch/apiv1/batchpb"
	durationpb "google.golang.org/protobuf/types/known/durationpb"
)

// Creates and runs a job with persistent disk
func createJobWithPD(w io.Writer, projectID, jobName, pdName string) error {
	// jobName := job-name
	// pdName := disk-name
	ctx := context.Background()
	batchClient, err := batch.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("batchClient error: %w", err)
	}
	defer batchClient.Close()

	runn := &batchpb.Runnable{
		Executable: &batchpb.Runnable_Script_{
			Script: &batchpb.Runnable_Script{
				Command: &batchpb.Runnable_Script_Text{
					Text: "echo Hello world from script 1 for task ${BATCH_TASK_INDEX}",
				},
			},
		},
	}
	volume := &batchpb.Volume{
		MountPath: fmt.Sprintf("/mnt/disks/%v", pdName),
		Source: &batchpb.Volume_DeviceName{
			DeviceName: pdName,
		},
	}

	// The disk type of the new persistent disk, either pd-standard,
	// pd-balanced, pd-ssd, or pd-extreme. For Batch jobs, the default is pd-balanced
	disk := &batchpb.AllocationPolicy_Disk{
		Type:   "pd-balanced",
		SizeGb: 10,
	}

	taskSpec := &batchpb.TaskSpec{
		ComputeResource: &batchpb.ComputeResource{
			// CpuMilli is milliseconds per cpu-second. This means the task requires 1 CPU.
			CpuMilli:  1000,
			MemoryMib: 16,
		},
		MaxRunDuration: &durationpb.Duration{
			Seconds: 3600,
		},
		MaxRetryCount: 2,
		Runnables:     []*batchpb.Runnable{runn},
		Volumes:       []*batchpb.Volume{volume},
	}

	taskGroups := []*batchpb.TaskGroup{
		{
			TaskCount: 4,
			TaskSpec:  taskSpec,
		},
	}

	labels := map[string]string{"env": "testing", "type": "container"}

	// Policies are used to define on what kind of virtual machines the tasks will run on.
	// Read more about local disks here: https://cloud.google.com/compute/docs/disks/persistent-disks
	allocationPolicy := &batchpb.AllocationPolicy{
		Instances: []*batchpb.AllocationPolicy_InstancePolicyOrTemplate{{
			PolicyTemplate: &batchpb.AllocationPolicy_InstancePolicyOrTemplate_Policy{
				Policy: &batchpb.AllocationPolicy_InstancePolicy{
					MachineType: "n1-standard-1",
					Disks: []*batchpb.AllocationPolicy_AttachedDisk{
						{
							Attached: &batchpb.AllocationPolicy_AttachedDisk_NewDisk{
								NewDisk: disk,
							},
							DeviceName: pdName,
						},
					},
				},
			},
		}},
	}

	// We use Cloud Logging as it's an out of the box available option
	logsPolicy := &batchpb.LogsPolicy{
		Destination: batchpb.LogsPolicy_CLOUD_LOGGING,
	}

	job := &batchpb.Job{
		Name:             jobName,
		TaskGroups:       taskGroups,
		AllocationPolicy: allocationPolicy,
		Labels:           labels,
		LogsPolicy:       logsPolicy,
	}

	request := &batchpb.CreateJobRequest{
		Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, "us-central1"),
		JobId:  jobName,
		Job:    job,
	}

	created_job, err := batchClient.CreateJob(ctx, request)
	if err != nil {
		return fmt.Errorf("unable to create job: %w", err)
	}

	fmt.Fprintf(w, "Job created: %v\n", created_job)
	return nil
}

Java

Per creare un job batch che utilizzi Persistent Disk nuovi o esistenti utilizzando le librerie client di Cloud per Java, utilizza la classe CreateJobRequest e includi quanto segue:

Ad esempio, utilizza il seguente esempio di codice:


import com.google.cloud.batch.v1.AllocationPolicy;
import com.google.cloud.batch.v1.AllocationPolicy.AttachedDisk;
import com.google.cloud.batch.v1.AllocationPolicy.Disk;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicy;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicyOrTemplate;
import com.google.cloud.batch.v1.AllocationPolicy.LocationPolicy;
import com.google.cloud.batch.v1.BatchServiceClient;
import com.google.cloud.batch.v1.CreateJobRequest;
import com.google.cloud.batch.v1.Job;
import com.google.cloud.batch.v1.LogsPolicy;
import com.google.cloud.batch.v1.Runnable;
import com.google.cloud.batch.v1.Runnable.Script;
import com.google.cloud.batch.v1.TaskGroup;
import com.google.cloud.batch.v1.TaskSpec;
import com.google.cloud.batch.v1.Volume;
import com.google.common.collect.Lists;
import com.google.protobuf.Duration;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreatePersistentDiskJob {

  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project you want to use.
    String projectId = "YOUR_PROJECT_ID";
    // Name of the region you want to use to run the job. Regions that are
    // available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
    String region = "europe-central2";
    // The name of the job that will be created.
    // It needs to be unique for each project and region pair.
    String jobName = "JOB_NAME";
    // The size of the new persistent disk in GB.
    // The allowed sizes depend on the type of persistent disk,
    // but the minimum is often 10 GB (10) and the maximum is often 64 TB (64000).
    int diskSize = 10;
    // The name of the new persistent disk.
    String newPersistentDiskName = "DISK-NAME";
    // The name of an existing persistent disk.
    String existingPersistentDiskName = "EXISTING-DISK-NAME";
    // The location of an existing persistent disk. For more info :
    // https://cloud.google.com/batch/docs/create-run-job-storage#gcloud
    String location = "regions/us-central1";
    // The disk type of the new persistent disk, either pd-standard,
    // pd-balanced, pd-ssd, or pd-extreme. For Batch jobs, the default is pd-balanced.
    String newDiskType = "pd-balanced";

    createPersistentDiskJob(projectId, region, jobName, newPersistentDiskName,
            diskSize, existingPersistentDiskName, location, newDiskType);
  }

  // Creates a job that attaches and mounts an existing persistent disk and a new persistent disk
  public static Job createPersistentDiskJob(String projectId, String region, String jobName,
                                            String newPersistentDiskName, int diskSize,
                                            String existingPersistentDiskName,
                                            String location, String newDiskType)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {
      // Define what will be done as part of the job.
      String text = "echo Hello world from task ${BATCH_TASK_INDEX}. "
              + ">> /mnt/disks/NEW_PERSISTENT_DISK_NAME/output_task_${BATCH_TASK_INDEX}.txt";
      Runnable runnable =
          Runnable.newBuilder()
              .setScript(
                  Script.newBuilder()
                      .setText(text)
                      // You can also run a script from a file. Just remember, that needs to be a
                      // script that's already on the VM that will be running the job.
                      // Using setText() and setPath() is mutually exclusive.
                      // .setPath("/tmp/test.sh")
                      .build())
              .build();

      TaskSpec task = TaskSpec.newBuilder()
              // Jobs can be divided into tasks. In this case, we have only one task.
              .addAllVolumes(volumes(newPersistentDiskName, existingPersistentDiskName))
              .addRunnables(runnable)
              .setMaxRetryCount(2)
              .setMaxRunDuration(Duration.newBuilder().setSeconds(3600).build())
              .build();

      // Tasks are grouped inside a job using TaskGroups.
      // Currently, it's possible to have only one task group.
      TaskGroup taskGroup = TaskGroup.newBuilder()
          .setTaskCount(3)
          .setParallelism(1)
          .setTaskSpec(task)
          .build();

      // Policies are used to define the type of virtual machines the tasks will run on.
      InstancePolicy policy = InstancePolicy.newBuilder()
              .addAllDisks(attachedDisks(newPersistentDiskName, diskSize, newDiskType,
                  projectId, location, existingPersistentDiskName))
              .build();

      AllocationPolicy allocationPolicy =
          AllocationPolicy.newBuilder()
              .addInstances(
                  InstancePolicyOrTemplate.newBuilder()
                      .setPolicy(policy))
                  .setLocation(LocationPolicy.newBuilder().addAllowedLocations(location))
              .build();

      Job job =
          Job.newBuilder()
              .addTaskGroups(taskGroup)
              .setAllocationPolicy(allocationPolicy)
              .putLabels("env", "testing")
              .putLabels("type", "script")
              // We use Cloud Logging as it's an out-of-the-box option.
              .setLogsPolicy(
                  LogsPolicy.newBuilder().setDestination(LogsPolicy.Destination.CLOUD_LOGGING))
              .build();

      CreateJobRequest createJobRequest =
          CreateJobRequest.newBuilder()
              // The job's parent is the region in which the job will run.
              .setParent(String.format("projects/%s/locations/%s", projectId, region))
              .setJob(job)
              .setJobId(jobName)
              .build();

      Job result =
          batchServiceClient
              .createJobCallable()
              .futureCall(createJobRequest)
              .get(5, TimeUnit.MINUTES);

      System.out.printf("Successfully created the job: %s", result.getName());

      return result;
    }
  }

  // Creates link to existing disk and creates configuration for new disk
  private static Iterable<AttachedDisk> attachedDisks(String newPersistentDiskName, int diskSize,
                                                      String newDiskType, String projectId,
                                                      String existingPersistentDiskLocation,
                                                      String existingPersistentDiskName) {
    AttachedDisk newDisk = AttachedDisk.newBuilder()
            .setDeviceName(newPersistentDiskName)
            .setNewDisk(Disk.newBuilder().setSizeGb(diskSize).setType(newDiskType))
            .build();

    String diskPath = String.format("projects/%s/%s/disks/%s", projectId,
            existingPersistentDiskLocation, existingPersistentDiskName);

    AttachedDisk existingDisk = AttachedDisk.newBuilder()
            .setDeviceName(existingPersistentDiskName)
            .setExistingDisk(diskPath)
            .build();

    return Lists.newArrayList(existingDisk, newDisk);
  }

  // Describes a volume and parameters for it to be mounted to a VM.
  private static Iterable<Volume> volumes(String newPersistentDiskName,
                                          String existingPersistentDiskName) {
    Volume newVolume = Volume.newBuilder()
            .setDeviceName(newPersistentDiskName)
            .setMountPath("/mnt/disks/" + newPersistentDiskName)
            .addMountOptions("rw")
            .addMountOptions("async")
            .build();

    Volume existingVolume = Volume.newBuilder()
            .setDeviceName(existingPersistentDiskName)
            .setMountPath("/mnt/disks/" + existingPersistentDiskName)
            .build();

    return Lists.newArrayList(newVolume, existingVolume);
  }
}

Node.js

Per creare un job batch che utilizzi Persistent Disk nuovi o esistenti utilizzando le librerie client di Cloud per Node.js, utilizza il metodo createJob e includi quanto segue:

// Imports the Batch library
const batchLib = require('@google-cloud/batch');
const batch = batchLib.protos.google.cloud.batch.v1;

// Instantiates a client
const batchClient = new batchLib.v1.BatchServiceClient();

/**
 * TODO(developer): Update these variables before running the sample.
 */
// Project ID or project number of the Google Cloud project you want to use.
const projectId = await batchClient.getProjectId();
// The name of the job that will be created.
// It needs to be unique for each project and region pair.
const jobName = 'batch-create-persistent-disk-job';
// Name of the region you want to use to run the job. Regions that are
// available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
const region = 'europe-central2';
// The name of an existing persistent disk.
const existingPersistentDiskName = 'existing-persistent-disk-name';
// The name of the new persistent disk.
const newPersistentDiskName = 'new-persistent-disk-name';
// The size of the new persistent disk in GB.
// The allowed sizes depend on the type of persistent disk,
// but the minimum is often 10 GB (10) and the maximum is often 64 TB (64000).
const diskSize = 10;
// The location of an existing persistent disk. For more info :
// https://cloud.google.com/batch/docs/create-run-job-storage#gcloud
const location = 'regions/us-central1';
// The disk type of the new persistent disk, either pd-standard,
// pd-balanced, pd-ssd, or pd-extreme. For Batch jobs, the default is pd-balanced.
const newDiskType = 'pd-balanced';

// Define what will be done as part of the job.
const runnable = new batch.Runnable({
  script: new batch.Runnable.Script({
    commands: [
      '-c',
      'echo Hello world! This is task ${BATCH_TASK_INDEX}.' +
        '>> /mnt/disks/NEW_PERSISTENT_DISK_NAME/output_task_${BATCH_TASK_INDEX}.txt',
    ],
  }),
});

// Define volumes and their parameters to be mounted to a VM.
const newVolume = new batch.Volume({
  deviceName: newPersistentDiskName,
  mountPath: `/mnt/disks/${newPersistentDiskName}`,
  mountOptions: ['rw', 'async'],
});

const existingVolume = new batch.Volume({
  deviceName: existingPersistentDiskName,
  mountPath: `/mnt/disks/${existingPersistentDiskName}`,
});

const task = new batch.TaskSpec({
  runnables: [runnable],
  volumes: [newVolume, existingVolume],
  maxRetryCount: 2,
  maxRunDuration: {seconds: 3600},
});

// Tasks are grouped inside a job using TaskGroups.
const group = new batch.TaskGroup({
  taskCount: 3,
  taskSpec: task,
});

const newDisk = new batch.AllocationPolicy.Disk({
  type: newDiskType,
  sizeGb: diskSize,
});

// Policies are used to define on what kind of virtual machines the tasks will run on.
// Read more about local disks here: https://cloud.google.com/compute/docs/disks/persistent-disks
const instancePolicy = new batch.AllocationPolicy.InstancePolicy({
  disks: [
    // Create configuration for new disk
    new batch.AllocationPolicy.AttachedDisk({
      deviceName: newPersistentDiskName,
      newDisk,
    }),
    // Create link to existing disk
    new batch.AllocationPolicy.AttachedDisk({
      existingDisk: `projects/${projectId}/${location}/disks/${existingPersistentDiskName}`,
      deviceName: existingPersistentDiskName,
    }),
  ],
});

const locationPolicy = new batch.AllocationPolicy.LocationPolicy({
  allowedLocations: [location],
});

const allocationPolicy = new batch.AllocationPolicy.InstancePolicyOrTemplate({
  instances: [{policy: instancePolicy}],
  location: locationPolicy,
});

const job = new batch.Job({
  name: jobName,
  taskGroups: [group],
  labels: {env: 'testing', type: 'script'},
  allocationPolicy,
  // We use Cloud Logging as it's an option available out of the box
  logsPolicy: new batch.LogsPolicy({
    destination: batch.LogsPolicy.Destination.CLOUD_LOGGING,
  }),
});
// The job's parent is the project and region in which the job will run
const parent = `projects/${projectId}/locations/${region}`;

async function callCreateBatchPersistentDiskJob() {
  // Construct request
  const request = {
    parent,
    jobId: jobName,
    job,
  };

  // Run request
  const [response] = await batchClient.createJob(request);
  console.log(JSON.stringify(response));
}

await callCreateBatchPersistentDiskJob();

Python

Per creare un job batch che utilizza Persistent Disk nuovi o esistenti utilizzando le librerie client di Cloud per Python, utilizza la funzione CreateJob e includi quanto segue:

  • Per collegare i Persistent Disk alle VM per un job, includi uno dei seguenti elementi:
  • Per montare i Persistent Disk nel job, utilizza la classe Volume con l'attributo device_name e l'attributo mount_path. Per i nuovi Persistent Disk, utilizza anche l'attributo mount_options per attivare la scrittura.

Ad esempio, utilizza il seguente esempio di codice:

from google.cloud import batch_v1


def create_with_pd_job(
    project_id: str,
    region: str,
    job_name: str,
    disk_name: str,
    zone: str,
    existing_disk_name=None,
) -> batch_v1.Job:
    """
    This method shows how to create a sample Batch Job that will run
    a simple command on Cloud Compute instances with mounted persistent disk.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        region: name of the region you want to use to run the job. Regions that are
            available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
        job_name: the name of the job that will be created.
            It needs to be unique for each project and region pair.
        disk_name: name of the disk to be mounted for your Job.
        existing_disk_name(optional): existing disk name, which you want to attach to a job

    Returns:
        A job object representing the job created.
    """
    client = batch_v1.BatchServiceClient()

    # Define what will be done as part of the job.
    task = batch_v1.TaskSpec()
    runnable = batch_v1.Runnable()
    runnable.script = batch_v1.Runnable.Script()
    runnable.script.text = (
        "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/disks/"
        + disk_name
        + "/output_task_${BATCH_TASK_INDEX}.txt"
    )
    task.runnables = [runnable]
    task.max_retry_count = 2
    task.max_run_duration = "3600s"

    volume = batch_v1.Volume()
    volume.device_name = disk_name
    volume.mount_path = f"/mnt/disks/{disk_name}"
    task.volumes = [volume]

    if existing_disk_name:
        volume2 = batch_v1.Volume()
        volume2.device_name = existing_disk_name
        volume2.mount_path = f"/mnt/disks/{existing_disk_name}"
        task.volumes.append(volume2)

    # Tasks are grouped inside a job using TaskGroups.
    # Currently, it's possible to have only one task group.
    group = batch_v1.TaskGroup()
    group.task_count = 4
    group.task_spec = task

    disk = batch_v1.AllocationPolicy.Disk()
    # The disk type of the new persistent disk, either pd-standard,
    # pd-balanced, pd-ssd, or pd-extreme. For Batch jobs, the default is pd-balanced
    disk.type_ = "pd-balanced"
    disk.size_gb = 10

    # Policies are used to define on what kind of virtual machines the tasks will run on.
    # Read more about local disks here: https://cloud.google.com/compute/docs/disks/persistent-disks
    policy = batch_v1.AllocationPolicy.InstancePolicy()
    policy.machine_type = "n1-standard-1"

    attached_disk = batch_v1.AllocationPolicy.AttachedDisk()
    attached_disk.new_disk = disk
    attached_disk.device_name = disk_name
    policy.disks = [attached_disk]

    if existing_disk_name:
        attached_disk2 = batch_v1.AllocationPolicy.AttachedDisk()
        attached_disk2.existing_disk = (
            f"projects/{project_id}/zones/{zone}/disks/{existing_disk_name}"
        )
        attached_disk2.device_name = existing_disk_name
        policy.disks.append(attached_disk2)

    instances = batch_v1.AllocationPolicy.InstancePolicyOrTemplate()
    instances.policy = policy

    allocation_policy = batch_v1.AllocationPolicy()
    allocation_policy.instances = [instances]

    location = batch_v1.AllocationPolicy.LocationPolicy()
    location.allowed_locations = [f"zones/{zone}"]
    allocation_policy.location = location

    job = batch_v1.Job()
    job.task_groups = [group]
    job.allocation_policy = allocation_policy
    job.labels = {"env": "testing", "type": "script"}

    create_request = batch_v1.CreateJobRequest()
    create_request.job = job
    create_request.job_id = job_name
    # The job's parent is the region in which the job will run
    create_request.parent = f"projects/{project_id}/locations/{region}"

    return client.create_job(create_request)

Utilizza un'unità SSD locale

Un job che utilizza SSD locali presenta le seguenti limitazioni:

Puoi creare un job che utilizza un'unità SSD locale utilizzando gcloud CLI, l'API Batch, Java o Python. L'esempio seguente descrive come creare un job che crea, collega e monta un'unità SSD locale. Il job ha anche tre task, ognuno dei quali esegue uno script per creare un file nell'SSD locale denominato output_task_TASK_INDEX.txt, dove TASK_INDEX è l'indice di ogni task: 0, 1 e 2.

gcloud

Per creare un job che utilizza SSD locali con gcloud CLI, utilizza il comando gcloud batch jobs submit. Nel file di configurazione JSON del job, crea e collega gli SSD locali nel campo instances e montali nel campo volumes.

  1. Crea un file JSON.

    • Se non utilizzi un template di istanza per questo job, crea un file JSON con i seguenti contenuti:

      {
          "allocationPolicy": {
              "instances": [
                  {
                      "policy": {
                          "machineType": MACHINE_TYPE,
                          "disks": [
                              {
                                  "newDisk": {
                                      "sizeGb": LOCAL_SSD_SIZE,
                                      "type": "local-ssd"
                                  },
                                  "deviceName": "LOCAL_SSD_NAME"
                              }
                          ]
                      }
                  }
              ]
          },
          "taskGroups": [
              {
                  "taskSpec": {
                      "runnables": [
                          {
                              "script": {
                                  "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/disks/LOCAL_SSD_NAME/output_task_${BATCH_TASK_INDEX}.txt"
                              }
                          }
                      ],
                      "volumes": [
                          {
                              "deviceName": "LOCAL_SSD_NAME",
                              "mountPath": "/mnt/disks/LOCAL_SSD_NAME",
                              "mountOptions": "rw,async"
                          }
                      ]
                  },
                  "taskCount":3
              }
          ],
          "logsPolicy": {
              "destination": "CLOUD_LOGGING"
          }
      }
      

      Sostituisci quanto segue:

      • MACHINE_TYPE: il tipo di macchina, che può essere predefinito o personalizzato, delle VM del job. Il numero consentito di SSD locali dipende dal tipo di macchina delle VM del job.
      • LOCAL_SSD_NAME: il nome di un'unità SSD locale creata per questo job.
      • LOCAL_SSD_SIZE: le dimensioni di tutte le unità SSD locali in GB. Ogni SSD locale ha una capacità di 375 GB, pertanto questo valore deve essere un multiplo di 375 GB. Ad esempio, per 2 SSD locali, imposta questo valore su 750 GB.
    • Se utilizzi un template di istanza VM per questo job, crea un file JSON come mostrato in precedenza, ma sostituisci il campo instances con quanto segue:

      "instances": [
          {
              "instanceTemplate": "INSTANCE_TEMPLATE_NAME"
          }
      ],
      

      dove INSTANCE_TEMPLATE_NAME è il nome del template di istanza per questo job. Per un job che utilizza SSD locali, questo template di istanza deve definire e collegare gli SSD locali che vuoi che vengano utilizzati dal job. Per questo esempio, il template deve definire e collegare un'unità SSD locale denominata LOCAL_SSD_NAME.

  2. Esegui questo comando:

    gcloud batch jobs submit JOB_NAME \
      --location LOCATION \
      --config JSON_CONFIGURATION_FILE
    

    Sostituisci quanto segue:

    • JOB_NAME: il nome del job.
    • LOCATION: la località del job.
    • JSON_CONFIGURATION_FILE: il percorso di un file JSON con i dettagli di configurazione del job.

API

Per creare un job che utilizza SSD locali utilizzando l'API Batch, utilizza il metodo jobs.create. Nella richiesta, crea e collega le unità SSD locali nel campo instances e montale nel campo volumes.

  • Se non utilizzi un template di istanza per questo job, effettua la seguente richiesta:

    POST https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs?job_id=JOB_NAME
    
    {
        "allocationPolicy": {
            "instances": [
                {
                    "policy": {
                        "machineType": MACHINE_TYPE,
                        "disks": [
                            {
                                "newDisk": {
                                    "sizeGb": LOCAL_SSD_SIZE,
                                    "type": "local-ssd"
                                },
                                "deviceName": "LOCAL_SSD_NAME"
                            }
                        ]
                    }
                }
            ]
        },
        "taskGroups": [
            {
                "taskSpec": {
                    "runnables": [
                        {
                            "script": {
                                "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/disks/LOCAL_SSD_NAME/output_task_${BATCH_TASK_INDEX}.txt"
                            }
                        }
                    ],
                    "volumes": [
                        {
                            "deviceName": "LOCAL_SSD_NAME",
                            "mountPath": "/mnt/disks/LOCAL_SSD_NAME",
                            "mountOptions": "rw,async"
                        }
                    ]
                },
                "taskCount":3
            }
        ],
        "logsPolicy": {
            "destination": "CLOUD_LOGGING"
        }
    }
    

    Sostituisci quanto segue:

    • PROJECT_ID: l'ID progetto del tuo progetto.
    • LOCATION: la località del job.
    • JOB_NAME: il nome del job.
    • MACHINE_TYPE: il tipo di macchina, che può essere predefinito o personalizzato, delle VM del job. Il numero consentito di SSD locali dipende dal tipo di macchina delle VM del job.
    • LOCAL_SSD_NAME: il nome di un'unità SSD locale creata per questo job.
    • LOCAL_SSD_SIZE: le dimensioni di tutte le unità SSD locali in GB. Ogni SSD locale ha una dimensione di 375 GB, pertanto questo valore deve essere un multiplo di 375 GB. Ad esempio, per 2 SSD locali, imposta questo valore su 750 GB.
  • Se utilizzi un template di istanza VM per questo job, crea un file JSON come mostrato in precedenza, ma sostituisci il campo instances con quanto segue:

    "instances": [
        {
            "instanceTemplate": "INSTANCE_TEMPLATE_NAME"
        }
    ],
    ...
    

    Dove INSTANCE_TEMPLATE_NAME è il nome del template di istanza per questo job. Per un job che utilizza SSD locali, questo template di istanza deve definire e collegare gli SSD locali che vuoi che vengano utilizzati dal job. Per questo esempio, il template deve definire e collegare un'unità SSD locale denominata LOCAL_SSD_NAME.

Vai

import (
	"context"
	"fmt"
	"io"

	batch "cloud.google.com/go/batch/apiv1"
	"cloud.google.com/go/batch/apiv1/batchpb"
	durationpb "google.golang.org/protobuf/types/known/durationpb"
)

// Creates and runs a job with local SSD
// Note: local SSD does not guarantee Local SSD data persistence.
// More details here: https://cloud.google.com/compute/docs/disks/local-ssd#data_persistence
func createJobWithSSD(w io.Writer, projectID, jobName, ssdName string) error {
	// jobName := job-name
	// ssdName := disk-name
	ctx := context.Background()
	batchClient, err := batch.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("batchClient error: %w", err)
	}
	defer batchClient.Close()

	runn := &batchpb.Runnable{
		Executable: &batchpb.Runnable_Script_{
			Script: &batchpb.Runnable_Script{
				Command: &batchpb.Runnable_Script_Text{
					Text: "echo Hello world from script 1 for task ${BATCH_TASK_INDEX}",
				},
			},
		},
	}
	volume := &batchpb.Volume{
		MountPath: fmt.Sprintf("/mnt/disks/%v", ssdName),
		Source: &batchpb.Volume_DeviceName{
			DeviceName: ssdName,
		},
	}

	// The size of all the local SSDs in GB. Each local SSD is 375 GB,
	// so this value must be a multiple of 375 GB.
	// For example, for 2 local SSDs, set this value to 750 GB.
	disk := &batchpb.AllocationPolicy_Disk{
		Type:   "local-ssd",
		SizeGb: 375,
	}

	taskSpec := &batchpb.TaskSpec{
		ComputeResource: &batchpb.ComputeResource{
			// CpuMilli is milliseconds per cpu-second. This means the task requires 1 CPU.
			CpuMilli:  1000,
			MemoryMib: 16,
		},
		MaxRunDuration: &durationpb.Duration{
			Seconds: 3600,
		},
		MaxRetryCount: 2,
		Runnables:     []*batchpb.Runnable{runn},
		Volumes:       []*batchpb.Volume{volume},
	}

	taskGroups := []*batchpb.TaskGroup{
		{
			TaskCount: 4,
			TaskSpec:  taskSpec,
		},
	}

	labels := map[string]string{"env": "testing", "type": "container"}

	allocationPolicy := &batchpb.AllocationPolicy{
		Instances: []*batchpb.AllocationPolicy_InstancePolicyOrTemplate{{
			PolicyTemplate: &batchpb.AllocationPolicy_InstancePolicyOrTemplate_Policy{
				Policy: &batchpb.AllocationPolicy_InstancePolicy{
					// The allowed number of local SSDs depends on the machine type for your job's VMs.
					// In this case, we tell the system to use "n1-standard-1" machine type, which require to attach local ssd manually.
					// Read more about local disks here: https://cloud.google.com/compute/docs/disks/local-ssd#lssd_disk_options
					MachineType: "n1-standard-1",
					Disks: []*batchpb.AllocationPolicy_AttachedDisk{
						{
							Attached: &batchpb.AllocationPolicy_AttachedDisk_NewDisk{
								NewDisk: disk,
							},
							DeviceName: ssdName,
						},
					},
				},
			},
		}},
	}

	// We use Cloud Logging as it's an out of the box available option
	logsPolicy := &batchpb.LogsPolicy{
		Destination: batchpb.LogsPolicy_CLOUD_LOGGING,
	}

	job := &batchpb.Job{
		Name:             jobName,
		TaskGroups:       taskGroups,
		AllocationPolicy: allocationPolicy,
		Labels:           labels,
		LogsPolicy:       logsPolicy,
	}

	request := &batchpb.CreateJobRequest{
		Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, "us-central1"),
		JobId:  jobName,
		Job:    job,
	}

	created_job, err := batchClient.CreateJob(ctx, request)
	if err != nil {
		return fmt.Errorf("unable to create job: %w", err)
	}

	fmt.Fprintf(w, "Job created: %v\n", created_job)
	return nil
}

Java


import com.google.cloud.batch.v1.AllocationPolicy;
import com.google.cloud.batch.v1.AllocationPolicy.AttachedDisk;
import com.google.cloud.batch.v1.AllocationPolicy.Disk;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicy;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicyOrTemplate;
import com.google.cloud.batch.v1.BatchServiceClient;
import com.google.cloud.batch.v1.CreateJobRequest;
import com.google.cloud.batch.v1.Job;
import com.google.cloud.batch.v1.LogsPolicy;
import com.google.cloud.batch.v1.Runnable;
import com.google.cloud.batch.v1.Runnable.Script;
import com.google.cloud.batch.v1.TaskGroup;
import com.google.cloud.batch.v1.TaskSpec;
import com.google.cloud.batch.v1.Volume;
import com.google.protobuf.Duration;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateLocalSsdJob {

  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project you want to use.
    String projectId = "YOUR_PROJECT_ID";
    // Name of the region you want to use to run the job. Regions that are
    // available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
    String region = "europe-central2";
    // The name of the job that will be created.
    // It needs to be unique for each project and region pair.
    String jobName = "JOB_NAME";
    // The name of a local SSD created for this job.
    String localSsdName = "SSD-NAME";
    // The machine type, which can be predefined or custom, of the job's VMs.
    // The allowed number of local SSDs depends on the machine type
    // for your job's VMs are listed on: https://cloud.google.com/compute/docs/disks#localssds
    String machineType = "c3d-standard-8-lssd";
    // The size of all the local SSDs in GB. Each local SSD is 375 GB,
    // so this value must be a multiple of 375 GB.
    // For example, for 2 local SSDs, set this value to 750 GB.
    int ssdSize = 375;

    createLocalSsdJob(projectId, region, jobName, localSsdName, ssdSize, machineType);
  }

  // Create a job that uses local SSDs
  public static Job createLocalSsdJob(String projectId, String region, String jobName,
                                      String localSsdName, int ssdSize, String machineType)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {
      // Define what will be done as part of the job.
      Runnable runnable =
          Runnable.newBuilder()
              .setScript(
                  Script.newBuilder()
                      .setText(
                          "echo Hello world! This is task ${BATCH_TASK_INDEX}. "
                                  + "This job has a total of ${BATCH_TASK_COUNT} tasks.")
                      // You can also run a script from a file. Just remember, that needs to be a
                      // script that's already on the VM that will be running the job.
                      // Using setText() and setPath() is mutually exclusive.
                      // .setPath("/tmp/test.sh")
                      .build())
              .build();

      Volume volume = Volume.newBuilder()
          .setDeviceName(localSsdName)
          .setMountPath("/mnt/disks/" + localSsdName)
          .addMountOptions("rw")
          .addMountOptions("async")
          .build();

      TaskSpec task = TaskSpec.newBuilder()
          // Jobs can be divided into tasks. In this case, we have only one task.
          .addVolumes(volume)
          .addRunnables(runnable)
          .setMaxRetryCount(2)
          .setMaxRunDuration(Duration.newBuilder().setSeconds(3600).build())
          .build();

      // Tasks are grouped inside a job using TaskGroups.
      // Currently, it's possible to have only one task group.
      TaskGroup taskGroup = TaskGroup.newBuilder()
          .setTaskCount(3)
          .setParallelism(1)
          .setTaskSpec(task)
          .build();

      // Policies are used to define on what kind of virtual machines the tasks will run on.
      InstancePolicy policy = InstancePolicy.newBuilder()
          .setMachineType(machineType)
          .addDisks(AttachedDisk.newBuilder()
              .setDeviceName(localSsdName)
              // For example, local SSD uses type "local-ssd".
              // Persistent disks and boot disks use "pd-balanced", "pd-extreme", "pd-ssd"
              // or "pd-standard".
              .setNewDisk(Disk.newBuilder().setSizeGb(ssdSize).setType("local-ssd")))
          .build();

      AllocationPolicy allocationPolicy =
          AllocationPolicy.newBuilder()
              .addInstances(
                  InstancePolicyOrTemplate.newBuilder()
                      .setPolicy(policy)
                      .build())
              .build();

      Job job =
          Job.newBuilder()
              .addTaskGroups(taskGroup)
              .setAllocationPolicy(allocationPolicy)
              .putLabels("env", "testing")
              .putLabels("type", "script")
              // We use Cloud Logging as it's an out of the box available option.
              .setLogsPolicy(
                  LogsPolicy.newBuilder().setDestination(LogsPolicy.Destination.CLOUD_LOGGING))
              .build();

      CreateJobRequest createJobRequest =
          CreateJobRequest.newBuilder()
              // The job's parent is the region in which the job will run.
              .setParent(String.format("projects/%s/locations/%s", projectId, region))
              .setJob(job)
              .setJobId(jobName)
              .build();

      Job result =
          batchServiceClient
              .createJobCallable()
              .futureCall(createJobRequest)
              .get(5, TimeUnit.MINUTES);

      System.out.printf("Successfully created the job: %s", result.getName());

      return result;
    }
  }
}

Node.js

// Imports the Batch library
const batchLib = require('@google-cloud/batch');
const batch = batchLib.protos.google.cloud.batch.v1;

// Instantiates a client
const batchClient = new batchLib.v1.BatchServiceClient();

/**
 * TODO(developer): Update these variables before running the sample.
 */
// Project ID or project number of the Google Cloud project you want to use.
const projectId = await batchClient.getProjectId();
// Name of the region you want to use to run the job. Regions that are
// available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
const region = 'europe-central2';
// The name of the job that will be created.
// It needs to be unique for each project and region pair.
const jobName = 'batch-local-ssd-job';
// The name of a local SSD created for this job.
const localSsdName = 'ssd-name';
// The machine type, which can be predefined or custom, of the job's VMs.
// The allowed number of local SSDs depends on the machine type
// for your job's VMs are listed on: https://cloud.google.com/compute/docs/disks#localssds
const machineType = 'c3d-standard-8-lssd';
// The size of all the local SSDs in GB. Each local SSD is 375 GB,
// so this value must be a multiple of 375 GB.
// For example, for 2 local SSDs, set this value to 750 GB.
const ssdSize = 375;

// Define what will be done as part of the job.
const runnable = new batch.Runnable({
  script: new batch.Runnable.Script({
    commands: [
      '-c',
      'echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks.',
    ],
  }),
});

const task = new batch.TaskSpec({
  runnables: [runnable],
  maxRetryCount: 2,
  maxRunDuration: {seconds: 3600},
});

// Tasks are grouped inside a job using TaskGroups.
const group = new batch.TaskGroup({
  taskCount: 3,
  taskSpec: task,
});

// Policies are used to define on what kind of virtual machines the tasks will run on.
const instancePolicy = new batch.AllocationPolicy.InstancePolicy({
  machineType,
  disks: [
    new batch.AllocationPolicy.AttachedDisk({
      deviceName: localSsdName,
      // For example, local SSD uses type "local-ssd".
      // Persistent disks and boot disks use "pd-balanced", "pd-extreme", "pd-ssd"
      // or "pd-standard".
      newDisk: new batch.AllocationPolicy.AttachedDisk({
        type: 'local-ssd',
        sizeGb: ssdSize,
      }),
    }),
  ],
});

const allocationPolicy = new batch.AllocationPolicy.InstancePolicyOrTemplate({
  instances: [{policy: instancePolicy}],
});

const job = new batch.Job({
  name: jobName,
  taskGroups: [group],
  labels: {env: 'testing', type: 'script'},
  allocationPolicy,
  // We use Cloud Logging as it's an option available out of the box
  logsPolicy: new batch.LogsPolicy({
    destination: batch.LogsPolicy.Destination.CLOUD_LOGGING,
  }),
});
// The job's parent is the project and region in which the job will run
const parent = `projects/${projectId}/locations/${region}`;

async function callCreateBatchGPUJob() {
  // Construct request
  const request = {
    parent,
    jobId: jobName,
    job,
  };

  // Run request
  const [response] = await batchClient.createJob(request);
  console.log(JSON.stringify(response));
}

await callCreateBatchGPUJob();

Python

from google.cloud import batch_v1


def create_local_ssd_job(
    project_id: str, region: str, job_name: str, ssd_name: str
) -> batch_v1.Job:
    """
    This method shows how to create a sample Batch Job that will run
    a simple command on Cloud Compute instances with mounted local SSD.
    Note: local SSD does not guarantee Local SSD data persistence.
    More details here: https://cloud.google.com/compute/docs/disks/local-ssd#data_persistence

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        region: name of the region you want to use to run the job. Regions that are
            available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
        job_name: the name of the job that will be created.
            It needs to be unique for each project and region pair.
        ssd_name: name of the local ssd to be mounted for your Job.

    Returns:
        A job object representing the job created.
    """
    client = batch_v1.BatchServiceClient()

    # Define what will be done as part of the job.
    task = batch_v1.TaskSpec()
    runnable = batch_v1.Runnable()
    runnable.script = batch_v1.Runnable.Script()
    runnable.script.text = "echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."
    task.runnables = [runnable]
    task.max_retry_count = 2
    task.max_run_duration = "3600s"

    volume = batch_v1.Volume()
    volume.device_name = ssd_name
    volume.mount_path = f"/mnt/disks/{ssd_name}"
    task.volumes = [volume]

    # Tasks are grouped inside a job using TaskGroups.
    # Currently, it's possible to have only one task group.
    group = batch_v1.TaskGroup()
    group.task_count = 4
    group.task_spec = task

    disk = batch_v1.AllocationPolicy.Disk()
    disk.type_ = "local-ssd"
    # The size of all the local SSDs in GB. Each local SSD is 375 GB,
    # so this value must be a multiple of 375 GB.
    # For example, for 2 local SSDs, set this value to 750 GB.
    disk.size_gb = 375
    assert disk.size_gb % 375 == 0

    # Policies are used to define on what kind of virtual machines the tasks will run on.
    # The allowed number of local SSDs depends on the machine type for your job's VMs.
    # In this case, we tell the system to use "n1-standard-1" machine type, which require to attach local ssd manually.
    # Read more about local disks here: https://cloud.google.com/compute/docs/disks/local-ssd#lssd_disk_options
    policy = batch_v1.AllocationPolicy.InstancePolicy()
    policy.machine_type = "n1-standard-1"

    attached_disk = batch_v1.AllocationPolicy.AttachedDisk()
    attached_disk.new_disk = disk
    attached_disk.device_name = ssd_name
    policy.disks = [attached_disk]

    instances = batch_v1.AllocationPolicy.InstancePolicyOrTemplate()
    instances.policy = policy

    allocation_policy = batch_v1.AllocationPolicy()
    allocation_policy.instances = [instances]

    job = batch_v1.Job()
    job.task_groups = [group]
    job.allocation_policy = allocation_policy
    job.labels = {"env": "testing", "type": "script"}
    # We use Cloud Logging as it's an out of the box available option
    job.logs_policy = batch_v1.LogsPolicy()
    job.logs_policy.destination = batch_v1.LogsPolicy.Destination.CLOUD_LOGGING

    create_request = batch_v1.CreateJobRequest()
    create_request.job = job
    create_request.job_id = job_name
    # The job's parent is the region in which the job will run
    create_request.parent = f"projects/{project_id}/locations/{region}"

    return client.create_job(create_request)

Utilizza un bucket Cloud Storage

Per creare un job che utilizza un bucket Cloud Storage esistente, seleziona uno dei seguenti metodi:

  • Consigliato: monta un bucket direttamente sulle VM del job specificando il bucket nella definizione del job, come mostrato in questa sezione. Quando il job viene eseguito, il bucket viene montato automaticamente sulle VM per il job utilizzando Cloud Storage FUSE.
  • Crea un job con attività che accedono direttamente a un bucket Cloud Storage utilizzando la gcloud CLI o le librerie client per l'API Storage. Per scoprire come accedere a un bucket Cloud Storage direttamente da una VM, consulta la documentazione di Compute Engine per la scrittura e la lettura di dati dai bucket Cloud Storage.

Prima di creare un job che utilizza un bucket, crea un bucket o identificane uno esistente. Per saperne di più, consulta Crea bucket ed Elenca bucket.

Puoi creare un job che utilizza un bucket Cloud Storage utilizzando la consoleGoogle Cloud , gcloud CLI, l'API Batch, C++, Go, Java, Node.js o Python.

L'esempio seguente descrive come creare un job che monta un bucket Cloud Storage. Il job ha anche 3 task, ognuno dei quali esegue uno script per creare un file nel bucket denominato output_task_TASK_INDEX.txt, dove TASK_INDEX è l'indice di ogni task: 0, 1 e 2.

Console

Per creare un job che utilizza un bucket Cloud Storage utilizzando la consoleGoogle Cloud , segui questi passaggi:

  1. Nella console Google Cloud , vai alla pagina Elenco job.

    Vai all'elenco dei job

  2. Fai clic su Crea. Viene visualizzata la pagina Crea job batch. Nel riquadro a sinistra, è selezionata la pagina Dettagli job.

  3. Configura la pagina Dettagli job:

    1. (Facoltativo) Nel campo Nome job, personalizza il nome del job.

      Ad esempio, inserisci example-bucket-job.

    2. Configura la sezione Dettagli attività:

      1. Nella finestra Nuovo eseguibile, aggiungi almeno uno script o un container per l'esecuzione di questo job.

        Ad esempio, segui questi passaggi:

        1. Seleziona la casella di controllo Script. Viene visualizzato un campo.

        2. Nel campo, inserisci il seguente script:

          echo Hello world from task ${BATCH_TASK_INDEX}. >> MOUNT_PATH/output_task_${BATCH_TASK_INDEX}.txt
          

          Sostituisci MOUNT_PATH con il percorso di montaggio utilizzato dagli eseguibili di questo job per accedere a un bucket Cloud Storage esistente. Il percorso deve iniziare con /mnt/disks/ seguito da una directory o un percorso di tua scelta. Ad esempio, se vuoi rappresentare questo bucket con una directory denominata my-bucket, imposta il percorso di montaggio su /mnt/disks/my-bucket.

        3. Fai clic su Fine.

      2. Nel campo Numero di attività, inserisci il numero di attività per questo job.

        Ad esempio, inserisci 3.

      3. Nel campo Parallelismo, inserisci il numero di attività da eseguire contemporaneamente.

        Ad esempio, inserisci 1 (impostazione predefinita).

  4. Configura la pagina Configurazioni aggiuntive:

    1. Nel riquadro a sinistra, fai clic su Configurazioni aggiuntive. Si apre la pagina Configurazioni aggiuntive.

    2. Per ogni bucket Cloud Storage che vuoi montare in questo job, segui questi passaggi:

      1. Nella sezione Volume di archiviazione, fai clic su Aggiungi nuovo volume. Viene visualizzata la finestra Nuovo volume.

      2. Nella finestra Nuovo volume, segui questi passaggi:

        1. Nella sezione Tipo di volume, seleziona Bucket Cloud Storage.

        2. Nel campo Nome bucket di archiviazione, inserisci il nome di un bucket esistente.

          Ad esempio, inserisci il bucket specificato nell'eseguibile di questo job.

        3. Nel campo Percorso di montaggio, inserisci il percorso di montaggio del bucket (MOUNT_PATH) che hai specificato nell'eseguibile.

        4. Fai clic su Fine.

  5. (Facoltativo) Configura gli altri campi per questo job.

  6. (Facoltativo) Per esaminare la configurazione del job, fai clic su Anteprima nel riquadro a sinistra.

  7. Fai clic su Crea.

Nella pagina Dettagli job viene visualizzato il job che hai creato.

gcloud

Per creare un job che utilizza un bucket Cloud Storage con gcloud CLI, utilizza il comando gcloud batch jobs submit. Nel file di configurazione JSON del job, monta il bucket nel campo volumes.

Ad esempio, per creare un job che restituisce file in un bucket Cloud Storage:

  1. Crea un file JSON con i seguenti contenuti:

    {
        "taskGroups": [
            {
                "taskSpec": {
                    "runnables": [
                        {
                            "script": {
                                "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> MOUNT_PATH/output_task_${BATCH_TASK_INDEX}.txt"
                            }
                        }
                    ],
                    "volumes": [
                        {
                            "gcs": {
                                "remotePath": "BUCKET_PATH"
                            },
                            "mountPath": "MOUNT_PATH"
                        }
                    ]
                },
                "taskCount": 3
            }
        ],
        "logsPolicy": {
            "destination": "CLOUD_LOGGING"
        }
    }
    

    Sostituisci quanto segue:

    • BUCKET_PATH: il percorso della directory del bucket a cui vuoi che acceda questo job, che deve iniziare con il nome del bucket. Ad esempio, per un bucket denominato BUCKET_NAME, il percorso BUCKET_NAME rappresenta la directory root del bucket e il percorso BUCKET_NAME/subdirectory rappresenta la sottodirectory subdirectory.
    • MOUNT_PATH: il percorso di montaggio utilizzato dagli elementi eseguibili del job per accedere a questo bucket. Il percorso deve iniziare con /mnt/disks/ seguito da una directory o un percorso di tua scelta. Ad esempio, se vuoi rappresentare questo bucket con una directory denominata my-bucket, imposta il percorso di montaggio su /mnt/disks/my-bucket.
  2. Esegui questo comando:

    gcloud batch jobs submit JOB_NAME \
      --location LOCATION \
      --config JSON_CONFIGURATION_FILE
    

    Sostituisci quanto segue:

    • JOB_NAME: il nome del job.
    • LOCATION: la località del job.
    • JSON_CONFIGURATION_FILE: il percorso di un file JSON con i dettagli di configurazione del job.

API

Per creare un job che utilizza un bucket Cloud Storage utilizzando l'API Batch, utilizza il metodo jobs.create e monta il bucket nel campo volumes.

POST https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs?job_id=JOB_NAME

{
    "taskGroups": [
        {
            "taskSpec": {
                "runnables": [
                    {
                        "script": {
                            "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> MOUNT_PATH/output_task_${BATCH_TASK_INDEX}.txt"
                        }
                    }
                ],
                "volumes": [
                    {
                        "gcs": {
                            "remotePath": "BUCKET_PATH"
                        },
                        "mountPath": "MOUNT_PATH"
                    }
                ]
            },
            "taskCount": 3
        }
    ],
    "logsPolicy": {
            "destination": "CLOUD_LOGGING"
    }
}

Sostituisci quanto segue:

  • PROJECT_ID: l'ID progetto del tuo progetto.
  • LOCATION: la località del job.
  • JOB_NAME: il nome del job.
  • BUCKET_PATH: il percorso della directory del bucket a cui vuoi che acceda questo job, che deve iniziare con il nome del bucket. Ad esempio, per un bucket denominato BUCKET_NAME, il percorso BUCKET_NAME rappresenta la directory root del bucket e il percorso BUCKET_NAME/subdirectory rappresenta la sottodirectory subdirectory.
  • MOUNT_PATH: il percorso di montaggio utilizzato dagli elementi eseguibili del job per accedere a questo bucket. Il percorso deve iniziare con /mnt/disks/ seguito da una directory o un percorso di tua scelta. Ad esempio, se vuoi rappresentare questo bucket con una directory denominata my-bucket, imposta il percorso di montaggio su /mnt/disks/my-bucket.

C++

C++

Per saperne di più, consulta la documentazione di riferimento dell'API Batch C++.

Per eseguire l'autenticazione in Batch, configura le Credenziali predefinite dell'applicazione. Per saperne di più, consulta Configura l'autenticazione per un ambiente di sviluppo locale.

#include "google/cloud/batch/v1/batch_client.h"

  [](std::string const& project_id, std::string const& location_id,
     std::string const& job_id, std::string const& bucket_name) {
    // Initialize the request; start with the fields that depend on the sample
    // input.
    google::cloud::batch::v1::CreateJobRequest request;
    request.set_parent("projects/" + project_id + "/locations/" + location_id);
    request.set_job_id(job_id);
    // Most of the job description is fixed in this example; use a string to
    // initialize it, and then override the GCS remote path.
    auto constexpr kText = R"pb(
      task_groups {
        task_count: 4
        task_spec {
          compute_resource { cpu_milli: 500 memory_mib: 16 }
          max_retry_count: 2
          max_run_duration { seconds: 3600 }
          runnables {
            script {
              text: "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/share/output_task_${BATCH_TASK_INDEX}.txt"
            }
          }
          volumes { mount_path: "/mnt/share" }
        }
      }
      allocation_policy {
        instances {
          policy { machine_type: "e2-standard-4" provisioning_model: STANDARD }
        }
      }
      labels { key: "env" value: "testing" }
      labels { key: "type" value: "script" }
      logs_policy { destination: CLOUD_LOGGING }
    )pb";
    auto* job = request.mutable_job();
    if (!google::protobuf::TextFormat::ParseFromString(kText, job)) {
      throw std::runtime_error("Error parsing Job description");
    }
    job->mutable_task_groups(0)
        ->mutable_task_spec()
        ->mutable_volumes(0)
        ->mutable_gcs()
        ->set_remote_path(bucket_name);
    // Create a client and issue the request.
    auto client = google::cloud::batch_v1::BatchServiceClient(
        google::cloud::batch_v1::MakeBatchServiceConnection());
    auto response = client.CreateJob(request);
    if (!response) throw std::move(response).status();
    std::cout << "Job : " << response->DebugString() << "\n";
  }

Vai

Go

Per saperne di più, consulta la documentazione di riferimento dell'API Batch Go.

Per eseguire l'autenticazione in Batch, configura le Credenziali predefinite dell'applicazione. Per saperne di più, consulta Configura l'autenticazione per un ambiente di sviluppo locale.

import (
	"context"
	"fmt"
	"io"

	batch "cloud.google.com/go/batch/apiv1"
	"cloud.google.com/go/batch/apiv1/batchpb"
	durationpb "google.golang.org/protobuf/types/known/durationpb"
)

// Creates and runs a job that executes the specified script
func createScriptJobWithBucket(w io.Writer, projectID, region, jobName, bucketName string) error {
	// projectID := "your_project_id"
	// region := "us-central1"
	// jobName := "some-job"
	// jobName := "some-bucket"

	ctx := context.Background()
	batchClient, err := batch.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient: %w", err)
	}
	defer batchClient.Close()

	// Define what will be done as part of the job.
	command := &batchpb.Runnable_Script_Text{
		Text: "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/share/output_task_${BATCH_TASK_INDEX}.txt",
	}

	// Specify the Google Cloud Storage bucket to mount
	volume := &batchpb.Volume{
		Source: &batchpb.Volume_Gcs{
			Gcs: &batchpb.GCS{
				RemotePath: bucketName,
			},
		},
		MountPath:    "/mnt/share",
		MountOptions: []string{},
	}

	// We can specify what resources are requested by each task.
	resources := &batchpb.ComputeResource{
		// CpuMilli is milliseconds per cpu-second. This means the task requires 50% of a single CPUs.
		CpuMilli:  500,
		MemoryMib: 16,
	}

	taskSpec := &batchpb.TaskSpec{
		Runnables: []*batchpb.Runnable{{
			Executable: &batchpb.Runnable_Script_{
				Script: &batchpb.Runnable_Script{Command: command},
			},
		}},
		ComputeResource: resources,
		MaxRunDuration: &durationpb.Duration{
			Seconds: 3600,
		},
		MaxRetryCount: 2,
		Volumes:       []*batchpb.Volume{volume},
	}

	// Tasks are grouped inside a job using TaskGroups.
	taskGroups := []*batchpb.TaskGroup{
		{
			TaskCount: 4,
			TaskSpec:  taskSpec,
		},
	}

	// Policies are used to define on what kind of virtual machines the tasks will run on.
	// In this case, we tell the system to use "e2-standard-4" machine type.
	// Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
	allocationPolicy := &batchpb.AllocationPolicy{
		Instances: []*batchpb.AllocationPolicy_InstancePolicyOrTemplate{{
			PolicyTemplate: &batchpb.AllocationPolicy_InstancePolicyOrTemplate_Policy{
				Policy: &batchpb.AllocationPolicy_InstancePolicy{
					MachineType: "e2-standard-4",
				},
			},
		}},
	}

	// We use Cloud Logging as it's an out of the box available option
	logsPolicy := &batchpb.LogsPolicy{
		Destination: batchpb.LogsPolicy_CLOUD_LOGGING,
	}

	jobLabels := map[string]string{"env": "testing", "type": "script"}

	// The job's parent is the region in which the job will run
	parent := fmt.Sprintf("projects/%s/locations/%s", projectID, region)

	job := batchpb.Job{
		TaskGroups:       taskGroups,
		AllocationPolicy: allocationPolicy,
		Labels:           jobLabels,
		LogsPolicy:       logsPolicy,
	}

	req := &batchpb.CreateJobRequest{
		Parent: parent,
		JobId:  jobName,
		Job:    &job,
	}

	created_job, err := batchClient.CreateJob(ctx, req)
	if err != nil {
		return fmt.Errorf("unable to create job: %w", err)
	}

	fmt.Fprintf(w, "Job created: %v\n", created_job)

	return nil
}

Java

Java

Per saperne di più, consulta la documentazione di riferimento dell'API Batch Java.

Per eseguire l'autenticazione in Batch, configura le Credenziali predefinite dell'applicazione. Per saperne di più, consulta Configura l'autenticazione per un ambiente di sviluppo locale.

import com.google.cloud.batch.v1.AllocationPolicy;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicy;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicyOrTemplate;
import com.google.cloud.batch.v1.BatchServiceClient;
import com.google.cloud.batch.v1.ComputeResource;
import com.google.cloud.batch.v1.CreateJobRequest;
import com.google.cloud.batch.v1.GCS;
import com.google.cloud.batch.v1.Job;
import com.google.cloud.batch.v1.LogsPolicy;
import com.google.cloud.batch.v1.LogsPolicy.Destination;
import com.google.cloud.batch.v1.Runnable;
import com.google.cloud.batch.v1.Runnable.Script;
import com.google.cloud.batch.v1.TaskGroup;
import com.google.cloud.batch.v1.TaskSpec;
import com.google.cloud.batch.v1.Volume;
import com.google.protobuf.Duration;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateWithMountedBucket {

  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Cloud project you want to use.
    String projectId = "YOUR_PROJECT_ID";

    // Name of the region you want to use to run the job. Regions that are
    // available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
    String region = "europe-central2";

    // The name of the job that will be created.
    // It needs to be unique for each project and region pair.
    String jobName = "JOB_NAME";

    // Name of the bucket to be mounted for your Job.
    String bucketName = "BUCKET_NAME";

    createScriptJobWithBucket(projectId, region, jobName, bucketName);
  }

  // This method shows how to create a sample Batch Job that will run
  // a simple command on Cloud Compute instances.
  public static void createScriptJobWithBucket(String projectId, String region, String jobName,
      String bucketName)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the `batchServiceClient.close()` method on the client to safely
    // clean up any remaining background resources.
    try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {

      // Define what will be done as part of the job.
      Runnable runnable =
          Runnable.newBuilder()
              .setScript(
                  Script.newBuilder()
                      .setText(
                          "echo Hello world from task ${BATCH_TASK_INDEX}. >> "
                              + "/mnt/share/output_task_${BATCH_TASK_INDEX}.txt")
                      // You can also run a script from a file. Just remember, that needs to be a
                      // script that's already on the VM that will be running the job.
                      // Using setText() and setPath() is mutually exclusive.
                      // .setPath("/tmp/test.sh")
                      .build())
              .build();

      Volume volume = Volume.newBuilder()
          .setGcs(GCS.newBuilder()
              .setRemotePath(bucketName)
              .build())
          .setMountPath("/mnt/share")
          .build();

      // We can specify what resources are requested by each task.
      ComputeResource computeResource =
          ComputeResource.newBuilder()
              // In milliseconds per cpu-second. This means the task requires 50% of a single CPUs.
              .setCpuMilli(500)
              // In MiB.
              .setMemoryMib(16)
              .build();

      TaskSpec task =
          TaskSpec.newBuilder()
              // Jobs can be divided into tasks. In this case, we have only one task.
              .addRunnables(runnable)
              .addVolumes(volume)
              .setComputeResource(computeResource)
              .setMaxRetryCount(2)
              .setMaxRunDuration(Duration.newBuilder().setSeconds(3600).build())
              .build();

      // Tasks are grouped inside a job using TaskGroups.
      // Currently, it's possible to have only one task group.
      TaskGroup taskGroup = TaskGroup.newBuilder().setTaskCount(4).setTaskSpec(task).build();

      // Policies are used to define on what kind of virtual machines the tasks will run on.
      // In this case, we tell the system to use "e2-standard-4" machine type.
      // Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
      InstancePolicy instancePolicy =
          InstancePolicy.newBuilder().setMachineType("e2-standard-4").build();

      AllocationPolicy allocationPolicy =
          AllocationPolicy.newBuilder()
              .addInstances(InstancePolicyOrTemplate.newBuilder().setPolicy(instancePolicy).build())
              .build();

      Job job =
          Job.newBuilder()
              .addTaskGroups(taskGroup)
              .setAllocationPolicy(allocationPolicy)
              .putLabels("env", "testing")
              .putLabels("type", "script")
              .putLabels("mount", "bucket")
              // We use Cloud Logging as it's an out of the box available option.
              .setLogsPolicy(
                  LogsPolicy.newBuilder().setDestination(Destination.CLOUD_LOGGING).build())
              .build();

      CreateJobRequest createJobRequest =
          CreateJobRequest.newBuilder()
              // The job's parent is the region in which the job will run.
              .setParent(String.format("projects/%s/locations/%s", projectId, region))
              .setJob(job)
              .setJobId(jobName)
              .build();

      Job result =
          batchServiceClient
              .createJobCallable()
              .futureCall(createJobRequest)
              .get(5, TimeUnit.MINUTES);

      System.out.printf("Successfully created the job: %s", result.getName());
    }
  }
}

Node.js

Node.js

Per saperne di più, consulta la documentazione di riferimento dell'API Batch Node.js.

Per eseguire l'autenticazione in Batch, configura le Credenziali predefinite dell'applicazione. Per saperne di più, consulta Configura l'autenticazione per un ambiente di sviluppo locale.

/**
 * TODO(developer): Uncomment and replace these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
/**
 * The region you want to the job to run in. The regions that support Batch are listed here:
 * https://cloud.google.com/batch/docs/get-started#locations
 */
// const region = 'us-central-1';
/**
 * The name of the job that will be created.
 * It needs to be unique for each project and region pair.
 */
// const jobName = 'YOUR_JOB_NAME';
/**
 * The name of the bucket to be mounted.
 */
// const bucketName = 'YOUR_BUCKET_NAME';

// Imports the Batch library
const batchLib = require('@google-cloud/batch');
const batch = batchLib.protos.google.cloud.batch.v1;

// Instantiates a client
const batchClient = new batchLib.v1.BatchServiceClient();

// Define what will be done as part of the job.
const task = new batch.TaskSpec();
const runnable = new batch.Runnable();
runnable.script = new batch.Runnable.Script();
runnable.script.text =
  'echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/share/output_task_${BATCH_TASK_INDEX}.txt';
// You can also run a script from a file. Just remember, that needs to be a script that's
// already on the VM that will be running the job. Using runnable.script.text and runnable.script.path is mutually
// exclusive.
// runnable.script.path = '/tmp/test.sh'
task.runnables = [runnable];

const gcsBucket = new batch.GCS();
gcsBucket.remotePath = bucketName;
const gcsVolume = new batch.Volume();
gcsVolume.gcs = gcsBucket;
gcsVolume.mountPath = '/mnt/share';
task.volumes = [gcsVolume];

// We can specify what resources are requested by each task.
const resources = new batch.ComputeResource();
resources.cpuMilli = 2000; // in milliseconds per cpu-second. This means the task requires 2 whole CPUs.
resources.memoryMib = 16;
task.computeResource = resources;

task.maxRetryCount = 2;
task.maxRunDuration = {seconds: 3600};

// Tasks are grouped inside a job using TaskGroups.
const group = new batch.TaskGroup();
group.taskCount = 4;
group.taskSpec = task;

// Policies are used to define on what kind of virtual machines the tasks will run on.
// In this case, we tell the system to use "e2-standard-4" machine type.
// Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
const allocationPolicy = new batch.AllocationPolicy();
const policy = new batch.AllocationPolicy.InstancePolicy();
policy.machineType = 'e2-standard-4';
const instances = new batch.AllocationPolicy.InstancePolicyOrTemplate();
instances.policy = policy;
allocationPolicy.instances = [instances];

const job = new batch.Job();
job.name = jobName;
job.taskGroups = [group];
job.allocationPolicy = allocationPolicy;
job.labels = {env: 'testing', type: 'script'};
// We use Cloud Logging as it's an option available out of the box
job.logsPolicy = new batch.LogsPolicy();
job.logsPolicy.destination = batch.LogsPolicy.Destination.CLOUD_LOGGING;

// The job's parent is the project and region in which the job will run
const parent = `projects/${projectId}/locations/${region}`;

async function callCreateJob() {
  // Construct request
  const request = {
    parent,
    jobId: jobName,
    job,
  };

  // Run request
  const response = await batchClient.createJob(request);
  console.log(response);
}

await callCreateJob();

Python

Python

Per saperne di più, consulta la documentazione di riferimento dell'API Batch Python.

Per eseguire l'autenticazione in Batch, configura le Credenziali predefinite dell'applicazione. Per saperne di più, consulta Configura l'autenticazione per un ambiente di sviluppo locale.

from google.cloud import batch_v1


def create_script_job_with_bucket(
    project_id: str, region: str, job_name: str, bucket_name: str
) -> batch_v1.Job:
    """
    This method shows how to create a sample Batch Job that will run
    a simple command on Cloud Compute instances.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        region: name of the region you want to use to run the job. Regions that are
            available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
        job_name: the name of the job that will be created.
            It needs to be unique for each project and region pair.
        bucket_name: name of the bucket to be mounted for your Job.

    Returns:
        A job object representing the job created.
    """
    client = batch_v1.BatchServiceClient()

    # Define what will be done as part of the job.
    task = batch_v1.TaskSpec()
    runnable = batch_v1.Runnable()
    runnable.script = batch_v1.Runnable.Script()
    runnable.script.text = "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/share/output_task_${BATCH_TASK_INDEX}.txt"
    task.runnables = [runnable]

    gcs_bucket = batch_v1.GCS()
    gcs_bucket.remote_path = bucket_name
    gcs_volume = batch_v1.Volume()
    gcs_volume.gcs = gcs_bucket
    gcs_volume.mount_path = "/mnt/share"
    task.volumes = [gcs_volume]

    # We can specify what resources are requested by each task.
    resources = batch_v1.ComputeResource()
    resources.cpu_milli = 500  # in milliseconds per cpu-second. This means the task requires 50% of a single CPUs.
    resources.memory_mib = 16
    task.compute_resource = resources

    task.max_retry_count = 2
    task.max_run_duration = "3600s"

    # Tasks are grouped inside a job using TaskGroups.
    # Currently, it's possible to have only one task group.
    group = batch_v1.TaskGroup()
    group.task_count = 4
    group.task_spec = task

    # Policies are used to define on what kind of virtual machines the tasks will run on.
    # In this case, we tell the system to use "e2-standard-4" machine type.
    # Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
    allocation_policy = batch_v1.AllocationPolicy()
    policy = batch_v1.AllocationPolicy.InstancePolicy()
    policy.machine_type = "e2-standard-4"
    instances = batch_v1.AllocationPolicy.InstancePolicyOrTemplate()
    instances.policy = policy
    allocation_policy.instances = [instances]

    job = batch_v1.Job()
    job.task_groups = [group]
    job.allocation_policy = allocation_policy
    job.labels = {"env": "testing", "type": "script", "mount": "bucket"}
    # We use Cloud Logging as it's an out of the box available option
    job.logs_policy = batch_v1.LogsPolicy()
    job.logs_policy.destination = batch_v1.LogsPolicy.Destination.CLOUD_LOGGING

    create_request = batch_v1.CreateJobRequest()
    create_request.job = job
    create_request.job_id = job_name
    # The job's parent is the region in which the job will run
    create_request.parent = f"projects/{project_id}/locations/{region}"

    return client.create_job(create_request)

Utilizza un file system di rete

Puoi creare un job che utilizza un Network File System (NFS) esistente, ad esempio una condivisione file di Filestore, utilizzando la console Google Cloud , gcloud CLI o l'API Batch.

Prima di creare un job che utilizza un NFS, assicurati che il firewall della tua rete sia configurato correttamente per consentire il traffico tra le VM del job e l'NFS. Per saperne di più, consulta Configura le regole firewall per Filestore.

L'esempio seguente descrive come creare un job che specifica e monta un NFS. Il job ha anche 3 task che eseguono ciascuno uno script per creare un file nell'NFS denominato output_task_TASK_INDEX.txt dove TASK_INDEX è l'indice di ogni task: 0, 1 e 2.

Console

Per creare un job che utilizza un NFS utilizzando la console Google Cloud , segui questi passaggi:

  1. Nella console Google Cloud , vai alla pagina Elenco job.

    Vai all'elenco dei job

  2. Fai clic su Crea. Viene visualizzata la pagina Crea job batch. Nel riquadro a sinistra, è selezionata la pagina Dettagli job.

  3. Configura la pagina Dettagli job:

    1. (Facoltativo) Nel campo Nome job, personalizza il nome del job.

      Ad esempio, inserisci example-nfs-job.

    2. Configura la sezione Dettagli attività:

      1. Nella finestra Nuovo eseguibile, aggiungi almeno uno script o un container per l'esecuzione di questo job.

        Ad esempio, segui questi passaggi:

        1. Seleziona la casella di controllo Script. Viene visualizzato un campo.

        2. Nel campo, inserisci il seguente script:

          echo Hello world from task ${BATCH_TASK_INDEX}. >> MOUNT_PATH/output_task_${BATCH_TASK_INDEX}.txt
          

          Sostituisci MOUNT_PATH con il percorso di montaggio utilizzato dall'eseguibile del job per accedere a questo NFS. Il percorso deve iniziare con /mnt/disks/ seguito da una directory o un percorso di tua scelta. Ad esempio, se vuoi rappresentare questo NFS con una directory denominata my-nfs, imposta il percorso di montaggio su /mnt/disks/my-nfs.

        3. Fai clic su Fine.

      2. Nel campo Numero di attività, inserisci il numero di attività per questo job.

        Ad esempio, inserisci 3.

      3. Nel campo Parallelismo, inserisci il numero di attività da eseguire contemporaneamente.

        Ad esempio, inserisci 1 (impostazione predefinita).

  4. Configura la pagina Configurazioni aggiuntive:

    1. Nel riquadro a sinistra, fai clic su Configurazioni aggiuntive. Si apre la pagina Configurazioni aggiuntive.

    2. Per ogni NFS che vuoi montare in questo job, segui questi passaggi:

      1. Nella sezione Volume di archiviazione, fai clic su Aggiungi nuovo volume. Viene visualizzata la finestra Nuovo volume.

      2. Nella finestra Nuovo volume, segui questi passaggi:

        1. Nella sezione Tipo di volume, seleziona Network File System.

        2. Nel campo File server, inserisci l'indirizzo IP del server in cui si trova l'NFS specificato nell'eseguibile di questo job.

          Ad esempio, se l'NFS è una condivisione file di Filestore, specifica l'indirizzo IP dell'istanza di Filestore, che puoi ottenere descrivendo l'istanza di Filestore.

        3. Nel campo Percorso remoto, inserisci un percorso che possa accedere all'NFS specificato nel passaggio precedente.

          Il percorso della directory NFS deve iniziare con / seguito dalla directory root di NFS.

        4. Nel campo Percorso di montaggio, inserisci il percorso di montaggio per l'NFS (MOUNT_PATH) che hai specificato nel passaggio precedente.

    3. Fai clic su Fine.

  5. (Facoltativo) Configura gli altri campi per questo job.

  6. (Facoltativo) Per esaminare la configurazione del job, fai clic su Anteprima nel riquadro a sinistra.

  7. Fai clic su Crea.

Nella pagina Dettagli job viene visualizzato il job che hai creato.

gcloud

Per creare un job che utilizza un NFS con gcloud CLI, utilizza il comando gcloud batch jobs submit. Nel file di configurazione JSON del job, monta l'NFS nel campo volumes.

  1. Crea un file JSON con i seguenti contenuti:

    {
        "taskGroups": [
            {
                "taskSpec": {
                    "runnables": [
                        {
                            "script": {
                                "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> MOUNT_PATH/output_task_${BATCH_TASK_INDEX}.txt"
                            }
                        }
                    ],
                    "volumes": [
                        {
                            "nfs": {
                                "server": "NFS_IP_ADDRESS",
                                "remotePath": "NFS_PATH"
                            },
                            "mountPath": "MOUNT_PATH"
                        }
                    ]
                },
                "taskCount": 3
            }
        ],
        "logsPolicy": {
            "destination": "CLOUD_LOGGING"
        }
    }
    

    Sostituisci quanto segue:

    • NFS_IP_ADDRESS: l'indirizzo IP dell'NFS. Ad esempio, se il tuo NFS è una condivisione file di Filestore, specifica l'indirizzo IP dell'istanza Filestore, che puoi ottenere descrivendo l'istanza Filestore.
    • NFS_PATH: il percorso della directory NFS a cui deve accedere questo job, che deve iniziare con / seguito dalla directory root di NFS. Ad esempio, per una condivisione file Filestore denominata FILE_SHARE_NAME, il percorso /FILE_SHARE_NAME rappresenta la directory root della condivisione file e il percorso /FILE_SHARE_NAME/subdirectory rappresenta la sottodirectory subdirectory.
    • MOUNT_PATH: il percorso di montaggio utilizzato dagli elementi eseguibili del job per accedere a questo NFS. Il percorso deve iniziare con /mnt/disks/ seguito da una directory o un percorso di tua scelta. Ad esempio, se vuoi rappresentare questo NFS con una directory denominata my-nfs, imposta il percorso di montaggio su /mnt/disks/my-nfs.
  2. Esegui questo comando:

    gcloud batch jobs submit JOB_NAME \
      --location LOCATION \
      --config JSON_CONFIGURATION_FILE
    

    Sostituisci quanto segue:

    • JOB_NAME: il nome del job.
    • LOCATION: la località del job.
    • JSON_CONFIGURATION_FILE: il percorso di un file JSON con i dettagli di configurazione del job.

API

Per creare un job che utilizza un NFS tramite l'API Batch, utilizza il metodo jobs.create e monta l'NFS nel campo volumes.

POST https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs?job_id=JOB_NAME

   {
    "taskGroups": [
        {
            "taskSpec": {
                "runnables": [
                    {
                        "script": {
                            "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> MOUNT_PATH/output_task_${BATCH_TASK_INDEX}.txt"
                        }
                    }
                ],
                "volumes": [
                    {
                        "nfs": {
                            "server": "NFS_IP_ADDRESS",
                            "remotePath": "NFS_PATH"
                        },
                        "mountPath": "MOUNT_PATH"
                    }
                ]
            },
            "taskCount": 3
        }
    ],
    "logsPolicy": {
        "destination": "CLOUD_LOGGING"
    }
}

Sostituisci quanto segue:

  • PROJECT_ID: l'ID progetto del tuo progetto.
  • LOCATION: la località del job.
  • JOB_NAME: il nome del job.
  • NFS_IP_ADDRESS: l'indirizzo IP del Network File System. Ad esempio, se il tuo NFS è una condivisione file di Filestore, specifica l'indirizzo IP dell'istanza Filestore, che puoi ottenere descrivendo l'istanza Filestore.
  • NFS_PATH: il percorso della directory NFS a cui deve accedere questo job, che deve iniziare con / seguito dalla directory root di NFS. Ad esempio, per una condivisione file Filestore denominata FILE_SHARE_NAME, il percorso /FILE_SHARE_NAME rappresenta la directory root della condivisione file e il percorso /FILE_SHARE_NAME/subdirectory rappresenta una sottodirectory.
  • MOUNT_PATH: il percorso di montaggio utilizzato dagli elementi eseguibili del job per accedere a questo NFS. Il percorso deve iniziare con /mnt/disks/ seguito da una directory o un percorso di tua scelta. Ad esempio, se vuoi rappresentare questo NFS con una directory denominata my-nfs, imposta il percorso di montaggio su /mnt/disks/my-nfs.

Java


import com.google.cloud.batch.v1.AllocationPolicy;
import com.google.cloud.batch.v1.BatchServiceClient;
import com.google.cloud.batch.v1.ComputeResource;
import com.google.cloud.batch.v1.CreateJobRequest;
import com.google.cloud.batch.v1.Job;
import com.google.cloud.batch.v1.LogsPolicy;
import com.google.cloud.batch.v1.NFS;
import com.google.cloud.batch.v1.Runnable;
import com.google.cloud.batch.v1.TaskGroup;
import com.google.cloud.batch.v1.TaskSpec;
import com.google.cloud.batch.v1.Volume;
import com.google.protobuf.Duration;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateScriptJobWithNfs {

  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Cloud project you want to use.
    String projectId = "YOUR_PROJECT_ID";

    // Name of the region you want to use to run the job. Regions that are
    // available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
    String region = "europe-central2";

    // The name of the job that will be created.
    // It needs to be unique for each project and region pair.
    String jobName = "JOB_NAME";

    // The path of the NFS directory that you want this job to access.
    String nfsPath = "NFS_PATH";
    // The IP address of the Network File System.
    String nfsIpAddress = "NFS_IP_ADDRESS";

    createScriptJobWithNfs(projectId, region, jobName, nfsPath, nfsIpAddress);
  }

  // This method shows how to create a batch script job that specifies and mounts a NFS.
  public static Job createScriptJobWithNfs(String projectId, String region, String jobName,
                                            String nfsPath, String nfsIpAddress)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {

      // Define what will be done as part of the job.
      Runnable runnable =
          Runnable.newBuilder()
              .setScript(
                  Runnable.Script.newBuilder()
                      .setText(
                          "echo Hello world from task ${BATCH_TASK_INDEX}. >> "
                              + "/mnt/share/output_task_${BATCH_TASK_INDEX}.txt")
                      // You can also run a script from a file. Just remember, that needs to be a
                      // script that's already on the VM that will be running the job.
                      // Using setText() and setPath() is mutually exclusive.
                      // .setPath("/tmp/test.sh")
                      .build())
              .build();

      // Describes a volume and parameters for it to be mounted to a VM.
      Volume volume = Volume.newBuilder()
          .setNfs(NFS.newBuilder()
              .setServer(nfsIpAddress)
              .setRemotePath(nfsPath)
              .build())
          .setMountPath("/mnt/share")
          .build();

      // We can specify what resources are requested by each task.
      ComputeResource computeResource =
          ComputeResource.newBuilder()
              // In milliseconds per cpu-second. This means the task requires 50% of a single CPUs.
              .setCpuMilli(500)
              // In MiB.
              .setMemoryMib(16)
              .build();

      TaskSpec task =
          TaskSpec.newBuilder()
              // Jobs can be divided into tasks. In this case, we have only one task.
              .addRunnables(runnable)
              .addVolumes(volume)
              .setComputeResource(computeResource)
              .setMaxRetryCount(2)
              .setMaxRunDuration(Duration.newBuilder().setSeconds(3600).build())
              .build();

      // Tasks are grouped inside a job using TaskGroups.
      // Currently, it's possible to have only one task group.
      TaskGroup taskGroup = TaskGroup.newBuilder().setTaskCount(4).setTaskSpec(task).build();

      // Policies are used to define on what kind of virtual machines the tasks will run on.
      // In this case, we tell the system to use "e2-standard-4" machine type.
      // Read more about machine types here:
      // https://cloud.google.com/compute/docs/machine-types
      AllocationPolicy.InstancePolicy instancePolicy =
          AllocationPolicy.InstancePolicy.newBuilder().setMachineType("e2-standard-4").build();

      AllocationPolicy allocationPolicy =
          AllocationPolicy.newBuilder()
              .addInstances(AllocationPolicy.InstancePolicyOrTemplate.newBuilder()
                      .setPolicy(instancePolicy).build())
              .build();

      Job job =
          Job.newBuilder()
              .addTaskGroups(taskGroup)
              .setAllocationPolicy(allocationPolicy)
              .putLabels("env", "testing")
              .putLabels("type", "script")
              .putLabels("mount", "bucket")
              // We use Cloud Logging as it's an out of the box available option.
              .setLogsPolicy(LogsPolicy.newBuilder()
                      .setDestination(LogsPolicy.Destination.CLOUD_LOGGING).build())
              .build();

      CreateJobRequest createJobRequest =
          CreateJobRequest.newBuilder()
              // The job's parent is the region in which the job will run.
              .setParent(String.format("projects/%s/locations/%s", projectId, region))
              .setJob(job)
              .setJobId(jobName)
              .build();

      Job result =
          batchServiceClient
              .createJobCallable()
              .futureCall(createJobRequest)
              .get(5, TimeUnit.MINUTES);

      System.out.printf("Successfully created the job: %s", result.getName());

      return result;
    }
  }
}

Node.js

// Imports the Batch library
const batchLib = require('@google-cloud/batch');
const batch = batchLib.protos.google.cloud.batch.v1;

// Instantiates a client
const batchClient = new batchLib.v1.BatchServiceClient();

/**
 * TODO(developer): Update these variables before running the sample.
 */
// Project ID or project number of the Google Cloud project you want to use.
const projectId = await batchClient.getProjectId();
// Name of the region you want to use to run the job. Regions that are
// available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
const region = 'europe-central2';
// The name of the job that will be created.
// It needs to be unique for each project and region pair.
const jobName = 'batch-nfs-job';
// The path of the NFS directory that you want this job to access.
const nfsPath = '/your_nfs_path';
// The IP address of the Network File System.
const nfsIpAddress = '0.0.0.0';
// The mount path that the job's tasks use to access the NFS.
const mountPath = '/mnt/disks';

// Define what will be done as part of the job.
const runnable = new batch.Runnable({
  script: new batch.Runnable.Script({
    commands: [
      '-c',
      'echo Hello world from task ${BATCH_TASK_INDEX}. >> ' +
        '/mnt/share/output_task_${BATCH_TASK_INDEX}.txt',
    ],
  }),
});

// Define a volume that uses NFS.
const volume = new batch.Volume({
  nfs: new batch.NFS({
    server: nfsIpAddress,
    remotePath: nfsPath,
  }),
  mountPath,
});

// Specify what resources are requested by each task.
const computeResource = new batch.ComputeResource({
  // In milliseconds per cpu-second. This means the task requires 50% of a single CPUs.
  cpuMilli: 500,
  // In MiB.
  memoryMib: 16,
});

const task = new batch.TaskSpec({
  runnables: [runnable],
  volumes: [volume],
  computeResource,
  maxRetryCount: 2,
  maxRunDuration: {seconds: 3600},
});

// Tasks are grouped inside a job using TaskGroups.
const group = new batch.TaskGroup({
  taskCount: 3,
  taskSpec: task,
});

// Policies are used to define on what kind of virtual machines the tasks will run on.
// In this case, we tell the system to use "e2-standard-4" machine type.
// Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
const instancePolicy = new batch.AllocationPolicy.InstancePolicy({
  machineType: 'e2-standard-4',
});

const allocationPolicy = new batch.AllocationPolicy.InstancePolicyOrTemplate({
  instances: [{policy: instancePolicy}],
});

const job = new batch.Job({
  name: jobName,
  taskGroups: [group],
  allocationPolicy,
  labels: {env: 'testing', type: 'script'},
  // We use Cloud Logging as it's an option available out of the box
  logsPolicy: new batch.LogsPolicy({
    destination: batch.LogsPolicy.Destination.CLOUD_LOGGING,
  }),
});

// The job's parent is the project and region in which the job will run
const parent = `projects/${projectId}/locations/${region}`;

async function callCreateBatchNfsJob() {
  // Construct request
  const request = {
    parent,
    jobId: jobName,
    job,
  };

  // Run request
  const [response] = await batchClient.createJob(request);
  console.log(JSON.stringify(response));
}

await callCreateBatchNfsJob();

Python

from google.cloud import batch_v1


def create_job_with_network_file_system(
    project_id: str,
    region: str,
    job_name: str,
    mount_path: str,
    nfs_ip_address: str,
    nfs_path: str,
) -> batch_v1.Job:
    """
    Creates a Batch job with status events that mounts a Network File System (NFS).
    Function mounts an NFS volume using the provided NFS server, IP address and path.

    Args:
        project_id (str): project ID or project number of the Cloud project you want to use.
        region (str): name of the region you want to use to run the job. Regions that are
            available for Batch are listed on: https://cloud.google.com/batch/docs/locations
        job_name (str): the name of the job that will be created.
            It needs to be unique for each project and region pair.
        mount_path (str): The mount path that the job's tasks use to access the NFS.
        nfs_ip_address (str): The IP address of the NFS server (e.g., Filestore instance).
            Documentation on how to create a
            Filestore instance is available here: https://cloud.google.com/filestore/docs/create-instance-gcloud
        nfs_path (str): The path of the NFS directory that the job accesses.
            The path must start with a / followed by the root directory of the NFS.

    Returns:
        batch_v1.Job: The created Batch job object containing configuration details.
    """
    client = batch_v1.BatchServiceClient()

    # Create a runnable with a script that writes a message to a file
    runnable = batch_v1.Runnable()
    runnable.script = batch_v1.Runnable.Script()
    runnable.script.text = f"echo Hello world from task ${{BATCH_TASK_INDEX}}. >> {mount_path}/output_task_${{BATCH_TASK_INDEX}}.txt"

    # Define a volume that uses NFS
    volume = batch_v1.Volume()
    volume.nfs = batch_v1.NFS(server=nfs_ip_address, remote_path=nfs_path)
    volume.mount_path = mount_path

    # Create a task specification and assign the runnable and volume to it
    task = batch_v1.TaskSpec()
    task.runnables = [runnable]
    task.volumes = [volume]

    # Specify what resources are requested by each task.
    resources = batch_v1.ComputeResource()
    resources.cpu_milli = 2000  # in milliseconds per cpu-second. This means the task requires 2 whole CPUs.
    resources.memory_mib = 16  # in MiB
    task.compute_resource = resources

    task.max_retry_count = 2
    task.max_run_duration = "3600s"

    # Create a task group and assign the task specification to it
    group = batch_v1.TaskGroup()
    group.task_count = 1
    group.task_spec = task

    # Policies are used to define on what kind of virtual machines the tasks will run on.
    # In this case, we tell the system to use "e2-standard-4" machine type.
    # Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
    policy = batch_v1.AllocationPolicy.InstancePolicy()
    policy.machine_type = "e2-standard-4"
    instances = batch_v1.AllocationPolicy.InstancePolicyOrTemplate()
    instances.policy = policy
    allocation_policy = batch_v1.AllocationPolicy()
    allocation_policy.instances = [instances]

    # Create the job and assign the task group and allocation policy to it
    job = batch_v1.Job()
    job.task_groups = [group]
    job.allocation_policy = allocation_policy
    job.labels = {"env": "testing", "type": "container"}
    # We use Cloud Logging as it's an out of the box available option
    job.logs_policy = batch_v1.LogsPolicy()
    job.logs_policy.destination = batch_v1.LogsPolicy.Destination.CLOUD_LOGGING

    # Create the job request and set the job and job ID
    create_request = batch_v1.CreateJobRequest()
    create_request.job = job
    create_request.job_id = job_name
    # The job's parent is the region in which the job will run
    create_request.parent = f"projects/{project_id}/locations/{region}"

    return client.create_job(create_request)

Passaggi successivi