def test_decode(self):
     ret = mime_packer.pack(self.parts)
     parts = [part for part in email.message_from_string(ret).walk()]
     compare(
         [part.get_content_type() for part in parts],
         ["multipart/mixed", "text/cloud-boothook", "text/cloud-config"],
         prefix="mimeparts are in expected order")
     compare(parts[1].get_payload(), "MORESTRING")
     compare(parts[2].get_payload(), "SOMESTRING")
 def test_encode(self):
     ret = mime_packer.pack(self.parts)
     self.assertTrue('SOMESTRING' in ret)
     self.assertTrue('MORESTRING' in ret)
예제 #3
0
    def ec2(self):
        # LOAD STACK TEMPLATE
        data = self.data["ec2"]
        resources = []
        sgs = []

        for sg_name, ingress in data["security_groups"].items():
            sg = SecurityGroup(sg_name, VpcId=Ref("VPC"), GroupDescription="BaseHost Security Group")

            sgs.append(sg)
            resources.append(sg)

            # Because we want to be able to add ingress rules to a security
            # group that referes to itself (for example allow all instances in
            # the sg to speak to each other on 9300 for Elasticsearch
            # clustering) we create the SG in one resource and rules as other
            # resources
            #
            # The yaml for this case is:
            #
            # security_groups:
            #   EScluster:
            #     - FromPort: 9300
            #     - ToPort: 9300
            #     - SourceSecurityGroupId: { Ref: EScluster }
            for idx, rule in enumerate(ingress):
                # Convert { Ref: "x"} to Ref("x")
                rule = self._convert_ref_dict_to_objects(rule)

                ingress = SecurityGroupIngress("{}Rule{}".format(sg_name, idx), GroupId=Ref(sg), **rule)
                resources.append(ingress)

        devices = []
        try:
            for i in data["block_devices"]:
                devices.append(
                    BlockDeviceMapping(DeviceName=i["DeviceName"], Ebs=EBSBlockDevice(VolumeSize=i["VolumeSize"]))
                )
        except KeyError:
            devices.append(BlockDeviceMapping(DeviceName="/dev/sda1", Ebs=EBSBlockDevice(VolumeSize=20)))

        launch_config = LaunchConfiguration(
            "BaseHostLaunchConfig",
            KeyName=data["parameters"]["KeyName"],
            SecurityGroups=[Ref(g) for g in sgs],
            InstanceType=data["parameters"]["InstanceType"],
            AssociatePublicIpAddress=True,
            IamInstanceProfile=Ref("InstanceProfile"),
            ImageId=FindInMap("AWSRegion2AMI", Ref("AWS::Region"), "AMI"),
            BlockDeviceMappings=devices,
        )

        user_data = self.get_ec2_userdata()
        if user_data:
            user_data = mime_packer.pack(user_data)
            launch_config.UserData = Base64(user_data)

        resources.append(launch_config)

        # Allow deprecation of tags
        ec2_tags = []
        deprecated_tags = ["Env"]
        for k, v in data["tags"].items():
            if k not in deprecated_tags:
                ec2_tags.append(Tag(k, v, True))
            else:
                logging.warning("config: Tag '%s' is deprecated.." % (k))

        scaling_group = AutoScalingGroup(
            "ScalingGroup",
            VPCZoneIdentifier=[Ref("SubnetA"), Ref("SubnetB"), Ref("SubnetC")],
            MinSize=data["auto_scaling"]["min"],
            MaxSize=data["auto_scaling"]["max"],
            DesiredCapacity=data["auto_scaling"]["desired"],
            AvailabilityZones=GetAZs(),
            Tags=ec2_tags,
            LaunchConfigurationName=Ref(launch_config),
        )
        resources.append(scaling_group)

        return resources