예제 #1
0
파일: views.py 프로젝트: wxxyd/iauto
 def delete(self, request):
     form, error = JsonParser(Argument('id', type=int,
                                       help='请指定操作对象')).parse(request.GET)
     if error is None:
         if Config.objects.filter(env_id=form.id).exists():
             return json_response(error='该环境已存在关联的配置信息,请删除相关配置后再尝试删除')
         if Deploy.objects.filter(env_id=form.id).exists():
             return json_response(error='该环境已关联了发布配置,请删除相关发布配置后再尝试删除')
         Environment.objects.filter(pk=form.id).delete()
     return json_response(error=error)
예제 #2
0
파일: views.py 프로젝트: zoushiwen/spug
 def patch(self, request):
     form, error = JsonParser(
         Argument('id', type=int, help='请指定操作对象'),
         Argument('is_active', type=bool,
                  required=False)).parse(request.body, True)
     if error is None:
         Detection.objects.filter(pk=form.id).update(**form)
         if form.get('is_active') is not None:
             if form.is_active:
                 task = Detection.objects.filter(pk=form.id).first()
                 message = {'id': form.id, 'action': 'add'}
                 message.update(
                     task.to_dict(selects=('addr', 'extra', 'rate',
                                           'type')))
             else:
                 message = {'id': form.id, 'action': 'remove'}
             rds_cli = get_redis_connection()
             rds_cli.lpush(settings.MONITOR_KEY, json.dumps(message))
     return json_response(error=error)
예제 #3
0
파일: views.py 프로젝트: wxxyd/iauto
 def post(self, request):
     form, error = JsonParser(Argument('id', type=int, required=False),
                              Argument('name', help='请输入环境名称'),
                              Argument('key', help='请输入唯一标识符'),
                              Argument('desc',
                                       required=False)).parse(request.body)
     if error is None:
         form.key = form.key.replace("'", '')
         env = Environment.objects.filter(key=form.key).first()
         if env and env.id != form.id:
             return json_response(error=f'唯一标识符 {form.key} 已存在,请更改后重试')
         if form.id:
             Environment.objects.filter(pk=form.id).update(**form)
         else:
             env = Environment.objects.create(created_by=request.user,
                                              **form)
             if request.user.role:
                 request.user.role.add_deploy_perm('envs', env.id)
     return json_response(error=error)
예제 #4
0
파일: views.py 프로젝트: wxxyd/iauto
def parse_text(request):
    form, error = JsonParser(
        Argument('o_id', type=int, help='缺少必要参数'),
        Argument('type',
                 filter=lambda x: x in dict(Config.TYPES),
                 help='缺少必要参数'), Argument('env_id', type=int, help='缺少必要参数'),
        Argument('data', handler=str.strip, help='缺少必要参数')).parse(request.body)
    if error is None:
        data = {}
        for line in form.pop('data').split('\n'):
            line = line.strip()
            if line:
                fields = line.split('=', 1)
                if len(fields) != 2 or fields[0].strip() == '':
                    return json_response(
                        error=f'解析配置{line!r}失败,确认其遵循 key = value 格式')
                data[fields[0].strip()] = fields[1].strip()
        _parse(request, form, data)
    return json_response(error=error)
예제 #5
0
 def delete(self, request):
     form, error = JsonParser(
         Argument('id', type=int, help='请指定操作对象')
     ).parse(request.GET)
     if error is None:
         group = Group.objects.filter(contacts__contains=f'\"{form.id}\"').first()
         if group:
             return json_response(error=f'报警联系组【{group.name}】包含此联系人,请解除关联后再尝试删除该联系人')
         Contact.objects.filter(pk=form.id).delete()
     return json_response(error=error)
