-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscript.py
More file actions
60 lines (42 loc) · 2.08 KB
/
script.py
File metadata and controls
60 lines (42 loc) · 2.08 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
import json
import subprocess
import time
REPO = "https://github.com/acoronadoc/python-deployment-script-sample.git"
BRANCH = "main"
CHECK_INTERVAL = 5
def exec( cmd, cwd=".", returnjson=False ):
out = subprocess.run( cmd, capture_output=True, text=True, encoding="utf8", cwd=cwd )
if ( out.returncode != 0 ):
print( "", flush=True )
print( "ERROR EXECUTING TASK: %s " % out.args, flush=True)
print( "STDOUT: %s " % out.stdout, flush=True )
print( "STDERR: %s " % out.stderr, flush=True )
print( "", flush=True )
return None
if ( returnjson ):
return json.loads( out.stdout )
return out
def process_pipeline():
r = exec( [ "rm", "-R", "-f", "dir1" ] )
r = exec( [ "git", "clone", REPO, "dir1" ] )
r = exec( [ "kubectl", "apply", "-f", "kubernetes/sampleapp.yaml" ] )
r = exec( [ "kubectl", "cp", "script.py", "nginx-statefulset-0:/usr/share/nginx/html" ] )
r = exec( [ "/bin/bash", "-c" ,"kubectl exec -it pod/nginx-statefulset-0 -- /bin/sh -c 'date > /usr/share/nginx/html/index.html'" ] )
r = exec( [ "/bin/bash", "-c" ,"kubectl exec -it pod/nginx-statefulset-0 -- /bin/sh -c 'echo '' >> /usr/share/nginx/html/index.html'" ] )
r = exec( [ "/bin/bash", "-c" ,"kubectl exec -it pod/nginx-statefulset-0 -- /bin/sh -c 'echo '' >> /usr/share/nginx/html/index.html'" ] )
r = exec( [ "/bin/bash", "-c" ,"kubectl exec -it pod/nginx-statefulset-0 -- /bin/sh -c 'cat /usr/share/nginx/html/script.py >> /usr/share/nginx/html/index.html'" ] )
last_commit = ""
while True:
r = exec( [ "git", "ls-remote", "--heads", REPO ] )
#print( r )
commits = ("%s" % r.stdout).splitlines()
commits = list( filter( lambda x: x.endswith( "refs/heads/" + BRANCH ), commits ) )
commits = list( map( lambda x: x.split( "\t" ), commits ) )
if last_commit != "" and last_commit != commits[0][0]:
print( "Processint new commit: %s" % commits[0][0] )
process_pipeline()
else:
print( "No new commit found: %s" % last_commit )
if len( commits ) > 0:
last_commit = commits[0][0]
time.sleep( CHECK_INTERVAL )