Skip to content

Instantly share code, notes, and snippets.

@kiriappeee
Created February 16, 2018 23:25
Show Gist options
  • Save kiriappeee/ac9dcf5d2e88d4ee884d2defcb3358cc to your computer and use it in GitHub Desktop.
Save kiriappeee/ac9dcf5d2e88d4ee884d2defcb3358cc to your computer and use it in GitHub Desktop.
Extracting docker services to be run from a complex docker-compose command
#!/bin/bash
function getLinkedServices(){
IFS=
serviceToCheck=$1
serviceDefintion=$(echo $finalDockerFile | grep "^\s\s$serviceToCheck:" -A $lengthOfDockerFile -m 1)
nextServiceLine=$(echo $serviceDefintion | tail -n +2 | grep "^\s\s[a-zA-Z0-9]\+:" -m 1 -n | awk '{print $1}')
if [ "$nextServiceLine" != "" ]
then
nextServiceLine=${nextServiceLine:0:-1}
else
nextServiceLine=$lengthOfDockerFile
fi
serviceDefintion=$(echo $serviceDefintion | head -n $nextServiceLine)
serviceLinks=$(echo $serviceDefintion | grep "^\s\s\s\slinks:" -A $nextServiceLine | tail -n +2 | grep "^\s\s\s\s[a-zA-Z]\+:" -B $nextServiceLine -m 1 | head -n -1)
serviceLinks=${serviceLinks//\- /}
IFS=$OLDIFS
for serviceLink in $serviceLinks
do
indexToCheckIfServiceIsAliased=$(echo "$serviceLink" | awk '{print index($serviceLink, ":")}')
if [ "$indexToCheckIfServiceIsAliased" != 0 ]
then
serviceLink=${serviceLink:0:$((indexToCheckIfServiceIsAliased-1))}
fi
serviceExistsCheck=$(echo ${servicesBeingStartedUp[@]} | grep "$serviceLink")
if [ "$serviceExistsCheck" == "" ]
then
servicesBeingStartedUp+=($serviceLink)
getLinkedServices $serviceLink
fi
done
}
#run this script with something like the following command
#./checkclones "docker-compose -f docker-compose.yml -f .docker-compose-servicea.yml -f .docker-compose-service-b.yml" "some-service"
#where some-service is a service defined within one of those docker-compose files.
dockerComposeCommandWithFiles=$1
servicesToUp=$2
servicesToIterateThrough=$(echo ${servicesToUp:1} | tr " " "\n")
OLDIFS=$IFS
IFS=
finalDockerFile=$(eval ${dockerComposeCommandWithFiles} config)
lengthOfDockerFile=$(echo $finalDockerFile | wc -l)
servicesBeingStartedUp=()
for service in $servicesToIterateThrough
do
getLinkedServices $service
done
echo ${servicesBeingStartedUp[@]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment