Skip to content

Commit

Permalink
Use non-deprecated get() and remove some unneeded null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Raihaan Shouhell committed May 24, 2019
1 parent 4d574f7 commit 6abf562
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 80 deletions.
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/ec2/AmazonEC2Cloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public FormValidation doCheckCloudName(@QueryParameter String value) {

String cloudId = createCloudId(value);
int found = 0;
for (Cloud c : Jenkins.getInstance().clouds) {
for (Cloud c : Jenkins.get().clouds) {
if (c.name.equals(cloudId)) {
found++;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/hudson/plugins/ec2/EC2AbstractSlave.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ protected Object readResolve() {
}

public EC2Cloud getCloud() {
return (EC2Cloud) Jenkins.getInstance().getCloud(cloudName);
return (EC2Cloud) Jenkins.get().getCloud(cloudName);
}

/**
Expand Down Expand Up @@ -396,7 +396,7 @@ void stop() {
} catch (AmazonClientException e) {
LOGGER.log(Level.WARNING, "Failed to stop EC2 instance: " + getInstanceId(), e);
}

}

boolean terminateInstance() {
Expand Down Expand Up @@ -725,7 +725,7 @@ public ListBoxModel doFillZoneItems(@QueryParameter boolean useInstanceProfileFo
}

public List<Descriptor<AMITypeData>> getAMITypeDescriptors() {
return Jenkins.getInstance().getDescriptorList(AMITypeData.class);
return Jenkins.get().getDescriptorList(AMITypeData.class);
}
}

Expand Down
54 changes: 25 additions & 29 deletions src/main/java/hudson/plugins/ec2/EC2Cloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ protected Object readResolve() {
}
}
// CREATE
for (CredentialsStore credentialsStore: CredentialsProvider.lookupStores(Jenkins.getInstance())) {
for (CredentialsStore credentialsStore: CredentialsProvider.lookupStores(Jenkins.get())) {

if (credentialsStore instanceof SystemCredentialsProvider.StoreImpl) {

Expand Down Expand Up @@ -324,7 +324,7 @@ public void doAttach(StaplerRequest req, StaplerResponse rsp, @QueryParameter St
StringWriter sw = new StringWriter();
StreamTaskListener listener = new StreamTaskListener(sw);
EC2AbstractSlave node = t.attach(id, listener);
Jenkins.getInstance().addNode(node);
Jenkins.get().addNode(node);

rsp.sendRedirect2(req.getContextPath() + "/computer/" + node.getNodeName());
}
Expand All @@ -339,7 +339,7 @@ public HttpResponse doProvision(@QueryParameter String template) throws ServletE
throw HttpResponses.error(SC_BAD_REQUEST, "No such template: " + template);
}

final Jenkins jenkinsInstance = Jenkins.getInstance();
final Jenkins jenkinsInstance = Jenkins.get();
if (jenkinsInstance.isQuietingDown()) {
throw HttpResponses.error(SC_BAD_REQUEST, "Jenkins instance is quieting down");
}
Expand Down Expand Up @@ -372,9 +372,7 @@ public HttpResponse doProvision(@QueryParameter String template) throws ServletE
*/
private int countCurrentEC2Slaves(SlaveTemplate template) throws AmazonClientException {
String jenkinsServerUrl = null;
JenkinsLocationConfiguration jenkinsLocation = JenkinsLocationConfiguration.get();
if (jenkinsLocation != null)
jenkinsServerUrl = jenkinsLocation.getUrl();
jenkinsServerUrl = JenkinsLocationConfiguration.get().getUrl();

if (jenkinsServerUrl == null) {
LOGGER.log(Level.WARNING, "No Jenkins server URL specified, it is strongly recommended to open /configure and set the server URL. " +
Expand All @@ -395,8 +393,8 @@ private int countCurrentEC2Slaves(SlaveTemplate template) throws AmazonClientExc
&& isEc2ProvisionedJenkinsSlave(i.getTags(), jenkinsServerUrl)
&& (template == null || template.getAmi().equals(i.getImageId()))) {
InstanceStateName stateName = InstanceStateName.fromValue(i.getState().getName());
if (stateName != InstanceStateName.Terminated &&
stateName != InstanceStateName.ShuttingDown &&
if (stateName != InstanceStateName.Terminated &&
stateName != InstanceStateName.ShuttingDown &&
stateName != InstanceStateName.Stopped ) {
LOGGER.log(Level.FINE, "Existing instance found: " + i.getInstanceId() + " AMI: " + i.getImageId()
+ (template != null ? (" Template: " + description) : "") + " Jenkins Server: " + jenkinsServerUrl);
Expand Down Expand Up @@ -450,15 +448,15 @@ && isEc2ProvisionedJenkinsSlave(i.getTags(), jenkinsServerUrl)
}
} else {
// Canceled or otherwise dead
for (Node node : Jenkins.getInstance().getNodes()) {
for (Node node : Jenkins.get().getNodes()) {
try {
if (!(node instanceof EC2SpotSlave))
continue;
EC2SpotSlave ec2Slave = (EC2SpotSlave) node;
if (ec2Slave.getSpotInstanceRequestId().equals(sir.getSpotInstanceRequestId())) {
LOGGER.log(Level.INFO, "Removing dead request: " + sir.getSpotInstanceRequestId() + " AMI: "
+ sir.getInstanceId() + " state: " + sir.getState() + " status: " + sir.getStatus());
Jenkins.getInstance().removeNode(node);
Jenkins.get().removeNode(node);
break;
}
} catch (IOException e) {
Expand All @@ -473,7 +471,7 @@ && isEc2ProvisionedJenkinsSlave(i.getTags(), jenkinsServerUrl)

// Count nodes where the spot request does not yet exist (sometimes it takes time for the request to appear
// in the EC2 API)
for (Node node : Jenkins.getInstance().getNodes()) {
for (Node node : Jenkins.get().getNodes()) {
if (!(node instanceof EC2SpotSlave))
continue;
EC2SpotSlave ec2Slave = (EC2SpotSlave) node;
Expand All @@ -495,14 +493,14 @@ && isEc2ProvisionedJenkinsSlave(i.getTags(), jenkinsServerUrl)
List<Tag> instanceTags = sir.getTags();
for (Tag tag : instanceTags) {
if (StringUtils.equals(tag.getKey(), EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE) && StringUtils.equals(tag.getValue(), getSlaveTypeTagValue(EC2_SLAVE_TYPE_SPOT, template.description)) && sir.getLaunchSpecification().getImageId().equals(template.getAmi())) {

if (sir.getInstanceId() != null && instanceIds.contains(sir.getInstanceId()))
continue;

LOGGER.log(Level.FINE, "Spot instance request found (from node): " + sir.getSpotInstanceRequestId() + " AMI: "
+ sir.getInstanceId() + " state: " + sir.getState() + " status: " + sir.getStatus());
n++;

if (sir.getInstanceId() != null)
instanceIds.add(sir.getInstanceId());
}
Expand Down Expand Up @@ -597,16 +595,14 @@ public Collection<PlannedNode> provision(final Label label, int excessWorkload)
final SlaveTemplate t = getTemplate(label);
List<PlannedNode> plannedNodes = new ArrayList<>();

Jenkins jenkinsInstance = Jenkins.getInstance();
if (jenkinsInstance != null) {
if (jenkinsInstance.isQuietingDown()) {
LOGGER.log(Level.FINE, "Not provisioning nodes, Jenkins instance is quieting down");
return Collections.emptyList();
}
else if (jenkinsInstance.isTerminating()) {
LOGGER.log(Level.FINE, "Not provisioning nodes, Jenkins instance is terminating");
return Collections.emptyList();
}
Jenkins jenkinsInstance = Jenkins.get();
if (jenkinsInstance.isQuietingDown()) {
LOGGER.log(Level.FINE, "Not provisioning nodes, Jenkins instance is quieting down");
return Collections.emptyList();
}
else if (jenkinsInstance.isTerminating()) {
LOGGER.log(Level.FINE, "Not provisioning nodes, Jenkins instance is terminating");
return Collections.emptyList();
}

try {
Expand All @@ -631,7 +627,7 @@ else if (jenkinsInstance.isTerminating()) {

LOGGER.log(Level.INFO, "{0}. Attempting provision finished, excess workload: " + excessWorkload, t);
LOGGER.log(Level.INFO, "We have now {0} computers, waiting for {1} more",
new Object[]{Jenkins.getInstance().getComputers().length, plannedNodes.size()});
new Object[]{jenkinsInstance.getComputers().length, plannedNodes.size()});
return plannedNodes;
} catch (AmazonClientException e) {
LOGGER.log(Level.WARNING, t + ". Exception during provisioning", e);
Expand Down Expand Up @@ -673,7 +669,7 @@ public Node call() throws Exception {
if (slave.getStopOnTerminate() && (c != null )) {
c.connect(false);
}

long startTime = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - instance.getLaunchTime().getTime());
LOGGER.log(Level.INFO, "{0} Node {1} moved to RUNNING state in {2} seconds and is ready to be connected by Jenkins",
new Object[]{t, slave.getNodeName(), startTime});
Expand Down Expand Up @@ -748,7 +744,7 @@ private static AmazonWebServicesCredentials getCredentials(@Nullable String cred
return null;
}
return (AmazonWebServicesCredentials) CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentials(AmazonWebServicesCredentials.class, Jenkins.getInstance(),
CredentialsProvider.lookupCredentials(AmazonWebServicesCredentials.class, Jenkins.get(),
ACL.SYSTEM, Collections.emptyList()),
CredentialsMatchers.withId(credentialsId));
}
Expand Down Expand Up @@ -785,7 +781,7 @@ public static ClientConfiguration createClientConfiguration(final String host) {
// cause problems. Raise it a bit.
// See: https://issues.jenkins-ci.org/browse/JENKINS-26800
config.setSignerOverride("AWS4SignerType");
ProxyConfiguration proxyConfig = Jenkins.getInstance().proxy;
ProxyConfiguration proxyConfig = Jenkins.get().proxy;
Proxy proxy = proxyConfig == null ? Proxy.NO_PROXY : proxyConfig.createProxy(host);
if (!proxy.equals(Proxy.NO_PROXY) && proxy.address() instanceof InetSocketAddress) {
InetSocketAddress address = (InetSocketAddress) proxy.address();
Expand Down Expand Up @@ -942,7 +938,7 @@ public ListBoxModel doFillCredentialsIdItems() {
.withMatching(
CredentialsMatchers.always(),
CredentialsProvider.lookupCredentials(AmazonWebServicesCredentials.class,
Jenkins.getInstance(),
Jenkins.get(),
ACL.SYSTEM,
Collections.emptyList()));
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/hudson/plugins/ec2/EC2OndemandSlave.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

/**
* Slave running on EC2.
*
*
* @author Kohsuke Kawaguchi
*/
public final class EC2OndemandSlave extends EC2AbstractSlave {
Expand Down Expand Up @@ -83,7 +83,7 @@ public void terminate() {
ec2.terminateInstances(request);
LOGGER.info("Terminated EC2 instance (terminated): " + getInstanceId());
}
Jenkins.getInstance().removeNode(this);
Jenkins.get().removeNode(this);
LOGGER.info("Removed EC2 instance from jenkins master: " + getInstanceId());
} catch (AmazonClientException | IOException e) {
LOGGER.log(Level.WARNING, "Failed to terminate EC2 instance: " + getInstanceId(), e);
Expand All @@ -99,7 +99,7 @@ public Node reconfigure(final StaplerRequest req, JSONObject form) throws FormEx
if (!isAlive(true)) {
LOGGER.info("EC2 instance terminated externally: " + getInstanceId());
try {
Jenkins.getInstance().removeNode(this);
Jenkins.get().removeNode(this);
} catch (IOException ioe) {
LOGGER.log(Level.WARNING, "Attempt to reconfigure EC2 instance which has been externally terminated: "
+ getInstanceId(), ioe);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/ec2/EC2RetentionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private long internalCheck(EC2Computer computer) {
@Override
public void start(EC2Computer c) {
//Jenkins is in the process of starting up
if (Jenkins.getInstance().getInitLevel() != InitMilestone.COMPLETED) {
if (Jenkins.get().getInitLevel() != InitMilestone.COMPLETED) {
InstanceState state = null;
try {
state = c.getState();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/hudson/plugins/ec2/EC2SlaveMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public long getRecurrencePeriod() {

@Override
protected void execute(TaskListener listener) throws IOException, InterruptedException {
for (Node node : Jenkins.getInstance().getNodes()) {
for (Node node : Jenkins.get().getNodes()) {
if (node instanceof EC2AbstractSlave) {
final EC2AbstractSlave ec2Slave = (EC2AbstractSlave) node;
try {
Expand All @@ -54,7 +54,7 @@ protected void execute(TaskListener listener) throws IOException, InterruptedExc

private void removeNode(EC2AbstractSlave ec2Slave) {
try {
Jenkins.getInstance().removeNode(ec2Slave);
Jenkins.get().removeNode(ec2Slave);
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to remove node: " + ec2Slave.getInstanceId());
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/ec2/EC2SpotSlave.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void terminate() {
// manually or a spot instance was killed due to pricing. If we don't remove the node,
// we screw up auto-scaling, since it will continue to count against the quota.
try {
Jenkins.getInstance().removeNode(this);
Jenkins.get().removeNode(this);
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to remove slave: " + name, e);
}
Expand Down
26 changes: 12 additions & 14 deletions src/main/java/hudson/plugins/ec2/NoDelayProvisionerStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,19 @@ public NodeProvisioner.StrategyDecision apply(NodeProvisioner.StrategyState stra
LOGGER.log(Level.FINE, "Available capacity={0}, currentDemand={1}",
new Object[]{availableCapacity, currentDemand});
if (availableCapacity < currentDemand) {
Jenkins jenkinsInstance = Jenkins.getInstance();
if (jenkinsInstance != null) {
for (Cloud cloud : jenkinsInstance.clouds) {
if (!(cloud instanceof AmazonEC2Cloud)) continue;
if (!cloud.canProvision(label)) continue;
AmazonEC2Cloud ec2 = (AmazonEC2Cloud) cloud;
if (!ec2.isNoDelayProvisioning()) continue;
Jenkins jenkinsInstance = Jenkins.get();
for (Cloud cloud : jenkinsInstance.clouds) {
if (!(cloud instanceof AmazonEC2Cloud)) continue;
if (!cloud.canProvision(label)) continue;
AmazonEC2Cloud ec2 = (AmazonEC2Cloud) cloud;
if (!ec2.isNoDelayProvisioning()) continue;

Collection<NodeProvisioner.PlannedNode> plannedNodes = cloud.provision(label, currentDemand - availableCapacity);
LOGGER.log(Level.FINE, "Planned {0} new nodes", plannedNodes.size());
strategyState.recordPendingLaunches(plannedNodes);
availableCapacity += plannedNodes.size();
LOGGER.log(Level.FINE, "After provisioning, available capacity={0}, currentDemand={1}", new Object[]{availableCapacity, currentDemand});
break;
}
Collection<NodeProvisioner.PlannedNode> plannedNodes = cloud.provision(label, currentDemand - availableCapacity);
LOGGER.log(Level.FINE, "Planned {0} new nodes", plannedNodes.size());
strategyState.recordPendingLaunches(plannedNodes);
availableCapacity += plannedNodes.size();
LOGGER.log(Level.FINE, "After provisioning, available capacity={0}, currentDemand={1}", new Object[]{availableCapacity, currentDemand});
break;
}
}
if (availableCapacity >= currentDemand) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/hudson/plugins/ec2/PluginImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public void start() throws Exception {
}

public DescriptorImpl getDescriptor() {
return (DescriptorImpl) Jenkins.getInstance().getDescriptorOrDie(getClass());
return (DescriptorImpl) Jenkins.get().getDescriptorOrDie(getClass());
}

public static PluginImpl get() {
return Jenkins.getInstance().getPlugin(PluginImpl.class);
return Jenkins.get().getPlugin(PluginImpl.class);
}

@Extension
Expand Down

0 comments on commit 6abf562

Please sign in to comment.