From 6367818bd594907fef28c1bf14d66a8927414810 Mon Sep 17 00:00:00 2001 From: Karel van Klink <karel.vanklink@geant.org> Date: Tue, 11 Apr 2023 17:24:35 +0200 Subject: [PATCH] Add initial bash script for running ansible in a single-use venv --- larp/routes/playbook.py | 18 ++---------------- larp/run_playbook.sh | 37 +++++++++++++++++++++++++++++++++++++ requirements.txt | 3 --- setup.py | 2 ++ test/playbook.yml | 4 ++++ 5 files changed, 45 insertions(+), 19 deletions(-) create mode 100755 larp/run_playbook.sh create mode 100644 test/playbook.yml diff --git a/larp/routes/playbook.py b/larp/routes/playbook.py index b4d60a7..efcc838 100644 --- a/larp/routes/playbook.py +++ b/larp/routes/playbook.py @@ -5,10 +5,6 @@ from fastapi import APIRouter from fastapi.responses import JSONResponse from pydantic import BaseModel -import ansible_runner -import venv -import tempfile - from larp import config router = APIRouter() @@ -48,7 +44,7 @@ async def add_playbook(new_playbook: Playbook) -> Playbook: @router.get('/run/{playbook_name}') -async def run_playbook(playbook_name: str) -> JSONResponse | PlaybookRun: +async def run_playbook(playbook_name: str) -> PlaybookRun: # TODO: implement params = config.load() playbook_list = os.listdir(params['playbook-dir']) @@ -57,16 +53,6 @@ async def run_playbook(playbook_name: str) -> JSONResponse | PlaybookRun: except ValueError: return JSONResponse(status_code=404, content=f'Playbook with name {playbook_name} not found.') - # Build a temp venv - venv_path = tempfile.mkdtemp(prefix='workflow-', suffix='-venv') - builder = venv.EnvBuilder(with_pip=True, system_site_packages=True) - builder.create(venv_path) - - - # Get collection from Ansible Galaxy - # ansible_collections - - # Run playbook asynchronously - ansible_runner.run_async() + # Run playbook asynchronously with run_playbook.sh return PlaybookRun(result_code=1) diff --git a/larp/run_playbook.sh b/larp/run_playbook.sh new file mode 100755 index 0000000..500dabe --- /dev/null +++ b/larp/run_playbook.sh @@ -0,0 +1,37 @@ +#!/bin/sh + +usage() { + echo "Usage: $0 <larp_collection_uri> <playbook-name> <remote-host> (<extra-vars> ...)" 1>&2; + exit 2 +} + +if [ $# -lt 2 ]; then + echo "Illegal number of parameters" + usage +fi + +VENV_PATH=$(mktemp -d -t larp-venv-) +if [[ ! -d "$VENV_PATH" ]]; then + echo "Unable to create temp directory for venv" + exit 1 +fi + +cleanup() { + rm -rf "$VENV_PATH" + echo "Deleted venv in $VENV_PATH" +} + +# Always delete old venv upon exit +trap cleanup EXIT + +### + +echo "Creating venv in $VENV_PATH" +python3 -m venv "$VENV_PATH" +source "$VENV_PATH/bin/activate" +pip install -r ../requirements.txt # FIXME +pip install ansible ansible_runner + +ansible-galaxy collection install "$1" +ansible-playbook "$2" -i "$3", --connection=local # TODO: Add support for extra vars +# FIXME: remove --conection=local from command above diff --git a/requirements.txt b/requirements.txt index 40613c9..53d10f3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,6 @@ jsonschema requests -ansible -ansible-runner -httpx fastapi pydantic uvicorn[standard] diff --git a/setup.py b/setup.py index 061160c..0a52ae1 100644 --- a/setup.py +++ b/setup.py @@ -11,5 +11,7 @@ setup( install_requires=[ 'jsonschema', 'requests', + 'fastapi', + 'pydantic' ] ) diff --git a/test/playbook.yml b/test/playbook.yml new file mode 100644 index 0000000..148b72e --- /dev/null +++ b/test/playbook.yml @@ -0,0 +1,4 @@ +- name: test-playbook + hosts: all + roles: + - kvklink.echo.echo_uptime -- GitLab