added podman, json and yaml

This commit is contained in:
2022-11-27 19:11:46 +01:00
parent 01135dea09
commit 5226e858bb
790 changed files with 114578 additions and 16 deletions

52
main.py
View File

@ -8,16 +8,27 @@ from pydantic import BaseModel
from random import randrange
import os
import subprocess
from podman import PodmanClient
import json
import yaml
app = FastAPI()
uri = "unix:///run/user/1000/podman/podman.sock"
podmanapi = PodmanClient(base_url=uri)
yaml_file = open("containers.yaml", 'r')
class Post(BaseModel):
title: str
content: str
published: bool = True
rating: Optional[int] = None
class Environment(BaseModel):
username: str
uid: str
my_posts= [{"title": "title of post 1", "content": "contents of post 1", "id": 1},{"title": "title of post 2", "content": "contents of post 2", "id": 2}]
def find_post(id):
@ -71,15 +82,9 @@ async def update_post(id: int, post: Post):
my_posts[index] = post_dict
return {"data": post_dict}
@app.get("/create_env")
async def ping_host():
# send one packet of data to the host
# this is specified by '-c 1' in the argument list
outputlist = []
# Iterate over all the servers in the list and ping each server
# get the output as a string
#output = str(os.system(cmd))
#cmd = subprocess.run(["/bin/ls", "-al"],stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True, text=True)
@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)
# store the output in the list
output = (f"{cmd.stdout}")
@ -88,12 +93,20 @@ async def ping_host():
@app.get("/environment/{id}")
async def get_env(id: str):
#env = find_post(id)
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 {"post_detail": cmd}
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.get("/list")
async def ping_host():
@ -106,9 +119,16 @@ async def ping_host():
#cmd = subprocess.run(["/bin/ls", "-al"],stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True, text=True)
cmd = subprocess.run(["cat list"],stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
#cmd = subprocess.run(["ls /usr/bin"],stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
# store the output in the list
#output = (f"{cmd.stdout}")
#output1 = (f'{cmd.stdout.decode("utf-8")}')
output2 = (f'{cmd.stdout.decode("utf-8").lower()}')
print (output2)
return output2
#for image in podmanapi.images.list():
# print(image, image.id, "\n")
#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)
#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))
return Response(content=output2, media_type='application/json')