Search in sources :

Example 16 with XmlFile

use of hudson.XmlFile in project hudson-2.x by hudson.

the class Hudson method loadTasks.

private synchronized TaskBuilder loadTasks() throws IOException {
    File projectsDir = new File(root, "jobs");
    if (!projectsDir.isDirectory() && !projectsDir.mkdirs()) {
        if (projectsDir.exists()) {
            throw new IOException(projectsDir + " is not a directory");
        }
        throw new IOException("Unable to create " + projectsDir + "\nPermission issue? Please create this directory manually.");
    }
    File[] subdirs = projectsDir.listFiles(new FileFilter() {

        public boolean accept(File child) {
            return child.isDirectory() && Items.getConfigFile(child).exists();
        }
    });
    TaskGraphBuilder g = new TaskGraphBuilder();
    Handle loadHudson = g.requires(InitMilestone.EXTENSIONS_AUGMENTED).attains(InitMilestone.JOB_LOADED).add("Loading global config", new Executable() {

        public void run(Reactor session) throws Exception {
            XmlFile cfg = getConfigFile();
            if (cfg.exists()) {
                // reset some data that may not exist in the disk file
                // so that we can take a proper compensation action later.
                primaryView = null;
                views.clear();
                // load from disk
                cfg.unmarshal(Hudson.this);
            }
            // if we are loading old data that doesn't have this field
            if (slaves == null) {
                slaves = new NodeList();
            }
            clouds.setOwner(Hudson.this);
            items.clear();
        }
    });
    for (final File subdir : subdirs) {
        g.requires(loadHudson).attains(InitMilestone.JOB_LOADED).notFatal().add("Loading job " + subdir.getName(), new Executable() {

            public void run(Reactor session) throws Exception {
                TopLevelItem item = (TopLevelItem) Items.load(Hudson.this, subdir);
                items.put(item.getName(), item);
            }
        });
    }
    g.requires(InitMilestone.JOB_LOADED).add("Finalizing set up", new Executable() {

        public void run(Reactor session) throws Exception {
            rebuildDependencyGraph();
            {
                // recompute label objects - populates the labels mapping.
                for (// Note that not all labels are visible until the slaves have connected.
                Node slave : // Note that not all labels are visible until the slaves have connected.
                slaves) {
                    slave.getAssignedLabels();
                }
                getAssignedLabels();
            }
            // this is both for clean Hudson and for backward compatibility.
            if (views.size() == 0 || primaryView == null) {
                View v = new AllView(Messages.Hudson_ViewName());
                v.owner = Hudson.this;
                views.add(0, v);
                primaryView = v.getViewName();
            }
            // read in old data that doesn't have the security field set
            if (authorizationStrategy == null) {
                if (useSecurity == null || !useSecurity) {
                    authorizationStrategy = AuthorizationStrategy.UNSECURED;
                } else {
                    authorizationStrategy = new FullControlOnceLoggedInAuthorizationStrategy();
                }
            }
            if (securityRealm == null) {
                if (useSecurity == null || !useSecurity) {
                    setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
                } else {
                    setSecurityRealm(new LegacySecurityRealm());
                }
            } else {
                // force the set to proxy
                setSecurityRealm(securityRealm);
            }
            if (useSecurity != null && !useSecurity) {
                // forced reset to the unsecure mode.
                // this works as an escape hatch for people who locked themselves out.
                authorizationStrategy = AuthorizationStrategy.UNSECURED;
                setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
            }
            // Initialize the filter with the crumb issuer
            setCrumbIssuer(crumbIssuer);
            // auto register root actions
            for (Action a : getExtensionList(RootAction.class)) {
                if (!actions.contains(a)) {
                    actions.add(a);
                }
            }
        }
    });
    return g;
}
Also used : FullControlOnceLoggedInAuthorizationStrategy(hudson.security.FullControlOnceLoggedInAuthorizationStrategy) XmlFile(hudson.XmlFile) NodeList(hudson.slaves.NodeList) IOException(java.io.IOException) ExtensionListView(hudson.ExtensionListView) FormException(hudson.model.Descriptor.FormException) RestartNotSupportedException(hudson.lifecycle.RestartNotSupportedException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ServletException(javax.servlet.ServletException) ReactorException(org.jvnet.hudson.reactor.ReactorException) TimeoutException(java.util.concurrent.TimeoutException) ParseException(java.text.ParseException) JellyException(org.apache.commons.jelly.JellyException) ANTLRException(antlr.ANTLRException) AcegiSecurityException(org.acegisecurity.AcegiSecurityException) BindException(java.net.BindException) AccessDeniedException(org.acegisecurity.AccessDeniedException) TaskGraphBuilder(org.jvnet.hudson.reactor.TaskGraphBuilder) Handle(org.jvnet.hudson.reactor.TaskGraphBuilder.Handle) LegacySecurityRealm(hudson.security.LegacySecurityRealm) FileFilter(java.io.FileFilter) Executable(org.jvnet.hudson.reactor.Executable) Reactor(org.jvnet.hudson.reactor.Reactor) TextFile(hudson.util.TextFile) XmlFile(hudson.XmlFile) File(java.io.File)

Example 17 with XmlFile

use of hudson.XmlFile in project hudson-2.x by hudson.

the class SuiteResultTest method testSuiteResultPersistence.

public void testSuiteResultPersistence() throws Exception {
    SuiteResult source = parseOne(getDataFile("junit-report-1233.xml"));
    File dest = File.createTempFile("testSuiteResultPersistence", ".xml");
    try {
        XmlFile xmlFile = new XmlFile(dest);
        xmlFile.write(source);
        SuiteResult result = (SuiteResult) xmlFile.read();
        assertNotNull(result);
        assertEquals(source.getName(), result.getName());
        assertEquals(source.getTimestamp(), result.getTimestamp());
        assertEquals(source.getDuration(), result.getDuration());
        assertEquals(source.getStderr(), result.getStderr());
        assertEquals(source.getStdout(), result.getStdout());
        assertEquals(source.getCases().size(), result.getCases().size());
        assertNotNull(result.getCase("testGetBundle"));
    } finally {
        dest.delete();
    }
}
Also used : XmlFile(hudson.XmlFile) XmlFile(hudson.XmlFile) File(java.io.File)

Example 18 with XmlFile

use of hudson.XmlFile in project hudson-2.x by hudson.

the class NodeListTest method testSerialization.

public void testSerialization() throws Exception {
    NodeList nl = new NodeList();
    nl.add(new DummyNode());
    nl.add(new EphemeralNode());
    File tmp = File.createTempFile("test", "test");
    try {
        XmlFile x = new XmlFile(Hudson.XSTREAM, tmp);
        x.write(nl);
        String xml = FileUtils.readFileToString(tmp);
        System.out.println(xml);
        assertEquals(4, xml.split("\n").length);
        NodeList back = (NodeList) x.read();
        assertEquals(1, back.size());
        assertEquals(DummyNode.class, back.get(0).getClass());
    } finally {
        tmp.delete();
    }
}
Also used : XmlFile(hudson.XmlFile) XmlFile(hudson.XmlFile) File(java.io.File)

Example 19 with XmlFile

use of hudson.XmlFile in project workflow-cps-plugin by jenkinsci.

the class ReplayActionTest method loadStep.

@Test
public void loadStep() throws Exception {
    story.addStep(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
            // Shortcut to simulate checking out an external repo with an auxiliary script.
            story.j.jenkins.getWorkspaceFor(p).child("f1.groovy").write("echo 'original first part'", null);
            story.j.jenkins.getWorkspaceFor(p).child("f2.groovy").write("echo 'original second part'", null);
            p.setDefinition(new CpsFlowDefinition("node {load 'f1.groovy'}; semaphore 'wait'; node {load 'f2.groovy'}", true));
            // Initial build loads external script and prints a message.
            SemaphoreStep.success("wait/1", null);
            WorkflowRun b1 = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
            story.j.assertLogContains("original first part", b1);
            story.j.assertLogContains("original second part", b1);
            // Editing main script to print an initial message, and editing one of the loaded scripts as well.
            SemaphoreStep.success("wait/2", null);
            WorkflowRun b2 = (WorkflowRun) b1.getAction(ReplayAction.class).run("echo 'trying edits'\nnode {load 'f1.groovy'}; semaphore 'wait'; node {load 'f2.groovy'}", Collections.singletonMap("Script2", "echo 'new second part'")).get();
            story.j.assertBuildStatusSuccess(b2);
            story.j.assertLogContains("trying edits", b2);
            story.j.assertLogContains("original first part", b2);
            story.j.assertLogContains("new second part", b2);
            // Can take a look at the build.xml and see that we are not duplicating script content once edits are applied (not yet formally asserted).
            System.out.println(new XmlFile(new File(b2.getRootDir(), "build.xml")).asString());
            // Diff should reflect both sets of changes.
            String diff = b2.getAction(ReplayAction.class).getDiff();
            assertThat(diff, containsString("+echo 'trying edits'"));
            assertThat(diff, containsString("Script2"));
            assertThat(diff, containsString("-echo 'original second part'"));
            assertThat(diff, containsString("+echo 'new second part'"));
            assertThat(diff, not(containsString("Script1")));
            assertThat(diff, not(containsString("first part")));
            System.out.println(diff);
            // Now replay #2, editing all scripts, and restarting in the middle.
            Map<String, String> replayMap = new HashMap<>();
            replayMap.put("Script1", "echo 'new first part'");
            replayMap.put("Script2", "echo 'newer second part'");
            WorkflowRun b3 = (WorkflowRun) b2.getAction(ReplayAction.class).run("node {load 'f1.groovy'}; semaphore 'wait'; node {load 'f2.groovy'}", Collections.unmodifiableMap(replayMap)).waitForStart();
            SemaphoreStep.waitForStart("wait/3", b3);
        }
    });
    story.addStep(new Statement() {

        @Override
        public void evaluate() throws Throwable {
            WorkflowJob p = story.j.jenkins.getItemByFullName("p", WorkflowJob.class);
            WorkflowRun b3 = p.getLastBuild();
            assertEquals(3, b3.getNumber());
            // Resume #3, and verify that the build completes with the expected replacements.
            SemaphoreStep.success("wait/3", null);
            story.j.waitForCompletion(b3);
            story.j.assertLogNotContains("trying edits", b3);
            story.j.assertLogContains("new first part", b3);
            story.j.assertLogContains("newer second part", b3);
        }
    });
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) XmlFile(hudson.XmlFile) Statement(org.junit.runners.model.Statement) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) XmlFile(hudson.XmlFile) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Aggregations

XmlFile (hudson.XmlFile)19 File (java.io.File)8 IOException (java.io.IOException)8 FileFilter (java.io.FileFilter)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ANTLRException (antlr.ANTLRException)1 ExtensionList (hudson.ExtensionList)1 ExtensionListView (hudson.ExtensionListView)1 ExtensionPoint (hudson.ExtensionPoint)1 RestartNotSupportedException (hudson.lifecycle.RestartNotSupportedException)1 Action (hudson.model.Action)1 FormException (hudson.model.Descriptor.FormException)1 AbstractQueueTask (hudson.model.queue.AbstractQueueTask)1 SubTask (hudson.model.queue.SubTask)1 FullControlOnceLoggedInAuthorizationStrategy (hudson.security.FullControlOnceLoggedInAuthorizationStrategy)1 LegacySecurityRealm (hudson.security.LegacySecurityRealm)1 NodeList (hudson.slaves.NodeList)1 SafeTimerTask (hudson.triggers.SafeTimerTask)1 AtomicFileWriter (hudson.util.AtomicFileWriter)1