-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloud-init.yaml
77 lines (67 loc) · 2.75 KB
/
cloud-init.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#cloud-config
write_files:
- path: /root/conf/enabled_plugins
content: |
[rabbitmq_management].
- path: /root/conf/rabbitmq.config
content: |
[ { rabbit, [
{ loopback_users, [ ] } ] }
].
- path: /root/find_hosts.sh
content: |
#!/usr/bin/env bash
export AWS_DEFAULT_REGION='${region}'
DNSES=$(aws ec2 describe-instances --filters "Name=tag:aws:autoscaling:groupName,Values=${asg_name}" "Name=instance-state-name,Values=running" | jq ".Reservations[].Instances[].PrivateDnsName" | xargs)
HOSTNAMES=()
for dns in $DNSES; do
hostname=($${dns//./ })
if [ "$hostname" != "$HOSTNAME" ]; then
HOSTNAMES+=( $hostname )
fi
done
echo $HOSTNAMES
- path: /root/bin/join_cluster.sh
content: |
#!/usr/bin/env sh
HOSTNAMES=( $@ )
for run in {1..3}; do
sleep $[ ( $RANDOM % 10 ) + 1 ]s
rabbitmqctl stop_app
NEW_HOSTNAMES=()
for hostname in $HOSTNAMES; do
rabbitmqctl join_cluster rabbit@$hostname
st=$?
if [ $st -ne 0 ] && [ $st -ne 130 ]; then # 130 is "already joined"
NEW_HOSTNAMES+=( $hostname )
fi
done
HOSTNAMES=( $${NEW_HOSTNAMES[@]} )
rabbitmqctl start_app
if [ $${#HOSTNAMES[@]} -eq 0 ]; then
exit 0
fi
done
- path: /root/configure.sh
content: |
#!/usr/bin/env bash
docker exec rabbitmq rabbitmqctl add_user admin ${admin_password}
docker exec rabbitmq rabbitmqctl set_user_tags admin administrator
docker exec rabbitmq rabbitmqctl add_user rabbit ${rabbit_password}
docker exec rabbitmq rabbitmqctl add_vhost /
docker exec rabbitmq rabbitmqctl set_policy -p / ${ha_policy_name} "^" '{"ha-mode":"exactly", "ha-params":${replica_count}, "ha-sync-mode":"automatic", "message-ttl":${message_timeout}, "expires":${message_timeout}}'
docker exec rabbitmq rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
docker exec rabbitmq rabbitmqctl set_permissions -p / rabbit ".*" ".*" ".*"
docker exec rabbitmq rabbitmqctl delete_user guest
docker exec rabbitmq rabbitmq-plugins enable rabbitmq_shovel rabbitmq_shovel_management
runcmd:
- yum update -y
- yum install -y docker jq
- service docker start
- chkconfig docker on
- usermod -a -G docker ec2-user
- docker run -d --name rabbitmq --hostname $HOSTNAME -p 4369:4369 -p 5672:5672 -p 15672:15672 -p 25672:25672 -e RABBITMQ_ERLANG_COOKIE='${secret_cookie}' -v /root/data:/var/lib/rabbitmq -v /root/conf/:/etc/rabbitmq -v /root/bin:/tmp/bin rabbitmq:3-management
- sleep 1
- docker exec rabbitmq bash /tmp/bin/join_cluster.sh $(bash /root/find_hosts.sh)
- sleep 1
- bash /root/configure.sh