Skip to content

Commit

Permalink
Merge pull request guardian#5 from guardian/aa-custom-channels
Browse files Browse the repository at this point in the history
add support for multiple stacks pushing to custom channels
  • Loading branch information
akash1810 authored Mar 14, 2017
2 parents f1dfe27 + a66a00c commit 3cbf724
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
8 changes: 2 additions & 6 deletions cf-notify.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
"Description": "cf notify stack",
"Parameters": {
"Bucket": {
"Description": "S3 bucket to locate lambda function (cf-notify-$CHANNEL.zip)",
"Type": "String"
},
"Channel": {
"Description": "Slack channel name",
"Description": "S3 bucket to locate lambda function (cf-notify.zip)",
"Type": "String"
}
},
Expand Down Expand Up @@ -77,7 +73,7 @@
},
"Code": {
"S3Bucket": { "Ref": "Bucket" },
"S3Key": { "Fn::Join": [ "", [ "cf-notify-", { "Ref": "Channel" }, ".zip" ] ] }
"S3Key": "cf-notify.zip"
},
"Runtime": "python2.7",
"Timeout": "30"
Expand Down
19 changes: 11 additions & 8 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,38 @@ fi
CHANNEL_NAME=`echo ${CHANNEL:1} | tr '[:upper:]' '[:lower:]'`

echo 'Creating bucket'
BUCKET="cf-notify-$CHANNEL_NAME-`pwgen -1 --no-capitalize 5`"
BUCKET="cf-notify-`pwgen -1 --no-capitalize 5`"
echo $BUCKET
aws s3 mb "s3://$BUCKET" --profile $PROFILE
echo "Bucket $BUCKET created"


echo 'Creating lambda zip artifact'
cat > slack.py <<EOL
WEBHOOK='$WEBHOOK'
CHANNEL='$CHANNEL'

if [ ! -f slack.py ]; then
cat > slack.py <<EOL
WEBHOOK='$WEBHOOK'
CHANNEL='$CHANNEL'
CUSTOM_CHANNELS={}
EOL
fi

zip cf-notify.zip lambda_notify.py slack.py
echo 'Lambda artifact created'


echo 'Moving lambda artifact to S3'
aws s3 cp cf-notify.zip s3://$BUCKET/cf-notify-$CHANNEL_NAME.zip --profile $PROFILE
aws s3 cp cf-notify.zip s3://$BUCKET/cf-notify.zip --profile $PROFILE

rm slack.py
rm cf-notify.zip
echo 'Lambda artifact moved'

echo 'Creating stack'
aws cloudformation create-stack \
--template-body file://cf-notify.json \
--stack-name cf-notify-$CHANNEL_NAME \
--stack-name cf-notify \
--capabilities CAPABILITY_IAM \
--parameters ParameterKey=Bucket,ParameterValue=$BUCKET ParameterKey=Channel,ParameterValue=$CHANNEL_NAME \
--parameters ParameterKey=Bucket,ParameterValue=$BUCKET \
--profile $PROFILE

if [[ $? != 0 ]];
Expand Down
15 changes: 13 additions & 2 deletions lambda_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,23 @@ def get_stack_update_message(cf_message):
'attachments': attachments
}

if slack.CHANNEL:
message['channel'] = slack.CHANNEL
channel = get_channel(cf_message['StackName'])

if channel:
message['channel'] = channel

return message


def get_channel(stack_name):
default = slack.CHANNEL if hasattr(slack, 'CHANNEL') else None

if hasattr(slack, 'CUSTOM_CHANNELS'):
return slack.CUSTOM_CHANNELS.get(stack_name, default)

return default


def get_stack_update_attachment(cf_message):
title = 'Stack {stack} is now status {status}'.format(
stack=cf_message['StackName'],
Expand Down

0 comments on commit 3cbf724

Please sign in to comment.