فهرست منبع

实现模板 ICON 的更新

James Iter 9 سال پیش
والد
کامیت
e2f6e3d50d
4فایلهای تغییر یافته به همراه176 افزوده شده و 19 حذف شده
  1. 1 1
      templates/guest_show.html
  2. 158 10
      templates/os_template_show.html
  3. 7 7
      views/guest.py
  4. 10 1
      views/os_template.py

+ 1 - 1
templates/guest_show.html

@@ -585,7 +585,7 @@
                             </a>
                         </div>
                     </td>
-                    <td><span class="{{ os_template_mapping_by_id[item.os_template_id].icon }}"></span></td>
+                    <td><span class="{{ os_templates_mapping_by_id[item.os_template_id].icon }}"></span></td>
                     <td>{{ format_guest_status(item.status)|safe }}</td>
                     <td>{{ item.on_host }}</td>
                     <td>CPU :&nbsp;&nbsp;{{ item.cpu }}&nbsp;核<br />内存 :&nbsp;&nbsp;{{ item.memory }}&nbsp;GB</td>

+ 158 - 10
templates/os_template_show.html

@@ -145,7 +145,16 @@
         });
 
         $('#add_os_template_modal').on('show.bs.modal', function (me) {
-            refresh_os_template_boot_job_selectpicker();
+            refresh_os_template_boot_job_selectpicker($('#boot_job_id'));
+        });
+
+        $('#update_boot_job_modal').on('show.bs.modal', function (me) {
+            refresh_os_template_boot_job_selectpicker($('#update_boot_job_id'));
+        });
+
+        $('#update_icon_modal').on('show.bs.modal', function (me) {
+            $('#os_template_id').val($(me.relatedTarget).parent().prev().prev().prev().prev().prev().text());
+            $('#update_icon_instance_desc').text($(me.relatedTarget).parent().prev().prev().prev().text());
         });
     });
 
@@ -168,7 +177,7 @@
         $(me).find(".edit_label_trigger, .edit_path_trigger").css('display','none');
     }
 
-    function refresh_os_template_boot_job_selectpicker() {
+    function refresh_os_template_boot_job_selectpicker(selectpicker) {
         $.ajax({
             url : '/api/boot_jobs?filter=use_for:eq:0',
             type : 'GET',
@@ -177,13 +186,21 @@
             error : function() {
             },
             success : function(data, textStatus, xhr) {
-                $('#boot_job_id').empty();
+                selectpicker.empty();
                 $.each(data.data, function(k, v) {
-                    $('#boot_job_id').append(
+                    selectpicker.append(
                         $('<option>', {value: v['id'], text: v['name'], 'data-subtext': v['remark']})
                     );
                 });
-                $('#boot_job_id').selectpicker('refresh');
+                selectpicker.selectpicker('refresh');
+
+                // $('#boot_job_id').empty();
+                // $.each(data.data, function(k, v) {
+                //     $('#boot_job_id').append(
+                //         $('<option>', {value: v['id'], text: v['name'], 'data-subtext': v['remark']})
+                //     );
+                // });
+                // $('#boot_job_id').selectpicker('refresh');
             }
         });
     }
@@ -322,6 +339,42 @@
         });
     }
 
+    function update_boot_job(id) {
+        $.ajax({
+            url : '/api/os_template/' + id,
+            type : 'PATCH',
+            contentType: "application/json; charset=utf-8",
+            data : JSON.stringify({
+                boot_job_id: parseInt($('#update_boot_job_id').val())
+            }),
+            error : function() {
+                alter_danger('变更初始化作业指令发送失败!');
+            },
+            success : function() {
+                alter_success('变更初始化作业指令发送成功!');
+                refresh();
+            }
+        });
+    }
+
+    function update_icon(id) {
+        $.ajax({
+            url : '/api/os_template/' + id,
+            type : 'PATCH',
+            contentType: "application/json; charset=utf-8",
+            data : JSON.stringify({
+                icon: $('#update_icon').val()
+            }),
+            error : function() {
+                alter_danger('变更 ICON 指令发送失败!');
+            },
+            success : function() {
+                alter_success('变更 ICON 指令发送成功!');
+                refresh();
+            }
+        });
+    }
+
     function enable_at(me) {
         var id = $(me).parent().parent().parent().parent().parent().children()[0].textContent;
         enable(id);
@@ -337,6 +390,18 @@
         remove(id);
         $('#delete_modal').modal('hide');
     }
