ソースを参照

修正基于node_id计算节点选择的bug

James Iter 8 年 前
コミット
8c481629e3
1 ファイル変更14 行追加6 行削除
  1. 14 6
      api/guest.py

+ 14 - 6
api/guest.py

@@ -120,7 +120,13 @@ def r_create():
             ret['state'] = ji.Common.exchange_state(50351)
             return ret
 
-        if node_id is not None and node_id not in [host['node_id'] for host in available_hosts]:
+        available_hosts_mapping_by_node_id = dict()
+
+        for host in available_hosts:
+            if host['node_id'] not in available_hosts_mapping_by_node_id:
+                available_hosts_mapping_by_node_id[host['node_id']] = host
+
+        if node_id is not None and node_id not in available_hosts_mapping_by_node_id:
             ret['state'] = ji.Common.exchange_state(50351)
             return ret
 
@@ -194,17 +200,19 @@ def r_create():
             disk.quota(config=config)
             disk.create()
 
-            # 在可用计算节点中平均分配任务
-            chosen_host = available_hosts[quantity % available_hosts.__len__()]
+            if node_id is None:
+                # 在可用计算节点中平均分配任务
+                chosen_host = available_hosts[quantity % available_hosts.__len__()]
+
+            else:
+                chosen_host = available_hosts_mapping_by_node_id[node_id]
+
             guest.node_id = chosen_host['node_id']
 
             guest_xml = GuestXML(host=chosen_host, guest=guest, disk=disk, config=config,
                                  os_type=os_template_profile.os_type)
             guest.xml = guest_xml.get_domain()
 
-            if node_id is not None:
-                guest.node_id = node_id
-
             guest.node_id = int(guest.node_id)
             guest.create()