forked from jenkinsci/vsphere-cloud-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerOff.java
More file actions
184 lines (148 loc) · 5.53 KB
/
PowerOff.java
File metadata and controls
184 lines (148 loc) · 5.53 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* Copyright 2013, MANDIANT, Eric Lordahl
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jenkinsci.plugins.vsphere.builders;
import hudson.*;
import hudson.model.*;
import hudson.tasks.BuildStep;
import hudson.tasks.BuildStepMonitor;
import hudson.util.FormValidation;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Collection;
import javax.annotation.Nonnull;
import javax.servlet.ServletException;
import jenkins.tasks.SimpleBuildStep;
import org.jenkinsci.plugins.vsphere.VSphereBuildStep;
import org.jenkinsci.plugins.vsphere.tools.VSphere;
import org.jenkinsci.plugins.vsphere.tools.VSphereException;
import org.jenkinsci.plugins.vsphere.tools.VSphereLogger;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import com.vmware.vim25.mo.VirtualMachine;
public class PowerOff extends VSphereBuildStep implements SimpleBuildStep {
private final String vm;
private final boolean evenIfSuspended;
private final boolean shutdownGracefully;
private final boolean ignoreIfNotExists;
@DataBoundConstructor
public PowerOff( final String vm, final boolean evenIfSuspended, final boolean shutdownGracefully, final boolean ignoreIfNotExists) throws VSphereException {
this.vm = vm;
this.evenIfSuspended = evenIfSuspended;
this.shutdownGracefully = shutdownGracefully;
this.ignoreIfNotExists = ignoreIfNotExists;
}
public boolean isIgnoreIfNotExists() {
return ignoreIfNotExists;
}
public boolean isEvenIfSuspended() {
return evenIfSuspended;
}
public boolean isShutdownGracefully() {return shutdownGracefully; }
public String getVm() {
return vm;
}
@Override
public boolean prebuild(AbstractBuild<?, ?> abstractBuild, BuildListener buildListener) {
return false;
}
@Override
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException {
try {
powerOff(run, launcher, listener);
} catch (Exception e) {
throw new AbortException(e.getMessage());
}
}
@Override
public boolean perform(final AbstractBuild<?, ?> build, Launcher launcher, final BuildListener listener) {
boolean retVal = false;
try {
retVal = powerOff(build, launcher, listener);
} catch (VSphereException e) {
e.printStackTrace();
}
return retVal;
//TODO throw AbortException instead of returning value
}
@Override
public Action getProjectAction(AbstractProject<?, ?> abstractProject) {
return null;
}
@Override
public Collection<? extends Action> getProjectActions(AbstractProject<?, ?> abstractProject) {
return null;
}
@Override
public BuildStepMonitor getRequiredMonitorService() {
return null;
}
private boolean powerOff(final Run<?, ?> run, Launcher launcher, final TaskListener listener) throws VSphereException{
PrintStream jLogger = listener.getLogger();
EnvVars env;
String expandedVm = vm;
if (run instanceof AbstractBuild) {
try {
env = run.getEnvironment(listener);
} catch (Exception e) {
throw new VSphereException(e);
}
env.overrideAll(((AbstractBuild)run).getBuildVariables()); // Add in matrix axes..
expandedVm = env.expand(vm);
}
VSphereLogger.vsLogger(jLogger, "Shutting Down VM " + expandedVm + "...");
VirtualMachine vsphereVm = vsphere.getVmByName(expandedVm);
if (vsphereVm == null && !ignoreIfNotExists) {
throw new RuntimeException(Messages.validation_notFound("vm " + expandedVm));
}
if (vsphereVm != null) {
vsphere.powerOffVm(vsphereVm, evenIfSuspended, shutdownGracefully);
VSphereLogger.vsLogger(jLogger, "Successfully shutdown \"" + expandedVm + "\"");
} else {
VSphereLogger.vsLogger(jLogger, "Does not exists, BUT ignore it! \"" + expandedVm + "\"");
}
return true;
}
@Extension
public static class PowerOffDescriptor extends VSphereBuildStepDescriptor {
@Override
public String getDisplayName() {
return Messages.vm_title_PowerOff();
}
public FormValidation doCheckVm(@QueryParameter String value)
throws IOException, ServletException {
if (value.length() == 0)
return FormValidation.error(Messages.validation_required("the VM name"));
return FormValidation.ok();
}
public FormValidation doTestData(@QueryParameter String serverName,
@QueryParameter String vm) {
try {
if (serverName.length() == 0 || vm.length()==0 )
return FormValidation.error(Messages.validation_requiredValues());
if (vm.indexOf('$') >= 0)
return FormValidation.warning(Messages.validation_buildParameter("VM"));
VSphere vsphere = getVSphereCloudByName(serverName).vSphereInstance();
VirtualMachine vmObj = vsphere.getVmByName(vm);
if ( vmObj == null)
return FormValidation.error(Messages.validation_notFound("VM"));
if (vmObj.getConfig().template)
return FormValidation.error(Messages.validation_notActually("VM"));
return FormValidation.ok(Messages.validation_success());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}