def get_script_default_params(self): self.response( generals.gen_response([ { "type": "system_default", "name": "HOSTS", "display": False, "description": ("集群中所有节点的信息,由系统发送给所有的脚本,其值为:" "结点的ip|结点的组名称_组中的顺序@" "结尾处的@表示当前机器。" "例如:10.10.1.123|master_1@,10.10.1.124|server_1") }, # { # "type":"system_default", # "name":"ON_GROUP", # "display":True, # "description":("在特定的组上执行。值为有效的组名称。" # "如果为空则在整个应用上执行。") # }, { "type": "system_default", "name": "ON_IP", "display": False, "description": ("在特定IP的结点上执行。值为有效的结点IP。" "如果为空则在整个应用上执行。") } ]))
def update_app(self, app_serial, body): try: app_name = body['name'] description = body['description'] sql = ("UPDATE manor.manor_app_instance " "SET app_name='%s',app_description='%s' " "WHERE app_serial='%s'") % (app_name, description, app_serial) execute(sql) vms = yield list_app_resources(app_serial) for vm in vms: yield update_vm_display_name(vm['vm_id'], app_name) rs = yield DBUtil().query( ("select * from manor.manor_app_instance " "where app_serial='%s'") % app_serial) seq = rs[0]['app_id'] optLog.write(self.request, optLog.Type.APP_INSTANCE, seq, Operator.UPDATE, '%s' % app_name) self.response(generals.gen_response({'result': 'ok'})) except Exception as detail: logging.getLogger('manor').error(generals.trace()) raise detail
def streamlet_list(self, step_type, *resgs): app_path = cfgutils.getval('app', 'streamlet_path') files = [f.replace('.yaml', '') for f in os.listdir(app_path)] if step_type == 'deploy': del files[files.index('delete_node')] del files[files.index('start_node')] del files[files.index('stop_node')] self.response(generals.gen_response(files))
def delete_template(self, name): template_path = cfgutils.getval('app', 'template_path') with open('%s/%s.yaml' % (template_path, name)) as f: tmp = f.read() t_content = yaml.safe_load(tmp) template = '%s/%s.yaml' % (template_path, name) shutil.rmtree('%s/_%s' % (template_path, name)) os.remove(template) optLog.write(self.request, optLog.Type.APP_TEMPLATE, name, Operator.DELETE, '%s' % t_content['label']) self.response(generals.gen_response('ok'))
def list_app_template_detail(self): rs = [] try: app_path = cfgutils.getval('app', 'template_path') self.log.debug('get templates path: %s' % app_path) for name in os.listdir(app_path): if not os.path.isdir(app_path + '/' + name) and name != '.yaml': rs.append(load_template(app_path, name)) self.log.debug('get templates: %s' % json.dumps(rs)) rs = generals.gen_response(rs) except: self.log.error(generals.trace()) self.response(rs)
def delete_app(self, app_serial): serial = app_serial def get_result(rows): app_name = rows[0]['app_name'] app_id = rows[0]['app_id'] optLog.write(self.request, optLog.Type.APP_INSTANCE, app_id, Operator.DELETE, '%s' % app_name) execute_query( "select * from manor.manor_app_instance where app_serial='%s'" % serial, get_result) do_delete_app(serial) self.response(generals.gen_response({'result': 'ok'}))
def change_status(self, name, body): t_path = cfgutils.getval('app', 'template_path') with open('%s/%s.yaml' % (t_path, name)) as f: tmp = f.read() t_content = yaml.safe_load(tmp) value = json.loads(self.request.body)['value'] t_content['status'] = value tmp_f = str(uuid.uuid1()) with codecs.open('%s/%s' % (t_path, tmp_f), 'w', 'utf-8') as f: f.write(yaml.safe_dump(t_content)) os.rename('%s/%s' % (t_path, tmp_f), '%s/%s.yaml' % (t_path, name)) optLog.write(self.request, optLog.Type.APP_TEMPLATE, name, Operator.ONLINE if value == 1 else Operator.OFFLINE, '%s' % t_content['label']) self.response(generals.gen_response('ok'))
def get_app_template(self, serial, *args): instance_path = cfgutils.getval('app', 'instance_path') with open('%s/%s.yaml' % (instance_path, serial.replace(' ', ''))) as f: rs = f.read() content = yaml.safe_load(rs) for a in content['action']: pk = a['streamlet'].keys() for k in pk: c_a_p = a['streamlet'][k]['params'] s = [json.dumps(_) for _ in c_a_p] ss = set(s) a['streamlet'][k]['params'] = [json.loads(_) for _ in list(ss)] self.response(generals.gen_response(content))
def execute_template(self, name, action_name, body): params = json.loads(self.request.body) app_name = params['app_name'] if 'type' not in params or params['type'] == 'deploy': seq = yield sequence.number_seq('CL', '') seq = 'CL-%s' % seq else: rs = yield DBUtil().query( ("select * from manor.manor_app_instance " "where app_serial='%s'") % app_name) seq = rs[0]['app_id'] info_serial, serial = create_instance(self.request, name, action_name, seq) self.response( generals.gen_response({ 'serial': str(serial), 'info': info_serial }))
def list_instance(self): rs = yield do_list_instance() self.response(generals.gen_response(rs))
def get_app_resources(self, serial, *args): rs = yield list_app_resources(serial) self.response(generals.gen_response(rs))
def save_template(self, action): app_path = cfgutils.getval('app', 'template_path') body = json.loads(self.request.body) try: check_template(body) # 为流程生成ID for a in body['action']: if 'name' not in a or a['name'] == '': seq = yield sequence.number_seq('SA', '') a['name'] = 'SA-%s' % seq logging.getLogger('manor').debug( 'generate action name: %s' % a['name']) else: logging.getLogger('manor').debug('action name: %s' % a['name']) if action == 'create': operation_log_type = Operator.CREATE seq = yield sequence.number_seq('TM', '') body['name'] = 'TM-%s' % seq body['status'] = 0 else: operation_log_type = Operator.UPDATE if 'name' not in body: raise Exception('error.manor.templates.no.name') if body['name'] == '': raise Exception('error.manor.templates.name.is.empty') if not os.path.isfile('%s/%s.yaml' % (app_path, body['name'])): raise Exception('error.manor.templates.update.not.exist') for a in body['action']: if 'deploy' == a['type']: a['label'] = 'install_stream' if 'label' not in a: raise Exception( 'error.manor.templates.action.no.label') if a['label'] == '': raise Exception( 'error.manor.templates.action.label.is.empty') script_path = '%s/_%s' % (app_path, body['name']) if not os.path.isdir(script_path): os.mkdir(script_path) for a in body['action']: for k in a['streamlet'].keys(): if k.split('$')[0] == 'execute_script': node_params = a['streamlet'][k]['params'] for p in node_params: if 'execute_script_content' in p: content = p['execute_script_content'] self.log.debug('get script content:\n%s' % content) e_name = get_script_ex_name(p) with codecs.open( '%s/%s%s' % (script_path, k, e_name), 'w+', 'utf-8') as f: f.write(content) del p['execute_script_content'] tmp_f = str(uuid.uuid1()) with codecs.open('%s/%s' % (app_path, tmp_f), "w+", 'utf-8') as text_file: text_file.write(yaml.safe_dump(body)) os.rename('%s/%s' % (app_path, tmp_f), '%s/%s.yaml' % (app_path, body['name'])) optLog.write(self.request, optLog.Type.APP_TEMPLATE, body['name'], operation_log_type, '%s' % body['label']) self.response(generals.gen_response("ok")) except Exception as e: logging.getLogger('manor').error(generals.trace()) raise e