-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathws-client-im.py
More file actions
81 lines (57 loc) · 2.93 KB
/
Copy pathws-client-im.py
File metadata and controls
81 lines (57 loc) · 2.93 KB
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
#!/usr/bin/python
# python 3.4
import websocket
import _thread
import time
import ssl
import json
import urllib.request
def on_message(ws, message):
# print (message)
responseDict=json.loads(message)
manifestJson=json.loads(responseDict['object']['dockerImageManifest'])
tag=manifestJson['tag']
# print (tag)
digestUrl=responseDict['object']['dockerImageReference']
print ("Received " + digestUrl)
print("Received tag " + tag)
if tag != "latest" and tag != "":
# tell jenkins to scan the image
response = urllib.request.urlopen('http://localhost:8080/job/process_digest/buildWithParameters?digestUrl=' + digestUrl )
def on_error(ws, error):
print (error)
def on_close(ws):
print ("### closed ###")
def on_open(ws):
def run(*args):
for i in range(30000):
time.sleep(1)
ws.send("Hello %d" % i)
time.sleep(1)
ws.close()
print ("thread terminating...")
_thread.start_new_thread(run, ())
if __name__ == "__main__":
# set auth credentials for openshift
authHeaderKey="Authorization"
authHeaderVal="Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJkZWZhdWx0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZWNyZXQubmFtZSI6ImplbmtpbnMtc2EtdG9rZW4tMXgxbzAiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiamVua2lucy1zYSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6ImQ1YTg2MTc1LWU3ZWQtMTFlNS05MTAyLTA4MDAyN2I2MTc4YyIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDpkZWZhdWx0OmplbmtpbnMtc2EifQ.SZIMjxUdZF9iN3q24lGrylStcZWbWwh8MxcEjD-oeNvK0LnbcmmIhC-jNUkaJyvhSsy2QcKhRM194vMJDY1sCRXAyO26y8bJ-beoS6xOaOUFST3PptnSPemckOkozQImpTy4tZ0hx8ma82UuUAF9wMf4LFy3kornZ6krA38U2k6z05NvB7Y717uQpGQZd2kh4xfHfWcEff3BF9fWZ5U4BuLEgf2t-62JCqYrvnEFxKBo__IL838eXg_hUZySEOUD_Nq9GDAlimnXzm2AxWDFWQfJXt-lS430DUQ1XHwqVq-cL0BrZhZR7sjtHkjtF-6Boq2tvpHtwba1ooIBhXwouQ"
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
# SSLContext.verify_mode CERT_NONE
# first, get the current version of the resource
url="https://localhost:8443/oapi/v1/images"
request = urllib.request.Request(url)
request.add_header(authHeaderKey, authHeaderVal)
responseJson = urllib.request.urlopen(request,context=ctx).read().decode('utf8')
responseDict=json.loads(responseJson)
# print (responseJson)
resourceVersion=responseDict['metadata']['resourceVersion']
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://localhost:8443/oapi/v1/images?watch=true&resourceVersion=" + resourceVersion,
on_message = on_message,
on_error = on_error,
on_close = on_close,
header = [authHeaderKey + ": " +authHeaderVal])
# ws.on_open = on_open
ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})