2021-10-19 15:06:14 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-10-22 16:48:30 +02:00
|
|
|
"strconv"
|
2021-10-19 15:06:14 +02:00
|
|
|
|
|
|
|
"alpha.dagger.io/alpine"
|
|
|
|
"alpha.dagger.io/dagger"
|
2021-11-23 18:33:28 +01:00
|
|
|
"alpha.dagger.io/dagger/op"
|
2021-10-19 15:06:14 +02:00
|
|
|
"alpha.dagger.io/os"
|
|
|
|
)
|
|
|
|
|
|
|
|
#Get: #Do & {method: "GET"}
|
|
|
|
#Post: #Do & {method: "POST"}
|
|
|
|
#Put: #Do & {method: "PUT"}
|
|
|
|
#Delete: #Do & {method: "DELETE"}
|
|
|
|
#Do: {
|
|
|
|
url: string
|
|
|
|
method: "GET" | "POST" | "PUT" | "DELETE" | "PATH" | "HEAD"
|
|
|
|
|
|
|
|
request: {
|
|
|
|
body: string | *""
|
|
|
|
header: [string]: string | [...string]
|
|
|
|
token: dagger.#Secret | *null
|
|
|
|
}
|
|
|
|
|
|
|
|
ctr: os.#Container & {
|
|
|
|
image: alpine.#Image & {
|
|
|
|
package: curl: true
|
|
|
|
package: bash: "=5.1.0-r0"
|
|
|
|
package: jq: "~=1.6"
|
|
|
|
}
|
|
|
|
shell: path: "/bin/bash"
|
|
|
|
always: true
|
|
|
|
|
|
|
|
env: {
|
|
|
|
METHOD: method
|
|
|
|
HEADERS: json.Marshal(request.header)
|
|
|
|
BODY: request.body
|
|
|
|
URL: url
|
|
|
|
}
|
|
|
|
if request.token != null {
|
|
|
|
secret: "/token": request.token
|
|
|
|
}
|
|
|
|
command: #"""
|
|
|
|
curlArgs=(
|
|
|
|
"$URL"
|
|
|
|
-L --fail --silent --show-error
|
|
|
|
--write-out "%{http_code}"
|
|
|
|
-X "$METHOD"
|
|
|
|
-o /response
|
|
|
|
)
|
|
|
|
|
2021-10-22 16:13:55 +02:00
|
|
|
[ -n "$BODY" ] && curlArgs+=("-d" "'$BODY'")
|
|
|
|
|
2021-10-19 15:06:14 +02:00
|
|
|
headers="$(echo $HEADERS | jq -r 'to_entries | map(.key + ": " + (.value | tostring) + "\n") | add')"
|
|
|
|
while read h; do
|
2021-10-22 16:13:55 +02:00
|
|
|
curlArgs+=("-H" "'$h'")
|
2021-10-19 15:06:14 +02:00
|
|
|
done <<< "$headers"
|
|
|
|
if [ -e /token ]; then
|
|
|
|
curlArgs+=("-H" "Authorization: bearer $(cat /token)")
|
|
|
|
fi
|
|
|
|
|
|
|
|
curl "${curlArgs[@]}" > /status
|
|
|
|
"""#
|
|
|
|
}
|
|
|
|
|
2021-10-22 16:48:30 +02:00
|
|
|
statusCode: {
|
|
|
|
os.#File & {
|
|
|
|
from: ctr
|
|
|
|
path: "/status"
|
|
|
|
}
|
|
|
|
}.contents @dagger(output)
|
|
|
|
|
|
|
|
body: {
|
|
|
|
os.#File & {
|
|
|
|
from: ctr
|
|
|
|
path: "/response"
|
|
|
|
}
|
2021-10-28 17:20:25 +02:00
|
|
|
}.contents @dagger(output)
|
2021-10-22 16:48:30 +02:00
|
|
|
|
2021-10-28 17:20:25 +02:00
|
|
|
// Force os.#File exec before Atoi
|
2021-10-19 15:06:14 +02:00
|
|
|
response: {
|
2021-10-28 17:20:25 +02:00
|
|
|
"body": body
|
2021-10-22 16:48:30 +02:00
|
|
|
"statusCode": strconv.Atoi(statusCode)
|
2021-10-19 15:06:14 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-23 18:33:28 +01:00
|
|
|
|
|
|
|
// URL listener
|
|
|
|
// Creates a dependency on URL
|
|
|
|
#Wait: {
|
|
|
|
// URL to listen
|
|
|
|
url: string
|
|
|
|
|
|
|
|
// Waiting time between checks (sec.)
|
|
|
|
interval: int | *30
|
2021-11-23 19:22:59 +01:00
|
|
|
|
2021-11-23 18:33:28 +01:00
|
|
|
// Max amount of retries
|
|
|
|
retries: int | *3
|
|
|
|
|
|
|
|
// Max initialization time (sec.)
|
|
|
|
startPeriod: int | *0
|
|
|
|
|
|
|
|
// Time until timeout (sec.)
|
|
|
|
timeout: int | *30
|
|
|
|
|
|
|
|
#up: [
|
|
|
|
op.#Load & {
|
|
|
|
from: alpine.#Image & {
|
|
|
|
package: bash: "=~5.1"
|
|
|
|
package: curl: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
op.#Exec & {
|
|
|
|
args: ["/bin/bash", "-c",
|
|
|
|
#"""
|
|
|
|
# (f: str -> int)
|
|
|
|
nb_retries=$(($NB_RETRIES+0))
|
|
|
|
starting_period=$(($START_PERIOD+0))
|
|
|
|
|
|
|
|
status="0"
|
|
|
|
SECONDS=0
|
|
|
|
# START_PERIOD implementation
|
|
|
|
while [ $SECONDS -lt $starting_period ]; do
|
|
|
|
status="$(curl --connect-timeout 1 -s -o /dev/null -w ''%{http_code}'' $HEALTH_URL)"
|
|
|
|
if [ "$status" == "200" ]; then
|
|
|
|
exit 0;
|
|
|
|
fi
|
|
|
|
sleep 1;
|
|
|
|
done
|
|
|
|
|
|
|
|
# TIMEOUT, INTERVAL, INTERVAL implementation
|
|
|
|
for ((i=0;i<NB_RETRIES;i++)); do
|
|
|
|
status="$(curl --connect-timeout $TIMEOUT -s -o /dev/null -w ''%{http_code}'' $HEALTH_URL)"
|
|
|
|
if [ "$status" == "200" ]; then
|
|
|
|
exit 0;
|
|
|
|
fi
|
|
|
|
sleep "$INTERVAL";
|
|
|
|
done
|
|
|
|
|
|
|
|
exit 1;
|
|
|
|
"""#,
|
|
|
|
]
|
|
|
|
always: true
|
|
|
|
env: {
|
2021-11-23 19:22:59 +01:00
|
|
|
HEALTH_URL: url
|
|
|
|
INTERVAL: "\(interval)"
|
|
|
|
NB_RETRIES: "\(retries)"
|
2021-11-23 18:33:28 +01:00
|
|
|
START_PERIOD: "\(startPeriod)"
|
2021-11-23 19:22:59 +01:00
|
|
|
TIMEOUT: "\(timeout)"
|
2021-11-23 18:33:28 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
]
|
2021-11-23 19:22:59 +01:00
|
|
|
}
|