Created
May 28, 2020 18:33
-
-
Save stepanselyuk/35bbe3379444f8517569618eef09bfae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import inotify.adapters | |
import yaml | |
import os.path | |
import time | |
def _main(): | |
i = inotify.adapters.InotifyTree('/tmp') | |
for event in i.event_gen(yield_nones=False): | |
(_, type_names, path, filename) = event | |
if 'IN_ISDIR' not in type_names: | |
continue | |
if 'IN_CREATE' not in type_names: | |
continue | |
fpath = '/'.join([path, filename]) | |
if 'helm-template-workdir' not in fpath: | |
continue | |
print("MATCH / PATH=[{}] EVENT_TYPES={}".format(fpath, type_names)) | |
files = [ | |
'jenkins-x/output/namespaces/jx/env/charts/jenkins-x-platform/charts/chartmuseum/templates/part0-deployment.yaml', | |
'jenkins-x/output/namespaces/jx/env/charts/jenkins-x-platform/charts/heapster/templates/part0-deployment.yaml' | |
] | |
for f in files: | |
filepath = '/'.join([fpath,f]) | |
slept = 0 | |
while not os.path.exists(filepath): | |
time.sleep(0.005) | |
slept += 0.005 | |
if slept > 1: | |
print("Cannot find file %s after 1 sec of waiting" % filepath) | |
break | |
if os.path.isfile(filepath): | |
fix_file(filepath) | |
print("FIXED FILE: PATH=[{}] EVENT_TYPES={}".format(fpath, type_names)) | |
def fix_file(fpath): | |
print(fpath) | |
with open(fpath, 'r') as file: | |
doc = yaml.load(file, Loader=yaml.FullLoader) | |
#if doc['kind'] != 'Deployment' and doc['apiVersion'] != 'extensions/v1beta1': | |
# return | |
doc['apiVersion'] = 'apps/v1' | |
labels = doc['spec']['template']['metadata']['labels'] | |
doc['spec']['selector'] = {} | |
doc['spec']['selector']['matchLabels'] = labels.copy() | |
with open(fpath, 'w') as file: | |
yaml.dump(doc, file) | |
if __name__ == '__main__': | |
_main() |
In a working case it looks like:
MATCH / PATH=[/tmp/helm-template-workdir-952010232] EVENT_TYPES=['IN_CREATE', 'IN_ISDIR']
/tmp/helm-template-workdir-952010232/jenkins-x/output/namespaces/jx/env/charts/jenkins-x-platform/charts/chartmuseum/templates/part0-deployment.yaml
FIXED FILE: PATH=[/tmp/helm-template-workdir-952010232] EVENT_TYPES=['IN_CREATE', 'IN_ISDIR']
/tmp/helm-template-workdir-952010232/jenkins-x/output/namespaces/jx/env/charts/jenkins-x-platform/charts/heapster/templates/part0-deployment.yaml
FIXED FILE: PATH=[/tmp/helm-template-workdir-952010232] EVENT_TYPES=['IN_CREATE', 'IN_ISDIR']
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
inotify
andpyyaml
pip modules required.