Search in sources :

Example 26 with Jenkins

use of jenkins.model.Jenkins in project support-core-plugin by jenkinsci.

the class RootCAs method addContents.

private void addContents(@NonNull Container container, @NonNull final Node node) {
    Computer c = node.toComputer();
    if (c == null) {
        return;
    }
    String name;
    String[] params;
    if (node instanceof Jenkins) {
        name = "nodes/master/RootCA.txt";
        params = new String[0];
    } else {
        name = "nodes/slave/{0}/RootCA.txt";
        params = new String[] { node.getNodeName() };
    }
    try {
        container.add(new UnfilteredStringContent(name, params, getRootCA(node)));
    } catch (IOException e) {
        container.add(new UnfilteredStringContent(name, params, Functions.printThrowable(e)));
    }
}
Also used : Jenkins(jenkins.model.Jenkins) UnfilteredStringContent(com.cloudbees.jenkins.support.api.UnfilteredStringContent) Computer(hudson.model.Computer) IOException(java.io.IOException)

Example 27 with Jenkins

use of jenkins.model.Jenkins in project support-core-plugin by jenkinsci.

the class SupportPlugin method threadDumpStartup.

@Initializer(after = InitMilestone.STARTED)
public static void threadDumpStartup() throws Exception {
    if (!logStartupPerformanceIssues)
        return;
    final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    final File f = new File(getRootDirectory(), "/startup-threadDump" + dateFormat.format(new Date()) + ".txt");
    if (!f.exists()) {
        try {
            f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    Thread t = new Thread("Support core plugin startup diagnostics") {

        @Override
        public void run() {
            try {
                while (true) {
                    final Jenkins jenkins = Jenkins.getInstanceOrNull();
                    if (jenkins == null || jenkins.getInitLevel() != InitMilestone.COMPLETED) {
                        continue;
                    }
                    try (PrintStream ps = new PrintStream(new FileOutputStream(f, true), false, "UTF-8")) {
                        ps.println("=== Thread dump at " + new Date() + " ===");
                        ThreadDumps.threadDump(ps);
                        // Generate a thread dump every few seconds/minutes
                        ps.flush();
                        TimeUnit.SECONDS.sleep(secondsPerThreadDump);
                    } catch (FileNotFoundException | UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                Thread.currentThread().interrupt();
            }
        }
    };
    t.start();
}
Also used : Jenkins(jenkins.model.Jenkins) PrintStream(java.io.PrintStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) Date(java.util.Date) Initializer(hudson.init.Initializer)

Example 28 with Jenkins

use of jenkins.model.Jenkins in project support-core-plugin by jenkinsci.

the class ConfigFileComponent method addContents.

@Override
public void addContents(@NonNull Container container) {
    Jenkins jenkins = Jenkins.get();
    File configFile = new File(jenkins.getRootDir(), "config.xml");
    if (configFile.exists()) {
        container.add(new XmlRedactedSecretFileContent("jenkins-root-configuration-files/{0}", new String[] { configFile.getName() }, configFile));
    } else {
        // this should never happen..
        LOGGER.log(Level.WARNING, "Jenkins global config file does not exist.");
    }
}
Also used : Jenkins(jenkins.model.Jenkins) File(java.io.File)

Example 29 with Jenkins

use of jenkins.model.Jenkins in project gitea-plugin by jenkinsci.

the class Gitea method jenkinsPluginClassLoader.

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.getInstance();
    classLoader = instance == null ? getClass().getClassLoader() : instance.getPluginManager().uberClassLoader;
    // END HACK
    return this;
}
Also used : Jenkins(jenkins.model.Jenkins)

Example 30 with Jenkins

use of jenkins.model.Jenkins in project workflow-cps-plugin by jenkinsci.

the class Snippetizer method doGenerateSnippet.

// accessed via REST API
@Restricted(DoNotUse.class)
public HttpResponse doGenerateSnippet(StaplerRequest req, @QueryParameter String json) throws Exception {
    // TODO is there not an easier way to do this? Maybe Descriptor.newInstancesFromHeteroList on a one-element JSONArray?
    JSONObject jsonO = JSONObject.fromObject(json);
    Jenkins j = Jenkins.getActiveInstance();
    Class<?> c = j.getPluginManager().uberClassLoader.loadClass(jsonO.getString("stapler-class"));
    Descriptor descriptor = j.getDescriptor(c.asSubclass(Describable.class));
    if (descriptor == null) {
        return HttpResponses.plainText("<could not find " + c.getName() + ">");
    }
    Object o;
    try {
        o = descriptor.newInstance(req, jsonO);
    } catch (RuntimeException x) {
        // e.g. IllegalArgumentException
        return HttpResponses.plainText(Functions.printThrowable(x));
    }
    try {
        Step step = null;
        if (o instanceof Step) {
            step = (Step) o;
        } else {
            // Look for a metastep which could take this as its delegate.
            for (StepDescriptor d : StepDescriptor.allMeta()) {
                if (d.getMetaStepArgumentType().isInstance(o)) {
                    DescribableModel<?> m = DescribableModel.of(d.clazz);
                    DescribableParameter soleRequiredParameter = m.getSoleRequiredParameter();
                    if (soleRequiredParameter != null) {
                        step = d.newInstance(Collections.singletonMap(soleRequiredParameter.getName(), o));
                        break;
                    }
                }
            }
        }
        if (step == null) {
            return HttpResponses.plainText("Cannot find a step corresponding to " + o.getClass().getName());
        }
        String groovy = step2Groovy(step);
        if (descriptor instanceof StepDescriptor && ((StepDescriptor) descriptor).isAdvanced()) {
            String warning = Messages.Snippetizer_this_step_should_not_normally_be_used_in();
            groovy = "// " + warning + "\n" + groovy;
        }
        return HttpResponses.plainText(groovy);
    } catch (UnsupportedOperationException x) {
        Logger.getLogger(CpsFlowExecution.class.getName()).log(Level.WARNING, "failed to render " + json, x);
        return HttpResponses.plainText(x.getMessage());
    }
}
Also used : DescribableParameter(org.jenkinsci.plugins.structs.describable.DescribableParameter) UninstantiatedDescribable(org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable) Describable(hudson.model.Describable) Step(org.jenkinsci.plugins.workflow.steps.Step) SimpleBuildStep(jenkins.tasks.SimpleBuildStep) Jenkins(jenkins.model.Jenkins) JSONObject(net.sf.json.JSONObject) BuildStepDescriptor(hudson.tasks.BuildStepDescriptor) Descriptor(hudson.model.Descriptor) StepDescriptor(org.jenkinsci.plugins.workflow.steps.StepDescriptor) BuildStepDescriptor(hudson.tasks.BuildStepDescriptor) StepDescriptor(org.jenkinsci.plugins.workflow.steps.StepDescriptor) JSONObject(net.sf.json.JSONObject) Restricted(org.kohsuke.accmod.Restricted)

Aggregations

Jenkins (jenkins.model.Jenkins)73 Test (org.junit.Test)22 ConfiguredWithCode (org.jenkinsci.plugins.casc.misc.ConfiguredWithCode)13 IOException (java.io.IOException)10 File (java.io.File)9 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)9 FlowExecution (org.jenkinsci.plugins.workflow.flow.FlowExecution)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Map (java.util.Map)7 Statement (org.junit.runners.model.Statement)7 CheckForNull (javax.annotation.CheckForNull)6 FilePath (hudson.FilePath)5 Computer (hudson.model.Computer)5 URL (java.net.URL)5 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)5 Issue (org.jvnet.hudson.test.Issue)5 Item (hudson.model.Item)4 Node (hudson.model.Node)4 Date (java.util.Date)4