Search in sources :

Example 11 with BulkChange

use of hudson.BulkChange in project jenkins by jenkinsci.

the class Jenkins method doConfigSubmit.

// 
// 
// actions
// 
// 
/**
 * Accepts submission from the configuration page.
 */
@POST
public synchronized void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, FormException {
    try (BulkChange bc = new BulkChange(this)) {
        checkPermission(MANAGE);
        JSONObject json = req.getSubmittedForm();
        systemMessage = Util.nullify(req.getParameter("system_message"));
        boolean result = true;
        for (Descriptor<?> d : Functions.getSortedDescriptorsForGlobalConfigUnclassified()) result &= configureDescriptor(req, json, d);
        save();
        updateComputerList();
        if (result)
            FormApply.success(req.getContextPath() + '/').generateResponse(req, rsp, null);
        else
            // back to config
            FormApply.success("configure").generateResponse(req, rsp, null);
        bc.commit();
    }
}
Also used : JSONObject(net.sf.json.JSONObject) BulkChange(hudson.BulkChange) POST(org.kohsuke.stapler.verb.POST) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST)

Example 12 with BulkChange

use of hudson.BulkChange in project jenkins by jenkinsci.

the class SetupWizard method init.

/**
 * Initialize the setup wizard, this will process any current state initializations
 */
/*package*/
void init(boolean newInstall) throws IOException, InterruptedException {
    Jenkins jenkins = Jenkins.get();
    if (newInstall) {
        // Create an admin user by default with a difficult password
        FilePath iapf = getInitialAdminPasswordFile();
        if (jenkins.getSecurityRealm() == null || jenkins.getSecurityRealm() == SecurityRealm.NO_AUTHENTICATION) {
            // this seems very fragile
            try (BulkChange bc = new BulkChange(jenkins)) {
                HudsonPrivateSecurityRealm securityRealm = new HudsonPrivateSecurityRealm(false, false, null);
                jenkins.setSecurityRealm(securityRealm);
                String randomUUID = UUID.randomUUID().toString().replace("-", "").toLowerCase(Locale.ENGLISH);
                // create an admin user
                User initialAdmin = securityRealm.createAccount(SetupWizard.initialSetupAdminUserName, randomUUID);
                if (ADMIN_INITIAL_API_TOKEN != null) {
                    createInitialApiToken(initialAdmin);
                }
                // JENKINS-33599 - write to a file in the jenkins home directory
                // most native packages of Jenkins creates a machine user account 'jenkins' to run Jenkins,
                // and use group 'jenkins' for admins. So we allow groups to read this file
                iapf.touch(System.currentTimeMillis());
                iapf.chmod(0640);
                iapf.write(randomUUID + System.lineSeparator(), "UTF-8");
                // Lock Jenkins down:
                FullControlOnceLoggedInAuthorizationStrategy authStrategy = new FullControlOnceLoggedInAuthorizationStrategy();
                authStrategy.setAllowAnonymousRead(false);
                jenkins.setAuthorizationStrategy(authStrategy);
                // Disable jnlp by default, but honor system properties
                jenkins.setSlaveAgentPort(SystemProperties.getInteger(Jenkins.class.getName() + ".slaveAgentPort", -1));
                // require a crumb issuer
                jenkins.setCrumbIssuer(GlobalCrumbIssuerConfiguration.createDefaultCrumbIssuer());
                // TODO could probably be removed since some of the above setters already call save
                jenkins.save();
                bc.commit();
            }
        }
        if (iapf.exists()) {
            String setupKey = iapf.readToString().trim();
            String ls = System.lineSeparator();
            LOGGER.info(ls + ls + "*************************************************************" + ls + "*************************************************************" + ls + "*************************************************************" + ls + ls + "Jenkins initial setup is required. An admin user has been created and " + "a password generated." + ls + "Please use the following password to proceed to installation:" + ls + ls + setupKey + ls + ls + "This may also be found at: " + iapf.getRemote() + ls + ls + "*************************************************************" + ls + "*************************************************************" + ls + "*************************************************************" + ls);
        }
    }
    try {
        // Make sure plugin metadata is up to date
        UpdateCenter.updateDefaultSite();
    } catch (RuntimeException e) {
        LOGGER.log(Level.WARNING, e.getMessage(), e);
    }
}
Also used : Jenkins(jenkins.model.Jenkins) FilePath(hudson.FilePath) FullControlOnceLoggedInAuthorizationStrategy(hudson.security.FullControlOnceLoggedInAuthorizationStrategy) User(hudson.model.User) BulkChange(hudson.BulkChange) FileUtils.readFileToString(org.apache.commons.io.FileUtils.readFileToString) HudsonPrivateSecurityRealm(hudson.security.HudsonPrivateSecurityRealm)

