From 3f17cb5fde3d9eaad685c28cf10b8ddf19b40d84 Mon Sep 17 00:00:00 2001 From: chendelin1982 Date: Tue, 12 Oct 2021 19:26:24 +0800 Subject: [PATCH] create app success --- cli/c2 | 1 - cli/controller.py | 48 +++++++++++++++++++++++++++++++++-------- cli/model.py | 40 +++++++++++++++++----------------- cli/repo/docker-drupal2 | 1 - cli/test.py | 43 ++++++++++++++++++++++++++++++------ 5 files changed, 96 insertions(+), 37 deletions(-) delete mode 160000 cli/c2 delete mode 160000 cli/repo/docker-drupal2 diff --git a/cli/c2 b/cli/c2 deleted file mode 160000 index 324c5bc0..00000000 --- a/cli/c2 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 324c5bc05dd27826e863d31f2e3b0caf84878b2c diff --git a/cli/controller.py b/cli/controller.py index 1b58605d..2dab0e4e 100644 --- a/cli/controller.py +++ b/cli/controller.py @@ -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 \ No newline at end of file diff --git a/cli/model.py b/cli/model.py index 047bd817..230717f8 100644 --- a/cli/model.py +++ b/cli/model.py @@ -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) \ No newline at end of file + return password \ No newline at end of file diff --git a/cli/repo/docker-drupal2 b/cli/repo/docker-drupal2 deleted file mode 160000 index 8531fd12..00000000 --- a/cli/repo/docker-drupal2 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 8531fd12cacfb9881de3c270f2b81e17b20f5a99 diff --git a/cli/test.py b/cli/test.py index 4da663bc..226ee2e1 100644 --- a/cli/test.py +++ b/cli/test.py @@ -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)