-
Notifications
You must be signed in to change notification settings - Fork 1
/
harbor.py
51 lines (40 loc) · 1.91 KB
/
harbor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
import json
import requests
from registry import Registry
class Harbor(Registry):
def __init__(self, client, registry):
super().__init__(client, registry, False)
def name(self):
return "harbor"
def last_build_commit(self, namespace, repo, tag):
try:
resp = requests.get(
# 'https://{}/api/repositories/{}/{}/tags/{}/manifest'.format(self.registry, namespace, repo, tag),
# Harbor API v2.0
'https://{}/api/v2.0/projects/{}/repositories/{}/artifacts/{}'.format(
self.registry, namespace, repo, tag),
auth=(self.username, self.password))
metadata = json.loads(resp.content.decode('utf-8'))
# return json.loads(metadata['config'])['config']['Labels']['org.label-schema.vcs-ref']
# Harbor API v2.0
return metadata['extra_attrs']['config']['Labels']['org.label-schema.vcs-ref']
except Exception as e:
print("last_build_commit failed: {}".format(e))
return None
def push_image(self, namespace, repo, tag):
image = '{}/{}'.format(namespace, repo)
image_tag = '{}/{}:{}'.format(self.registry, image, tag)
print('Push Docker image {} on harbor ({})'.format(image_tag, self.name()))
self.client.api.tag(image, image_tag)
image = '{}/{}/{}'.format(self.registry, namespace, repo)
self.client.images.push(image, tag=tag)
def get_remote_image_id(self, namespace, repo, tag):
try:
resp = requests.get(
'https://{}/api/v2.0/projects/{}/repositories/{}/artifacts?q=tags={}'.format(self.registry, namespace, repo, tag),
auth=(self.username, self.password))
metadata = json.loads(resp.content.decode('utf-8'))
return metadata[0]['digest']
except Exception as e:
return None