Search in sources :

Example 1 with PipelineRun

use of com.epam.pipeline.entity.pipeline.PipelineRun in project cloud-pipeline by epam.

the class NotificationManager method notifyIdleRuns.

/**
 * Issues a notification of an idle Pipeline Run for multiple runs.
 *
 * @param pipelineCpuRatePairs a list of pairs of PipelineRun and Double cpu usage rate value
 * @param notificationType a type of notification to be issued. Supported types are IDLE_RUN, IDLE_RUN_PAUSED,
 *                         IDLE_RUN_STOPPED
 * @throws IllegalArgumentException if notificationType is not from IDLE_RUN group
 */
@Transactional(propagation = Propagation.REQUIRED)
public void notifyIdleRuns(List<Pair<PipelineRun, Double>> pipelineCpuRatePairs, NotificationType notificationType) {
    if (CollectionUtils.isEmpty(pipelineCpuRatePairs)) {
        return;
    }
    Assert.isTrue(NotificationSettings.NotificationGroup.IDLE_RUN == notificationType.getGroup(), "Only IDLE_RUN group notification types are allowed");
    NotificationSettings idleRunSettings = notificationSettingsManager.load(notificationType);
    if (idleRunSettings == null || !idleRunSettings.isEnabled()) {
        LOGGER.info("No template configured for idle pipeline run notifications or it was disabled!");
        return;
    }
    List<Long> ccUserIds = getKeepInformedUserIds(idleRunSettings);
    if (idleRunSettings.isKeepInformedAdmins()) {
        ExtendedRole extendedRole = roleManager.loadRoleWithUsers(DefaultRoles.ROLE_ADMIN.getId());
        ccUserIds.addAll(extendedRole.getUsers().stream().map(PipelineUser::getId).collect(Collectors.toList()));
    }
    double idleCpuLevel = preferenceManager.getPreference(SystemPreferences.SYSTEM_IDLE_CPU_THRESHOLD_PERCENT);
    Map<String, PipelineUser> pipelineOwners = userManager.loadUsersByNames(pipelineCpuRatePairs.stream().map(p -> p.getLeft().getOwner()).collect(Collectors.toList())).stream().collect(Collectors.toMap(PipelineUser::getUserName, user -> user));
    List<NotificationMessage> messages = pipelineCpuRatePairs.stream().map(pair -> {
        NotificationMessage message = new NotificationMessage();
        message.setTemplate(new NotificationTemplate(idleRunSettings.getTemplateId()));
        message.setTemplateParameters(PipelineRunMapper.map(pair.getLeft(), null));
        message.getTemplateParameters().put("idleCpuLevel", idleCpuLevel);
        message.getTemplateParameters().put("cpuRate", pair.getRight() * PERCENT);
        message.setToUserId(pipelineOwners.getOrDefault(pair.getLeft().getOwner(), new PipelineUser()).getId());
        message.setCopyUserIds(ccUserIds);
        return message;
    }).collect(Collectors.toList());
    monitoringNotificationDao.createMonitoringNotifications(messages);
}
Also used : PipelineRunMapper(com.epam.pipeline.mapper.PipelineRunMapper) Arrays(java.util.Arrays) MessageConstants(com.epam.pipeline.common.MessageConstants) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) SystemPreferences(com.epam.pipeline.manager.preference.SystemPreferences) ArrayList(java.util.ArrayList) PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) Matcher(java.util.regex.Matcher) Pair(org.apache.commons.lang3.tuple.Pair) MessageHelper(com.epam.pipeline.common.MessageHelper) Propagation(org.springframework.transaction.annotation.Propagation) ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) Service(org.springframework.stereotype.Service) Map(java.util.Map) NotificationType(com.epam.pipeline.entity.notification.NotificationSettings.NotificationType) IssueComment(com.epam.pipeline.entity.issue.IssueComment) TypeReference(com.fasterxml.jackson.core.type.TypeReference) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) EntityManager(com.epam.pipeline.manager.EntityManager) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) Logger(org.slf4j.Logger) AbstractSecuredEntity(com.epam.pipeline.entity.AbstractSecuredEntity) PreferenceManager(com.epam.pipeline.manager.preference.PreferenceManager) RoleManager(com.epam.pipeline.manager.user.RoleManager) DefaultRoles(com.epam.pipeline.entity.user.DefaultRoles) JsonMapper(com.epam.pipeline.config.JsonMapper) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) NotificationMessageVO(com.epam.pipeline.controller.vo.notification.NotificationMessageVO) Collectors(java.util.stream.Collectors) List(java.util.List) UserManager(com.epam.pipeline.manager.user.UserManager) MonitoringNotificationDao(com.epam.pipeline.dao.notification.MonitoringNotificationDao) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) CollectionUtils(org.springframework.util.CollectionUtils) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Issue(com.epam.pipeline.entity.issue.Issue) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with PipelineRun

