websoft9/cli/controller.py

149 lines
4.4 KiB
Python
Raw Normal View History

2021-11-02 12:54:10 +00:00
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Type, Union
2021-10-12 11:26:24 +00:00
import model, os, sys, subprocess, re
2021-09-28 10:34:18 +00:00
from model import GitOp
2021-09-26 09:57:34 +00:00
path_repo = "./data/application.list"
path_project = ""
2021-09-26 15:05:59 +00:00
# for Git clone HA
2021-10-12 11:26:24 +00:00
github_url = ("https://github.com", "https://hub.fastgit.org", "https://github.com.cnpmjs.org")
2021-09-26 15:05:59 +00:00
2021-09-26 09:57:34 +00:00
class Print:
2021-10-18 11:07:41 +00:00
'''display all any information you want'''
2021-10-31 10:00:02 +00:00
def __init__(self, status:str):
2021-10-18 11:07:41 +00:00
self.status = status
2021-09-26 09:57:34 +00:00
def printRepo():
2021-09-26 15:05:59 +00:00
model.FileOp.printJson(path_repo)
2021-10-18 11:07:41 +00:00
def lsProject(self):
ls = model.DockerOp(self.status)
ls.lsProject()
2021-09-26 15:05:59 +00:00
2021-09-27 09:32:10 +00:00
class Create:
2021-09-26 15:05:59 +00:00
2021-09-27 09:32:10 +00:00
def __init__(self, app_name: str, project_name: str):
self.folder = None
self.app_name = app_name
self.project_name = project_name
if self.project_name != None:
self.folder = self.project_name
else:
self.folder = self.app_name
2021-09-26 15:05:59 +00:00
2021-09-27 09:32:10 +00:00
def downRepo(self):
2021-09-28 10:34:18 +00:00
'''download repository'''
2021-10-08 07:03:27 +00:00
geturl = model.SmoothUrl()
gitop = model.GitOp()
cmd = "git clone --depth=1 " + geturl.res(github_url) + "/websoft9/docker-" + self.app_name + " " + self.folder
2021-09-27 09:32:10 +00:00
if os.path.exists("./"+self.folder):
print(os.path.abspath(self.folder)+" folder already exists")
2021-09-28 10:34:18 +00:00
sys.exit(0)
2021-09-27 09:32:10 +00:00
else:
2021-10-08 07:03:27 +00:00
gitop.gitClone(cmd)
2021-09-28 10:34:18 +00:00
def setEnv(self):
2021-10-12 11:26:24 +00:00
'''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)
2021-09-27 09:32:10 +00:00
def upRepo(self):
2021-09-28 10:34:18 +00:00
'''docker-compose up repository'''
2021-10-16 10:57:11 +00:00
cmd_up = "docker-compose up -d"
cmd_down = "docker-compose down -v"
2021-09-27 09:32:10 +00:00
os.chdir(self.folder)
2021-10-16 10:57:11 +00:00
try:
os.system(cmd_up)
except:
print("Create failed")
os.system(cmd_down)
sys.exit(0)
2021-09-28 10:34:18 +00:00
def printResult(self):
2021-10-16 10:57:11 +00:00
pass
2021-11-01 11:23:17 +00:00
class Status:
'''The status operation of project, e.g Start | Stop | Restart | Erase'''
2021-11-02 12:54:10 +00:00
def __init__(self, project_name: str, **parameter):
2021-11-01 11:23:17 +00:00
self.project_name = project_name
self.dockerop = model.DockerOp()
projectdict = self.dockerop.getProject()
2021-11-02 12:54:10 +00:00
if self.project_name != None:
2021-11-01 11:23:17 +00:00
try:
self.project_path = projectdict[self.project_name]
except:
print("No this application!")
sys.exit(0)
2021-11-02 12:54:10 +00:00
else
try:
self.project_path = parameter[project-directory]
except:
print("Directory not exist, please check it")
sys.exit(0)
self.dockercomposeop = model.DockerComposeOp(self.project_path)
2021-10-16 10:57:11 +00:00
2021-11-01 11:23:17 +00:00
def stopApp(self):
pass
def upApp(self):
2021-11-02 12:54:10 +00:00
print("It up the application at directory: "+self.project_path)
self.dockercomposeop.execute()
2021-11-01 11:23:17 +00:00
def startApp(self):
pass
2021-11-02 12:54:10 +00:00
def restartApp(self):
self.dockercomposeop.execute()
2021-11-01 11:23:17 +00:00
2021-11-02 12:54:10 +00:00
def deleteApp(self):
self.dockercomposeop.execute()