Пример #1
0
    def _get_autoscaling_group_parameters(self, chain_context, launch_config_name):

        config = {
            'AvailabilityZones': Ref("AvailabilityZones"),  # Not really required in this case (yet)
            'LaunchConfigurationName': Ref(launch_config_name),
            'MinSize': Ref("MinSize"),
            'MaxSize': Ref("MaxSize"),
            'VPCZoneIdentifier': Ref("PrivateSubnets"),
            'Tags': [ASTag('Name', self.name, True)],
        }

        if META_TARGET_GROUP_NAME in chain_context.metadata:
            config['TargetGroupARNs'] = [Ref(chain_context.metadata[META_TARGET_GROUP_NAME])]

        if self.use_update_policy:
            update_policy = UpdatePolicy(
                AutoScalingReplacingUpdate=AutoScalingReplacingUpdate(
                    WillReplace=True,
                ),
                AutoScalingRollingUpdate=AutoScalingRollingUpdate(
                    PauseTime='PT5M',
                    MinInstancesInService="1",
                    MaxBatchSize='1',
                    WaitOnResourceSignals=True
                )
            )
            config['UpdatePolicy'] = update_policy

        return config
Пример #2
0
 def create_autoscaling_group(self):
     t = self.template
     t.add_resource(
         autoscaling.LaunchConfiguration(
             'EmpireControllerLaunchConfig',
             IamInstanceProfile=GetAtt("EmpireControllerProfile", "Arn"),
             ImageId=FindInMap('AmiMap', Ref("AWS::Region"),
                               Ref("ImageName")),
             BlockDeviceMappings=self.build_block_device(),
             InstanceType=Ref("InstanceType"),
             KeyName=Ref("SshKeyName"),
             UserData=self.generate_user_data(),
             SecurityGroups=[Ref("DefaultSG"),
                             Ref(CLUSTER_SG_NAME)]))
     t.add_resource(
         autoscaling.AutoScalingGroup(
             'EmpireControllerAutoscalingGroup',
             AvailabilityZones=Ref("AvailabilityZones"),
             LaunchConfigurationName=Ref("EmpireControllerLaunchConfig"),
             MinSize=Ref("MinHosts"),
             MaxSize=Ref("MaxHosts"),
             VPCZoneIdentifier=Ref("PrivateSubnets"),
             LoadBalancerNames=[
                 Ref("EmpireControllerLoadBalancer"),
             ],
             Tags=[ASTag('Name', 'empire_controller', True)]))
Пример #3
0
 def get_autoscaling_group_parameters(self, launch_config_name, elb_name):
     return {
         'AvailabilityZones': Ref("AvailabilityZones"),
         'LaunchConfigurationName': Ref(launch_config_name),
         'MinSize': Ref("MinSize"),
         'MaxSize': Ref("MaxSize"),
         'VPCZoneIdentifier': Ref("PrivateSubnets"),
         'LoadBalancerNames': If("CreateELB", [Ref(elb_name), ], []),
         'Tags': [ASTag('Name', self.name, True)],
     }
Пример #4
0
 def create_autoscaling_group(self):
     t = self.template
     t.add_resource(
         autoscaling.LaunchConfiguration(
             'BastionLaunchConfig',
             AssociatePublicIpAddress=True,
             ImageId=FindInMap(
                 'AmiMap', Ref("AWS::Region"), Ref("ImageName")),
             InstanceType=Ref("InstanceType"),
             KeyName=Ref("SshKeyName"),
             UserData=self.generate_user_data(),
             SecurityGroups=[Ref("DefaultSG"), Ref(CLUSTER_SG_NAME)]))
     t.add_resource(
         autoscaling.AutoScalingGroup(
             'BastionAutoscalingGroup',
             AvailabilityZones=Ref("AvailabilityZones"),
             LaunchConfigurationName=Ref("BastionLaunchConfig"),
             MinSize=Ref("MinSize"),
             MaxSize=Ref("MaxSize"),
             VPCZoneIdentifier=Ref("PublicSubnets"),
             Tags=[ASTag('Name', 'bastion', True)]))