+
+    function update_boot_job_at(me) {
+        var id = $('#os_template_id').val();
+        update_boot_job(id);
+        $('#update_boot_job_modal').modal('hide');
+    }
+
+    function update_icon_at(me) {
+        var id = $('#os_template_id').val();
+        update_icon(id);
+        $('#update_icon_modal').modal('hide');
+    }
 </script>
 <div class="panel">
     <div class="panel-body">
@@ -397,8 +462,16 @@
                             </a>
                         </div>
                     </td>
-                    <td><span class="{{ item.icon }}"></span></td>
-                    <td>{{ item.boot_job_id }}</td>
+                    <td>
+                        <a href="javascript:;" data-toggle="modal" data-target="#update_icon_modal">
+                            <span class="{{ item.icon }}"></span>
+                        </a>
+                    </td>
+                    <td>
+                        <a href="/boot_jobs/{{ item.boot_job_id }}">
+                            {{ boot_jobs_mapping_by_id[item.boot_job_id].name }}
+                        </a>
+                    </td>
                     <td>
                         <div class="dropdown inline-block">
                             <a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown">
@@ -416,9 +489,10 @@
                                     </a>
                                 </li>
                                 <li class="divider" style="{% if item.sequence == 0 %} display: none; {% endif %}"></li>
-                                <li class="{% if item.state not in [1] %} disabled {% endif %}" style="{% if item.sequence == 0 %} display: none; {% endif %}">
-                                    <a href="javascript:;" data-toggle="modal" data-target="#mount_modal"
-                                       onclick="$('#instance_uuid').val($($(this).parent().parent().parent().parent().parent().children()[1]).find('div a')[0].text);">
+                                <li>
+                                    <a href="javascript:;" data-toggle="modal" data-target="#update_boot_job_modal"
+                                        onclick="$('#os_template_id').val($(this).parent().parent().parent().parent().parent().children()[0].textContent);
+                                        $('#update_boot_job_instance_desc').text($(this).parent().parent().parent().parent().parent().children()[2].textContent)">
                                         变更初始化作业
                                     </a>
                                 </li>
@@ -596,4 +670,78 @@
         </div>
     </div>
 </div>
+
+<div class="modal" id="update_boot_job_modal" tabindex="-1" role="dialog" style="margin-top: 100px;">
+    <div class="modal-dialog">
+        <div class="modal-content">
+            <div class="modal-header">
+                <h4 class="modal-title">变更初始化作业:</h4>
+            </div>
+            <div class="modal-body" style="padding-top: 0;">
+                <form class="form-horizontal bordered-row">
+                    <div class="form-group">
+                        <div class="col-sm-1"></div>
+                        <label class="col-sm-3 control-label"><span class="glyph-icon icon-elusive-compass-circled"></span>&nbsp;&nbsp;模板名称</label>
+                        <div class="col-sm-8">
+                            <h3 style="color: orangered;" id="update_boot_job_instance_desc"></h3>
+                        </div>
+                    </div>
+                    <div class="form-group">
+                        <div class="col-sm-1"></div>
+                        <label class="col-sm-3 control-label"><span class="glyph-icon icon-file-code-o"></span>&nbsp;&nbsp;初始化作业</label>
+                        <div class="col-sm-8">
+                            <select id="update_boot_job_id" name="boot_job_id" title="实例的启动作业" class="selectpicker">
+                            </select>
+                        </div>
+                    </div>
+                </form>
+            </div>
+            <div class="modal-footer">
+                <button type="button" class="btn btn-sm btn-primary" onclick="update_boot_job_at();">确定</button>
+                <button type="button" class="btn btn-sm btn-default" data-dismiss="modal">取消</button>
+            </div>
+        </div>
+    </div>
+</div>
+
+<div class="modal" id="update_icon_modal" tabindex="-1" role="dialog" style="margin-top: 100px;">
+    <div class="modal-dialog">
+        <div class="modal-content">
+            <div class="modal-header">
+                <h4 class="modal-title">变更 ICON:</h4>
+            </div>
+            <div class="modal-body" style="padding-top: 0;">
+                <form class="form-horizontal bordered-row">
+                    <div class="form-group">
+                        <div class="col-sm-1"></div>
+                        <label class="col-sm-3 control-label"><span class="glyph-icon icon-elusive-compass-circled"></span>&nbsp;&nbsp;模板名称</label>
+                        <div class="col-sm-8">
+                            <h3 style="color: orangered;" id="update_icon_instance_desc"></h3>
+                        </div>
+                    </div>
+                    <div class="form-group">
+                        <div class="col-sm-1"></div>
+                        <div class="col-sm-8">
+                            <select id="update_icon" name="icon" title="模板 ICON" class="selectpicker">
+                                <option value="icon-os icon-os-centos" data-icon="icon-os icon-os-centos" selected>CentOS</option>
+                                <option value="icon-os icon-os-linux" data-icon="icon-os icon-os-linux">Linux</option>
+                                <option value="icon-os icon-os-ubuntu" data-icon="icon-os icon-os-ubuntu">Ubuntu</option>
+                                <option value="icon-os icon-os-suse" data-icon="icon-os icon-os-suse">Suse</option>
+                                <option value="icon-os icon-os-gentoo" data-icon="icon-os icon-os-gentoo">Gentoo</option>
+                                <option value="icon-os icon-os-debian" data-icon="icon-os icon-os-debian">Debian</option>
+                                <option value="icon-os icon-os-redhat" data-icon="icon-os icon-os-redhat">Redhat</option>
+                                <option value="icon-os icon-os-bsd" data-icon="icon-os icon-os-bsd">BSD</option>
+                                <option value="icon-os icon-os-windows" data-icon="icon-os icon-os-windows">Windows</option>
+                            </select>
+                        </div>
+                    </div>
+                </form>
+            </div>
+            <div class="modal-footer">
+                <button type="button" class="btn btn-sm btn-primary" onclick="update_icon_at();">确定</button>
+                <button type="button" class="btn btn-sm btn-default" data-dismiss="modal">取消</button>
+            </div>
+        </div>
+    </div>
+</div>
 {% endblock content %}

