Search in sources :

Example 1 with AirflowDAG

use of io.hops.hopsworks.common.util.templates.airflow.AirflowDAG in project hopsworks by logicalclocks.

the class AirflowService method composeDAG.

@POST
@Path("/dag")
@Produces(MediaType.APPLICATION_JSON)
@AllowedProjectRoles({ AllowedProjectRoles.DATA_OWNER, AllowedProjectRoles.DATA_SCIENTIST })
@JWTRequired(acceptedTokens = { Audience.API }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER" })
@ApiOperation(value = "Generate an Airflow Python DAG file from a DAG definition")
public Response composeDAG(AirflowDagDTO dagDefinition, @Context SecurityContext sc) throws AirflowException {
    Users user = jwtHelper.getUserPrincipal(sc);
    AirflowDAG dag = AirflowDagDTO.toAirflowDagTemplate(dagDefinition, user, project);
    Map<String, Object> dataModel = new HashMap<>(4);
    dataModel.put(AirflowJobLaunchOperator.class.getSimpleName(), AirflowJobLaunchOperator.class);
    dataModel.put(AirflowJobSuccessSensor.class.getSimpleName(), AirflowJobSuccessSensor.class);
    dataModel.put("dag", dag);
    java.nio.file.Path outputFile = Paths.get(airflowJWTManager.getProjectDagDirectory(project.getId()).toString(), dagDefinition.getName() + ".py");
    try (FileWriter out = new FileWriter(outputFile.toFile(), false)) {
        templateEngine.template(AirflowDAG.TEMPLATE_NAME, dataModel, out);
    } catch (IOException | TemplateException ex) {
        throw new AirflowException(RESTCodes.AirflowErrorCode.DAG_NOT_TEMPLATED, Level.SEVERE, "Could not template DAG file for Project " + project.getName(), ex.getMessage(), ex);
    }
    return Response.ok().build();
}
Also used : AirflowDAG(io.hops.hopsworks.common.util.templates.airflow.AirflowDAG) HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) FileWriter(java.io.FileWriter) AirflowJobLaunchOperator(io.hops.hopsworks.common.util.templates.airflow.AirflowJobLaunchOperator) Users(io.hops.hopsworks.persistence.entity.user.Users) AirflowJobSuccessSensor(io.hops.hopsworks.common.util.templates.airflow.AirflowJobSuccessSensor) IOException(java.io.IOException) AirflowException(io.hops.hopsworks.exceptions.AirflowException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) JWTRequired(io.hops.hopsworks.jwt.annotation.JWTRequired) ApiOperation(io.swagger.annotations.ApiOperation) AllowedProjectRoles(io.hops.hopsworks.api.filter.AllowedProjectRoles)

Example 2 with AirflowDAG

use of io.hops.hopsworks.common.util.templates.airflow.AirflowDAG in project hopsworks by logicalclocks.

the class AirflowDagDTO method toAirflowDagTemplate.

public static AirflowDAG toAirflowDagTemplate(AirflowDagDTO dagDefinition, Users owner, Project project) {
    AirflowDAG dag = new AirflowDAG(dagDefinition.getName(), owner.getUsername(), project.getName());
    dag.setScheduleInterval(dagDefinition.getScheduleInterval());
    if (dagDefinition.getApiKey() != null) {
        dag.setApiKey(dagDefinition.getApiKey());
    }
    List<AirflowOperator> operators = new ArrayList<>(dagDefinition.getOperators().size());
    for (AirflowOperatorDTO op : dagDefinition.getOperators()) {
        AirflowOperator operator = null;
        switch(op.getName()) {
            case AirflowJobLaunchOperator.NAME:
                operator = new AirflowJobLaunchOperator(project.getName(), op.getId(), op.getJobName(), op.getJobArgs());
                ((AirflowJobLaunchOperator) operator).setWait(op.isWait());
                break;
            case AirflowJobSuccessSensor.NAME:
                operator = new AirflowJobSuccessSensor(project.getName(), op.getId(), op.getJobName());
                break;
            default:
        }
        if (operator != null) {
            if (op.getDependsOn() != null && !op.getDependsOn().isEmpty()) {
                StringBuilder sb = new StringBuilder();
                if (op.getDependsOn().size() == 1) {
                    sb.append(AirflowOperator.sanitizeId(op.getDependsOn().get(0)));
                } else {
                    sb.append("[");
                    for (String dependency : op.getDependsOn()) {
                        sb.append(AirflowOperator.sanitizeId(dependency)).append(",");
                    }
                    sb.append("]");
                }
                operator.setUpstream(sb.toString());
            }
            operators.add(operator);
        }
    }
    dag.setOperators(operators);
    return dag;
}
Also used : AirflowDAG(io.hops.hopsworks.common.util.templates.airflow.AirflowDAG) ArrayList(java.util.ArrayList) AirflowOperator(io.hops.hopsworks.common.util.templates.airflow.AirflowOperator) AirflowJobLaunchOperator(io.hops.hopsworks.common.util.templates.airflow.AirflowJobLaunchOperator) AirflowJobSuccessSensor(io.hops.hopsworks.common.util.templates.airflow.AirflowJobSuccessSensor)

Aggregations

AirflowDAG (io.hops.hopsworks.common.util.templates.airflow.AirflowDAG)2 AirflowJobLaunchOperator (io.hops.hopsworks.common.util.templates.airflow.AirflowJobLaunchOperator)2 AirflowJobSuccessSensor (io.hops.hopsworks.common.util.templates.airflow.AirflowJobSuccessSensor)2 TemplateException (freemarker.template.TemplateException)1 AllowedProjectRoles (io.hops.hopsworks.api.filter.AllowedProjectRoles)1 AirflowOperator (io.hops.hopsworks.common.util.templates.airflow.AirflowOperator)1 AirflowException (io.hops.hopsworks.exceptions.AirflowException)1 JWTRequired (io.hops.hopsworks.jwt.annotation.JWTRequired)1 Users (io.hops.hopsworks.persistence.entity.user.Users)1 ApiOperation (io.swagger.annotations.ApiOperation)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1