Project

Profile

Help

Test #5181 » add_remove_utils.py

amacdona@redhat.com, 07/25/2019 02:48 PM

 
1
import sys
2
import json
3
from requests.auth import HTTPBasicAuth
4
import requests
5
from subprocess import check_output
6

    
7
DESTINATION_REPO = 'testrepo'
8
SOURCE_REPO = 'busybox'
9

    
10
TAGS = [
11
    u'54ba1853-5e31-4b9d-b162-71ce4830d60d', u'bb84916b-b152-4ecb-950d-3974ef88154a',
12
    u'2e83a76e-7511-4a28-8237-d6ac342ff5ba', u'eba2af8f-7ca3-4147-b07f-72b0d3b4c113',
13
    u'958be6c8-cc96-4c1e-9e58-5e912044e68e', u'b9024e42-035d-4454-b418-954e94dda345',
14
    u'ef82acad-afa8-4424-ada9-3e0e05d863e1', u'ec6e6830-1201-451a-a1e8-52e7dadbaf9b',
15
    u'48b53d66-2446-4f9f-8cb4-40f6ea54b16d', u'c4048925-b632-4edb-9bf2-c62d1d8d096f'
16
]
17

    
18
# TEN_MANIFEST_LIST_IDS = [
19
#     u'7a163c20-400c-49a2-bd10-53fd87005920', u'376ec577-96fc-4ae2-9977-3469fc6145f3',
20
#     u'da70ae27-bca4-4602-ae21-eb5dbcdcaa84', u'd284f52a-16d5-47e9-bfb2-7f62c1cb45a1',
21
#     u'581ecfeb-f5c5-43b7-8086-20c6251baa1a', u'5ee10453-92f1-4367-ba82-9e4c2b5b1136',
22
#     u'c4a2c81e-53e1-4195-9c9d-19f7109e241c', u'8393b0dc-de8b-45bb-879a-8b8bb689d569',
23
#     u'bcfd8d6d-09cc-4744-923c-385da00896e8', u'6ef97386-c7c3-49eb-8604-67c73b31ac01'
24
# ]
25

    
26
MANIFEST_LISTS_NO_SHARED_MANIFESTS = [
27
    u'7a163c20-400c-49a2-bd10-53fd87005920', u'376ec577-96fc-4ae2-9977-3469fc6145f3',
28
    u'da70ae27-bca4-4602-ae21-eb5dbcdcaa84', u'581ecfeb-f5c5-43b7-8086-20c6251baa1a',
29
    u'c4a2c81e-53e1-4195-9c9d-19f7109e241c', u'8393b0dc-de8b-45bb-879a-8b8bb689d569',
30
    u'bcfd8d6d-09cc-4744-923c-385da00896e8', u'6ef97386-c7c3-49eb-8604-67c73b31ac01'
31
]
32

    
33
# 2 manifests are shared between them
34
MANIFEST_LISTS_SHARING_MANIFESTS = [
35
    u'd284f52a-16d5-47e9-bfb2-7f62c1cb45a1',
36
    u'5ee10453-92f1-4367-ba82-9e4c2b5b1136',
37
]
38

    
39
MANIFESTS_NO_SHARED_BLOBS = [
40
    u'0023cd1e-b3f1-4044-9421-16b06f0189fc',
41
    u'03ccd921-ae5c-48a3-a7ca-867896a8c00a',
42
    u'05976e8f-fd28-450c-860c-d356409b8171',
43
    u'063fa040-251f-42a0-a1d4-6df3081aa6b3',
44
    u'0702f826-8cbb-4481-969f-58456d28fc04',
45
    u'08648b19-9fa8-4793-a613-97d3aa2138ac',
46
    u'087e9d85-56ef-45f0-991a-3e33b319e27c',
47
]
48

    
49
MANIFESTS_WITH_SHARED_BLOBS = [
50
    u'03769303-0fec-479b-9b98-69d13da6ebb4',
51
    u'076f45c1-a066-41e5-a45a-45a65cf379fc',
52
    u'0b11afd6-6d6a-4ac7-801d-cd87c62c7729'
53
]
54

    
55

    
56
AUTH = HTTPBasicAuth('admin', 'admin')
57
BASE_URL = 'https://pulp2.dev/pulp/api/v2'
58

    
59

    
60
def git_info():
61
    out = check_output(["git", "branch"]).decode("utf8")
62
    current = next(line for line in out.split("\n") if line.startswith("*"))
63
    print("\n=======================================================")
64
    print("        BRANCH                    " + current.strip("*").strip())
65
    print("\n=======================================================")
66
    cleanup()
67

    
68

    
69
def ensure_empty():
70
    count = get_repo_content_count(DESTINATION_REPO)
71
    if count:
72
        print("NON EMPTY REPO")
73
        print(count)
74
        sys.exit(1)
75

    
76

    
77
def cleanup():
78
    tags = set(get_units_by_type(DESTINATION_REPO, 'docker_tag'))
79
    manifest_lists = set(get_units_by_type(DESTINATION_REPO, 'docker_manifest_list'))
80
    manifests = set(get_units_by_type(DESTINATION_REPO, 'docker_manifest'))
81
    blobs = set(get_units_by_type(DESTINATION_REPO, 'docker_blob'))
82
    rm_by_content_pk(list(tags) + list(manifest_lists) + list(manifests) + list(blobs))
83
    if get_repo_content_count(DESTINATION_REPO):
84
        raise Exception("cleanup")
85

    
86

    
87
def wait_until_complete(task):
88
    state = ''
89
    while state != "finished":
90
        req = requests.get(
91
            url='https://pulp2.dev{task}'.format(task=task),
92
            auth=AUTH,
93
        )
94
        state = json.loads(req.content)['state']
95
    return json.loads(req.content)
96

    
97

    
98
def get_units_by_type(repo_id, content_type):
99
    criteria = {
100
        "type_ids": [content_type],
101
        "filters": {
102
            "unit": {}
103
        }
104
    }
105
    req = requests.post(
106
        url='{base}/repositories/{repo}/search/units/'.format(base=BASE_URL, repo=repo_id),
107
        data=json.dumps({"criteria": criteria}),
108
        auth=AUTH,
109
    )
110
    id_list = [content['unit_id'] for content in json.loads(req.content)]
111
    return id_list
112

    
113

    
114
def get_repo_content_count(repo_id):
115
    req = requests.get(
116
        url='{base}/repositories/{repo}/'.format(base=BASE_URL, repo=repo_id),
117
        auth=AUTH
118
    )
119
    counts_dict = json.loads(req.content)['content_unit_counts']
120
    return counts_dict
121

    
122

    
123
def get_repo_tag_names(repo_id):
124
    criteria = {
125
        "type_ids": ['docker_tag'],
126
        "filters": {
127
            "unit": {}
128
        }
129
    }
130
    req = requests.post(
131
        url='{base}/repositories/{repo}/search/units/'.format(base=BASE_URL, repo=repo_id),
132
        data=json.dumps({"criteria": criteria}),
133
        auth=AUTH,
134
    )
135
    tag_names = [content['metadata']['name'] for content in json.loads(req.content)]
136
    return tag_names
137

    
138

    
139
def copy_by_content_pk(content_type, content_pks):
140
    criteria = {
141
        "type_ids": [content_type],
142
        "filters": {"unit": {"_id": {"$in": content_pks}}},
143
    }
144
    req = requests.post(
145
        url='{base}/repositories/{repo}/actions/associate/'.format(base=BASE_URL,
146
                                                                   repo=DESTINATION_REPO),
147
        data=json.dumps({
148
            "criteria": criteria,
149
            "source_repo_id": SOURCE_REPO,
150
        }),
151
        auth=AUTH,
152
    )
153
    task = json.loads(req.content)['spawned_tasks'][0]['_href']
154
    content = wait_until_complete(task)
155

    
156

    
157
def rm_by_content_pk(all_units):
158
    criteria = {
159
        "type_ids": ['docker_tag', 'docker_manifest_list', 'docker_manifest', 'docker_blob'],
160
        "filters": {"unit": {"_id": {"$in": all_units}}},
161
    }
162
    req = requests.post(
163
        url='{base}/repositories/{repo}/actions/unassociate/'.format(base=BASE_URL,
164
                                                                     repo=DESTINATION_REPO),
165
        data=json.dumps({"criteria": criteria}),
166
        auth=AUTH,
167
    )
168
    task = json.loads(req.content)['spawned_tasks'][0]['_href']
169
    content = wait_until_complete(task)
(2-2/3)