예제 #6
0
 def delete(self, request):
     form, error = JsonParser(
         Argument('id', type=int, help='请指定操作对象')
     ).parse(request.GET)
     if error is None:
         detection = Detection.objects.filter(notify_grp__regex=fr'\D{form.id}\D').first()
         if detection:
             return json_response(error=f'监控任务【{detection.name}】正在使用该报警组,请解除关联后再尝试删除该联系组')
         Group.objects.filter(pk=form.id).delete()
     return json_response(error=error)
예제 #7
0
파일: views.py 프로젝트: staneyffer/spug
 def delete(self, request):
     form, error = JsonParser(Argument('id', type=int,
                                       help='请指定操作对象')).parse(request.GET)
     if error is None:
         task = Detection.objects.filter(pk=form.id).first()
         if task:
             if task.is_active:
                 return json_response(error='该监控项正在运行中,请先停止后再尝试删除')
             task.delete()
     return json_response(error=error)
예제 #8
0
 def post(self, request):
     form, error = JsonParser(
         Argument('id', type=int, required=False),
         Argument('deploy_id', type=int, help='缺少必要参数'),
         Argument('name', help='请输申请标题'),
         Argument('extra', type=list, help='缺少必要参数'),
         Argument('host_ids',
                  type=list,
                  filter=lambda x: len(x),
                  help='请选择要部署的主机'),
         Argument('desc', required=False),
     ).parse(request.body)
     req_id = None
     if error is None:
         deploy = Deploy.objects.filter(pk=form.deploy_id).first()
         if not deploy:
             return json_response(error='未找到该发布配置')
         if form.extra[0] == 'tag' and not form.extra[1]:
             return json_response(error='请选择要发布的Tag')
         if form.extra[0] == 'branch' and not form.extra[2]:
             return json_response(error='请选择要发布的分支及Commit ID')
         if deploy.extend == '2':
             if form.extra[0]:
                 form.extra[0] = form.extra[0].replace("'", '')
             if DeployExtend2.objects.filter(
                     deploy=deploy,
                     host_actions__contains='"src_mode": "1"').exists():
                 if len(form.extra) < 2:
                     return json_response(
                         error='该应用的发布配置中使用了数据传输动作且设置为发布时上传,请上传要传输的数据')
                 form.version = form.extra[1].get('path')
         form.name = form.name.replace("'", '')
         form.status = '0' if deploy.is_audit else '1'
         form.extra = json.dumps(form.extra)
         form.host_ids = json.dumps(form.host_ids)
         if form.id:
             req = DeployRequest.objects.get(pk=form.id)
             is_required_notify = deploy.is_audit and req.status == '-1'
             DeployRequest.objects.filter(pk=form.id).update(
                 created_by=request.user, reason=None, **form)
         else:
             req = DeployRequest.objects.create(created_by=request.user,
                                                **form)
             req_id = req.id
             is_required_notify = deploy.is_audit
         if is_required_notify:
             Thread(target=Helper.send_deploy_notify,
                    args=(req, 'approve_req')).start()
     if req_id is None:
         return json_response(error=error)
     else:
         return json_response({'id': req_id})
예제 #9
0
파일: views.py 프로젝트: zhbjerry/spug
 def patch(self, request):
     form, error = JsonParser(
         Argument('id', type=int, help='参数错误'),
         Argument('page_perms', type=dict, required=False),
         Argument('deploy_perms', type=dict, required=False),
         Argument('host_perms', type=list,
                  required=False)).parse(request.body)
     if error is None:
         role = Role.objects.filter(pk=form.pop('id')).first()
         if not role:
             return json_response(error='未找到指定角色')
         if form.page_perms is not None:
             role.page_perms = json.dumps(form.page_perms)
         if form.deploy_perms is not None:
             role.deploy_perms = json.dumps(form.deploy_perms)
         if form.host_perms is not None:
             role.host_perms = json.dumps(form.host_perms)
         role.user_set.update(token_expired=0)
         role.save()
     return json_response(error=error)
