Search in sources :

Example 1 with Operation

use of org.opentosca.toscana.model.operation.Operation in project TOSCAna by StuPro-TOSCAna.

the class DockerfileBuildingVisitor method visit.

@Override
public void visit(MysqlDatabase node) {
    builder.env(ENV_KEY_MYSQL_DATABASE, node.getDatabaseName());
    if (node.getUser().isPresent() && !node.getUser().get().equals("root")) {
        builder.env(ENV_KEY_MYSQL_USER, node.getUser().get());
        builder.env(ENV_KEY_MYSQL_PASSWORD, node.getPassword().orElse(""));
        if (!node.getPassword().isPresent()) {
            builder.env(ENV_KEY_MYSQL_ALLOW_EMPTY_PASSWORD, "true");
        }
    }
    handleDefault(node, new String[] {});
    List<Optional<Operation>> lifecycles = new ArrayList<>();
    lifecycles.add(node.getStandardLifecycle().getConfigure());
    lifecycles.add(node.getStandardLifecycle().getCreate());
    if (lifecycles.stream().anyMatch(Optional::isPresent)) {
        builder.workdir("/docker-entrypoint-initdb.d");
        lifecycles.forEach(e -> {
            if (e.isPresent()) {
                Operation operation = e.get();
                Optional<Artifact> artifact = operation.getArtifact();
                if (artifact.isPresent()) {
                    String path = artifact.get().getFilePath();
                    if (path.endsWith(".sql")) {
                        String filename = determineFilename(path);
                        try {
                            builder.copyFromCsar(path, "", filename);
                        } catch (IOException ex) {
                            logger.error("Copying dependencies of node {} has failed!", node.getEntityName(), ex);
                            throw new TransformationFailureException("Copying dependencies failed", ex);
                        }
                    }
                }
            }
        });
        builder.workdir(TOSCANA_ROOT_WORKDIR_PATH);
    }
}
Also used : Optional(java.util.Optional) TransformationFailureException(org.opentosca.toscana.plugins.util.TransformationFailureException) ArrayList(java.util.ArrayList) Operation(org.opentosca.toscana.model.operation.Operation) IOException(java.io.IOException) Artifact(org.opentosca.toscana.model.artifact.Artifact)

Example 2 with Operation

use of org.opentosca.toscana.model.operation.Operation in project TOSCAna by StuPro-TOSCAna.

the class GraphNormalizerTest method operationNormalization.

@Test
public void operationNormalization() {
    ServiceGraph graph = new ServiceGraph(OPERATION, logMock());
    EntityId lifecycleId = ToscaStructure.NODE_TEMPLATES.descend("test-node").descend(RootNode.INTERFACES.name).descend(RootNode.STANDARD_LIFECYCLE.name);
    EntityId createId = lifecycleId.descend(StandardLifecycle.CREATE.name);
    Entity createEntity = graph.getEntity(createId).get();
    Operation create = new TypeWrapper().wrapEntity((MappingEntity) createEntity, Operation.class);
    assertTrue(create.getArtifact().isPresent());
    Artifact createArtifact = create.getArtifact().get();
    assertEquals("test-artifact", createArtifact.getFilePath());
    EntityId startId = lifecycleId.descend(StandardLifecycle.START.name);
    Entity startEntity = graph.getEntity(startId).get();
    Operation start = new TypeWrapper().wrapEntity((MappingEntity) startEntity, Operation.class);
    assertTrue(start.getArtifact().isPresent());
    Artifact startArtifact = start.getArtifact().get();
    assertEquals("test-artifact2", startArtifact.getFilePath());
}
Also used : EntityId(org.opentosca.toscana.model.EntityId) ScalarEntity(org.opentosca.toscana.core.parse.model.ScalarEntity) Entity(org.opentosca.toscana.core.parse.model.Entity) MappingEntity(org.opentosca.toscana.core.parse.model.MappingEntity) ServiceGraph(org.opentosca.toscana.core.parse.model.ServiceGraph) Operation(org.opentosca.toscana.model.operation.Operation) Artifact(org.opentosca.toscana.model.artifact.Artifact) BaseUnitTest(org.opentosca.toscana.core.BaseUnitTest) Test(org.junit.Test)

Example 3 with Operation

use of org.opentosca.toscana.model.operation.Operation in project TOSCAna by StuPro-TOSCAna.

the class DockerfileBuildingVisitor method copyArtifactsOfLifecycleOperation.

/**
 *     This method copies the the artifact of the lifecycle operation into the Dockerfile working directory (including its dependencies)
 *     <p>
 *     It will also write the Run and Copy commands into the Dockerfile
 *     <p>
 *     Properties of the Operations will get mapped as Environment variables (ENV command)
 */
private void copyArtifactsOfLifecycleOperation(String nodeName, String opName, Optional<Operation> optionalOperation, String[] ignoredLifecycles, boolean isStartup) throws IOException {
    // Skip ignored lifecycles
    if (Arrays.asList(ignoredLifecycles).contains(opName)) {
        return;
    }
    // Add the artifacts if they are present
    if (optionalOperation.isPresent()) {
        // Set the properties as environment variables
        optionalOperation.get().getInputs().forEach(e -> {
            if (e.getValue().isPresent()) {
                logger.info("Adding Environment Variable {}:{}", e.getKey(), e.getValue().get());
                builder.env(e.getKey(), e.getValue().get());
            }
        });
        logger.debug("{} - {} is present", nodeName, opName);
        Operation operation = optionalOperation.get();
        // Copy all Dependencies
        for (String e : operation.getDependencies()) {
            String filename = determineFilename(e);
            builder.copyFromCsar(e, nodeName, filename);
        }
        if (operation.getArtifact().isPresent()) {
            String path = operation.getArtifact().get().getFilePath();
            // Ignore SQL files since their handled elsewhere (for MySQL)
            if (path.endsWith(".sql")) {
                return;
            }
            // this is the case if a script contains "sudo "
            if (needsSudo(path) && !sudoInstalled) {
                // Install sudo, currently only works with Debian based systems
                Optional<String> sudocmd = SudoUtils.getSudoInstallCommand(this.baseImage);
                if (sudocmd.isPresent()) {
                    builder.run(sudocmd.get());
                    sudoInstalled = true;
                } else {
                    throw new TransformationFailureException("Cannot determine Sudo install command for base image '" + this.baseImage + "'");
                }
            }
            builder.copyFromCsar(path, nodeName, nodeName + "-" + opName);
            String command = "sh " + nodeName + "-" + opName;
            // the building procedure
            if (!isStartup) {
                builder.run(command);
            } else {
                startCommands.add(command);
            }
        }
    }
}
Also used : TransformationFailureException(org.opentosca.toscana.plugins.util.TransformationFailureException) Operation(org.opentosca.toscana.model.operation.Operation)

Example 4 with Operation

use of org.opentosca.toscana.model.operation.Operation in project TOSCAna by StuPro-TOSCAna.

the class LinkResolverTest method resolveImplementationLink.

@Test
public void resolveImplementationLink() {
    EffectiveModel model = new EffectiveModelFactory().create(ARTIFACT, logMock());
    WebServer node = (WebServer) model.getNodeMap().get("test-node");
    Optional<Operation> create = node.getStandardLifecycle().getCreate();
    assertTrue(create.isPresent());
    Optional<Artifact> artifact = create.get().getArtifact();
    assertTrue(artifact.isPresent());
    assertEquals("test-file", artifact.get().getFilePath());
}
Also used : WebServer(org.opentosca.toscana.model.node.WebServer) Operation(org.opentosca.toscana.model.operation.Operation) EffectiveModel(org.opentosca.toscana.model.EffectiveModel) EffectiveModelFactory(org.opentosca.toscana.model.EffectiveModelFactory) Artifact(org.opentosca.toscana.model.artifact.Artifact) BaseUnitTest(org.opentosca.toscana.core.BaseUnitTest) Test(org.junit.Test)

Example 5 with Operation

use of org.opentosca.toscana.model.operation.Operation in project TOSCAna by StuPro-TOSCAna.

the class OperationHandler method handleConfigure.

/**
 *     Handles a configure operation.
 *
 *     @param node            which the operation belongs to
 *     @param computeHostName alphanumerical name of the Compute host of node
 */
public void handleConfigure(RootNode node, String computeHostName) {
    if (node.getStandardLifecycle().getConfigure().isPresent()) {
        Operation configure = node.getStandardLifecycle().getConfigure().get();
        handleOperation(configure, computeHostName, CONFIG_CONFIGURE);
    }
}
Also used : Operation(org.opentosca.toscana.model.operation.Operation)

Aggregations

Operation (org.opentosca.toscana.model.operation.Operation)10 Artifact (org.opentosca.toscana.model.artifact.Artifact)5 Test (org.junit.Test)2 BaseUnitTest (org.opentosca.toscana.core.BaseUnitTest)2 OperationVariable (org.opentosca.toscana.model.operation.OperationVariable)2 TransformationFailureException (org.opentosca.toscana.plugins.util.TransformationFailureException)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 Entity (org.opentosca.toscana.core.parse.model.Entity)1 MappingEntity (org.opentosca.toscana.core.parse.model.MappingEntity)1 ScalarEntity (org.opentosca.toscana.core.parse.model.ScalarEntity)1 ServiceGraph (org.opentosca.toscana.core.parse.model.ServiceGraph)1 EffectiveModel (org.opentosca.toscana.model.EffectiveModel)1 EffectiveModelFactory (org.opentosca.toscana.model.EffectiveModelFactory)1 EntityId (org.opentosca.toscana.model.EntityId)1 RootNode (org.opentosca.toscana.model.node.RootNode)1 WebServer (org.opentosca.toscana.model.node.WebServer)1 Interface (org.opentosca.toscana.model.operation.Interface)1 StandardLifecycle (org.opentosca.toscana.model.operation.StandardLifecycle)1