Skip to content

Commit

Permalink
Merge pull request #204 from keveinliu/image-tool
Browse files Browse the repository at this point in the history
[feat] Sidecar image tool
  • Loading branch information
nixff authored Dec 24, 2024
2 parents ac40805 + 7df3e72 commit f4299af
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ RUN rm -fr pipy/build \
FROM alpine:3.16.1 as prod
COPY --from=builder /pipy/bin/pipy /usr/local/bin/pipy
COPY --from=builder /pipy/tutorial /etc/pipy/tutorial
COPY tools/* /usr/local/bin/
RUN apk add --no-cache ca-certificates libstdc++ libcap su-exec tar curl busybox-extras iptables tzdata socat logrotate
RUN adduser -Su 1340 pipy \
&& chmod -R g=u /usr/local/bin/pipy /etc/pipy \
Expand Down
1 change: 1 addition & 0 deletions Dockerfile-loongnix
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ FROM cr.loongnix.cn/loongson/loongnix-server:8.3 as prod
COPY --from=builder /pipy/bin/pipy /usr/local/bin/pipy
COPY --from=builder /pipy/tutorial /etc/pipy
COPY --from=builder /usr/bin/su-exec /usr/bin/su-exec
COPY tools/* /usr/local/bin/
RUN yum install -y --quiet ca-certificates libstdc++ libcap tar curl iptables tzdata socat logrotate
RUN useradd -ru 1340 pipy \
&& chmod -R g=u /usr/local/bin/pipy /etc/pipy \
Expand Down
7 changes: 7 additions & 0 deletions tools/gkill
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env sh

#Gracefully kill sidecar.
#Pipy won't accept new connections when receiving SIGINT signal
#And quit after all connections are closed.

kill -SIGINT 1
93 changes: 93 additions & 0 deletions tools/wait-pipy
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env sh

# Default values
DEFAULT_URL="http://127.0.0.1:15000/config_dump"
DEFAULT_REQ_TMOUT=1 # Single request timeout in seconds
DEFAULT_INTERVAL=1 # Interval between requests in seconds
DEFAULT_TIMEOUT=60 # Total timeout in seconds

# Display usage
usage() {
echo "Usage: $0 [options]"
echo "Options:"
echo " -u, --url URL URL to check (default: $DEFAULT_URL)"
echo " -r, --req-tmout SEC Single request timeout in seconds (default: $DEFAULT_REQ_TMOUT)"
echo " -i, --interval SEC Interval between requests in seconds (default: $DEFAULT_INTERVAL)"
echo " -t, --timeout SEC Total timeout in seconds (default: $DEFAULT_TIMEOUT)"
echo " -h, --help Show this help message"
}

# Initialize variables with default values
URL=$DEFAULT_URL
REQ_TMOUT=$DEFAULT_REQ_TMOUT
INTERVAL=$DEFAULT_INTERVAL
TIMEOUT=$DEFAULT_TIMEOUT

# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-u|--url)
URL="$2"
shift 2
;;
-r|--req-tmout)
REQ_TMOUT="$2"
shift 2
;;
-i|--interval)
INTERVAL="$2"
shift 2
;;
-t|--timeout)
TIMEOUT="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown parameter: $1"
usage
exit 1
;;
esac
done

# Calculate start time
start_time=$(date +%s)

echo "Starting HTTP service check..."
echo "URL: $URL"
echo "Single request timeout (REQ_TMOUT): $REQ_TMOUT seconds"
echo "Request interval (INTERVAL): $INTERVAL seconds"
echo "Total timeout (TIMEOUT): $TIMEOUT seconds"

while true; do
# Check if total timeout is exceeded
current_time=$(date +%s)
elapsed=$((current_time - start_time))

if [ $elapsed -ge $TIMEOUT ]; then
echo "Total timeout of $TIMEOUT seconds reached. Service check failed!"
exit 1
fi

# Send HTTP request
response=$(curl -s -o /dev/null -w "%{http_code}" \
--connect-timeout $REQ_TMOUT \
--max-time $REQ_TMOUT \
"$URL" 2>/dev/null)

if [ "$response" = "200" ]; then
echo "HTTP service is ready!"
echo "Total time elapsed: $elapsed seconds"
exit 0
else
echo "Service not ready, HTTP status code: $response"
echo "Time elapsed: $elapsed seconds, timeout: $TIMEOUT seconds"
echo "Waiting $INTERVAL seconds before next attempt..."
sleep $INTERVAL
fi
done

0 comments on commit f4299af

Please sign in to comment.