def get_all_projects(self, access_token):
     """获取所有项目信息"""
     projects = paas_cc.get_projects(access_token)
     if projects.get('code') != 0:
         self.stdout.write(projects.get('message'))
         return
     return projects.get('data') or []
Beispiel #2
0
    def handle(self, *args, **options):
        """用到了 paas_cc 的两个API,必须要带用户登录态的accesstoken才可以
        """
        print(options)
        if options.get('access_token'):
            access_token = options['access_token']
        else:
            return False
        # 获取所有的项目信息
        pro_res = paas_cc.get_projects(access_token)
        pro_data = pro_res.get('data') or []

        init_ns = 0
        has_init = 0
        error_list = []
        for _d in pro_data:
            # 只需要处理 mesos 项目
            _kind = _d.get('kind')
            if _kind != 2:
                continue

            project_id = _d.get('project_id')
            project_code = _d.get('english_name')
            self.stdout.write(self.style.SUCCESS("init project:%s" % project_code))
            # 查询项目下的所有命名空间
            ns_result = paas_cc.get_namespace_list(access_token, project_id, limit=constants.ALL_LIMIT)
            ns_res = ns_result.get('data') or {}
            ns_data = ns_res.get('results') or []

            for _ns in ns_data:
                has_image_secret = _ns.get('has_image_secret')
                # 已经初始化过的则不再初始化
                if has_image_secret:
                    has_init = has_init + 1
                    continue

                cluster_id = _ns.get('cluster_id')
                ns_name = _ns.get('name')
                namespace_id = _ns.get('id')
                ns_base = NamespaceBase()
                try:
                    ns_base.init_mesos_ns_by_bcs(access_token, project_id, project_code, cluster_id, ns_name)
                except Exception:
                    error_list.append('%s[%s]' % (ns_name, namespace_id))
                    self.stdout.write(self.style.WARNING("error init image secret in ns: %s[%s]" % (
                                      ns_name, namespace_id)))
                else:
                    # 更新
                    paas_cc.update_namespace(access_token, project_id, namespace_id, has_image_secret=True)
                    self.stdout.write(self.style.SUCCESS("init image secret in ns: %s[%s]" % (ns_name, namespace_id)))
                    init_ns = init_ns + 1
        self.stdout.write(self.style.WARNING("error_list:%s" % ','.join(error_list)))
        self.stdout.write(self.style.SUCCESS("init end, init_ns:%s, has_init:%s, error:%s" % (init_ns, has_init,
                          len(error_list))))
Beispiel #3
0
def query_projects(access_token, query_params=None):
    return paas_cc.get_projects(access_token, query_params)