Skip to content

Commit

Permalink
docs: use repo-metadata to generate README (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored and Ace Nassri committed Nov 21, 2022
1 parent df35469 commit d55cfd0
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 110 deletions.
24 changes: 18 additions & 6 deletions compute/createVM.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,30 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// sample-metadata:
// title: Create VM
// usage: node createVM <vmName>

'use strict';

async function createVM(
async function main(
vmName = 'new_virtual_machine' // VM name of your choice
) {
// [START gce_create_vm]
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('us-central1-c');
const [vm, operation] = await zone.createVM(vmName, {os: 'ubuntu'});
console.log(vm);
await operation.promise();
console.log('Virtual machine created!');

async function createVM() {
// TODO(developer): provide a name for your VM
// const vmName = 'new-virutal-machine';
const [vm, operation] = await zone.createVM(vmName, {os: 'ubuntu'});
console.log(vm);
await operation.promise();
console.log('Virtual machine created!');
}
createVM();
// [END gce_create_vm]
}

createVM(...process.argv.slice(2)).catch(console.error);
main(...process.argv.slice(2));
28 changes: 20 additions & 8 deletions compute/deleteVM.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,30 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// sample-metadata:
// title: Delete VM
// usage: node deleteVM <vmName>

'use strict';

async function deleteVM(
async function main(
name = 'virtual_machine_name' // VM name of your choice
) {
// [START gce_delete_vm]
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('us-central1-c');
const vm = zone.vm(name);
const [operation] = await vm.delete();
await operation.promise();
console.log(`VM deleted!`);

async function deleteVM() {
const compute = new Compute();
const zone = compute.zone('us-central1-c');
// TODO(developer): choose a name for the VM to delete
// const name = 'vm-name';
const vm = zone.vm(name);
const [operation] = await vm.delete();
await operation.promise();
console.log(`VM deleted!`);
}
deleteVM();
// [END gce_delete_vm]
}

deleteVM(...process.argv.slice(2)).catch(console.error);
main(...process.argv.slice(2));
26 changes: 16 additions & 10 deletions compute/listVMs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@
* limitations under the License.
*/

// sample-metadata:
// title: List VMs
// usage: node listVMs

'use strict';

// [START list]
async function listVMs() {
async function main() {
// [START gce_list_vms]
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const vms = await compute.getVMs({
maxResults: 10,
});
console.log(`Found ${vms.length} VMs!`);
vms.forEach(vm => console.log(vm));
async function listVMs() {
const vms = await compute.getVMs({
maxResults: 10,
});
console.log(`Found ${vms.length} VMs!`);
vms.forEach(vm => console.log(vm));
}
listVMs();
// [END gce_list_vms]
}
// [END list]

listVMs().catch(console.error);
main().catch(console.error);
7 changes: 5 additions & 2 deletions compute/mailjet.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
* limitations under the License.
*/

// sample-metadata:
// title: Mailjet
// usage: node mailjet

'use strict';

// [START send]
Expand All @@ -39,6 +43,5 @@ async function mailjet() {
});
console.log(json);
}
mailjet();
// [END send]

mailjet().catch(console.error);
32 changes: 19 additions & 13 deletions compute/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,35 @@

'use strict';

// [START compute_engine_quickstart]
async function createVM(
async function main(
vmName = 'new_virtual_machine' // VM name of your choice
) {
// [START compute_engine_quickstart]
// Imports the Google Cloud client library
const Compute = require('@google-cloud/compute');

// Creates a client
const compute = new Compute();

// Create a new VM using the latest OS image of your choice.
const zone = compute.zone('us-central1-c');
async function quickstart() {
// Create a new VM using the latest OS image of your choice.
const zone = compute.zone('us-central1-c');

// Start the VM create task
const [vm, operation] = await zone.createVM(vmName, {os: 'ubuntu'});
console.log(vm);
// TODO(developer): choose a name for the VM
// const vmName = 'vm-name';

// `operation` lets you check the status of long-running tasks.
await operation.promise();
// Start the VM create task
const [vm, operation] = await zone.createVM(vmName, {os: 'ubuntu'});
console.log(vm);

// Complete!
console.log('Virtual machine created!');
// `operation` lets you check the status of long-running tasks.
await operation.promise();

// Complete!
console.log('Virtual machine created!');
}
quickstart();
// [END compute_engine_quickstart]
}
// [END compute_engine_quickstart]

createVM(...process.argv.slice(2)).catch(console.error);
main(...process.argv.slice(2));
2 changes: 1 addition & 1 deletion compute/sendgrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ async function sendgridExample() {
'Well hello! This is a Sendgrid test email from Node.js on Google Cloud Platform.',
});
}
sendgridExample().catch(console.error);
sendgridExample();
// [END send]
147 changes: 77 additions & 70 deletions compute/startupScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,81 +15,88 @@

'use strict';

const Compute = require('@google-cloud/compute');
const fetch = require('node-fetch');
async function main(name = 'start-script-example') {
// [START gce_startup_script]
const Compute = require('@google-cloud/compute');
const fetch = require('node-fetch');

const compute = new Compute();
const zone = compute.zone('us-central1-c');
const compute = new Compute();
const zone = compute.zone('us-central1-c');

/**
* Create a new virtual machine with Ubuntu and Apache
* @param {string} name Name of the virtual machine
*/
async function createVMWithStartupScript(name) {
// Create a new VM, using default ubuntu image. The startup script
// installs apache and a custom homepage.
const config = {
os: 'ubuntu',
http: true,
metadata: {
items: [
{
key: 'startup-script',
value: `#! /bin/bash
# Installs apache and a custom homepage
apt-get update
apt-get install -y apache2
cat <<EOF > /var/www/html/index.html
<!doctype html>
<h1>Hello World</h1>
<p>This page was created from a simple start-up script!</p>`,
},
],
},
};

const vm = zone.vm(name);

console.log(`Creating VM ${name}...`);
const [, operation] = await vm.create(config);

console.log(`Polling operation ${operation.id}...`);
await operation.promise();

console.log('Acquiring VM metadata...');
const [metadata] = await vm.getMetadata();

// External IP of the VM.
const ip = metadata.networkInterfaces[0].accessConfigs[0].natIP;
console.log(`Booting new VM with IP http://${ip}...`);

// Ping the VM to determine when the HTTP server is ready.
console.log('Operation complete. Waiting for IP');
await pingVM(ip);

console.log(`\n${name} created succesfully`);
}
// TODO(developer): choose a name for your virtual machine
// const name = 'your-vm-name';

/**
* Poll a given IP address until it returns a result.
* @param {string} ip IP address to poll
*/
async function pingVM(ip) {
let exit = false;
while (!exit) {
await new Promise(r => setTimeout(r, 2000));
try {
const res = await fetch(`http://${ip}`);
if (res.status !== 200) {
throw new Error(res.status);
/**
* Create a new virtual machine with Ubuntu and Apache
* @param {string} name Name of the virtual machine
*/
async function createVMWithStartupScript() {
// Create a new VM, using default ubuntu image. The startup script
// installs apache and a custom homepage.
const config = {
os: 'ubuntu',
http: true,
metadata: {
items: [
{
key: 'startup-script',
value: `#! /bin/bash
# Installs apache and a custom homepage
apt-get update
apt-get install -y apache2
cat <<EOF > /var/www/html/index.html
<!doctype html>
<h1>Hello World</h1>
<p>This page was created from a simple start-up script!</p>`,
},
],
},
};

const vm = zone.vm(name);

console.log(`Creating VM ${name}...`);
const [, operation] = await vm.create(config);

console.log(`Polling operation ${operation.id}...`);
await operation.promise();

console.log('Acquiring VM metadata...');
const [metadata] = await vm.getMetadata();

// External IP of the VM.
const ip = metadata.networkInterfaces[0].accessConfigs[0].natIP;
console.log(`Booting new VM with IP http://${ip}...`);

// Ping the VM to determine when the HTTP server is ready.
console.log('Operation complete. Waiting for IP');
await pingVM(ip);

console.log(`\n${name} created succesfully`);
}

/**
* Poll a given IP address until it returns a result.
* @param {string} ip IP address to poll
*/
async function pingVM(ip) {
let exit = false;
while (!exit) {
await new Promise(r => setTimeout(r, 2000));
try {
const res = await fetch(`http://${ip}`);
if (res.status !== 200) {
throw new Error(res.status);
}
exit = true;
} catch (err) {
process.stdout.write('.');
}
exit = true;
} catch (err) {
process.stdout.write('.');
}
}

createVMWithStartupScript();
}

const args = process.argv.slice(2);
createVMWithStartupScript(...args).catch(console.error);
main(...process.argv.slice(2));

0 comments on commit d55cfd0

Please sign in to comment.