Skip to content

Instantly share code, notes, and snippets.

@Voronenko
Created January 12, 2021 15:51
Show Gist options
  • Save Voronenko/60f9a12a06e6cacb33101d6316f922f2 to your computer and use it in GitHub Desktop.
Save Voronenko/60f9a12a06e6cacb33101d6316f922f2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import click
import yaml
# define a custom representer for strings
def quoted_presenter(dumper, data):
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='"')
yaml.add_representer(str, quoted_presenter)
def RepresentsInt(s):
try:
int(s)
return True
except ValueError:
return False
@click.command()
@click.option('--filename', prompt='File',
help='Dockerfile to parse')
def deployment(filename='Dockerfile'):
"""Simple program that transforms docker file
ENV sequence into helm env vars"""
dockerfile = open(filename, 'r')
lines = dockerfile.readlines()
variables = []
for line in lines:
if line.startswith("ENV"):
line = line.replace("<account name> <account key>", "account_name account_key")
line = line.replace("<account name>", "account_name")
parts = line.split()
pair = dict()
pair["name"] = parts[1]
if (1==1):
pair["value"] = parts[2]
elif (parts[2].isnumeric()):
if (RepresentsInt(parts[2])):
pair["value"] = int(parts[2])
else:
pair["value"] = float(parts[2])
elif (parts[2] == "true" or parts[2] == "false"):
pair["value"] = parts[2].lower() in ['true', '1']
else:
pair["value"] = parts[2]
variables.append(pair)
output = dict()
output["env"] = variables
output_str = yaml.dump(output)
output_str = output_str.replace('"name":', 'name:')
output_str = output_str.replace('"value":', 'value:')
output_str = output_str.replace('"env":', 'env:')
print(output_str)
if __name__ == '__main__':
deployment()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment