|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/** |
| 16 | + * Creates a new Windows image from the specified source disk. |
| 17 | + * |
| 18 | + * @param {string} projectId - Project ID or project number of the Cloud project you use. |
| 19 | + * @param {string} zone - Zone of the disk you copy from. |
| 20 | + * @param {string} sourceDiskName - Name of the source disk you copy from. |
| 21 | + * @param {string} imageName - Name of the image you want to create. |
| 22 | + * @param {string} storageLocation - Storage location for the image. If the value is undefined, function will store the image in the multi-region closest to your image's source location. |
| 23 | + * @param {boolean} forceCreate - Create the image even if the source disk is attached to a running instance. |
| 24 | + */ |
| 25 | +function main( |
| 26 | + projectId, |
| 27 | + zone, |
| 28 | + sourceDiskName, |
| 29 | + imageName, |
| 30 | + storageLocation, |
| 31 | + forceCreate = false |
| 32 | +) { |
| 33 | + // [START compute_windows_image_create] |
| 34 | + /** |
| 35 | + * TODO(developer): Uncomment and replace these variables before running the sample. |
| 36 | + */ |
| 37 | + // const projectId = 'YOUR_PROJECT_ID'; |
| 38 | + // const zone = 'europe-central2-b'; |
| 39 | + // const sourceDiskName = 'YOUR_SOURCE_DISK_NAME'; |
| 40 | + // const imageName = 'YOUR_IMAGE_NAME'; |
| 41 | + // const storageLocation = 'eu'; |
| 42 | + // const forceCreate = false; |
| 43 | + |
| 44 | + const compute = require('@google-cloud/compute'); |
| 45 | + |
| 46 | + function parseInstanceName(name) { |
| 47 | + const parsedName = name.split('/'); |
| 48 | + const l = parsedName.length; |
| 49 | + |
| 50 | + if (parsedName.legth < 5) { |
| 51 | + throw new Error( |
| 52 | + 'Provide correct instance name in the following format: https://www.googleapis.com/compute/v1/projects/PROJECT/zones/ZONE/instances/INSTANCE_NAME' |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + return [parsedName[l - 1], parsedName[l - 3], parsedName[l - 5]]; |
| 57 | + } |
| 58 | + |
| 59 | + async function createWindowsOSImage() { |
| 60 | + const imagesClient = new compute.ImagesClient(); |
| 61 | + const instancesClient = new compute.InstancesClient(); |
| 62 | + const disksClient = new compute.DisksClient(); |
| 63 | + |
| 64 | + // Getting instances where source disk is attached |
| 65 | + const [sourceDisk] = await disksClient.get({ |
| 66 | + project: projectId, |
| 67 | + zone, |
| 68 | + disk: sourceDiskName, |
| 69 | + }); |
| 70 | + |
| 71 | + // Сhecking whether the instances is stopped |
| 72 | + for (const fullInstanceName of sourceDisk.users) { |
| 73 | + const [instanceName, instanceZone, instanceProjectId] = |
| 74 | + parseInstanceName(fullInstanceName); |
| 75 | + const [instance] = await instancesClient.get({ |
| 76 | + project: instanceProjectId, |
| 77 | + zone: instanceZone, |
| 78 | + instance: instanceName, |
| 79 | + }); |
| 80 | + |
| 81 | + if ( |
| 82 | + !['TERMINATED', 'STOPPED'].includes(instance.status) && |
| 83 | + !forceCreate |
| 84 | + ) { |
| 85 | + throw new Error( |
| 86 | + `Instance ${instanceName} should be stopped. Please stop the instance using GCESysprep command or set forceCreate parameter to true (not recommended). More information here: https://cloud.google.com/compute/docs/instances/windows/creating-windows-os-image#api.` |
| 87 | + ); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if (forceCreate) { |
| 92 | + console.warn( |
| 93 | + 'Warning: forceCreate option compromise the integrity of your image. Stop the instance before you create the image if possible.' |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + const [response] = await imagesClient.insert({ |
| 98 | + project: projectId, |
| 99 | + forceCreate, |
| 100 | + imageResource: { |
| 101 | + name: imageName, |
| 102 | + sourceDisk: `/zones/${zone}/disks/${sourceDiskName}`, |
| 103 | + storageLocations: storageLocation ? [storageLocation] : [], |
| 104 | + }, |
| 105 | + }); |
| 106 | + let operation = response.latestResponse; |
| 107 | + const operationsClient = new compute.GlobalOperationsClient(); |
| 108 | + |
| 109 | + // Wait for the create operation to complete. |
| 110 | + while (operation.status !== 'DONE') { |
| 111 | + [operation] = await operationsClient.wait({ |
| 112 | + operation: operation.name, |
| 113 | + project: projectId, |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + console.log('Image created.'); |
| 118 | + } |
| 119 | + |
| 120 | + createWindowsOSImage(); |
| 121 | + // [END compute_windows_image_create] |
| 122 | +} |
| 123 | + |
| 124 | +process.on('unhandledRejection', err => { |
| 125 | + console.error(err.message); |
| 126 | + process.exitCode = 1; |
| 127 | +}); |
| 128 | + |
| 129 | +const args = process.argv.slice(2); |
| 130 | +if (args.length >= 6) { |
| 131 | + args[7] = args[7] === 'true'; |
| 132 | +} |
| 133 | + |
| 134 | +main(...args); |
0 commit comments