-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathvSphereStep.java
More file actions
141 lines (112 loc) · 4.45 KB
/
vSphereStep.java
File metadata and controls
141 lines (112 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package org.jenkinsci.plugins.workflow;
import com.google.inject.Inject;
import hudson.*;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.slaves.Cloud;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.vSphereCloud;
import org.jenkinsci.plugins.vsphere.VSphereBuildStep;
import org.jenkinsci.plugins.vsphere.VSphereBuildStepContainer;
import org.jenkinsci.plugins.vsphere.builders.*;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution;
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import java.util.Map;
/**
* The vSphere invocation step for the Jenkins workflow plugin.
*/
public class vSphereStep extends AbstractStepImpl {
private String serverName;
private VSphereBuildStep buildStep;
public String getServerName() {
return serverName;
}
public VSphereBuildStep getBuildStep() {
return buildStep;
}
@DataBoundConstructor
public vSphereStep() {
}
@DataBoundSetter
public void setBuildStep(VSphereBuildStep buildStep) {
this.buildStep = buildStep;
}
@DataBoundSetter
public void setServerName(String serverName) {
this.serverName = serverName;
}
@Extension
public static final class DescriptorImpl extends AbstractStepDescriptorImpl {
public DescriptorImpl() {
super(vSphereExecution.class);
}
@Override
public String getFunctionName() {
return "vSphere";
}
@Override
public String getDisplayName() {
return "Invoke an vSphere action, exposing the VM IP under some actions";
}
public ListBoxModel doFillServerNameItems() {
ListBoxModel select = new ListBoxModel();
try {
for (Cloud cloud : Jenkins.getInstance().clouds) {
if (cloud instanceof vSphereCloud) {
select.add(((vSphereCloud) cloud).getVsDescription());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return select;
}
public DescriptorExtensionList<VSphereBuildStep, VSphereBuildStep.VSphereBuildStepDescriptor> getBuildSteps() {
return Jenkins.getInstance().getDescriptorList(VSphereBuildStep.class);
}
}
public static final class vSphereExecution extends AbstractSynchronousNonBlockingStepExecution<String> {
private static final long serialVersionUID = 1;
private transient VSphereBuildStepContainer vSphereBSC;
@Inject
private transient vSphereStep step;
@StepContextParameter
private transient Run run;
@StepContextParameter
private transient FilePath filePath;
@StepContextParameter
private transient Launcher launcher;
@StepContextParameter
private transient TaskListener listener;
@StepContextParameter
private transient EnvVars envVars;
@Override
protected String run() throws Exception {
String IP = "";
vSphereBSC = new VSphereBuildStepContainer(step.getBuildStep(), step.getServerName());
vSphereBSC.perform(run, filePath, launcher, listener);
if (step.getBuildStep().getClass().toString().contains("PowerOn") ||
step.getBuildStep().getClass().toString().contains("Deploy") ||
step.getBuildStep().getClass().toString().contains("Clone") ||
step.getBuildStep().getClass().toString().contains("ExposeGuestInfo")) {
IP = step.getBuildStep().getIP();
if(IP != null){
envVars.put("VSPHERE_IP", IP);
}
if (step.getBuildStep().getClass().toString().contains("ExposeGuestInfo")) {
Map<String, String> envVars = ((ExposeGuestInfo)step.getBuildStep()).getVars();
for (Map.Entry<String, String> envVar: envVars.entrySet()) {
envVars.put(envVar.getKey(), envVar.getValue());
}
}
}
vSphereBSC = null;
return IP;
}
}
}