Skip to content

Instantly share code, notes, and snippets.

@winebarrel
Created March 9, 2014 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save winebarrel/9451233 to your computer and use it in GitHub Desktop.
Save winebarrel/9451233 to your computer and use it in GitHub Desktop.
Kumogata+Serf - template.rb
hosts = [:cthulhu, :hastur, :nyar]
Parameters do
SerfURL do
Type "String"
Default "https://dl.bintray.com/mitchellh/serf/0.4.5_linux_amd64.zip"
end
EventHandlerURL do
Type "String"
Default "https://gist.githubusercontent.com/winebarrel/9447917/raw/event_handler.rb"
end
end
Resources do
hosts.each {|name|
_(name) do
Type "AWS::EC2::Instance"
Properties do
# amzn-ami-pv-2013.09.2.x86_64-ebs
ImageId "ami-0d13700c"
InstanceType "t1.micro"
KeyName "XXX"
NetworkInterfaces [
_{
DeviceIndex 0
SubnetId "subnet-XXXXXXXX"
AssociatePublicIpAddress true
}
]
UserData do
Fn__Base64 (<<-EOS).fn_join
#!/bin/bash
/opt/aws/bin/cfn-init -s <%= Ref "AWS::StackName" %> -r #{name} --region <%= Ref "AWS::Region" %>
/opt/aws/bin/cfn-signal -e 0 '<%= Ref "#{name}WaitHandle" %>'
EOS
end
Tags [
_{
Key "Name"
Value name
}
]
end # Properties
Metadata do
AWS__CloudFormation__Init do
config do
commands do
setup do
command (<<-EOS).fn_join
hostname #{name}
# Install Serf
wget <%= Ref "SerfURL" %> -O /tmp/serf.zip
unzip /tmp/serf.zip -d /tmp
mv /tmp/serf /usr/bin
# Install serf-hosts
mkdir -p /usr/libexec/serf
wget <%= Ref "EventHandlerURL" %> -O /usr/libexec/serf/event_handler
chmod 755 /usr/libexec/serf/event_handler
IP_ADDR=`/sbin/ip addr show dev eth0 | awk -F '[ /]+' '/inet /{print $3}'`
exec serf agent -node=#{name} \\
-bind=$IP_ADDR \\
-event-handler=/usr/libexec/serf/event_handler > /var/log/serf.log 2>&1 &
<% if "#{name}" != "cthulhu" %>
for i in {1..12}; do
sleep 5
serf join -replay <%= Fn__GetAtt "cthulhu", "PrivateIp" %>
[ $? -eq 0 ] && break
done
<% end %>
EOS
end
end
end
end
end
end
_("#{name}WaitHandle") do
Type "AWS::CloudFormation::WaitConditionHandle"
end
_("#{name}WaitCondition") do
Type "AWS::CloudFormation::WaitCondition"
DependsOn name
Properties do
Handle { Ref "#{name}WaitHandle" }
Timeout 300
end
end
} # end of each
end
Outputs do
hosts.each do |name|
_("#{name}PublicIp") do
Value { Fn__GetAtt name, "PublicIp" }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment