Pārlūkot izejas kodu

实现删除ssh-key时,一并解除已被注入该ssh-key的虚拟机关联

James Iter 8 gadi atpakaļ
vecāks
revīzija
8c9636c12a
4 mainītis faili ar 60 papildinājumiem un 56 dzēšanām
  1. 59 49
      api/ssh_key.py
  2. 0 5
      api_route_table.py
  3. 0 1
      models/rules.py
  4. 1 1
      templates/ssh_keys_show.html

+ 59 - 49
api/ssh_key.py

@@ -9,7 +9,7 @@ import json
 import jimit as ji
 
 from api.base import Base
-from models import SSHKey, OSTemplateImage, OSTemplateProfile
+from models import SSHKey, OSTemplateImage, OSTemplateProfile, GuestState
 from models import SSHKeyGuestMapping
 from models import Guest
 from models import Utils
@@ -131,9 +131,66 @@ def r_content_search():
     return ssh_key_base.content_search()
 
 
+def update_ssh_key(uuid):
+
+    guest = Guest()
+    guest.uuid = uuid
+    guest.get_by('uuid')
+
+    # 不支持更新离线虚拟机的 SSH-KEY
+    if guest.status != GuestState.running.value:
+        return
+
+    os_template_image = OSTemplateImage()
+    os_template_profile = OSTemplateProfile()
+
+    os_template_image.id = guest.os_template_image_id
+    os_template_image.get()
+
+    os_template_profile.id = os_template_image.os_template_profile_id
+    os_template_profile.get()
+
+    # 不支持更新 Windows 虚拟机的 SSH-KEY
+    if os_template_profile.os_type == 'windows':
+        return
+
+    rows, _ = SSHKeyGuestMapping.get_by_filter(filter_str=':'.join(['guest_uuid', 'eq', uuid]))
+
+    ssh_keys_id = list()
+    for row in rows:
+        ssh_keys_id.append(row['ssh_key_id'].__str__())
+
+    ssh_keys = list()
+
+    if ssh_keys_id.__len__() > 0:
+        rows, _ = SSHKey.get_by_filter(filter_str=':'.join(['id', 'in', ','.join(ssh_keys_id)]))
+        for row in rows:
+            ssh_keys.append(row['public_key'])
+
+    else:
+        ssh_keys.append('')
+
+    message = {
+        '_object': 'guest',
+        'uuid': uuid,
+        'node_id': guest.node_id,
+        'action': 'update_ssh_key',
+        'ssh_keys': ssh_keys,
+        'os_type': os_template_profile.os_type,
+        'passback_parameters': {'uuid': uuid, 'ssh_keys': ssh_keys, 'os_type': os_template_profile.os_type}
+    }
+
+    Utils.emit_instruction(message=json.dumps(message, ensure_ascii=False))
+
+
 @Utils.dumps2response
 def r_delete(ids):
-    # TODO: 做好依赖逻辑处理。比如已关联 ssh_key 的删除。
+    rows, _ = SSHKeyGuestMapping.get_by_filter(filter_str=':'.join(['ssh_key_id', 'in', ids]))
+    SSHKeyGuestMapping.delete_by_filter(filter_str=':'.join(['ssh_key_id', 'in', ids]))
+
+    for row in rows:
+        update_ssh_key(uuid=row['guest_uuid'])
+
     return ssh_key_base.delete(ids=ids, ids_rule=Rules.IDS.value, by_field='id')
 
 
@@ -193,53 +250,6 @@ def r_unbound(ssh_key_id):
         return json.loads(e.message)
 
 
-def update_ssh_key(uuid):
-
-    guest = Guest()
-    guest.uuid = uuid
-    guest.get_by('uuid')
-
-    os_template_image = OSTemplateImage()
-    os_template_profile = OSTemplateProfile()
-
-    os_template_image.id = guest.os_template_image_id
-    os_template_image.get()
-
-    os_template_profile.id = os_template_image.os_template_profile_id
-    os_template_profile.get()
-
-    if os_template_profile.os_type == 'windows':
-        return
-
-    rows, _ = SSHKeyGuestMapping.get_by_filter(filter_str=':'.join(['guest_uuid', 'eq', uuid]))
-
-    ssh_keys_id = list()
-    for row in rows:
-        ssh_keys_id.append(row['ssh_key_id'].__str__())
-
-    ssh_keys = list()
-
-    if ssh_keys_id.__len__() > 0:
-        rows, _ = SSHKey.get_by_filter(filter_str=':'.join(['id', 'in', ','.join(ssh_keys_id)]))
-        for row in rows:
-            ssh_keys.append(row['public_key'])
-
-    else:
-        ssh_keys.append('')
-
-    message = {
-        '_object': 'guest',
-        'uuid': uuid,
-        'node_id': guest.node_id,
-        'action': 'update_ssh_key',
-        'ssh_keys': ssh_keys,
-        'os_type': os_template_profile.os_type,
-        'passback_parameters': {'uuid': uuid, 'ssh_keys': ssh_keys, 'os_type': os_template_profile.os_type}
-    }
-
-    Utils.emit_instruction(message=json.dumps(message, ensure_ascii=False))
-
-
 @Utils.dumps2response
 def r_bind(ssh_key_id, uuids):
 

+ 0 - 5
api_route_table.py

@@ -102,11 +102,6 @@ add_rule_api(guest.blueprints, '', api_func='guest.r_get_by_filter', methods=['G
 add_rule_api(guest.blueprints, '/_search', api_func='guest.r_content_search', methods=['GET'])
 add_rule_api(guest.blueprint, '/<uuid>', api_func='guest.r_update', methods=['PATCH'])
 add_rule_api(guest.blueprints, '/<uuids>', api_func='guest.r_delete', methods=['DELETE'])
-add_rule_api(guest.blueprints, '/_boot_jobs/<uuids>/<boot_jobs_id>', api_func='guest.r_add_boot_jobs', methods=['PUT'])
-add_rule_api(guest.blueprints, '/_boot_jobs/<uuids>', api_func='guest.r_get_boot_jobs', methods=['GET'])
-add_rule_api(guest.blueprints, '/_boot_jobs/<uuids>/<boot_jobs_id>', api_func='guest.r_delete_boot_jobs',
-             methods=['DELETE'])
-add_rule_api(guest.blueprints, '/_boot_jobs/uuids', api_func='guest.r_get_uuids_of_all_had_boot_job', methods=['GET'])
 add_rule_api(guest.blueprints, '/_reset_password/<uuids>/<password>', api_func='guest.r_reset_password',
              methods=['PUT'])
 add_rule_api(guest.blueprints, '/_distribute_count', api_func='guest.r_distribute_count', methods=['GET'])

+ 0 - 1
models/rules.py

@@ -30,7 +30,6 @@ class Rules(Enum):
     ID = (REG_NUMBER, 'id')
     IDS = (REG_NUMBERS, 'ids')
     HOSTS_NAME = (REG_HOSTS_NAME, 'hosts_name')
-    BOOT_JOBS_ID = (REG_NUMBERS, 'boot_jobs_id')
 
     CONFIG_ID = (int, 'id')
     JIMV_EDITION = (int, 'jimv_edition')

+ 1 - 1
templates/ssh_keys_show.html

@@ -649,7 +649,7 @@
                         </div>
                         <div class="form-group">
                             <div class="col-sm-2"></div>
-                            <label class="col-sm-2 control-label"><span class="glyph-icon icon-newspaper-o"></span>&nbsp;&nbsp;Public Key</label>
+                            <label class="col-sm-2 control-label"><span class="glyph-icon icon-key"></span>&nbsp;&nbsp;Public Key</label>
                             <div class="col-sm-6">
                                 <textarea id="public_key" name="public_key" title="Public key" class="form-control"></textarea>
                             </div>