Skip to content

Commit

Permalink
Webitools 172 make snapshot and publish imaging comply with the new f…
Browse files Browse the repository at this point in the history
…ast scan workflow lfo (#148)

* Making the snapshot and Pubish to dashbord complies the Fast-Scan workflow
* Updated snapshot command for Fast-Scan workflow
* Updated JENKINS plugin snapshot builder for Fast-Scan workflow
* Updated JENKINS plugin snapshot builder for Fast-Scan workflow
* added integration tests
* log the request information
* Makes sleep duration a shared option
* prevent deep-analyze to trigger analysis step
* updated the node version
  • Loading branch information
leon2Meudon92 committed Apr 3, 2023
1 parent 46379d0 commit 0dc8adb
Show file tree
Hide file tree
Showing 20 changed files with 263 additions and 37 deletions.
7 changes: 5 additions & 2 deletions aip-console-jenkins/README.md
Expand Up @@ -334,9 +334,12 @@ Under `Advanced Settings` you will find the following parameters :
The Snapshot step provides the following parameters :

* *Application Name* (**required**): The name of tshe application for which the analysis will be run.
* *Snapshot Name* : The name of the Snapshot to create. If left blank, the snapshot will be named with the following pattern: `Snapshot-YYYY-MM-DDThh-mm-ss` depending on current date and time.
* *Snapshot Name* : The name of the Snapshot to create. If left blank, the snapshot will be named with the following
pattern: `Snapshot-YYYY-MM-DDThh-mm-ss` depending on current date and time.
* *Publish to Imaging*: Publish to Imaging if Imaging is configured with AIP Console
* *Publish to the Health Dashboard* : When checked, consolidates snapshots and publishes data to the Health Dashboards. Checked by default.
* *Publish to the Health Dashboard* : When checked, consolidates snapshots and publishes data to the Health Dashboards.
Checked by default.
* *Sleep Duration*: Number of seconds used to refresh the ongoing job status

Under `Advanced Settings` you will find the following parameters :

Expand Down
Expand Up @@ -23,6 +23,7 @@ public class SnapshotAction extends BaseAction implements RunAction2 {
private boolean failureIgnored = false;
private long timeout = Constants.DEFAULT_HTTP_TIMEOUT;
private boolean consolidation = true;
private long sleepDuration = Constants.DEFAULT_SLEEP_DURATION;

@CheckForNull
public String getApplicationName() {
Expand Down Expand Up @@ -120,4 +121,12 @@ public String getDisplayName() {
public String getUrlName() {
return null;
}

public long getSleepDuration() {
return sleepDuration;
}

public void setSleepDuration(long sleepDuration) {
this.sleepDuration = sleepDuration;
}
}
Expand Up @@ -9,6 +9,7 @@
import com.castsoftware.aip.console.tools.core.dto.jobs.JobState;
import com.castsoftware.aip.console.tools.core.dto.jobs.JobType;
import com.castsoftware.aip.console.tools.core.dto.jobs.LogContentDto;
import com.castsoftware.aip.console.tools.core.dto.jobs.ScanAndReScanApplicationJobRequest;
import com.castsoftware.aip.console.tools.core.exceptions.ApiCallException;
import com.castsoftware.aip.console.tools.core.exceptions.ApplicationServiceException;
import com.castsoftware.aip.console.tools.core.exceptions.JobServiceException;
Expand Down Expand Up @@ -55,6 +56,7 @@
import static io.jenkins.plugins.aipconsole.Messages.SnapshotBuilder_Snapshot_error_jobFailure;
import static io.jenkins.plugins.aipconsole.Messages.SnapshotBuilder_Snapshot_error_noAnalyzedVersion;
import static io.jenkins.plugins.aipconsole.Messages.SnapshotBuilder_Snapshot_error_version;
import static io.jenkins.plugins.aipconsole.Messages.SnapshotBuilder_Snapshot_info_fastScanWorkflow;
import static io.jenkins.plugins.aipconsole.Messages.SnapshotBuilder_Snapshot_info_pollJobMessage;
import static io.jenkins.plugins.aipconsole.Messages.SnapshotBuilder_Snapshot_success_complete;

Expand All @@ -78,6 +80,7 @@ public class SnapshotBuilder extends BaseActionBuilder implements SimpleBuildSte
private boolean failureIgnored = false;
private long timeout = Constants.DEFAULT_HTTP_TIMEOUT;
private boolean consolidation = true;
private long sleepDuration;

@DataBoundConstructor
public SnapshotBuilder(String applicationName) {
Expand Down Expand Up @@ -235,6 +238,38 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnul
}

boolean forcedConsolidation = processImaging || consolidation;
//TODO: refactor after release to get separated workflows
if (app.isOnboarded()) {
log.println(SnapshotBuilder_Snapshot_info_fastScanWorkflow(expandedAppName));
ScanAndReScanApplicationJobRequest.ScanAndReScanApplicationJobRequestBuilder requestBuilder = ScanAndReScanApplicationJobRequest.builder()
.appGuid(applicationGuid);
String targetNode = app.getTargetNode();
if (StringUtils.isNotEmpty(targetNode)) {
requestBuilder.targetNode(targetNode);
}
caipVersion = app.getCaipVersion();
if (StringUtils.isNotEmpty(caipVersion)) {
requestBuilder.caipVersion(caipVersion);
}
if (StringUtils.isNotEmpty(resolveSnapshotName)) {
requestBuilder.snapshotName(resolveSnapshotName);
}

requestBuilder.processImaging(processImaging);
requestBuilder.publishToEngineering(forcedConsolidation);
//Should remains true to prevent deep-analyze to trigger analyze step when publis and imaging options are set false
requestBuilder.uploadApplication(true);

log.println("Job request : " + requestBuilder.build().toString());

JenkinsLogPollingProviderServiceImpl jnksLogPollingProvider = new JenkinsLogPollingProviderServiceImpl(jobsService, run, listener, getDescriptor().configuration.isVerbose(), getSleepDuration());
String appGuid = applicationService.runDeepAnalysis(requestBuilder.build(), jnksLogPollingProvider);
if (StringUtils.isEmpty(appGuid)) {
run.setResult(Result.FAILURE);
}
return;
}

JobRequestBuilder requestBuilder = JobRequestBuilder.newInstance(applicationGuid, null, JobType.ANALYZE, caipVersion)
.nodeName(app.getTargetNode())
.startStep(Constants.SNAPSHOT_STEP_NAME)
Expand Down Expand Up @@ -289,6 +324,15 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnul
}
}

public long getSleepDuration() {
return sleepDuration;
}

@DataBoundSetter
public void setSleepDuration(long sleepDuration) {
this.sleepDuration = sleepDuration;
}

private JobState pollJob(String jobGuid, PrintStream log) throws JobServiceException {
return jobsService.pollAndWaitForJobFinished(jobGuid,
jobStatusWithSteps -> log.println(
Expand Down
Expand Up @@ -89,6 +89,7 @@ SnapshotBuilder.Snapshot.error.jobException=An error occurred while starting or
SnapshotBuilder.Snapshot.info.startJob=Snapshot Job is started.
SnapshotBuilder.Snapshot.info.pollJobMessage=Checking status of Snapshot job.
SnapshotBuilder.Snapshot.success.complete=Snapshot for application ''{0}'' completed successfully.
SnapshotBuilder.Snapshot.info.fastScanWorkflow=Triggering Snapshot for an application using the Fast-Scan workflow: ''{0}''

# Analyze Builder and Action
AnalyzeBuilder.DescriptorImpl.displayName=Console Analyze Application
Expand Down
Expand Up @@ -10,14 +10,17 @@
<f:checkbox default="false"/>
</f:entry>
<f:entry title="${%consolidation}" field="consolidation" description="${%consolidation.descr}">
<f:checkbox default="true" />
<f:checkbox default="true"/>
</f:entry>
<f:entry title="${%sleepDuration}" field="sleepDuration" description="${%sleepDuration.descr}">
<f:number default="15"/>
</f:entry>
<f:advanced title="${%advancedSettings}">
<f:entry title="${%aipConsoleUrl}" field="aipConsoleUrl" description="${%aipConsoleUrl.descr}">
<f:textbox field="aipConsoleUrl" value="${aipConsoleUrl}"/>
</f:entry>
<f:entry title="${%apiKey}" field="apiKey" description="${%apiKey.descr}">
<f:password />
<f:password/>
</f:entry>
<f:entry title="${%failureIgnored}" field="failureIgnored" description="${%failureIgnored.descr}">
<f:checkbox/>
Expand Down
Expand Up @@ -15,3 +15,5 @@ apiKey=API Key
apiKey.descr=The API Key to access AIP Console
consolidation=Publish application to the Health dashboard
consolidation.descr=Consolidate snapshot or publish application to the Health dashboard
sleepDuration=sleep duration
sleepDuration.descr=Number of seconds between two refreshes of the status of the ongoing job
Expand Up @@ -39,11 +39,6 @@ public class OnboardApplicationDeepAnalysisCommand extends BasicCollable {
description = "The name of the snapshot to create")
private String snapshotName;

@CommandLine.Option(names = {"--sleep-duration"},
description = "Number of seconds used to refresh the ongoing job status. The default value is: ${DEFAULT-VALUE}",
defaultValue = "15")
private long sleepDuration;

@CommandLine.Option(names = "--module-option"
, description = "Generates a user defined module option for either technology module or analysis unit module. Possible value is one of: full_content, one_per_au, one_per_techno (default: ${DEFAULT-VALUE})")
private ModuleGenerationType moduleGenerationType = ModuleGenerationType.FULL_CONTENT;
Expand All @@ -66,7 +61,7 @@ public Integer processCallCommand() throws Exception {
}

log.info("Deep-Analysis args:");
log.info(String.format("\tApplication: %s%n\tsnapshot name: %s%n\tmodule generation type: %s%n\tsleep: %d%n", applicationName, StringUtils.isEmpty(snapshotName) ? "Auto assigned" : snapshotName, moduleGenerationType.toString(), sleepDuration));
log.info(String.format("\tApplication: %s%n\tsnapshot name: %s%n\tmodule generation type: %s%n\tsleep: %d%n", applicationName, StringUtils.isEmpty(snapshotName) ? "Auto assigned" : snapshotName, moduleGenerationType.toString(), getSharedOptions().getSleepDuration()));

Thread shutdownHook = null;
try {
Expand Down Expand Up @@ -103,7 +98,7 @@ public Integer processCallCommand() throws Exception {
String caipVersion = app.getCaipVersion();
String targetNode = app.getTargetNode();

CliLogPollingProviderImpl cliLogPolling = new CliLogPollingProviderImpl(jobsService, getSharedOptions().isVerbose(), sleepDuration);
CliLogPollingProviderImpl cliLogPolling = new CliLogPollingProviderImpl(jobsService, getSharedOptions().isVerbose(), getSharedOptions().getSleepDuration());

//Run Analysis
if (!applicationService.isImagingAvailable()) {
Expand Down
Expand Up @@ -61,11 +61,6 @@ public class OnboardApplicationFastScanCommand extends BasicCollable {
, description = "Project's exclusion rules, separated with comma. Valid values: ${COMPLETION-CANDIDATES}")
private ExclusionRuleType[] exclusionRules;

@CommandLine.Option(names = {"--sleep-duration"},
description = "Number of seconds used to refresh the ongoing job status. The default value is: ${DEFAULT-VALUE}",
defaultValue = "1")
private long sleepDuration;

@CommandLine.Mixin
private SharedOptions sharedOptions;

Expand All @@ -89,7 +84,7 @@ public Integer processCallCommand() throws Exception {
}

log.info("Fast-Scan args:");
log.info(String.format("\tApplication: %s%n\tFile: %s%n\tsleep: %d%n", applicationName, filePath.getAbsolutePath(), sleepDuration));
log.info(String.format("\tApplication: %s%n\tFile: %s%n\tsleep: %d%n", applicationName, filePath.getAbsolutePath(), sharedOptions.getSleepDuration()));

String applicationGuid;
Thread shutdownHook = null;
Expand All @@ -106,7 +101,7 @@ public Integer processCallCommand() throws Exception {
log.info("About to trigger New workflow for: 'Fast-Scan'");
String sourcePath = uploadFile(app != null ? app.getGuid() : null);

CliLogPollingProviderImpl cliLogPolling = new CliLogPollingProviderImpl(jobsService, getSharedOptions().isVerbose(), sleepDuration);
CliLogPollingProviderImpl cliLogPolling = new CliLogPollingProviderImpl(jobsService, getSharedOptions().isVerbose(), getSharedOptions().getSleepDuration());
if (app == null) {
applicationGuid = applicationService.onboardApplication(applicationName, domainName, getSharedOptions().isVerbose(), sourcePath);
log.info("Onboard Application job has started: application GUID= " + applicationGuid);
Expand Down
Expand Up @@ -3,6 +3,7 @@
import com.castsoftware.aip.console.tools.core.dto.ApplicationDto;
import com.castsoftware.aip.console.tools.core.dto.VersionDto;
import com.castsoftware.aip.console.tools.core.dto.VersionStatus;
import com.castsoftware.aip.console.tools.core.dto.jobs.ScanAndReScanApplicationJobRequest;
import com.castsoftware.aip.console.tools.core.exceptions.ApplicationServiceException;
import com.castsoftware.aip.console.tools.core.services.ApplicationService;
import com.castsoftware.aip.console.tools.core.services.JobsService;
Expand All @@ -14,8 +15,8 @@
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import picocli.CommandLine;

import java.util.EnumSet;
Expand Down Expand Up @@ -80,7 +81,30 @@ protected Integer processCallCommand() throws Exception {
return Constants.RETURN_ONBOARD_VERSION_STATUS_INVALID;
}

CliLogPollingProviderImpl cliLogPolling = new CliLogPollingProviderImpl(jobsService, getSharedOptions().isVerbose(), 15);
CliLogPollingProviderImpl cliLogPolling = new CliLogPollingProviderImpl(jobsService, getSharedOptions().isVerbose(), getSharedOptions().getSleepDuration());
//TODO: refactor after release to get separated workflows
if (applicationDto.isOnboarded()) {
log.info("Triggering Publish to Imaging for an application using Fast-Scan workflow.");
ScanAndReScanApplicationJobRequest.ScanAndReScanApplicationJobRequestBuilder requestBuilder = ScanAndReScanApplicationJobRequest.builder()
.appGuid(applicationDto.getGuid());
String targetNode = applicationDto.getTargetNode();
if (StringUtils.isNotEmpty(targetNode)) {
requestBuilder.targetNode(targetNode);
}
String caipVersion = applicationDto.getCaipVersion();
if (StringUtils.isNotEmpty(caipVersion)) {
requestBuilder.caipVersion(caipVersion);
}
requestBuilder.processImaging(true);

String appGuid = applicationService.runDeepAnalysis(requestBuilder.build(), cliLogPolling);
if (StringUtils.isEmpty(appGuid)) {
log.error("Something went wrong while Publishing to Imaging.");
return Constants.RETURN_JOB_FAILED;
}
return Constants.RETURN_OK;
}

String appGuid = applicationService.publishToImaging(applicationDto.getGuid(), cliLogPolling);

if (StringUtils.isEmpty(appGuid)) {
Expand Down
Expand Up @@ -30,6 +30,11 @@ public class SharedOptions {
+ " if specified without parameter: ${FALLBACK-VALUE}", fallbackValue = "true")
private boolean verbose = true;

@CommandLine.Option(names = {"--sleep-duration"},
description = "Number of seconds used to refresh the ongoing job status. The default value is: ${DEFAULT-VALUE}",
defaultValue = "10")
private long sleepDuration;

@CommandLine.Unmatched
private List<String> unmatchedOptions;

Expand Down Expand Up @@ -103,6 +108,14 @@ public String getFullServerRootUrl() {
return serverRootUrl;
}

public long getSleepDuration() {
return sleepDuration;
}

public void setSleepDuration(long sleepDuration) {
this.sleepDuration = sleepDuration;
}

@Override
public String toString() {
return "SharedOptions{" +
Expand All @@ -112,7 +125,8 @@ public String toString() {
", username='" + username + '\'' +
", timeout='" + timeout + '\'' +
", unmatchedOptions=" + unmatchedOptions +
", log output= " + verbose +
", log verbose= " + verbose +
", sleep duration= " + sleepDuration +
'}';
}
}
Expand Up @@ -8,6 +8,7 @@
import com.castsoftware.aip.console.tools.core.dto.jobs.JobRequestBuilder;
import com.castsoftware.aip.console.tools.core.dto.jobs.JobState;
import com.castsoftware.aip.console.tools.core.dto.jobs.JobType;
import com.castsoftware.aip.console.tools.core.dto.jobs.ScanAndReScanApplicationJobRequest;
import com.castsoftware.aip.console.tools.core.exceptions.ApiCallException;
import com.castsoftware.aip.console.tools.core.exceptions.ApiKeyMissingException;
import com.castsoftware.aip.console.tools.core.exceptions.ApplicationServiceException;
Expand All @@ -17,6 +18,7 @@
import com.castsoftware.aip.console.tools.core.services.RestApiService;
import com.castsoftware.aip.console.tools.core.utils.Constants;
import com.castsoftware.aip.console.tools.core.utils.SemVerUtils;
import com.castsoftware.aip.console.tools.providers.CliLogPollingProviderImpl;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -154,8 +156,39 @@ public Integer call() throws Exception {
snapshotName = String.format("Snapshot-%s", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(new Date()));
}

// Run snapshot
boolean forcedConsolidation = processImaging || consolidation;
//TODO: refactor after release to get separated workflows
if (app.isOnboarded()) {
log.info("Triggering snapshot for an application using Fast-Scan workflow.");
ScanAndReScanApplicationJobRequest.ScanAndReScanApplicationJobRequestBuilder requestBuilder = ScanAndReScanApplicationJobRequest.builder()
.appGuid(applicationGuid);
String targetNode = app.getTargetNode();
if (StringUtils.isNotEmpty(targetNode)) {
requestBuilder.targetNode(targetNode);
}
String caipVersion = app.getCaipVersion();
if (StringUtils.isNotEmpty(caipVersion)) {
requestBuilder.caipVersion(caipVersion);
}
if (StringUtils.isNotEmpty(snapshotName)) {
requestBuilder.snapshotName(snapshotName);
}

requestBuilder.processImaging(processImaging);
requestBuilder.publishToEngineering(forcedConsolidation);
//Should remains true to prevent deep-analyze to trigger analyze step when publis and imaging options are set false
requestBuilder.uploadApplication(true);

CliLogPollingProviderImpl cliLogPolling = new CliLogPollingProviderImpl(jobsService, getSharedOptions().isVerbose(), getSharedOptions().getSleepDuration());
String appGuid = applicationService.runDeepAnalysis(requestBuilder.build(), cliLogPolling);
if (StringUtils.isEmpty(appGuid)) {
log.error("Snapshot operating wasn't performed successfully. Toggle verbose ON or check CAST Console logs for more details.");
return Constants.RETURN_JOB_FAILED;
}
return Constants.RETURN_OK;
}

// Run snapshot in legacy workflow
JobRequestBuilder builder = JobRequestBuilder.newInstance(applicationGuid, null, JobType.ANALYZE, app.getCaipVersion())
.nodeName(app.getTargetNode())
.startStep(Constants.SNAPSHOT_STEP_NAME)
Expand All @@ -165,7 +198,7 @@ public Integer call() throws Exception {
.uploadApplication(true)
.snapshotDate(applicationService.getVersionDate(snapshotDateString))
.processImaging(processImaging)
.uploadApplication(forcedConsolidation)
.uploadApplication(true)
.endStep(SemVerUtils.isNewerThan115(apiInfoDto.getApiVersionSemVer()) ?
Constants.UPLOAD_APP_SNAPSHOT : Constants.SNAPSHOT_INDICATOR);

Expand Down

0 comments on commit 0dc8adb

Please sign in to comment.