-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
141 lines (98 loc) · 4.3 KB
/
main.py
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
#!/usr/bin/env python3
import argparse
import os
import sys
import yaml
def main():
parser=argparse.ArgumentParser()
parser.add_argument('-n', '--name', help='name of your app', required=True, type=str)
parser.add_argument('-k', '--kind', help='kind of your yaml file', required=True, type=str)
parser.add_argument('-i', '--image', help='image you want to use', type=str)
parser.add_argument('-s', '--selector', help='name of your selector', type=str)
parser.add_argument('-p', '--port', help='number of your port', type=str)
parser.add_argument('-r', '--replicas', help='count of your replicas', type=str)
args=parser.parse_args()
tempDict=createTempDict()
if args.kind: # we controlled kind field is empty or not
if (list(args.kind)[0]=="p"): # We fixed lower inital letters
kind=args.kind
kind=kind.replace("p","P")
api="v1" #we use v1 for pods
else:
kind=args.kind
api="v1" #we use v1 for pods
if (list(args.kind)[0]=="d"):
kind=args.kind # We fixed lower inital letters
kind=kind.replace("d","D")
api="apps/v1" #we use apps/v1 for deployments
else:
kind=args.kind
api="apps/v1" #we use apps/v1 for deployments
if args.image:
tempDict["image"]=args.image
if args.name:
name=args.name
if args.replicas and kind == "Deployment":
replicas=int(args.replicas)
else:
replicas= None
if args.port:
tempDict["ports"]=int(args.port)
try:
yamlDict={
"apiVersion":api,
"kind":kind,
"metadata":{"labels":{"app":"%s"%(name,)}},
"spec":{"selector":{"matchLabels":{"app":"%s"%(name,)}},"replicas":replicas,"template":{"metadata":{"labels":{"app":"%s"%(name,)}},"spec":{"containers":[{"name":name,"image":"%s"%(tempDict["image"],),"ports":[{"containerPort":tempDict["ports"]}],"volumeMounts":[{"name":tempDict["volumeName"],"mountPath":"%s"%(None,)}]}],"volumes":[{"name":tempDict["volumeName"]}]}}},
}
except NameError:
print("You are missing define name or kind")
return None
try:
with open(r"./deployment.yaml","w") as file:
yaml.safe_dump((yamlDict),file)
print("{}/{} yaml file created. ".format(kind,name))
except Exception as err:
print(err)
try:
file=open("deployment.yaml","r")
print(file.read())
except Exception as err:
print(err)
def createTempDict():
titles=''
index=0
with open(r"./docker-compose.yml") as yFile:
dcFile=yaml.full_load(yFile) # we read yaml file in dict type
if len(list(dcFile["services"]))>1:
print("services:")
for item in list(dcFile["services"]):
print(item )
image=input("You have {} services in your docker-compose.yaml file, please choose a service: ".format(len(list(dcFile["services"]))))
try:
index=list(dcFile["services"]).index(image)
except Exception:
image=input("Please enter your service name correctly")
index=list(dcFile["services"]).index(image)
firstItem=list(dcFile["services"])[int(index)]
#firstItem1=list(dcFile["services"])[1] # first services name from docker-compose
titles=list(dcFile["services"][firstItem])#we will hold subtitles below of big title
#these both variables will help us to access data we need rapidly on dcFile
tempDict={}
for item in titles: # it gets values of titles
value=str(dcFile["services"][firstItem][item])
tempDict[item]=value # created tempDict to use data more efficiently
try:
volumeName=list(dcFile["volumes"])[int(index)] # we took volume name of volume created in compose file
tempDict["volumeName"]=volumeName
except IndexError:
tempDict["volumeName"]=""
try:
ports=tempDict["ports"].split(":",2) #ports were getten in list form we had to convert it to str
ports=ports[1].split("']",2)
tempDict["ports"]=int(ports[0]) #
except KeyError:
tempDict["ports"]=""
return tempDict
if __name__ == "__main__":
main()