Skip to content

Commit

Permalink
chore(samples): add preemptible vm samples (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrodoTheTrue authored and Ace Nassri committed Nov 21, 2022
1 parent bef3bf4 commit b30b005
Show file tree
Hide file tree
Showing 4 changed files with 285 additions and 0 deletions.
88 changes: 88 additions & 0 deletions compute/instances/preemptible/createPreemptible.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* Creates a new preemptible VM instance with Debian 11 operating system.
*
* @param {string} projectId - ID or number of the project you want to use.
* @param {string} zone - Name of the zone you want to use, for example: us-west3-b
* @param {string} instanceName - Name of the new machine.
*/
function main(projectId, zone, instanceName) {
// [START compute_preemptible_create]
/**
* TODO(developer): Uncomment and replace these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const zone = 'europe-central2-b';
// const instanceName = 'YOUR_INSTANCE_NAME';

const compute = require('@google-cloud/compute');

async function createPreemptible() {
const instancesClient = new compute.InstancesClient();

const [response] = await instancesClient.insert({
instanceResource: {
name: instanceName,
disks: [
{
initializeParams: {
diskSizeGb: '64',
sourceImage:
'projects/debian-cloud/global/images/family/debian-11/',
},
autoDelete: true,
boot: true,
},
],
scheduling: {
// Set the preemptible setting
preemptible: true,
},
machineType: `zones/${zone}/machineTypes/e2-small`,
networkInterfaces: [
{
name: 'global/networks/default',
},
],
},
project: projectId,
zone,
});
let operation = response.latestResponse;
const operationsClient = new compute.ZoneOperationsClient();

// Wait for the create operation to complete.
while (operation.status !== 'DONE') {
[operation] = await operationsClient.wait({
operation: operation.name,
project: projectId,
zone: operation.zone.split('/').pop(),
});
}

console.log('Instance created.');
}

createPreemptible();
// [END compute_preemptible_create]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
76 changes: 76 additions & 0 deletions compute/instances/preemptible/preemptionHistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* Gets a list of preemption operations from given zone in a project.
* Optionally limit the results to instance name.
*
* @param {string} projectId - ID or number of the project you want to use.
* @param {string} zone - Name of the zone you want to check, for example: us-west3-b.
* @param {string} instanceName - Name of the virtual machine to look for.
* @param {string} customFilter - Filter string to be used for this listing operation.
*/
function main(projectId, zone, instanceName = '', customFilter = '') {
// [START compute_preemptible_history]
/**
* TODO(developer): Uncomment and replace these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const zone = 'europe-central2-b';
// const instanceName = 'YOUR_INSTANCE_NAME';
// const customFilter = 'operationType="compute.instances.preempted"';

const compute = require('@google-cloud/compute');

async function preemptionHistory() {
const zoneOperationsClient = new compute.ZoneOperationsClient();

let filter;

if (customFilter !== '') {
filter = customFilter;
} else {
filter = 'operationType="compute.instances.preempted"';

if (instanceName !== '') {
filter += ` AND targetLink="https://www.googleapis.com/compute/v1/projects/${projectId}/zones/${zone}/instances/${instanceName}"`;
}
}

const [operationsList] = await zoneOperationsClient.list({
project: projectId,
zone,
filter,
});

for (const operation of operationsList) {
const thisInstanceName = operation.targetLink.split('/').pop();
if (thisInstanceName === instanceName) {
// The filter used is not 100% accurate, it's `contains` not `equals`
// So we need to check the name to make sure it's the one we want.
console.log(`- ${instanceName} ${operation.insertTime}`);
}
}
}

preemptionHistory();
// [END compute_preemptible_history]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
54 changes: 54 additions & 0 deletions compute/instances/preemptible/printPreemptible.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* Prints if a given instance is preemptible or not.
*
* @param {string} projectId - ID or number of the project you want to use.
* @param {string} zone - Name of the zone you want to use, for example: us-west3-b
* @param {string} instanceName - name of the virtual machine to check.
*/
function main(projectId, zone, instanceName) {
// [START compute_preemptible_check]
/**
* TODO(developer): Uncomment and replace these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const zone = 'europe-central2-b';
// const instanceName = 'YOUR_INSTANCE_NAME';

const compute = require('@google-cloud/compute');

async function printPreemptible() {
const instancesClient = new compute.InstancesClient();

const [instance] = await instancesClient.get({
project: projectId,
zone,
instance: instanceName,
});

console.log(`Is instance preemptible: ${instance.scheduling.preemptible}`);
}

printPreemptible();
// [END compute_preemptible_check]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
67 changes: 67 additions & 0 deletions compute/test/preemptible.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const compute = require('@google-cloud/compute');

const {describe, it} = require('mocha');
const cp = require('child_process');
const {assert} = require('chai');

const {generateTestId, getStaleVMInstances, deleteInstance} = require('./util');

const instancesClient = new compute.InstancesClient();

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

describe('preemptible instances tests', () => {
const instanceName = generateTestId();
const zone = 'europe-central2-b';

before(async () => {
const instances = await getStaleVMInstances();
await Promise.all(
instances.map(instance =>
deleteInstance(instance.zone, instance.instanceName)
)
);
});

it('create and print preemptible instance', async () => {
const projectId = await instancesClient.getProjectId();

let output;

output = execSync(
`node instances/preemptible/createPreemptible ${projectId} ${zone} ${instanceName}`
);
assert.match(output, /Instance created./);

output = execSync(
`node instances/preemptible/printPreemptible ${projectId} ${zone} ${instanceName}`
);
assert.include(output, 'Is instance preemptible: true');

const filter = `'targetLink="https://www.googleapis.com/compute/v1/projects/${projectId}/zones/${zone}/instances/${instanceName}"'`;

output = execSync(
`node instances/preemptible/preemptionHistory ${projectId} ${zone} ${instanceName} ${filter}`
);

assert.include(output, `- ${instanceName}`);

execSync(`node deleteInstance ${projectId} ${zone} ${instanceName}`);
});
});

0 comments on commit b30b005

Please sign in to comment.