diff --git a/.github/workflows/ci_test_api_service.yml b/.github/workflows/ci_test_api_service.yml new file mode 100644 index 0000000..07bc7c6 --- /dev/null +++ b/.github/workflows/ci_test_api_service.yml @@ -0,0 +1,44 @@ +name: Test API Service + +on: + push: + branches: + - main + pull_request: + branches: + - "*" + +jobs: + test_api_service: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Build and run Docker Compose + run: | + docker-compose build + docker-compose up -d + + - name: Wait for API to start + run: sleep 10 + + - name: Test Quote Disp Health + run: | + status=$(curl --write-out %{http_code} --silent --output /dev/null http://localhost:5001/health) + if [[ "$status" -ne 200 ]]; then + echo "API endpoint returned $status" + exit 1 + fi + + - name: Test Quote Gen Health + run: | + status=$(curl --write-out %{http_code} --silent --output /dev/null http://localhost:5000/health) + if [[ "$status" -ne 200 ]]; then + echo "API endpoint returned $status" + exit 1 + fi + + - name: Stop Docker Compose + run: docker-compose down diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..579e82e --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,23 @@ +version: "3.7" +services: + web1: + build: ./quote_gen + deploy: + replicas: 1 + ports: + - "5000:5000" + restart: always + volumes: + - ./quote_gen:/app:rw + + web2: + build: ./quote_disp + deploy: + replicas: 1 + ports: + - "5001:5001" + restart: always + volumes: + - ./quote_disp:/app:rw + depends_on: + - web1 diff --git a/quote_disp/Dockerfile b/quote_disp/Dockerfile new file mode 100644 index 0000000..db26812 --- /dev/null +++ b/quote_disp/Dockerfile @@ -0,0 +1,6 @@ +FROM python:3.8-slim-buster +COPY . /app +WORKDIR /app +RUN pip install -r requirements.txt +ENTRYPOINT [ "python" ] +CMD [ "app.py" ] \ No newline at end of file diff --git a/quote_disp/app.py b/quote_disp/app.py index 18b3844..02af0cc 100644 --- a/quote_disp/app.py +++ b/quote_disp/app.py @@ -8,7 +8,7 @@ @app.route("/health") def health(): - return "healthy" + return "healthy time" @app.route("/") @@ -18,8 +18,16 @@ def home(): @app.route("/get_quote") def quote(): - quote = requests.get("http://gen:5000/quote").text - print("quote - ", quote) + hosts = [ + "http://week2-devops-web1-1:5000/quote", + "http://week2-devops-web1-2:5000/quote" + ] + quote = "Quote Service is unavailable" + for host in hosts: + r = requests.get(host) + if r.status_code ==200: + quote = r.text + break return render_template("quote.html", quote=quote) diff --git a/quote_gen/Dockerfile b/quote_gen/Dockerfile new file mode 100644 index 0000000..db26812 --- /dev/null +++ b/quote_gen/Dockerfile @@ -0,0 +1,6 @@ +FROM python:3.8-slim-buster +COPY . /app +WORKDIR /app +RUN pip install -r requirements.txt +ENTRYPOINT [ "python" ] +CMD [ "app.py" ] \ No newline at end of file