def perm_role_recycle(request): role_id = request.GET.get('role_id') asset_ids = request.GET.get('asset_id').split(',') # 仅有推送的角色才回收 assets = [get_object(Asset, id=asset_id) for asset_id in asset_ids] recycle_assets = [] for asset in assets: if True in [push.success for push in asset.perm_push.all()]: recycle_assets.append(asset) recycle_resource = gen_resource(recycle_assets) task = MyTask(recycle_resource) try: msg_del_user = task.del_user(get_object(PermRole, id=role_id).name) msg_del_sudo = task.del_user_sudo( get_object(PermRole, id=role_id).name) logger.info("recycle user msg: %s" % msg_del_user) logger.info("recycle sudo msg: %s" % msg_del_sudo) except Exception as e: logger.warning("Recycle Role failed: %s" % e) raise ServerError("回收已推送的系统用户失败: %s" % e) for asset_id in asset_ids: asset = get_object(Asset, id=asset_id) assets.append(asset) role = get_object(PermRole, id=role_id) PermPush.objects.filter(asset=asset, role=role).delete() return HttpResponse('删除成功')
def perm_role_recycle(request): role_id = request.GET.get('role_id') asset_ids = request.GET.get('asset_id').split(',') # 仅有推送的角色才回收 assets = [get_object(Asset, id=asset_id) for asset_id in asset_ids] recycle_assets = [] for asset in assets: if True in [push.success for push in asset.perm_push.all()]: recycle_assets.append(asset) recycle_resource = gen_resource(recycle_assets) task = MyTask(recycle_resource) try: msg_del_user = task.del_user(get_object(PermRole, id=role_id).name) msg_del_sudo = task.del_user_sudo(get_object(PermRole, id=role_id).name) logger.info("recycle user msg: %s" % msg_del_user) logger.info("recycle sudo msg: %s" % msg_del_sudo) except Exception, e: logger.warning("Recycle Role failed: %s" % e) raise ServerError(u"回收已推送的系统用户失败: %s" % e)
logger.warning(u"Delete Role: role_id %s not exist" % role_id) raise ServerError(u"role_id %s 无数据记录" % role_id) role_key = role.key_path # 删除推送到主机上的role recycle_assets = [ push.asset for push in role.perm_push.all() if push.success ] logger.debug(u"delete role %s - delete_assets: %s" % (role.name, recycle_assets)) if recycle_assets: recycle_resource = gen_resource(recycle_assets) task = MyTask(recycle_resource) try: msg_del_user = task.del_user( get_object(PermRole, id=role_id).name) msg_del_sudo = task.del_user_sudo( get_object(PermRole, id=role_id).name) except Exception, e: logger.warning(u"Recycle Role failed: %s" % e) raise ServerError(u"回收已推送的系统用户失败: %s" % e) logger.info(u"delete role %s - execute delete user: %s" % (role.name, msg_del_user)) logger.info(u"delete role %s - execute delete sudo: %s" % (role.name, msg_del_sudo)) # TODO: 判断返回结果,处理异常 # 删除存储的秘钥,以及目录 try: key_files = os.listdir(role_key) for key_file in key_files: os.remove(os.path.join(role_key, key_file)) os.rmdir(role_key) except OSError, e:
# 获取参数删除的role对象 role_id = request.POST.get("id") role = get_object(PermRole, id=role_id) if not role: logger.warning(u"Delete Role: role_id %s not exist" % role_id) raise ServerError(u"role_id %s 无数据记录" % role_id) role_key = role.key_path # 删除推送到主机上的role recycle_assets = [push.asset for push in role.perm_push.all() if push.success] logger.debug(u"delete role %s - delete_assets: %s" % (role.name, recycle_assets)) if recycle_assets: recycle_resource = gen_resource(recycle_assets) task = MyTask(recycle_resource) try: msg_del_user = task.del_user(get_object(PermRole, id=role_id).name) msg_del_sudo = task.del_user_sudo(get_object(PermRole, id=role_id).name) except Exception, e: logger.warning(u"Recycle Role failed: %s" % e) raise ServerError(u"回收已推送的系统用户失败: %s" % e) logger.info(u"delete role %s - execute delete user: %s" % (role.name, msg_del_user)) logger.info(u"delete role %s - execute delete sudo: %s" % (role.name, msg_del_sudo)) # TODO: 判断返回结果,处理异常 # 删除存储的秘钥,以及目录 try: key_files = os.listdir(role_key) for key_file in key_files: os.remove(os.path.join(role_key, key_file)) os.rmdir(role_key) except OSError, e: logger.warning(u"Delete Role: delete key error, %s" % e) raise ServerError(u"删除系统用户key失败: %s" % e)
def perm_role_delete(request): """ delete role page """ if request.method == "GET": try: # 获取参数删除的role对象 role_id = request.GET.get("id") role = get_object(PermRole, id=role_id) if not role: logger.warning("Delete Role: role_id %s not exist" % role_id) raise ServerError("role_id %s 无数据记录" % role_id) # 删除推送到主机上的role filter_type = request.GET.get("filter_type") if filter_type: if filter_type == "recycle_assets": recycle_assets = [ push.asset for push in role.perm_push.all() if push.success ] print(recycle_assets) recycle_assets_ip = ','.join( [asset.ip for asset in recycle_assets]) return HttpResponse(recycle_assets_ip) else: return HttpResponse("no such filter_type: %s" % filter_type) else: return HttpResponse("filter_type: ?") except ServerError as e: return HttpResponse(e) if request.method == "POST": try: # 获取参数删除的role对象 role_id = request.POST.get("id") role = get_object(PermRole, id=role_id) if not role: logger.warning("Delete Role: role_id %s not exist" % role_id) raise ServerError("role_id %s 无数据记录" % role_id) role_key = role.key_path # 删除推送到主机上的role recycle_assets = [ push.asset for push in role.perm_push.all() if push.success ] logger.debug("delete role %s - delete_assets: %s" % (role.name, recycle_assets)) if recycle_assets: recycle_resource = gen_resource(recycle_assets) task = MyTask(recycle_resource) try: msg_del_user = task.del_user( get_object(PermRole, id=role_id).name) msg_del_sudo = task.del_user_sudo( get_object(PermRole, id=role_id).name) except Exception as e: logger.warning("Recycle Role failed: %s" % e) raise ServerError("回收已推送的系统用户失败: %s" % e) logger.info("delete role %s - execute delete user: %s" % (role.name, msg_del_user)) logger.info("delete role %s - execute delete sudo: %s" % (role.name, msg_del_sudo)) # TODO: 判断返回结果,处理异常 # 删除存储的秘钥,以及目录 try: key_files = os.listdir(role_key) for key_file in key_files: os.remove(os.path.join(role_key, key_file)) os.rmdir(role_key) except OSError as e: logger.warning("Delete Role: delete key error, %s" % e) raise ServerError("删除系统用户key失败: %s" % e) logger.info("delete role %s - delete role key directory: %s" % (role.name, role_key)) # 数据库里删除记录 role.delete() return HttpResponse("删除系统用户: %s" % role.name) except ServerError as e: return HttpResponseBadRequest("删除失败, 原因: %s" % e) return HttpResponseNotAllowed("仅支持POST")