11import os
2- import yaml
32
4- # Define directory and file structure
5- project_name = 'MyHelm'
6- base_path = os .path .join ('app' , 'media' , project_name )
3+ # Define project structure
4+ project_name = 'app/media/MyHelm'
5+ directories = ['charts/' , 'templates/' ]
6+ templates_dir = 'templates/web'
7+ files = ['Chart.yaml' , 'values.yaml' ]
8+ template_subdirectories = ['web' ]
9+ template_files = ['service.yaml' , 'helpers.tpl' ]
710
8- directories = [
9- os .path .join (base_path , 'charts' ),
10- os .path .join (base_path , 'templates' , 'web' )
11- ]
11+ # Chart.yaml content
12+ chart_yaml_content = """apiVersion: v2
13+ name: my-helm-chart
14+ description: A Helm chart for Kubernetes
15+ version: 0.1.0
16+ """
1217
13- files = {
14- 'Chart.yaml' : {
15- 'apiVersion' : 'v2' ,
16- 'name' : project_name ,
17- 'description' : 'A Helm chart for Kubernetes' ,
18- 'version' : '0.1.0' ,
19- 'appVersion' : '1.0.0'
20- },
21- 'values.yaml' : {
22- 'web' : {
23- 'image' : {
24- 'repository' : 'nginx' ,
25- 'tag' : 'latest'
26- },
27- 'service' : {
28- 'enabled' : True ,
29- 'port' : 80
30- },
31- 'replicaCount' : 1 ,
32- 'persistence' : {
33- 'enabled' : True ,
34- 'size' : '1Gi' ,
35- 'accessModes' : ['ReadWriteOnce' ]
36- },
37- 'env' : [{
38- 'name' : 'ENV1' ,
39- 'value' : 'Hi'
40- }],
41- 'ingress' : {
42- 'enabled' : False ,
43- 'host' : 'www.example.com'
44- }
45- }
46- }
47- }
18+ # values.yaml content
19+ values_yaml_content = """web:
20+ image: nginx
21+ replicas: 1
22+ service:
23+ targetPort: 80
24+ persistence:
25+ enabled: true
26+ size: 1Gi
27+ accessModes:
28+ - ReadWriteOnce
29+ env:
30+ - name: ENV1
31+ value: Hi
32+ ingress:
33+ enabled: false
34+ host: www.example.com
35+ """
4836
49- # Create directories
37+ # Create project structure
38+ os .makedirs (project_name , exist_ok = True )
5039for directory in directories :
51- os .makedirs (directory , exist_ok = True )
40+ os .makedirs (os . path . join ( project_name , directory ) , exist_ok = True )
5241
53- # Create files with initial content
54- for filename , content in files .items ():
55- with open (os .path .join (base_path , filename ), 'w' ) as file :
56- yaml .dump (content , file )
42+ # Create templates/web directory
43+ os .makedirs (os .path .join (project_name , templates_dir ), exist_ok = True )
5744
58- # Create service.yaml template
59- service_yaml_content = """apiVersion: v1
60- kind: Service
61- metadata:
62- name: {{ include "{project_name}.fullname" . }}
63- spec:
64- type: ClusterIP
65- ports:
66- - port: {{ .Values.web.service.port }}
67- selector:
68- app: {{ include "{project_name}.name" . }}
69- """
45+ # Create files
46+ with open (os .path .join (project_name , 'Chart.yaml' ), 'w' ) as chart_file :
47+ chart_file .write (chart_yaml_content )
7048
71- with open (os .path .join (base_path , 'templates' , 'web' , 'service .yaml' ), 'w' ) as service_file :
72- service_file .write (service_yaml_content . format ( project_name = project_name ) )
49+ with open (os .path .join (project_name , 'values .yaml' ), 'w' ) as values_file :
50+ values_file .write (values_yaml_content )
7351
74- # Create secret.yaml template if environment variables exist
75- if files ['values.yaml' ]['web' ].get ('env' ):
76- secret_yaml_content = """apiVersion: v1
77- kind: Secret
78- metadata:
79- name: {{ include "{project_name}.fullname" . }}-env
80- type: Opaque
81- data:
82- ENV1: {{ .Values.web.env[0].value | b64enc | quote }}
83- """
52+ # Create template files and directories
53+ for template in template_subdirectories :
54+ template_path = os .path .join (project_name , 'templates' , template )
55+ os .makedirs (template_path , exist_ok = True )
56+ for template_file in template_files :
57+ with open (os .path .join (template_path , template_file ), 'w' ) as file :
58+ file .write (f"# { template_file } for { template } \n " )
59+
60+ # Create secret.yaml if env variable is considered
61+ with open (os .path .join (template_path , 'secret.yaml' ), 'w' ) as secret_file :
62+ secret_file .write ("# secret.yaml for environment variables\n " )
8463
85- with open (os .path .join (base_path , 'templates' , 'web' , 'secret.yaml' ), 'w' ) as secret_file :
86- secret_file .write (secret_yaml_content .format (project_name = project_name ))
64+ # Create helpers.tpl
65+ with open (os .path .join (template_path , 'helpers.tpl' ), 'w' ) as helpers_file :
66+ helpers_file .write (f"""{{- define "{ template } .labels" -}}
67+ name: {{ .Release.Name }}
68+ {{- end -}}
69+ // Add additional helper functions if needed
70+ """ )
0 commit comments