|
15 | 15 |
|
16 | 16 | 'use strict'; |
17 | 17 |
|
18 | | -const Compute = require('@google-cloud/compute'); |
19 | | -const fetch = require('node-fetch'); |
| 18 | +async function main(name = 'start-script-example') { |
| 19 | + // [START gce_startup_script] |
| 20 | + const Compute = require('@google-cloud/compute'); |
| 21 | + const fetch = require('node-fetch'); |
20 | 22 |
|
21 | | -const compute = new Compute(); |
22 | | -const zone = compute.zone('us-central1-c'); |
| 23 | + const compute = new Compute(); |
| 24 | + const zone = compute.zone('us-central1-c'); |
23 | 25 |
|
24 | | -/** |
25 | | - * Create a new virtual machine with Ubuntu and Apache |
26 | | - * @param {string} name Name of the virtual machine |
27 | | - */ |
28 | | -async function createVMWithStartupScript(name) { |
29 | | - // Create a new VM, using default ubuntu image. The startup script |
30 | | - // installs apache and a custom homepage. |
31 | | - const config = { |
32 | | - os: 'ubuntu', |
33 | | - http: true, |
34 | | - metadata: { |
35 | | - items: [ |
36 | | - { |
37 | | - key: 'startup-script', |
38 | | - value: `#! /bin/bash |
39 | | -
|
40 | | - # Installs apache and a custom homepage |
41 | | - apt-get update |
42 | | - apt-get install -y apache2 |
43 | | - cat <<EOF > /var/www/html/index.html |
44 | | - <!doctype html> |
45 | | - <h1>Hello World</h1> |
46 | | - <p>This page was created from a simple start-up script!</p>`, |
47 | | - }, |
48 | | - ], |
49 | | - }, |
50 | | - }; |
51 | | - |
52 | | - const vm = zone.vm(name); |
53 | | - |
54 | | - console.log(`Creating VM ${name}...`); |
55 | | - const [, operation] = await vm.create(config); |
56 | | - |
57 | | - console.log(`Polling operation ${operation.id}...`); |
58 | | - await operation.promise(); |
59 | | - |
60 | | - console.log('Acquiring VM metadata...'); |
61 | | - const [metadata] = await vm.getMetadata(); |
62 | | - |
63 | | - // External IP of the VM. |
64 | | - const ip = metadata.networkInterfaces[0].accessConfigs[0].natIP; |
65 | | - console.log(`Booting new VM with IP http://${ip}...`); |
66 | | - |
67 | | - // Ping the VM to determine when the HTTP server is ready. |
68 | | - console.log('Operation complete. Waiting for IP'); |
69 | | - await pingVM(ip); |
70 | | - |
71 | | - console.log(`\n${name} created succesfully`); |
72 | | -} |
| 26 | + // TODO(developer): choose a name for your virtual machine |
| 27 | + // const name = 'your-vm-name'; |
73 | 28 |
|
74 | | -/** |
75 | | - * Poll a given IP address until it returns a result. |
76 | | - * @param {string} ip IP address to poll |
77 | | - */ |
78 | | -async function pingVM(ip) { |
79 | | - let exit = false; |
80 | | - while (!exit) { |
81 | | - await new Promise(r => setTimeout(r, 2000)); |
82 | | - try { |
83 | | - const res = await fetch(`http://${ip}`); |
84 | | - if (res.status !== 200) { |
85 | | - throw new Error(res.status); |
| 29 | + /** |
| 30 | + * Create a new virtual machine with Ubuntu and Apache |
| 31 | + * @param {string} name Name of the virtual machine |
| 32 | + */ |
| 33 | + async function createVMWithStartupScript() { |
| 34 | + // Create a new VM, using default ubuntu image. The startup script |
| 35 | + // installs apache and a custom homepage. |
| 36 | + const config = { |
| 37 | + os: 'ubuntu', |
| 38 | + http: true, |
| 39 | + metadata: { |
| 40 | + items: [ |
| 41 | + { |
| 42 | + key: 'startup-script', |
| 43 | + value: `#! /bin/bash |
| 44 | +
|
| 45 | + # Installs apache and a custom homepage |
| 46 | + apt-get update |
| 47 | + apt-get install -y apache2 |
| 48 | + cat <<EOF > /var/www/html/index.html |
| 49 | + <!doctype html> |
| 50 | + <h1>Hello World</h1> |
| 51 | + <p>This page was created from a simple start-up script!</p>`, |
| 52 | + }, |
| 53 | + ], |
| 54 | + }, |
| 55 | + }; |
| 56 | + |
| 57 | + const vm = zone.vm(name); |
| 58 | + |
| 59 | + console.log(`Creating VM ${name}...`); |
| 60 | + const [, operation] = await vm.create(config); |
| 61 | + |
| 62 | + console.log(`Polling operation ${operation.id}...`); |
| 63 | + await operation.promise(); |
| 64 | + |
| 65 | + console.log('Acquiring VM metadata...'); |
| 66 | + const [metadata] = await vm.getMetadata(); |
| 67 | + |
| 68 | + // External IP of the VM. |
| 69 | + const ip = metadata.networkInterfaces[0].accessConfigs[0].natIP; |
| 70 | + console.log(`Booting new VM with IP http://${ip}...`); |
| 71 | + |
| 72 | + // Ping the VM to determine when the HTTP server is ready. |
| 73 | + console.log('Operation complete. Waiting for IP'); |
| 74 | + await pingVM(ip); |
| 75 | + |
| 76 | + console.log(`\n${name} created succesfully`); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Poll a given IP address until it returns a result. |
| 81 | + * @param {string} ip IP address to poll |
| 82 | + */ |
| 83 | + async function pingVM(ip) { |
| 84 | + let exit = false; |
| 85 | + while (!exit) { |
| 86 | + await new Promise(r => setTimeout(r, 2000)); |
| 87 | + try { |
| 88 | + const res = await fetch(`http://${ip}`); |
| 89 | + if (res.status !== 200) { |
| 90 | + throw new Error(res.status); |
| 91 | + } |
| 92 | + exit = true; |
| 93 | + } catch (err) { |
| 94 | + process.stdout.write('.'); |
86 | 95 | } |
87 | | - exit = true; |
88 | | - } catch (err) { |
89 | | - process.stdout.write('.'); |
90 | 96 | } |
91 | 97 | } |
| 98 | + |
| 99 | + createVMWithStartupScript(); |
92 | 100 | } |
93 | 101 |
|
94 | | -const args = process.argv.slice(2); |
95 | | -createVMWithStartupScript(...args).catch(console.error); |
| 102 | +main(...process.argv.slice(2)); |
0 commit comments