예제 #10
0
 def delete(self, request):
     form, error = JsonParser(Argument('id', type=int,
                                       help='请指定操作对象')).parse(request.GET)
     if error is None:
         user = User.objects.filter(pk=form.id).first()
         if user:
             user.role_id = None
             user.deleted_at = human_datetime()
             user.deleted_by = request.user
             user.save()
     return json_response(error=error)
예제 #11
0
 def delete(self, request):
     form, error = JsonParser(Argument('id', type=int,
                                       help='请指定操作对象')).parse(request.GET)
     if error is None:
         if Deploy.objects.filter(app_id=form.id).exists():
             return json_response(
                 error='该应用在应用发布中已存在关联的发布配置,请删除相关发布配置后再尝试删除')
         if Config.objects.filter(type='app', o_id=form.id).exists():
             return json_response(error='该应用在配置中心已存在关联的配置信息,请删除相关配置后再尝试删除')
         App.objects.filter(pk=form.id).delete()
     return json_response(error=error)
예제 #12
0
파일: navigation.py 프로젝트: jackerzz/spug
 def post(self, request):
     form, error = JsonParser(
         Argument('id', type=int, required=False),
         Argument('title', help='请输入导航标题'),
         Argument('desc', help='请输入导航描述'),
         Argument('logo', help='请上传导航logo'),
         Argument('links',
                  type=list,
                  filter=lambda x: len(x),
                  help='请设置导航链接'),
     ).parse(request.body)
     if error is None:
         form.links = json.dumps(form.links)
         if form.id:
             Navigation.objects.filter(pk=form.id).update(**form)
         else:
             nav = Navigation.objects.create(**form)
             nav.sort_id = nav.id
             nav.save()
     return json_response(error=error)
예제 #13
0
파일: views.py 프로젝트: zhbjerry/spug
 def post(self, request):
     form, error = JsonParser(Argument('id', type=int, required=False),
                              Argument('name', help='请输入角色名称'),
                              Argument('desc',
                                       required=False)).parse(request.body)
     if error is None:
         if form.id:
             Role.objects.filter(pk=form.id).update(**form)
         else:
             Role.objects.create(created_by=request.user, **form)
     return json_response(error=error)
예제 #14
0
 def delete(self, request):
     form, error = JsonParser(Argument('id', type=int, help='参数错误'),
                              Argument('file',
                                       help='请输入文件路径')).parse(request.GET)
     if error is None:
         host = Host.objects.get(pk=form.id)
         if not host:
             return json_response(error='未找到指定主机')
         cli = host.get_ssh()
         cli.remove_file(form.file)
     return json_response(error=error)
예제 #15
0
 def post(self, request):
     form, error = JsonParser(Argument('id', type=int, required=False),
                              Argument('name', help='请输入服务名称'),
                              Argument('key', help='请输入唯一标识符'),
                              Argument('desc',
                                       required=False)).parse(request.body)
     if error is None:
         form.name = form.name.replace("'", '')
         app = App.objects.filter(key=form.key).first()
         if app and app.id != form.id:
             return json_response(error=f'唯一标识符 {form.key} 已存在,请更改后重试')
         if form.id:
             App.objects.filter(pk=form.id).update(**form)
         else:
             app = App.objects.create(created_by=request.user, **form)
             app.sort_id = app.id
             app.save()
             if request.user.role:
                 request.user.role.add_deploy_perm('apps', app.id)
     return json_response(error=error)
예제 #16
0
파일: views.py 프로젝트: staneyffer/spug
 def delete(self, request):
     form, error = JsonParser(
         Argument('host_list', type=list, help='请输入主机列表'),
     ).parse(request.GET)
     if error is None and len(form.host_list) > 0:
         for host in form.host_list:
             Host.objects.filter(pk=host.get('id')).update(
                 deleted_at=human_datetime(),
                 deleted_by=request.user,
             )
     return json_response(error=error)
