Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VsphereSelection: Allow dynamic selection of vsphere cloud #24

Merged
merged 1 commit into from
Feb 3, 2015
Merged
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
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 @@ -39,4 +39,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}"!