Last active
April 4, 2019 09:56
-
-
Save yarax/7aa7d76c2e92ba02d854e64ee99bc319 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
TAG="kafka-brokers-demo" | |
sudo yum install -y jq | |
sudo yum install -y wget | |
sudo yum install -y java | |
cd ~ | |
wget "http://archive.apache.org/dist/kafka/2.1.0/kafka_2.11-2.1.0.tgz" | |
tar -xvf kafka_2.11-2.1.0.tgz | |
cd kafka_2.11-2.1.0 | |
MY_INST_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id) | |
MY_IP="UNDEFINED" | |
ZOOKEEPER_NODES="" | |
ZOOKEEPER_CONNECT="" | |
COUNTER=1 | |
ASSIGNED=0 | |
eips=$(aws ec2 describe-addresses --region eu-central-1 --filter Name=tag-value,Values=$TAG) | |
for eip in $(echo "${eips}" | jq -rc '.Addresses[]'); do | |
INST_ID=$(echo $eip | jq -r '.InstanceId') | |
ALLOC=$(echo $eip | jq -r '.AllocationId') | |
IP=$(echo $eip | jq -r '.PublicIp') | |
IP_INT=$(echo $IP | tr . '\n' | awk '{s = s*256 + $1} END{print s}') | |
if [[ $INST_ID == 'null' ]] && [[ $ASSIGNED == 0 ]]; then | |
ASSIGNATION_FAILED=0 | |
aws ec2 associate-address --region eu-central-1 --allocation-id $ALLOC --instance-id $MY_INST_ID || ASSIGNATION_FAILED=1 | |
if [[ $ASSIGNATION_FAILED == 0 ]]; then | |
ASSIGNED=1 | |
MY_IP=$IP | |
IP='0.0.0.0' | |
echo "Assigned $IP" | |
fi | |
fi | |
ZOOKEEPER_NODES=$ZOOKEEPER_NODES"server.$IP_INT=$IP:2888:3888"$'\n' | |
if [ $COUNTER == 1 ]; then | |
ZOOKEEPER_CONNECT="$IP:2181" | |
else | |
ZOOKEEPER_CONNECT="$ZOOKEEPER_CONNECT,$IP:2181" | |
fi | |
COUNTER=$(($COUNTER+1)) | |
done | |
MY_ZOOKEEPER_ID=$(echo $MY_IP | tr . '\n' | awk '{s = s*256 + $1} END{print s}') # set to /tmp/zookeeper/myid | |
# Update configuration with concatenated strings | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment