-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromotion.py
231 lines (212 loc) · 8.71 KB
/
promotion.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# Rob Casale
# 4/22/2016
# This will pull and image from the CDL and promote the image to DC1 and DC2
# The image that gets pulled is specified by the <repository>/<image-name>:<tag>
# <repository> is required
# <image-name> is optional
# <tag> is required
# User will enter their request in the following format:
# ./promotion.py <repository>/<image-name> <tag>
# Return codes:
# - 0 success
# - 1 repository does not exist
# - 2 image does not exist
# - 3 tag does not exist
# - 4 account does not exist
# - 8 other
import json, sys, requests, os, ssl, urllib2, base64
from docker import Client
cli = Client(base_url='unix://var/run/docker.sock')
# Get pass verification and get response
gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
# DTR urls
test_dtr_url='<dtr-hostname>'
cdl_dtr_url='<dtr-hostname>'
dc1_dtr_url='<dtr-hostname>'
dc2_dtr_url='<dtr-hostname>'
# API url requests
test_dtr_repo_url='https://<dtr-hostname>/api/v0/repositories/'
cdl_dtr_repo_url='https://<dtr-hostname>/api/v0/repositories/'
dc1_dtr_repo_url='https://<dtr-hostname>/api/v0/repositories/'
dc2_dtr_repo_url='https://<dtr-hostname>/api/v0/repositories/'
# Login credentials
username='casaler'
password=''
email='<email>'
# DTR login responses
login_status='Status'
login_success='Login Succeeded'
# JSON Attributes
name='name'
tags='tags'
visibility='visibility'
repositories='repositories'
# Proxy settings
proxies={
"http":"http://<username>:<password>@<proxy-info>:8080",
"https":"http://<username>:<password>@<proxy-info>:8080",
}
# Build request
def build_request(requestUrl):
request=urllib2.Request(requestUrl)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
return request
# Get the response code of the submitted request
def get_code(request):
code=urllib2.urlopen(request,context=gcontext).getcode()
return code
# Get the json response of the submitted request
def get_info(request):
info=urllib2.urlopen(request,context=gcontext)
return json.load(info)
# Create a repository on a DTR
def create_repository_code(request, repo, visibility):
data={'name':repo,'visibility':visibility}
request.add_header('Content-Type', 'application/json')
code=urllib2.urlopen(request,json.dumps(data),context=gcontext).getcode()
return code
# Check arguments
help_option="help"
if len(sys.argv) == 2 and sys.argv[1] == help_option:
print 'To execute this script:\n\n' + \
'\tpython promotion.py <repository>/<image-name> <tag>\n'
print 'Return codes:\n' + \
'- 0 success\n' + \
'- 1 repository does not exist\n' + \
'- 2 image does not exist\n' + \
'- 3 tag does not exist\n' + \
'- 4 account does not exist\n' + \
'- 8 other'
exit()
elif len(sys.argv) != 3:
print 'exit(8): Type \"python promotion.py <repository>/<image-name> <tag>\"'
exit()
# Looks for the given tag in a specific image
def find_tag(tag_list_url, image_name):
# Used to push/pull/tag the image(s) properly
test_image_name=test_dtr_url + '/' + repo + '/' + image_name
cdl_image_name=cdl_dtr_url + '/' + repo + '/' + image_name
dc1_image_name=dc1_dtr_url + '/' + repo + '/' + image_name
dc2_image_name=dc2_dtr_url + '/' + repo + '/' + image_name
try:
tag_response=get_info(build_request(tag_list_url))
tag_list=tag_response[tags]
pulled=False
for tag_version in tag_list:
if tag_version[name] == tag:
print 'Pulling image...'
cli.pull(cdl_image_name, tag, stream=False)
pulled=True
break
if pulled:
print repo + '/' + image_name + ':' + tag + ' pulled'
try:
#get_code(build_request(test_dtr_repo_url + repo + '/' + image_name))
get_code(build_request(dc1_dtr_repo_url + repo + '/' + image_name))
get_code(build_request(dc2_dtr_repo_url + repo + '/' + image_name))
try:
# Login to DC1 and DC2
#cli.login(username, password, email, test_dtr_url)
cli.login(username, password, email, dc1_dtr_url)
try:
cli.login(username, password, email, dc2_dtr_url)
# Push to DC1 and DC2
#cli.tag(cdl_image_name + ':' + tag, test_image_name, tag)
cli.tag(cdl_image_name + ':' + tag, dc1_image_name, tag)
cli.tag(cdl_image_name + ':' + tag, dc2_image_name, tag)
#cli.push(test_image_name, tag, stream=False)
cli.push(dc1_image_name, tag, stream=False)
cli.push(dc2_image_name, tag, stream=False)
# Once complete remove the images
cli.remove_image(cdl_image_name + ':' + tag)
#cli.remove_image(test_image_name + ':' + tag)
cli.remove_image(dc1_image_name + ':' + tag)
cli.remove_image(dc2_image_name + ':' + tag)
print repo + '/' + image_name + ':' + tag + ' removed'
print 'exit(0): success'
except Exception as e:
print 'exit(8): Failed to login to DC2 DTR'
print e
except Exception as e:
print 'exit(8): Failed to login to DC1 DTR'
print e
# Repository is not created, therefore repository needs to be created
except Exception as e:
# Get repository details
repo_details=get_info(build_request(cdl_request_url))
repo_visibility=repo_details[visibility]
try:
# Create repo on DC1 and DC2
#create_repository_code(build_request(test_dtr_repo_url + repo), image_name, repo_visibility)
create_repository_code(build_request(dc1_dtr_repo_url + repo), image_name, repo_visibility)
create_repository_code(build_request(dc2_dtr_repo_url + repo), image_name, repo_visibility)
try:
# Login to DC1 and DC2
#cli.login(username, password, email, test_dtr_url)
cli.login(username, password, email, dc1_dtr_url)
try:
cli.login(username, password, email, dc2_dtr_url)
# Push to DC1 and DC2
#cli.tag(cdl_image_name + ':' + tag, test_image_name, tag)
cli.tag(cdl_image_name + ':' + tag, dc1_image_name, tag)
cli.tag(cdl_image_name + ':' + tag, dc2_image_name, tag)
#cli.push(test_image_name, tag, stream=False)
cli.push(dc1_image_name, tag, stream=False)
cli.push(dc2_image_name, tag, stream=False)
# Once complete remove the images
cli.remove_image(cdl_image_name + ':' + tag)
#cli.remove_image(test_image_name + ':' + tag)
cli.remove_image(dc1_image_name + ':' + tag)
cli.remove_image(dc2_image_name + ':' + tag)
print repo + '/' + image_name + ':' + tag + ' removed'
print 'exit(0): success'
except Exception as e:
print 'exit(8): Failed to login to DC2 DTR'
print e
except Exception as e:
print 'exit(8): Failed to login to DC1 DTR'
print e
except Exception as e:
print 'exit(4): Cannot create repository' + \
', account \"' + repo + '\" does not exist'
print 'Exception: ' + str(e)
else:
print 'exit(3): "' + repo + '/' + image_name + \
'" with tag "' + tag + '" was not found'
except Exception as e:
# repository or image does not exist
print 'exit(8): Repository or image does not exist'
print 'Exception: ' + str(e)
# Grab values from user
repository_image=sys.argv[1].split('/')
tag=sys.argv[2]
# Split up the first argument
repo=""
image_name=""
if len(repository_image) == 2:
repo=repository_image[0]
image_name=repository_image[1]
elif len(repository_image) == 1:
repo=repository_image[0]
else:
print 'exit(8): Incorrect arguments. Check <repository>/<image-name>'
exit()
try:
cli.login(username, password, email, cdl_dtr_url)
# Grab a specific image with a specific tag
if image_name != "":
# Used to grab a list of tags and could be used for other API calls
cdl_request_url=cdl_dtr_repo_url + repo + '/' + image_name
# Get list of tags /api/v0/repositories/{namespace}/{reponame}/tags
tag_list_url=cdl_request_url + '/tags'
find_tag(tag_list_url, image_name)
# Grab all images under a repository with a specific tag
else:
list_images=get_info(build_request(cdl_dtr_repo_url + repo))
for image in list_images[repositories]:
tag_list_url=cdl_dtr_repo_url + repo + '/' + image[name] + '/tags'
find_tag(tag_list_url, image[name])
except Exception as e:
print 'exit(8): Failed to login to CDL DTR'
print 'Exception: ' + str(e)