58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
from fastapi import FastAPI
|
|
from pydantic import BaseModel
|
|
import paramiko
|
|
import pystache
|
|
|
|
app = FastAPI()
|
|
ssh_client = paramiko.SSHClient()
|
|
|
|
deploy_script = 'pull-deploy-api.sh'
|
|
docker_compose_template = 'docker-compose-template.yaml'
|
|
docker_compose_file = 'docker-compose-api.yaml'
|
|
source = 'host'
|
|
destination = '/home/daniel'
|
|
|
|
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
|
|
class Deployment(BaseModel):
|
|
image: str
|
|
env: list
|
|
|
|
@app.post("/deploy")
|
|
async def deploy_image(deploy: Deployment):
|
|
|
|
# Setup for moustache templating
|
|
template = ""
|
|
new_env = []
|
|
for item in deploy.env:
|
|
for key, value in item.items():
|
|
temp = {"key": key, "value": value}
|
|
new_env.append(temp)
|
|
|
|
# Overwrite existing model
|
|
deploy.env = new_env
|
|
|
|
# Read template and render
|
|
with open(f'{source}/{docker_compose_template}', 'r') as f:
|
|
template = pystache.render(f.read(), deploy)
|
|
|
|
# Write rendered template to disk
|
|
with open(f'{source}/{docker_compose_file}', 'w') as f:
|
|
f.write(template)
|
|
|
|
# Connect to host ststem
|
|
ssh_client.connect('192.168.40.22', username='daniel', key_filename='server.pem')
|
|
|
|
# Copy files over
|
|
ftp_client = ssh_client.open_sftp()
|
|
ftp_client.put(f'{source}/{deploy_script}', f'{destination}/{deploy_script}')
|
|
ftp_client.put(f'{source}/{docker_compose_file}', f'{destination}/{docker_compose_file}')
|
|
ftp_client.close()
|
|
|
|
# Execute files
|
|
stdin,stdout,stderr = ssh_client.exec_command(f'chmod +x {destination}/{deploy_script} && bash {destination}/{deploy_script}')
|
|
|
|
# Debug out
|
|
print(stdout.readlines())
|
|
|
|
return {} |