| 1 |  |  | #!/usr/bin/env python | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | # Licensed to the StackStorm, Inc ('StackStorm') under one or more | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | # contributor license agreements.  See the NOTICE file distributed with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | # this work for additional information regarding copyright ownership. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | # The ASF licenses this file to You under the Apache License, Version 2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | # (the "License"); you may not use this file except in compliance with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | # the License.  You may obtain a copy of the License at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | #     http://www.apache.org/licenses/LICENSE-2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | # Unless required by applicable law or agreed to in writing, software | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | # distributed under the License is distributed on an "AS IS" BASIS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | # See the License for the specific language governing permissions and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | # limitations under the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | from pyVmomi import vim | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | from vmwarelib import inventory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | from vmwarelib import checkinputs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | from vmwarelib.actions import BaseAction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 25 |  |  | class VMCreateBareBones(BaseAction): | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |     def run(self, vm_name, cluster, datastore_cluster, | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |             datastore, resourcepool, cpu_size, ram_size, | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |             guestos, version, description): | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |         # Setup Identifiers for objects | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |         si = self.si | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |         si_content = si.RetrieveContent() | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |         # checkinputs.vm_storage(datastore_cluster, datastore) | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |         checkinputs.one_of_two_strings(datastore_cluster, | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |                                        datastore, | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |                                        "Datastore Cluster or Datastore") | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |         data_center = self.si_content.rootFolder.childEntity[0] | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |         cluster = inventory.get_cluster(self.si_content, name=cluster) | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |         data_store_cluster = inventory.get_datastore_cluster( | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |             self.si_content, | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |             name=datastore_cluster) | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |         # data_store = inventory.get_datastore(self.si_content, name=datastore) | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |         target_folder = data_center.vmFolder | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |         # If No Resource Pool issued the Default one for | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |         # the Cluster will be selected. | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |         if resourcepool: | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |             resource_pool = inventory.get_resource_pool(self.si_content, | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |                                                         name=resourcepool) | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |         else: | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |             resource_pool = cluster.resourcePool | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |         # Config created that is required for DS selection | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         # Config is BareBones, CPU RAM no more. | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |         config = vim.vm.ConfigSpec(name=vm_name, | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |                                    memoryMB=(ram_size * 1024), | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |                                    numCPUs=cpu_size, | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |                                    guestId=guestos, | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |                                    version=version, | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |                                    cpuHotAddEnabled=True, | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |                                    memoryHotAddEnabled=True, | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |                                    annotation=description) | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |         # if Datastore cluster is provided it will find the | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |         # recommended Datastore to store VM files | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |         if datastore_cluster: | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |             podsel = vim.storageDrs.PodSelectionSpec() | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |             podsel.storagePod = data_store_cluster | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |             storage_spec = vim.storageDrs.StoragePlacementSpec( | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |                 type='create', | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |                 configSpec=config, | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |                 resourcePool=resource_pool, | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |                 podSelectionSpec=podsel, | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |                 folder=target_folder) | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |             # Create Instance of Storage Resource Manager - | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |             # This is used to identify a Recommended Datastore from a Cluster | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |             srm = si_content.storageResourceManager | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |             results = srm.RecommendDatastores(storageSpec=storage_spec) | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |             rec_ds = results.recommendations[0].action[0]\ | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |                 .relocateSpec.datastore | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |             datastore_path = '[' + rec_ds.name + '] ' + vm_name | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |         else: | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |             # No Datastore Cluster has been offered so using the D | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |             if datastore: | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |                 datastore_path = '[' + datastore + '] ' + vm_name | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |             else: | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |                 raise Exception('Error No Storage Data Provided') | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |         # Now Datastore is known the remaining | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |         # of VM Config can be setup and added | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |         vmx_file = vim.vm.FileInfo(logDirectory=None, | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |                                    snapshotDirectory=None, | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |                                    suspendDirectory=None, | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |                                    vmPathName=datastore_path) | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |         config.files = vmx_file | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |         # Create task to Build actual Machine | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |         task = target_folder.CreateVM_Task(config=config, pool=resource_pool) | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |         self._wait_for_task(task) | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |         if task.info.state != vim.TaskInfo.State.success: | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |             raise Exception(task.info.error.msg) | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |         return {'vm_id': task.info.result._moId} | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |  |