예제 #1
0
 def folder_recfg(self):
     """ Move a VM to another folder """
     host = Query.get_obj(self.virtual_machines.view, self.opts.name)
     folder = Query.folders_lookup(self.datacenters.view,
                                   self.opts.datacenter, self.opts.folder)
     self.logger.info('%s folder: %s', host.name, self.opts.folder)
     self.mvfolder(host, folder)
예제 #2
0
 def folder_recfg(self):
     """ Move a VM to another folder """
     host = Query.get_obj(self.virtual_machines.view, self.opts.name)
     folder = Query.folders_lookup(
         self.datacenters.view, self.opts.datacenter, self.opts.folder
     )
     self.logger.info('%s folder: %s', host.name, self.opts.folder)
     self.mvfolder(host, folder)
예제 #3
0
    def create_wrapper(self, **spec):
        """
        Wrapper method for creating VMs. If certain information was
        not provided in the yaml config (like a datastore), then the client
        will be prompted to select one inside the cfg_checker method.

        Args:
            yaml_cfg (file): A yaml file containing the necessary information
                for creating a new VM. This file will override the defaults set
                in the dotrc file.
        """

        # create a copy before manipulating the data for vsphere
        server_cfg = copy.deepcopy(spec)

        cluster = spec['vmconfig']['cluster']
        datastore = spec['vmconfig']['datastore']
        folder = spec['vmconfig']['folder']

        del server_cfg['general']['passwd']

        self.logger.info('vmconfig %s', server_cfg)
        cluster_obj = Query.get_obj(self.clusters.view, cluster)

        # list of cdrom and disk devices
        devices = []

        # add the cdrom device
        devices.append(self.cdrom_config())

        scsis = []
        if isinstance(spec['vmconfig']['disks'], dict):
            for scsi, disks in spec['vmconfig']['disks'].iteritems():
                scsis.append(self.scsi_config(scsi))
                devices.append(scsis[scsi][1])
                for disk in enumerate(disks):
                    disk_cfg_opts = {}
                    disk_cfg_opts.update({
                        'container': cluster_obj.datastore,
                        'datastore': datastore,
                        'size': int(disk[1]) * (1024 * 1024),
                        'controller': scsis[scsi][0],
                        'unit': disk[0],
                    })
                    devices.append(self.disk_config(**disk_cfg_opts))
        else:
            # attach up to four disks, each on its own scsi adapter
            for scsi, disk in enumerate(spec['vmconfig']['disks']):
                scsis.append(self.scsi_config(scsi))
                devices.append(scsis[scsi][1])
                disk_cfg_opts = {}
                disk_cfg_opts.update({
                    'container': cluster_obj.datastore,
                    'datastore': datastore,
                    'size': int(disk) * (1024 * 1024),
                    'controller': scsis[scsi][0],
                    'unit': 0,
                })
                devices.append(self.disk_config(**disk_cfg_opts))

        # configure each network and add to devices
        for nic in spec['vmconfig']['nics']:
            nic_cfg_opts = {}
            nic_cfg_opts.update({
                'container': cluster_obj.network,
                'network': nic
            })
            devices.append(self.nic_config(**nic_cfg_opts))

        spec['vmconfig'].update({'deviceChange': devices})

        folder = Query.folders_lookup(self.datacenters.view,
                                      self.opts.datacenter, folder)

        # delete items that are no longer needed

        # delete keys that vSphere does not understand, so we can pass it a
        # dictionary to build the VM.
        del spec['vmconfig']['disks']
        del spec['vmconfig']['nics']
        del spec['vmconfig']['folder']
        del spec['vmconfig']['datastore']
        del spec['vmconfig']['datacenter']
        del spec['vmconfig']['cluster']

        pool = cluster_obj.resourcePool

        self.logger.debug(folder, datastore, pool, devices, spec)
        self.create(folder, datastore, pool, **spec['vmconfig'])

        return server_cfg
예제 #4
0
    def create_wrapper(self, **spec):
        """
        Wrapper method for creating VMs. If certain information was
        not provided in the yaml config (like a datastore), then the client
        will be prompted to select one inside the cfg_checker method.

        Args:
            yaml_cfg (file): A yaml file containing the necessary information
                for creating a new VM. This file will override the defaults set
                in the dotrc file.
        """

        # create a copy before manipulating the data for vsphere
        server_cfg = copy.deepcopy(spec)

        cluster = spec['vmconfig']['cluster']
        datastore = spec['vmconfig']['datastore']
        folder = spec['vmconfig']['folder']

        if server_cfg.get('general', None):
            del server_cfg['general']['passwd']

        self.logger.info('vmconfig %s', server_cfg)
        cluster_obj = Query.get_obj(self.clusters.view, cluster)

        # list of cdrom and disk devices
        devices = []

        # add the cdrom device
        devices.append(self.cdrom_config())

        scsis = []
        if isinstance(spec['vmconfig']['disks'], dict):
            for scsi, disks in spec['vmconfig']['disks'].items():
                scsis.append(self.scsi_config(scsi))
                devices.append(scsis[scsi][1])
                for disk in enumerate(disks):
                    disk_cfg_opts = {}
                    disk_cfg_opts.update(
                        {
                            'container' : cluster_obj.datastore,
                            'datastore' : datastore,
                            'size' : int(disk[1]) * (1024*1024),
                            'controller' : scsis[scsi][0],
                            'unit' : disk[0],
                        }
                    )
                    devices.append(self.disk_config(**disk_cfg_opts))
        else:
            # attach up to four disks, each on its own scsi adapter
            for scsi, disk in enumerate(spec['vmconfig']['disks']):
                scsis.append(self.scsi_config(scsi))
                devices.append(scsis[scsi][1])
                disk_cfg_opts = {}
                disk_cfg_opts.update(
                    {
                        'container' : cluster_obj.datastore,
                        'datastore' : datastore,
                        'size' : int(disk) * (1024*1024),
                        'controller' : scsis[scsi][0],
                        'unit' : 0,
                    }
                )
                devices.append(self.disk_config(**disk_cfg_opts))

        # configure each network and add to devices
        for nic in spec['vmconfig']['nics']:
            nic_cfg_opts = {}

            if spec['vmconfig'].get('switch_type', None) == 'distributed':
                nic_cfg_opts.update({'switch_type' : 'distributed'})

            nic_cfg_opts.update({'container' : cluster_obj.network, 'network' : nic})
            devices.append(self.nic_config(**nic_cfg_opts))

        spec['vmconfig'].update({'deviceChange':devices})

        if self.opts.datacenter:
            folder = Query.folders_lookup(
                self.datacenters.view, self.opts.datacenter, folder
            )
        else:
            folder = Query.folders_lookup(
                self.datacenters.view, spec['vmconfig']['datacenter'], folder
            )

        # delete keys that vSphere does not understand, so we can pass it a
        # dictionary to build the VM.
        del spec['vmconfig']['disks']
        del spec['vmconfig']['nics']
        del spec['vmconfig']['folder']
        del spec['vmconfig']['datastore']
        del spec['vmconfig']['datacenter']
        del spec['vmconfig']['cluster']

        if spec['vmconfig'].get('switch_type', None):
            del spec['vmconfig']['switch_type']

        pool = cluster_obj.resourcePool

        self.logger.debug(folder, datastore, pool, devices, spec)
        self.create(folder, datastore, pool, **spec['vmconfig'])

        return server_cfg