websoft9/cli/model.py

163 lines
4.6 KiB
Python
Raw Normal View History

2021-09-26 07:22:38 +00:00
2021-10-17 14:47:15 +00:00
import os, io, sys, platform, psutil, json, secrets, string, docker
2021-09-26 07:22:38 +00:00
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Type, Union
import urllib.request
2021-09-28 10:34:18 +00:00
2021-09-26 07:22:38 +00:00
class SmoothUrl:
2021-09-28 10:34:18 +00:00
''' Get the best smooth url for Git or Download'''
2021-09-26 07:22:38 +00:00
def __init__(self):
pass
2021-10-07 09:36:09 +00:00
def res(self, url_list: Tuple):
2021-09-26 07:22:38 +00:00
for item in url_list:
try:
urllib.request.urlopen(item,timeout=3).read()
2021-09-28 10:34:18 +00:00
print("Smooth URL is: " + item)
2021-09-26 07:22:38 +00:00
return item
2021-09-28 10:34:18 +00:00
except:
2021-09-26 07:22:38 +00:00
continue
2021-09-28 10:34:18 +00:00
print("Necessary resource URL can not reachable, system exit!")
sys.exit(0)
2021-09-26 07:22:38 +00:00
2021-09-28 10:34:18 +00:00
class GitOp:
'''Git operation'''
2021-09-26 07:22:38 +00:00
def __init__(self):
pass
2021-09-29 10:24:23 +00:00
def gitClone(self, cmd: str):
2021-09-28 10:34:18 +00:00
'''git clone'''
try:
print("Command is "+cmd)
os.system(cmd)
except:
print("Git clone failed, try again and check your URL can be accessed")
sys.exit(0)
2021-09-26 09:57:34 +00:00
class FileOp:
'''File operation'''
2021-10-12 11:26:24 +00:00
def __init__(self, path: str):
self.path = path
2021-09-26 09:57:34 +00:00
2021-10-12 11:26:24 +00:00
def printFile(self):
2021-09-29 10:24:23 +00:00
'''output file content'''
2021-10-12 11:26:24 +00:00
with open(self.path,newline='') as file:
2021-09-28 10:34:18 +00:00
print(file.read())
2021-10-12 11:26:24 +00:00
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()
2021-09-28 10:34:18 +00:00
2021-10-12 11:26:24 +00:00
def fileToDict(self, remark: Optional[str] = "#", separate: Optional[str] = "="):
2021-09-29 10:24:23 +00:00
''' convert file to Json '''
dict = {}
2021-10-12 11:26:24 +00:00
with open(self.path) as fh:
2021-09-29 10:24:23 +00:00
for line in fh:
2021-10-07 09:36:09 +00:00
if line == "\n":
continue
2021-09-29 10:24:23 +00:00
if line.find(remark) != 0:
item, value = line.strip().split(separate, -1)
2021-10-12 11:26:24 +00:00
dict[item] = value
2021-09-29 10:24:23 +00:00
else:
continue
fh.close()
2021-10-07 09:36:09 +00:00
return dict
2021-09-28 10:34:18 +00:00
class NetOp:
'''Network and port manage'''
def __init__(self):
pass
def checkPort(self, port: int):
'''check the target port's status'''
search_key = "port="+str(port)
2021-10-12 11:26:24 +00:00
if str(psutil.net_connections()).find(search_key) != -1:
2021-09-28 10:34:18 +00:00
print(str(port)+" is used")
return False
else:
return True
class SecurityOp:
'''Password and security operation'''
def __int__(self):
pass
def randomPass(self, length: Optional[int] = 16):
2021-09-29 10:24:23 +00:00
'''set strong password'''
2021-09-28 10:34:18 +00:00
alphabet = string.ascii_letters + string.digits
while True:
password = ''.join(secrets.choice(alphabet) for i in range(length))
if (any(c.islower() for c in password)
and any(c.isupper() for c in password)
and sum(c.isdigit() for c in password) >= 3):
break
2021-10-16 10:57:11 +00:00
return password
class DockerComposeOp:
'''Docker Compose operation'''
2021-11-02 12:54:10 +00:00
def __init__(self, command: str, **parameter):
2021-10-16 10:57:11 +00:00
2021-11-02 12:54:10 +00:00
self.cmd = "docker-compose "+ command
for key, value in parameter.items():
self.cmd = self.cmd + " --" + key + " " + value
2021-10-16 10:57:11 +00:00
try:
2021-11-02 12:54:10 +00:00
os.chdir(self.parameter[project-directory])
2021-10-16 10:57:11 +00:00
except:
2021-11-01 11:23:17 +00:00
print("Not found project directory")
2021-10-16 10:57:11 +00:00
sys.exit(0)
2021-11-02 12:54:10 +00:00
def execute(self):
'''docker-compose command executing'''
2021-10-16 10:57:11 +00:00
try:
2021-11-02 12:54:10 +00:00
os.system(self.cmd)
2021-10-16 10:57:11 +00:00
except:
2021-11-02 12:54:10 +00:00
print("This operation execute failed")
2021-10-16 10:57:11 +00:00
sys.exit(0)
2021-11-02 12:54:10 +00:00
2021-10-16 10:57:11 +00:00
class DockerOp:
''' Docker operation '''
2021-10-18 11:07:41 +00:00
def __init__(self, status: Optional[str] = 'all'):
2021-10-17 14:47:15 +00:00
self.client = docker.from_env()
2021-10-18 11:07:41 +00:00
self.status = status
2021-10-16 10:57:11 +00:00
2021-10-17 14:47:15 +00:00
def lsContainer(self):
container_list = []
2021-10-18 11:07:41 +00:00
for container in self.client.containers.list(self.status):
2021-10-17 14:47:15 +00:00
container_list.append(container.name)
return container_list
2021-10-18 11:07:41 +00:00
def getProject(self):
2021-10-17 14:47:15 +00:00
project_dict = {}
for name in self.lsContainer():
2021-10-18 11:07:41 +00:00
project_dict[self.client.containers.get(name).labels['com.docker.compose.project']] = self.client.containers.get(name).labels['com.docker.compose.project.working_dir']
return project_dict
def lsProject(self):
'''list all project and path'''
for key, value in self.getProject().items():
print(key,value)