还是有许多地方可能错了 希望大婶们 看见 给予意见 ! 

这个文件位于\nova\virt,是一个底层的driver.py,源代码如下(和以前一样添加了些注释,另外把我 觉得比较重要的computerDriver类列出来 了,并将下面的每个函数分离 加以注释《见下面图片》!我看见后面好多函数都是继承的ComputerDriver比如nova\virt\libvirt下面的connection.py里面的class LibvirtConnection(driver.ComputeDriver):):

 
  
  1. """  
  2. Driver base-classes:  
  3.  
  4.     (Beginning of) the contract that compute drivers must follow, and shared  
  5.     types that support that contract  
  6. """ 
  7.  
  8. from nova.compute import power_state  
  9.  
  10.  
  11. class InstanceInfo(object):  
  12.     def __init__(self, name, state):  
  13.         self.name = name  
  14.         assert state in power_state.valid_states(), "Bad state: %s" % state  
  15.         self.state = state  
  16.  
  17.  
  18. def block_device_info_get_root(block_device_info):  
  19.     block_device_info = block_device_info or {}  
  20.     return block_device_info.get('root_device_name')  
  21.  
  22.  
  23. def block_device_info_get_swap(block_device_info):  
  24.     block_device_info = block_device_info or {}  
  25.     return block_device_info.get('swap'or {'device_name'None,  
  26.                                              'swap_size'0}  
  27.  
  28.  
  29. def swap_is_usable(swap):  
  30.     return swap and swap['device_name'and swap['swap_size'] > 0 
  31.  
  32.  
  33. def block_device_info_get_ephemerals(block_device_info):  
  34.     block_device_info = block_device_info or {}  
  35.     ephemerals = block_device_info.get('ephemerals'or []  
  36.     return ephemerals  
  37.  
  38.  
  39. def block_device_info_get_mapping(block_device_info):  
  40.     block_device_info = block_device_info or {}  
  41.     block_device_mapping = block_device_info.get('block_device_mapping'or []  
  42.     return block_device_mapping  
  43.  
  44.  
  45. class ComputeDriver(object):  
  46.     """Base class for compute drivers.  
  47.  
  48.     The interface to this class talks in terms of 'instances' (Amazon EC2 and  
  49.     internal Nova terminology), by which we mean 'running virtual machine'  
  50.     (XenAPI terminology) or domain (Xen or libvirt terminology).  
  51.  
  52.     An instance has an ID, which is the identifier chosen by Nova to represent  
  53.     the instance further up the stack.  This is unfortunately also called a  
  54.     'name' elsewhere.  As far as this layer is concerned, 'instance ID' and  
  55.     'instance name' are synonyms.  
  56.  
  57.     Note that the instance ID or name is not human-readable or  
  58.     customer-controlled -- it's an internal ID chosen by Nova.  At the  
  59.     nova.virt layer, instances do not have human-readable names at all -- such  
  60.     things are only known higher up the stack.  
  61.  
  62.     Most virtualization platforms will also have their own identity schemes,  
  63.     to uniquely identify a VM or domain.  These IDs must stay internal to the  
  64.     platform-specific layer, and never escape the connection interface.  The  
  65.     platform-specific layer is responsible for keeping track of which instance  
  66.     ID maps to which platform-specific ID, and vice versa.  
  67.  
  68.     In contrast, the list_disks and list_interfaces calls may return  
  69.     platform-specific IDs.  These identify a specific virtual disk or specific  
  70.     virtual network interface, and these IDs are opaque to the rest of Nova.  
  71.  
  72.     Some methods here take an instance of nova.compute.service.Instance.  This  
  73.     is the datastructure used by nova.compute to store details regarding an  
  74.     instance, and pass them into this layer.  This layer is responsible for  
  75.     translating that generic datastructure into terms that are specific to the  
  76.     virtualization platform.  
  77.  
  78.     """ 
  79.     #此处为了让大家看的明白 将 ComputerDriver的类图 列出来  
  80.  
  81.     def init_host(self, host):  
  82.         """Initialize anything that is necessary for the driver to function,  
  83.         including catching up with currently running VM's on the given host.""" 
  84.         # TODO(Vek): Need to pass context in for access to auth_token  
  85.         raise NotImplementedError()  
  86.  
  87.     def get_info(self, instance_name):  
  88.         """Get the current status of an instance, by name (not ID!)  
  89.  
  90.         Returns a dict containing:  
  91.  
  92.         :state:           the running state, one of the power_state codes  
  93.         :max_mem:         (int) the maximum memory in KBytes allowed  
  94.         :mem:             (int) the memory in KBytes used by the domain  
  95.         :num_cpu:         (int) the number of virtual CPUs for the domain  
  96.         :cpu_time:        (int) the CPU time used in nanoseconds  
  97.         """ 
  98.         # TODO(Vek): Need to pass context in for access to auth_token  
  99.         raise NotImplementedError()  
  100.  
  101.     def list_instances(self):  
  102.         """  
  103.         Return the names of all the instances known to the virtualization  
  104.         layer, as a list.  
  105.         """ 
  106.         # TODO(Vek): Need to pass context in for access to auth_token  
  107.         raise NotImplementedError()  
  108.  
  109.     def list_instances_detail(self):  
  110.         """Return a list of InstanceInfo for all registered VMs""" 
  111.         # TODO(Vek): Need to pass context in for access to auth_token  
  112.         raise NotImplementedError()  
  113.  
  114.     def spawn(self, context, instance,  
  115.               network_info=None, block_device_info=None):  
  116.         """  
  117.         Create a new instance/VM/domain on the virtualization platform.  
  118.  
  119.         Once this successfully completes, the instance should be  
  120.         running (power_state.RUNNING).  
  121.  
  122.         If this fails, any partial instance should be completely  
  123.         cleaned up, and the virtualization platform should be in the state  
  124.         that it was before this call began.  
  125.  
  126.         :param context: security context  
  127.         :param instance: Instance object as returned by DB layer.  
  128.                          This function should use the data there to guide  
  129.                          the creation of the new instance.  
  130.         :param network_info:  
  131.            :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`  
  132.         :param block_device_info:  
  133.         """ 
  134.         raise NotImplementedError()  
  135.  
  136.     def destroy(self, instance, network_info, cleanup=True):  
  137.         """Destroy (shutdown and delete) the specified instance.  
  138.  
  139.         If the instance is not found (for example if networking failed), this  
  140.         function should still succeed.  It's probably a good idea to log a  
  141.         warning in that case.  
  142.  
  143.         :param instance: Instance object as returned by DB layer.  
  144.         :param network_info:  
  145.            :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`  
  146.         :param cleanup:  
  147.  
  148.         """ 
  149.         # TODO(Vek): Need to pass context in for access to auth_token  
  150.         raise NotImplementedError()  
  151.  
  152.     def reboot(self, instance, network_info):  
  153.         """Reboot the specified instance.  
  154.  
  155.         :param instance: Instance object as returned by DB layer.  
  156.         :param network_info:  
  157.            :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`  
  158.         """ 
  159.         # TODO(Vek): Need to pass context in for access to auth_token  
  160.         raise NotImplementedError()  
  161.  
  162.     def snapshot_instance(self, context, instance_id, p_w_picpath_id):  
  163.         raise NotImplementedError()  
  164.  
  165.     def get_console_pool_info(self, console_type):  
  166.         # TODO(Vek): Need to pass context in for access to auth_token  
  167.         raise NotImplementedError()  
  168.  
  169.     def get_console_output(self, instance):  
  170.         # TODO(Vek): Need to pass context in for access to auth_token  
  171.         raise NotImplementedError()  
  172.  
  173.     def get_ajax_console(self, instance):  
  174.         # TODO(Vek): Need to pass context in for access to auth_token  
  175.         raise NotImplementedError()  
  176.  
  177.     def get_diagnostics(self, instance):  
  178.         """Return data about VM diagnostics""" 
  179.         # TODO(Vek): Need to pass context in for access to auth_token  
  180.         raise NotImplementedError()  
  181.  
  182.     def get_host_ip_addr(self):  
  183.         """  
  184.         Retrieves the IP address of the dom0  
  185.         """ 
  186.         # TODO(Vek): Need to pass context in for access to auth_token  
  187.         raise NotImplementedError()  
  188.  
  189.     def attach_volume(self, context, instance_id, volume_id, mountpoint):  
  190.         """Attach the disk at device_path to the instance at mountpoint""" 
  191.         raise NotImplementedError()  
  192.  
  193.     def detach_volume(self, context, instance_id, volume_id):  
  194.         """Detach the disk attached to the instance at mountpoint""" 
  195.         raise NotImplementedError()  
  196.  
  197.     def compare_cpu(self, cpu_info):  
  198.         """Compares given cpu info against host 确保vm能运行  
  199.  
  200.         Before attempting to migrate a VM to this host,  
  201.         compare_cpu is called to ensure that the VM will  
  202.         actually run here.  
  203.  
  204.         :param cpu_info: (str) JSON structure describing the source CPU.  
  205.         :returns: None if migration is acceptable  
  206.         :raises: :py:class:`~nova.exception.InvalidCPUInfo` if migration  
  207.                  is not acceptable.  
  208.         """ 
  209.         raise NotImplementedError()  
  210.  
  211.     def migrate_disk_and_power_off(self, instance, dest):  
  212.         """  
  213.         Transfers the disk of a running instance in multiple phases, turning  
  214.         off the instance before the end.  
  215.         """ 
  216.         # TODO(Vek): Need to pass context in for access to auth_token  
  217.         raise NotImplementedError()  
  218.  
  219.     def snapshot(self, context, instance, p_w_picpath_id):  
  220.         """  
  221.         Snapshots the specified instance.  
  222.  
  223.         :param context: security context  
  224.         :param instance: Instance object as returned by DB layer.  
  225.         :param p_w_picpath_id: Reference to a pre-created p_w_picpath that will  
  226.                          hold the snapshot.  
  227.         """ 
  228.         raise NotImplementedError()  
  229.  
  230.     def finish_migration(self, context, instance, disk_info, network_info,  
  231.                          resize_instance):  
  232.         #打开一个迁移的实例,完成一个调整  
  233.         """Completes a resize, turning on the migrated instance  
  234.  
  235.         :param network_info:  
  236.            :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`  
  237.         """ 
  238.         raise NotImplementedError()  
  239.  
  240.     def revert_migration(self, instance):  
  241.         #返回一个调整,推动回到实例?  
  242.         """Reverts a resize, powering back on the instance""" 
  243.         # TODO(Vek): Need to pass context in for access to auth_token  
  244.         raise NotImplementedError()  
  245.  
  246.     def pause(self, instance, callback):  
  247.         """Pause the specified instance.""" 
  248.         # TODO(Vek): Need to pass context in for access to auth_token  
  249.         raise NotImplementedError()  
  250.  
  251.     def unpause(self, instance, callback):  
  252.         """Unpause paused VM instance""" 
  253.         # TODO(Vek): Need to pass context in for access to auth_token  
  254.         raise NotImplementedError()  
  255.  
  256.     def suspend(self, instance, callback):  
  257.         # 挂起指定的实例  
  258.         """suspend the specified instance""" 
  259.         # TODO(Vek): Need to pass context in for access to auth_token  
  260.         raise NotImplementedError()  
  261.  
  262.     def resume(self, instance, callback):  
  263.         """resume the specified instance""" 
  264.         # TODO(Vek): Need to pass context in for access to auth_token  
  265.         raise NotImplementedError()  
  266.  
  267.     def rescue(self, context, instance, callback, network_info):  
  268.         #恢复指定的实例  
  269.         """Rescue the specified instance""" 
  270.         raise NotImplementedError()  
  271.  
  272.     def unrescue(self, instance, callback, network_info):  
  273.         """Unrescue the specified instance""" 
  274.         # TODO(Vek): Need to pass context in for access to auth_token  
  275.         raise NotImplementedError()  
  276.  
  277.     def update_available_resource(self, ctxt, host):  
  278.         #在computeNode表中更新电脑资源管理的信息  
  279.         """Updates compute manager resource info on ComputeNode table.  
  280.  
  281.         This method is called when nova-compute launches, and  
  282.         whenever admin executes "nova-manage service update_resource".  
  283.  
  284.         :param ctxt: security context  
  285.         :param host: hostname that compute manager is currently running  
  286.  
  287.         """ 
  288.         # TODO(Vek): Need to pass context in for access to auth_token  
  289.         raise NotImplementedError()  
  290.  
  291.     def live_migration(self, ctxt, instance_ref, dest,  
  292.                        post_method, recover_method):  
  293.         #分发处理高负荷,当有高负荷操作时候,大量生成 live_mirgration  
  294.         """Spawning live_migration operation for distributing high-load.  
  295.  
  296.         :param ctxt: security context  
  297.         :param instance_ref:  
  298.             nova.db.sqlalchemy.models.Instance object  
  299.             instance object that is migrated.  
  300.         :param dest: destination host  
  301.         :param post_method:  
  302.             post operation method.  
  303.             expected nova.compute.manager.post_live_migration.  
  304.         :param recover_method:  
  305.             recovery method when any exception occurs.  
  306.             expected nova.compute.manager.recover_live_migration.  
  307.  
  308.         """ 
  309.         # TODO(Vek): Need to pass context in for access to auth_token  
  310.         raise NotImplementedError()  
  311.  
  312.     def refresh_security_group_rules(self, security_group_id):  
  313.         #改变安全组之后调用  
  314.         """This method is called after a change to security groups.  
  315.  
  316.         All security groups and their associated rules live in the datastore,  
  317.         and calling this method should apply the updated rules to instances  
  318.         running the specified security group.  
  319.  
  320.         An error should be raised if the operation cannot complete.  
  321.  
  322.         """ 
  323.         # TODO(Vek): Need to pass context in for access to auth_token  
  324.         raise NotImplementedError()  
  325.  
  326.     def refresh_security_group_members(self, security_group_id):  
  327.         #当一个安全组被添加到一个实例,这个方法被调用  
  328.         """This method is called when a security group is added to an instance.  
  329.  
  330.         This message is sent to the virtualization drivers on hosts that are  
  331.         running an instance that belongs to a security group that has a rule  
  332.         that references the security group identified by `security_group_id`.  
  333.         It is the responsiblity of this method to make sure any rules  
  334.         that authorize traffic flow with members of the security group are  
  335.         updated and any new members can communicate, and any removed members  
  336.         cannot.  
  337.  
  338.         Scenario:  
  339.             * we are running on host 'H0' and we have an instance 'i-0'.  
  340.             * instance 'i-0' is a member of security group 'speaks-b'  
  341.             * group 'speaks-b' has an ingress rule that authorizes group 'b'  
  342.             * another host 'H1' runs an instance 'i-1'  
  343.             * instance 'i-1' is a member of security group 'b'  
  344.  
  345.             When 'i-1' launches or terminates we will recieve the message  
  346.             to update members of group 'b', at which time we will make  
  347.             any changes needed to the rules for instance 'i-0' to allow  
  348.             or deny traffic coming from 'i-1', depending on if it is being  
  349.             added or removed from the group.  
  350.  
  351.         In this scenario, 'i-1' could just as easily have been running on our  
  352.         host 'H0' and this method would still have been called.  The point was  
  353.         that this method isn't called on the host where instances of that  
  354.         group are running (as is the case with  
  355.         :method:`refresh_security_group_rules`) but is called where references  
  356.         are made to authorizing those instances.  
  357.  
  358.         An error should be raised if the operation cannot complete.  
  359.  
  360.         """ 
  361.         # TODO(Vek): Need to pass context in for access to auth_token  
  362.         raise NotImplementedError()  
  363.  
  364.     def refresh_provider_fw_rules(self, security_group_id):  
  365.         #这触发一个基于数据库改变的防火墙的更新  
  366.         """This triggers a firewall update based on database changes.  
  367.  
  368.         When this is called, rules have either been added or removed from the  
  369.         datastore.  You can retrieve rules with  
  370.         :method:`nova.db.api.provider_fw_rule_get_all`.  
  371.  
  372.         Provider rules take precedence over security group rules.  If an IP  
  373.         would be allowed by a security group ingress rule, but blocked by  
  374.         a provider rule, then packets from the IP are dropped.  This includes  
  375.         intra-project traffic in the case of the allow_project_net_traffic  
  376.         flag for the libvirt-derived classes.  
  377.  
  378.         """ 
  379.         # TODO(Vek): Need to pass context in for access to auth_token  
  380.         raise NotImplementedError()  
  381.  
  382.     def reset_network(self, instance):  
  383.         """reset networking for specified instance""" 
  384.         # TODO(Vek): Need to pass context in for access to auth_token  
  385.         pass 
  386.  
  387.     def ensure_filtering_rules_for_instance(self, instance_ref, network_info):  
  388.         #设置过滤规则,并等待它完成  
  389.         """Setting up filtering rules and waiting for its completion.  
  390.  
  391.         To migrate an instance, filtering rules to hypervisors  
  392.         and firewalls are inevitable on destination host.  
  393.         ( Waiting only for filtering rules to hypervisor,  
  394.         since filtering rules to firewall rules can be set faster).  
  395.  
  396.         Concretely, the below method must be called.  
  397.         - setup_basic_filtering (for nova-basic, etc.)  
  398.         - prepare_instance_filter(for nova-instance-instance-xxx, etc.)  
  399.  
  400.         to_xml may have to be called since it defines PROJNET, PROJMASK.  
  401.         but libvirt migrates those value through migrateToURI(),  
  402.         so , no need to be called.  
  403.  
  404.         Don't use thread for this method since migration should  
  405.         not be started when setting-up filtering rules operations  
  406.         are not completed.  
  407.  
  408.         :params instance_ref: nova.db.sqlalchemy.models.Instance object  
  409.  
  410.         """ 
  411.         # TODO(Vek): Need to pass context in for access to auth_token  
  412.         raise NotImplementedError()  
  413.  
  414.     def unfilter_instance(self, instance, network_info):  
  415.         """Stop filtering instance""" 
  416.         # TODO(Vek): Need to pass context in for access to auth_token  
  417.         raise NotImplementedError()  
  418.  
  419.     def set_admin_password(self, context, instance_id, new_pass=None):  
  420.         """  
  421.         Set the root password on the specified instance.  
  422.  
  423.         The first parameter is an instance of nova.compute.service.Instance,  
  424.         and so the instance is being specified as instance.name. The second  
  425.         parameter is the value of the new password.  
  426.         """ 
  427.         raise NotImplementedError()  
  428.  
  429.     def inject_file(self, instance, b64_path, b64_contents):  
  430.         """在指定的实例上写文件  
  431.         Writes a file on the specified instance.  
  432.  
  433.         The first parameter is an instance of nova.compute.service.Instance,  
  434.         and so the instance is being specified as instance.name. The second  
  435.         parameter is the base64-encoded path to which the file is to be  
  436.         written on the instance; the third is the contents of the file, also  
  437.         base64-encoded.  
  438.         """ 
  439.         # TODO(Vek): Need to pass context in for access to auth_token  
  440.         raise NotImplementedError()  
  441.  
  442.     def agent_update(self, instance, url, md5hash):  
  443.         #在指定的实例中更新代理  
  444.         """  
  445.         Update agent on the specified instance.  
  446.  
  447.         The first parameter is an instance of nova.compute.service.Instance,  
  448.         and so the instance is being specified as instance.name. The second  
  449.         parameter is the URL of the agent to be fetched and updated on the  
  450.         instance; the third is the md5 hash of the file for verification  
  451.         purposes.  
  452.         """ 
  453.         # TODO(Vek): Need to pass context in for access to auth_token  
  454.         raise NotImplementedError()  
  455.  
  456.     def inject_network_info(self, instance, nw_info):  
  457.         #为指定的实例注入网络信息  
  458.         """inject network info for specified instance""" 
  459.         # TODO(Vek): Need to pass context in for access to auth_token  
  460.         pass 
  461.  
  462.     def poll_rescued_instances(self, timeout):  
  463.         #轮询已经恢复的实例  
  464.         """Poll for rescued instances""" 
  465.         # TODO(Vek): Need to pass context in for access to auth_token  
  466.         raise NotImplementedError()  
  467.  
  468.     def host_power_action(self, host, action):  
  469.         """Reboots, shuts down or powers up the host.""" 
  470.         raise NotImplementedError()  
  471.  
  472.     def set_host_enabled(self, host, enabled):  
  473.         #设置指定的主机有能力接受新的实例  
  474.         """Sets the specified host's ability to accept new instances.""" 
  475.         # TODO(Vek): Need to pass context in for access to auth_token  
  476.         raise NotImplementedError()  
  477.  
  478.     def plug_vifs(self, instance, network_info):  
  479.         """Plugs in VIFs to networks.""" 
  480.         # TODO(Vek): Need to pass context in for access to auth_token  
  481.         raise NotImplementedError()  
  482.  
  483.     def update_host_status(self):  
  484.         """Refresh host stats""" 
  485.         raise NotImplementedError()  
  486.  
  487.     def get_host_stats(self, refresh=False):  
  488.         """Return currently known host stats""" 
  489.         raise NotImplementedError()  
  490.  
  491.     def list_disks(self, instance_name):  
  492.         """  
  493.         Return the IDs of all the virtual disks attached to the specified  
  494.         instance, as a list.  These IDs are opaque to the caller (they are  
  495.         only useful for giving back to this layer as a parameter to  
  496.         disk_stats).  These IDs only need to be unique for a given instance.  
  497.  
  498.         Note that this function takes an instance ID.  
  499.         """ 
  500.         raise NotImplementedError()  
  501.  
  502.     def list_interfaces(self, instance_name):  
  503.         """  
  504.         Return the IDs of all the virtual network interfaces attached to the  
  505.         specified instance, as a list.  These IDs are opaque to the caller  
  506.         (they are only useful for giving back to this layer as a parameter to  
  507.         interface_stats).  These IDs only need to be unique for a given  
  508.         instance.  
  509.  
  510.         Note that this function takes an instance ID.  
  511.         """ 
  512.         raise NotImplementedError()  
  513.  
  514.     def resize(self, instance, flavor):  
  515.         """  
  516.         Resizes/Migrates the specified instance.  
  517.  
  518.         The flavor parameter determines whether or not the instance RAM and  
  519.         disk space are modified, and if so, to what size.  
  520.         """ 
  521.         raise NotImplementedError()  
  522.  
  523.     def block_stats(self, instance_name, disk_id):  
  524.         """  
  525.         Return performance counters associated with the given disk_id on the  
  526.         given instance_name.  These are returned as [rd_req, rd_bytes, wr_req,  
  527.         wr_bytes, errs], where rd indicates read, wr indicates write, req is  
  528.         the total number of I/O requests made, bytes is the total number of  
  529.         bytes transferred, and errs is the number of requests held up due to a  
  530.         full pipeline.  
  531.  
  532.         All counters are long integers.  
  533.  
  534.         This method is optional.  On some platforms (e.g. XenAPI) performance  
  535.         statistics can be retrieved directly in aggregate form, without Nova  
  536.         having to do the aggregation.  On those platforms, this method is  
  537.         unused.  
  538.  
  539.         Note that this function takes an instance ID.  
  540.         """ 
  541.         raise NotImplementedError()  
  542.  
  543.     def interface_stats(self, instance_name, iface_id):  
  544.         """  
  545.         Return performance counters associated with the given iface_id on the  
  546.         given instance_id.  These are returned as [rx_bytes, rx_packets,  
  547.         rx_errs, rx_drop, tx_bytes, tx_packets, tx_errs, tx_drop], where rx  
  548.         indicates receive, tx indicates transmit, bytes and packets indicate  
  549.         the total num

     

  550. ber of bytes or packets transferred, and errs and dropped  
  551.         is the total number of packets failed / dropped.  
  552.  
  553.         All counters are long integers.  
  554.  
  555.         This method is optional.  On some platforms (e.g. XenAPI) performance  
  556.         statistics can be retrieved directly in aggregate form, without Nova  
  557.         having to do the aggregation.  On those platforms, this method is  
  558.         unused.  
  559.  
  560.         Note that this function takes an instance ID.  
  561.         """ 
  562.         raise NotImplementedError()  

1:

 

 

2:

3:

4:
4