Bladeren bron

加入Guest添加启动作业模块

James Iter 9 jaren geleden
bovenliggende
commit
dc71c6eb88
7 gewijzigde bestanden met toevoegingen van 327 en 6 verwijderingen
  1. 11 5
      api/guest.py
  2. 1 0
      docs/todo.md
  3. 1 1
      models/guest.py
  4. 283 0
      templates/guest_boot_job.html
  5. 4 0
      templates/guest_show.html
  6. 26 0
      views/guest.py
  7. 1 0
      views_route_table.py

+ 11 - 5
api/guest.py

@@ -297,7 +297,7 @@ def r_boot(uuids):
         config.get()
 
         for uuid in uuids.split(','):
-            boot_jobs_id = guest.get_boot_jobs()
+            _, boot_jobs_id = guest.get_boot_jobs()
 
             boot_jobs = list()
 
@@ -660,11 +660,14 @@ def r_add_boot_jobs(uuids, boot_jobs_id):
             ret['data'] = dict()
             for uuid in uuids.split(','):
                 guest.uuid = uuid
-                ret['data'][uuid] = guest.get_boot_jobs()
+                boot_jobs = dict()
+                boot_jobs['ttl'], boot_jobs['boot_jobs'] = guest.get_boot_jobs()
+                ret['data'][uuid] = boot_jobs
 
         else:
             guest.uuid = uuids
-            ret['data'] = guest.get_boot_jobs()
+            ret['data'] = dict()
+            ret['data']['ttl'], ret['data']['boot_jobs'] = guest.get_boot_jobs()
 
         return ret
 
@@ -695,11 +698,14 @@ def r_get_boot_jobs(uuids):
             ret['data'] = dict()
             for uuid in uuids.split(','):
                 guest.uuid = uuid
-                ret['data'][uuid] = guest.get_boot_jobs()
+                boot_jobs = dict()
+                boot_jobs['ttl'], boot_jobs['boot_jobs'] = guest.get_boot_jobs()
+                ret['data'][uuid] = boot_jobs
 
         else:
             guest.uuid = uuids
-            ret['data'] = guest.get_boot_jobs()
+            ret['data'] = dict()
+            ret['data']['ttl'], ret['data']['boot_jobs'] = guest.get_boot_jobs()
 
         return ret
 

+ 1 - 0
docs/todo.md

@@ -15,6 +15,7 @@
 - [ ] 增加模板镜像上传功能
 - [ ] 虚拟机加入用户级的重启作业功能
 - [ ] 提示待做的启动作业
+- [ ] 收集存在于 JimV-N,却不存在于 JimV-C 的 Guest
 - [x] 不显示系统级别的启动作业
 - [ ] 添加对 Guest 的IP管理功能
 > 1. 给宿主机替换当前 IP 池中可用的 IP。替换后,新 IP 置为已用状态,被替换的 IP,

+ 1 - 1
models/guest.py

@@ -77,7 +77,7 @@ class Guest(ORM):
         db.r.expire(key, app.config['guest_boot_jobs_wait_time'])
 
     def get_boot_jobs(self):
-        return list(db.r.smembers(self.get_boot_jobs_key()))
+        return db.r.ttl(self.get_boot_jobs_key()), list(db.r.smembers(self.get_boot_jobs_key()))
 
     def delete_boot_jobs(self, boot_jobs_id):
         if not isinstance(boot_jobs_id, list):

+ 283 - 0
templates/guest_boot_job.html

@@ -0,0 +1,283 @@
+{% extends "layout.html" %}
+{% block head %}
+    {{ super() }}
+    <style type="text/css">
+
+        @media (min-width: 768px) {
+            .form-horizontal .control-label {
+                text-align: left;
+            }
+        }
+
+        label>span {
+            color: deepskyblue;
+        }
+
+        .table {
+            font-size: 12px;
+            border-width: 1px;
+            line-height: 20px;
+        }
+        .table > thead > tr > th,
+        .table > tfoot > tr > th {
+            color: #999999;
+            font-weight: normal;
+            border-bottom: 0 solid #e1e6eb;
+            background-color: #F5F6FA;
+        }
+        
+        .table > tbody > tr > td {
+            color: #424547;
+        }
+
+        .table-bordered > tbody > tr {
+            padding-top: 10px;
+            padding-bottom: 10px;
+        }
+
+        .table-bordered > thead > tr > th,
+        .table-bordered > tbody > tr > th,
+        .table-bordered > tfoot > tr > th,
+        .table-bordered > thead > tr > td,
+        .table-bordered > tbody > tr > td,
+        .table-bordered > tfoot > tr > td {
+            border-style: solid;
+            border-width: 1px 0 0 0;
+        }
+
+        .btn,
+        .form-control,
+        .modal-content {
+            border-radius: 0 !important;
+        }
+    </style>
+
+{% endblock head %}
+{% block content %}
+
+<script type="text/javascript">
+    var resource_path = window.location.pathname;
+    var cur_url = resource_path;
+
+    $(document).ready(function() {
+        $('#add_boot_job_modal').on('show.bs.modal', function (me) {
+            refresh_boot_job_selectpicker($('#boot_job_selector'));
+        });
+    });
+
+    function refresh() {
+        cur_url = resource_path;
+        window.location.href=cur_url;
+    }
+
+    function refresh_boot_job_selectpicker(selectpicker) {
+        $.ajax({
+            url : '/api/boot_jobs?filter=use_for:eq:2',
+            type : 'GET',
+            contentType: "application/json; charset=utf-8",
+            dataType: 'json',
+            error : function() {
+            },
+            success : function(data, textStatus, xhr) {
+                selectpicker.empty();
+                $.each(data.data, function(k, v) {
+                    selectpicker.append(
+                        $('<option>', {value: v['id'], text: v['name'], 'data-subtext': v['remark']})
+                    );
+                });
+                selectpicker.selectpicker('refresh');
+            }
+        });
+    }
+
+    function add_boot_job(id) {
+        $.ajax({
+            url : '/api/guests/_boot_jobs/{{ uuid }}/' + id,
+            type : 'PUT',
+            contentType: "application/json; charset=utf-8",
+            error : function() {
+                alter_danger('启动作业添加指令发送失败!');
+            },
+            success : function() {
+                alter_success('启动作业添加指令发送成功!');
+                refresh();
+            }
+        });
+    }
+
+    function remove(id) {
+        $.ajax({
+            url : '/api/guests/_boot_jobs/{{ uuid }}/' + id,
+            type : 'DELETE',
+            contentType: "application/json; charset=utf-8",
+            error : function() {
+                alter_danger('启动作业删除指令发送失败!');
+            },
+            success : function() {
+                alter_success('启动作业删除指令发送成功!');
+                refresh();
+            }
+        });
+    }
+
+    function delete_at(me) {
+        var id = $('#boot_job_id').val();
+        remove(id);
+        $('#delete_modal').modal('hide');
+    }
+
+    function add_boot_job_at(me) {
+        var id = $('#boot_job_selector').val();
+        add_boot_job(id);
+        $('#add_boot_job_modal').modal('hide');
+    }
+</script>
+<div class="panel">
+    <div class="panel-body">
+        <h3 class="title-hero" style="font-size: 24px;">
+            启动作业(<a href="/guest/detail/{{ guest_ret.data.uuid }}" style="color: #0275d8;">{{ guest_ret.data.label }}/{{ guest_ret.data.remark }}</a>)
+        </h3>
+        <div>
+            <div id="datatable-row-highlight_wrapper" class="dataTables_wrapper form-inline">
+                <div class="row" style="padding: 10px 10px 10px 0; width: 100%;">
+                    <div class="col-sm-12" style="padding-right: 0;">
+                        <h4 style="color: orange; {% if guest_boot_jobs_ret.data.ttl < 1 %}display: none;{% endif %}">下列启动作业将在
+                            <span style="color: #b94a48; {% if guest_boot_jobs_ret.data.ttl // 60 == 0 %}display: none;{% endif %}">{{ guest_boot_jobs_ret.data.ttl // 60 }}分</span>
+                            <span style="color: #b94a48; {% if guest_boot_jobs_ret.data.ttl % 60 == 0 %}display: none;{% endif %}">{{ guest_boot_jobs_ret.data.ttl % 60 }}秒</span>
+                            <span>后失效</span>
+                        </h4>
+                        <div class="pull-right">
+                            <button class="btn btn-default" onclick="refresh()" style="border-radius: 0;"><span class="glyph-icon icon-elusive-arrows-cw"></span></button>
+                            <a class="btn btn-info" href="javascript:;" data-toggle="modal" data-target="#add_boot_job_modal" style="border-radius: 0; padding-left: 40px; padding-right: 40px;">添加启动作业</a>
+                        </div>
+                    </div>
+                </div>
+                <table id="boot_job_list" class="table table-bordered table-hover" cellspacing="0" width="100%" role="grid"
+                       style="width: 100%; margin-bottom: 0; border-bottom-width: 0;">
+                <thead>
+                <tr role="row">
+                    <th style="display: none;">ID</th>
+                    <th width="280px;">名称</th>
+                    <th>作用域</th>
+                    <th width="600px;">备注</th>
+                    <th>操作</th>
+                </tr>
+                </thead>
+                <tbody>
+                {% for item in guest_boot_jobs_ret.data.boot_jobs %}
+                <tr role="row" class="odd">
+                    <td style="display: none;">{{ item }}</td>
+                    <td>
+                        <div>
+                            <a href="/operate_rules?boot_job_id={{ item }}"><p style="display: inline-block;">{{ boot_jobs_mapping_by_id[item | int].name }}</p></a>
+                        </div>
+                    </td>
+                    <td>
+                        {% if boot_jobs_mapping_by_id[item | int].use_for == 0 %}
+                            <span style="color: #8800FF;">模板</span>
+                        {% elif boot_jobs_mapping_by_id[item | int].use_for == 1 %}
+                            <span style="color: #3333FF;">系统</span>
+                        {% elif boot_jobs_mapping_by_id[item | int].use_for == 2 %}
+                            <span style="color: #00BB00;">用户</span>
+                        {% else %}
+                            <span style="color: #990000;">未知</span>
+                        {% endif %}
+                    </td>
+                    <td>
+                        <div>
+                            <p style="display: inline-block;">{{ boot_jobs_mapping_by_id[item | int].remark }}</p>
+                        </div>
+                    </td>
+                    <td>
+                        <div class="dropdown inline-block">
+                            <a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown">
+                                更多
+                            </a>
+                            <ul class="dropdown-menu">
+                                <li>
+                                    <a href="javascript:;" data-toggle="modal" data-target="#delete_modal"
+                                       onclick="$('#boot_job_id').val($(this).parent().parent().parent().parent().parent().children()[0].textContent);
+                                       $('#delete_instance_desc').text($(this).parent().parent().parent().parent().parent().children()[1].textContent)">
+                                        删除
+                                    </a>
+                                </li>
+                            </ul>
+                        </div>
+                    </td>
+                </tr>
+                {% endfor %}
+                </tbody>
+                </table>
+
+                <table class="table table-bordered" style="border-top-width: 0; z-index: 99; position: sticky; bottom: 0;">
+                    <tfoot>
+                    <tr style="height: 70px;">
+                        <th>
+                            <div class="row">
+                                <div class="col-sm-6">
+                                </div>
+                                <div class="col-sm-3" style="font-size: 12px; padding-top: 5px; text-align: right;">
+                                </div>
+                                <div class="col-sm-3" style="text-align: left;">
+                                </div>
+                            </div>
+                        </th>
+                    </tr>
+                    </tfoot>
+                </table>
+            </div>
+        </div>
+    </div>
+</div>
+
+<input id="boot_job_id" title="启动作业 ID" class="form-control" name="boot_job_id" hidden>
+
+<div class="modal" id="delete_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">
+                <p>你确定要删除该启动作业吗?</p>
+                <h3 style="color: orangered;" id="delete_instance_desc"></h3>
+            </div>
+            <div class="modal-footer">
+                <button type="button" class="btn btn-sm btn-primary" onclick="delete_at();">确定</button>
+                <button type="button" class="btn btn-sm btn-default" data-dismiss="modal">取消</button>
+            </div>
+        </div>
+    </div>
+</div>
+
+<div class="modal" id="add_boot_job_modal" tabindex="-1" role="dialog" style="margin-top: 100px;">
+    <div class="modal-dialog modal-lg">
+        <div class="modal-content">
+            <div class="modal-header">
+                <h4 class="modal-title">添加启动作业:</h4>
+            </div>
+            <div class="modal-body" style="padding-top: 0; padding-bottom: 0;">
+                <div class="example-box-wrapper">
+                    <form class="form-horizontal bordered-row">
+                        <div class="form-group">
+                            <div class="col-sm-2"></div>
+                            <label class="col-sm-2 control-label"><span class="glyph-icon icon-list-alt"></span>&nbsp;&nbsp;启动作业</label>
+                            <div class="col-sm-6">
+                                <select id="boot_job_selector" name="boot_job_selector" title="启动作业" class="selectpicker">
+                                </select>
+                            </div>
+                        </div>
+                        <div class="form-group">
+                            <div class="col-sm-4"></div>
+                            <div class="col-sm-6 pull-right">
+                                <button class="btn btn-blue-alt" style="width: 180px; height: 40px; font-size: 16px;" onclick="add_boot_job_at();">添加</button>
+                                <button class="btn btn-default" style="width: 64px; height: 40px; font-size: 16px;" data-dismiss="modal">取消</button>
+                            </div>
+                        </div>
+                    </form>
+                </div>
+            </div>
+        </div>
+    </div>
+</div>
+{% endblock content %}

+ 4 - 0
templates/guest_show.html

@@ -625,6 +625,10 @@
                                     <a href="/guest/vnc/{{ item.uuid }}" target="_blank">远程连接</a>
                                 </li>
                                 <li class="divider"></li>
+                                <li class="">
+                                    <a href="/guest/boot_jobs/{{ item.uuid }}" target="_blank">启动作业</a>
+                                </li>
+                                <li class="divider"></li>
                                 <li class="">
                                     <a href="javascript:;" data-toggle="modal" data-target="#reset_password_modal"
                                        onclick="$('#reset_password_type').val('single')">

+ 26 - 0
views/guest.py

@@ -195,3 +195,29 @@ def success():
     return render_template('success.html', go_back_url='/guests', timeout=10000, title='提交成功',
                            message_title='创建实例的请求已被接受',
                            message='您所提交的资源正在创建中。根据所提交资源的大小,需要等待几到十几分钟。页面将在10秒钟后自动跳转到实例列表页面!')
+
+
+def show_boot_job(uuid):
+    host_url = request.host_url.rstrip('/')
+
+    guest_url = host_url + url_for('api_guests.r_get', uuids=uuid)
+
+    guest_ret = requests.get(url=guest_url)
+    guest_ret = json.loads(guest_ret.content)
+
+    guest_boot_jobs_url = host_url + url_for('api_guests.r_get_boot_jobs', uuids=uuid)
+    guest_boot_jobs_ret = requests.get(url=guest_boot_jobs_url)
+    guest_boot_jobs_ret = json.loads(guest_boot_jobs_ret.content)
+
+    boot_jobs_url = host_url + url_for('api_boot_jobs.r_get_by_filter') + '?filter=id:in:' + \
+        ','.join(guest_boot_jobs_ret['data']['boot_jobs'])
+
+    boot_jobs_ret = requests.get(url=boot_jobs_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
+
+    return render_template('guest_boot_job.html', uuid=uuid, guest_ret=guest_ret,
+                           guest_boot_jobs_ret=guest_boot_jobs_ret, boot_jobs_mapping_by_id=boot_jobs_mapping_by_id)
+

+ 1 - 0
views_route_table.py

@@ -17,6 +17,7 @@ add_rule_views(guest.blueprints, '/create', views_func='guest.create', methods=[
 add_rule_views(guest.blueprints, '/success', views_func='guest.success', methods=['GET'])
 add_rule_views(guest.blueprint, '/vnc/<uuid>', views_func='guest.vnc', methods=['GET'])
 add_rule_views(guest.blueprint, '/detail/<uuid>', views_func='guest.detail', methods=['GET'])
+add_rule_views(guest.blueprint, '/boot_jobs/<uuid>', views_func='guest.show_boot_job', methods=['GET'])
 
 add_rule_views(disk.blueprints, '', views_func='disk.show', methods=['GET'])
 add_rule_views(disk.blueprints, '/create', views_func='disk.create', methods=['GET', 'POST'])