Search in sources :

Example 1 with Instance

use of com.scaleset.cfbuilder.ec2.Instance in project TOSCAna by StuPro-TOSCAna.

the class OperationHandler method handleDependency.

/**
 *     Adds all dependencies to file uploads and to the EC2 Instance in the CloudFormation template.
 *
 *     @param operation  to be handled
 *     @param serverName name of the Compute/EC2 where the dependencies must be stored
 *     @param config     name of the config (Create/Start/Configure)
 */
private void handleDependency(Operation operation, String serverName, String config) {
    // Add dependencies
    for (String dependency : operation.getDependencies()) {
        markFile(dependency);
        CFNFile cfnFile = handleOperationFile(dependency, MODE_644, serverName);
        // Add file to config
        cfnModule.getCFNInit(serverName).getOrAddConfig(CONFIG_SETS, config).putFile(cfnFile);
    }
}
Also used : CFNFile(com.scaleset.cfbuilder.ec2.metadata.CFNFile)

Example 2 with Instance

use of com.scaleset.cfbuilder.ec2.Instance in project TOSCAna by StuPro-TOSCAna.

the class OperationHandler method handleArtifact.

/**
 *     Adds all artifacts to file uploads and to the EC2 Instance in the CloudFormation template.
 *     Also adds them as commands with input variables as environment variables to the given config.
 *
 *     @param operation  to be handled
 *     @param serverName name of the Compute/EC2 where the artifacts must be stored and executed
 *     @param config     name of the config (Create/Start/Configure)
 */
private void handleArtifact(Operation operation, String serverName, String config) {
    // Add artifact
    if (operation.getArtifact().isPresent()) {
        String artifact = operation.getArtifact().get().getFilePath();
        Set<OperationVariable> inputs = operation.getInputs();
        CFNCommand cfnCommand = handleOperationCommand(artifact, inputs);
        markFile(artifact);
        CFNFile cfnFile = handleOperationFile(artifact, MODE_500, serverName);
        // Add file to config and execution command
        cfnModule.getCFNInit(serverName).getOrAddConfig(CONFIG_SETS, config).putFile(cfnFile).putCommand(cfnCommand);
    }
}
Also used : OperationVariable(org.opentosca.toscana.model.operation.OperationVariable) CFNFile(com.scaleset.cfbuilder.ec2.metadata.CFNFile) CFNCommand(com.scaleset.cfbuilder.ec2.metadata.CFNCommand)

Example 3 with Instance

use of com.scaleset.cfbuilder.ec2.Instance in project TOSCAna by StuPro-TOSCAna.

the class CapabilityMapper method mapDiskSize.

/**
 *     Maps the disk_size property of a ComputeCapability to an EC2 Instance.
 *
 *     @param computeCapability Capability containing the disk_size property
 *     @param cfnModule Module containing the Instance
 *     @param nodeName name of the Instance
 */
public void mapDiskSize(ComputeCapability computeCapability, CloudFormationModule cfnModule, String nodeName) {
    // If disk_size is not set, default to 8000 Mb
    Integer diskSizeInMb = computeCapability.getDiskSizeInMb().orElse(8000);
    // Convert disk_size to Gb
    Integer diskSizeInGb = diskSizeInMb / 1000;
    logger.debug("Check diskSize: '{}' Gb", diskSizeInGb);
    if (diskSizeInGb < 8) {
        logger.warn("Disk size of '{}' smaller than the minimum value required by EC2 Instances. Setting the disk size of '{}' to the minimum allowed value of 8 Gb.", nodeName, nodeName);
        diskSizeInGb = 8;
    }
    // Add BlockDeviceMapping if needed
    if (diskSizeInGb > 8) {
        logger.debug("Disk size of '{}' bigger than the default value of EC2 Instances. Adding a BlockDeviceMapping to '{}'.", nodeName, nodeName);
        Instance computeAsInstance = (Instance) cfnModule.getResource(nodeName);
        computeAsInstance.blockDeviceMappings(new EC2BlockDeviceMapping().deviceName("/dev/sda1").ebs(new EC2EBSBlockDevice().volumeSize(diskSizeInGb.toString())));
    }
}
Also used : EC2BlockDeviceMapping(com.scaleset.cfbuilder.ec2.instance.EC2BlockDeviceMapping) EC2EBSBlockDevice(com.scaleset.cfbuilder.ec2.instance.ec2blockdevicemapping.EC2EBSBlockDevice) Instance(com.scaleset.cfbuilder.ec2.Instance)

Example 4 with Instance

use of com.scaleset.cfbuilder.ec2.Instance in project TOSCAna by StuPro-TOSCAna.

the class TransformModelNodeVisitor method visit.

