def do_delete_app(serial): log = logging.getLogger('manor') def do_delete(rows): for r in rows: log.debug('delete stack %s' % r['stack_id']) stack_util.delete_stack(r['stack_id']) execute_query( "SELECT * FROM manor.manor_stacks where app_serial='%s'" % serial, do_delete) execute("DELETE FROM manor.manor_app_instance WHERE app_serial=%s", serial) execute("DELETE FROM manor.manor_stacks WHERE app_serial=%s", serial) execute("DELETE FROM manor.manor_app_group_seq WHERE app_serial=%s", serial) log_file = '%s/stream_%s.log' % (cfgutils.getval('log', 'path'), serial) if os.path.isfile(log_file): os.remove(log_file) if os.path.exists('%s/%s' % (cfgutils.getval('log', 'path'), serial)): shutil.rmtree('%s/%s' % (cfgutils.getval('log', 'path'), serial)) path = cfgutils.getval('app', 'instance_path') if os.path.isfile('%s/%s.yaml' % (path, serial)): os.remove('%s/%s.yaml' % (path, serial))
def message_sending(self): while True: time.sleep(0.5) if self.msg != self.current_msg: self.msg = self.current_msg send_msg = json.loads(self.msg) info = list_app_resources(self.serial) while not info.done(): pass send_msg['info'] = info.result() send_msg = json.dumps(send_msg) clients = [] for c in _clients.values(): if self.serial in set(c['serial']): c['client'].write_message(send_msg) clients.append(1) if len([ _ for _ in _clients.values() if self.serial in _['serial'] ]) == 0: self.status = 'interrupt' # 这段代码的位置很重要。 if self.status == 'interrupt': del _monitors[self.serial] break if self.status == 'building': def do_result(rows): if len(rows) > 0: self.status = rows[0]['state'] self.log.debug('check state from db : %s' % self.status) execute_query(("SELECT * FROM manor.manor_app_instance " "where app_serial='%s'") % self.serial, do_result) if self.status == 'failure': try: r = redis_tool.get_it() error = r.get('manage_error_$_%s' % self.serial) if error is None: error = '""' error = json.loads(error) self.current_msg = '{"serial":"%s","status":"failure","msg":[],"error":%s}' % ( self.serial, json.dumps(error)) thread.start_new(self.delete_error_msg, (r, )) except: self.log.error(generals.trace()) self.status = 'interrupt' if self.status == 'thread_error': self.current_msg = '{"serial":"%s","status":"error","msg":[]}' % self.serial self.status = 'interrupt'
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 create_instance(request, name, action_name, seq): log = logging.getLogger('manor') try: log.debug(request.body) instance_path = cfgutils.getval('app', 'instance_path') template_path = cfgutils.getval('app', 'template_path') params = json.loads(request.body) app_name = params['app_name'] if 'type' in params: action_type = params['type'] else: action_type = 'deploy' if action_type == 'deploy': if not os.path.isfile('%s/%s.yaml' % (template_path, name)): raise Exception('error.manor.templates.not.exist') content = load_template(template_path, name.replace(' ', '') + '.yaml') else: with open( '%s/%s.yaml' % (instance_path, params['app_name'].replace(' ', ''))) as f: rs = f.read() content = yaml.safe_load(rs) if action_name not in [_['name'] for _ in content['action']]: raise Exception('error.manor.action.not.exist') if 'app_description' not in content: content['app_description'] = '' if params['app_description']: content['app_description'] = params['app_description'] # 合并参数 merge_params(action_name, content, log, params) check_template(content) info_token_list = [] if action_type == 'deploy': serial = uuid.uuid1() execute(( "INSERT INTO manor.manor_app_instance" "(template_name,app_name,app_serial,state,app_description,app_id)" "VALUES(%s,%s,%s,%s,%s,%s)"), (name, app_name, serial, 'building', content['app_description'], seq)) # save origin templates. log.debug('save app templates : %s' % str(serial)) with codecs.open( '%s/%s.yaml' % (instance_path, str(serial).replace(' ', '')), 'w+', 'utf-8') as f: f.write(yaml.safe_dump(content)) else: # 执行管理流程 serial = params['app_name'] streamlet_key = params['params'].keys() for k in streamlet_key: s_ps = params['params'][k]['params'] for s_p in s_ps: if 'info_return' in s_p and s_p['info_return']: token = str(uuid.uuid1()) s_ps.append({'info_token': token}) info_token_list.append(token) # 为了token merge_params(action_name, content, log, params) logging.getLogger('manor').debug(pyaml.dumps(content)) stream.execute(content, action_name, str(serial)) if action_type == 'deploy': optLog.write(request, optLog.Type.APP_INSTANCE, seq, Operator.CREATE, '%s' % app_name) else: action_label = [ _ for _ in content['action'] if _['name'] == action_name ][0]['label'] def get_result(rows): app_name = rows[0]['app_name'] optLog.write(request, optLog.Type.APP_INSTANCE, seq, Operator.EXECUTE_ACTION, '%s %s' % (app_name, action_label)) execute_query( "select * from manor.manor_app_instance where app_serial='%s'" % serial, get_result) return info_token_list, serial except Exception as e: log.error(generals.trace()) raise e
def working(self): while True: if self.status == 'interrupt': break if self.status == 'failure': break if self.status == 'normal': try: def set_rs(rows): resources = [] for r in rows: stack_id = r['stack_id'] group_name = r['group_name'] res = get_resources_info(stack_id) while True: if res.done(): break time.sleep(0.1) for info in res.result(): info['group_id'] = group_name resources = resources + res.result() for r in resources: redis = redis_tool.get_it() road_map = redis.keys('mapup*') ip = r['addresses'].values()[0][0]['addr'] ips = [_.split('_$_')[3] for _ in road_map] if ip in ips: r['agent_state'] = 'online' else: r['agent_state'] = 'offline' if len(resources) == len( [_ for _ in resources if _['status'] == 'ACTIVE']): self.current_msg = json.dumps({ 'serial': self.serial, 'status': 'normal', 'msg': self.return_msg(resources) }) if 'offline' in [ _['agent_state'] for _ in resources if _['status'] == 'ACTIVE' ]: self.current_msg = json.dumps({ 'serial': self.serial, 'status': 'offline', 'msg': self.return_msg(resources) }) elif len([ _ for _ in resources if _['status'] == 'ACTIVE' ]) != 0: group_ids = [_['group_id'] for _ in resources] down = False for g in group_ids: if len([ _ for _ in resources if _['status'] == 'ACTIVE' and _['group_id'] == g ]) == 0: down = True if not down: self.current_msg = json.dumps({ 'serial': self.serial, 'status': 'part', 'msg': self.return_msg(resources) }) else: self.current_msg = json.dumps({ 'serial': self.serial, 'status': 'down', 'msg': self.return_msg(resources) }) else: self.current_msg = json.dumps({ 'serial': self.serial, 'status': 'stop', 'msg': self.return_msg(resources) }) r = redis_tool.get_it() error = r.get('manage_error_$_%s' % self.serial) if error: self.log.debug(error) current_msg = json.loads(self.current_msg) current_msg['error'] = json.loads(error) self.current_msg = json.dumps(current_msg) thread.start_new(self.delete_error_msg, (r, )) execute_query( ("select stack_id,group_name from manor.manor_stacks " "WHERE app_serial= '%s'" % self.serial), set_rs) except: self.log.error('work thread error ..') self.log.error(generals.trace()) self.status = 'thread_error' time.sleep(1)