Skip to content

Commit 5ec9438

Browse files
Joanna Grycziennae
authored andcommitted
feat: tpu_vm_start, tpu_vm_stop
1 parent 253ae08 commit 5ec9438

File tree

7 files changed

+136
-4
lines changed

7 files changed

+136
-4
lines changed

compute/test/tpu.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ describe('Compute tpu', async () => {
6666
assert(Array.isArray(response));
6767
});
6868

69+
it('should stop tpu node', () => {
70+
const response = execSync(`node ./tpu/vmStop.js ${nodeName} ${zone}`, {
71+
cwd,
72+
});
73+
74+
assert(response.includes(`Node: ${nodeName} stopped.`));
75+
});
76+
77+
it('should start tpu node', () => {
78+
const response = execSync(`node ./tpu/vmStart.js ${nodeName} ${zone}`, {
79+
cwd,
80+
});
81+
82+
assert(response.includes(`Node: ${nodeName} started.`));
83+
});
84+
6985
it('should delete tpu node', () => {
7086
const response = execSync(`node ./tpu/vmDelete.js ${nodeName} ${zone}`, {
7187
cwd,

compute/tpu/vmCreate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async function main(nodeName, zone, tpuType, tpuSoftwareVersion) {
3939
const region = 'europe-west4';
4040

4141
// The name for your TPU.
42-
// nodeName = 'node-name-2';
42+
// nodeName = 'node-name-1';
4343

4444
// The zone in which to create the TPU.
4545
// For more information about supported TPU types for specific zones,

compute/tpu/vmDelete.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async function main(nodeName, zone) {
3434
// nodeName = 'node-name-1';
3535

3636
// The zone, where the TPU is created.
37-
// zone = 'us-central1-a';
37+
// zone = 'europe-west4-a';
3838

3939
async function callDeleteTpuVM() {
4040
const request = {

compute/tpu/vmGet.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async function main(nodeName, zone) {
3434
// nodeName = 'node-name-1';
3535

3636
// The zone, where the TPU is created.
37-
// zone = 'us-central1-a';
37+
// zone = 'europe-west4-a';
3838

3939
async function callGetTpuVM() {
4040
const request = {

compute/tpu/vmList.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async function main(zone) {
3131
const projectId = await tpuClient.getProjectId();
3232

3333
// The zone from which the TPUs are retrived.
34-
// zone = 'us-central1-a';
34+
// zone = 'europe-west4-a';
3535

3636
async function callTpuVMList() {
3737
const request = {

compute/tpu/vmStart.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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(nodeName, zone) {
20+
// [START tpu_vm_start]
21+
// Import TpuClient
22+
const {TpuClient} = require('@google-cloud/tpu').v2;
23+
24+
// Instantiate a tpuClient
25+
const tpuClient = new TpuClient();
26+
27+
/**
28+
* TODO(developer): Update/uncomment these variables before running the sample.
29+
*/
30+
// Project ID or project number of the Google Cloud project you want to start a node.
31+
const projectId = await tpuClient.getProjectId();
32+
33+
// The name of TPU to start.
34+
// nodeName = 'node-name-1';
35+
36+
// The zone, where the TPU is created.
37+
// zone = 'europe-west4-a';
38+
39+
async function callStartTpuVM() {
40+
const request = {
41+
name: `projects/${projectId}/locations/${zone}/nodes/${nodeName}`,
42+
};
43+
44+
const [operation] = await tpuClient.startNode(request);
45+
// Wait for the operation to complete.
46+
await operation.promise();
47+
48+
console.log(`Node: ${nodeName} started.`);
49+
}
50+
51+
await callStartTpuVM();
52+
// [END tpu_vm_start]
53+
}
54+
55+
main(...process.argv.slice(2)).catch(err => {
56+
console.error(err);
57+
process.exitCode = 1;
58+
});

compute/tpu/vmStop.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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(nodeName, zone) {
20+
// [START tpu_vm_stop]
21+
// Import TpuClient
22+
const {TpuClient} = require('@google-cloud/tpu').v2;
23+
24+
// Instantiate a tpuClient
25+
const tpuClient = new TpuClient();
26+
27+
/**
28+
* TODO(developer): Update/uncomment these variables before running the sample.
29+
*/
30+
// Project ID or project number of the Google Cloud project you want to stop a node.
31+
const projectId = await tpuClient.getProjectId();
32+
33+
// The name of TPU to stop.
34+
// nodeName = 'node-name-1';
35+
36+
// The zone, where the TPU is created.
37+
// zone = 'europe-west4-a';
38+
39+
async function callStopTpuVM() {
40+
const request = {
41+
name: `projects/${projectId}/locations/${zone}/nodes/${nodeName}`,
42+
};
43+
44+
const [operation] = await tpuClient.stopNode(request);
45+
// Wait for the operation to complete.
46+
await operation.promise();
47+
48+
console.log(`Node: ${nodeName} stopped.`);
49+
}
50+
51+
await callStopTpuVM();
52+
// [END tpu_vm_stop]
53+
}
54+
55+
main(...process.argv.slice(2)).catch(err => {
56+
console.error(err);
57+
process.exitCode = 1;
58+
});

0 commit comments

Comments
 (0)