use of com.epam.pipeline.entity.pipeline.PipelineRun in project cloud-pipeline by epam.

the class PipelineRunManager method runCmd.

/**
 * Launches cmd command execution, uses Tool as ACL identity
 * @param runVO
 * @return
 */
@ToolSecurityPolicyCheck
public PipelineRun runCmd(PipelineStart runVO) {
    Assert.notNull(runVO.getInstanceType(), messageHelper.getMessage(MessageConstants.SETTING_IS_NOT_PROVIDED, "instance_type"));
    Assert.notNull(runVO.getHddSize(), messageHelper.getMessage(MessageConstants.SETTING_IS_NOT_PROVIDED, "instance_disk"));
    int maxRunsNumber = preferenceManager.getPreference(SystemPreferences.LAUNCH_MAX_SCHEDULED_NUMBER);
    LOGGER.debug("Allowed runs count - {}, actual - {}", maxRunsNumber, getNodeCount(runVO.getNodeCount(), 1));
    Assert.isTrue(getNodeCount(runVO.getNodeCount(), 1) < maxRunsNumber, messageHelper.getMessage(MessageConstants.ERROR_EXCEED_MAX_RUNS_COUNT, maxRunsNumber, getNodeCount(runVO.getNodeCount(), 1)));
    Tool tool = toolManager.loadByNameOrId(runVO.getDockerImage());
    PipelineConfiguration configuration = configurationManager.getPipelineConfiguration(runVO, tool);
    boolean clusterRun = configurationManager.initClusterConfiguration(configuration, true);
    PipelineRun run = launchPipeline(configuration, null, null, runVO.getInstanceType(), runVO.getParentNodeId(), runVO.getConfigurationName(), null, runVO.getParentRunId(), null, null, runVO.getRunSids());
    run.setParent(tool);
    run.setAclClass(AclClass.TOOL);
    if (clusterRun) {
        runClusterWorkers(run, runVO, null, null, configuration);
    }
    return run;
}
Also used : PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) PipelineConfiguration(com.epam.pipeline.entity.configuration.PipelineConfiguration) Tool(com.epam.pipeline.entity.pipeline.Tool) ToolSecurityPolicyCheck(com.epam.pipeline.manager.docker.scan.ToolSecurityPolicyCheck)

Example 3 with PipelineRun

use of com.epam.pipeline.entity.pipeline.PipelineRun in project cloud-pipeline by epam.

the class PipelineRunManager method runPipeline.

/**
 * Runs specified pipeline version, uses Pipeline as ACL identity
 *
 * @param runVO
 * @return
 */
@ToolSecurityPolicyCheck
public PipelineRun runPipeline(PipelineStart runVO) {
    Long pipelineId = runVO.getPipelineId();
    String version = runVO.getVersion();
    int maxRunsNumber = preferenceManager.getPreference(SystemPreferences.LAUNCH_MAX_SCHEDULED_NUMBER);
    LOGGER.debug("Allowed runs count - {}, actual - {}", maxRunsNumber, getNodeCount(runVO.getNodeCount(), 1));
    Assert.isTrue(getNodeCount(runVO.getNodeCount(), 1) < maxRunsNumber, messageHelper.getMessage(MessageConstants.ERROR_EXCEED_MAX_RUNS_COUNT, maxRunsNumber, getNodeCount(runVO.getNodeCount(), 1)));
    Pipeline pipeline = pipelineManager.load(pipelineId);
    PipelineConfiguration configuration = configurationManager.getPipelineConfiguration(runVO);
    boolean isClusterRun = configurationManager.initClusterConfiguration(configuration, true);
    // check that tool execution is allowed
    toolApiService.loadToolForExecution(configuration.getDockerImage());
    PipelineRun run = launchPipeline(configuration, pipeline, version, runVO.getInstanceType(), runVO.getParentNodeId(), runVO.getConfigurationName(), null, runVO.getParentRunId(), null, null, runVO.getRunSids());
    run.setParent(pipeline);
    if (isClusterRun) {
        runClusterWorkers(run, runVO, version, pipeline, configuration);
    }
    return run;
}
Also used : PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) PipelineConfiguration(com.epam.pipeline.entity.configuration.PipelineConfiguration) Pipeline(com.epam.pipeline.entity.pipeline.Pipeline) ToolSecurityPolicyCheck(com.epam.pipeline.manager.docker.scan.ToolSecurityPolicyCheck)

