|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +* ******************************************************* |
| 5 | +* Copyright (c) VMware, Inc. 2021. All Rights Reserved. |
| 6 | +* SPDX-License-Identifier: MIT |
| 7 | +* ******************************************************* |
| 8 | +* |
| 9 | +* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT |
| 10 | +* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN, |
| 11 | +* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED |
| 12 | +* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, |
| 13 | +* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | +""" |
| 15 | + |
| 16 | +__author__ = 'VMware, Inc.' |
| 17 | +__copyright__ = 'Copyright 2021 VMware, Inc. All rights reserved.' |
| 18 | +__vcenter_version__ = 'VCenter 7.0 U3' |
| 19 | + |
| 20 | +from com.vmware.vcenter.guest_client import CustomizationSpec,\ |
| 21 | + CloudConfiguration, CloudinitConfiguration, ConfigurationSpec,\ |
| 22 | + GlobalDNSSettings |
| 23 | +from pprint import pprint |
| 24 | +from samples.vsphere.common import sample_cli, sample_util |
| 25 | +from samples.vsphere.common.ssl_helper import get_unverified_session |
| 26 | +from vmware.vapi.vsphere.client import create_vsphere_client |
| 27 | +import os |
| 28 | + |
| 29 | + |
| 30 | +class CloudinitDataCustomizationSpecs(object): |
| 31 | + """ |
| 32 | + Demonstrates create/list/get/set/delete cloud-init data customizationSpecs |
| 33 | + Sample Prerequisites: 1 vcenter, no ESXi nor VM needed |
| 34 | + """ |
| 35 | + def __init__(self): |
| 36 | + self.metadata = None |
| 37 | + self.userdata = None |
| 38 | + self.parser = sample_cli.build_arg_parser() |
| 39 | + self.args = sample_util.process_cli_args(self.parser.parse_args()) |
| 40 | + self.session =\ |
| 41 | + get_unverified_session() if self.args.skipverification else None |
| 42 | + self.client = create_vsphere_client(server=self.args.server, |
| 43 | + username=self.args.username, |
| 44 | + password=self.args.password, |
| 45 | + session=self.session) |
| 46 | + self.specs_svc = self.client.vcenter.guest.CustomizationSpecs |
| 47 | + |
| 48 | + def createCloudinitDataSpec(self, specName, specDesc): |
| 49 | + """ |
| 50 | + create a cloud-init data customizationSpec |
| 51 | + """ |
| 52 | + print('------create 1 linux cloud-init data CustomizationSpec-------') |
| 53 | + cloudinitConfig = CloudinitConfiguration(metadata=self.metadata, |
| 54 | + userdata=self.userdata) |
| 55 | + cloudConfig =\ |
| 56 | + CloudConfiguration(cloudinit=cloudinitConfig, |
| 57 | + type=CloudConfiguration.Type('CLOUDINIT')) |
| 58 | + configSpec = ConfigurationSpec(cloud_config=cloudConfig) |
| 59 | + globalDnsSettings = GlobalDNSSettings() |
| 60 | + adapterMappingList = [] |
| 61 | + customizationSpec =\ |
| 62 | + CustomizationSpec(configuration_spec=configSpec, |
| 63 | + global_dns_settings=globalDnsSettings, |
| 64 | + interfaces=adapterMappingList) |
| 65 | + createSpec = self.specs_svc.CreateSpec(name=specName, |
| 66 | + description=specDesc, |
| 67 | + spec=customizationSpec) |
| 68 | + self.specs_svc.create(spec=createSpec) |
| 69 | + print('{} has been created'.format(specName)) |
| 70 | + print('-------------------------------------------------------------') |
| 71 | + |
| 72 | + def createSpecWithMetadataInYamlAndUserdata(self): |
| 73 | + """ |
| 74 | + create a linux cloud-init data customizationSpec with metadata in yaml |
| 75 | + format and userdata |
| 76 | + """ |
| 77 | + metadataYamlFilePath = os.path.join(os.path.dirname( |
| 78 | + os.path.realpath(__file__)), |
| 79 | + 'sample_metadata.yaml') |
| 80 | + userdataFilePath = os.path.join(os.path.dirname( |
| 81 | + os.path.realpath(__file__)), |
| 82 | + 'sample_userdata') |
| 83 | + with open(metadataYamlFilePath, "r") as fp: |
| 84 | + self.metadata = fp.read().rstrip('\n') |
| 85 | + with open(userdataFilePath, "r") as fp: |
| 86 | + self.userdata = fp.read().rstrip('\n') |
| 87 | + self.createCloudinitDataSpec('specWithMetadataInYamlAndUserdata', |
| 88 | + 'linux cloud-init data customization spec' |
| 89 | + 'with metadata in yaml format and ' |
| 90 | + 'userdata') |
| 91 | + |
| 92 | + def createSpecWithMetadataInJsonAndUserdata(self): |
| 93 | + """ |
| 94 | + create a linux cloud-init data customizationSpec with metadata in json |
| 95 | + format and userdata |
| 96 | + """ |
| 97 | + metadataYamlFilePath = os.path.join(os.path.dirname( |
| 98 | + os.path.realpath(__file__)), |
| 99 | + 'sample_metadata.json') |
| 100 | + userdataFilePath = os.path.join(os.path.dirname( |
| 101 | + os.path.realpath(__file__)), |
| 102 | + 'sample_userdata') |
| 103 | + with open(metadataYamlFilePath, "r") as fp: |
| 104 | + self.metadata = fp.read().rstrip('\n') |
| 105 | + with open(userdataFilePath, "r") as fp: |
| 106 | + self.userdata = fp.read().rstrip('\n') |
| 107 | + self.createCloudinitDataSpec('specWithMetadataInJsonAndUserdata', |
| 108 | + 'linux cloud-init data customization spec' |
| 109 | + 'with metadata in json format and ' |
| 110 | + 'userdata') |
| 111 | + |
| 112 | + def createSpecWithMetadataOnly(self): |
| 113 | + """ |
| 114 | + create a linux cloud-init data customizationSpec with metadata in yaml |
| 115 | + format and without userdata |
| 116 | + """ |
| 117 | + metadataYamlFilePath = os.path.join(os.path.dirname( |
| 118 | + os.path.realpath(__file__)), |
| 119 | + 'sample_metadata.yaml') |
| 120 | + with open(metadataYamlFilePath, "r") as fp: |
| 121 | + self.metadata = fp.read().rstrip('\n') |
| 122 | + self.createCloudinitDataSpec('specWithMetadataOnly', |
| 123 | + 'linux cloud-init data customization spec' |
| 124 | + 'with metadata only') |
| 125 | + |
| 126 | + def listCustomizationSpecs(self): |
| 127 | + print('------list all existing customization Spec------') |
| 128 | + existingSpecs = self.specs_svc.list() |
| 129 | + if (len(existingSpecs) > 0): |
| 130 | + pprint(existingSpecs) |
| 131 | + else: |
| 132 | + print('no specs found') |
| 133 | + print('------------------------------------------------') |
| 134 | + |
| 135 | + def getSetCloudinitDataCustomizationSpec(self): |
| 136 | + print('------get an existing cloud-init data customization Spec------') |
| 137 | + existingSpecs = self.specs_svc.list() |
| 138 | + if (len(existingSpecs) > 0): |
| 139 | + existingSpecName = existingSpecs[0].name |
| 140 | + existingSpec = self.specs_svc.get(existingSpecName) |
| 141 | + pprint(existingSpec) |
| 142 | + # Set a new spec description |
| 143 | + newSpecDesc =\ |
| 144 | + '{} updated by vapi set() method'.format(existingSpecName) |
| 145 | + existingSpec.spec.description = newSpecDesc |
| 146 | + # Set a new metadata |
| 147 | + metadata =\ |
| 148 | + """ |
| 149 | + instance-id: test-vm-id-01-updated |
| 150 | + local-hostname: test-vm-01-updated |
| 151 | + network: |
| 152 | + version: 2 |
| 153 | + ethernets: |
| 154 | + ens160: |
| 155 | + match: |
| 156 | + name: ens* |
| 157 | + dhcp4: yes |
| 158 | + """ |
| 159 | + existingSpec.spec.spec.configuration_spec.cloud_config.cloudinit.\ |
| 160 | + metadata = metadata |
| 161 | + print('------set existing cloud-init data customizationSpec------') |
| 162 | + self.specs_svc.set(name=existingSpecName, spec=existingSpec.spec) |
| 163 | + print('{} has been set'.format(existingSpecName)) |
| 164 | + pprint(existingSpec) |
| 165 | + else: |
| 166 | + print('no specs found') |
| 167 | + print('-------------------------------------------------------------') |
| 168 | + |
| 169 | + def deleteCustomizationSpecs(self): |
| 170 | + print('-----delete existing customization Specs-----') |
| 171 | + existingSpecs = self.specs_svc.list() |
| 172 | + if (len(existingSpecs) > 0): |
| 173 | + for i in range(len(existingSpecs)): |
| 174 | + specName = existingSpecs[i].name |
| 175 | + self.specs_svc.delete(specName) |
| 176 | + print('{} has been deleted'.format(specName)) |
| 177 | + else: |
| 178 | + print('no specs found') |
| 179 | + print('-------------------------------------------------------------') |
| 180 | + |
| 181 | + |
| 182 | +def main(): |
| 183 | + cloudinitDataCustomizationSpecs = CloudinitDataCustomizationSpecs() |
| 184 | + cloudinitDataCustomizationSpecs.createSpecWithMetadataInYamlAndUserdata() |
| 185 | + cloudinitDataCustomizationSpecs.createSpecWithMetadataInJsonAndUserdata() |
| 186 | + cloudinitDataCustomizationSpecs.createSpecWithMetadataOnly() |
| 187 | + cloudinitDataCustomizationSpecs.listCustomizationSpecs() |
| 188 | + cloudinitDataCustomizationSpecs.getSetCloudinitDataCustomizationSpec() |
| 189 | + cloudinitDataCustomizationSpecs.deleteCustomizationSpecs() |
| 190 | + |
| 191 | + |
| 192 | +if __name__ == '__main__': |
| 193 | + main() |
0 commit comments