Search in sources :

Example 1 with Compute

use of org.opentosca.toscana.model.node.Compute 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 2 with Compute

use of org.opentosca.toscana.model.node.Compute in project TOSCAna by StuPro-TOSCAna.

the class TransformModelNodeVisitor method visit.

@Override
public void visit(Nodejs node) {
    try {
        Compute computeHost = getCompute(node);
        String computeHostName = toAlphanumerical(computeHost.getEntityName());
        String nodeName = node.getEntityName();
        // handle configure
        operationHandler.handleConfigure(node, computeHostName);
        // handle start
        operationHandler.handleStart(node, computeHostName);
        // add NodeJs create script
        operationHandler.addCreate(FILEPATH_NODEJS_CREATE, computeHostName);
        // Get ports
        List<Integer> portList = new ArrayList<>();
        node.getCapabilities().forEach(e -> {
            try {
                if (e instanceof EndpointCapability && ((EndpointCapability) e).getPort().isPresent()) {
                    int port = ((EndpointCapability) e).getPort().get().port;
                    logger.debug("Marking '{}' as port to be opened for '{}'.", port, nodeName);
                    portList.add(port);
                }
            } catch (Exception ex) {
                logger.warn("Failed reading Port from node {}", nodeName, ex);
            }
        });
        // Open ports
        String SecurityGroupName = computeHostName + SECURITY_GROUP;
        SecurityGroup securityGroup = (SecurityGroup) cfnModule.getResource(SecurityGroupName);
        securityGroup.ingress(ingress -> ingress.cidrIp(IP_OPEN), PROTOCOL_TCP, portList.toArray());
    } catch (Exception e) {
        logger.error("Error while creating Nodejs");
        throw new TransformationFailureException("Failed at Nodejs node " + node.getEntityName(), e);
    }
}
Also used : TransformationFailureException(org.opentosca.toscana.plugins.util.TransformationFailureException) Compute(org.opentosca.toscana.model.node.Compute) ArrayList(java.util.ArrayList) SecurityGroup(com.scaleset.cfbuilder.ec2.SecurityGroup) TransformationFailureException(org.opentosca.toscana.plugins.util.TransformationFailureException) SdkClientException(com.amazonaws.SdkClientException) EndpointCapability(org.opentosca.toscana.model.capability.EndpointCapability)

Example 3 with Compute

use of org.opentosca.toscana.model.node.Compute in project TOSCAna by StuPro-TOSCAna.

the class TransformModelNodeVisitor method visit.

@Override
public void visit(Database node) {
    try {
        Compute computeHost = getCompute(node);
        String computeHostName = toAlphanumerical(computeHost.getEntityName());
        operationHandler.handleGenericHostedNode(node, computeHost);
        // Open Database port
        String SecurityGroupName = computeHostName + SECURITY_GROUP;
        SecurityGroup securityGroup = (SecurityGroup) cfnModule.getResource(SecurityGroupName);
        if (node.getPort().isPresent()) {
            Integer databasePort = node.getPort().orElseThrow(() -> new IllegalArgumentException("Database " + "port not set"));
            Set<Compute> hostsOfConnectedTo = getHostsOfConnectedTo(node);
            for (Compute hostOfConnectedTo : hostsOfConnectedTo) {
                securityGroup.ingress(ingress -> ingress.sourceSecurityGroupName(cfnModule.ref(toAlphanumerical(hostOfConnectedTo.getEntityName()) + SECURITY_GROUP)), PROTOCOL_TCP, databasePort);
            }
        }
    } catch (Exception e) {
        logger.error("Error while creating Database resource.");
        throw new TransformationFailureException("Failed at Database node " + node.getEntityName(), e);
    }
}
Also used : TransformationFailureException(org.opentosca.toscana.plugins.util.TransformationFailureException) Compute(org.opentosca.toscana.model.node.Compute) SecurityGroup(com.scaleset.cfbuilder.ec2.SecurityGroup) TransformationFailureException(org.opentosca.toscana.plugins.util.TransformationFailureException) SdkClientException(com.amazonaws.SdkClientException)

Example 4 with Compute

use of org.opentosca.toscana.model.node.Compute in project TOSCAna by StuPro-TOSCAna.

the class TransformModelNodeVisitor method visit.

