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 from model import GitOp
path_repo = "./data/application.list" path_repo = "./data/application.list"
path_project = "" path_project = ""
# for Git clone HA # 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: class Print:
@ -43,10 +43,42 @@ class Create:
gitop.gitClone(cmd) gitop.gitClone(cmd)
def setEnv(self): def setEnv(self):
'''set the usable port for application''' '''reset the password | port | container_name for application'''
fileop=model.FileOp()
print(fileop.fileToJson(self.folder+'/.env')) fileop=model.FileOp(self.folder+'/.env')
pass 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): def upRepo(self):
'''docker-compose up repository''' '''docker-compose up repository'''
@ -58,5 +90,3 @@ class Create:
def printResult(self): def printResult(self):
pass pass

View File

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

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

View File

@ -1,10 +1,41 @@
import model,re import model,re
env = {} fileop=model.FileOp('c2/.env')
fileop=model.FileOp() securityop=model.SecurityOp()
env = fileop.fileToJson('c2/.env') netop=model.NetOp()
for key,value in env.items():
if re.match(pattern,key,re.I) != None: env_dict = fileop.fileToDict()
print(value) 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)