Skip to content

Instantly share code, notes, and snippets.

@lucwj
Last active August 14, 2019 04:10
Show Gist options
  • Save lucwj/083190f1cdf6e46df860e977258c93ca to your computer and use it in GitHub Desktop.
Save lucwj/083190f1cdf6e46df860e977258c93ca to your computer and use it in GitHub Desktop.
Shell Snippets
check_service_completed() {
service_name=$1
state=$(docker service ps $service_name | awk 'FNR == 2 {print $6}')
if [ "$state" = "no such service: $service_name" ]; then
state="Complete"
fi
if [ "$state" = "Complete" ]; then
echo "Y"
else
echo "N"
fi
}
for p in "${parts[@]}"; do
file=${p}
IFS='_' read -r -a segments <<<"$file"
m=${segments[0]}
deploy_service "$m/$file.yml"
done
#!/bin/bash
input_file=$1
if [ -z "$input_file" ]; then
echo "No file mentioned. Please run 'bash run.sh fileName.txt'"
exit 0
fi
FILES=()
while IFS= read -r line || [[ "$line" ]]; do
FILES+=("$line")
done <$input_file
# Number of items in group
g=25
deploy_service() {
docker stack deploy -c /root/stacks/vts-import-historical-data/$1 vts-ihs
}
check_service_completed() {
service_name=$1
state=$(docker service ps $service_name | awk 'FNR == 2 {print $6}')
if [ "$state" = "no such service: $service_name" ]; then
state="Complete"
fi
if [ "$state" = "Complete" ]; then
echo "Y"
else
echo "N"
fi
}
for ((i = 0; i < ${#FILES[@]}; i += g)); do
parts=("${FILES[@]:i:g}")
echo ""
echo ">> Running: ${parts[*]}"
for p in "${parts[@]}"; do
file=${p}
IFS='_' read -r -a segments <<<"$file"
m=${segments[0]}
deploy_service "$m/$file.yml"
done
while true; do
check="Y"
for p in "${parts[@]}"; do
file=${p}
service_name="vts-ihs_$file"
is_completed=$(check_service_completed $service_name)
if [ "$is_completed" = "N" ]; then
check="N"
break
fi
done
if [ "$check" = "Y" ]; then
echo "Done: ${parts[*]}"
break
fi
sleep 5
done
done
echo "Done!"
@lucwj
Copy link
Author

lucwj commented Aug 14, 2019

Loop in array. 💃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment