cue modules: move stdlib to pkg/alpha.dagger.io
In preparation for Europa, we will vendor multiple CUE modules: - `pkg/alpha.dagger.io`: legacy non-europa packages - `pkg/dagger.io`: core Europa packages - `pkg/universe.dagger.io`: Europa universe Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
159
pkg/alpha.dagger.io/http/http.cue
Normal file
159
pkg/alpha.dagger.io/http/http.cue
Normal file
@@ -0,0 +1,159 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
|
||||
"alpha.dagger.io/alpine"
|
||||
"alpha.dagger.io/dagger"
|
||||
"alpha.dagger.io/dagger/op"
|
||||
"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: true
|
||||
package: jq: true
|
||||
}
|
||||
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
|
||||
)
|
||||
|
||||
[ -n "$BODY" ] && curlArgs+=("-d" "'$BODY'")
|
||||
|
||||
headers="$(echo $HEADERS | jq -r 'to_entries | map(.key + ": " + (.value | tostring) + "\n") | add')"
|
||||
while read h; do
|
||||
curlArgs+=("-H" "'$h'")
|
||||
done <<< "$headers"
|
||||
if [ -e /token ]; then
|
||||
curlArgs+=("-H" "Authorization: bearer $(cat /token)")
|
||||
fi
|
||||
curl "${curlArgs[@]}" > /status
|
||||
"""#
|
||||
}
|
||||
|
||||
statusCode: {
|
||||
os.#File & {
|
||||
from: ctr
|
||||
path: "/status"
|
||||
}
|
||||
}.contents @dagger(output)
|
||||
|
||||
body: {
|
||||
os.#File & {
|
||||
from: ctr
|
||||
path: "/response"
|
||||
}
|
||||
}.contents @dagger(output)
|
||||
|
||||
// Force os.#File exec before Atoi
|
||||
response: {
|
||||
"body": body
|
||||
"statusCode": strconv.Atoi(statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
// URL listener
|
||||
// Creates a dependency on URL
|
||||
#Wait: {
|
||||
// URL to listen
|
||||
url: string
|
||||
|
||||
// Waiting time between checks (sec.)
|
||||
interval: int | *30
|
||||
|
||||
// Max amount of retries
|
||||
retries: int | *3
|
||||
|
||||
// Max initialization time (sec.)
|
||||
startPeriod: int | *0
|
||||
|
||||
// Time until timeout (sec.)
|
||||
timeout: int | *30
|
||||
|
||||
// Env variables
|
||||
env: [string]: string
|
||||
|
||||
#up: [
|
||||
op.#Load & {
|
||||
from: alpine.#Image & {
|
||||
package: bash: true
|
||||
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": {
|
||||
HEALTH_URL: url
|
||||
INTERVAL: "\(interval)"
|
||||
NB_RETRIES: "\(retries)"
|
||||
START_PERIOD: "\(startPeriod)"
|
||||
TIMEOUT: "\(timeout)"
|
||||
for k, v in env {
|
||||
"\(k)": v
|
||||
}
|
||||
}
|
||||
},
|
||||
]
|
||||
}
|
62
pkg/alpha.dagger.io/http/tests/http.cue
Normal file
62
pkg/alpha.dagger.io/http/tests/http.cue
Normal file
@@ -0,0 +1,62 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"alpha.dagger.io/alpine"
|
||||
"alpha.dagger.io/dagger"
|
||||
"alpha.dagger.io/docker"
|
||||
"alpha.dagger.io/os"
|
||||
"alpha.dagger.io/random"
|
||||
)
|
||||
|
||||
TestDockersocket: dagger.#Stream & dagger.#Input
|
||||
|
||||
TestSuffix: random.#String & {
|
||||
seed: ""
|
||||
}
|
||||
|
||||
TestRun: docker.#Run & {
|
||||
name: "daggerci-test-wait-\(TestSuffix.out)"
|
||||
ref: "nginx"
|
||||
socket: TestDockersocket
|
||||
ports: ["8088:80"]
|
||||
}
|
||||
|
||||
// Waits for TestRun to finish initializing
|
||||
Testhealth: #Wait & {
|
||||
url: "http://localhost:8088/"
|
||||
}
|
||||
|
||||
TestWait: query: os.#Container & {
|
||||
image: alpine.#Image & {
|
||||
package: bash: true
|
||||
package: curl: true
|
||||
}
|
||||
command: #"""
|
||||
test "$(curl -L --fail --silent --show-error --write-out "%{http_code}" "$URL" -o /dev/null)" = "200"
|
||||
"""#
|
||||
env: URL: Testhealth.url
|
||||
}
|
||||
|
||||
TestRequest: {
|
||||
req: #Get & {
|
||||
url: Testhealth.url
|
||||
}
|
||||
|
||||
testRaw: os.#Container & {
|
||||
image: alpine.#Image & {
|
||||
package: jq: true
|
||||
package: bash: true
|
||||
}
|
||||
env: STATUS: "\(req.response.statusCode)"
|
||||
files: "/content.json": {
|
||||
content: req.response.body
|
||||
mode: 0o500
|
||||
}
|
||||
shell: args: ["--noprofile", "--norc", "-eo", "pipefail", "-c"]
|
||||
command: #Command
|
||||
}
|
||||
#Command: #"""
|
||||
cat /content.json | grep -q nginx >/dev/null
|
||||
test "$STATUS" = "200"
|
||||
"""#
|
||||
}
|
Reference in New Issue
Block a user