Skip to content

Instantly share code, notes, and snippets.

@ChuckJHardy
Created February 1, 2016 12:01
Show Gist options
  • Save ChuckJHardy/d4c7f4b5ef8fe1e9755c to your computer and use it in GitHub Desktop.
Save ChuckJHardy/d4c7f4b5ef8fe1e9755c to your computer and use it in GitHub Desktop.
Microservices Setup Script
#!/usr/bin/env sh
set -e
blue="\033[34m"
reset="\033[0m"
red="\033[31m"
function warn {
echo "$1" > /dev/stderr
}
function alert {
warn "$red$1$reset"
}
function notice {
warn "$blue$1$reset"
}
root=`dirname $0`
projects=".projects"
target_dir="$1"
link_dir="${root}/../apps"
if [[ -z "$target_dir" ]]; then
warn "Usage: $0 <target-directory>"
exit 1
fi
mkdir -p "$link_dir"
while read line; do
array=($line)
name="${array[0]}"
git_url="${array[1]}"
target="$target_dir/$name"
link="$link_dir/$name"
notice "* Preparing $name"
if [[ ! -e "$target" ]]; then
warn "Project $name not present. Cloning ..."
git clone "$git_url" "$target"
fi
if [[ ! -e "$link" ]]; then
ln -sf "../$target" "$link"
fi
done < $projects
any_variable_missing=false
notice "* Checking environment configuration"
while read line; do
array=($line)
name="${array[0]}"
link="$link_dir/$name"
env_file="$link/ENV"
if [[ -e "$env_file" ]]; then
while read line; do
var_name=$(echo "$line" | cut -d' ' -f 1)
description=$(echo "$line" | cut -d'#' -f 2)
var_value="${!var_name}"
if [[ -z "$var_value" ]]; then
any_variable_missing=true
alert "$var_name not set -$description"
else
warn "$var_name=$var_value"
fi
done < "$env_file"
else
warn "No ENV file for project ${name}"
fi
done < $projects
if [[ "$any_variable_missing" == true ]]; then
alert "Some required environment variables are not set"
exit 78
fi
while read line; do
array=($line)
name="${array[0]}"
link="$link_dir/$name"
notice "* Setting up $name"
if [[ -e "$link/bin/setup" ]]; then
pushd "$link" > /dev/null
bin/setup
popd > /dev/null
else
warn "No bin/setup for project ${name}"
fi
done < $projects
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment