Browse Source

抽象出boot_job角色

James Iter 9 years ago
parent
commit
9591c498bc

+ 1 - 1
README.md

@@ -213,7 +213,7 @@ gunicorn -c gunicorn_config.py main:app
 
 ### [配置](docs/config.md)
 
-### [实例初始化操作簇](docs/os_init.md)
+### [实例初始化操作簇](docs/boot_job.md)
 
 ### [系统模板](docs/os_template.md)
 

+ 38 - 31
api/os_init.py → api/boot_job.py

@@ -8,61 +8,62 @@ import jimit as ji
 import json
 
 from api.base import Base
-from models import OSInit
-from models import OSInitWrite
 from models import Rules
 from models import Utils
+from models.boot_job import BootJob, OperateRule
 
 
 __author__ = 'James Iter'
-__date__ = '2017/3/30'
+__date__ = '2017/6/19'
 __contact__ = 'james.iter.cn@gmail.com'
 __copyright__ = '(c) 2017 by James Iter.'
 
 
 blueprint = Blueprint(
-    'api_os_init',
+    'api_boot_job',
     __name__,
-    url_prefix='/api/os_init'
+    url_prefix='/api/boot_job'
 )
 
 blueprints = Blueprint(
-    'api_os_inits',
+    'api_boot_jobs',
     __name__,
-    url_prefix='/api/os_inits'
+    url_prefix='/api/boot_jobs'
 )
 
 
-os_init_base = Base(the_class=OSInit, the_blueprint=blueprint, the_blueprints=blueprints)
+boot_job_base = Base(the_class=BootJob, the_blueprint=blueprint, the_blueprints=blueprints)
 
 
 @Utils.dumps2response
 def r_create():
 
-    os_init = OSInit()
+    boot_job = BootJob()
 
     args_rules = [
         Rules.NAME.value,
+        Rules.USE_FOR.value,
         Rules.REMARK.value
     ]
 
-    os_init.name = request.json.get('name')
-    os_init.remark = request.json.get('remark')
+    boot_job.name = request.json.get('name')
+    boot_job.use_for = request.json.get('use_for')
+    boot_job.remark = request.json.get('remark')
 
     try:
-        ji.Check.previewing(args_rules, os_init.__dict__)
+        ji.Check.previewing(args_rules, boot_job.__dict__)
 
         ret = dict()
         ret['state'] = ji.Common.exchange_state(20000)
 
-        if os_init.exist_by('name'):
+        if boot_job.exist_by('name'):
             ret['state'] = ji.Common.exchange_state(40901)
-            ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], ': ', os_init.name])
+            ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], ': ', boot_job.name])
             return ret
 
-        os_init.create()
-        os_init.get_by('name')
-        ret['data'] = os_init.__dict__
+        boot_job.create()
+        boot_job.get_by('name')
+        ret['data'] = boot_job.__dict__
         return ret
     except ji.PreviewingError, e:
         return json.loads(e.message)
@@ -71,7 +72,7 @@ def r_create():
 @Utils.dumps2response
 def r_update(_id):
 
-    os_init = OSInit()
+    boot_job = BootJob()
 
     args_rules = [
         Rules.ID.value
@@ -82,6 +83,11 @@ def r_update(_id):
             Rules.NAME.value,
         )
 
+    if 'use_for' in request.json:
+        args_rules.append(
+            Rules.USE_FOR.value,
+        )
+
     if 'remark' in request.json:
         args_rules.append(
             Rules.REMARK.value,
@@ -96,19 +102,20 @@ def r_update(_id):
 
     try:
         ji.Check.previewing(args_rules, request.json)
-        os_init.id = request.json.get('id')
-        os_init.get()
-        os_init.name = request.json.get('name', os_init.name)
-        os_init.remark = request.json.get('remark', os_init.remark)
+        boot_job.id = request.json.get('id')
+        boot_job.get()
+        boot_job.name = request.json.get('name', boot_job.name)
+        boot_job.use_for = request.json.get('use_for', boot_job.use_for)
+        boot_job.remark = request.json.get('remark', boot_job.remark)
 
-        os_init.update()
+        boot_job.update()
         g.config = None
 
-        os_init.get()
+        boot_job.get()
 
         ret = dict()
         ret['state'] = ji.Common.exchange_state(20000)
-        ret['data'] = os_init.__dict__
+        ret['data'] = boot_job.__dict__
         return ret
     except ji.PreviewingError, e:
         return json.loads(e.message)
@@ -116,23 +123,23 @@ def r_update(_id):
 
 @Utils.dumps2response
 def r_delete(ids):
-    os_init_write_base = Base(the_class=OSInitWrite)
-    os_init_write_base.delete(ids=ids, ids_rule=Rules.IDS.value, by_field='os_init_id')
+    operate_rule_base = Base(the_class=OperateRule)
+    operate_rule_base.delete(ids=ids, ids_rule=Rules.IDS.value, by_field='boot_job_id')
 
-    return os_init_base.delete(ids=ids, ids_rule=Rules.IDS.value, by_field='id')
+    return boot_job_base.delete(ids=ids, ids_rule=Rules.IDS.value, by_field='id')
 
 
 @Utils.dumps2response
 def r_get(ids):
-    return os_init_base.get(ids=ids, ids_rule=Rules.IDS.value, by_field='id')
+    return boot_job_base.get(ids=ids, ids_rule=Rules.IDS.value, by_field='id')
 
 
 @Utils.dumps2response
 def r_get_by_filter():
-    return os_init_base.get_by_filter()
+    return boot_job_base.get_by_filter()
 
 
 @Utils.dumps2response
 def r_content_search():
-    return os_init_base.content_search()
+    return boot_job_base.content_search()
 

+ 7 - 7
api/guest.py

@@ -11,7 +11,7 @@ import jimit as ji
 
 from api.base import Base
 from models import DiskState
-from models import OSInitWrite
+from models import OperateRule
 from models.initialize import app, dev_table
 from models import Database as db
 from models import Config
@@ -78,8 +78,8 @@ def r_create():
 
         os_template.get()
 
-        os_init_writes, os_init_writes_count = OSInitWrite.get_by_filter(
-            filter_str='os_init_id:in:' + os_template.os_init_id.__str__())
+        operate_rules, operate_rules_count = OperateRule.get_by_filter(
+            filter_str='boot_job_id:in:' + os_template.boot_job_id.__str__())
 
         if db.r.scard(app.config['ip_available_set']) < 1:
             ret['state'] = ji.Common.exchange_state(50350)
@@ -128,9 +128,9 @@ def r_create():
             guest.create()
 
             # 替换占位符为有效内容
-            _os_init_writes = copy.deepcopy(os_init_writes)
-            for k, v in enumerate(_os_init_writes):
-                _os_init_writes[k]['content'] = v['content'].replace('{IP}', guest.ip).\
+            _operate_rules = copy.deepcopy(operate_rules)
+            for k, v in enumerate(_operate_rules):
+                _operate_rules[k]['content'] = v['content'].replace('{IP}', guest.ip).\
                     replace('{HOSTNAME}', guest.name).\
                     replace('{NETMASK}', config.netmask).\
                     replace('{GATEWAY}', config.gateway).\
@@ -144,7 +144,7 @@ def r_create():
                 'glusterfs_volume': config.glusterfs_volume,
                 'template_path': os_template.path,
                 'disk': disk.__dict__,
-                'writes': _os_init_writes,
+                'writes': _operate_rules,
                 'password': guest.password,
                 'xml': guest_xml.get_domain()
             }

+ 179 - 0
api/operate_rule.py

@@ -0,0 +1,179 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+from flask import Blueprint
+from flask import request
+import jimit as ji
+import json
+
+from api.base import Base
+from models import Rules
+from models import Utils
+from models.boot_job import OperateRule, BootJob
+from models.status import OperateRuleKind
+
+__author__ = 'James Iter'
+__date__ = '2017/6/19'
+__contact__ = 'james.iter.cn@gmail.com'
+__copyright__ = '(c) 2017 by James Iter.'
+
+
+blueprint = Blueprint(
+    'api_operate_rule',
+    __name__,
+    url_prefix='/api/operate_rule'
+)
+
+blueprints = Blueprint(
+    'api_operate_rules',
+    __name__,
+    url_prefix='/api/operate_rules'
+)
+
+
+operate_rule_base = Base(the_class=OperateRule, the_blueprint=blueprint, the_blueprints=blueprints)
+
+
+@Utils.dumps2response
+def r_create():
+
+    boot_job = BootJob()
+    operate_rule = OperateRule()
+
+    args_rules = [
+        Rules.BOOT_JOB_ID_EXT.value,
+        Rules.OPERATE_RULE_KIND.value
+    ]
+
+    operate_rule.boot_job_id = request.json.get('boot_job_id')
+    operate_rule.kind = request.json.get('kind')
+    operate_rule.path = request.json.get('path', '')
+    operate_rule.content = request.json.get('content', '')
+    operate_rule.command = request.json.get('command', '')
+
+    if operate_rule.kind == OperateRuleKind.cmd.value:
+        args_rules.append(
+            Rules.OPERATE_RULE_COMMAND.value
+        )
+
+    else:
+        args_rules.extend([
+            Rules.OPERATE_RULE_PATH.value,
+            Rules.OPERATE_RULE_CONTENT.value
+        ])
+
+    try:
+        ji.Check.previewing(args_rules, operate_rule.__dict__)
+
+        ret = dict()
+        ret['state'] = ji.Common.exchange_state(20000)
+
+        boot_job.id = operate_rule.boot_job_id
+        if not boot_job.exist():
+            ret['state'] = ji.Common.exchange_state(40401)
+            ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], ': ', boot_job.id.__str__()])
+            return ret
+
+        data, total = operate_rule.get_by_filter(
+            filter_str=':'.join(['boot_job_id', 'eq', operate_rule.boot_job_id.__str__()]) + ';' +
+                       ':'.join(['path', 'eq', operate_rule.path]))
+
+        if data.__len__() > 0:
+            ret['state'] = ji.Common.exchange_state(40901)
+            ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], ', path: ', operate_rule.path,
+                                                    ', boot_job_id: ', operate_rule.boot_job_id.__str__()])
+            return ret
+
+        operate_rule.create()
+        data, total = operate_rule.get_by_filter(
+            filter_str=':'.join(['boot_job_id', 'eq', operate_rule.boot_job_id.__str__()]) + ';' +
+                       ':'.join(['path', 'eq', operate_rule.path]))
+        ret['data'] = data[0]
+        return ret
+
+    except ji.PreviewingError, e:
+        return json.loads(e.message)
+
+
+@Utils.dumps2response
+def r_update(_id):
+
+    operate_rule = OperateRule()
+
+    args_rules = [
+        Rules.ID.value
+    ]
+
+    if 'boot_job_id' in request.json:
+        args_rules.append(
+            Rules.BOOT_JOB_ID_EXT.value,
+        )
+
+    if 'kind' in request.json:
+        args_rules.append(
+            Rules.OPERATE_RULE_KIND.value,
+        )
+
+    if 'path' in request.json:
+        args_rules.append(
+            Rules.OPERATE_RULE_PATH.value,
+        )
+
+    if 'content' in request.json:
+        args_rules.append(
+            Rules.OPERATE_RULE_CONTENT.value,
+        )
+
+    if 'command' in request.json:
+        args_rules.append(
+            Rules.OPERATE_RULE_COMMAND.value,
+        )
+
+    if args_rules.__len__() < 2:
+        ret = dict()
+        ret['state'] = ji.Common.exchange_state(20000)
+        return ret
+
+    request.json['id'] = _id
+
+    try:
+        ji.Check.previewing(args_rules, request.json)
+        operate_rule.id = request.json.get('id')
+        operate_rule.get()
+        operate_rule.boot_job_id = request.json.get('boot_job_id', operate_rule.boot_job_id)
+        operate_rule.kind = request.json.get('kind', operate_rule.kind)
+        operate_rule.path = request.json.get('path', operate_rule.path)
+        operate_rule.content = request.json.get('content', operate_rule.content)
+        operate_rule.command = request.json.get('command', operate_rule.command)
+
+        operate_rule.update()
+        operate_rule.get()
+
+        ret = dict()
+        ret['state'] = ji.Common.exchange_state(20000)
+        ret['data'] = operate_rule.__dict__
+        return ret
+    except ji.PreviewingError, e:
+        return json.loads(e.message)
+
+
+@Utils.dumps2response
+def r_delete(ids):
+    return operate_rule_base.delete(ids=ids, ids_rule=Rules.IDS.value, by_field='id')
+
+
+@Utils.dumps2response
+def r_get(ids):
+    return operate_rule_base.get(ids=ids, ids_rule=Rules.IDS.value, by_field='id')
+
+
+@Utils.dumps2response
+def r_get_by_filter():
+    return operate_rule_base.get_by_filter()
+
+
+@Utils.dumps2response
+def r_content_search():
+    return operate_rule_base.content_search()
+

+ 0 - 155
api/os_init_write.py

