From 70eaa5205af67be9f04a66a88a83883ed6d90c2f Mon Sep 17 00:00:00 2001 From: KLxHunter Date: Wed, 7 Aug 2024 16:53:39 +0700 Subject: [PATCH] init --- 05-spawn-fcgi.sh | 2 + Dockerfile | 9 +++ default.conf | 33 ++++++++++ docker-compose.yml | 19 ++++++ html/50x.html | 19 ++++++ html/azure.sh | 146 +++++++++++++++++++++++++++++++++++++++++++++ html/demo.sh | 11 ++++ html/index.html | 22 +++++++ test.sh | 39 ++++++++++++ 9 files changed, 300 insertions(+) create mode 100644 05-spawn-fcgi.sh create mode 100644 Dockerfile create mode 100644 default.conf create mode 100644 docker-compose.yml create mode 100644 html/50x.html create mode 100755 html/azure.sh create mode 100755 html/demo.sh create mode 100644 html/index.html create mode 100644 test.sh diff --git a/05-spawn-fcgi.sh b/05-spawn-fcgi.sh new file mode 100644 index 0000000..07ee9f1 --- /dev/null +++ b/05-spawn-fcgi.sh @@ -0,0 +1,2 @@ +find /usr/share/nginx/html -type f -iname "*.sh" -exec chmod +x {} \; +/usr/bin/spawn-fcgi -s /var/run/fcgiwrap.socket -M 766 /usr/bin/fcgiwrap \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1bedaf9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM nginx:alpine + +RUN apk add fcgiwrap spawn-fcgi bash + +COPY ./05-spawn-fcgi.sh /docker-entrypoint.d/05-spawn-fcgi.sh + +RUN chmod +x /docker-entrypoint.d/05-spawn-fcgi.sh + +SHELL ["/bin/bash", "-c"] \ No newline at end of file diff --git a/default.conf b/default.conf new file mode 100644 index 0000000..9bdaba9 --- /dev/null +++ b/default.conf @@ -0,0 +1,33 @@ +server { + listen 80; + listen [::]:80; + server_name localhost; + + root /usr/share/nginx/html; + + location / { + index index.html index.htm; + try_files $uri $uri/ /index.html =404; + } + + # added example, any shell script in content root will be executable + location ~ \.sh$ { + gzip off; + fastcgi_pass unix:/var/run/fcgiwrap.socket; + include /etc/nginx/fastcgi_params; + } + + location ~ ^/azure/(.+)/notify$ { + gzip off; + fastcgi_pass unix:/var/run/fcgiwrap.socket; + include /etc/nginx/fastcgi_params; + fastcgi_param PROJECT $1; + fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/azure.sh; + } + + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..31174d7 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,19 @@ +version: '3' + +services: + +# https://mac-blog.org.ua/docker-nginx-shell-script/ +# chmod +x html/*.sh + + nginx-bash: + build: + context: ./ + dockerfile: Dockerfile + image: nginx-bash + container_name: nginx-bash + restart: always + ports: + - 8081:80 + volumes: + - ./html:/usr/share/nginx/html + - ./default.conf:/etc/nginx/conf.d/default.conf \ No newline at end of file diff --git a/html/50x.html b/html/50x.html new file mode 100644 index 0000000..7b56181 --- /dev/null +++ b/html/50x.html @@ -0,0 +1,19 @@ + + + +Error + + + +

An error occurred.

+

Sorry, the page you are looking for is currently unavailable.
+Please try again later.

+

If you are the system administrator of this resource then you should check +the error log for details.

+

Faithfully yours, nginx.

+ + \ No newline at end of file diff --git a/html/azure.sh b/html/azure.sh new file mode 100755 index 0000000..d9c64aa --- /dev/null +++ b/html/azure.sh @@ -0,0 +1,146 @@ +#!/bin/bash + +echo 'Content-Type: text/plain' +echo '' +# echo "PROJECT: $PROJECT" + +object () { + # e.g account + kind=$1 + + # e.g leandroAccount + self=$2 + + shift + shift + + # iterates over the remaining args + for arg in "$@"; do + # e.g name=Leandro becomes ARG_KEY=name ARG_VALUE=Leandro + read ARG_KEY ARG_VALUE <<< $(echo "$arg" | sed -E "s/(\w+)=(.*?)/\1 \2/") + + if [[ ! -z "$ARG_KEY" ]] && [[ ! -z "$ARG_VALUE" ]]; then + # declare the object's state!!!! + # e.g export leandroAccount_balance=100 + export ${self}_$ARG_KEY="$ARG_VALUE" + fi + done +} + +notify () { + for i in `seq 1 30`; + do + local RESPONSE_HTTP_CODE=$(curl --max-time 5 -s -o /dev/null -i -w "%{http_code}" -X POST -H "Authorization: Bearer ${1}" -d "message=${3}" "https://notify-api.line.me/api/notify") + [[ "${RESPONSE_HTTP_CODE}" == "200" ]] && break + sleep 2 + done + curl -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{\"content\": \"$(echo ${3}| sed 's/%0A/\\n/g')\"}" https://discord.com/api/webhooks/${2} +} + +cicd () { + local LINE_TOKEN=$1 + local DISCORD_TOKEN=$2 + local repoNameOrigin=$4 + local branchName=$5 + local BUILD_NUMBER=$6 + local buildVersion=$7 + local GIT_COMMIT=$8 + local JOB_URL=$9 + local BUILD_URL=${10} + + case "$3" in + "success") + local MESSAGE_OPTION="✅✅✅✅✅%0A*CI DONE*" + ;; + "unstable") + local MESSAGE_OPTION="💥💥💥💥💥💥%0A*CI Unstable*" + ;; + "failure") + local MESSAGE_OPTION="🔥🔥🔥🔥🔥🔥%0A*CI Fail*" + ;; + esac + + notify ${LINE_TOKEN} ${DISCORD_TOKEN} "${MESSAGE_OPTION}%0ARepositories: ${repoNameOrigin}%0ABranch: ${branchName}%0ABuild Number: ${BUILD_NUMBER}%0AImage Tag: ${buildVersion}%0ACommit ID: ${GIT_COMMIT}%0AJob URL: ${JOB_URL}%0ABuild Log: ${BUILD_URL}console" +} + +pod () { + local LINE_TOKEN=$1 + local DISCORD_TOKEN=$2 + local POD_NAME=$4 + local POD_IP=$5 + local IMAGE_TAG=$6 + local NODE=$7 + case "$3" in + "start") + local MESSAGE_OPTION="🔆🟢🔆🟢🔆🟢🔆 Service UP" + ;; + "stop") + local MESSAGE_OPTION="☠️🔴☠️🔴☠️🔴☠️ Service DOWN" + ;; + esac + notify ${LINE_TOKEN} ${DISCORD_TOKEN} "${MESSAGE_OPTION}%0APod NAME: ${POD_NAME}%0APod IP: ${POD_IP}%0AImage Tag: ${IMAGE_TAG}%0ANode: ${NODE}" +} + +networkOutbound () { + local LINE_TOKEN=$1 + local DISCORD_TOKEN=$2 + local DURATION_MIN=$3 + local DURATION_SEC=$4 + local NOTIFY_RESPONSE=$(echo $5 | sed 's/-ONLINE/ ✅✅✅✅✅✅/g;s/-OFFLINE/ 🔥🔥🔥🔥🔥🔥/g;s/-:-/ : /g;s/-/ /g;') + notify ${LINE_TOKEN} ${DISCORD_TOKEN} "${DURATION_MIN} min ${DURATION_SEC} sec${NOTIFY_RESPONSE}" +} + +action () { + local LINE_TOKEN=$(eval echo \${${PROJECT}_line_${HTTP_ACTION}}) + local DISCORD_TOKEN=$(eval echo \${${PROJECT}_discord_${HTTP_ACTION}}) + case "${HTTP_ACTION}" in + "cicd") + cicd ${LINE_TOKEN} ${DISCORD_TOKEN} ${HTTP_ACTIONVAL} ${HTTP_REPO} ${HTTP_BRANCH} ${HTTP_BUILDNUM} ${HTTP_BUILDVER} ${HTTP_COMMIT} ${HTTP_JOBURL} ${HTTP_BUILDURL} + ;; + "pod") + pod ${LINE_TOKEN} ${DISCORD_TOKEN} ${HTTP_ACTIONVAL} ${HTTP_PODNAME} ${HTTP_PODIP} ${HTTP_IMAGETAG} ${HTTP_NODE} + ;; + "networkOutbound") + networkOutbound ${LINE_TOKEN} ${DISCORD_TOKEN} ${HTTP_DURATIONMIN} ${HTTP_DURATIONSEC} ${HTTP_NOTIFYRESPONSE} + ;; + *) + echo '{"status":404,"message":"action type not found"}' + exit 0 + ;; + esac +} + +case "${PROJECT}" in + "clm") + object lineToken \ + ${PROJECT}_line \ + cicd=ZcjlGQUzDqJXHSuh1GQACy6dDGMj32CdMW0MLwwA9da \ + pod=ZcjlGQUzDqJXHSuh1GQACy6dDGMj32CdMW0MLwwA9da \ + networkOutbound=ZcjlGQUzDqJXHSuh1GQACy6dDGMj32CdMW0MLwwA9da + object discordToken \ + ${PROJECT}_discord \ + cicd=1270450815580111043/29nP43fgoajLtAHiSbRxRdPjgeE3i-4j4gw5ND4s3mzOsnHjOIeKZS2aZGwlMVkusmta \ + pod=1270450815580111043/29nP43fgoajLtAHiSbRxRdPjgeE3i-4j4gw5ND4s3mzOsnHjOIeKZS2aZGwlMVkusmta \ + networkOutbound=1270450815580111043/29nP43fgoajLtAHiSbRxRdPjgeE3i-4j4gw5ND4s3mzOsnHjOIeKZS2aZGwlMVkusmta + action + ;; + "ncs") + object lineToken \ + ${PROJECT}_line \ + cicd=ZcjlGQUzDqJXHSuh1GQACy6dDGMj32CdMW0MLwwA9da \ + pod=ZcjlGQUzDqJXHSuh1GQACy6dDGMj32CdMW0MLwwA9da \ + networkOutbound=ZcjlGQUzDqJXHSuh1GQACy6dDGMj32CdMW0MLwwA9da + object discordToken \ + ${PROJECT}_discord \ + cicd=1270450815580111043/29nP43fgoajLtAHiSbRxRdPjgeE3i-4j4gw5ND4s3mzOsnHjOIeKZS2aZGwlMVkusmta \ + pod=1270450815580111043/29nP43fgoajLtAHiSbRxRdPjgeE3i-4j4gw5ND4s3mzOsnHjOIeKZS2aZGwlMVkusmta \ + networkOutbound=1270450815580111043/29nP43fgoajLtAHiSbRxRdPjgeE3i-4j4gw5ND4s3mzOsnHjOIeKZS2aZGwlMVkusmta + action + ;; + *) + echo '{"status":404,"message":"project not found"}' + exit 0 + ;; +esac + +echo '{"status":200,"message":"ok"}' \ No newline at end of file diff --git a/html/demo.sh b/html/demo.sh new file mode 100755 index 0000000..2e3fc64 --- /dev/null +++ b/html/demo.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +echo 'Content-Type: text/plain' +echo '' +echo 'Hello, world!' +echo 'KLxHunter' +echo 'Environment:' +env + +# notify_token=ZcjlGQUzDqJXHSuh1GQACy6dDGMj32CdMW0MLwwA9da +# curl -X POST -H "Authorization: Bearer ${notify_token}" -F "message=return of the mount hua sect มาแว๊ววว" https://notify-api.line.me/api/notify \ No newline at end of file diff --git a/html/index.html b/html/index.html new file mode 100644 index 0000000..b74357b --- /dev/null +++ b/html/index.html @@ -0,0 +1,22 @@ + + + +Welcome to nginx! + + + +

Welcome to nginx!

+

If you see this page, the nginx web server is successfully installed and +working. Further configuration is required.

+ +

For online documentation and support please refer to +nginx.org.
+Commercial support is available at +nginx.com.

+

Thank you for using nginx.

+ + \ No newline at end of file diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..1d33788 --- /dev/null +++ b/test.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +Object () { + # e.g account + kind=$1 + + # e.g leandroAccount + self=$2 + + shift + shift + + # iterates over the remaining args + for arg in "$@"; do + # e.g name=Leandro becomes ARG_KEY=name ARG_VALUE=Leandro + read ARG_KEY ARG_VALUE <<< $(echo "$arg" | sed -E "s/(\w+)=(.*?)/\1 \2/") + + if [[ ! -z "$ARG_KEY" ]] && [[ ! -z "$ARG_VALUE" ]]; then + # declare the object's state!!!! + # e.g export leandroAccount_balance=100 + export ${self}_$ARG_KEY="$ARG_VALUE" + fi + done +} + +Object account leandroAccount name=Leandro balance=500 + +echo $leandroAccount_name # prints Leandro +echo $leandroAccount_balance # prints 500 + +Object account carlosAccount name=Carlitos balance=800 + +echo $carlosAccount_name # prints Carlitos +echo $carlosAccount_balance # prints 800 + +# curl -H "ACTION: cicd" -H "ACTIONVAL: success" -H "REPO: complaint-backend-complaint" -H "BRANCH: develop" -H "BUILDNUM: 478" -H "BUILDVER: develop-0.0.478" -H "COMMIT: 3634e06b2c7b93cf146530917a93a30de25943e0" -H "JOBURL: https://jenkins-back.devopsnonprd.vayuktbcs:8080/job/complaint/job/backend/job/complaint-backend-complaint/job/develop/" -H "BUILDURL: https://jenkins-back.devopsnonprd.vayuktbcs:8080/job/complaint/job/backend/job/complaint-backend-complaint/job/develop/478/" -H "Content-Type: application/json" localhost:8081/azure/clm/notify + +# curl -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{\"content\": \"✅✅✅✅✅\n*CI DONE*\nRepositories: ${repoNameOrigin}\nBranch: ${branchName}\nBuild Number: ${BUILD_NUMBER}\nImage Tag: ${buildVersion}\nCommit ID: ${GIT_COMMIT}\nJob URL: ${JOB_URL}\nBuild Log: ${BUILD_URL}console\"}" https://discord.com/api/webhooks/1270450815580111043/29nP43fgoajLtAHiSbRxRdPjgeE3i-4j4gw5ND4s3mzOsnHjOIeKZS2aZGwlMVkusmta +