Java Code Examples for jenkins.model.Jenkins#getInstanceOrNull()

The following examples show how to use jenkins.model.Jenkins#getInstanceOrNull() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Reaper.java    From kubernetes-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public void eventReceived(Watcher.Action action, Pod pod) {
    String ns = pod.getMetadata().getNamespace();
    String name = pod.getMetadata().getName();
    Jenkins jenkins = Jenkins.getInstanceOrNull();
    if (jenkins == null) {
        return;
    }
    Optional<KubernetesSlave> optionalNode = resolveNode(jenkins, ns, name);
    if (!optionalNode.isPresent()) {
        return;
    }
    ExtensionList.lookup(Listener.class).forEach(listener -> {
        try {
            listener.onEvent(action, optionalNode.get(), pod);
        } catch (Exception x) {
            LOGGER.log(Level.WARNING, "Listener " + listener + " failed for " + ns + "/" + name, x);
        }
    });
}
 
Example 2
Source File: AWSClientFactory.java    From awseb-deployment-plugin with Apache License 2.0 6 votes vote down vote up
private static AmazonWebServicesCredentials lookupNamedCredential(String credentialsId)
        throws CredentialNotFoundException {
    final Jenkins jenkins = Jenkins.getInstanceOrNull();

    if (jenkins == null)
        throw new RuntimeException("Missing Jenkins Instance");

    List<AmazonWebServicesCredentials> credentialList =
            CredentialsProvider.lookupCredentials(
                    AmazonWebServicesCredentials.class, jenkins, ACL.SYSTEM,
                    Collections.<DomainRequirement>emptyList());

    AmazonWebServicesCredentials cred =
            CredentialsMatchers.firstOrNull(credentialList,
                    CredentialsMatchers.allOf(
                            CredentialsMatchers.withId(credentialsId)));

    if (cred == null) {
        throw new CredentialNotFoundException(credentialsId);
    }
    return cred;
}
 
Example 3
Source File: Gitea.java    From gitea-plugin with MIT License 5 votes vote down vote up
public Gitea jenkinsPluginClassLoader() {
    // HACK for Jenkins
    // by rights this should be the context classloader, but Jenkins does not expose plugins on that
    // so we need instead to use the uberClassLoader as that will have the implementations
    Jenkins instance = Jenkins.getInstanceOrNull();
    classLoader = instance == null ? getClass().getClassLoader() : instance.getPluginManager().uberClassLoader;
    // END HACK
    return this;
}
 
Example 4
Source File: BlueRunChangesetPreloader.java    From blueocean-plugin with MIT License 5 votes vote down vote up
private BluePipeline getPipeline(BlueUrlTokenizer blueUrl) {
    Jenkins jenkins = Jenkins.getInstanceOrNull();
    if (jenkins == null) { return null; }
    String pipelineFullName = blueUrl.getPart(BlueUrlTokenizer.UrlPart.PIPELINE);

    try {
        Item pipelineJob = jenkins.getItemByFullName(pipelineFullName);
        return (BluePipeline) BluePipelineFactory.resolve(pipelineJob);
    } catch (Exception e) {
        LOGGER.log(Level.FINE, String.format("Unable to find Job named '%s'.", pipelineFullName), e);
        return null;
    }
}
 
Example 5
Source File: Connector.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * Uses proxy if configured on pluginManager/advanced page
 *
 * @param host GitHub's hostname to build proxy to
 *
 * @return proxy to use it in connector. Should not be null as it can lead to unexpected behaviour
 */
@Nonnull
private static Proxy getProxy(@Nonnull String host) {
    Jenkins jenkins = Jenkins.getInstanceOrNull();
    if (jenkins == null || jenkins.proxy == null) {
        return Proxy.NO_PROXY;
    } else {
        return jenkins.proxy.createProxy(host);
    }
}
 
Example 6
Source File: KubernetesCloud.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused") // used by jelly
public List<Descriptor<PodRetention>> getAllowedPodRetentions() {
    Jenkins jenkins = Jenkins.getInstanceOrNull();
    if (jenkins == null) {
        return new ArrayList<>(0);
    }
    return DescriptorVisibilityFilter.apply(this, jenkins.getDescriptorList(PodRetention.class));
}
 
Example 7
Source File: KubernetesCloud.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"rawtypes", "unused"}) // used by jelly
public Descriptor getDefaultPodRetention() {
    Jenkins jenkins = Jenkins.getInstanceOrNull();
    if (jenkins == null) {
        return null;
    }
    return jenkins.getDescriptor(PodRetention.getKubernetesCloudDefault().getClass());
}
 
Example 8
Source File: DockerTemplate.java    From docker-plugin with MIT License 5 votes vote down vote up
/**
 * Returns a node name for a new node that doesn't clash with any we've
 * currently got.
 * 
 * @param templateName
 *            The template's {@link #getName()}. This is used as a prefix for
 *            the node name.
 * @return A unique unused node name suitable for use as a slave name for a
 *         slave created from this template.
 */
private static String calcUnusedNodeName(final String templateName) {
    final Jenkins jenkins = Jenkins.getInstanceOrNull();
    while (true) {
        // make a should-be-unique ID
        final String uniqueId = ID_GENERATOR.getUniqueId();
        final String nodeName = templateName + '-' + uniqueId;
        // now check it doesn't collide with any existing slaves that might
        // have been made previously.
        if (jenkins == null || jenkins.getNode(nodeName) == null) {
            return nodeName;
        }
    }
}