Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ configurations {
}

ext {
xlPlatformVersion = "2016.2.6"
xlPlatformVersion = "2017.7.1"

}

Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/xebialabs/deployit/ci/DeployitPerformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.base.Joiner;
import com.google.common.base.Strings;
Expand Down Expand Up @@ -127,8 +129,15 @@ public boolean doPerform() throws InterruptedException, IOException {

final String versionId = Joiner.on("/").join(resolvedApplication, packageVersion);
deploymentListener.info(Messages.DeployitNotifier_deploy(versionId, resolvedEnvironment));

Map<String, String> deploymentProperties = new HashMap<String, String>();
if (deploymentParameters.deploymentOptions.deploymentProperties != null) {
for (DeploymentProperty deploymentProperty : deploymentParameters.deploymentOptions.deploymentProperties) {
deploymentProperties.put(deploymentProperty.propertyName, envVars.expand(deploymentProperty.propertyValue));
}
}
try {
deployitServer.deploy(versionId, resolvedEnvironment, deploymentParameters.deploymentOptions, deploymentListener);
deployitServer.deploy(versionId, resolvedEnvironment, deploymentProperties, deploymentParameters.deploymentOptions, deploymentListener);
} catch (Exception e) {
deploymentListener.error(Messages._DeployitNotifier_errorDeploy(e.getMessage()));
return false;
Expand Down Expand Up @@ -181,4 +190,4 @@ public DeployitPerformerParameters(JenkinsPackageOptions packageOptions, List<Pa
this.verbose = verbose;
}
}
}
}
51 changes: 51 additions & 0 deletions src/main/java/com/xebialabs/deployit/ci/DeploymentProperty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.xebialabs.deployit.ci;

import com.xebialabs.deployit.ci.server.DeployitDescriptorRegistry;
import com.xebialabs.deployit.ci.server.DeployitServer;
import com.xebialabs.deployit.ci.util.ListBoxModels;
import hudson.Extension;
import hudson.RelativePath;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.util.ListBoxModel;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

import java.util.Collection;

import static com.xebialabs.deployit.ci.PackageProperty.PackagePropertyDescriptor.ONLY_SIMPLE_EDITABLE_PROPERTIES;

