@@ -19,10 +19,13 @@ const Compute = require('@google-cloud/compute');
19
19
const fetch = require ( 'node-fetch' ) ;
20
20
21
21
const compute = new Compute ( ) ;
22
-
23
22
const zone = compute . zone ( 'us-central1-a' ) ;
24
23
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 ) {
26
29
// Create a new VM, using default ubuntu image. The startup script
27
30
// installs apache and a custom homepage.
28
31
@@ -34,7 +37,7 @@ async function createVm(name) {
34
37
{
35
38
key : 'startup-script' ,
36
39
value : `#! /bin/bash
37
-
40
+
38
41
# Installs apache and a custom homepage
39
42
apt-get update
40
43
apt-get install -y apache2
@@ -46,21 +49,35 @@ async function createVm(name) {
46
49
] ,
47
50
} ,
48
51
} ;
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 } ...` ) ;
52
59
await operation . promise ( ) ;
60
+
61
+ console . log ( 'Acquiring VM metadata...' ) ;
53
62
const [ metadata ] = await vm . getMetadata ( ) ;
63
+ console . log ( metadata ) ;
54
64
55
65
// External IP of the VM.
56
66
const ip = metadata . networkInterfaces [ 0 ] . accessConfigs [ 0 ] . natIP ;
57
67
console . log ( `Booting new VM with IP http://${ ip } ...` ) ;
58
68
59
69
// Ping the VM to determine when the HTTP server is ready.
70
+ console . log ( `Operation complete. Waiting for IP ...` ) ;
60
71
await pingVM ( ip ) ;
72
+
73
+ console . log ( `${ name } created succesfully` ) ;
61
74
return ip ;
62
75
}
63
76
77
+ /**
78
+ * Poll a given IP address until it returns a result.
79
+ * @param {string } ip IP address to poll
80
+ */
64
81
async function pingVM ( ip ) {
65
82
let waiting = true ;
66
83
while ( waiting ) {
@@ -80,10 +97,12 @@ async function pingVM(ip) {
80
97
}
81
98
}
82
99
}
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 ( ) {
85
104
const [ vms ] = await zone . getVMs ( ) ;
86
- return await Promise . all (
105
+ const results = await Promise . all (
87
106
vms . map ( async vm => {
88
107
const [ metadata ] = await vm . getMetadata ( ) ;
89
108
return {
@@ -94,31 +113,26 @@ async function listVms() {
94
113
} ;
95
114
} )
96
115
) ;
116
+ console . log ( results ) ;
117
+ return results ;
97
118
}
98
119
99
- async function deleteVm ( name ) {
120
+ /**
121
+ * Delete a VM with a given name.
122
+ * @param {string } name
123
+ */
124
+ async function deleteVM ( name ) {
100
125
const vm = zone . vm ( name ) ;
101
- console . log ( ' Deleting ...' ) ;
126
+ console . log ( ` Deleting ${ name } ...` ) ;
102
127
const [ operation ] = await vm . delete ( ) ;
128
+ console . log ( `Polling operation ${ operation . id } ...` ) ;
103
129
await operation . promise ( ) ;
104
- // VM deleted
130
+ console . log ( ` ${ name } deleted succesfully!` ) ;
105
131
return name ;
106
132
}
107
133
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,
124
138
} ;
0 commit comments