docker sdk

This commit is contained in:
chendelin1982 2021-10-16 18:57:11 +08:00
parent 3f17cb5fde
commit dd66f49cc3
4 changed files with 72 additions and 46 deletions

View file

@ -83,10 +83,21 @@ class Create:
def upRepo(self):
'''docker-compose up repository'''
cmd = "docker-compose up -d"
print(cmd)
cmd_up = "docker-compose up -d"
cmd_down = "docker-compose down -v"
os.chdir(self.folder)
os.system(cmd)
try:
os.system(cmd_up)
except:
print("Create failed")
os.system(cmd_down)
sys.exit(0)
def printResult(self):
pass
class Manage(model.DockerComposeOp):
pass

View file

@ -111,3 +111,52 @@ class SecurityOp:
and sum(c.isdigit() for c in password) >= 3):
break
return password
class DockerComposeOp:
'''Docker Compose operation'''
def __int__(self, path: Optional[str] = ""):
self.cmd_up = "docker-compose up -d"
self.cmd_stop = "docker-compose stop"
self.cmd_down = "docker-compose down"
self.path = path
try:
os.chdir(self.path)
except:
print("No found project directory")
sys.exit(0)
def up(self):
'''docker-compose up'''
try:
os.system(cmd_up)
except:
print("Create failed")
os.system(cmd_up)
sys.exit(0)
def stop(self):
'''docker-compose stop'''
try:
os.system(cmd_stop)
except:
print("Stop failed, suggest try it again")
sys.exit(0)
def down(self):
'''docker-compose down'''
try:
os.system(cmd_down)
except:
print("Down failed, suggest try it again")
sys.exit(0)
class DockerOp:
''' Docker operation '''
def __init__(self):
pass

View file

@ -1,3 +1,5 @@
typer
psutil
jq
docker
docker-compose

View file

@ -1,41 +1,5 @@
import model,re
fileop=model.FileOp('c2/.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)
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)
import docker
client = docker.from_env()
for image in client.images.list():
print(image.id)