Skip to content

Commit

Permalink
FAI-11873 - Return exit code from containers (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjwooo authored Jun 13, 2024
1 parent 964896d commit 3568b9b
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions airbyte-local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ function help() {
function setDefaults() {
declare -Ag src_config=()
declare -Ag dst_config=()
keep_containers="--rm"
log_level="info"
max_log_size="10m"
max_memory=""
Expand Down Expand Up @@ -192,7 +191,7 @@ function parseFlags() {
jq_color_opt="-M"
shift 1 ;;
--keep-containers)
keep_containers=""
keep_containers=1
shift 1 ;;
--k8s-deployment)
k8s_deployment=1
Expand Down Expand Up @@ -753,7 +752,7 @@ function readSrc() {
if [[ "$src_file" ]]; then
cat $src_file
else
docker run $keep_containers $max_memory $max_cpus --init --cidfile="$tempPrefix-src_cid" -v "$tempdir:/configs" --log-opt max-size="$max_log_size" -a stdout -a stderr --env LOG_LEVEL="$log_level" $src_docker_options "$src_docker_image" read \
docker run --name $src_container_name $max_memory $max_cpus --init --cidfile="$tempPrefix-src_cid" -v "$tempdir:/configs" --log-opt max-size="$max_log_size" -a stdout -a stderr --env LOG_LEVEL="$log_level" $src_docker_options "$src_docker_image" read \
--config "/configs/$src_config_filename" \
--catalog "/configs/$src_catalog_filename" \
--state "/configs/$src_state_filename"
Expand All @@ -774,7 +773,7 @@ function sync_local() {
jq -rR --unbuffered "$jq_src_msg" >&2) |
jq -cR --unbuffered "fromjson? | select(.type == \"RECORD\" or .type == \"STATE\") | .record.stream |= \"${dst_stream_prefix}\" + ." |
tee "$output_filepath" |
docker run $keep_containers $dst_use_host_network $max_memory $max_cpus --cidfile="$tempPrefix-dst_cid" -i --init -v "$tempdir:/configs" --log-opt max-size="$max_log_size" -a stdout -a stderr -a stdin --env LOG_LEVEL="$log_level" $dst_docker_options "$dst_docker_image" write \
docker run --name $dst_container_name $dst_use_host_network $max_memory $max_cpus --cidfile="$tempPrefix-dst_cid" -i --init -v "$tempdir:/configs" --log-opt max-size="$max_log_size" -a stdout -a stderr -a stdin --env LOG_LEVEL="$log_level" $dst_docker_options "$dst_docker_image" write \
--config "/configs/$dst_config_filename" --catalog "/configs/$dst_catalog_filename" |
tee >(jq -cR --unbuffered 'fromjson? | select(.type == "STATE") | .state.data' | tail -n 1 > "$new_source_state_file") |
# https://stedolan.github.io/jq/manual/#Colors
Expand Down Expand Up @@ -834,11 +833,11 @@ function sync() {

function cleanup() {
if ((k8s_deployment)); then
if [[ ! -z "$keep_containers" ]]; then
if ((keep_containers)); then
log "Pod $pod_name is left on the cluster. To delete it, run 'kubectl delete pod $pod_name -n $k8s_namespace'"
else
log "Deleting pod $pod_name"
kubectl delete pod $pod_name -n $k8s_namespace
else
log "Pod $pod_name is left on the cluster. To delete it, run 'kubectl delete pod $pod_name -n $k8s_namespace'"
fi
else
if [[ -s "$tempPrefix-src_cid" ]]; then
Expand All @@ -849,8 +848,22 @@ function cleanup() {
docker container kill $(cat "$tempPrefix-dst_cid") 2>/dev/null || true
rm "$tempPrefix-dst_cid"
fi
src_exit_code=$(docker inspect $src_container_name --format="{{.State.ExitCode}}")
dst_exit_code=$(docker inspect $dst_container_name --format="{{.State.ExitCode}}")
debug "Docker container $src_container_name exited with code $src_exit_code"
debug "Docker container $dst_container_name exited with code $dst_exit_code"
if ((keep_containers)); then
log "Docker containers $src_container_name and $dst_container_name have been saved"
else
docker container rm -f $src_container_name $dst_container_name > /dev/null 2>&1 || true
fi
fi
rm -rf "$tempdir"
if [ -n "$src_exit_code" ] && [ "$src_exit_code" -ne 0 ]; then
exit $src_exit_code
elif [ -n "$dst_exit_code" ] && [ "$dst_exit_code" -ne 0 ]; then
exit $dst_exit_code
fi
}

function fmtLog(){
Expand Down Expand Up @@ -919,9 +932,12 @@ main() {
validateRequirements
setTheme
validateInput
tempPrefix="tmp-$(jq -r -n "now")"
timestamp=$(jq -r -n "now")
tempPrefix="tmp-$timestamp"
tempPath="$(pwd)/$tempPrefix"
tempdir=$(mkdir -p $tempPath && echo $tempPath)
src_container_name="airbyte-local-src-$timestamp"
dst_container_name="airbyte-local-dst-$timestamp"
trap cleanup EXIT
trap cleanup SIGINT
echo "Created folder $tempdir for temporary Airbyte files"
Expand Down

0 comments on commit 3568b9b

Please sign in to comment.