Ejemplo n.º 1
0
def refresh_repo_list(access_token, git_name, uid, git_uid):
    """ 刷新 github 代码列表 """
    from authServer.common.oauthclient.HubApi import GetGithubRepoList
    retbool, ret_json = GetGithubRepoList(token=access_token,
                                          username=git_name)
    if retbool is False:
        return ret_json

    repo_list = list()
    code_repos = list()

    old_repo_name_d = dict()
    old_githubrepo = g.db_session.query(CodeRepo).filter(
        CodeRepo.uid == str(uid)).all()
    if old_githubrepo is not None:
        for node_repo in old_githubrepo:
            old_repo_name_d[node_repo.repo_id] = node_repo.repo_id

    # print "------------> 01"
    # print ret_json
    # print "------------> 02"
    for node in ret_json:

        repo_id = str(node['id'])
        repo_name = node['name']
        if repo_id in old_repo_name_d:
            g.db_session.query(CodeRepo).filter(
                CodeRepo.uid == uid, CodeRepo.repo_id == repo_id).update({
                    'repo_name':
                    repo_name,
                    'html_url':
                    node['html_url'],
                    'ssh_url':
                    node['ssh_url'],
                    'url':
                    node['url'],
                    'description':
                    node['description'],
                })
        else:
            code_repos.append(
                CodeRepo(uid=uid,
                         repo_uid=git_uid,
                         repo_id=repo_id,
                         repo_name=repo_name,
                         update_time=get_now_time(),
                         creation_time=get_now_time(),
                         src_type='github',
                         html_url=node['html_url'],
                         ssh_url=node['ssh_url'],
                         url=node['url'],
                         description=node['description']))

    g.db_session.add_all(code_repos)
    g.db_session.commit()
    ret = request_result(0, ret=repo_list)
    return ret
    def siggup_init(self, user_name, email, password):  # 注册初始化
        self.user_name = user_name
        self.email = email
        self.salt = self.get_salt()
        self.save_password = self.get_save_password(password, self.salt)

        print "------000---"
        print self.user_name
        print self.salt
        print self.save_password

        self.uuid = uuid.uuid3(uuid.NAMESPACE_DNS, self.user_name).__str__()
        now_time = get_now_time()

        try:
            user_base = UserBase(user_id=self.uuid,
                                 username=self.user_name,
                                 email=self.email,
                                 password=self.save_password,
                                 creation_time=now_time,
                                 update_time=now_time,
                                 salt=self.salt)
            g.db_session.add(user_base)
            g.db_session.commit()
        except Exception as msg:
            return request_result(401, ret=msg.message)

        from authServer.v1.usercenter.passwordFind import SendFindEmail
        from authServer.conf.conf import callback_url

        if CONFIRM_EMAIL:
            print 'callback_url'
            print callback_url

            sendret = SendFindEmail(self.user_name,
                                    action='ConfirmEmail',
                                    callback_url=callback_url,
                                    email_template='email_confirm.html')

            print 'sendret'
            print sendret

        try:
            ret = make_orgs(uid=self.uuid,
                            user_name=self.user_name,
                            org_name=self.user_name)

            if 'status' in ret and ret['status'] == 0:
                return ret
        except Exception as msg:

            print 'error'
            print msg.message
            user_b = UserBase(user_id=self.uuid)
            g.db_session.delete(user_b)
            g.db_session.commit()

            return request_result(401, ret=msg.message)
Ejemplo n.º 3
0
def init_data(user_name, email, password, role):
    import uuid
    import authServer.pyTools.token.token as TK
    from authServer.pyTools.tools.timeControl import get_now_time

    try:

        salt = TK.GenerateRandomString(randlen=32)
        save_password = TK.encrypy_pbkdf2(password=password, salt=salt)
        uuid = uuid.uuid3(uuid.NAMESPACE_DNS, user_name).__str__()

        now_time = get_now_time()

        db_session = Session()



        user_base = UserBase(user_id=uuid, username=user_name, email=email,
                             password=save_password, creation_time=now_time,
                             update_time=now_time, salt=salt)
        db_session.add(user_base)
        db_session.commit()
    except Exception as msg:
        print msg.message
        return False


    try:
        # 默认组织名和用户名一样
        org_base = OrgsBase(org_id=uuid, org_name=user_name, creation_time=now_time)
        db_session.add(org_base)
        db_session.commit()
    except Exception as msg:
        user_b = UserBase(user_id=uuid)
        db_session.delete(user_b)
        db_session.commit()
        db_session.close()
        return False

    try:
        org_user = OrgsUser(org_id=uuid, uid=uuid, username=user_name,
                            role=role, creation_time=now_time, update_time=now_time)
        db_session.add(org_user)
        db_session.commit()
        return True
    except Exception as msg:
        org_base = OrgsBase(org_id=uuid)
        db_session.delete(org_base)
        db_session.commit()

        user_b = UserBase(user_id=uuid)
        db_session.delete(user_b)
        db_session.commit()
        db_session.close()
        return False
Ejemplo n.º 4
0
    def create_org_user(kwargs):

        # 不是管理员 或  要操作的组织和token不符
        print kwargs['role_uuid']
        print type(kwargs['role_uuid'])
        print OrgaRole.OrgaMaster.value
        print OrgaRole.OrgaDevelop.value

        if kwargs['role_uuid'] != str(
                OrgaRole.OrgaMaster.value) and kwargs['role_uuid'] != str(
                    OrgaRole.OrgaDevelop.value):
            return request_result(806)

        try:
            now_time = get_now_time()

            # 已经是组织成员
            orgs_u = g.db_session.query(OrgsUser).filter(
                OrgsUser.org_id == kwargs['orga_uuid'],
                OrgsUser.uid == kwargs['user_uuid_arg']).first()

            # print "----create_org_user----"

            if orgs_u is not None:
                # print "----create_org_user---- orgs_u is not None"
                # print orgs_u.is_delete
                # print type(orgs_u.is_delete)
                if str(orgs_u.is_delete) == '0':  # 已经是组织成员
                    return request_result(807, ret=kwargs['user_name'])
                elif str(orgs_u.is_delete) == '1':  # 删除的用户,冲洗标记
                    g.db_session.query(OrgsUser).filter(
                        OrgsUser.org_id == kwargs['orga_uuid'],
                        OrgsUser.uid == kwargs['user_uuid_arg']).update({
                            "is_delete":
                            '0',
                            "update_time":
                            now_time,
                            "role":
                            OrgaRole.OrgaDevelop.value
                        })

                    g.db_session.commit()
                    return request_result(0)

            org_user = OrgsUser(org_id=kwargs['orga_uuid'],
                                uid=kwargs['user_uuid_arg'],
                                role=OrgaRole.OrgaDevelop.value,
                                creation_time=now_time,
                                update_time=now_time)
            g.db_session.add(org_user)
            g.db_session.commit()
            return request_result(0)
        except Exception as msg:
            return request_result(401, ret=msg.message)
Ejemplo n.º 5
0
def leave_orga(kwargs):  # 离开组织
    try:
        orgs_user = g.db_session.query(OrgsUser).filter(
            OrgsUser.org_id == kwargs['orga_uuid_arg'], OrgsUser.uid == kwargs['uid'],
            OrgsUser.is_delete == '0').first()

        if orgs_user is None:
            return request_result(806)

        if orgs_user.role == str(OrgaRole.OrgaMaster.value):  # 群主不能离开组织
            return request_result(810)

        g.db_session.query(OrgsUser).filter(
            OrgsUser.org_id == kwargs['orga_uuid_arg'], OrgsUser.uid == kwargs['uid']).update(
            {"is_delete": '1', "update_time": get_now_time()})
        g.db_session.commit()

        return get_login_token(kwargs['user_name'])  # 离开组织,给用户返回token
        # return request_result(0, ret=retdict)
    except Exception as msg:
        return request_result(403, ret=msg.message)
Ejemplo n.º 6
0
    def delete_org_user(kwargs):

        #
        orgs_u = g.db_session.query(OrgsUser).filter(
            OrgsUser.org_id == kwargs['orga_uuid_arg'],
            OrgsUser.uid == kwargs['user_uuid_arg'],
            OrgsUser.is_delete == '0').first()

        if orgs_u is None:
            return request_result(808)

        # 不是管理员
        if int(kwargs['role_uuid']) < int(orgs_u.role) is False:  # 权限超出自己权限范围
            return request_result(806)

        if kwargs['orga_uuid_arg'] != kwargs['orga_uuid']:  # 要操作的组织和token不符
            return request_result(806)

        try:
            now = get_now_time()
            g.db_session.query(OrgsUser).filter(
                OrgsUser.org_id == kwargs['orga_uuid'],
                OrgsUser.uid == kwargs['user_uuid_arg']).update({
                    "is_delete":
                    '1',
                    "update_time":
                    now
                })
            g.db_session.commit()

            return request_result(0)
        except Exception as msg:
            print msg.message
            print msg.args

            for va in msg.args:
                print va
            return request_result(403)
Ejemplo n.º 7
0
def make_orgs(uid, user_name, org_name):
    """ 组织数据表中添加数据"""
    org_id = uuid.uuid3(uuid.NAMESPACE_DNS, org_name).__str__()
    now_time = get_now_time()
    try:

        org_base = OrgsBase(org_id=org_id, org_name=org_name, creation_time=now_time)
        g.db_session.add(org_base)
        g.db_session.commit()
    except Exception as msg:
        return request_result(401, ret=msg.message)

    try:
        org_user = OrgsUser(org_id=org_id, uid=uid, username=user_name,
                            role=OrgaRole.OrgaMaster.value,
                            creation_time=now_time, update_time=now_time)
        g.db_session.add(org_user)
        g.db_session.commit()
        return request_result(0, ret=org_id)
    except Exception as msg:
        org_base = OrgsBase(org_id=org_id)
        g.db_session.delete(org_base)
        g.db_session.commit()
        return request_result(401, ret=msg.message)
Ejemplo n.º 8
0
def refresh_repos(access_token, git_name, uid, git_uid, src_type):
    """ 刷新 github 代码列表 """

    if src_type == 'github':
        from authServer.common.oauthclient.HubApi import GetGithubRepoList, GetGithubALLRepoList
        # # modify liuzhangpei 20170121
        # retbool, ret_json = GetGithubRepoList(token=access_token, username=git_name)
        retbool, ret_json = GetGithubALLRepoList(token=access_token,
                                                 username=git_name)
        if retbool is False:
            return ret_json
    elif src_type == 'coding':
        from authServer.common.oauthclient.CodingApi import GetRepoList
        code, ret_json = GetRepoList(access_token=access_token)
        if code is False:
            return ret_json
        ret_json = ret_json['data']['list']  # coding 和 github 结构不一样

    repo_list = list()
    code_repos = list()

    old_repo_name_d = dict()
    old_githubrepo = g.db_session.query(CodeRepo).filter(
        CodeRepo.uid == str(uid), CodeRepo.src_type == src_type).all()
    if old_githubrepo is not None:
        for node_repo in old_githubrepo:
            old_repo_name_d[node_repo.repo_id] = node_repo.repo_id

    # print "------------> 01"
    # print ret_json
    # print "------------> 02"

    # 全部标记已经删除
    g.db_session.query(CodeRepo).filter(CodeRepo.uid == uid,
                                        CodeRepo.src_type == src_type).update({
                                            'deleted':
                                            '1',
                                        })

    g.db_session.commit()

    # ret_json = ret_json['data']['list']  # coding 和 github 结构不一样
    for node in ret_json:

        # github == coding
        repo_id = str(node['id'])
        description = node['description']
        repo_name = node['name']
        ssh_url = node['ssh_url']
        git_url = node['git_url']

        if src_type == 'github':
            html_url = node['html_url']
        elif src_type == 'coding':
            html_url = node['https_url']

        if repo_id in old_repo_name_d:
            g.db_session.query(CodeRepo).filter(
                CodeRepo.uid == uid, CodeRepo.repo_id == repo_id,
                CodeRepo.src_type == src_type).update({
                    'repo_name': repo_name,
                    'html_url': html_url,
                    'ssh_url': ssh_url,
                    'url': git_url,
                    'description': description,
                    'deleted': '0',
                })
        else:
            code_repos.append(
                CodeRepo(uid=uid,
                         repo_uid=git_uid,
                         repo_id=repo_id,
                         repo_name=repo_name,
                         update_time=get_now_time(),
                         creation_time=get_now_time(),
                         src_type=src_type,
                         html_url=html_url,
                         ssh_url=ssh_url,
                         url=git_url,
                         description=description,
                         deleted='0'))

    g.db_session.add_all(code_repos)
    g.db_session.commit()

    # 真正删除
    try:
        del_repos = g.db_session.query(CodeRepo).filter(
            CodeRepo.uid == str(uid), CodeRepo.src_type == src_type,
            CodeRepo.deleted == '1').all()

        for del_repo in del_repos:
            g.db_session.delete(del_repo)
            g.db_session.commit()
        print "del is ok"
    except Exception as msg:
        print "del is error"
        print msg.message
        return request_result(100, ret=msg.message)

    return DbGetRepoList(access_token=access_token,
                         git_name=git_name,
                         uid=uid,
                         git_uid=git_uid,
                         src_type=src_type)
Ejemplo n.º 9
0
 def set_creation_time(self):
     self.creation_time = get_now_time()
Ejemplo n.º 10
0
 def set_update_time(self):
     self.update_time = get_now_time()
Ejemplo n.º 11
0
def child(kwargs, pipe_name):

    db = hub_db()
    DBOP = mysqlOperation(host=db.host,
                          user=db.user,
                          passwd=db.pawd,
                          port=db.port,
                          db=db.cydb,
                          debug=True)

    # 开始时间
    begin_time = time.time()

    docker_file = kwargs['dockerfile_name']
    image_name = kwargs['images_name']
    user_name = kwargs['user_name']
    image_tag = kwargs['image_tag']
    github_repo_id = kwargs['id']

    # 更新状态
    # # 构建状态 1:构建成功, 2:构建构建中,  3:构建失败
    args_d = dict()
    args_d['build_pro_id'] = github_repo_id
    args_d['build_status'] = '2'
    args_d['last_build'] = get_now_time()

    print 'last_build'
    print args_d['last_build']

    DBOP.InsertOrUpdateBySql(update_status_last_build, args_d)

    # 日志索引
    labels_logs = 'auto_build-' + str(user_name) + '-' + str(github_repo_id)

    repo_path = sys.path[0] + '/' + kwargs['git_name'] + '-' + random_str(8) + '/' + kwargs['repo_name']\

    # clone 代码地址
    repo_url = '[email protected]:' + kwargs['git_name'] + '/' + kwargs[
        'repo_name'] + '.git'

    if os.path.exists(path=repo_path):
        shutil.rmtree(repo_path)

    git_cmd = 'git clone ' + repo_url + ' ' + repo_path

    try:
        pipeout = os.open(pipe_name, os.O_WRONLY)
        os.write(pipeout, '--> git clone begin: ' + git_cmd + '\n')

        ps = subprocess.Popen(git_cmd,
                              shell=True,
                              bufsize=0,
                              stdin=subprocess.PIPE,
                              stdout=pipeout,
                              stderr=pipeout)
        retcode = ps.wait()
        if str(retcode) == '0':
            os.write(pipeout, '--> git clone is ok' + '\n')
    except Exception as msg:
        print msg.message
        print msg.args

        os.write(pipeout, '--> git clone is error ' + '\n')
        args_d['build_status'] = '3'
        args_d['use_time'] = str(time.time() - begin_time)
        DBOP.InsertOrUpdateBySql(update_status_last_build_use_time, args_d)
        return True

    docker_path = repo_path + kwargs['dockerfile_path']

    if os.path.exists(docker_path) is False:
        os.write(pipeout,
                 'error: --> git clone scr file is not exists ' + '\n')
        return False

    try:
        # imagename_demo = 'index.boxlinker.com/{0}/{1}:{2}'
        # imagename = imagename_demo.format(user_name, image_name, image_tag)
        # 20161009 修改镜像命名逻辑
        imagename_demo = 'index.boxlinker.com/{0}:{1}'
        imagename = imagename_demo.format(image_name, image_tag)

        docker_build_cmd_demo = 'docker build -t {0} {1}'
        # docker_build_cmd_demo = 'docker build --no-cache -t {0} {1}'
        docker_build_cmd = docker_build_cmd_demo.format(imagename, docker_path)

        os.write(pipeout, '--> docker build begin: ' + str(imagename) + '\n')
        os.write(pipeout,
                 '--> docker build cmd  : ' + str(docker_build_cmd) + '\n')

        ps = subprocess.Popen(docker_build_cmd,
                              shell=True,
                              bufsize=0,
                              stdin=subprocess.PIPE,
                              stdout=pipeout,
                              stderr=pipeout)
        retcode = ps.wait()

        if str(retcode) == '0':
            os.write(pipeout, '--> docker build is ok: ' + '\n')
    except Exception as msg:
        print msg.message
        print msg.args
        os.write(pipeout, '--> docker build is error: ' + '\n')

        args_d['build_status'] = '3'
        args_d['use_time'] = str(time.time() - begin_time)
        DBOP.InsertOrUpdateBySql(update_status_last_build_use_time, args_d)
        return True

    try:
        image_push = 'docker push {0}'.format(imagename)
        os.write(pipeout, '--> push image begin: ' + image_push + '\n')
        ps = subprocess.Popen(image_push,
                              shell=True,
                              bufsize=0,
                              stdin=subprocess.PIPE,
                              stdout=pipeout,
                              stderr=pipeout)
        retcode = ps.wait()
        if str(retcode) == '0':
            os.write(pipeout, 'docker push is ok: ' + image_push + '\n')
            os.write(
                pipeout,
                ' ---- An automated build all three steps completed successfully ---- '
                + '\n')

        args_d['build_status'] = '1'
    except Exception as msg:
        print msg.message
        print msg.args

        os.write(pipeout, 'docker push is error: ' + image_push + '\n')
        args_d['build_status'] = '3'
    finally:
        # 更新状态
        # 构建状态 1:构建成功, 2:构建构建中,  3:构建失败

        args_d['use_time'] = str(time.time() - begin_time)
        DBOP.InsertOrUpdateBySql(update_status_last_build_use_time, args_d)