Skip to content

Commit

Permalink
Merge pull request #24 from vorbis/topic/selectableVsphere
Browse files Browse the repository at this point in the history
VsphereSelection: Allow dynamic selection of vsphere cloud
  • Loading branch information
jswager committed Feb 3, 2015
2 parents 0086d1c + cecfd99 commit e64d7ef
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static vSphereCloud getVSphereCloudByName(String serverName) throws Runti
}
}
}
throw new RuntimeException(Messages.validation_instanceNotFound());
throw new RuntimeException(Messages.validation_instanceNotFound(serverName));
}

public static vSphereCloud getVSphereCloudByHash(int hash) throws RuntimeException, VSphereException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.jenkinsci.plugins.vsphere;

import hudson.DescriptorExtensionList;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Launcher;
import hudson.init.InitMilestone;
Expand All @@ -41,15 +42,21 @@

public class VSphereBuildStepContainer extends Builder {

public static final String SELECTABLE_SERVER_NAME = "${VSPHERE_CLOUD_NAME}";

private final VSphereBuildStep buildStep;
private final String serverName;
private final int serverHash;
private final Integer serverHash;

@DataBoundConstructor
public VSphereBuildStepContainer(final VSphereBuildStep buildStep, final String serverName) throws VSphereException {
this.buildStep = buildStep;
this.serverName = serverName;
this.serverHash = VSphereBuildStep.VSphereBuildStepDescriptor.getVSphereCloudByName(serverName).getHash();
if (!(SELECTABLE_SERVER_NAME.equals(serverName))) {
this.serverHash = VSphereBuildStep.VSphereBuildStepDescriptor.getVSphereCloudByName(serverName).getHash();
} else {
this.serverHash = null;
}
}

public String getServerName(){
Expand All @@ -63,11 +70,21 @@ public VSphereBuildStep getBuildStep() {
@Override
public boolean perform(final AbstractBuild<?, ?> build, final Launcher launcher, final BuildListener listener) {
try {
startLogs(listener.getLogger());
EnvVars env = build.getEnvironment(listener);
env.overrideAll(build.getBuildVariables()); // Add in matrix axes..
String expandedServerName = env.expand(serverName);

startLogs(listener.getLogger(), expandedServerName);
//Need to ensure this server is same as one that was previously saved.
//TODO - also need to improve logging here.
VSphere vsphere = VSphereBuildStep.VSphereBuildStepDescriptor.getVSphereCloudByHash(this.serverHash).vSphereInstance();
VSphere vsphere;
// select by hash if we have one
if (serverHash != null) {
vsphere = VSphereBuildStep.VSphereBuildStepDescriptor.getVSphereCloudByHash(serverHash).vSphereInstance();
} else {
vsphere = VSphereBuildStep.VSphereBuildStepDescriptor.getVSphereCloudByName(expandedServerName).vSphereInstance();
}

buildStep.setVsphere(vsphere);

return buildStep.perform(build, launcher, listener);
Expand All @@ -77,7 +94,7 @@ public boolean perform(final AbstractBuild<?, ?> build, final Launcher launcher,
return false;
}

private void startLogs(PrintStream logger){
private void startLogs(PrintStream logger, String serverName){
VSphereLogger.vsLogger(logger,"");
VSphereLogger.vsLogger(logger,
Messages.console_buildStepStart(buildStep.getDescriptor().getDisplayName()));
Expand Down Expand Up @@ -115,11 +132,16 @@ public ListBoxModel doFillServerNameItems(){

//adding try block to prevent page from not loading
try{
for (Cloud cloud : Hudson.getInstance().clouds) {
if (cloud instanceof vSphereCloud ){
boolean hasVsphereClouds = false;
for (Cloud cloud : Hudson.getInstance().clouds) {
if (cloud instanceof vSphereCloud ) {
hasVsphereClouds = true;
select.add( ((vSphereCloud) cloud).getVsDescription() );
}
}
if (hasVsphereClouds) {
select.add(SELECTABLE_SERVER_NAME);
}
}catch(Exception e){
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div>
The vSphere configuration to use.
The vSphere configuration to use. Use ${VSPHERE_CLOUD_NAME} and create a matching parameter if you want to dynamically select the vSphere configuration.
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ console.buildStepStart=Performing vSphere build step: "{0}"
console.usingServerConfig=Attempting to use server configuration: "{0}"

validation.serverExistence=Server does not exist in global config! Please re-save your job configuration.
validation.instanceNotFound=Could not find our vSphere Cloud instance!
validation.instanceNotFound=Could not find vSphere Cloud instance "{0}"!

0 comments on commit e64d7ef

Please sign in to comment.