@@ -19,10 +19,13 @@ const Compute = require('@google-cloud/compute');
1919const fetch = require ( 'node-fetch' ) ;
2020
2121const compute = new Compute ( ) ;
22-
2322const zone = compute . zone ( 'us-central1-a' ) ;
2423
25- async function createVm ( name ) {
24+ /**
25+ * Create a new virtual machine with Ubuntu and Apache
26+ * @param {string } name Name of the virtual machine
27+ */
28+ async function createVM ( name ) {
2629 // Create a new VM, using default ubuntu image. The startup script
2730 // installs apache and a custom homepage.
2831
@@ -34,7 +37,7 @@ async function createVm(name) {
3437 {
3538 key : 'startup-script' ,
3639 value : `#! /bin/bash
37-
40+
3841 # Installs apache and a custom homepage
3942 apt-get update
4043 apt-get install -y apache2
@@ -46,21 +49,35 @@ async function createVm(name) {
4649 ] ,
4750 } ,
4851 } ;
49- const vmObj = zone . vm ( name ) ;
50- console . log ( 'Creating VM ...' ) ;
51- const [ vm , operation ] = await vmObj . create ( config ) ;
52+
53+ const vm = zone . vm ( name ) ;
54+
55+ console . log ( `Creating VM ${ name } ...` ) ;
56+ const [ , operation ] = await vm . create ( config ) ;
57+
58+ console . log ( `Polling operation ${ operation . id } ...` ) ;
5259 await operation . promise ( ) ;
60+
61+ console . log ( 'Acquiring VM metadata...' ) ;
5362 const [ metadata ] = await vm . getMetadata ( ) ;
63+ console . log ( metadata ) ;
5464
5565 // External IP of the VM.
5666 const ip = metadata . networkInterfaces [ 0 ] . accessConfigs [ 0 ] . natIP ;
5767 console . log ( `Booting new VM with IP http://${ ip } ...` ) ;
5868
5969 // Ping the VM to determine when the HTTP server is ready.
70+ console . log ( `Operation complete. Waiting for IP ...` ) ;
6071 await pingVM ( ip ) ;
72+
73+ console . log ( `${ name } created succesfully` ) ;
6174 return ip ;
6275}
6376
77+ /**
78+ * Poll a given IP address until it returns a result.
79+ * @param {string } ip IP address to poll
80+ */
6481async function pingVM ( ip ) {
6582 let waiting = true ;
6683 while ( waiting ) {
@@ -80,10 +97,12 @@ async function pingVM(ip) {
8097 }
8198 }
8299}
83- // List all VMs and their external IPs in a given zone.
84- async function listVms ( ) {
100+ /**
101+ * List all VMs and their external IPs in a given zone.
102+ */
103+ async function listVMs ( ) {
85104 const [ vms ] = await zone . getVMs ( ) ;
86- return await Promise . all (
105+ const results = await Promise . all (
87106 vms . map ( async vm => {
88107 const [ metadata ] = await vm . getMetadata ( ) ;
89108 return {
@@ -94,31 +113,26 @@ async function listVms() {
94113 } ;
95114 } )
96115 ) ;
116+ console . log ( results ) ;
117+ return results ;
97118}
98119
99- async function deleteVm ( name ) {
120+ /**
121+ * Delete a VM with a given name.
122+ * @param {string } name
123+ */
124+ async function deleteVM ( name ) {
100125 const vm = zone . vm ( name ) ;
101- console . log ( ' Deleting ...' ) ;
126+ console . log ( ` Deleting ${ name } ...` ) ;
102127 const [ operation ] = await vm . delete ( ) ;
128+ console . log ( `Polling operation ${ operation . id } ...` ) ;
103129 await operation . promise ( ) ;
104- // VM deleted
130+ console . log ( ` ${ name } deleted succesfully!` ) ;
105131 return name ;
106132}
107133
108- exports . create = async name => {
109- const ip = await createVm ( name ) ;
110- console . log ( `${ name } created succesfully` ) ;
111- return ip ;
112- } ;
113-
114- exports . list = async ( ) => {
115- const vms = await listVms ( ) ;
116- console . log ( vms ) ;
117- return vms ;
118- } ;
119-
120- exports . delete = async name => {
121- const result = await deleteVm ( name ) ;
122- console . log ( `${ name } deleted succesfully` ) ;
123- return result ;
134+ module . exports = {
135+ createVM,
136+ deleteVM,
137+ listVMs,
124138} ;
0 commit comments