예제 #17
0
파일: views.py 프로젝트: staneyffer/spug
    def post(self, request):
        form, error = JsonParser(
            Argument('id', type=int, required=False),
            Argument('zone', help='请输入主机类型'),
            Argument('name', help='请输主机名称'),
            Argument('username', help='请输入登录用户名'),
            Argument('hostname', help='请输入主机名或IP'),
            Argument('port', type=int, help='请输入SSH端口'),
            Argument('desc', required=False),
            Argument('password', required=False),
        ).parse(request.body)
        if error is None:
            if valid_ssh(form.hostname, form.port, form.username, form.pop('password')) is False:
                return json_response('auth fail')

            if form.id:
                Host.objects.filter(pk=form.pop('id')).update(**form)
            else:
                form.created_by = request.user
                Host.objects.create(**form)
        return json_response(error=error)
예제 #18
0
def get_requests(request):
    form, error = JsonParser(
        Argument('repository_id', type=int, help='参数错误')
    ).parse(request.GET)
    if error is None:
        requests = []
        for item in DeployRequest.objects.filter(repository_id=form.repository_id):
            data = item.to_dict(selects=('id', 'name', 'created_at'))
            data['host_ids'] = json.loads(item.host_ids)
            data['status_alias'] = item.get_status_display()
            requests.append(data)
        return json_response(requests)
예제 #19
0
 def delete(self, request):
     form, error = JsonParser(
         Argument('id', type=int, help='请指定操作对象')
     ).parse(request.GET)
     if error is None:
         repository = Repository.objects.filter(pk=form.id).first()
         if not repository:
             return json_response(error='未找到指定构建记录')
         if repository.deployrequest_set.exists():
             return json_response(error='已关联发布申请无法删除')
         repository.delete()
     return json_response(error=error)
예제 #20
0
 def post(self, request):
     form, error = JsonParser(
         Argument('deploy_id', type=int, help='参数错误'),
         Argument('version', help='请输入构建版本'),
         Argument('extra', type=list, help='参数错误'),
         Argument('remarks', required=False)
     ).parse(request.body)
     if error is None:
         deploy = Deploy.objects.filter(pk=form.deploy_id).first()
         if not deploy:
             return json_response(error='未找到指定发布配置')
         form.extra = json.dumps(form.extra)
         form.spug_version = Repository.make_spug_version(deploy.id)
         rep = Repository.objects.create(
             app_id=deploy.app_id,
             env_id=deploy.env_id,
             created_by=request.user,
             **form)
         Thread(target=dispatch, args=(rep,)).start()
         return json_response(rep.to_view())
     return json_response(error=error)
예제 #21
0
 def get(self, request):
     form, error = JsonParser(Argument('id', type=int, help='参数错误'),
                              Argument('path',
                                       help='参数错误')).parse(request.GET)
     if error is None:
         host = Host.objects.get(pk=form.id)
         if not host:
             return json_response(error='未找到指定主机')
         cli = host.get_ssh()
         objects = cli.list_dir_attr(form.path)
         return json_response([parse_sftp_attr(x) for x in objects])
     return json_response(error=error)
예제 #22
0
파일: views.py 프로젝트: jackerzz/spug
 def delete(self, request):
     form, error = JsonParser(
         Argument('id', type=int, help='请指定操作对象')
     ).parse(request.GET)
     if error is None:
         deploy = Deploy.objects.get(pk=form.id)
         if deploy.deployrequest_set.exists():
             return json_response(error='已存在关联的发布记录,请删除关联的发布记录后再尝试删除发布配置')
         for item in deploy.repository_set.all():
             item.delete()
         deploy.delete()
     return json_response(error=error)
예제 #23
0
 def patch(self, request):
     form, error = JsonParser(
         Argument('id', type=int, required=False),
         Argument('zone', help='请输入主机类别')
     ).parse(request.body)
     if error is None:
         host = Host.objects.filter(pk=form.id).first()
         if not host:
             return json_response(error='未找到指定主机')
         count = Host.objects.filter(zone=host.zone, deleted_by_id__isnull=True).update(zone=form.zone)
         return json_response(count)
     return json_response(error=error)