@@ -1,155 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-
-
-from flask import Blueprint
-from flask import request
-import jimit as ji
-import json
-
-from api.base import Base
-from models import OSInit
-from models import OSInitWrite
-from models import Rules
-from models import Utils
-
-
-__author__ = 'James Iter'
-__date__ = '2017/3/30'
-__contact__ = 'james.iter.cn@gmail.com'
-__copyright__ = '(c) 2017 by James Iter.'
-
-
-blueprint = Blueprint(
-    'api_os_init_write',
-    __name__,
-    url_prefix='/api/os_init_write'
-)
-
-blueprints = Blueprint(
-    'api_os_init_writes',
-    __name__,
-    url_prefix='/api/os_init_writes'
-)
-
-
-os_init_write_base = Base(the_class=OSInitWrite, the_blueprint=blueprint, the_blueprints=blueprints)
-
-
-@Utils.dumps2response
-def r_create():
-
-    os_init = OSInit()
-    os_init_write = OSInitWrite()
-
-    args_rules = [
-        Rules.OS_INIT_ID_EXT.value,
-        Rules.OS_INIT_WRITE_PATH.value,
-        Rules.OS_INIT_WRITE_CONTENT.value
-    ]
-
-    os_init_write.os_init_id = request.json.get('os_init_id')
-    os_init_write.path = request.json.get('path')
-    os_init_write.content = request.json.get('content')
-
-    try:
-        ji.Check.previewing(args_rules, os_init_write.__dict__)
-
-        ret = dict()
-        ret['state'] = ji.Common.exchange_state(20000)
-
-        os_init.id = os_init_write.os_init_id
-        if not os_init.exist():
-            ret['state'] = ji.Common.exchange_state(40401)
-            ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], ': ', os_init.id.__str__()])
-            return ret
-
-        data, total = os_init_write.get_by_filter(
-            filter_str=':'.join(['os_init_id','eq',os_init_write.os_init_id.__str__()]) + ';' +
-                       ':'.join(['path', 'eq', os_init_write.path]))
-
-        if data.__len__() > 0:
-            ret['state'] = ji.Common.exchange_state(40901)
-            ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], ', path: ', os_init_write.path,
-                                                    ', os_init_id: ', os_init_write.os_init_id.__str__()])
-            return ret
-
-        os_init_write.create()
-        data, total = os_init_write.get_by_filter(
-            filter_str=':'.join(['os_init_id','eq',os_init_write.os_init_id.__str__()]) + ';' +
-                       ':'.join(['path', 'eq', os_init_write.path]))
-        ret['data'] = data[0]
-        return ret
-    except ji.PreviewingError, e:
-        return json.loads(e.message)
-
-
-@Utils.dumps2response
-def r_update(_id):
-
-    os_init_write = OSInitWrite()
-
-    args_rules = [
-        Rules.ID.value
-    ]
-
-    if 'os_init_id' in request.json:
-        args_rules.append(
-            Rules.OS_INIT_ID_EXT.value,
-        )
-
-    if 'path' in request.json:
-        args_rules.append(
-            Rules.OS_INIT_WRITE_PATH.value,
-        )
-
-    if 'content' in request.json:
-        args_rules.append(
-            Rules.OS_INIT_WRITE_CONTENT.value,
-        )
-
-    if args_rules.__len__() < 2:
-        ret = dict()
-        ret['state'] = ji.Common.exchange_state(20000)
-        return ret
-
-    request.json['id'] = _id
-
-    try:
-        ji.Check.previewing(args_rules, request.json)
-        os_init_write.id = request.json.get('id')
-        os_init_write.get()
-        os_init_write.os_init_id = request.json.get('os_init_id', os_init_write.os_init_id)
-        os_init_write.path = request.json.get('path', os_init_write.path)
-        os_init_write.content = request.json.get('content', os_init_write.content)
-
-        os_init_write.update()
-        os_init_write.get()
-
-        ret = dict()
-        ret['state'] = ji.Common.exchange_state(20000)
-        ret['data'] = os_init_write.__dict__
-        return ret
-    except ji.PreviewingError, e:
-        return json.loads(e.message)
-
-
-@Utils.dumps2response
-def r_delete(ids):
-    return os_init_write_base.delete(ids=ids, ids_rule=Rules.IDS.value, by_field='id')
-
-
-@Utils.dumps2response
-def r_get(ids):
-    return os_init_write_base.get(ids=ids, ids_rule=Rules.IDS.value, by_field='id')
-
-
-@Utils.dumps2response
-def r_get_by_filter():
-    return os_init_write_base.get_by_filter()
-
-
-@Utils.dumps2response
-def r_content_search():
-    return os_init_write_base.content_search()
-

+ 5 - 5
api/os_template.py

@@ -45,14 +45,14 @@ def r_create():
         Rules.PATH.value,
         Rules.ACTIVE.value,
         Rules.ICON.value,
-        Rules.OS_INIT_ID_EXT.value
+        Rules.BOOT_JOB_ID_EXT.value
     ]
 
     os_template.label = request.json.get('label')
     os_template.path = request.json.get('path')
     os_template.active = request.json.get('active')
     os_template.icon = request.json.get('icon')
-    os_template.os_init_id = request.json.get('os_init_id', 0)
+    os_template.boot_job_id = request.json.get('boot_job_id', 0)
 
     try:
         ji.Check.previewing(args_rules, os_template.__dict__)
@@ -102,9 +102,9 @@ def r_update(_id):
             Rules.ICON.value,
         )
 
-    if 'os_init_id' in request.json:
+    if 'boot_job_id' in request.json:
         args_rules.append(
-            Rules.OS_INIT_ID_EXT.value,
+            Rules.BOOT_JOB_ID_EXT.value,
         )
 
     if args_rules.__len__() < 2:
@@ -123,7 +123,7 @@ def r_update(_id):
         os_template.path = request.json.get('path', os_template.path)
         os_template.active = request.json.get('active', os_template.active)
         os_template.icon = request.json.get('icon', os_template.icon)
-        os_template.os_init_id = request.json.get('os_init_id', os_template.os_init_id)
+        os_template.boot_job_id = request.json.get('boot_job_id', os_template.boot_job_id)
 
         os_template.update()
         os_template.get()

+ 15 - 15
api_route_table.py

@@ -6,8 +6,8 @@ from models.utils import add_rule_api
 from api import config
 from api import guest
 from api import disk
-from api import os_init
-from api import os_init_write
+from api import boot_job
+from api import operate_rule
 from api import os_template
 from api import log
 from api import host
@@ -25,20 +25,20 @@ add_rule_api(config.blueprint, '', api_func='config.r_create', methods=['POST'])
 add_rule_api(config.blueprint, '', api_func='config.r_update', methods=['PATCH'])
 add_rule_api(config.blueprint, '', api_func='config.r_get', methods=['GET'])
 
