Last active
June 25, 2021 09:07
-
-
Save egeneralov/e583ff979c438999ae581a66ad6af73c 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
#!/usr/bin/env python3 | |
import sys | |
import argparse | |
import subprocess | |
import yaml | |
def main(path, name, namespace, options): | |
command = "helm template" | |
command += " --name-template " + name | |
command += " " + path | |
if namespace == "": | |
namespace = name | |
command += " --namespace=" + namespace | |
try: | |
tpl = subprocess.check_output( | |
command.split() | |
).decode() | |
except subprocess.CalledProcessError: | |
sys.exit(1) | |
payload = [ | |
yaml.load( | |
a, | |
Loader=yaml.FullLoader | |
) | |
for a in tpl.split("---") | |
] | |
payload = [ | |
a | |
for a in payload | |
if a != None | |
] | |
result = [ | |
{ | |
"name": "apt : python-pip", | |
"apt": { | |
"name": "python-pip", | |
"update_cache": "yes", | |
"cache_valid_time": 3600 | |
}, | |
"when": "ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'" | |
}, | |
{ | |
"name": "yum : python-pip", | |
"yum": { | |
"name": "python-pip" | |
}, | |
"when": "ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux' or ansible_distribution == 'RedHat'" | |
}, | |
{ | |
"name": "pip : openshift, PyYAML", | |
"pip": { | |
"name": [ | |
"openshift", | |
"PyYAML" | |
] | |
} | |
}, | |
{ | |
"name": "Create namespace {}".format(namespace), | |
"k8s": { | |
"state": "present", | |
"definition": { | |
"apiVersion": "v1", | |
"kind": "Namespace", | |
"metadata": { | |
"name": namespace | |
} | |
} | |
} | |
} | |
] | |
for a in payload: | |
a = { | |
"name": "install {} {} to {} namespace".format( | |
a["kind"], | |
a["metadata"]["name"], | |
namespace | |
), | |
"k8s": { | |
"state": "present", | |
"definition": a | |
} | |
} | |
result.append(a) | |
print( | |
yaml.dump([{ | |
"name": "Install " + name + " to namespace " + namespace, | |
"hosts": "kube-master[0]", | |
"gather_facts": "yes", | |
"tasks": result | |
}]) | |
) | |
def parse_args(): | |
parser = argparse.ArgumentParser( | |
description = 'Convert helm chart to ansible' | |
) | |
parser.add_argument( | |
'--path', | |
default = "", | |
required = True, | |
help = 'path to unpacked chart' | |
) | |
parser.add_argument( | |
'--name', | |
default = "", | |
required = True, | |
help = 'helm release name' | |
) | |
parser.add_argument( | |
'--namespace', | |
default = "", | |
help = 'namespace to install' | |
) | |
parser.add_argument( | |
'--options', | |
default = "", | |
help = 'additional helm chart options' | |
) | |
return parser.parse_args() | |
if __name__ == "__main__": | |
args = parse_args() | |
print("# helm2ansible") | |
print("# "+str(args)) | |
print("\n---") | |
main( | |
path = args.path, | |
name = args.name, | |
namespace = args.namespace, | |
options = args.options | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment