-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinottify.py
More file actions
159 lines (119 loc) · 3.93 KB
/
Copy pathinottify.py
File metadata and controls
159 lines (119 loc) · 3.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
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
from inotify_simple import INotify, flags
import subprocess
import os
import yaml
import glob
# import kubernetes
from kubernetes import client, config, utils
'''
version: v1
kind: DevConfig
data:
script:
- script 1
- script 2
- script 3
stages:
- script
'''
general_config = {'version': 'v1', 'kind': 'DevConfig', 'data': {}, 'stages': []}
devignore = {}
# Note: Import the config first and then the client.
config.load_kube_config()
k8s_client = client.ApiClient()
def _dev_config_sh(files):
general_config['data']['scripts'] = files
return general_config
def _dev_config_k8s(files):
general_config['data']['k8s'] = files
return general_config
def _dev_config_create(context,files):
if context == 'shell_script':
return _dev_config_sh(files)
elif context == "yaml_script":
return _dev_config_k8s(files)
def _execute_config_file():
config_data = _read_config()
_execute_config_data(config_data)
def _read_config():
with open('dev.yaml','r') as f:
config_data = yaml.load(f, Loader=yaml.FullLoader)
return config_data
def _check_object_exists(kind):
# check whether the object exists or not
def _check_patch_object(kind):
#check patch here, return True otherwise False
# check for try and except, if patch is successful then write a true message otherwise return to called function
def _recerate_object(kind):
# recreate the objects like pods, failure in patching
def _delete_kind_object(kind):
# check for patch
# otherwise delete the file
def _execute_script(scripts):
print('Execute script')
for script in scripts:
os.chmod(script,0o755)
subprocess.call(script,shell=True)
def _execute_yaml(yaml_file):
with open(yaml_file,'rt') as f:
output = list(yaml.load_all(f, Loader=yaml.FullLoader))
for kind in output:
#check pod status here
# then go for delete
_delete_kind_object(kind)
print(output)
# utils.create_from_yaml(k8s_client, yaml_file)
def _execute_yaml_script(yaml_scripts):
for file in yaml_scripts['files']:
_execute_yaml(file)
def _execute_stage(stage,config_file):
print('execute stage')
if stage == 'scripts':
scripts = config_file['data']['scripts']
_execute_script(scripts)
elif stage == 'k8s':
yaml_scripts = config_file['data']['k8s']
_execute_yaml_script(yaml_scripts)
def _execute_config_data(config_file):
stages = config_file['stages']
for stage in stages:
_execute_stage(stage,config_file)
return None
def _check_devignore():
print(list(yaml.load_all('.devignore.yaml',Loader=yaml.FullLoader)))
def dev_mode():
directory = os.getcwd()
inotify = INotify()
# Add the code to use rm_watch over the directories and the files that are to be removed from tracking
_check_devignore()
watch_flags = flags.CREATE | flags.DELETE | flags.MODIFY | flags.DELETE_SELF
inotify_object_list = []
for (root,dirs,files) in os.walk(directory,topdown=True):
inotify_object_list.append(inotify.add_watch(root, watch_flags))
print('before loop')
_execute_config_file()
while True:
event = inotify.read()
print(event)
if event:
_execute_config_file()
def init_mode():
dir = os.getcwd()
shell_script = glob.glob('./*.sh')
yaml_script = glob.glob('./*.yaml')
dockerfile_script = glob.glob('./Dockerfile')
file_data = general_config
shell_script.remove('./dev_notify.sh')
yaml_script.remove('./dev.yaml')
if shell_script:
file_data = _dev_config_create('shell_script',shell_script)
# print(file_data)
if yaml_script:
file_data = _dev_config_create('yaml_script',yaml_script)
# print(file_data)
with open('dev.yaml', 'w') as file:
data = yaml.dump(file_data, file)
def main():
print('Main function')
if __name__=='__main__':
main()