Example 13 with BulkChange

use of hudson.BulkChange in project jenkins by jenkinsci.

the class Run method doConfigSubmit.

@POST
@NonNull
public HttpResponse doConfigSubmit(StaplerRequest req) throws IOException, ServletException, FormException {
    checkPermission(UPDATE);
    try (BulkChange bc = new BulkChange(this)) {
        JSONObject json = req.getSubmittedForm();
        submit(json);
        bc.commit();
    }
    return FormApply.success(".");
}
Also used : JSONObject(net.sf.json.JSONObject) BulkChange(hudson.BulkChange) POST(org.kohsuke.stapler.verb.POST) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 14 with BulkChange

use of hudson.BulkChange in project jenkins by jenkinsci.

the class Job method doConfigSubmit.

// 
// 
// actions
// 
// 
/**
 * Accepts submission from the configuration page.
 */
@POST
public synchronized void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, FormException {
    checkPermission(CONFIGURE);
    description = req.getParameter("description");
    JSONObject json = req.getSubmittedForm();
    try {
        try (BulkChange bc = new BulkChange(this)) {
            setDisplayName(json.optString("displayNameOrNull"));
            logRotator = null;
            DescribableList<JobProperty<?>, JobPropertyDescriptor> t = new DescribableList<>(NOOP, getAllProperties());
            JSONObject jsonProperties = json.optJSONObject("properties");
            if (jsonProperties != null) {
                t.rebuild(req, jsonProperties, JobPropertyDescriptor.getPropertyDescriptors(Job.this.getClass()));
            } else {
                t.clear();
            }
            properties.clear();
            for (JobProperty p : t) {
                p.setOwner(this);
                properties.add(p);
            }
            submit(req, rsp);
            bc.commit();
        }
        ItemListener.fireOnUpdated(this);
        final ProjectNamingStrategy namingStrategy = Jenkins.get().getProjectNamingStrategy();
        if (namingStrategy.isForceExistingJobs()) {
            namingStrategy.checkName(name);
        }
        FormApply.success(".").generateResponse(req, rsp, null);
    } catch (JSONException e) {
        LOGGER.log(Level.WARNING, "failed to parse " + json, e);
        sendError(e, req, rsp);
    }
}
Also used : JSONObject(net.sf.json.JSONObject) DescribableList(hudson.util.DescribableList) BulkChange(hudson.BulkChange) JSONException(net.sf.json.JSONException) ProjectNamingStrategy(jenkins.model.ProjectNamingStrategy) POST(org.kohsuke.stapler.verb.POST) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST)

Example 15 with BulkChange

use of hudson.BulkChange in project jenkins by jenkinsci.

the class Job method setBuildDiscarder.

public synchronized void setBuildDiscarder(BuildDiscarder bd) throws IOException {
    try (BulkChange bc = new BulkChange(this)) {
        removeProperty(BuildDiscarderProperty.class);
        if (bd != null) {
            addProperty(new BuildDiscarderProperty(bd));
        }
        bc.commit();
    }
}
Also used : BulkChange(hudson.BulkChange) BuildDiscarderProperty(jenkins.model.BuildDiscarderProperty)

Aggregations

BulkChange (hudson.BulkChange)38 IOException (java.io.IOException)11 JSONObject (net.sf.json.JSONObject)7 POST (org.kohsuke.stapler.verb.POST)7 RequirePOST (org.kohsuke.stapler.interceptor.RequirePOST)5 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 XStreamException (com.thoughtworks.xstream.XStreamException)2 NonNull (edu.umd.cs.findbugs.annotations.NonNull)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 Action (hudson.model.Action)2 Descriptor (hudson.model.Descriptor)2 FormException (hudson.model.Descriptor.FormException)2 FreeStyleBuild (hudson.model.FreeStyleBuild)2 FreeStyleProject (hudson.model.FreeStyleProject)2 Saveable (hudson.model.Saveable)2 NodeMonitor (hudson.node_monitors.NodeMonitor)2 List (java.util.List)2 ObjectMetadataAction (jenkins.scm.api.metadata.ObjectMetadataAction)2