-# 系统初始化配置操作
-add_rule_api(os_init.blueprint, '', api_func='os_init.r_create', methods=['POST'])
-add_rule_api(os_init.blueprint, '/<_id>', api_func='os_init.r_update', methods=['PATCH'])
-add_rule_api(os_init.blueprints, '/<ids>', api_func='os_init.r_delete', methods=['DELETE'])
-add_rule_api(os_init.blueprints, '/<ids>', api_func='os_init.r_get', methods=['GET'])
-add_rule_api(os_init.blueprints, '', api_func='os_init.r_get_by_filter', methods=['GET'])
-add_rule_api(os_init.blueprints, '/_search', api_func='os_init.r_content_search', methods=['GET'])
+# 系统启动作业配置操作
+add_rule_api(boot_job.blueprint, '', api_func='boot_job.r_create', methods=['POST'])
+add_rule_api(boot_job.blueprint, '/<_id>', api_func='boot_job.r_update', methods=['PATCH'])
+add_rule_api(boot_job.blueprints, '/<ids>', api_func='boot_job.r_delete', methods=['DELETE'])
+add_rule_api(boot_job.blueprints, '/<ids>', api_func='boot_job.r_get', methods=['GET'])
+add_rule_api(boot_job.blueprints, '', api_func='boot_job.r_get_by_filter', methods=['GET'])
+add_rule_api(boot_job.blueprints, '/_search', api_func='boot_job.r_content_search', methods=['GET'])
 
-add_rule_api(os_init_write.blueprint, '', api_func='os_init_write.r_create', methods=['POST'])
-add_rule_api(os_init_write.blueprint, '/<_id>', api_func='os_init_write.r_update', methods=['PATCH'])
-add_rule_api(os_init_write.blueprints, '/<ids>', api_func='os_init_write.r_delete', methods=['DELETE'])
-add_rule_api(os_init_write.blueprints, '/<ids>', api_func='os_init_write.r_get', methods=['GET'])
-add_rule_api(os_init_write.blueprints, '', api_func='os_init_write.r_get_by_filter', methods=['GET'])
-add_rule_api(os_init_write.blueprints, '/_search', api_func='os_init_write.r_content_search', methods=['GET'])
+add_rule_api(operate_rule.blueprint, '', api_func='operate_rule.r_create', methods=['POST'])
+add_rule_api(operate_rule.blueprint, '/<_id>', api_func='operate_rule.r_update', methods=['PATCH'])
+add_rule_api(operate_rule.blueprints, '/<ids>', api_func='operate_rule.r_delete', methods=['DELETE'])
+add_rule_api(operate_rule.blueprints, '/<ids>', api_func='operate_rule.r_get', methods=['GET'])
+add_rule_api(operate_rule.blueprints, '', api_func='operate_rule.r_get_by_filter', methods=['GET'])
+add_rule_api(operate_rule.blueprints, '/_search', api_func='operate_rule.r_content_search', methods=['GET'])
 
 # 系统模板操作
 add_rule_api(os_template.blueprint, '', api_func='os_template.r_create', methods=['POST'])

+ 0 - 0
docs/os_init.md → docs/boot_job.md


+ 8 - 8
main.py

@@ -20,10 +20,10 @@ from models.initialize import app, logger, q_ws
 import api_route_table
 import views_route_table
 from models import Database as db
-from api.os_init import blueprint as os_init_blueprint
-from api.os_init import blueprints as os_init_blueprints
-from api.os_init_write import blueprint as os_init_write_blueprint
-from api.os_init_write import blueprints as os_init_write_blueprints
+from api.boot_job import blueprint as boot_job_blueprint
+from api.boot_job import blueprints as boot_job_blueprints
+from api.operate_rule import blueprint as operate_rule_blueprint
+from api.operate_rule import blueprints as operate_rule_blueprints
 from api.os_template import blueprint as os_template_blueprint
 from api.os_template import blueprints as os_template_blueprints
 from api.guest import blueprint as guest_blueprint
@@ -87,10 +87,10 @@ try:
     thread.start_new_thread(db.keepalived_mysql, ())
     db.init_conn_redis()
 
-    app.register_blueprint(os_init_blueprint)
-    app.register_blueprint(os_init_blueprints)
-    app.register_blueprint(os_init_write_blueprint)
-    app.register_blueprint(os_init_write_blueprints)
+    app.register_blueprint(boot_job_blueprint)
+    app.register_blueprint(boot_job_blueprints)
+    app.register_blueprint(operate_rule_blueprint)
+    app.register_blueprint(operate_rule_blueprints)
     app.register_blueprint(os_template_blueprint)
     app.register_blueprint(os_template_blueprints)
     app.register_blueprint(guest_blueprint)

+ 8 - 5
misc/init.sql

@@ -79,26 +79,29 @@ CREATE TABLE IF NOT EXISTS os_template(
     path VARCHAR(255) NOT NULL,
     active BOOLEAN NOT NULL DEFAULT TRUE,
     icon VARCHAR(255) NOT NULL,
-    os_init_id BIGINT UNSIGNED NOT NULL DEFAULT 0,
+    boot_job_id BIGINT UNSIGNED NOT NULL DEFAULT 0,
     PRIMARY KEY (id))
     ENGINE=InnoDB
     DEFAULT CHARSET=utf8;
 
 