@Override
public void visit(Compute node) {
    try {
        if (cfnModule.checkComputeToEc2(node)) {
            logger.debug("Compute '{}' will be transformed to EC2", node.getEntityName());
            String nodeName = toAlphanumerical(node.getEntityName());
            // default security group the EC2 Instance
            SecurityGroup webServerSecurityGroup = cfnModule.resource(SecurityGroup.class, nodeName + SECURITY_GROUP).groupDescription("Enables ports for " + nodeName + ".");
            // open endpoint port
            node.getEndpoint().getPort().ifPresent(port -> webServerSecurityGroup.ingress(ingress -> ingress.cidrIp(IP_OPEN), PROTOCOL_TCP, port.port));
            // check what image id should be taken
            CapabilityMapper capabilityMapper = createCapabilityMapper();
            OsCapability computeOs = node.getOs();
            String imageId = capabilityMapper.mapOsCapabilityToImageId(computeOs);
            ComputeCapability computeCompute = node.getHost();
            String instanceType = capabilityMapper.mapComputeCapabilityToInstanceType(computeCompute, CapabilityMapper.EC2_DISTINCTION);
            // create CFN init and store it
            CFNInit init = new CFNInit(CONFIG_SETS);
            cfnModule.putCFNInit(nodeName, init);
            cfnModule.resource(Instance.class, nodeName).securityGroupIds(webServerSecurityGroup).imageId(imageId).instanceType(instanceType);
            capabilityMapper.mapDiskSize(computeCompute, cfnModule, nodeName);
            // Add Reference to keyName if KeyPair needed and open Port 22 (Allows SSH access)
            if (cfnModule.hasKeyPair()) {
                Instance instance = (Instance) cfnModule.getResource(nodeName);
                instance.keyName(cfnModule.getKeyNameVar());
                webServerSecurityGroup.ingress(ingress -> ingress.cidrIp(IP_OPEN), PROTOCOL_TCP, 22);
            }
        } else {
            logger.debug("Compute '{}' will not be transformed to EC2", node.getEntityName());
        }
    } catch (SdkClientException se) {
        logger.error("SDKClient failed, no valid credentials or no internet connection");
        throw new TransformationFailureException("Failed", se);
    } catch (Exception e) {
        logger.error("Error while creating EC2Instance resource.");
        throw new TransformationFailureException("Failed at Compute node " + node.getEntityName(), e);
    }
}
Also used : Apache(org.opentosca.toscana.model.node.Apache) EndpointCapability(org.opentosca.toscana.model.capability.EndpointCapability) CFNInit(com.scaleset.cfbuilder.ec2.metadata.CFNInit) CONFIG_CONFIGURE(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule.CONFIG_CONFIGURE) CapabilityMapper(org.opentosca.toscana.plugins.cloudformation.mapper.CapabilityMapper) SECURITY_GROUP(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule.SECURITY_GROUP) ArrayList(java.util.ArrayList) TransformationFailureException(org.opentosca.toscana.plugins.util.TransformationFailureException) CFNPackage(com.scaleset.cfbuilder.ec2.metadata.CFNPackage) MysqlDatabase(org.opentosca.toscana.model.node.MysqlDatabase) CFNCommand(com.scaleset.cfbuilder.ec2.metadata.CFNCommand) CONFIG_CREATE(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule.CONFIG_CREATE) CONFIG_SETS(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule.CONFIG_SETS) MysqlDbms(org.opentosca.toscana.model.node.MysqlDbms) APACHE_RESTART_COMMAND(org.opentosca.toscana.plugins.cloudformation.handler.OperationHandler.APACHE_RESTART_COMMAND) WebApplication(org.opentosca.toscana.model.node.WebApplication) FILEPATH_NODEJS_CREATE(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule.FILEPATH_NODEJS_CREATE) APACHE_ENV_IMPORT(org.opentosca.toscana.plugins.cloudformation.handler.EnvironmentHandler.APACHE_ENV_IMPORT) Artifact(org.opentosca.toscana.model.artifact.Artifact) Compute(org.opentosca.toscana.model.node.Compute) ComputeCapability(org.opentosca.toscana.model.capability.ComputeCapability) Database(org.opentosca.toscana.model.node.Database) OperationHandler(org.opentosca.toscana.plugins.cloudformation.handler.OperationHandler) SecurityGroup(com.scaleset.cfbuilder.ec2.SecurityGroup) Dbms(org.opentosca.toscana.model.node.Dbms) Nodejs(org.opentosca.toscana.model.node.Nodejs) Instance(com.scaleset.cfbuilder.ec2.Instance) Set(java.util.Set) OsCapability(org.opentosca.toscana.model.capability.OsCapability) StrictNodeVisitor(org.opentosca.toscana.model.visitor.StrictNodeVisitor) List(java.util.List) SdkClientException(com.amazonaws.SdkClientException) CloudFormationLifecycle.toAlphanumerical(org.opentosca.toscana.plugins.cloudformation.CloudFormationLifecycle.toAlphanumerical) TransformationContext(org.opentosca.toscana.core.transformation.TransformationContext) DBInstance(com.scaleset.cfbuilder.rds.DBInstance) CONFIG_START(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule.CONFIG_START) CloudFormationModule(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule) SdkClientException(com.amazonaws.SdkClientException) TransformationFailureException(org.opentosca.toscana.plugins.util.TransformationFailureException) Instance(com.scaleset.cfbuilder.ec2.Instance) DBInstance(com.scaleset.cfbuilder.rds.DBInstance) OsCapability(org.opentosca.toscana.model.capability.OsCapability) CFNInit(com.scaleset.cfbuilder.ec2.metadata.CFNInit) SecurityGroup(com.scaleset.cfbuilder.ec2.SecurityGroup) CapabilityMapper(org.opentosca.toscana.plugins.cloudformation.mapper.CapabilityMapper) TransformationFailureException(org.opentosca.toscana.plugins.util.TransformationFailureException) SdkClientException(com.amazonaws.SdkClientException) ComputeCapability(org.opentosca.toscana.model.capability.ComputeCapability)

Example 5 with Instance

use of com.scaleset.cfbuilder.ec2.Instance in project TOSCAna by StuPro-TOSCAna.

the class CloudFormationModule method build.

/**
 *     Build the template
 *     1. Add CFNInit to corresponding instance resource
 *     2. Check if EC2 instances need access to S3. If yes, then
 *     2a. Add necessary IAM resources to the module
 *     2b. Add <tt>Authentication<tt> and <tt>IamInstanceProfile<tt> to corresponding instance resource
 */
@Override
public void build() {
    for (Map.Entry<String, CFNInit> pair : cfnInitMap.entrySet()) {
        Resource res = this.getResource(pair.getKey());
        if (res instanceof Instance) {
            Instance instance = (Instance) res;
            if (!pair.getValue().getConfigs().isEmpty()) {
                instance.addCFNInit(pair.getValue()).userData(new UserData(getUserDataFn(pair.getKey(), CONFIG_SETS, this)));
            }
        }
    }
    if (!fileUploadList.isEmpty()) {
        Role instanceRole = getS3InstanceRole(this);
        getS3Policy(this).roles(instanceRole);
        getS3InstanceProfile(this).roles(instanceRole);
        Authentication s3authentication = getS3Authentication(bucketName);
        for (String instanceName : authenticationSet) {
            Resource res = this.getResource(instanceName);
            if (res instanceof Instance) {
                Instance instance = (Instance) res;
                instance.authentication(s3authentication).iamInstanceProfile(ref(INSTANCE_PROFILE));
            }
        }
    }
    if (this.hasKeyPair()) {
        strParam(KEYNAME).type(KEYNAME_TYPE).description(KEYNAME_DESCRIPTION).constraintDescription(KEYNAME_CONSTRAINT_DESCRIPTION);
    }
}
Also used : AuthenticationUtils.getS3InstanceRole(org.opentosca.toscana.plugins.cloudformation.util.AuthenticationUtils.getS3InstanceRole) Role(com.scaleset.cfbuilder.iam.Role) Instance(com.scaleset.cfbuilder.ec2.Instance) UserData(com.scaleset.cfbuilder.ec2.UserData) AuthenticationUtils.getS3Authentication(org.opentosca.toscana.plugins.cloudformation.util.AuthenticationUtils.getS3Authentication) Authentication(com.scaleset.cfbuilder.cloudformation.Authentication) Resource(com.scaleset.cfbuilder.core.Resource) CFNInit(com.scaleset.cfbuilder.ec2.metadata.CFNInit) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Instance (com.scaleset.cfbuilder.ec2.Instance)3 CFNCommand (com.scaleset.cfbuilder.ec2.metadata.CFNCommand)3 CFNFile (com.scaleset.cfbuilder.ec2.metadata.CFNFile)3 CFNInit (com.scaleset.cfbuilder.ec2.metadata.CFNInit)2 SdkClientException (com.amazonaws.SdkClientException)1 Authentication (com.scaleset.cfbuilder.cloudformation.Authentication)1 Resource (com.scaleset.cfbuilder.core.Resource)1 SecurityGroup (com.scaleset.cfbuilder.ec2.SecurityGroup)1 UserData (com.scaleset.cfbuilder.ec2.UserData)1 EC2BlockDeviceMapping (com.scaleset.cfbuilder.ec2.instance.EC2BlockDeviceMapping)1 EC2EBSBlockDevice (com.scaleset.cfbuilder.ec2.instance.ec2blockdevicemapping.EC2EBSBlockDevice)1 CFNPackage (com.scaleset.cfbuilder.ec2.metadata.CFNPackage)1 Role (com.scaleset.cfbuilder.iam.Role)1 DBInstance (com.scaleset.cfbuilder.rds.DBInstance)1 Files (gen.core.filters.Files)1 Filter (gen.core.filters.Filter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1