+ 7 - 7
views/guest.py

@@ -53,7 +53,7 @@ def show():
     if keyword is not None:
         guests_url = host_url + url_for('api_guests.r_content_search')
 
-    os_template_url = host_url + url_for('api_os_templates.r_get_by_filter')
+    os_templates_url = host_url + url_for('api_os_templates.r_get_by_filter')
 
     if args.__len__() > 0:
         guests_url = guests_url + '?' + '&'.join(args)
@@ -61,11 +61,11 @@ def show():
     guests_ret = requests.get(url=guests_url)
     guests_ret = json.loads(guests_ret.content)
 
-    os_template_ret = requests.get(url=os_template_url)
-    os_template_ret = json.loads(os_template_ret.content)
-    os_template_mapping_by_id = dict()
-    for os_template in os_template_ret['data']:
-        os_template_mapping_by_id[os_template['id']] = os_template
+    os_templates_ret = requests.get(url=os_templates_url)
+    os_templates_ret = json.loads(os_templates_ret.content)
+    os_templates_mapping_by_id = dict()
+    for os_template in os_templates_ret['data']:
+        os_templates_mapping_by_id[os_template['id']] = os_template
 
     last_page = int(ceil(guests_ret['paging']['total'] / float(page_size)))
     page_length = 5
@@ -89,7 +89,7 @@ def show():
                 break
 
     return render_template('guest_show.html', guests_ret=guests_ret, resource_path=resource_path,
-                           os_template_mapping_by_id=os_template_mapping_by_id, page=page,
+                           os_templates_mapping_by_id=os_templates_mapping_by_id, page=page,
                            page_size=page_size, keyword=keyword, pages=pages, last_page=last_page)
 
 

+ 10 - 1
views/os_template.py

@@ -69,12 +69,20 @@ def show():
     if keyword is not None:
         os_templates_url = host_url + url_for('api_os_templates.r_content_search')
 
+    boot_job_url = host_url + url_for('api_boot_jobs.r_get_by_filter')
+
     if args.__len__() > 0:
         os_templates_url = os_templates_url + '?' + '&'.join(args)
 
     os_templates_ret = requests.get(url=os_templates_url)
     os_templates_ret = json.loads(os_templates_ret.content)
 
+    boot_jobs_ret = requests.get(url=boot_job_url)
+    boot_jobs_ret = json.loads(boot_jobs_ret.content)
+    boot_jobs_mapping_by_id = dict()
+    for boot_job in boot_jobs_ret['data']:
+        boot_jobs_mapping_by_id[boot_job['id']] = boot_job
+
     last_page = int(ceil(os_templates_ret['paging']['total'] / float(page_size)))
     page_length = 5
     pages = list()
@@ -96,7 +104,8 @@ def show():
             if i == last_page:
                 break
 
-    return render_template('os_template_show.html', os_templates_ret=os_templates_ret, resource_path=resource_path,
+    return render_template('os_template_show.html', os_templates_ret=os_templates_ret,
+                           boot_jobs_mapping_by_id=boot_jobs_mapping_by_id, resource_path=resource_path,
                            page=page, page_size=page_size, keyword=keyword, pages=pages, order_by=order_by, order=order,
                            last_page=last_page)