def server_list(): select_fields = ['PrivateIP'] svr_list = [ x[0] for x in mysql_exec.select_sql('serverinfo', select_fields) ] woops_log.log_write('server').debug('server_list:%s' % svr_list) return svr_list
def user_list(): fields = [ 'id', 'login_name', 'name_cn', 'password', 'mobile', 'email', 'role', 'status', 'update_time', 'last_login_time' ] users_info_tuple = mysql_exec.select_sql('users', fields) user_info_list = [dict(zip(fields, i)) for i in users_info_tuple] woops_log.log_write('users').debug('user_info_list : %s' % user_info_list) return render_template("/user_list.html", users_info=user_info_list)
def app_ip_detail(mysql_table): app_ip_fields = ['app_ip'] app_ip_select = mysql_exec.select_sql('%s' % (mysql_table), app_ip_fields) if app_ip_select: app_ip_list_pre = [i[0].split(' ; ') for i in app_ip_select] app_ip_list = [] for i in app_ip_list_pre: app_ip_list = app_ip_list + i app_ip_list = list(set(app_ip_list)) woops_log.log_write('server').debug('app_ip_list: %s' % app_ip_list) return app_ip_list
def server_list(): fields_1 = [ 'id', 'HostName', 'PrivateIP', 'PublicIP', 'ENV', 'OS', 'Kernel' ] fields_2 = ['CpuCount', 'RAM_GB', 'PhyDiskSize', 'IDC', 'status'] fields = fields_1 + fields_2 server_list = mysql_exec.select_sql('serverinfo', fields) server_list_list = [dict(zip(fields, i)) for i in server_list] woops_log.log_write('server').debug('server_list_list: %s' % server_list_list) return render_template("server_list.html", server_list=server_list_list)
def server_delete(): delete_condition = {} delete_server = {} delete_condition['id'] = request.args.get('id') delete_server = [ dict(zip(['PrivateIP'], i)) for i in mysql_exec.select_sql( 'serverinfo', ['PrivateIP'], delete_condition) ][0] mysql_exec.delete_sql('serverinfo', delete_condition) woops_log.log_write('server').info('Delete server "%s" success' % delete_server) return json.dumps({'result': 0, 'msg': 'ok'})
def user_info(): select_conditon = {} select_conditon['login_name'] = session.get('login_name', None) fields = [ 'id', 'login_name', 'name_cn', 'mobile', 'email', 'role', 'status', 'update_time', 'last_login_time' ] user_info_dict = [ dict(zip(fields, i)) for i in mysql_exec.select_sql('users', fields, select_conditon) ][0] woops_log.log_write('users').debug('user_info_dict : %s' % user_info_dict) return render_template("user_personal_info.html", user_info=user_info_dict)
def user_delete(): delete_condition = {} delete_user = {} delete_condition['id'] = request.args.get('id') delete_user = [ dict(zip(['login_name'], i)) for i in mysql_exec.select_sql( 'users', ['login_name'], delete_condition) ][0] woops_log.log_write('users').debug('delete_user : %s' % delete_user['login_name']) mysql_exec.delete_sql('users', delete_condition) woops_log.log_write('users').info('Delete user "%s" success' % delete_user['login_name']) return json.dumps({'result': 0, 'msg': 'ok'})
def pub_list(): fields_1 = [ 'id', 'pub_title', 'workform_type', 'pub_level', 'pub_module', 'pub_content', 'pub_SQL', 'pub_SQL_detail' ] fields_2 = [ 'pub_application_people', 'pub_status', 'pub_audit_people', 'pub_submit_time', 'pub_done_time', 'pub_operation_people' ] fields = fields_1 + fields_2 pub_info_tuple = mysql_exec.select_sql('publish_online', fields) pub_info_list = [dict(zip(fields, i)) for i in pub_info_tuple] woops_log.log_write('publish').debug('pub_info_list : %s' % pub_info_list) print "**** pub_info_list ****" print pub_info_list return json.dumps({'pub_info': pub_info_list})
def pub_my(): if request.method == 'GET': fields_1 = [ 'id', 'pub_title', 'pub_level', 'pub_module', 'pub_content', 'pub_SQL', 'pub_SQL_detail', 'pub_application_people' ] fields_2 = [ 'workform_type', 'pub_status', 'pub_audit_people', 'pub_submit_time', 'audit_time', 'pub_done_time', 'pub_operation_people' ] fields = fields_1 + fields_2 my_conditon = {} my_conditon['pub_application_people'] = session.get('login_name', None) pub_my_tuple = mysql_exec.select_sql('publish_online', fields, my_conditon) pub_my_list = [dict(zip(fields, i)) for i in pub_my_tuple] woops_log.log_write('publish').debug('pub_my_list : %s' % pub_my_list) print "**** pub_my_list ****" print pub_my_list return json.dumps({'pub_my': pub_my_list})
def pub_info(): if request.method == 'GET': fields_1 = [ 'id', 'pub_title', 'pub_level', 'pub_module', 'pub_content', 'pub_SQL', 'pub_SQL_detail', 'pub_application_people' ] fields_2 = [ 'workform_type', 'pub_status', 'pub_submit_time', 'QA_audit', 'QA_audit_result', 'OPS_audit', 'OPS_pub_result' ] fields = fields_1 + fields_2 pub_info_conditon = {} pub_info_conditon['id'] = request.args.get('id', None) pub_info_tuple = mysql_exec.select_sql('publish_online', fields, pub_info_conditon) pub_info_dict = [dict(zip(fields, i)) for i in pub_info_tuple][0] woops_log.log_write('publish').debug('pub_info_dict : %s' % pub_info_dict) print "**** pub_info_dict ****" print pub_info_dict return json.dumps(pub_info_dict)
def user_update(): if request.method == 'GET': select_condition = {} select_condition['id'] = request.args.get('id') select_fields = [ 'id', 'login_name', 'name_cn', 'mobile', 'email', 'role', 'status' ] select_results_pre = mysql_exec.select_sql('users', select_fields, select_condition) select_result_dict = [ dict(zip(select_fields, x)) for x in select_results_pre ][0] woops_log.log_write('users').debug('select_result_dict : %s' % select_result_dict) return json.dumps(select_result_dict) if request.method == "POST": update_conditions = {} update_user = dict((i, j[0]) for i, j in dict(request.form).items()) update_conditions['id'] = update_user['id'].strip('') update_conditions['login_name'] = update_user['login_name'].strip('') update_user['update_time'] = "%s" % ( datetime.now().strftime("%Y-%m-%d %X")) woops_log.log_write('users').debug('update_user : %s' % update_user) if not update_user['name_cn'].strip('') or not update_user[ 'mobile'].strip('') or not update_user['email'].strip(''): woops_log.log_write('users').error( 'User information input can not be empty') return json.dumps({ 'result': 1, 'msg': 'User information input can not be empty' }) del update_user['id'] del update_user['login_name'] mysql_exec.update_sql('users', update_user, update_conditions) woops_log.log_write('users').info('User information change success') return json.dumps({'result': 0, 'msg': 'ok'})
def user_login(): if request.method == 'POST': login_info = {} login_info = dict((i, j[0]) for i, j in dict(request.form).items()) if not login_info.get('login_name', None) or not login_info.get( 'password', None): woops_log.log_write('users').error('Input can not be empty') return json.dumps({'result': 1, 'msg': 'Input can not be empty'}) fields = [ 'id', 'login_name', 'name_cn', 'password', 'mobile', 'email', 'role', 'status', 'last_login_time' ] password = hashlib.md5(login_info['password'] + salt).hexdigest() del login_info['password'] select_login_info = mysql_exec.select_sql('users', fields, login_info) woops_log.log_write('users').debug('select_login_info:%s' % select_login_info) if not select_login_info: woops_log.log_write('users').error('user "%s" does not exists' % login_info['login_name']) return json.dumps({'result': 1, 'msg': 'user does not exists'}) select_login_info_dict = [ dict(zip(fields, x)) for x in select_login_info ][0] woops_log.log_write('users').debug('select_login_info_dict:%s' % select_login_info_dict) if select_login_info_dict['password'] != password: woops_log.log_write('users').error( 'User "%s" input password is not correct' % login_info['login_name']) return json.dumps({ 'result': 1, 'msg': 'Input password is not correct' }) if select_login_info_dict['status'] == 1: woops_log.log_write('users').error( 'The state of the user "%s" has locked, please contact OPS' % login_info['login_name']) return json.dumps({ 'result': 1, 'msg': 'User has locked,please contact OPS' }) session['login_name'] = select_login_info_dict['login_name'] session['role'] = select_login_info_dict['role'] session['status'] = select_login_info_dict['status'] mysql_exec.update_sql('users', { 'last_login_time': "%s" % (datetime.now().strftime("%Y-%m-%d %X")) }, login_info) woops_log.log_write('users').info('"%s" login success' % login_info['login_name']) return json.dumps({'result': 0, 'msg': 'ok'}) if request.method == 'GET': return render_template("user_login.html")
def user_logout(): woops_log.log_write('users').info('User "%s" logout success' % session['login_name']) del session['login_name'] return redirect("/users/login")
def server_add(): if request.method == 'GET': return render_template("server_add.html") if request.method == 'POST': server_add_dict = dict( (i, j[0]) for i, j in dict(request.form).items()) if not server_add_dict['HostName'].strip() or not server_add_dict[ 'ENV'].strip() or not server_add_dict['PrivateIP']: woops_log.log_write('server').error( 'The * symbol part of the input cannot be empty') msg = "The * symbol part of the input cannot be empty" return json.dumps({'result': 1, 'msg': msg}) if not server_add_dict['OS'] or not server_add_dict[ 'Kernel'] or not server_add_dict['CpuType']: woops_log.log_write('server').error( 'The * symbol part of the input cannot be empty') msg = "The * symbol part of the input cannot be empty" return json.dumps({'result': 1, 'msg': msg}) if not server_add_dict['CpuCount'] or not server_add_dict[ 'RAM_GB'] or not server_add_dict[ 'PhyDiskSize'] or not server_add_dict['IDC']: woops_log.log_write('server').error( 'The * symbol part of the input cannot be empty') msg = "The * symbol part of the input cannot be empty" return json.dumps({'result': 1, 'msg': msg}) if server_add_dict['PrivateIP'] in server_list(): woops_log.log_write('server').error('IP "%s" already exists' % server_add_dict['PrivateIP']) return json.dumps({'result': 1, 'msg': 'IP already exists'}) server_add_dict['OnlineTime'] = datetime.now().strftime("%Y-%m-%d %X") woops_log.log_write('server').debug('server_add_dict: %s' % server_add_dict) insert_fields = [x for x in server_add_dict.keys()] mysql_exec.insert_sql('serverinfo', insert_fields, server_add_dict) woops_log.log_write('server').info( 'The server "%s" added successfully' % server_add_dict['PrivateIP']) return json.dumps({'result': 0, 'msg': 'ok'})
def user_update_password(): fields = ['id', 'login_name', 'password'] if request.method == 'POST': select_password_condition = {} update_password_info = {} select_password_condition['login_name'] = request.form.get( 'login_name').strip('') update_password_info['password'] = request.form.get( 'password_new').strip('') password_again = hashlib.md5( request.form.get('password_new_again').strip('') + salt).hexdigest() if session.get('role', 2) != 0 and session.get('role', 2) != 1: password_old_input = request.form.get('password_old_input', None).strip('') select_password_pre = mysql_exec.select_sql( 'users', fields, select_password_condition) select_password_info = [ dict(zip(fields, x)) for x in select_password_pre ] woops_log.log_write('users').debug('select_password_info : %s' % select_password_info) password_old = select_password_info[0]['password'] if not password_old_input: woops_log.log_write('users').error( 'The old password input can not be empty') return json.dumps({ 'result': 1, 'msg': 'The old password input can not be empty' }) password_old_input = hashlib.md5(password_old_input + salt).hexdigest() if password_old_input != password_old: woops_log.log_write('users').error( 'The old password input can not be correctly') return json.dumps({ 'result': 1, 'msg': 'The old password input can not be correctly' }) if not update_password_info['password']: woops_log.log_write('users').error( 'The update password input can not be empty') return json.dumps({ 'result': 1, 'msg': 'The update password input can not be empty' }) update_password_info['password'] = hashlib.md5( update_password_info['password'] + salt).hexdigest() if update_password_info['password'] != password_again: woops_log.log_write('users').error( 'Input two password is not consistent') return json.dumps({ 'result': 1, 'msg': 'Input two password is not consistent' }) update_password_info['update_time'] = "%s" % ( datetime.now().strftime("%Y-%m-%d %X")) mysql_exec.update_sql('users', update_password_info, select_password_condition) woops_log.log_write('users').info('User password change success') return json.dumps({'result': 0, 'msg': 'ok'})
def server_update(): if request.method == 'GET': select_condition = {} select_condition['id'] = request.args.get('id') fields_1 = [ 'id', 'HostName', 'PrivateIP', 'PublicIP', 'ENV', 'ServerBrand', 'ServerModel', 'OS', 'Kernel' ] fields_2 = [ 'CpuType', 'CpuCount', 'RAM_GB', 'PhyDiskSize', 'IDC', 'status', 'OnlineTime', 'OfflineTime' ] fields = fields_1 + fields_2 server_info = mysql_exec.select_sql('serverinfo', fields, select_condition) server_info_dict = [dict(zip(fields, i)) for i in server_info][0] woops_log.log_write('server').debug('server_info_dict: %s' % server_info_dict) return json.dumps(server_info_dict) if request.method == 'POST': server_update_dict = dict( (i, j[0]) for i, j in dict(request.form).items()) if not server_update_dict['HostName'].strip( ) or not server_update_dict['ENV'].strip( ) or not server_update_dict['OS']: woops_log.log_write('server').error( 'The * symbol part of the input cannot be empty') msg = "The * symbol part of the input cannot be empty" return json.dumps({'result': 1, 'msg': msg}) if not server_update_dict['Kernel'] or not server_update_dict[ 'CpuType'] or not server_update_dict['CpuCount']: woops_log.log_write('server').error( 'The * symbol part of the input cannot be empty') msg = "The * symbol part of the input cannot be empty" return json.dumps({'result': 1, 'msg': msg}) if not server_update_dict['RAM_GB'] or not server_update_dict[ 'PhyDiskSize'] or not server_update_dict['IDC']: woops_log.log_write('server').error( 'The * symbol part of the input cannot be empty') msg = "The * symbol part of the input cannot be empty" return json.dumps({'result': 1, 'msg': msg}) if server_update_dict[ 'status'] == '1' or server_update_dict['ENV'] != 'online': app_ip_list = app_ip_detail('cmdb_online') if server_update_dict['PrivateIP'] in app_ip_list: woops_log.log_write('server').error( 'Server update failure,because of he ip has been in cmdb_online ; Please delete if from cmdb_online first!' ) msg = 'The ip has been in cmdb_online ; Please delete if from cmdb_online first!' return json.dumps({'result': 1, 'msg': msg}) update_conditions = {} update_conditions['id'] = server_update_dict['id'].strip('') update_conditions['PrivateIP'] = server_update_dict['PrivateIP'].strip( '') if server_update_dict['status'].strip('') == '1': server_update_dict['OfflineTime'] = datetime.now().strftime( "%Y-%m-%d %X") else: server_update_dict['OfflineTime'] = '' woops_log.log_write('server').debug('server_update_dict: %s' % server_update_dict) del server_update_dict['id'] del server_update_dict['PrivateIP'] mysql_exec.update_sql('serverinfo', server_update_dict, update_conditions) woops_log.log_write('server').info( 'The server "%s" update successfully' % update_conditions['PrivateIP']) return json.dumps({'result': 0, 'msg': 'ok'})
def user_register(): woops_log.log_write('hello').error('hello') select_fields = ['login_name'] user_list = [x[0] for x in mysql_exec.select_sql('users', select_fields)] woops_log.log_write('users').debug('user_list:%s' % user_list) if request.method == 'GET': return render_template('/register.html') if request.method == 'POST': user_register = dict((i, j[0]) for i, j in dict(request.form).items()) woops_log.log_write('users').debug('user_register:%s' % user_register) user_register['status'] = 0 user_register['update_time'] = datetime.now().strftime("%Y-%m-%d %X") user_register['last_login_time'] = user_register['update_time'] if not user_register['login_name'].strip( '') or not user_register['name_cn'].strip( '') or not user_register['password'].strip(''): woops_log.log_write('users').error('Input can not be empty') return json.dumps({'result': 1, 'msg': 'Input can not be empty'}) if not user_register['mobile'].strip('') or not user_register[ 'email'].strip('') or not user_register['role'].strip(''): woops_log.log_write('users').error('Input can not be empty') return json.dumps({'result': 1, 'msg': 'Input can not be empty'}) if user_register['login_name'] in user_list: woops_log.log_write('users').error('user "%s" already exists' % user_register['login_name']) return json.dumps({'result': 1, 'msg': 'User already exists'}) if user_register['password'] != user_register['password_again']: woops_log.log_write('users').error( 'Input two password is not consistent') return json.dumps({ 'result': 1, 'msg': 'Input two password is not consistent' }) del user_register['password_again'] insert_fields = [x for x in user_register.keys()] user_register['password'] = hashlib.md5(user_register['password'] + salt).hexdigest() mysql_exec.insert_sql('users', insert_fields, user_register) woops_log.log_write('users').info('User "%s" registration successful' % user_register['login_name']) return json.dumps({'result': 0, 'msg': 'ok'})
def pub_audit(): if request.method == 'GET': fields_1 = [ 'id', 'pub_title', 'pub_level', 'pub_module', 'pub_SQL', 'pub_application_people' ] fields_2 = [ 'workform_type', 'pub_status', 'pub_submit_time', 'pub_audit_people', 'audit_time' ] fields = fields_1 + fields_2 audit_conditon = {} audit_conditon['pub_status'] = session.get('role', None) pub_audit_tuple = mysql_exec.select_sql('publish_online', fields, audit_conditon) pub_audit_list = [dict(zip(fields, i)) for i in pub_audit_tuple] woops_log.log_write('publish').debug('pub_audit_list : %s' % pub_audit_list) print "**** pub_my_audit ****" print pub_audit_list return json.dumps({'pub_audit': pub_audit_list}) if request.method == 'POST': audit_msg_condation = {} audit_msg = dict((i, j[0]) for i, j in dict(request.form).items()) audit_msg_condation['id'] = audit_msg.get('id', None) if not audit_msg_condation['id']: return json.dumps({ 'result': 1, 'msg': 'Please contact the OPS...' }) del audit_msg['id'] if session['role'] == 0 or session['role'] == 3: audit_msg['pub_audit_people'] = session.get('login_name', None) if not audit_msg['pub_audit_people']: return json.dumps({ 'result': 1, 'msg': 'Please log in again...' }) if audit_msg['QA_audit_result'] == 'YES': audit_msg['pub_status'] = '1' audit_msg['audit_time'] = datetime.now().strftime( "%Y-%m-%d %X") else: audit_msg['pub_status'] = '6' audit_msg['audit_time'] = datetime.now().strftime( "%Y-%m-%d %X") audit_msg['pub_done_time'] = datetime.now().strftime( "%Y-%m-%d %X") elif session['role'] == 1: audit_msg['pub_operation_people'] = session.get('login_name', None) if not audit_msg['pub_operation_people']: return json.dumps({ 'result': 1, 'msg': 'Please log in again...' }) if audit_msg['OPS_pub_result'] == 'YES': audit_msg['pub_status'] = '5' audit_msg['pub_done_time'] = datetime.now().strftime( "%Y-%m-%d %X") else: audit_msg['pub_status'] = '7' audit_msg['pub_done_time'] = datetime.now().strftime( "%Y-%m-%d %X") mysql_exec.update_sql('publish_online', audit_msg, audit_msg_condation) return json.dumps({'result': 0, 'msg': 'ok'})
def pub_add(): if request.method == 'GET': fields = ['app_name'] module_name_tuple = mysql_exec.select_sql('cmdb_online', fields) module_name_list = [dict(zip(fields, i)) for i in module_name_tuple] woops_log.log_write('publish').debug('module_name_list : %s' % module_name_list) return json.dumps({'module_name': module_name_list}) if request.method == 'POST': pub_info = dict( (i, ' ; '.join(j)) for i, j in dict(request.form).items()) print "**** pub_info ****" print pub_info woops_log.log_write('publish').debug('pub_info:%s' % pub_info) if not pub_info['pub_title'].strip( '') or not pub_info['pub_module'].strip( '') or not pub_info['pub_content'].strip(''): msg = 'Input can not be empty' woops_log.log_write('publish').error(msg) return json.dumps({'result': 1, 'msg': msg}) if pub_info['pub_SQL'].strip( '') == 'YES' and not pub_info['pub_SQL_detail'].strip(''): msg = 'Please choose correct if there were any SQL' woops_log.log_write('publish').error(msg) return json.dumps({'result': 1, 'msg': msg}) if pub_info['pub_SQL'].strip( '') == 'NO' and pub_info['pub_SQL_detail'].strip(''): msg = 'Please choose correct if there were any SQL' woops_log.log_write('publish').error(msg) return json.dumps({'result': 1, 'msg': msg}) if pub_info['pub_level'].strip('') == 'emergency' or pub_info[ 'workform_type'].strip('') == 'rollback': pub_info['pub_status'] = '0' else: pub_info['pub_status'] = '3' pub_info['pub_submit_time'] = datetime.now().strftime("%Y-%m-%d %X") pub_info['pub_application_people'] = session.get('login_name', None) print '**** pub_info ****' print pub_info insert_fields = [x for x in pub_info.keys()] mysql_exec.insert_sql('publish_online', insert_fields, pub_info) woops_log.log_write('publish').info('"%s" submit successful' % pub_info['pub_title']) return json.dumps({'result': 0, 'msg': 'ok'})