Adden create and delete environment
This commit is contained in:
Binary file not shown.
@ -0,0 +1,36 @@
|
||||
Containers:
|
||||
- name: nlcdcds1
|
||||
cpu: 2
|
||||
memory: 2
|
||||
image:
|
||||
version:
|
||||
- name: nlcdcds2
|
||||
cpu: 2
|
||||
memory: 2
|
||||
image:
|
||||
version:
|
||||
- name: nlcdcrs2
|
||||
cpu: 2
|
||||
memory: 2
|
||||
image:
|
||||
version:
|
||||
- name: nlcdcrs3
|
||||
cpu: 2
|
||||
memory: 2
|
||||
image:
|
||||
version:
|
||||
- name: brcdcds1
|
||||
cpu: 2
|
||||
memory: 2
|
||||
image:
|
||||
version:
|
||||
- name: brcdcds2
|
||||
cpu: 2
|
||||
memory: 2
|
||||
image:
|
||||
version:
|
||||
- name: brcdcrs5
|
||||
cpu: 2
|
||||
memory: 2
|
||||
image:
|
||||
version:
|
||||
|
||||
37
main.py
37
main.py
@ -18,6 +18,7 @@ uri = "unix:///run/user/1000/podman/podman.sock"
|
||||
podmanapi = PodmanClient(base_url=uri)
|
||||
|
||||
yaml_file = open("containers.yaml", 'r')
|
||||
yaml_content = yaml.load(yaml_file,Loader=yaml.FullLoader)
|
||||
|
||||
class Post(BaseModel):
|
||||
title: str
|
||||
@ -85,10 +86,15 @@ async def update_post(id: int, post: Post):
|
||||
|
||||
@app.post("/create_env", status_code=status.HTTP_201_CREATED)
|
||||
async def create_env(env: Environment):
|
||||
cmd = subprocess.run(["cat","list"],stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True, text=True)
|
||||
env_dict = env.dict()
|
||||
#print(env_dict)
|
||||
#cmd = subprocess.run(["cat","list"],stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True, text=True)
|
||||
# store the output in the list
|
||||
output = (f"{cmd.stdout}")
|
||||
return output
|
||||
#output = (f"{cmd.stdout}")
|
||||
for container in yaml_content['Containers']:
|
||||
#print(container['name'],'-', env_dict['username'],'-',env_dict['uid'],sep='')
|
||||
container = podmanapi.containers.run(image="docker.io/library/nginx:latest",name=container['name']+'-'+env_dict['username']+'-'+env_dict['uid'],detach=True,publish_all_ports=True)
|
||||
return
|
||||
|
||||
|
||||
@app.get("/environment/{id}")
|
||||
@ -99,13 +105,16 @@ async def get_env(id: str):
|
||||
else:
|
||||
return {"environment_detail": cmd}
|
||||
|
||||
@app.delete("/environment/{id}")
|
||||
async def get_env(id: str):
|
||||
cmd = subprocess.run(["cat list | grep %s" % id],stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
|
||||
if cmd.returncode != 0:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Post id {id} not found")
|
||||
else:
|
||||
return {"Deleted environment": {id}}
|
||||
@app.delete("/delete_env", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def get_env(env: Environment):
|
||||
env_dict = env.dict()
|
||||
#cmd = subprocess.run(["cat list | grep %s" % id],stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
|
||||
for container in yaml_content['Containers']:
|
||||
container = podmanapi.containers.remove(container_id=container['name']+'-'+env_dict['username']+'-'+env_dict['uid'],force=True,v=True)
|
||||
#if container:
|
||||
# raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Post id {id} not found")
|
||||
#else:
|
||||
return
|
||||
|
||||
|
||||
@app.get("/list")
|
||||
@ -123,11 +132,15 @@ async def ping_host():
|
||||
print (output2)
|
||||
#for image in podmanapi.images.list():
|
||||
# print(image, image.id, "\n")
|
||||
#for key, value in yaml_content.items():
|
||||
# print(f"{key}: {value}")
|
||||
image = podmanapi.images.pull("docker.io/nginx", tag="latest")
|
||||
for container in yaml_content['Containers']:
|
||||
container = podmanapi.containers.run(image="docker.io/library/nginx:latest",name=container['name'],detach=True,publish_all_ports=True)
|
||||
#output = podmanapi.images.list()
|
||||
#json_str = json.dumps(podmanapi.df(), indent=4)
|
||||
image = podmanapi.images.pull("docker.io/nginx", tag="latest")
|
||||
#container = podmanapi.containers.create(image="docker.io/library/nginx:latest",name='test',detach=True,publish_all_ports=True)
|
||||
container = podmanapi.containers.run(image="docker.io/library/nginx:latest",name='test',detach=True,publish_all_ports=True)
|
||||
#container = podmanapi.containers.run(image="docker.io/library/nginx:latest",name='test',detach=True,publish_all_ports=True)
|
||||
#run_container = podmanapi.containers.run(image="docker.io/library/nginx:latest",name='test',stdout=True, stderr=False)
|
||||
#json_str = json.dumps(podmanapi.containers.list(), indent=4)
|
||||
#print(json.dumps(podmanapi.version(), indent=4))
|
||||
|
||||
Reference in New Issue
Block a user