create app success

This commit is contained in:
chendelin1982 2021-10-12 19:26:24 +08:00
parent be8dbf112a
commit 3f17cb5fde
5 changed files with 96 additions and 37 deletions

1
cli/c2

@ -1 +0,0 @@
Subproject commit 324c5bc05dd27826e863d31f2e3b0caf84878b2c

View file

@ -1,11 +1,11 @@
import model, os, sys, subprocess
import model, os, sys, subprocess, re
from model import GitOp
path_repo = "./data/application.list"
path_project = ""
# for Git clone HA
github_url = ("https://github.com", "https://github.com.cnpmjs.org", "https://hub.fastgit.org")
github_url = ("https://github.com", "https://hub.fastgit.org", "https://github.com.cnpmjs.org")
class Print:
@ -43,10 +43,42 @@ class Create:
gitop.gitClone(cmd)
def setEnv(self):
'''set the usable port for application'''
fileop=model.FileOp()
print(fileop.fileToJson(self.folder+'/.env'))
pass
'''reset the password | port | container_name for application'''
fileop=model.FileOp(self.folder+'/.env')
securityop=model.SecurityOp()
netop=model.NetOp()
env_dict = fileop.fileToDict()
env_str = fileop.fileToString()
port_list = []
for key in list(env_dict.keys()):
if env_dict[key] in ["", "True", "False"]:
del env_dict[key]
for key,value in env_dict.items():
# replace password
if re.match('\w*PASSWORD',key,re.I) != None:
env_str = env_str.replace(key+"="+value, key+"="+securityop.randomPass())
# replace port
if re.match('\w*PORT',key,re.I) != None:
port = int(value)
while port in port_list or not netop.checkPort(port):
port = port + 1
port_list.append(port)
env_str = env_str.replace(key+"="+value, key+"="+str(port))
# replace app_container
if re.match('\w*APP_CONTAINER_NAME',key,re.I) != None:
env_str = env_str.replace(key+"="+value, key+"="+self.folder)
# replace app_network
if re.match('\w*APP_NETWORK',key,re.I) != None:
env_str = env_str.replace(key+"="+value, key+"="+self.folder)
fileop.stringToFile(env_str)
def upRepo(self):
'''docker-compose up repository'''
@ -57,6 +89,4 @@ class Create:
os.system(cmd)
def printResult(self):
pass
pass

View file

@ -43,27 +43,36 @@ class GitOp:
class FileOp:
'''File operation'''
def __init__(self):
pass
def __init__(self, path: str):
self.path = path
def printJson(self, path: str):
def printFile(self):
'''output file content'''
with open(path,newline='') as file:
with open(self.path,newline='') as file:
print(file.read())
def fileToString(self):
'''read file content'''
with open(self.path,'r') as file:
return file.read()
def stringToFile(self, content: Optional[str] = ""):
'''string content to file'''
with open(self.path,'w+') as file:
return file.write(content)
file.close()
def fileToJson(self, path: str, remark: Optional[str] = "#", separate: Optional[str] = "="):
def fileToDict(self, remark: Optional[str] = "#", separate: Optional[str] = "="):
''' convert file to Json '''
dict = {}
with open(path) as fh:
with open(self.path) as fh:
for line in fh:
if line == "\n":
continue
if line.find(remark) != 0:
item, value = line.strip().split(separate, -1)
if value != "":
dict[item] = value
dict[item] = value
else:
continue
fh.close()
@ -79,20 +88,11 @@ class NetOp:
def checkPort(self, port: int):
'''check the target port's status'''
search_key = "port="+str(port)
if str(+psutil.net_connections()).find(search_key) != -1:
if str(psutil.net_connections()).find(search_key) != -1:
print(str(port)+" is used")
return False
else:
print(str(port)+" is free")
return True
def setPort(self, port: int):
'''set usable port'''
while self.checkPort(port) == False:
port=port+1
print(port)
return port
class SecurityOp:
'''Password and security operation'''
@ -110,4 +110,4 @@ class SecurityOp:
and any(c.isupper() for c in password)
and sum(c.isdigit() for c in password) >= 3):
break
print(password)
return password

@ -1 +0,0 @@
Subproject commit 8531fd12cacfb9881de3c270f2b81e17b20f5a99

View file

@ -1,10 +1,41 @@
import model,re
env = {}
fileop=model.FileOp()
env = fileop.fileToJson('c2/.env')
fileop=model.FileOp('c2/.env')
securityop=model.SecurityOp()
netop=model.NetOp()
for key,value in env.items():
if re.match(pattern,key,re.I) != None:
print(value)
env_dict = fileop.fileToDict()
env_str = fileop.fileToString()
port_list = []
for key in list(env_dict.keys()):
if env_dict[key] in ["", "True", "False"]:
del env_dict[key]
for key,value in env_dict.items():
# replace password
if re.match('\w*PASSWORD',key,re.I) != None:
env_str = env_str.replace(key+"="+value, key+"="+securityop.randomPass())
# replace port
if re.match('\w*PORT',key,re.I) != None:
port = int(value)
while port in port_list or not netop.checkPort(port):
port = port + 1
port_list.append(port)
print(port_list)
env_str = env_str.replace(key+"="+value, key+"="+netop.setPort(int(port)))
# replace app_container
if re.match('\w*APP_CONTAINER_NAME',key,re.I) != None:
env_str = env_str.replace(key+"="+value, key+"="+"hello")
# replace app_container
if re.match('\w*APP_NETWORK',key,re.I) != None:
env_str = env_str.replace(key+"="+value, key+"="+"hello")
fileop.stringToFile(env_str)
print(env_str)