def del_disk_by_id(self, disk_id): resp = ResMsg() slot = get_slot_from_id(disk_id) print("del:", disk_id, slot) if slot: return self.del_disk_by_slot(slot) else: resp.code = 1 resp.msg = u"未获取到磁盘ID" return Response(data=resp.to_dict(), status=status.HTTP_200_OK)
def add_disk_by_id(self, disk_id): resp = ResMsg() slot = get_slot_from_id(disk_id) logger.info("slot:{}".format(slot)) if DEBUG: slot = slot if slot: return self.add_disk_by_slot(slot) else: resp.code = 1 resp.msg = u"未获取到磁盘ID" return Response(data=resp.to_dict(), status=status.HTTP_200_OK)
def post(self, request): "gluster volume remove-brick gvol replica 2 gfs-224:/opt/gfs/bk3 gfs-223:/opt/gfs/bk3 force" request_data = request.data volname = request_data.get("volname", None) stripe = request_data.get("stripe", 0) replica = request_data.get("replica", 0) bricks = request_data.get("bricks", []) print(request_data) print("volname:", volname, replica, stripe) print("brikcs:", bricks) resp = ResMsg() resp.data = bricks return Response(data=resp.to_dict())
def get(self, request, format=None): disk_info = self.get_raid_info() resp = ResMsg() if disk_info is None: resp.code = 1 resp.msg = u"获取磁盘信息失败" return Response(data=resp.to_dict()) disk_data = self.gene_raiddata_resp(request, disk_info) resp.data = disk_data resp.msg = u"查询RAID成功" return Response(data=resp.to_dict(), status=status.HTTP_200_OK)
def delete(self, request, format=None): request_params = request.data raid_id = request_params.get("id", None) resp = ResMsg() if not raid_id: resp.code = 1 resp.msg = u"获取RAID失败" return Response(data=resp.to_dict(), status=status.HTTP_200_OK) else: code, result = delete_raid(raid_id) if code == 0: resp.msg = u"RAID删除成功" else: resp.code = 1 resp.msg = u"RAID:{}删除失败".format(raid_id) return Response(data=resp.to_dict(), status=status.HTTP_200_OK)
def add_disk_by_slot(self, disk_slot): resp = ResMsg() disk_size = get_disk_size_by_slot(disk_slot) logger.info("disk_size: {} {}".format(disk_slot, disk_size)) if disk_size: hd_name = generate_hd_name(disk_slot) logger.info("ha name:{}".format(hd_name)) code, result = psutil_shell_add_disk(disk_slot, hd_name) if code == 0: resp.msg = u"添加成功" self.config_json = self.load() pool = self.config_json['pool'] self.config_json['disks'][disk_slot] = { "disk_slot": disk_slot, "hd_name": hd_name } if hd_name not in pool: pool.append(hd_name) self.config_json['pool'] = pool self.save() else: resp.code = 1 resp.msg = u"添加磁盘失败" + result print("add disk error. {}".format(result)) return Response(data=resp.to_dict(), status=status.HTTP_200_OK)
def delete(self, request): request_data = request.data vol = request_data.get('volname', None) resp = ResMsg() if vol is None: resp.code = 1 resp.msg = u"卷获取失败,卷名为空" else: code, result = gfs_volume_stop(vol) print("delete vol : code:{}, {}".format(code, result)) if code: resp.msg = u"停用成功" else: resp.code = 1 resp.msg = result return Response(data=resp.to_dict())
def put(self, request): request_data = request.data vol = request_data.get('volname', None) print("put ", vol) resp = ResMsg() if vol is None: resp.code = 1 resp.msg = u"卷获取失败,卷名为空" else: code, result = gfs_volume_start(vol) if code: resp.msg = u"启用成功" else: resp.code = 1 resp.msg = result return Response(data=resp.to_dict())
def delete(self, request): request_data = request.data hostname = request_data.get('hostname', None) print(hostname) resp = ResMsg() code, result = gfs_peer_detach(hostname) if code: resp.msg = u"删除成功" else: resp.code = 1 resp.msg = u"删除失败," + result return Response(data=resp.to_dict())
def post(self, request): request_data = request.data hostname = request_data.get('hostname', None) print(request_data) print("add peer:", hostname) resp = ResMsg() code, result = gfs_peer_probe(hostname) if code: resp.msg = u"添加成功" else: resp.code = 1 resp.msg = u"添加失败," + result return Response(data=resp.to_dict())
def post(self, request): request_param = request.data # print(request_param) # print(request.META) key = request_param.get('key') content = request_param.get('content') user = request_param.get('user') ip = request.META.get('REMOTE_ADDR') resp = ResMsg() if not self.add_log(key, content, user, ip): resp.code = 1 resp.msg = u"添加失败" else: resp.msg = u"添加成功" return Response(data=resp.to_dict())
def get(self, request): request_parmas = request.query_params.dict() start_time = request_parmas.get("st", None) # start time end_time = request_parmas.get("et", None) # end time content = request_parmas.get("content", "") key = request_parmas.get("key", "") ip = request_parmas.get("ip", "") page = int(request_parmas.get("page", 1)) # print(start_time, end_time, ip, key, content) resp = ResMsg() if start_time is not None and end_time is not None: start_time = str_to_datetime(start_time) end_time = str_to_datetime(end_time) else: resp.code = 1 resp.msg = u"时间范围输入错误" return Response(data=resp.to_dict()) offset_start = (page - 1) * 20 offset_end = page * 20 data = UserLog.objects.filter(key__contains=content, content__contains=key, ip__contains=ip, created__gte=start_time, created__lte=end_time).all().order_by('-created' )[offset_start: offset_end] total = data.count() result = [] for item in data: log_dict = dict() log_dict['user'] = item.user log_dict['id'] = item.id log_dict['key'] = item.key log_dict['content'] = item.content log_dict['created'] = datatime_to_str(item.created) log_dict['ip'] = item.ip result.append(log_dict) pass db.connections.close_all() resp.data = result total = ceil(total / 20) total = total if total > 0 else 1 return Response(data={"data": resp.to_dict(), "total": total})
def del_disk_by_slot(self, disk_slot): print("del:", disk_slot) resp = ResMsg() disk_size = get_disk_size_by_slot(disk_slot) if disk_size: hd_name = generate_hd_name(disk_slot) code, result = psutil_shell_delete_disk(disk_slot, hd_name) print("del", hd_name, disk_size) if code == 0: self.config_json = self.load() if hd_name in self.config_json['pool']: self.config_json['pool'].remove(hd_name) self.save() resp.msg = u"删除成功" else: resp.code = 1 resp.msg = u"删除磁盘失败" + result print("add disk error. {}".format(result)) return Response(data=resp.to_dict(), status=status.HTTP_200_OK)
def post(self, request, format=None): print(self.request.data) print(request.data) request_params = request.data raid_type = request_params.get('type') # 'raid0 raid_disk = request_params.get('disks') # [] # print(raid_type, raid_disk) raid_type_num = raid_type[4:] logger.info("raid type:{}, disk:{}".format(raid_type, raid_disk)) check_param = True disk_num = len(raid_disk) if raid_type_num == '0' or raid_type_num == '1': if disk_num != 2: check_param = False if raid_type_num == '5': if disk_num != 3: check_param = False if raid_type_num == '10': if disk_num != 4: check_param = False resp = ResMsg() if check_param is False: resp.code = 1 resp.msg = u"磁盘数量不正确" return Response(data=resp.to_dict(), status=status.HTTP_200_OK) for disk_id in raid_disk: disk = get_slot_from_id(disk_id) if DEBUG: disk = disk_id if not disk: resp.code = 1 resp.msg = u"磁盘id={}未找到".format(disk_id) return Response(data=resp.to_dict(), status=status.HTTP_200_OK) # print("disk_info:", disk_info) raid = pustil_shell_mount_raid(disk) print('is raid:', raid) if raid: resp.code = 1 resp.msg = u"磁盘{}已配置RAID".format(disk) return Response(data=resp.to_dict(), status=status.HTTP_200_OK) disk_mount = psutil_shell_mount_status(disk) if disk_mount: resp.code = 1 resp.msg = u"磁盘{}使用中,不能配置RAID".format(disk) return Response(data=resp.to_dict(), status=status.HTTP_200_OK) raid_name = "/dev/md" for item in raid_disk: raid_name += str(ord(item[7]) - ord('a')) code, result = create_raid(raid_name, raid_type_num, disk_num, raid_disk) if code == 0: resp.msg = u"RAID配置成功" else: resp.code = 1 resp.msg = u"RAID配置失败" + result return Response(data=resp.to_dict(), status=status.HTTP_200_OK)