-CREATE TABLE IF NOT EXISTS os_init(
+CREATE TABLE IF NOT EXISTS boot_job(
     id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
     name VARCHAR(255) NOT NULL,
-    remark TEXT NOT NULL DEFAULT '',
+    use_for TINYINT UNSIGNED NOT NULL DEFAULT 0,
+    remark VARCHAR(255) NOT NULL DEFAULT '',
     PRIMARY KEY (id))
     ENGINE=InnoDB
     DEFAULT CHARSET=utf8;
 
 
-CREATE TABLE IF NOT EXISTS os_init_write(
+CREATE TABLE IF NOT EXISTS operate_rule(
     id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
-    os_init_id BIGINT UNSIGNED NOT NULL,
+    boot_job_id BIGINT UNSIGNED NOT NULL,
+    kind TINYINT UNSIGNED NOT NULL DEFAULT 0,
     path VARCHAR(255) NOT NULL,
     content TEXT NOT NULL DEFAULT '',
+    command TEXT NOT NULL DEFAULT '',
     PRIMARY KEY (id))
     ENGINE=InnoDB
     DEFAULT CHARSET=utf8;

+ 3 - 3
models/__init__.py

@@ -39,8 +39,8 @@ from guest_xml import (
     GuestXML
 )
 
-from os_init import (
-    OSInit, OSInitWrite
+from boot_job import (
+    BootJob, OperateRule
 )
 
 from os_template import (
@@ -72,7 +72,7 @@ __copyright__ = '(c) 2017 by James Iter.'
 
 __all__ = [
     'Rules', 'Utils', 'Init', 'Database', 'FilterFieldType', 'Filter', 'EmitKind', 'GuestState', 'DiskState',
-    'LogLevel', 'ORM', 'Config', 'Guest', 'Disk', 'OSInit', 'OSInitWrite', 'OSTemplate', 'GuestXML', 'Log',
+    'LogLevel', 'ORM', 'Config', 'Guest', 'Disk', 'BootJob', 'OperateRule', 'OSTemplate', 'GuestXML', 'Log',
     'EventProcessor', 'ResponseState'
 ]
 

+ 19 - 13
models/os_init.py → models/boot_job.py

@@ -7,20 +7,21 @@ from models import ORM
 
 
 __author__ = 'James Iter'
-__date__ = '2017/3/23'
+__date__ = '2017/6/19'
 __contact__ = 'james.iter.cn@gmail.com'
 __copyright__ = '(c) 2017 by James Iter.'
 
 
-class OSInit(ORM):
+class BootJob(ORM):
 
-    _table_name = 'os_init'
+    _table_name = 'boot_job'
     _primary_key = 'id'
 
     def __init__(self):
-        super(OSInit, self).__init__()
+        super(BootJob, self).__init__()
         self.id = 0
         self.name = None
+        self.use_for = None
         self.remark = ''
 
     @staticmethod
@@ -33,22 +34,24 @@ class OSInit(ORM):
 
     @staticmethod
     def get_allow_update_keywords():
-        return ['remark']
+        return ['use_for']
 
     @staticmethod
     def get_allow_content_search_keywords():
         return ['name', 'remark']
 
 
-class OSInitWrite(ORM):
+class OperateRule(ORM):
 
-    _table_name = 'os_init_write'
+    _table_name = 'operate_rule'
     _primary_key = 'id'
 
     def __init__(self):
-        super(OSInitWrite, self).__init__()
+        super(OperateRule, self).__init__()
         self.id = 0
-        self.os_init_id = None
+        self.boot_job_id = None
+        self.kind = ''
+        self.command = ''
         self.path = ''
         self.content = ''
 
@@ -56,15 +59,18 @@ class OSInitWrite(ORM):
     def get_filter_keywords():
         return {
             'id': FilterFieldType.INT.value,
-            'os_init_id': FilterFieldType.STR.value,
-            'path': FilterFieldType.STR.value
+            'boot_job_id': FilterFieldType.INT.value,
+            'command': FilterFieldType.STR.value,
+            'path': FilterFieldType.STR.value,
+            'content': FilterFieldType.STR.value,
         }
 
     @staticmethod
     def get_allow_update_keywords():
-        return ['os_init_id', 'path']
+        return ['boot_job_id']
 
     @staticmethod
     def get_allow_content_search_keywords():
-        return ['path', 'content']
+        return ['command', 'path', 'content']
+
 

+ 3 - 3
models/os_template.py

@@ -24,7 +24,7 @@ class OSTemplate(ORM):
         self.path = None
         self.active = None
         self.icon = None
-        self.os_init_id = None
+        self.boot_job_id = None
 
     @staticmethod
     def get_filter_keywords():
@@ -33,12 +33,12 @@ class OSTemplate(ORM):
             'label': FilterFieldType.STR.value,
             'path': FilterFieldType.STR.value,
             'active': FilterFieldType.BOOL.value,
-            'os_init_id': FilterFieldType.INT.value
+            'boot_job_id': FilterFieldType.INT.value
         }
 
     @staticmethod
     def get_allow_update_keywords():
-        return ['active', 'os_init_id']
+        return ['active', 'boot_job_id']
 
     @staticmethod
     def get_allow_content_search_keywords():

+ 6 - 3
models/rules.py

@@ -57,11 +57,14 @@ class Rules(Enum):
     DISK_SIZE_STR = ('regex:^\d{1,17}$', 'size')
 
     REMARK = (basestring, 'remark')
+    USE_FOR = (int, 'use_for')
     LABEL = (basestring, 'label')
     ACTIVE = (bool, 'active')
     ICON = (basestring, 'icon')
 
-    OS_INIT_ID_EXT = (int, 'os_init_id')
-    OS_INIT_WRITE_PATH = (basestring, 'path')
-    OS_INIT_WRITE_CONTENT = (basestring, 'content')
+    BOOT_JOB_ID_EXT = (int, 'boot_job_id')
+    OPERATE_RULE_KIND = (int, 'kind')
+    OPERATE_RULE_PATH = (basestring, 'path')
+    OPERATE_RULE_CONTENT = (basestring, 'content')
+    OPERATE_RULE_COMMAND = (basestring, 'command')
 

+ 12 - 0
models/status.py

@@ -58,3 +58,15 @@ class DiskState(IntEnum):
     mounted = 2
     dirty = 255
 
+
+class BootJobUseFor(IntEnum):
+    template = 0
+    user = 1
+
+
+class OperateRuleKind(IntEnum):
+    cmd = 0
+    write_file = 1
+    append_file = 2
+
+

+ 1 - 0
models/utils.py

@@ -1,6 +1,7 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
+
 from functools import wraps
 
 import commands

+ 130 - 0
tests/test_boot_job.py

@@ -0,0 +1,130 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+import requests
+import json
+import unittest
+
+
+__author__ = 'James Iter'
+__date__ = '2017/3/31'
+__contact__ = 'james.iter.cn@gmail.com'
+__copyright__ = '(c) 2017 by James Iter.'
+
+
+class TestOSInit(unittest.TestCase):
+
+    base_url = 'http://127.0.0.1:8008/api'
+    boot_job_id = 0
+
+    def setUp(self):
+        pass
+
+    def tearDown(self):
+        pass
+
+    # 创建系统初始化组
+    def test_11_create(self):
+        payload = {
+            "name": 'CentOS-Systemd',
+            "use_for": 0,
+            "remark": u'用作红帽 Systemd 系列的系统初始化。初始化操作依据 CentOS 7 来实现。'
+        }
+
+        url = TestOSInit.base_url + '/boot_job'
+        headers = {'content-type': 'application/json'}
+        r = requests.post(url, data=json.dumps(payload), headers=headers)
+        j_r = json.loads(r.content)
+        print json.dumps(j_r, ensure_ascii=False)
+        self.assertEqual('200', j_r['state']['code'])
+
+    # 获取系统初始化组列表
+    def test_12_get(self):
+        url = TestOSInit.base_url + '/boot_jobs'
+        headers = {'content-type': 'application/json'}
+        r = requests.get(url, headers=headers)
+        j_r = json.loads(r.content)
+        print json.dumps(j_r, ensure_ascii=False)
+        TestOSInit.boot_job_id = j_r['data'][0]['id']
+        self.assertEqual('200', j_r['state']['code'])
+
+    # 创建更新系统初始化组
+    def test_13_update(self):
+        payload = {
+            "name": 'RedHat-Systemd'
+        }
+
+        url = TestOSInit.base_url + '/boot_job/' + TestOSInit.boot_job_id.__str__()
+        headers = {'content-type': 'application/json'}
+        r = requests.patch(url, data=json.dumps(payload), headers=headers)
+        j_r = json.loads(r.content)
+        print json.dumps(j_r, ensure_ascii=False)
+        self.assertEqual('200', j_r['state']['code'])
+
+    # 校验系统初始化组列表更新结果
+    def test_14_get(self):
+        url = TestOSInit.base_url + '/boot_jobs'
+        headers = {'content-type': 'application/json'}
+        r = requests.get(url, headers=headers)
+        j_r = json.loads(r.content)
+        print json.dumps(j_r, ensure_ascii=False)
+        self.assertEqual('200', j_r['state']['code'])
+        self.assertEqual('RedHat-Systemd', j_r['data'][0]['name'])
+
+    # # 删除系统初始化组列表更新结果
+    # def test_15_delete(self):
+    #     url = TestOSInit.base_url + '/boot_jobs/' + TestOSInit.boot_job_id.__str__()
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.delete(url, headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
+
+    def test_21_create(self):
+        payload = {
+            "name": 'Gentoo-OpenRC',
+            "use_for": 0,
+            "remark": u'Gentoo startup process。'
+        }
+
+        url = TestOSInit.base_url + '/boot_job'
+        headers = {'content-type': 'application/json'}
+        r = requests.post(url, data=json.dumps(payload), headers=headers)
+        j_r = json.loads(r.content)
+        print json.dumps(j_r, ensure_ascii=False)
+        self.assertEqual('200', j_r['state']['code'])
+
+    # 创建系统初始化组
+    def test_22_create(self):
+        payload = {
+            "name": 'Ubuntu-Upstart',
+            "use_for": 0,
+            "remark": u'Ubuntu startup process。'
+        }
+
+        url = TestOSInit.base_url + '/boot_job'
+        headers = {'content-type': 'application/json'}
+        r = requests.post(url, data=json.dumps(payload), headers=headers)
+        j_r = json.loads(r.content)
+        print json.dumps(j_r, ensure_ascii=False)
+        self.assertEqual('200', j_r['state']['code'])
+
+    # 创建系统初始化组
+    def test_23_create(self):
+        payload = {
+            "name": 'CentOS-SysV',
+            "use_for": 0,
+            "remark": u'用作CentOS SysV 系列的系统初始化。初始化操作依据 CentOS 6.8 来实现。'
+        }
+
+        url = TestOSInit.base_url + '/boot_job'
+        headers = {'content-type': 'application/json'}
+        r = requests.post(url, data=json.dumps(payload), headers=headers)
+        j_r = json.loads(r.content)
+        print json.dumps(j_r, ensure_ascii=False)
+        self.assertEqual('200', j_r['state']['code'])
+
+if __name__ == '__main__':
+    unittest.main()
+

+ 9 - 9
tests/test_guest.py

@@ -219,15 +219,15 @@ class TestGuest(unittest.TestCase):
     #     j_r = json.loads(r.content)
     #     print json.dumps(j_r, ensure_ascii=False)
     #     self.assertEqual('200', j_r['state']['code'])
-
-    def test_59_detach_disk(self):
-        TestGuest.disk_uuid = '23d09c24-3bf3-4b05-bde4-e9fe5de317e8'
-        url = TestGuest.base_url + '/guest/_detach_disk/' + TestGuest.disk_uuid
-        headers = {'content-type': 'application/json'}
-        r = requests.put(url, headers=headers)
-        j_r = json.loads(r.content)
-        print json.dumps(j_r, ensure_ascii=False)
-        self.assertEqual('200', j_r['state']['code'])
+    #
+    # def test_59_detach_disk(self):
+    #     TestGuest.disk_uuid = '23d09c24-3bf3-4b05-bde4-e9fe5de317e8'
+    #     url = TestGuest.base_url + '/guest/_detach_disk/' + TestGuest.disk_uuid
+    #     headers = {'content-type': 'application/json'}
+    #     r = requests.put(url, headers=headers)
+    #     j_r = json.loads(r.content)
+    #     print json.dumps(j_r, ensure_ascii=False)
+    #     self.assertEqual('200', j_r['state']['code'])
 
     # def test_60_delete(self):
     #     url = TestGuest.base_url + '/guests/' + TestGuest.uuid

+ 43 - 33
tests/test_os_init_write.py → tests/test_operate_rule.py

@@ -13,11 +13,11 @@ __contact__ = 'james.iter.cn@gmail.com'
 __copyright__ = '(c) 2017 by James Iter.'
 
 
-class TestOSInitWrite(unittest.TestCase):
+class TestOperateRule(unittest.TestCase):
 
     base_url = 'http://127.0.0.1:8008/api'
-    os_init_id = 3
-    os_init_write_id = 0
+    boot_job_id = 2
+    operate_rule_id = 0
 
     def setUp(self):
         pass
@@ -29,21 +29,23 @@ class TestOSInitWrite(unittest.TestCase):
     # def test_11_create(self):
     #     payload = {
     #         "name": "CentOS-Systemd",
+    #         "use_for": 0,
     #         "remark": u"用作红帽 Systemd 系列的系统初始化。初始化操作依据 CentOS 7 来实现。"
     #     }
     #
-    #     url = TestOSInitWrite.base_url + '/os_init'
+    #     url = TestOperateRule.base_url + '/boot_job'
     #     headers = {'content-type': 'application/json'}
     #     r = requests.post(url, data=json.dumps(payload), headers=headers)
     #     j_r = json.loads(r.content)
     #     print json.dumps(j_r, ensure_ascii=False)
-    #     TestOSInitWrite.os_init_id = j_r['data']['id']
+    #     TestOperateRule.boot_job_id = j_r['data']['id']
     #     self.assertEqual('200', j_r['state']['code'])
-    #
+
     # # 添加系统初始化操作
     # def test_21_create(self):
     #     payload = {
-    #         "os_init_id": TestOSInitWrite.os_init_id,
+    #         "boot_job_id": TestOperateRule.boot_job_id,
+    #         "kind": 1,
     #         "path": "/etc/resolv.conf",
     #         "content": "\n".join([
     #             "nameserver {DNS1}",
@@ -51,7 +53,7 @@ class TestOSInitWrite(unittest.TestCase):
     #         ])
     #     }
     #
-    #     url = TestOSInitWrite.base_url + '/os_init_write'
+    #     url = TestOperateRule.base_url + '/operate_rule'
     #     headers = {'content-type': 'application/json'}
     #     r = requests.post(url, data=json.dumps(payload), headers=headers)
     #     j_r = json.loads(r.content)
@@ -61,7 +63,8 @@ class TestOSInitWrite(unittest.TestCase):
     # # 添加系统初始化操作
     # def test_22_create(self):
     #     payload = {
-    #         "os_init_id": TestOSInitWrite.os_init_id,
+    #         "boot_job_id": TestOperateRule.boot_job_id,
+    #         "kind": 1,
     #         "path": "/etc/sysconfig/network-scripts/ifcfg-eth0",
     #         "content": "\n".join([
     #             "DEVICE=eth0",
@@ -78,7 +81,7 @@ class TestOSInitWrite(unittest.TestCase):
     #         ])
     #     }
     #
-    #     url = TestOSInitWrite.base_url + '/os_init_write'
+    #     url = TestOperateRule.base_url + '/operate_rule'
     #     headers = {'content-type': 'application/json'}
     #     r = requests.post(url, data=json.dumps(payload), headers=headers)
     #     j_r = json.loads(r.content)
@@ -88,21 +91,22 @@ class TestOSInitWrite(unittest.TestCase):
     # # 添加系统初始化操作
     # def test_23_create(self):
     #     payload = {
-    #         "os_init_id": TestOSInitWrite.os_init_id,
+    #         "boot_job_id": TestOperateRule.boot_job_id,
+    #         "kind": 1,
     #         "path": "/etc/hostname",
     #         "content": "hostname"
     #     }
     #
-    #     url = TestOSInitWrite.base_url + '/os_init_write'
+    #     url = TestOperateRule.base_url + '/operate_rule'
     #     headers = {'content-type': 'application/json'}
     #     r = requests.post(url, data=json.dumps(payload), headers=headers)
     #     j_r = json.loads(r.content)
     #     print json.dumps(j_r, ensure_ascii=False)
-    #     TestOSInitWrite.os_init_write_id = j_r['data']['id']
+    #     TestOperateRule.operate_rule_id = j_r['data']['id']
     #     self.assertEqual('200', j_r['state']['code'])
-    #
+
     # def test_25_get_list(self):
-    #     url = TestOSInitWrite.base_url + '/os_init_writes'
+    #     url = TestOperateRule.base_url + '/operate_rules'
     #     headers = {'content-type': 'application/json'}
     #     r = requests.get(url, headers=headers)
     #     j_r = json.loads(r.content)
@@ -115,16 +119,16 @@ class TestOSInitWrite(unittest.TestCase):
     #         "content": "{HOSTNAME}"
     #     }
     #
-    #     url = TestOSInitWrite.base_url + '/os_init_write/' + TestOSInitWrite.os_init_write_id.__str__()
+    #     url = TestOperateRule.base_url + '/operate_rule/' + TestOperateRule.operate_rule_id.__str__()
     #     headers = {'content-type': 'application/json'}
     #     r = requests.patch(url, data=json.dumps(payload), headers=headers)
     #     j_r = json.loads(r.content)
     #     print json.dumps(j_r, ensure_ascii=False)
     #     self.assertEqual('200', j_r['state']['code'])
-    #
+
     # @unittest.skip('skip delete os init write!')
     # def test_27_delete(self):
-    #     url = TestOSInitWrite.base_url + '/os_init_writes/' + TestOSInitWrite.os_init_write_id.__str__()
+    #     url = TestOperateRule.base_url + '/operate_rules/' + TestOperateRule.operate_rule_id.__str__()
     #     headers = {'content-type': 'application/json'}
     #     r = requests.delete(url, headers=headers)
     #     j_r = json.loads(r.content)
@@ -134,7 +138,7 @@ class TestOSInitWrite(unittest.TestCase):
     # @unittest.skip('skip delete os init!')
     # # 删除系统初始化组列表更新结果
     # def test_31_delete(self):
-    #     url = TestOSInitWrite.base_url + '/os_init/' + TestOSInitWrite.os_init_id.__str__()
+    #     url = TestOperateRule.base_url + '/boot_jobs/' + TestOperateRule.boot_job_id.__str__()
     #     headers = {'content-type': 'application/json'}
     #     r = requests.delete(url, headers=headers)
     #     j_r = json.loads(r.content)
@@ -143,7 +147,8 @@ class TestOSInitWrite(unittest.TestCase):
     #
     # def test_51_create(self):
     #     payload = {
-    #         "os_init_id": 8,
+    #         "boot_job_id": 5,
+    #         "kind": 1,
     #         "path": "/etc/resolv.conf",
     #         "content": "\n".join([
     #             "nameserver {DNS1}",
@@ -151,7 +156,7 @@ class TestOSInitWrite(unittest.TestCase):
     #         ])
     #     }
     #
-    #     url = TestOSInitWrite.base_url + '/os_init_write'
+    #     url = TestOperateRule.base_url + '/operate_rule'
     #     headers = {'content-type': 'application/json'}
     #     r = requests.post(url, data=json.dumps(payload), headers=headers)
     #     j_r = json.loads(r.content)
@@ -160,7 +165,8 @@ class TestOSInitWrite(unittest.TestCase):
     #
     # def test_52_create(self):
     #     payload = {
-    #         "os_init_id": 8,
+    #         "boot_job_id": 5,
+    #         "kind": 1,
     #         "path": "/etc/sysconfig/network-scripts/ifcfg-eth0",
     #         "content": "\n".join([
     #             "DEVICE=eth0",
@@ -175,7 +181,7 @@ class TestOSInitWrite(unittest.TestCase):
     #         ])
     #     }
     #
-    #     url = TestOSInitWrite.base_url + '/os_init_write'
+    #     url = TestOperateRule.base_url + '/operate_rule'
     #     headers = {'content-type': 'application/json'}
     #     r = requests.post(url, data=json.dumps(payload), headers=headers)
     #     j_r = json.loads(r.content)
@@ -184,7 +190,8 @@ class TestOSInitWrite(unittest.TestCase):
     #
     # def test_53_create(self):
     #     payload = {
-    #         "os_init_id": 8,
+    #         "boot_job_id": 5,
+    #         "kind": 1,
     #         "path": "/etc/sysconfig/network",
     #         "content": "\n".join([
     #             "NETWORKING=yes",
@@ -192,17 +199,18 @@ class TestOSInitWrite(unittest.TestCase):
     #         ])
     #     }
     #
-    #     url = TestOSInitWrite.base_url + '/os_init_write'
+    #     url = TestOperateRule.base_url + '/operate_rule'
     #     headers = {'content-type': 'application/json'}
     #     r = requests.post(url, data=json.dumps(payload), headers=headers)
     #     j_r = json.loads(r.content)
     #     print json.dumps(j_r, ensure_ascii=False)
-    #     TestOSInitWrite.os_init_write_id = j_r['data']['id']
+    #     TestOperateRule.operate_rule_id = j_r['data']['id']
     #     self.assertEqual('200', j_r['state']['code'])
     #
     # def test_61_create(self):
     #     payload = {
-    #         "os_init_id": 7,
+    #         "boot_job_id": 3,
+    #         "kind": 1,
     #         "path": "/etc/resolv.conf",
     #         "content": "\n".join([
     #             "nameserver {DNS1}",
@@ -210,7 +218,7 @@ class TestOSInitWrite(unittest.TestCase):
     #         ])
     #     }
     #
-    #     url = TestOSInitWrite.base_url + '/os_init_write'
+    #     url = TestOperateRule.base_url + '/operate_rule'
     #     headers = {'content-type': 'application/json'}
     #     r = requests.post(url, data=json.dumps(payload), headers=headers)
     #     j_r = json.loads(r.content)
@@ -219,7 +227,8 @@ class TestOSInitWrite(unittest.TestCase):
     #
     # def test_62_create(self):
     #     payload = {
-    #         "os_init_id": 7,
+    #         "boot_job_id": 3,
+    #         "kind": 1,
     #         "path": "/etc/conf.d/net",
     #         "content": "\n".join([
     #             "config_eth0=\"{IP}/{NETMASK}\"",
@@ -227,7 +236,7 @@ class TestOSInitWrite(unittest.TestCase):
     #         ])
     #     }
     #
-    #     url = TestOSInitWrite.base_url + '/os_init_write'
+    #     url = TestOperateRule.base_url + '/operate_rule'
     #     headers = {'content-type': 'application/json'}
     #     r = requests.post(url, data=json.dumps(payload), headers=headers)
     #     j_r = json.loads(r.content)
@@ -236,17 +245,18 @@ class TestOSInitWrite(unittest.TestCase):
     #
     # def test_63_create(self):
     #     payload = {
-    #         "os_init_id": 7,
+    #         "boot_job_id": 3,
+    #         "kind": 1,
     #         "path": "/etc/conf.d/hostname",
     #         "content": "hostname=\"{HOSTNAME}\""
     #     }
     #
-    #     url = TestOSInitWrite.base_url + '/os_init_write'
+    #     url = TestOperateRule.base_url + '/operate_rule'
     #     headers = {'content-type': 'application/json'}
     #     r = requests.post(url, data=json.dumps(payload), headers=headers)
     #     j_r = json.loads(r.content)
     #     print json.dumps(j_r, ensure_ascii=False)
-    #     TestOSInitWrite.os_init_write_id = j_r['data']['id']
+    #     TestOperateRule.operate_rule_id = j_r['data']['id']
     #     self.assertEqual('200', j_r['state']['code'])
 
 if __name__ == '__main__':

+ 0 - 126
tests/test_os_init.py

@@ -1,126 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-
-
-import requests
-import json
-import unittest
-
-
-__author__ = 'James Iter'
-__date__ = '2017/3/31'
-__contact__ = 'james.iter.cn@gmail.com'
-__copyright__ = '(c) 2017 by James Iter.'
-
-
-class TestOSInit(unittest.TestCase):
-
-    base_url = 'http://127.0.0.1:8008/api'
-    os_init_id = 0
-
-    def setUp(self):
-        pass
-
-    def tearDown(self):
-        pass
-
-    # # 创建系统初始化组
-    # def test_11_create(self):
-    #     payload = {
-    #         "name": 'CentOS-Systemd',
-    #         "remark": u'用作红帽 Systemd 系列的系统初始化。初始化操作依据 CentOS 7 来实现。'
-    #     }
-    #
-    #     url = TestOSInit.base_url + '/os_init'
-    #     headers = {'content-type': 'application/json'}
-    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
-    #     j_r = json.loads(r.content)
-    #     print json.dumps(j_r, ensure_ascii=False)
-    #     self.assertEqual('200', j_r['state']['code'])
-
-    # 获取系统初始化组列表
-    def test_12_get(self):
-        url = TestOSInit.base_url + '/os_inits'
-        headers = {'content-type': 'application/json'}
-        r = requests.get(url, headers=headers)
-        j_r = json.loads(r.content)
-        print json.dumps(j_r, ensure_ascii=False)
-        TestOSInit.os_init_id = j_r['data'][0]['id']
-        self.assertEqual('200', j_r['state']['code'])
-
-    # # 创建更新系统初始化组
-    # def test_13_update(self):
-    #     payload = {
-    #         "name": 'RedHat-Systemd'
-    #     }
-    #
-    #     url = TestOSInit.base_url + '/os_init/' + TestOSInit.os_init_id.__str__()
-    #     headers = {'content-type': 'application/json'}
-    #     r = requests.patch(url, data=json.dumps(payload), headers=headers)
-    #     j_r = json.loads(r.content)
-    #     print json.dumps(j_r, ensure_ascii=False)
-    #     self.assertEqual('200', j_r['state']['code'])
-    #
-    # # 校验系统初始化组列表更新结果
-    # def test_14_get(self):
-    #     url = TestOSInit.base_url + '/os_inits'
-    #     headers = {'content-type': 'application/json'}
-    #     r = requests.get(url, headers=headers)
-    #     j_r = json.loads(r.content)
-    #     print json.dumps(j_r, ensure_ascii=False)
-    #     self.assertEqual('200', j_r['state']['code'])
-    #     self.assertEqual('RedHat-Systemd', j_r['data'][0]['name'])
-    #
-    # # 删除系统初始化组列表更新结果
-    # def test_15_delete(self):
-    #     url = TestOSInit.base_url + '/os_inits/' + TestOSInit.os_init_id.__str__()
-    #     headers = {'content-type': 'application/json'}
-    #     r = requests.delete(url, headers=headers)
-    #     j_r = json.loads(r.content)
-    #     print json.dumps(j_r, ensure_ascii=False)
-    #     self.assertEqual('200', j_r['state']['code'])
-    #
-    # def test_21_create(self):
-    #     payload = {
-    #         "name": 'Gentoo-OpenRC',
-    #         "remark": u'Gentoo startup process。'
-    #     }
-    #
-    #     url = TestOSInit.base_url + '/os_init'
-    #     headers = {'content-type': 'application/json'}
-    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
-    #     j_r = json.loads(r.content)
-    #     print json.dumps(j_r, ensure_ascii=False)
-    #     self.assertEqual('200', j_r['state']['code'])
-    #
-    # # 创建系统初始化组
-    # def test_22_create(self):
-    #     payload = {
-    #         "name": 'Ubuntu-Upstart',
-    #         "remark": u'Ubuntu startup process。'
-    #     }
-    #
-    #     url = TestOSInit.base_url + '/os_init'
-    #     headers = {'content-type': 'application/json'}
-    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
-    #     j_r = json.loads(r.content)
-    #     print json.dumps(j_r, ensure_ascii=False)
-    #     self.assertEqual('200', j_r['state']['code'])
-    #
-    # # 创建系统初始化组
-    # def test_23_create(self):
-    #     payload = {
-    #         "name": 'CentOS-SysV',
-    #         "remark": u'用作CentOS SysV 系列的系统初始化。初始化操作依据 CentOS 6.8 来实现。'
-    #     }
-    #
-    #     url = TestOSInit.base_url + '/os_init'
-    #     headers = {'content-type': 'application/json'}
-    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
-    #     j_r = json.loads(r.content)
-    #     print json.dumps(j_r, ensure_ascii=False)
-    #     self.assertEqual('200', j_r['state']['code'])
-
-if __name__ == '__main__':
-    unittest.main()
-