forked from rbocchinfuso/di2020_clint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghetto_cicd.py
executable file
·66 lines (57 loc) · 2.43 KB
/
ghetto_cicd.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
#!/usr/bin/env python
import time, sys, os, subprocess, shlex
from colorama import Fore, Back, Style
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
# local
container_name = 'di2020_clint'
stop = 'docker stop ' + container_name
rm = 'docker rm -f ' + container_name
rmi = 'docker rmi -f ' + container_name
build = 'docker image build -t ' + container_name + ' .'
run = 'docker run --rm -ti --name ' + container_name + ' ' + container_name
prune = 'docker system prune -f'
class MyHandler(PatternMatchingEventHandler):
patterns = ["*build*"]
def process(self, event):
"""
event.event_type
'modified' | 'created' | 'moved' | 'deleted'
event.is_directory
True | False
event.src_path
path/to/observed/file
"""
# the file will be processed there
print (event.src_path, event.event_type) # print degugging
def on_modified(self, event):
self.process(event)
# Build and deploy conainer
print (Fore.WHITE + Back.RED + "___STOPPING RUNNING CONTAINER: " + container_name + '___' + Style.RESET_ALL)
stop_proc = subprocess.Popen(shlex.split(stop), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stop_proc.wait()
print (Fore.WHITE + Back.RED + "___REMOVING CONTAINER: " + container_name + '___' + Style.RESET_ALL)
rm_proc = subprocess.Popen(shlex.split(rm), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
rm_proc.wait()
rmi_proc = subprocess.Popen(shlex.split(rmi), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
rmi_proc.wait()
print (Fore.WHITE + Back.BLUE + "___BUILDING NEW IMAGE: " + container_name + '___' + Style.RESET_ALL)
build_proc = subprocess.Popen(shlex.split(build))
build_proc.wait()
print (Fore.WHITE + Back.GREEN + "___RUNNING CONTIANER: " + container_name + '___' + Style.RESET_ALL)
run_proc = subprocess.Popen(shlex.split(run))
time.sleep(10)
prune_proc = subprocess.Popen(shlex.split(prune), stdout=subprocess.PIPE)
def on_created(self, event):
self.process(event)
if __name__ == '__main__':
args = sys.argv[1:]
observer = Observer()
observer.schedule(MyHandler(), path=args[0] if args else '.')
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()