ソースを参照

禁止删除被 Guest 所依赖的模板镜像

James Iter 8 年 前
コミット
a36555d636
3 ファイル変更37 行追加5 行削除
  1. 1 1
      README.md
  2. 34 2
      api/os_template_image.py
  3. 2 2
      templates/os_templates_image_show.html

+ 1 - 1
README.md

@@ -105,7 +105,7 @@ JimV 是一个,结构清晰简单,易于部署、维护、使用的,低门
    > 存放系统镜像模板,到所有计算节点可以访问到的路径(如果是单个计算节点,可以存放到 /opt/template_images 目录下。如果是多个计算节点,可以通过 NFS 共享该目录。)。
 8. 创建虚拟机模板
    > 在 JimV-C 控制面板中,添加虚拟机模板。
-9. 享受简单、快速开创虚拟机实例的快乐。。。。。
+9. 享受"简单、快速开"创虚拟机实例的快乐。。。。。
 
 ### [JimV-C 手动安装](docs/install.md)
 

+ 34 - 2
api/os_template_image.py

@@ -8,7 +8,7 @@ import jimit as ji
 import json
 
 from api.base import Base
-from models import OSTemplateImage, OSTemplateProfile, Host, Config, OSTemplateImageKind
+from models import OSTemplateImage, OSTemplateProfile, Host, Config, OSTemplateImageKind, Guest
 from models import Rules
 from models import Utils
 
@@ -183,6 +183,7 @@ def r_delete(ids):
 
     os_template_image = OSTemplateImage()
 
+    # TODO: 加入对,是否有被 Guest 引用的判断
     for _id in ids.split(','):
         os_template_image.id = _id
         os_template_image.get()
@@ -226,7 +227,38 @@ def r_get(ids):
 
 @Utils.dumps2response
 def r_get_by_filter():
-    return os_template_image_base.get_by_filter()
+    ret = os_template_image_base.get_by_filter()
+
+    if '200' != ret['state']['code']:
+        return ret
+
+    os_template_images_id = list()
+
+    for os_template_image in ret['data']:
+        os_template_image_id = os_template_image['id']
+
+        if os_template_image_id not in os_template_images_id:
+            os_template_images_id.append(os_template_image_id)
+
+    rows, _ = Guest.get_by_filter(filter_str=':'.join(['os_template_image_id', 'in',
+                                                       ','.join(_id.__str__() for _id in os_template_images_id)]))
+
+    os_template_image_guest_mapping = dict()
+
+    for guest in rows:
+        os_template_image_id = guest['os_template_image_id']
+
+        if os_template_image_id not in os_template_image_guest_mapping:
+            os_template_image_guest_mapping[os_template_image_id] = list()
+
+        os_template_image_guest_mapping[os_template_image_id].append(guest)
+
+    for i, os_template_image in enumerate(ret['data']):
+
+        if os_template_image['id'] in os_template_image_guest_mapping:
+            ret['data'][i]['guests'] = os_template_image_guest_mapping[os_template_image['id']]
+
+    return ret
 
 
 @Utils.dumps2response

+ 2 - 2
templates/os_templates_image_show.html

@@ -623,7 +623,7 @@
                                         </a>
                                     </li>
                                     <li class="divider" style="{% if item.sequence == 0 %} display: none; {% endif %}"></li>
-                                    <li>
+                                    <li class="{% if 'guests' in item %} disabled {% endif %}">
                                         <a href="javascript:;" data-toggle="modal" data-target="#delete_modal"
                                            onclick="$('#os_template_image_id').val($(this).parent().parent().parent().parent().parent().children()[0].textContent);
                                        $('#delete_instance_desc').text($(this).parent().parent().parent().parent().parent().children()[2].textContent)">
@@ -750,7 +750,7 @@
                                         </a>
                                     </li>
                                     <li class="divider" style="{% if item.sequence == 0 %} display: none; {% endif %}"></li>
-                                    <li class="{% if item.progress != 100 %} disabled {% endif %}">
+                                    <li class="{% if item.progress != 100 or 'guests' in item %} disabled {% endif %}">
                                         <a href="javascript:;" data-toggle="modal" data-target="#delete_modal"
                                            onclick="$('#os_template_image_id').val($(this).parent().parent().parent().parent().parent().children()[0].textContent);
                                        $('#delete_instance_desc').text($(this).parent().parent().parent().parent().parent().children()[2].textContent)">