public class DeploymentProperty extends NameValuePair {

@DataBoundConstructor
public DeploymentProperty(String propertyName, String propertyValue) {
super(propertyName, propertyValue);
}

@Extension
public static final class DescriptorImpl extends Descriptor<NameValuePair> {

public static String PROPERTY_TYPE = "udm.DeployedApplication";

@Override
public String getDisplayName() {
return DeploymentProperty.class.getSimpleName();
}

public ListBoxModel doFillPropertyNameItems(
@QueryParameter(value = "credential") @RelativePath(value = "../..") String credentialExistingProps,
@QueryParameter(value = "credential") @RelativePath(value = "..") String credentialNewProps,
@AncestorInPath AbstractProject project) {
String creds = credentialExistingProps != null ? credentialExistingProps : credentialNewProps;
Credential overridingCredential = RepositoryUtils.retrieveOverridingCredentialFromProject(project);
// load type descriptor
DeployitServer deployitServer = RepositoryUtils.getDeployitServer(creds, overridingCredential);
DeployitDescriptorRegistry descriptorRegistry = deployitServer.getDescriptorRegistry();
Collection<String> properties = descriptorRegistry.getPropertiesForDeployableType(PROPERTY_TYPE, ONLY_SIMPLE_EDITABLE_PROPERTIES);
return ListBoxModels.of(properties);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,20 @@ public class JenkinsDeploymentOptions implements Describable<JenkinsDeploymentOp
public boolean skipMode;
public boolean testMode;
public boolean rollbackOnError;
public List<DeploymentProperty> deploymentProperties;

public final VersionKind versionKind;
public String version;

@DataBoundConstructor
public JenkinsDeploymentOptions(String environment, VersionKind versionKind, boolean generateDeployedOnUpgrade, boolean skipMode, boolean testMode, boolean rollbackOnError) {
public JenkinsDeploymentOptions(String environment, VersionKind versionKind, boolean generateDeployedOnUpgrade, boolean skipMode, boolean testMode, boolean rollbackOnError, List<DeploymentProperty> deploymentProperties) {
this.generateDeployedOnUpgrade = generateDeployedOnUpgrade;
this.skipMode = skipMode;
this.testMode = testMode;
this.rollbackOnError = rollbackOnError;
this.environment = environment;
this.versionKind = versionKind;
this.deploymentProperties = deploymentProperties;
}

@Override
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/xebialabs/deployit/ci/dar/RemotePackaging.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,24 @@ public RemotePackaging withRegistryVersion(String registryVersion) {
return this;
}

private DarPackager getDarPackager() {
ClassLoader origClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(DarPackager.class.getClassLoader());
ManifestWriter mw = new ManifestXmlWriter();
return new DarPackager(mw);
} finally {
Thread.currentThread().setContextClassLoader(origClassLoader);
}
}

/**
* Call to be executed via jenkins virtual channel
*/
@Override
public String call() throws RuntimeException {
targetDir.mkdirs();
ManifestWriter mw = new ManifestXmlWriter();
DarPackager pkger = new DarPackager(mw);
DarPackager pkger = getDarPackager();
DescriptorRegistry descriptorRegistry = DescriptorRegistry.getDescriptorRegistry(booterConfig);
if (null == descriptorRegistry) {
SlaveRemoteDescriptorRegistry.boot(descriptors, booterConfig, registryVersion);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package com.xebialabs.deployit.ci.server;

import com.google.common.base.Throwables;
import com.xebialabs.deployit.ci.DeployitPluginException;
import com.xebialabs.deployit.ci.util.JenkinsDeploymentListener;
import com.xebialabs.deployit.engine.api.TaskService;
import com.xebialabs.deployit.engine.api.execution.StepExecutionState;
import com.xebialabs.deployit.engine.api.execution.StepState;
import com.xebialabs.deployit.engine.api.execution.TaskExecutionState;
import com.xebialabs.deployit.engine.api.execution.TaskState;
import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;
import static java.lang.String.format;

public abstract class AbstractDeploymentCommand {

protected TaskService taskService;
protected JenkinsDeploymentListener listener;

protected AbstractDeploymentCommand(TaskService taskService, JenkinsDeploymentListener listener) {
this.taskService = taskService;
this.listener = listener;
}

protected boolean executeTask(String taskId, boolean skipMode, boolean testMode) {

if (skipMode) {
listener.info("skip mode, skip all the steps");
taskService.skip(taskId, range(taskService.getTask(taskId).getNrSteps() + 1));
}

checkTaskState(taskId);
if (testMode) {
listener.info("test mode, cancel task " + taskId);
taskService.cancel(taskId);
return false;
}

try {
listener.info("Start deployment task " + taskId);
startTaskAndWait(taskId);
checkTaskState(taskId);
taskService.archive(taskId);
return true;
} catch (RuntimeException e) {
String msg = format("Error when executing task %s", taskId);
throw new DeployitPluginException(msg);
}

}

private List<Integer> range(int end) {
List<Integer> result = newArrayList();
for (int i = 1; i < end; i++) {
result.add(i);
}
return result;
}

private void checkTaskState(String taskId) {
TaskState taskState = taskService.getTask(taskId);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
listener.info(format("%s Description %s", taskId, taskState.getDescription()));
listener.info(format("%s State %s %d/%d", taskId, taskState.getState(), taskState.getCurrentStepNr(), taskState.getNrSteps()));
if (taskState.getStartDate() != null) {
final GregorianCalendar startDate = taskState.getStartDate().toGregorianCalendar();
listener.info(format("%s Start %s", taskId, sdf.format(startDate.getTime())));
}

if (taskState.getCompletionDate() != null) {
final GregorianCalendar completionDate = taskState.getCompletionDate().toGregorianCalendar();
listener.info(format("%s Completion %s", taskId, sdf.format(completionDate.getTime())));
}

StringBuilder sb = new StringBuilder();
for (int i = 1; i <= taskState.getNrSteps(); i++) {
final StepState stepInfo = taskService.getStep(taskId, i, null);
final String description = stepInfo.getDescription();
final String log = stepInfo.getLog();
String stepInfoMessage;
if (StringUtils.isEmpty(log) || description.equals(log)) {
stepInfoMessage = format("%s step #%d %s\t%s", taskId, i, stepInfo.getState(), description);
} else {
stepInfoMessage = format("%s step #%d %s\t%s\n%s", taskId, i, stepInfo.getState(), description, log);
}

listener.info(stepInfoMessage);
if (StepExecutionState.FAILED.equals(stepInfo.getState()))
sb.append(stepInfoMessage);
}

if (taskState.getState().isExecutionHalted())
throw new DeployitPluginException(format("Errors when executing task %s: %s", taskId, sb));
}

private void startTaskAndWait(String taskId) {
taskService.start(taskId);
// Wait until done/failed
boolean done = false;
TaskState ti;

int retryCount = 1;
while (!done) {
try {
ti = taskService.getTask(taskId);
TaskExecutionState state = ti.getState();
listener.debug("Task state: " + state.toString());
done = state.isPassiveAfterExecuting();
retryCount = 1;
} catch (Exception e) {
if (retryCount == 6) { //fail after 5 consecutive errors.
Throwables.propagate(e);
} else {
listener.info("Failed to get task status. Error message: " + Throwables.getRootCause(e).getMessage());
listener.info("Will attempt retry " + retryCount + " of 5 in one second.");
retryCount++;
}
}

try {
listener.debug("Waiting for task to be done...");
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

Expand Down Expand Up @@ -87,7 +88,7 @@ private void verifyPackageExistInRemoteRepository(String deploymentPackage) {

}

public void deploy(String deploymentPackage, String environment) {
public void deploy(String deploymentPackage, String environment, Map<String, String> deploymentProperties) {
listener.debug(deploymentOptions.toString());

verifyPackageExistInRemoteRepository(deploymentPackage);
Expand All @@ -108,6 +109,11 @@ public void deploy(String deploymentPackage, String environment) {
deployment = deploymentService.prepareAutoDeployeds(deployment);
}

for (Map.Entry<String, String> deploymentProperty : deploymentProperties.entrySet()) {
listener.debug(String.format("Setting deployment property %s = %s", deploymentProperty.getKey(), deploymentProperty.getValue()));
deployment.getDeployedApplication().setProperty(deploymentProperty.getKey(), deploymentProperty.getValue());
}

listener.debug(" dump Deployeds");
for (ConfigurationItem itemDto : deployment.getDeployeds()) {
listener.debug(" - " + itemDto);
Expand Down Expand Up @@ -264,4 +270,4 @@ private void startTaskAndWait(String taskId) {
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public interface DeployitDescriptorRegistry extends Versioned {
String UDM_ARTIFACT = "udm.Artifact";
String UDM_DEPLOYABLE = "udm.Deployable";
String UDM_EMBEDDED_DEPLOYABLE = "udm.EmbeddedDeployable";
String UDM_DEPLOYED_APPLICATION = "udm.DeployedApplication";
String UDM_CONTAINER = "udm.Container";

Type typeForClass(Class<?> clazz);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import com.xebialabs.deployit.ci.ArtifactView;
import com.xebialabs.deployit.ci.util.Strings2;
import com.xebialabs.deployit.plugin.api.udm.base.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -61,11 +62,6 @@
import com.xebialabs.deployit.plugin.api.udm.Version;
import com.xebialabs.deployit.plugin.api.udm.artifact.FolderArtifact;
import com.xebialabs.deployit.plugin.api.udm.artifact.SourceArtifact;
import com.xebialabs.deployit.plugin.api.udm.base.BaseConfigurationItem;
import com.xebialabs.deployit.plugin.api.udm.base.BaseDeployable;
import com.xebialabs.deployit.plugin.api.udm.base.BaseDeployableFileArtifact;
import com.xebialabs.deployit.plugin.api.udm.base.BaseDeployableFolderArtifact;
import com.xebialabs.deployit.plugin.api.udm.base.BaseEmbeddedDeployable;

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Sets.newHashSet;
Expand Down Expand Up @@ -217,6 +213,15 @@ public ConfigurationItem newInstance(String typeName, String name) {
return ci;
}

private Object getPropertyDescriptorDefaultValue(RemoteDescriptor remoteDescriptor, String propertyName) {
for (PropertyDescriptor pd : remoteDescriptor.getPropertyDescriptors()) {
if (propertyName.equals(pd.getName())) {
return pd.getDefaultValue();
}
}
return null;
}

private ConfigurationItem newInstance(Type type, String id) {
try {
RemoteDescriptor remoteDescriptor = (RemoteDescriptor) getDescriptor(type);
Expand All @@ -231,6 +236,7 @@ private ConfigurationItem newInstance(Type type, String id) {
} else {
ci = new BaseDeployableFileArtifact();
}
((BaseDeployableArtifact) ci).setTextFileNamesRegex(String.valueOf(getPropertyDescriptorDefaultValue(remoteDescriptor, "textFileNamesRegex")));
} else if (remoteDescriptor.isAssignableTo(typeForClass(Deployable.class))) {
ci = new BaseDeployable();
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.xebialabs.deployit.ci.server;

import java.util.List;
import java.util.Map;

import com.xebialabs.deployit.booter.remote.BooterConfig;
import com.xebialabs.deployit.booter.remote.DeployitCommunicator;
Expand All @@ -22,7 +23,11 @@ public interface DeployitServer {

ConfigurationItem importPackage(String darFile);

void deploy(String deploymentPackage, String environment, JenkinsDeploymentOptions deploymentOptions, JenkinsDeploymentListener listener);
void deploy(String deploymentPackage, String environment, Map<String, String> deploymentProperties, JenkinsDeploymentOptions deploymentOptions, JenkinsDeploymentListener listener);

void undeploy(String deployedApplication, JenkinsDeploymentListener listener);

void executeControlTask(String hostId, String controlTask, Map<String, String> parameters, JenkinsDeploymentListener listener);

DeployitCommunicator newCommunicator();

Expand Down
Loading