예제 #24
0
파일: views.py 프로젝트: zhbjerry/spug
 def patch(self, request):
     form, error = JsonParser(
         Argument('old_password', required=False),
         Argument('new_password', required=False),
         Argument('nickname', required=False),
     ).parse(request.body, True)
     if error is None:
         if form.get('old_password') and form.get('new_password'):
             if len(form.new_password) < 6:
                 return json_response(error='请设置至少6位的新密码')
             if request.user.verify_password(form.old_password):
                 request.user.password_hash = User.make_password(
                     form.new_password)
                 request.user.token_expired = 0
                 request.user.save()
             else:
                 return json_response(error='原密码错误,请重新输入')
         if form.get('nickname'):
             request.user.nickname = form.nickname
             request.user.save()
     return json_response(error=error)
예제 #25
0
파일: views.py 프로젝트: zzti/spug
 def delete(self, request):
     form, error = JsonParser(Argument('id', type=int,
                                       help='请指定操作对象')).parse(request.GET)
     if error is None:
         user = User.objects.filter(pk=form.id).first()
         if user:
             if user.type == 'ldap':
                 return json_response(error='ldap账户无法删除,请使用禁用功能来禁止该账户访问系统')
             user.deleted_at = human_datetime()
             user.deleted_by = request.user
             user.save()
     return json_response(error=error)
예제 #26
0
 def post(self, request):
     form, error = JsonParser(
         Argument('o_id', type=int, help='缺少必要参数'),
         Argument('type',
                  filter=lambda x: x in dict(Config.TYPES),
                  help='缺少必要参数'),
         Argument('envs', type=list, filter=lambda x: len(x), help='请选择环境'),
         Argument('key', help='请输入Key'),
         Argument('is_public', type=bool, help='缺少必要参数'),
         Argument('value', type=str, default=''),
         Argument('desc', required=False)).parse(request.body)
     if error is None:
         form.value = form.value.strip()
         form.updated_at = human_datetime()
         form.updated_by = request.user
         envs = form.pop('envs')
         for env_id in envs:
             cf = Config.objects.filter(o_id=form.o_id,
                                        type=form.type,
                                        env_id=env_id,
                                        key=form.key).first()
             if cf:
                 raise Exception(f'{cf.env.name} 中已存在该Key')
             Config.objects.create(env_id=env_id, **form)
             ConfigHistory.objects.create(action='1', env_id=env_id, **form)
     return json_response(error=error)
예제 #27
0
파일: views.py 프로젝트: jackerzz/spug
def post_request_ext2(request):
    form, error = JsonParser(
        Argument('id', type=int, required=False),
        Argument('deploy_id', type=int, help='缺少必要参数'),
        Argument('name', help='请输申请标题'),
        Argument('host_ids',
                 type=list,
                 filter=lambda x: len(x),
                 help='请选择要部署的主机'),
        Argument('extra', type=dict, required=False),
        Argument('version', default=''),
        Argument('type', default='1'),
        Argument('plan', required=False),
        Argument('desc', required=False),
    ).parse(request.body)
    if error is None:
        deploy = Deploy.objects.filter(pk=form.deploy_id).first()
        if not deploy:
            return json_response(error='未找到该发布配置')
        extra = form.pop('extra')
        if DeployExtend2.objects.filter(
                deploy=deploy,
                host_actions__contains='"src_mode": "1"').exists():
            if not extra:
                return json_response(
                    error='该应用的发布配置中使用了数据传输动作且设置为发布时上传,请上传要传输的数据')
            form.spug_version = extra['path']
            form.extra = json.dumps(extra)
        else:
            form.spug_version = Repository.make_spug_version(deploy.id)
        form.name = form.name.replace("'", '')
        form.status = '0' if deploy.is_audit else '1'
        form.host_ids = json.dumps(form.host_ids)
        if form.id:
            req = DeployRequest.objects.get(pk=form.id)
            is_required_notify = deploy.is_audit and req.status == '-1'
            DeployRequest.objects.filter(pk=form.id).update(
                created_by=request.user, reason=None, **form)
        else:
            req = DeployRequest.objects.create(created_by=request.user, **form)
            is_required_notify = deploy.is_audit
        if is_required_notify:
            Thread(target=Helper.send_deploy_notify,
                   args=(req, 'approve_req')).start()
    return json_response(error=error)
예제 #28
0
파일: add.py 프로젝트: jackerzz/spug
def cloud_import(request):
    form, error = JsonParser(
        Argument('type', filter=lambda x: x in ('ali', 'tencent'),
                 help='参数错误'),
        Argument('ak', help='请输入AccessKey ID'),
        Argument('ac', help='请输入AccessKey Secret'),
        Argument('region_id', help='请选择区域'),
        Argument('group_id', type=int, help='请选择分组'),
        Argument('username', help='请输入默认SSH用户名'),
        Argument('port', type=int, help='请输入默认SSH端口号'),
        Argument('host_type',
                 filter=lambda x: x in ('public', 'private'),
                 help='请选择连接地址'),
    ).parse(request.body)
    if error is None:
        group = Group.objects.filter(pk=form.group_id).first()
        if not group:
            return json_response(error='未找到指定分组')
        if form.type == 'ali':
            instances = utils.fetch_ali_instances(form.ak, form.ac,
                                                  form.region_id)
        else:
            instances = utils.fetch_tencent_instances(form.ak, form.ac,
                                                      form.region_id)

        host_add_ids = []
        for item in instances:
            instance_id = item['instance_id']
            host_name = item.pop('instance_name')
            public_ips = item['public_ip_address'] or []
            private_ips = item['private_ip_address'] or []
            item['public_ip_address'] = json.dumps(public_ips)
            item['private_ip_address'] = json.dumps(private_ips)
            if HostExtend.objects.filter(instance_id=instance_id).exists():
                HostExtend.objects.filter(instance_id=instance_id).update(
                    **item)
            else:
                if form.host_type == 'public':
                    hostname = public_ips[0] if public_ips else ''
                else:
                    hostname = private_ips[0] if private_ips else ''
                host = Host.objects.create(name=host_name,
                                           hostname=hostname,
                                           port=form.port,
                                           username=form.username,
                                           created_by=request.user)
                HostExtend.objects.create(host=host, **item)
                host_add_ids.append(host.id)
        if host_add_ids:
            group.hosts.add(*host_add_ids)
        return json_response(len(instances))
    return json_response(error=error)
예제 #29
0
파일: views.py 프로젝트: jackerzz/spug
 def delete(self, request):
     form, error = JsonParser(Argument('id', type=int, help='参数错误'),
                              Argument('file',
                                       help='请输入文件路径')).parse(request.GET)
     if error is None:
         if not has_host_perm(request.user, form.id):
             return json_response(error='无权访问主机,请联系管理员')
         host = Host.objects.get(pk=form.id)
         if not host:
             return json_response(error='未找到指定主机')
         with host.get_ssh() as ssh:
             ssh.remove_file(form.file)
     return json_response(error=error)
예제 #30
0
파일: views.py 프로젝트: jackerzz/spug
 def get(self, request):
     form, error = JsonParser(Argument('id', type=int, help='参数错误'),
                              Argument('path',
                                       help='参数错误')).parse(request.GET)
     if error is None:
         if not has_host_perm(request.user, form.id):
             return json_response(error='无权访问主机,请联系管理员')
         host = Host.objects.get(pk=form.id)
         if not host:
             return json_response(error='未找到指定主机')
         objects = fetch_dir_list(host, form.path)
         return json_response(objects)
     return json_response(error=error)