Skip to content

Commit

Permalink
Merge pull request #1 from PTC-ALM/master
Browse files Browse the repository at this point in the history
Merging from master
  • Loading branch information
shipatil committed Jan 4, 2018
2 parents 270bced + 74fbf67 commit 29d1bf4
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 129 deletions.
29 changes: 24 additions & 5 deletions src/main/java/hudson/scm/IntegrityItemAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import hudson.tasks.test.TestResult;
import hudson.tasks.test.AggregatedTestResultAction.ChildReport;
import hudson.util.ListBoxModel;
import hudson.util.Secret;
import net.sf.json.JSONObject;

public class IntegrityItemAction extends Notifier implements Serializable
Expand Down Expand Up @@ -684,11 +685,29 @@ private boolean collectTestResults(AbstractBuild<?, ?> build, BuildListener list
*/
private IntegrityConfigurable getProjectSettings(AbstractBuild<?, ?> thisBuild)
{
AbstractProject<?, ?> thisProject = thisBuild.getProject();
if (!(thisProject.getScm() instanceof IntegritySCM)) {
LOGGER.severe("IntegrityItemAction - Failed to initialize project specific connection settings!");
}
return ((IntegritySCM) thisProject.getScm()).getProjectSettings();
IntegrityConfigurable desSettings =
DescriptorImpl.INTEGRITY_DESCRIPTOR.getConfiguration(serverConfig);
IntegrityConfigurable ciSettings =
new IntegrityConfigurable("TEMP_ID", desSettings.getIpHostName(), desSettings.getIpPort(),
desSettings.getHostName(), desSettings.getPort(), desSettings.getSecure(), "", "");
AbstractProject<?, ?> thisProject = thisBuild.getProject();
if (thisProject.getScm() instanceof IntegritySCM)
{
String userName = ((IntegritySCM) thisProject.getScm()).getUserName();
ciSettings.setUserName(userName);
LOGGER.fine("IntegrityItemAction - Project Userame = " + userName);

Secret password = ((IntegritySCM) thisProject.getScm()).getSecretPassword();
ciSettings.setPassword(password.getEncryptedValue());
LOGGER.fine("IntegrityItemAction - Project User password = " + password.getEncryptedValue());
} else
{
LOGGER.severe(
"IntegrityItemAction - Failed to initialize project specific connection settings!");
return desSettings;
}

return ciSettings;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/scm/IntegritySCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ public DescriptorImpl()

// Initialize our derby environment
System.setProperty(DerbyUtils.DERBY_SYS_HOME_PROPERTY,
Jenkins.getInstance().getRootDir().getAbsolutePath());;
Jenkins.getInstance().getRootDir().getAbsolutePath());
DerbyUtils.loadDerbyDriver();
LOGGER.info("Creating Integrity SCM cache db connection...");
dataSource = DerbyUtils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,10 @@ public List<IntegrityLcChangeSet> getLogs()
{
return changeSets;
}

@Override
public String getKind()
{
return "integrity";
}
}
154 changes: 58 additions & 96 deletions src/main/java/hudson/scm/localclient/SandboxUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.mks.api.Command;
import com.mks.api.Option;
import com.mks.api.response.APIException;
import com.mks.api.response.Response;
import com.mks.api.response.WorkItem;
import com.mks.api.response.WorkItemIterator;
import com.mks.api.response.*;
import hudson.FilePath;
import hudson.model.TaskListener;
import hudson.scm.AbstractIntegritySCM;
Expand Down Expand Up @@ -114,110 +111,75 @@ else if (siProject.isBuild())
return false;
}