Example 4 with PipelineRun

use of com.epam.pipeline.entity.pipeline.PipelineRun in project cloud-pipeline by epam.

the class PipelineRunManager method launchPipeline.

/**
 * Internal method for creating a pipeline run,
 * it assumes that ACL filtering was already applied to input arguments
 * @param configuration
 * @param pipeline - null in case of CMD launch
 * @param version - null in case of CMD launch
 * @param instanceType
 * @param configurationName
 * @param runSids - a list of identities (user names or groups) that have access to run
 * @return
 */
@Transactional(propagation = Propagation.REQUIRED)
public PipelineRun launchPipeline(PipelineConfiguration configuration, Pipeline pipeline, String version, String instanceType, Long parentNodeId, String configurationName, String clusterId, Long parentRunId, List<Long> entityIds, Long configurationId, List<RunSid> runSids) {
    validateInstanceAndPriceTypes(configuration, pipeline, instanceType);
    String instanceDisk = configuration.getInstanceDisk();
    if (StringUtils.hasText(instanceDisk)) {
        Assert.isTrue(NumberUtils.isNumber(instanceDisk) && Integer.parseInt(instanceDisk) > 0, messageHelper.getMessage(MessageConstants.ERROR_INSTANCE_DISK_IS_INVALID, instanceDisk));
    }
    Tool tool = getToolForRun(configuration);
    adjustInstanceDisk(configuration);
    List<String> endpoints = configuration.isEraseRunEndpoints() ? Collections.emptyList() : tool.getEndpoints();
    configuration.setSecretName(tool.getSecretName());
    PipelineRun run = createPipelineRun(version, configuration, pipeline, parentRunId, entityIds, configurationId);
    if (parentNodeId != null && !parentNodeId.equals(run.getId())) {
        setParentInstance(run, parentNodeId);
    }
    String useNodeLabel = parentNodeId != null ? parentNodeId.toString() : run.getId().toString();
    run.setConfigName(configurationName);
    run.setRunSids(runSids);
    String launchedCommand = pipelineLauncher.launch(run, configuration, endpoints, useNodeLabel, clusterId);
    // update instance info according to evaluated command
    run.setActualCmd(launchedCommand);
    save(run);
    dataStorageManager.analyzePipelineRunsParameters(Collections.singletonList(run));
    return run;
}
Also used : PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) Tool(com.epam.pipeline.entity.pipeline.Tool) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with PipelineRun

use of com.epam.pipeline.entity.pipeline.PipelineRun in project cloud-pipeline by epam.

the class PipelineRunManager method updateServiceUrl.

@Transactional(propagation = Propagation.REQUIRED)
public PipelineRun updateServiceUrl(Long runId, PipelineRunServiceUrlVO serviceUrl) {
    PipelineRun pipelineRun = pipelineRunDao.loadPipelineRun(runId);
    Assert.notNull(pipelineRun, messageHelper.getMessage(MessageConstants.ERROR_PIPELINE_NOT_FOUND, runId));
    pipelineRun.setServiceUrl(serviceUrl.getServiceUrl());
    pipelineRunDao.updateServiceUrl(pipelineRun);
    // TODO: check if we need it
    setParent(pipelineRun);
    return pipelineRun;
}
Also used : PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

PipelineRun (com.epam.pipeline.entity.pipeline.PipelineRun)88 Test (org.junit.Test)31 Transactional (org.springframework.transaction.annotation.Transactional)29 AbstractSpringTest (com.epam.pipeline.AbstractSpringTest)23 EnvVarsBuilderTest (com.epam.pipeline.manager.execution.EnvVarsBuilderTest)22 ArrayList (java.util.ArrayList)18 RunInstance (com.epam.pipeline.entity.pipeline.RunInstance)17 Date (java.util.Date)15 Pipeline (com.epam.pipeline.entity.pipeline.Pipeline)14 List (java.util.List)14 PipelineConfiguration (com.epam.pipeline.entity.configuration.PipelineConfiguration)13 Map (java.util.Map)11 LocalDateTime (java.time.LocalDateTime)10 Arrays (java.util.Arrays)10 Collectors (java.util.stream.Collectors)10 PipelineUser (com.epam.pipeline.entity.user.PipelineUser)9 Collections (java.util.Collections)9 HashMap (java.util.HashMap)9 MessageHelper (com.epam.pipeline.common.MessageHelper)8 RunSid (com.epam.pipeline.entity.pipeline.run.parameter.RunSid)8