Adding swarm-connectivity-tester
This commit is contained in:
parent
c1d9ba18c5
commit
8761546c3c
9 changed files with 1398 additions and 0 deletions
119
.github/workflows/swarm-connectivity-tester.yml
vendored
Normal file
119
.github/workflows/swarm-connectivity-tester.yml
vendored
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
name: Build Swarm Connectivity Tester
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
paths:
|
||||
- swarm-connectivity-tester/**
|
||||
- .github/workflows/swarm-connectivity-tester.yml
|
||||
workflow_call:
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: swarm-connectivity-tester-${{ github.head_ref || github.run_id }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
swarm-connectivity-tester-build:
|
||||
name: Build Swarm Connectivity Tester
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: "Setup: Checkout Source"
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
sparse-checkout: |
|
||||
swarm-connectivity-tester
|
||||
|
||||
- name: "Setup: PHP"
|
||||
uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: 8.2
|
||||
env:
|
||||
runner: self-hosted
|
||||
|
||||
- name: "Setup: Setup QEMU"
|
||||
uses: docker/setup-qemu-action@v3
|
||||
|
||||
- name: "Setup: Expose GitHub Runtime"
|
||||
uses: crazy-max/ghaction-github-runtime@v3
|
||||
|
||||
- name: "Setup: Setup Docker Buildx"
|
||||
uses: docker/setup-buildx-action@v2
|
||||
|
||||
- name: "Setup: Login to Docker Hub"
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
username: matthewbaggett
|
||||
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
|
||||
|
||||
- name: "Setup: Login to GHCR"
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: matthewbaggett
|
||||
password: ${{ secrets.GHCR_PASSWORD }}
|
||||
|
||||
- name: "Setup: Find Composer Cache Directory"
|
||||
id: composer-cache
|
||||
working-directory: swarm-connectivity-tester
|
||||
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: "Setup: Composer Cache"
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: ${{ steps.composer-cache.outputs.dir }}
|
||||
key: ${{ runner.os }}-swarm-connectivity-tester-composer-${{ hashFiles('**/composer.lock') }}
|
||||
restore-keys: ${{ runner.os }}-swarm-connectivity-tester-composer-
|
||||
|
||||
- name: "Dependencies: Composer Install"
|
||||
working-directory: swarm-connectivity-tester
|
||||
run: composer install --ignore-platform-reqs
|
||||
|
||||
- name: "Build: Build & Push Target Image"
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: swarm-connectivity-tester
|
||||
target: connect-target
|
||||
platforms: ${{ !env.ACT && 'linux/amd64,linux/arm64' || 'linux/amd64' }}
|
||||
pull: true
|
||||
push: true
|
||||
build-args: |
|
||||
GIT_SHA=${{ github.sha }}
|
||||
GIT_BUILD_ID=${{ github.ref_name }}
|
||||
BUILD_DATE=${{ steps.date.outputs.container_build_datetime }}
|
||||
GIT_COMMIT_MESSAGE=${{ github.event.head_commit.message }}
|
||||
tags: |
|
||||
${{ !env.ACT && 'benzine/swarm-connectivity-tester:target' || '' }}
|
||||
${{ !env.ACT && 'ghcr.io/benzine-framework/swarm-connectivity-tester:target' || 'ghcr.io/benzine-framework/swarm-connectivity-tester:target-devel' }}
|
||||
cache-from: ${{ !env.ACT && 'type=gha' || '' }}
|
||||
cache-to: ${{ !env.ACT && 'type=gha,mode=max' || '' }}
|
||||
build-contexts: |
|
||||
php:cli=docker-image://ghcr.io/benzine-framework/php:cli-8.2
|
||||
php:nginx=docker-image://ghcr.io/benzine-framework/php:nginx-8.2
|
||||
|
||||
- name: "Build: Build & Push Report Image"
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: swarm-connectivity-tester
|
||||
target: connect-reporter
|
||||
platforms: ${{ !env.ACT && 'linux/amd64,linux/arm64' || 'linux/amd64' }}
|
||||
pull: true
|
||||
push: true
|
||||
build-args: |
|
||||
GIT_SHA=${{ github.sha }}
|
||||
GIT_BUILD_ID=${{ github.ref_name }}
|
||||
BUILD_DATE=${{ steps.date.outputs.container_build_datetime }}
|
||||
GIT_COMMIT_MESSAGE=${{ github.event.head_commit.message }}
|
||||
tags: |
|
||||
${{ !env.ACT && 'benzine/swarm-connectivity-tester:reporter' || '' }}
|
||||
${{ !env.ACT && 'ghcr.io/benzine-framework/swarm-connectivity-tester:reporter' || 'ghcr.io/benzine-framework/swarm-connectivity-tester:reporter-devel' }}
|
||||
cache-from: ${{ !env.ACT && 'type=gha' || '' }}
|
||||
cache-to: ${{ !env.ACT && 'type=gha,mode=max' || '' }}
|
||||
build-contexts: |
|
||||
php:cli=docker-image://ghcr.io/benzine-framework/php:cli-8.2
|
||||
php:nginx=docker-image://ghcr.io/benzine-framework/php:nginx-8.2
|
||||
30
swarm-connectivity-tester/Dockerfile
Normal file
30
swarm-connectivity-tester/Dockerfile
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
# checkov:skip=CKV_DOCKER_3 user cannot be determined at this stage.
|
||||
FROM php:nginx as connect-target
|
||||
LABEL maintainer="Matthew Baggett <matthew@baggett.me>" \
|
||||
org.label-schema.vcs-url="https://github.com/benzine-framework/docker" \
|
||||
org.opencontainers.image.source="https://github.com/benzine-framework/docker"
|
||||
RUN echo -e "#!/bin/bash\n\ntail -f /var/log/php8.2-fpm.log" > /etc/service/logs-phpfpm-error/run && \
|
||||
chmod +x /etc/service/logs-phpfpm-error/run
|
||||
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
|
||||
WORKDIR /app
|
||||
HEALTHCHECK --interval=30s --timeout=3s \
|
||||
CMD curl -f http://localhost/ping.php || exit 1
|
||||
COPY ./public-target /app/public
|
||||
|
||||
# checkov:skip=CKV_DOCKER_3 user cannot be determined at this stage.
|
||||
FROM php:nginx as connect-reporter
|
||||
LABEL maintainer="Matthew Baggett <matthew@baggett.me>" \
|
||||
org.label-schema.vcs-url="https://github.com/benzine-framework/docker" \
|
||||
org.opencontainers.image.source="https://github.com/benzine-framework/docker"
|
||||
RUN echo -e "#!/bin/bash\n\ntail -f /var/log/php8.2-fpm.log" > /etc/service/logs-phpfpm-error/run && \
|
||||
chmod +x /etc/service/logs-phpfpm-error/run
|
||||
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
|
||||
WORKDIR /app
|
||||
HEALTHCHECK --interval=30s --timeout=3s \
|
||||
CMD curl -f http://localhost/ping.php || exit 1
|
||||
COPY ./composer.* /app/
|
||||
COPY ./vendor /app/vendor
|
||||
RUN composer install -q
|
||||
|
||||
COPY ./public-reporter /app/public
|
||||
|
||||
20
swarm-connectivity-tester/composer.json
Normal file
20
swarm-connectivity-tester/composer.json
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"name": "benzine/swarm-connectivity-tester",
|
||||
"type": "project",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Benzine\\SwarmConnectivityTester\\": "src/"
|
||||
}
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "Matthew Baggett",
|
||||
"email": "matthew@baggett.me"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"guzzlehttp/guzzle": "^7.8",
|
||||
"vlucas/phpdotenv": "^5.6",
|
||||
"kint-php/kint": "^5.1"
|
||||
}
|
||||
}
|
||||
1140
swarm-connectivity-tester/composer.lock
generated
Normal file
1140
swarm-connectivity-tester/composer.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
27
swarm-connectivity-tester/docker-compose.yml
Normal file
27
swarm-connectivity-tester/docker-compose.yml
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
version: "3.8"
|
||||
services:
|
||||
alive-target:
|
||||
deploy:
|
||||
replicas: 3
|
||||
build:
|
||||
context: .
|
||||
target: connect-target
|
||||
additional_contexts:
|
||||
- php:nginx=docker-image://ghcr.io/benzine-framework/php:nginx-8.2
|
||||
image: ghcr.io/benzine-framework/swarm-connectivity-tester:target
|
||||
|
||||
reporter:
|
||||
build:
|
||||
context: .
|
||||
target: connect-reporter
|
||||
additional_contexts:
|
||||
- php:nginx=docker-image://ghcr.io/benzine-framework/php:nginx-8.2
|
||||
image: ghcr.io/benzine-framework/swarm-connectivity-tester:reporter
|
||||
ports:
|
||||
- "127.0.0.6:8080:80"
|
||||
environment:
|
||||
TARGETS: "alive-target,dead-target"
|
||||
DEBUG: "true"
|
||||
volumes:
|
||||
- ./:/app
|
||||
- ./public-reporter:/app/public
|
||||
52
swarm-connectivity-tester/public-reporter/index.php
Normal file
52
swarm-connectivity-tester/public-reporter/index.php
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
use GuzzleHttp\Promise\Utils;
|
||||
|
||||
require_once("../vendor/autoload.php");
|
||||
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . "/../");
|
||||
$dotenv->safeLoad();
|
||||
|
||||
$targets = explode(",", $_ENV['TARGETS']);
|
||||
# trim whitespace from each target
|
||||
$targets = array_map('trim', $targets);
|
||||
|
||||
# For each $target, resolve the target to IP addresses
|
||||
foreach($targets as $target){
|
||||
$targetIps[$target] = gethostbynamel($target) ?: [];
|
||||
$targetIps[$target] = array_values($targetIps[$target]);
|
||||
}
|
||||
# For each $target, Create a guzzle request to get the status of each target
|
||||
$guzzle = new GuzzleHttp\Client();
|
||||
$promises = [];
|
||||
foreach ($targets as $target) {
|
||||
$url = "http://$target:80/";
|
||||
$promises[$target] = $guzzle->getAsync($url);
|
||||
}
|
||||
# Wait for all the requests to complete
|
||||
$responses = Utils::settle($promises)->wait();
|
||||
$rollup = true;
|
||||
$json = [];
|
||||
foreach($responses as $target => $response){
|
||||
if(!isset($response['value']) || $response['value']->getStatusCode() != 200) {
|
||||
$rollup = false;
|
||||
if ($response['reason'] instanceof \Exception) {
|
||||
$json[$target] = ['Status' => 'ERROR', 'Reason' => $response['reason']->getMessage()];
|
||||
}else {
|
||||
$json[$target] = ['Status' => "ERROR", 'Reason' => 'Unknown'];
|
||||
}
|
||||
}else{
|
||||
$json[$target] = json_decode($response['value']->getBody()->getContents(), true);
|
||||
}
|
||||
$json[$target]['IP'] = $targetIps[$target];
|
||||
|
||||
}
|
||||
if(!$rollup){
|
||||
header("HTTP/1.0 500 Internal Server Error");
|
||||
}
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode([
|
||||
'Status' => $rollup ? "OK" : "ERROR",
|
||||
'Hostname' => gethostname(),
|
||||
'Targets' => $json,
|
||||
]);
|
||||
2
swarm-connectivity-tester/public-reporter/ping.php
Normal file
2
swarm-connectivity-tester/public-reporter/ping.php
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
<?php
|
||||
echo json_encode(['Pong',]);
|
||||
6
swarm-connectivity-tester/public-target/index.php
Normal file
6
swarm-connectivity-tester/public-target/index.php
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode([
|
||||
'Status' => 'OK',
|
||||
'Hostname' => gethostname(),
|
||||
]);
|
||||
2
swarm-connectivity-tester/public-target/ping.php
Normal file
2
swarm-connectivity-tester/public-target/ping.php
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
<?php
|
||||
echo json_encode(['Pong',]);
|
||||
Loading…
Reference in a new issue