Пример #5
0
 def create_autoscaling_group(self):
     name = "%sASG" % self.name
     sg_name = CLUSTER_SG_NAME % self.name
     launch_config = "%sLaunchConfig" % name
     elb_name = ELB_NAME % self.name
     t = self.template
     t.add_resource(autoscaling.LaunchConfiguration(
         launch_config,
         ImageId=FindInMap('AmiMap', Ref("AWS::Region"),
                           Ref('ImageName')),
         InstanceType=Ref("InstanceType"),
         KeyName=Ref("SshKeyName"),
         SecurityGroups=[Ref("DefaultSG"), Ref(sg_name)]))
     t.add_resource(autoscaling.AutoScalingGroup(
         name,
         AvailabilityZones=Ref("AvailabilityZones"),
         LaunchConfigurationName=Ref(launch_config),
         MinSize=Ref("MinSize"),
         MaxSize=Ref("MaxSize"),
         VPCZoneIdentifier=Ref("PrivateSubnets"),
         LoadBalancerNames=If("CreateELB", [Ref(elb_name), ], []),
         Tags=[ASTag('Name', self.name, True)]))
Пример #6
0
    def add_autoscaling_group(self):
        t = self.template

        self.asgLaunchConfig = t.add_resource(
            LaunchConfiguration(
                'ASGLaunchConfig',
                ImageId='ami-0b33d91d',  #TODO Mapping for different regions
                InstanceMonitoring=False,
                AssociatePublicIpAddress=False,
                InstanceType="t2.micro",
                SecurityGroups=[Ref(self.asgSg)],
                KeyName=Ref(self.keyPairParam),
                UserData=Base64(
                    Join(
                        "",
                        [
                            "#!/bin/bash -xe\n",
                            "yum update -y aws-cfn-bootstrap\n",
                            "/opt/aws/bin/cfn-init -v ",
                            "         --stack ",
                            {
                                "Ref": "AWS::StackName"
                            },
                            "         --resource LaunchConfig ",
                            "         --configsets wordpress_install ",
                            "         --region ",
                            {
                                "Ref": "AWS::Region"
                            },
                            "\n",

                            # "/opt/aws/bin/cfn-signal -e $? ",
                            # "         --stack ", { "Ref" : "AWS::StackName" },
                            # "         --resource WebServerGroup ",
                            # "         --region ", { "Ref" : "AWS::Region" }, "\n"
                        ])),
                Metadata=cfn.Metadata(
                    cfn.Init(
                        cfn.InitConfigSets(wordpress_install=[
                            'install_cfn', 'install_chefdk', "install_chef",
                            "install_wordpress", "run_chef"
                        ]),
                        install_cfn=cfn.InitConfig(
                            # Starts cfn-hup daemon which detects changes in metadata
                            # and runs user-specified actions when a change is detected.
                            # This allows configuration updates through UpdateStack.
                            # The cfn-hup.conf file stores the name of the stack and
                            # the AWS credentials that the cfn-hup daemon targets.
                            # The cfn-hup daemon parses and loads each file in the /etc/cfn/hooks.d directory.
                            files={
                                "/etc/cfn/cfn-hup.conf": {
                                    "content": {
                                        "Fn::Join": [
                                            "",
                                            [
                                                "[main]\n", "stack=", {
                                                    "Ref": "AWS::StackId"
                                                }, "\n", "region=", {
                                                    "Ref": "AWS::Region"
                                                }, "\n"
                                            ]
                                        ]
                                    },
                                    "mode": "000400",
                                    "owner": "root",
                                    "group": "root"
                                },
                                "/etc/cfn/hooks.d/cfn-auto-reloader.conf": {
                                    "content": {
                                        "Fn::Join": [
                                            "",
                                            [
                                                "[cfn-auto-reloader-hook]\n",
                                                "triggers=post.update\n",
                                                "path=Resources.LaunchConfig.Metadata.AWS::CloudFormation::Init\n",
                                                "action=/opt/aws/bin/cfn-init -v ",
                                                "         --stack ", {
                                                    "Ref": "AWS::StackName"
                                                },
                                                "         --resource LaunchConfig ",
                                                "         --configsets wordpress_install ",
                                                "         --region ", {
                                                    "Ref": "AWS::Region"
                                                }, "\n"
                                            ]
                                        ]
                                    },
                                    "mode": "000400",
                                    "owner": "root",
                                    "group": "root"
                                }
                            },
                            services={
                                "sysvinit": {
                                    "cfn-hup": {
                                        "enabled":
                                        "true",
                                        "ensureRunning":
                                        "true",
                                        "files": [
                                            "/etc/cfn/cfn-hup.conf",
                                            "/etc/cfn/hooks.d/cfn-auto-reloader.conf"
                                        ]
                                    }
                                }
                            }),
                        install_chefdk=cfn.InitConfig(
                            packages={
                                "rpm": {
                                    "chefdk":
                                    "https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chefdk-0.2.0-2.el6.x86_64.rpm"
                                }
                            }),
                        install_chef=cfn.InitConfig(
                            sources={
                                #  Set up a local Chef repository on the instance.
                                "/var/chef/chef-repo":
                                "http://github.com/opscode/chef-repo/tarball/master"
                            },
                            files={
                                #  Chef installation file.
                                "/tmp/install.sh": {
                                    "source":
                                    "https://www.opscode.com/chef/install.sh",
                                    "mode": "000400",
                                    "owner": "root",
                                    "group": "root"
                                },
                                # Knife configuration file.
                                "/var/chef/chef-repo/.chef/knife.rb": {
                                    "content": {
                                        "Fn::Join": [
                                            "",
                                            [
                                                "cookbook_path [ '/var/chef/chef-repo/cookbooks' ]\n",
                                                "node_path [ '/var/chef/chef-repo/nodes' ]\n"
                                            ]
                                        ]
                                    },
                                    "mode": "000400",
                                    "owner": "root",
                                    "group": "root"
                                },
                                # Chef client configuration file.
                                "/var/chef/chef-repo/.chef/client.rb": {
                                    "content": {
                                        "Fn::Join": [
                                            "",
                                            [
                                                "cookbook_path [ '/var/chef/chef-repo/cookbooks' ]\n",
                                                "node_path [ '/var/chef/chef-repo/nodes' ]\n"
                                            ]
                                        ]
                                    },
                                    "mode": "000400",
                                    "owner": "root",
                                    "group": "root"
                                }
                            },
                            commands={
                                #  make the /var/chef directory readable, run the
                                # Chef installation, and then start Chef local mode
                                # by using the client.rb file that was created.
                                # The commands are run in alphanumeric order.
                                "01_make_chef_readable": {
                                    "command": "chmod +rx /var/chef"
                                },
                                "02_install_chef": {
                                    "command": "bash /tmp/install.sh",
                                    "cwd": "/var/chef"
                                },
                                "03_create_node_list": {
                                    "command":
                                    "chef-client -z -c /var/chef/chef-repo/.chef/client.rb",
                                    "cwd": "/var/chef/chef-repo",
                                    "env": {
                                        "HOME": "/var/chef"
                                    }
                                }
                            }),
                        install_wordpress=cfn.InitConfig(
                            # Installs WordPress by using a WordPress cookbook.
                            files={
                                # knife.rb and client.rb files are overwritten to
                                # point to the cookbooks that are required to install WordPress.
                                "/var/chef/chef-repo/.chef/knife.rb": {
                                    "content": {
                                        "Fn::Join": [
                                            "",
                                            [
                                                "cookbook_path [ '/var/chef/chef-repo/cookbooks/wordpress/berks-cookbooks' ]\n",
                                                "node_path [ '/var/chef/chef-repo/nodes' ]\n"
                                            ]
                                        ]
                                    },
                                    "mode": "000400",
                                    "owner": "root",
                                    "group": "root"
                                },
                                "/var/chef/chef-repo/.chef/client.rb": {
                                    "content": {
                                        "Fn::Join": [
                                            "",
                                            [
                                                "cookbook_path [ '/var/chef/chef-repo/cookbooks/wordpress/berks-cookbooks' ]\n",
                                                "node_path [ '/var/chef/chef-repo/nodes' ]\n"
                                            ]
                                        ]
                                    },
                                    "mode": "000400",
                                    "owner": "root",
                                    "group": "root"
                                },
                                #  Specify the Amazon RDS database instance as the WordPress database
                                "/var/chef/chef-repo/cookbooks/wordpress/attributes/aws_rds_config.rb":
                                {
                                    "content": {
                                        "Fn::Join": [
                                            "",
                                            [
                                                "normal['wordpress']['db']['pass'] = '******'\n",
                                                "normal['wordpress']['db']['user'] = '******'\n",
                                                "normal['wordpress']['db']['host'] = '",
                                                GetAtt(self.rds,
                                                       "Endpoint.Address"),
                                                "'\n",
                                                "normal['wordpress']['db']['name'] = '",
                                                Ref(self.dbNameParam), "'\n"
                                            ]
                                        ]
                                    },
                                    "mode": "000400",
                                    "owner": "root",
                                    "group": "root"
                                }
                            },
                            commands={
                                "01_get_cookbook": {
                                    "command":
                                    "knife cookbook site download wordpress",
                                    "cwd": "/var/chef/chef-repo",
                                    "env": {
                                        "HOME": "/var/chef"
                                    }
                                },
                                "02_unpack_cookbook": {
                                    "command":
                                    "tar xvfz /var/chef/chef-repo/wordpress*",
                                    "cwd": "/var/chef/chef-repo/cookbooks"
                                },
                                "03_init_berkshelf": {
                                    "command":
                                    "berks init /var/chef/chef-repo/cookbooks/wordpress --skip-vagrant --skip-git",
                                    "cwd":
                                    "/var/chef/chef-repo/cookbooks/wordpress",
                                    "env": {
                                        "HOME": "/var/chef"
                                    }
                                },
                                "04_vendorize_berkshelf": {
                                    "command": "berks vendor",
                                    "cwd":
                                    "/var/chef/chef-repo/cookbooks/wordpress",
                                    "env": {
                                        "HOME": "/var/chef"
                                    }
                                },
                                "05_configure_node_run_list": {
                                    "command":
                                    "knife node run_list add -z `knife node list -z` recipe[wordpress]",
                                    "cwd": "/var/chef/chef-repo",
                                    "env": {
                                        "HOME": "/var/chef"
                                    }
                                }
                            }),
                        run_chef=cfn.InitConfig(
                            commands={
                                "01_run_chef_client": {
                                    "command":
                                    "chef-client -z -c /var/chef/chef-repo/.chef/client.rb",
                                    "cwd": "/var/chef/chef-repo",
                                    "env": {
                                        "HOME": "/var/chef"
                                    }
                                }
                            })))))

        webserverSubnetIds = [
            self.sceptreUserData['subnets']['privateWebAZ1Id'],
            self.sceptreUserData['subnets']['privateWebAZ2Id'],
            self.sceptreUserData['subnets']['privateWebAZ3Id']
        ]

        self.webServerASG = t.add_resource(
            AutoScalingGroup(
                'WebServerASG',
                LaunchConfigurationName=Ref(self.asgLaunchConfig),
                LoadBalancerNames=[Ref(self.elb)],
                MinSize='1',
                DesiredCapacity='2',
                Cooldown='1',
                MaxSize='5',
                UpdatePolicy=UpdatePolicy(
                    AutoScalingRollingUpdate=AutoScalingRollingUpdate(
                        MinInstancesInService="1")),
                VPCZoneIdentifier=webserverSubnetIds,
                Tags=[
                    ASTag('Contact', Ref(self.ownerEmailParam), True),
                    ASTag('Name', Join("", [self.namePrefix, 'ASG']), True)
                ]))

        return 0