/**
* Verifies the sandbox associated with the workspace.
*
* @param session
* @param siProject
* @param workspace
*/
protected boolean verifySandbox(ISession session,
IntegrityCMProject siProject,
FilePath workspace) throws APIException
{
boolean sandboxToBeDropped = false;
Response response = null;
session.checkifAlive();
listener.getLogger()
.println("[LocalClient] Executing Sandbox Verification ");
boolean sandboxExists = false;
Command cmd = new Command(Command.SI, "sandboxes");
listener.getLogger()
.println("[LocalClient] Executing si sandboxes ");
Response response = session.runCommand(cmd);
listener.getLogger()
.println("[LocalClient] si sandboxes response:" +
Objects.toString(
response.getExitCode(),
null));
listener.getLogger()
.printf("[LocalClient] Searching Sandbox: : %s, for Project Config: [%s], Variant: [%s], Build Revision: [%s]" +
AbstractIntegritySCM.NL,
getQualifiedWorkspaceName(
workspace),
siProject.getProjectName(),
siProject.getVariantName(),
siProject.getProjectRevision());
if (response != null && response.getExitCode() == 0) {
.println("[LocalClient] Checking sandbox exists for :"+siProject.getConfigurationPath());
Command cmd = new Command(Command.SI, "sandboxinfo");
cmd.addOption(new Option(IAPIOption.SANDBOX,
getQualifiedWorkspaceName(workspace).concat("/project.pj")));
try {
response = session.runCommand(cmd);
} catch (CommandException e) {
listener.getLogger().println("[LocalClient] "+e.getMessage());
return true;
}
if(response !=null && response.getExitCode() == 0){
// Determine if the sandbox is the on the same backing project as the jenkins project
WorkItemIterator it = response.getWorkItems();
while (it.hasNext()) {
WorkItem wi = it.next();
String sBoxname = wi.getField("SandboxName")
.getValueAsString();
String projectName = wi.getField("ProjectName")
.getValueAsString();
listener.getLogger()
.printf("[LocalClient] Evaluating Sandbox: %s for project: %s - [%s] - [%s]" +
AbstractIntegritySCM.NL,
sBoxname, projectName,
Objects.toString(
wi.getField(DEVELOPMENT_PATH)
.getValueAsString(),
""),
Objects.toString(
wi.getField(BUILD_REVISION)
.getValueAsString(),
""));
if (sBoxname.replace(PROJECT_PJ, "").equalsIgnoreCase(
getQualifiedWorkspaceName(workspace))
&& projectName.equals(
siProject.getProjectName())
&& (siProject.isVariant() &&
Objects.equals(siProject.getVariantName(),
wi.getField(DEVELOPMENT_PATH)
.getValueAsString()))
&& (siProject.isBuild() &&
Objects.equals(siProject
.getProjectRevision(),
wi.getField(BUILD_REVISION)
.getItem()
.getId()))) {
WorkItem wi = it.next();
String projectName = wi.getField("ProjectName")
.getValueAsString();
String devPath = wi.contains(DEVELOPMENT_PATH)?wi.getField(DEVELOPMENT_PATH).getValueAsString():"";
String buildRev = wi.contains(BUILD_REVISION)?wi.getField(BUILD_REVISION).getValueAsString():"";

listener.getLogger()
.println("[LocalClient] Existing workspace sandbox :"+wi.getField("sandboxName").getValueAsString());
listener.getLogger()
.println("[LocalClient] Checking sandbox. Sandbox Project: "+ projectName
+ " & Jenkins project: "+siProject.getProjectName());
if(Objects.equals(siProject.getProjectName(), projectName)) {
if (siProject.isVariant()) {
listener.getLogger()
.printf("[LocalClient] Found Existing Sandbox for Project:[%s], Sandbox: [%s], Variant: [%s], " +
"Build: [%s], in Workspace: [%s]" +
AbstractIntegritySCM.NL,
projectName,
sBoxname,
Objects.toString(
wi.getField(DEVELOPMENT_PATH)
.getValueAsString(),
""),
Objects.toString(
wi.getField(BUILD_REVISION)
.getValueAsString(),
""),
getQualifiedWorkspaceName(
workspace));
return false;
} else if (sBoxname.replace(PROJECT_PJ, "")
.equalsIgnoreCase(getQualifiedWorkspaceName(
workspace))) {
.println("[LocalClient] Checking sandbox. Sandbox Variant: "+ devPath
+ " & Jenkins project variant: "+siProject.getVariantName());
if(Objects.equals(siProject.getVariantName(), devPath))
{
// Same variant. Don't recreate sandbox
return false;
} else {
sandboxToBeDropped = true;
}
}
else if (siProject.isBuild()){
listener.getLogger()
.println("[LocalClient] Sandbox marked for deletion: " +
sBoxname);
sandboxExists = true;
.println("[LocalClient] Checking sandbox. Sandbox Revision: "+ buildRev
+ " & Jenkins project revision: "+siProject.getProjectRevision());
if(Objects.equals(siProject
.getProjectRevision(), buildRev))
{
// Same revision. Don't recreate sandbox
return false;
} else {
sandboxToBeDropped = true;
}
} else {
return false;
}
}
else {
sandboxToBeDropped = true;
}

if(sandboxToBeDropped) {
return dropSandbox(session, workspace, siProject);
}
}
//No existing match found! Drop the existing workspace sandbox if there are no matches
if (sandboxExists)
return dropSandbox(session, workspace, siProject);
listener.getLogger()
.printf("[LocalClient] Sandbox not found in : %s, for Project Config: [%s], Variant: [%s], Build Revision: [%s]" +
AbstractIntegritySCM.NL,
getQualifiedWorkspaceName(
workspace),
siProject.getProjectName(),
siProject.getVariantName(),
siProject.getProjectRevision());
return true;
}

Expand Down
58 changes: 56 additions & 2 deletions src/test/java/hudson/scm/IntegritySCMTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import com.mks.api.Option;
import com.mks.api.response.APIException;
import com.mks.api.response.Response;
import hudson.Functions;
import hudson.model.*;
import hudson.remoting.VirtualChannel;
import hudson.scm.api.session.APISession;
import hudson.scm.api.session.ISession;
import hudson.slaves.DumbSlave;
import hudson.slaves.SlaveComputer;
import hudson.triggers.SCMTrigger;
import hudson.util.StreamTaskListener;
import org.junit.Before;
Expand All @@ -20,6 +23,7 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestEnvironment;

import java.io.File;
import java.io.IOException;
Expand All @@ -38,7 +42,58 @@
public class IntegritySCMTest
{
@Rule
public JenkinsRule jenkinsRule = new JenkinsRule();
public JenkinsRule jenkinsRule = new JenkinsRule(){
private void purgeSlaves(){
List<Computer> disconnectingComputers = new ArrayList<Computer>();
List<VirtualChannel> closingChannels = new ArrayList<VirtualChannel>();
for (Computer computer: jenkins.getComputers()) {
if (!(computer instanceof SlaveComputer)) {
continue;
}
// disconnect slaves.
// retrieve the channel before disconnecting.
// even a computer gets offline, channel delays to close.
if (!computer.isOffline()) {
VirtualChannel ch = computer.getChannel();
computer.disconnect(null);
disconnectingComputers.add(computer);
closingChannels.add(ch);
}
}

try {
// Wait for all computers disconnected and all channels closed.
for (Computer computer: disconnectingComputers) {
computer.waitUntilOffline();
}
for (VirtualChannel ch: closingChannels) {
ch.join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Override
public void after() throws Exception
{
if(Functions.isWindows()) {
purgeSlaves();
}
super.after();
if(TestEnvironment.get() != null)
{
try
{
TestEnvironment.get().dispose();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
};
@Rule
public TemporaryFolder testFolder = new TemporaryFolder();
protected static String successConfigPath="#/JenkinsBulkProject1";
Expand Down Expand Up @@ -294,5 +349,4 @@ public Void call() throws Exception
};
return singleThreadExecutor.submit(callable);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void testCleanSandboxWithConcurrentBuilds() throws Exception
QueueTaskFuture<FreeStyleBuild> build3 = localBuildClientProject.scheduleBuild2(0, new Cause.UserIdCause());
build3.waitForStart();
QueueTaskFuture<FreeStyleBuild> build4 = localBuildClientProject.scheduleBuild2(0, new Cause.UserIdCause());
build4.waitForStart();

jenkinsRule.assertBuildStatusSuccess(build1.get());
jenkinsRule.assertBuildStatusSuccess(build2.get());
Expand Down
Loading

0 comments on commit 29d1bf4

Please sign in to comment.