{
    "AWSTemplateFormatVersion": "2010-09-09",
    
    "Description" : "AWS CloudFormation Plone OpsWorks Template: Launches OpsWorks stack, layer, instances and associated resources to run a Plone CMS.",
    
    "Resources": {

        "PloneStack": {
            "Type": "AWS::OpsWorks::Stack",
            "Properties": {
                "Name": {
                    "Ref": "AWS::StackName"
                },                
                "ServiceRoleArn": {
                    "Fn::GetAtt": [ "OpsWorksServiceRole", "Arn"]
                },
                "DefaultInstanceProfileArn": {
                    "Fn::GetAtt": [ "OpsWorksInstanceProfile", "Arn"]
                },
                "DefaultRootDeviceType": "ebs",
                "ConfigurationManager": {
                    "Version": "11.10",
                    "Name": "Chef"
                },
                "ChefConfiguration": {
                    "BerkshelfVersion": "3.1.3",
                    "ManageBerkshelf": true
                },
                "UseCustomCookbooks": true,
                "CustomCookbooksSource": {
                    "Url": "https://github.com/alecpm/opsworks-web-python.git",
                    "Type": "git",
                    "Revision": "master"
                },
                "DefaultOs": "Ubuntu 14.04 LTS"
            }
        },
        
        "PloneInstances": {
            "Type": "AWS::OpsWorks::Layer",
            "Properties": {
                "StackId": {
                    "Ref": "PloneStack"
                },
                "Name": "Plone Instances",
                "Type": "custom",
                "Shortname": "plone_instances",
                "EnableAutoHealing": false,
                "AutoAssignElasticIps": false,
                "AutoAssignPublicIps": true,
                "Packages": [
                    "libjpeg8-dev", "libfreetype6-dev", "liblcms1-dev",
                    "zlib1g-dev", "libwebp-dev", "webp", "mime-support",
                    "libxml2-dev", "wv", "poppler-utils", "libxslt1-dev",
                    "libtiff4-dev", "autoconf", "automake"
                ],
                "CustomRecipes": {
                    "Undeploy": ["plone_buildout::instances-undeploy"],
                    "Setup": ["opsworks_deploy_python::r3-mount-patch", "plone_buildout::instances-setup", "apt::unattended-upgrades"],
                    "Configure": ["plone_buildout::instances-configure"],
                    "Shutdown": ["plone_buildout::instances-stop"],
                    "Deploy": ["plone_buildout::instances-deploy"]
                }
            }
        },
        
        "Zeoserver": {
            "Type": "AWS::OpsWorks::Layer",
            "Properties": {
                "StackId": {
                    "Ref": "PloneStack"
                },
                "Name": "Zeoserver",
                "Type": "custom",
                "Shortname": "zeoserver",
                "EnableAutoHealing": false,
                "AutoAssignElasticIps": false,
                "AutoAssignPublicIps": true,
                "Packages": ["autoconf", "automake"],
                "CustomRecipes": {
                    "Undeploy": ["plone_buildout::zeoserver-undeploy"],
                    "Setup": ["opsworks_deploy_python::r3-mount-patch", "plone_buildout::zeoserver-setup", "apt::unattended-upgrades"],
                    "Configure": ["plone_buildout::zeoserver-configure"],
                    "Shutdown": ["plone_buildout::zeoserver-stop"],
                    "Deploy": ["plone_buildout::zeoserver-deploy"]
                },
                "VolumeConfigurations": [{
                    "MountPoint": "/mnt/shared/zodb/filestorage",
                    "Size": 10,
                    "NumberOfDisks": 1
                }]
            }
        },
        
        "SharedBlobs": {
            "Type": "AWS::OpsWorks::Layer",
            "Properties": {
                "StackId": {
                    "Ref": "PloneStack"
                },
                "Type": "custom",
                "Name": "Shared Blobs",
                "CustomRecipes": {
                    "Undeploy": [],
                    "Setup": ["opsworks_deploy_python::r3-mount-patch", "plone_buildout::nfs_blobs", "apt::unattended-upgrades"],
                    "Configure": ["plone_buildout::nfs_blobs"],
                    "Shutdown": [],
                    "Deploy": []
                },
                "AutoAssignPublicIps": true,
                "EnableAutoHealing": false,
                "Shortname": "shared_blobs",
                "AutoAssignElasticIps": false,
                "VolumeConfigurations": [{
                    "MountPoint": "/srv/exports",
                    "Size": 200,
                    "NumberOfDisks": 1
                }],
                "Packages": ["autoconf", "automake"]
            }
        },
        
        "EBSSnapshots": {
            "Type": "AWS::OpsWorks::Layer",
            "Properties": {
                "StackId": {
                    "Ref": "PloneStack"
                },
                "Type": "custom",
                "Name": "EBS Snapshotting",
                "CustomRecipes": {
                    "Undeploy": [],
                    "Setup": ["opsworks_deploy_python::r3-mount-patch"],
                    "Configure": ["plone_buildout::ebs_snapshots"],
                    "Shutdown": [],
                    "Deploy": []
                },
                "AutoAssignPublicIps": true,
                "EnableAutoHealing": false,
                "Shortname": "ebs_snapshots",
                "AutoAssignElasticIps": false
            }
        },
        
        "LB": {
            "Type": "AWS::OpsWorks::Layer",
            "Properties": {
                "StackId": {
                    "Ref": "PloneStack"
                },
                "Type": "lb",
                "Name": "HAProxy",
                "CustomRecipes": {
                    "Undeploy": [],
                    "Setup": [
                    "opsworks_deploy_python::r3-mount-patch",
                    "plone_buildout::haproxy",
                    "plone_buildout::varnish",
                    "plone_buildout::nginx", 
                    "apt::unattended-upgrades"
                    ],
                    "Configure": [
                    "plone_buildout::haproxy",
                    "plone_buildout::varnish",
                    "plone_buildout::nginx"
                    ],
                    "Shutdown": [],
                    "Deploy": ["plone_buildout::haproxy"]
                },
                "Packages": ["autoconf", "automake"],
                "AutoAssignPublicIps": true,
                "AutoAssignElasticIps": true,
                "EnableAutoHealing": false,
                "Attributes": {
                    "HaproxyHealthCheckUrl": "/misc_/CMFPlone/plone_icon",
                    "HaproxyHealthCheckMethod": "HEAD",
                    "HaproxyStatsPassword": "STATSPASSWORD",
                    "HaproxyStatsUrl": "/balancer/stats",
                    "HaproxyStatsUser": "stats"
                },
                "Shortname": "lb"
            }
        },

        "InstancesApp": {
            "Type": "AWS::OpsWorks::App",
            "Properties": {
                "StackId": {
                    "Ref": "PloneStack"
                },
                "Name": "Plone Instances",
                "Type": "other",
                "Shortname": "plone_instances",
                "AppSource": {
                    "Type": "git",
                    "Url": "https://github.com/alecpm/opsworks_example_buildouts.git",
                    "Revision": "plone-5.0"
                },
                "EnableSsl": false,
                "SslConfiguration": {}
            }
        },

        "ZeoserverApp": {
            "Type": "AWS::OpsWorks::App",
            "Properties": {
                "StackId": {
                    "Ref": "PloneStack"
                },
                "Name": "Zeoserver",
                "Type": "other",
                "Shortname": "zeoserver",
                "AppSource": {
                    "Type": "git",
                    "Url": "https://github.com/alecpm/opsworks_example_buildouts.git",
                    "Revision": "plone-5.0"
                },
                "EnableSsl": false,
                "SslConfiguration": {}
            }
        },

        "OpsWorksServiceRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "AssumeRolePolicyDocument": {
                    "Statement": [{
                        "Effect": "Allow",
                        "Principal": {
                            "Service": [
                            "opsworks.amazonaws.com"
                            ]
                        },
                        "Action": [
                        "sts:AssumeRole"
                        ]
                    }]
                },
                "Path": "/",
                "Policies": [
                {
                    "PolicyName": "opsworks-service",
                    "PolicyDocument": {
                        "Statement": [
                        {
                            "Effect": "Allow",
                            "Action": [
                            "ec2:*",
                            "iam:PassRole",
                            "cloudwatch:GetMetricStatistics",
                            "elasticloadbalancing:*"
                            ],
                            "Resource": "*"
                        }
                        ]
                    }
                }
                ]
            }
        },

        "OpsWorksInstanceRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "AssumeRolePolicyDocument": {
                    "Statement": [{
                        "Effect": "Allow",
                        "Principal": {
                            "Service": [
                            "ec2.amazonaws.com"
                            ]
                        },
                        "Action": [
                        "sts:AssumeRole"
                        ]
                    }]
                },
                "Path": "/"
            }
        },

        "OpsWorksInstanceProfile": {
            "Type": "AWS::IAM::InstanceProfile",
            "Properties": {
                "Path": "/",
                "Roles": [{
                    "Ref": "OpsWorksInstanceRole"
                }]
            }
        }
    },

    "Outputs" : {
        "StackId" : {
            "Value" : { "Ref" : "PloneStack" }
        },   
        "AppId" : {
            "Value" : { "Ref" : "PloneInstances" }
        }
    }
}