def test(): api_version = api_versions.get_api_version( DEFAULT_MAJOR_OS_COMPUTE_API_VERSION) admin = 'admin' password = '******' project = 1 base_url = 'http://10.108.211.22:8000' harbor = client.Client(api_version, admin, password, project, base_url) repositories = harbor.repositories.list(project) data = [] for repo in repositories: tags = harbor.repositories.list_tags(repo['name']) for tag in tags: item = repo.copy() manifest = harbor.repositories.get_manifests( item['name'], tag['name']) size = 0 for layer in manifest['manifest']['layers']: size += layer['size'] item['size'] = size if tag['name'] != 'latest': item['name'] = repo['name'] + ":" + tag['name'] data.append(item) fields = [ "name", 'project_id', 'size', "tags_count", "star_count", "pull_count", "update_time" ] utils.print_list(data, fields)
def do_policy_list(cs, args): """List filters policies by name and project_id.""" target = None if is_id(args.target): target = args.target else: targets = cs.targets.list() for t in targets: if t['name'] == args.target: target = t['id'] break if not target: print("target '%s' not found!" % args.target) return 1 try: policies = cs.targets.list_policies(target) except exp.NotFound: print("target '%s' not found!" % args.target) return 1 if not policies: policies = [] fields = [ "id", "name", "description", "enabled", "start_time", "cron_str", "creation_time" ] utils.print_list(policies, fields, sortby='id')
def do_tags_list(cs, args): """Get tags of a relevant repository.""" tags = cs.repositories.list_tags(args.repository) fields = [ "name", 'author', 'architecture', 'os', 'docker_version', 'created' ] utils.print_list(tags, fields, sortby="name")
def do_target_list(cs, args): """List filters targets.""" targets = cs.targets.list() fields = [ 'id', 'name', 'endpoint', 'username', 'password', 'creation_time' ] utils.print_list(targets, fields)
def do_list(cs, args): """Get repositories accompany with relevant project and repo name.""" project_id = args.project_id if not project_id: project_id = cs.client.project repositories = cs.repositories.list(project_id) data = [] for repo in repositories: tags = cs.repositories.list_tags(repo['name']) for tag in tags: item = repo.copy() manifest = cs.repositories.get_manifests(item['name'], tag['name']) size = 0 for layer in manifest['manifest']['layers']: size += layer['size'] item['size'] = size if tag['name'] != 'latest': item['name'] = repo['name'] + ":" + tag['name'] data.append(item) fields = [ "name", 'project_id', 'size', "tags_count", "star_count", "pull_count", "update_time" ] utils.print_list(data, fields, sortby=args.sortby)
def do_job_list(cs, args): """List filters jobs according to the policy and repository.""" jobs = cs.jobs.list(args.policy_id) for job in jobs: if job['tags']: job['name'] += ":" + job['tags'] fields = ['id', 'repository', 'operation', 'status', 'update_time'] utils.print_list(jobs, fields, sortby='id')
def _dump_timings(self, timings): results = [{ "url": url, "seconds": end - start } for url, start, end in timings] total = 0.0 for tyme in results: total += tyme['seconds'] results.append({"url": "Total", "seconds": total}) utils.print_list(results, ["url", "seconds"], align='l') print("Total: %s seconds" % total)
def do_top(cs, args): """Get public repositories which are accessed most.""" try: count = int(args.count) except ValueError: print("'%s' is not a valid number." % args.count) return 1 if count < 1: print("invalid count %s, count must > 0." % args.count) return 1 data = cs.repositories.get_top(count) utils.print_list(data, ['name', 'pull_count', 'star_count'], sortby='pull_count')
def do_member_list(cs, args): """List a project's relevant role members.""" project = args.project_id if not project: project = cs.client.project members = cs.projects.get_members(project) fields = [ 'username', 'role_name', 'user_id', 'role_id', ] utils.print_list(members, fields, formatters={}, sortby='user_id')
def do_project_list(cs, args): """List projects.""" projects = cs.projects.list() fields = [ 'project_id', 'name', 'owner_id', 'current_user_role_id', 'repo_count', 'creation_time', 'public', ] utils.print_list(projects, fields, formatters={}, sortby=args.sortby)
def do_get_conf(cs, args): """Get system configurations.""" try: configurations = cs.configurations.get() except exp.Forbidden: raise exp.CommandError("Only admin can perform this operation.") data = [] for key in configurations: item = {} item['name'] = key item['value'] = configurations[key]['value'] item['editable'] = configurations[key]['editable'] data.append(item) utils.print_list(data, ['name', 'value', 'editable'], sortby='name')
def do_user_list(cs, args): """Get registered users of Harbor.""" try: users = harbor_client.users.list() except exp.Forbidden as e: raise exp.CommandError(e.message) # Get admin user try: admin = harbor_client.users.get(1) users.append(admin) except Exception: pass fields = ['user_id', 'username', 'is_admin', 'email', 'realname', 'comment'] formatters = {"is_admin": 'has_admin_role'} utils.print_list(users, fields, formatters=formatters, sortby=args.sortby)
def do_logs(cs, args): """Get recent logs of the projects which the user is a member of.""" logs = cs.logs.list() or [] for log in logs: repo = log['repo_name'] tag = None if log['repo_tag'] != 'N/A': tag = log['repo_tag'] if tag: repo += ":%s" % tag log['repository'] = repo fields = [ 'log_id', 'op_time', 'username', 'project_id', 'operation', 'repository' ] utils.print_list(logs, fields, sortby=args.sortby)
def do_search(cs, args): """Search for projects and repositories.""" data = cs.searcher.search(args.query) project_fields = [ 'project_id', 'name', 'public', 'repo_count', 'creation_time' ] print("Find %d Projects: " % len(data['project'])) utils.print_list(data['project'], project_fields, formatters={}, sortby='id') repository_fields = [ 'repository_name', 'project_name', 'project_id', 'project_public' ] print("\n") print("Find %d Repositories: " % len(data['repository'])) utils.print_list(data['repository'], repository_fields, formatters={}, sortby='repository_name')
def get_image_list(): """查詢鏡像倉庫的鏡像列表""" project = request.args.get('project', default=1) #project = 1 repositories = harbor_client.repositories.list(project) return_model = {} data = [] for repo in repositories: tags = harbor_client.repositories.list_tags(repo['name']) for tag in tags: item = repo.copy() manifest = harbor_client.repositories.get_manifests(item['name'], tag['name']) size = 0 for layer in manifest['manifest']['layers']: size += layer['size'] item['size'] = str(size/(1024*1024)) + 'Mi' if tag['name'] != 'latest': item['name'] = repo['name'] + ":" + tag['name'] item['labels'].append(tag['name']) data.append(item) result = {'length': len(data), 'images': data} return_model['retCode'] = 200 return_model['retDesc'] = 'success' return_model['data'] = result fields = [ "name", 'project_id', 'size', "tags_count", "star_count", "pull_count", "update_time" ] utils.print_list(data, fields) return jsonify(return_model)