@Override
public void visit(Apache node) {
    try {
        Compute compute = getCompute(node);
        String computeName = toAlphanumerical(compute.getEntityName());
        // instead of lifecycle create we add the package apache2 to the configset
        cfnModule.getCFNInit(computeName).getOrAddConfig(CONFIG_SETS, CONFIG_CREATE).putPackage(// TODO apt only if linux
        new CFNPackage("apt").addPackage("apache2"));
        // handle configure
        operationHandler.handleConfigure(node, computeName);
        // handle start
        operationHandler.handleStart(node, computeName);
        // Source environment variables in /etc/apache/envvars from /etc/environment and restart apache2 directly
        // afterwards
        cfnModule.getCFNInit(computeName).getOrAddConfig(CONFIG_SETS, CONFIG_CONFIGURE).putCommand(new CFNCommand("Add Apache environment variables", APACHE_ENV_IMPORT));
        // we add restart apache2 command to the configscript if start or configure existed
        if (node.getStandardLifecycle().getConfigure().isPresent() || node.getStandardLifecycle().getStart().isPresent()) {
            cfnModule.getCFNInit(computeName).getOrAddConfig(CONFIG_SETS, CONFIG_START).putCommand(new CFNCommand("restart apache2", APACHE_RESTART_COMMAND));
        }
    } catch (Exception e) {
        logger.error("Error while creating Apache");
        throw new TransformationFailureException("Failed at Apache node " + node.getEntityName(), e);
    }
}
Also used : CFNPackage(com.scaleset.cfbuilder.ec2.metadata.CFNPackage) TransformationFailureException(org.opentosca.toscana.plugins.util.TransformationFailureException) Compute(org.opentosca.toscana.model.node.Compute) CFNCommand(com.scaleset.cfbuilder.ec2.metadata.CFNCommand) TransformationFailureException(org.opentosca.toscana.plugins.util.TransformationFailureException) SdkClientException(com.amazonaws.SdkClientException)

Example 5 with Compute

use of org.opentosca.toscana.model.node.Compute in project TOSCAna by StuPro-TOSCAna.

the class CloudFormationPluginTest method testLamp.

@Test(expected = TransformationFailureException.class)
public void testLamp() {
    try {
        Set<RootNode> nodes = lamp.getNodes();
        // visit compute nodes first
        for (VisitableNode node : nodes) {
            if (node instanceof Compute) {
                node.accept(cfnNodeVisitor);
            }
        }
        for (VisitableNode node : nodes) {
            if (!(node instanceof Compute)) {
                node.accept(cfnNodeVisitor);
            }
        }
        System.err.println(cfnModule.toString());
    } catch (TransformationFailureException tfe) {
        // provided so this test can pass
        if (!(tfe.getCause() instanceof SdkClientException)) {
            throw tfe;
        }
        logger.debug("Passed without internet connection / credentials provided");
    }
}
Also used : RootNode(org.opentosca.toscana.model.node.RootNode) VisitableNode(org.opentosca.toscana.model.visitor.VisitableNode) TransformationFailureException(org.opentosca.toscana.plugins.util.TransformationFailureException) SdkClientException(com.amazonaws.SdkClientException) Compute(org.opentosca.toscana.model.node.Compute) BaseUnitTest(org.opentosca.toscana.core.BaseUnitTest) Test(org.junit.Test)

Aggregations

Compute (org.opentosca.toscana.model.node.Compute)15 SdkClientException (com.amazonaws.SdkClientException)8 TransformationFailureException (org.opentosca.toscana.plugins.util.TransformationFailureException)8 SecurityGroup (com.scaleset.cfbuilder.ec2.SecurityGroup)6 RootNode (org.opentosca.toscana.model.node.RootNode)4 WebApplication (org.opentosca.toscana.model.node.WebApplication)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 BaseUnitTest (org.opentosca.toscana.core.BaseUnitTest)3 MysqlDatabase (org.opentosca.toscana.model.node.MysqlDatabase)3 Fn (com.scaleset.cfbuilder.core.Fn)2 CFNCommand (com.scaleset.cfbuilder.ec2.metadata.CFNCommand)2 CFNPackage (com.scaleset.cfbuilder.ec2.metadata.CFNPackage)2 List (java.util.List)2 TransformationContext (org.opentosca.toscana.core.transformation.TransformationContext)2 EffectiveModel (org.opentosca.toscana.model.EffectiveModel)2 EffectiveModelFactory (org.opentosca.toscana.model.EffectiveModelFactory)2 Artifact (org.opentosca.toscana.model.artifact.Artifact)2 ComputeCapability (org.opentosca.toscana.model.capability.ComputeCapability)2 EndpointCapability (org.opentosca.toscana.model.capability.EndpointCapability)2