From f3c2d67cd5148e997e86f9b8f046f2d92a8854f4 Mon Sep 17 00:00:00 2001 From: Angus Salkeld Date: Mon, 7 May 2012 20:47:28 +1000 Subject: [PATCH] Add WaitConditions to HA template Signed-off-by: Angus Salkeld --- ...WordPress_Single_Instance_With_HA.template | 62 +++++++++++++++---- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/templates/WordPress_Single_Instance_With_HA.template b/templates/WordPress_Single_Instance_With_HA.template index 88c49d7b3..cd3d2eb05 100644 --- a/templates/WordPress_Single_Instance_With_HA.template +++ b/templates/WordPress_Single_Instance_With_HA.template @@ -153,8 +153,8 @@ "/etc/cfn/notify-on-httpd-restarted" : { "content" : { "Fn::Join" : ["", [ - "#!/bin/sh\n", - "logger -t cfn-event 'http got restarted'\n" + "#!/bin/sh\n", + "logger -t cfn-event 'http got restarted'\n" ]]}, "mode" : "000700", "owner" : "root", @@ -172,6 +172,20 @@ "group" : "root" }, + "/tmp/setup.mysql" : { + "content" : { "Fn::Join" : ["", [ + "CREATE DATABASE ", { "Ref" : "DBName" }, ";\n", + "GRANT ALL PRIVILEGES ON ", { "Ref" : "DBName" }, + ".* TO '", { "Ref" : "DBUsername" }, "'@'localhost'\n", + "IDENTIFIED BY '", { "Ref" : "DBPassword" }, "';\n", + "FLUSH PRIVILEGES;\n", + "EXIT\n" + ]]}, + "mode" : "000644", + "owner" : "root", + "group" : "root" + }, + "/etc/cfn/hooks.conf" : { "content": { "Fn::Join" : ["", [ "[cfn-http-restarted]\n", @@ -211,22 +225,44 @@ "KeyName" : { "Ref" : "KeyName" }, "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [ "#!/bin/bash -v\n", - "/opt/aws/bin/cfn-init\n", + "# Helper function\n", + "function error_exit\n", + "{\n", + " /opt/aws/bin/cfn-signal -e 1 -r \"$1\" '", { "Ref" : "WaitHandle" }, "'\n", + " exit 1\n", + "}\n", + + "/opt/aws/bin/cfn-init -s ", { "Ref" : "AWS::StackName" }, " -r WikiDatabase ", + " --region ", { "Ref" : "AWS::Region" }, " || error_exit 'Failed to run cfn-init'\n", + "# Setup MySQL root password and create a user\n", - "mysqladmin -u root password '", { "Ref" : "DBRootPassword" }, "'\n", - "cat << EOF | mysql -u root --password='", { "Ref" : "DBRootPassword" }, "'\n", - "CREATE DATABASE ", { "Ref" : "DBName" }, ";\n", - "GRANT ALL PRIVILEGES ON ", { "Ref" : "DBName" }, ".* TO \"", { "Ref" : "DBUsername" }, "\"@\"localhost\"\n", - "IDENTIFIED BY \"", { "Ref" : "DBPassword" }, "\";\n", - "FLUSH PRIVILEGES;\n", - "EXIT\n", - "EOF\n", - "sed --in-place --e s/database_name_here/", { "Ref" : "DBName" }, "/ --e s/username_here/", { "Ref" : "DBUsername" }, "/ --e s/password_here/", { "Ref" : "DBPassword" }, "/ /usr/share/wordpress/wp-config.php\n", + "mysqladmin -u root password '", { "Ref" : "DBRootPassword" }, + "' || error_exit 'Failed to initialize root password'\n", + + "mysql -u root --password='", { "Ref" : "DBRootPassword" }, + "' < /tmp/setup.mysql || error_exit 'Failed to create database.'\n", "# install cfn-hup crontab\n", - "crontab /tmp/cfn-hup-crontab.txt\n" + "crontab /tmp/cfn-hup-crontab.txt\n", + + "# All is well so signal success\n", + "/opt/aws/bin/cfn-signal -e 0 -r \"Wiki server setup complete\" '", + { "Ref" : "WaitHandle" }, "'\n" ]]}} } + }, + + "WaitHandle" : { + "Type" : "AWS::CloudFormation::WaitConditionHandle" + }, + + "WaitCondition" : { + "Type" : "AWS::CloudFormation::WaitCondition", + "DependsOn" : "WikiDatabase", + "Properties" : { + "Handle" : {"Ref" : "WaitHandle"}, + "Timeout" : "300" + } } },