Exemple #1
0
    def vms_to_delete(self):

        removed_VMs = []
        self.esxi_expire_days = int(self.esxi_expire_days)
        vms = self.sdk.get_objects_Virtual_Machines()
        now = datetime.now(pytz.utc)
        for vm in vms:
            vm_info = VM(vm)
            summary = vm_info.summary()
            notes = summary.config.annotation.lower()
            create_date = vm.config.createDate

            if create_date < datetime(2000, 1, 1, tzinfo=pytz.utc):
                continue

            if self.dont_delete_note:
                if self.dont_delete_note.lower() in notes:
                    continue

            delete_note_match = self.delete_note.lower() == notes
            date_check = create_date < (now -
                                        timedelta(days=self.esxi_expire_days))
            if delete_note_match or date_check:
                removed_VMs.append(vm_info)

        return removed_VMs
Exemple #2
0
 def find_by_ip(self, ip_address):
     search_index = self.content().searchIndex
     vm = search_index.FindByIp(datacenter=None,
                                ip=ip_address,
                                vmSearch=True)
     if vm:
         return VM(vm)
Exemple #3
0
 def find_by_host_name(self, host_name):
     search_index = self.content().searchIndex
     vm = search_index.FindByDnsName(
         datacenter=None, dnsName=host_name, vmSearch=True
     )  # note: use host_name since the documentation of this method says "The DNS name for a virtual machine is the one returned from VMware tools, hostName."
     if vm:
         return VM(vm)
    def process(self):
        if not self.mode_tag or not self.start_shut_tag:
            print("`mode_tag` or `start_shut_tag` is not defined on env vars")
            return

        if self.mode_tag == "" or self.start_shut_tag == "":
            print("`mode_tag` or `start_shut_tag` should not be blank")
            return

        altered_vms = []
        vms_o = self._get_vms()
        print(f"total vms : {len(vms_o)}")

        for vm_o in vms_o:
            vm = VM(vm_o)
            altered = self.process_vm(vm)
            if altered:
                altered_vms.append(altered)

        if altered_vms:
            print("Altered VMs: ")
            print("=============")
            print("\n".join(altered_vms))
        else:
            print("No VM was Altered !")
    def vms_to_delete(self):

        removed_VMs             = []
        self.esxi_expire_days   = int(self.esxi_expire_days)
        vms                     = self.sdk.get_objects_Virtual_Machines()
        now                     = datetime.now(pytz.utc)
        for vm in vms:
            vm_info     = VM(vm)
            summary     = vm_info.summary()
            notes       = summary.config.annotation
            create_date = vm.config.createDate

            if create_date < datetime(2000, 1, 1, tzinfo=pytz.utc):
                continue

            if (self.delete_note and self.delete_note.lower() == notes.lower()) or \
                    (create_date < (now - timedelta(days=self.esxi_expire_days)) and (
                            not self.dont_delete_note or self.dont_delete_note.lower() not in notes.lower())):
                removed_VMs.append(vm_info)

        return removed_VMs
Exemple #6
0
 def find_by_uuid(self, uuid, instance_uuid=False):
     search_index = self.content().searchIndex
     if instance_uuid:
         vm = search_index.FindByUuid(datacenter=None,
                                      uuid=uuid,
                                      vmSearch=True,
                                      instanceUuid=True)
     else:
         vm = search_index.FindByUuid(datacenter=None,
                                      uuid=uuid,
                                      vmSearch=True)
     if vm:
         return VM(vm)
Exemple #7
0
    def create(self):
        folder = self.sdk.datacenter_folder()
        resource_pool = self.sdk.resource_pool()

        datastore_path = '[' + self.data_store + '] '  #+ self.vm_name  # add "self.vm_name" here (as seen in samples) was creating an extra folder and was leaving a folder behind

        vmx_file = self.sdk.file_info(vm_path_name=datastore_path)
        # todo :refactor these values below
        config = pyVmomi.vim.vm.ConfigSpec(name=self.vm_name,
                                           memoryMB=self.memory,
                                           numCPUs=1,
                                           files=vmx_file,
                                           guestId=self.guest_id,
                                           version='vmx-07',
                                           deviceChange=self.devices)

        task = folder.CreateVM_Task(config=config, pool=resource_pool)
        Task().wait_for_task(task)

        self.vm = VM(task.info.result)
        return self.vm
Exemple #8
0
 def test_get_object_virtual_machine(self):
     name = self.vm.name()
     vm = VM(self.sdk.get_object_virtual_machine(name))
     assert vm.name() == name
env_path = PurePath(__file__).parent / 'script.env'
load_dotenv(dotenv_path=env_path)
load_dotenv()
rm_note = environ.get('rm_note')
dont_rm_note = environ.get('dont_rm_note')
expire_days_no = environ.get('expire_days_no', '')
if not expire_days_no.isdigit():
    print(" Expire days number must be integer")
    sys.exit(1)
expire_days_no = int(expire_days_no)
sdk = Sdk()
vms_o = sdk.get_objects_Virtual_Machines()
removed_VMs = []
now = datetime.now(pytz.utc)
for vm_o in vms_o:
    vm = VM(vm_o)
    summary = vm.summary()
    info = vm.info()
    state = summary.runtime.powerState
    notes = summary.config.annotation
    create_date = vm_o.config.createDate
    if create_date < datetime(2000, 1, 1):
        continue
    if rm_note.lower() in notes.lower() or (
            create_date < (now - timedelta(days=expire_days_no))
            and dont_rm_note.lower() not in notes.lower()):
        if state == 'poweredOn':
            vm.task().power_off()
            vm.task().delete()
        removed_VMs.append(info["Name"])
Exemple #10
0
 def find_by_name(self, name):
     vm = self.get_object_virtual_machine(name)
     if vm:
         return VM(vm)
Exemple #11
0
 def vms(self):
     vms = []
     for folder in self.folders():
         for vm in folder.childEntity:
             vms.append(VM(vm))
     return vms
Exemple #12
0
 def vm(self, vm_name):
     vm = self.get_object_virtual_machine(vm_name)
     if vm:
         return VM(vm)
Exemple #13
0
 def find_by_uuid(self, uuid):
     search_index = self.content().searchIndex
     vm = search_index.FindByUuid(datacenter=None, uuid=uuid, vmSearch=True)
     if vm:
         return VM(vm)