|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +async function main( |
| 20 | + nodeName, |
| 21 | + queuedResourceName, |
| 22 | + zone, |
| 23 | + tpuType, |
| 24 | + tpuSoftwareVersion |
| 25 | +) { |
| 26 | + // [START tpu_queued_resources_startup_script] |
| 27 | + // Import the TPU library |
| 28 | + const {TpuClient} = require('@google-cloud/tpu').v2alpha1; |
| 29 | + const {Node, NetworkConfig, QueuedResource} = |
| 30 | + require('@google-cloud/tpu').protos.google.cloud.tpu.v2alpha1; |
| 31 | + |
| 32 | + // Instantiate a tpuClient |
| 33 | + const tpuClient = new TpuClient(); |
| 34 | + |
| 35 | + /** |
| 36 | + * TODO(developer): Update/uncomment these variables before running the sample. |
| 37 | + */ |
| 38 | + // Project ID or project number of the Google Cloud project, where you want to create queued resource. |
| 39 | + const projectId = await tpuClient.getProjectId(); |
| 40 | + |
| 41 | + // The name of the network you want the node to connect to. The network should be assigned to your project. |
| 42 | + const networkName = 'compute-tpu-network'; |
| 43 | + |
| 44 | + // The region of the network, that you want the node to connect to. |
| 45 | + const region = 'europe-west4'; |
| 46 | + |
| 47 | + // The name for your queued resource. |
| 48 | + // queuedResourceName = 'queued-resource-1'; |
| 49 | + |
| 50 | + // The name for your node. |
| 51 | + // nodeName = 'node-name-1'; |
| 52 | + |
| 53 | + // The zone in which to create the node. |
| 54 | + // For more information about supported TPU types for specific zones, |
| 55 | + // see https://cloud.google.com/tpu/docs/regions-zones |
| 56 | + // zone = 'europe-west4-a'; |
| 57 | + |
| 58 | + // The accelerator type that specifies the version and size of the node you want to create. |
| 59 | + // For more information about supported accelerator types for each TPU version, |
| 60 | + // see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions. |
| 61 | + // tpuType = 'v2-8'; |
| 62 | + |
| 63 | + // Software version that specifies the version of the node runtime to install. For more information, |
| 64 | + // see https://cloud.google.com/tpu/docs/runtimes |
| 65 | + // tpuSoftwareVersion = 'tpu-vm-tf-2.14.1'; |
| 66 | + |
| 67 | + async function callCreateQueuedResourceStartupScript() { |
| 68 | + // Create a node |
| 69 | + const node = new Node({ |
| 70 | + name: nodeName, |
| 71 | + zone, |
| 72 | + acceleratorType: tpuType, |
| 73 | + runtimeVersion: tpuSoftwareVersion, |
| 74 | + // Define network |
| 75 | + networkConfig: new NetworkConfig({ |
| 76 | + enableExternalIps: true, |
| 77 | + network: `projects/${projectId}/global/networks/${networkName}`, |
| 78 | + subnetwork: `projects/${projectId}/regions/${region}/subnetworks/${networkName}`, |
| 79 | + }), |
| 80 | + queuedResource: `projects/${projectId}/locations/${zone}/queuedResources/${queuedResourceName}`, |
| 81 | + metadata: { |
| 82 | + // The script updates numpy to the latest version and logs the output to a file. |
| 83 | + 'startup-script': `#!/bin/bash |
| 84 | + echo "Hello World" > /var/log/hello.log |
| 85 | + sudo pip3 install --upgrade numpy >> /var/log/hello.log 2>&1`, |
| 86 | + }, |
| 87 | + }); |
| 88 | + |
| 89 | + // Define parent for requests |
| 90 | + const parent = `projects/${projectId}/locations/${zone}`; |
| 91 | + |
| 92 | + // Create queued resource |
| 93 | + const queuedResource = new QueuedResource({ |
| 94 | + name: queuedResourceName, |
| 95 | + tpu: { |
| 96 | + nodeSpec: [ |
| 97 | + { |
| 98 | + parent, |
| 99 | + node, |
| 100 | + nodeId: nodeName, |
| 101 | + }, |
| 102 | + ], |
| 103 | + }, |
| 104 | + }); |
| 105 | + |
| 106 | + const request = { |
| 107 | + parent: `projects/${projectId}/locations/${zone}`, |
| 108 | + queuedResource, |
| 109 | + queuedResourceId: queuedResourceName, |
| 110 | + }; |
| 111 | + |
| 112 | + const [operation] = await tpuClient.createQueuedResource(request); |
| 113 | + |
| 114 | + // Wait for the create operation to complete. |
| 115 | + await operation.promise(); |
| 116 | + |
| 117 | + // You can wait until TPU Node is READY, |
| 118 | + // and check its status using getTpuVm() from `tpu_vm_get` sample. |
| 119 | + console.log( |
| 120 | + `Queued resource ${queuedResourceName} with start-up script created.` |
| 121 | + ); |
| 122 | + } |
| 123 | + await callCreateQueuedResourceStartupScript(); |
| 124 | + // [END tpu_queued_resources_startup_script] |
| 125 | +} |
| 126 | + |
| 127 | +main(...process.argv.slice(2)).catch(err => { |
| 128 | + console.error(err); |
| 129 | + process.exitCode = 1; |
| 130 | +}); |
0 commit comments