Search in sources :

Example 1 with FormException

use of hudson.model.Descriptor.FormException in project promoted-builds-plugin by jenkinsci.

the class PromotionProcess method fromJson.

/**
 * Creates unconnected {@link PromotionProcess} instance from the JSON configuration.
 * This is mostly only useful for capturing its configuration in XML format.
 * @param req Request
 * @param o JSON object with source data
 * @throws FormException form submission issue, includes form validation
 * @throws IOException {@link PromotionProcess} creation issue
 * @return Parsed promotion process
 */
public static PromotionProcess fromJson(StaplerRequest req, JSONObject o) throws FormException, IOException {
    String name = o.getString("name");
    try {
        Jenkins.checkGoodName(name);
    } catch (Failure f) {
        throw new Descriptor.FormException(f.getMessage(), name);
    }
    PromotionProcess p = new PromotionProcess(null, name);
    BulkChange bc = new BulkChange(p);
    try {
        // apply configuration. prevent it from trying to save to disk while we do this
        p.configure(req, o);
    } finally {
        bc.abort();
    }
    return p;
}
Also used : FormException(hudson.model.Descriptor.FormException) BuildStepDescriptor(hudson.tasks.BuildStepDescriptor) Descriptor(hudson.model.Descriptor) BulkChange(hudson.BulkChange) Failure(hudson.model.Failure)

Example 2 with FormException

use of hudson.model.Descriptor.FormException in project hudson-2.x by hudson.

the class Hudson method doConfigSubmit.

// 
// 
// actions
// 
// 
/**
 * Accepts submission from the configuration page.
 */
public synchronized void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, FormException {
    BulkChange bc = new BulkChange(this);
    try {
        checkPermission(ADMINISTER);
        JSONObject json = req.getSubmittedForm();
        // useSecurity = null;
        if (json.has("use_security")) {
            useSecurity = true;
            JSONObject security = json.getJSONObject("use_security");
            setSecurityRealm(SecurityRealm.all().newInstanceFromRadioList(security, "realm"));
            setAuthorizationStrategy(AuthorizationStrategy.all().newInstanceFromRadioList(security, "authorization"));
            if (security.has("markupFormatter")) {
                markupFormatter = req.bindJSON(MarkupFormatter.class, security.getJSONObject("markupFormatter"));
            } else {
                markupFormatter = null;
            }
        } else {
            useSecurity = null;
            setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
            authorizationStrategy = AuthorizationStrategy.UNSECURED;
            markupFormatter = null;
        }
        if (json.has("csrf")) {
            JSONObject csrf = json.getJSONObject("csrf");
            setCrumbIssuer(CrumbIssuer.all().newInstanceFromRadioList(csrf, "issuer"));
        } else {
            setCrumbIssuer(null);
        }
        if (json.has("viewsTabBar")) {
            viewsTabBar = req.bindJSON(ViewsTabBar.class, json.getJSONObject("viewsTabBar"));
        } else {
            viewsTabBar = new DefaultViewsTabBar();
        }
        if (json.has("myViewsTabBar")) {
            myViewsTabBar = req.bindJSON(MyViewsTabBar.class, json.getJSONObject("myViewsTabBar"));
        } else {
            myViewsTabBar = new DefaultMyViewsTabBar();
        }
        primaryView = json.has("primaryView") ? json.getString("primaryView") : getViews().iterator().next().getViewName();
        noUsageStatistics = json.has("usageStatisticsCollected") ? null : true;
        {
            String v = req.getParameter("slaveAgentPortType");
            if (!isUseSecurity() || v == null || v.equals("random")) {
                slaveAgentPort = 0;
            } else if (v.equals("disable")) {
                slaveAgentPort = -1;
            } else {
                try {
                    slaveAgentPort = Integer.parseInt(req.getParameter("slaveAgentPort"));
                } catch (NumberFormatException e) {
                    throw new FormException(Messages.Hudson_BadPortNumber(req.getParameter("slaveAgentPort")), "slaveAgentPort");
                }
            }
            // relaunch the agent
            if (tcpSlaveAgentListener == null) {
                if (slaveAgentPort != -1) {
                    tcpSlaveAgentListener = new TcpSlaveAgentListener(slaveAgentPort);
                }
            } else {
                if (tcpSlaveAgentListener.configuredPort != slaveAgentPort) {
                    tcpSlaveAgentListener.shutdown();
                    tcpSlaveAgentListener = null;
                    if (slaveAgentPort != -1) {
                        tcpSlaveAgentListener = new TcpSlaveAgentListener(slaveAgentPort);
                    }
                }
            }
        }
        numExecutors = json.getInt("numExecutors");
        if (req.hasParameter("master.mode")) {
            mode = Mode.valueOf(req.getParameter("master.mode"));
        } else {
            mode = Mode.NORMAL;
        }
        label = json.optString("labelString", "");
        quietPeriod = json.getInt("quiet_period");
        scmCheckoutRetryCount = json.getInt("retry_count");
        systemMessage = Util.nullify(req.getParameter("system_message"));
        jdks.clear();
        jdks.addAll(req.bindJSONToList(JDK.class, json.get("jdks")));
        boolean result = true;
        for (Descriptor<?> d : Functions.getSortedDescriptorsForGlobalConfig()) {
            result &= configureDescriptor(req, json, d);
        }
        for (JSONObject o : StructuredForm.toList(json, "plugin")) {
            pluginManager.getPlugin(o.getString("name")).getPlugin().configure(req, o);
        }
        clouds.rebuildHetero(req, json, Cloud.all(), "cloud");
        JSONObject np = json.getJSONObject("globalNodeProperties");
        if (np != null) {
            globalNodeProperties.rebuild(req, np, NodeProperty.for_(this));
        }
        version = VERSION;
        save();
        updateComputerList();
        if (result) {
            // go to the top page
            rsp.sendRedirect(req.getContextPath() + '/');
        } else {
            // back to config
            rsp.sendRedirect("configure");
        }
    } finally {
        bc.commit();
    }
}
Also used : DefaultMyViewsTabBar(hudson.views.DefaultMyViewsTabBar) MyViewsTabBar(hudson.views.MyViewsTabBar) BulkChange(hudson.BulkChange) DefaultViewsTabBar(hudson.views.DefaultViewsTabBar) DefaultMyViewsTabBar(hudson.views.DefaultMyViewsTabBar) ViewsTabBar(hudson.views.ViewsTabBar) MyViewsTabBar(hudson.views.MyViewsTabBar) DefaultMyViewsTabBar(hudson.views.DefaultMyViewsTabBar) FormException(hudson.model.Descriptor.FormException) TcpSlaveAgentListener(hudson.TcpSlaveAgentListener) JSONObject(net.sf.json.JSONObject) RawHtmlMarkupFormatter(hudson.markup.RawHtmlMarkupFormatter) MarkupFormatter(hudson.markup.MarkupFormatter) DefaultViewsTabBar(hudson.views.DefaultViewsTabBar)

Example 3 with FormException

use of hudson.model.Descriptor.FormException in project hudson-2.x by hudson.

the class View method create.

public static View create(StaplerRequest req, StaplerResponse rsp, ViewGroup owner) throws FormException, IOException, ServletException {
    String name = req.getParameter("name");
    checkGoodName(name);
    if (owner.getView(name) != null)
        throw new FormException(Messages.Hudson_ViewAlreadyExists(name), "name");
    String mode = req.getParameter("mode");
    if (mode == null || mode.length() == 0)
        throw new FormException(Messages.View_MissingMode(), "mode");
    // create a view
    View v = all().findByName(mode).newInstance(req, req.getSubmittedForm());
    v.owner = owner;
    // redirect to the config screen
    rsp.sendRedirect2(req.getContextPath() + '/' + v.getUrl() + v.getPostConstructLandingPage());
    return v;
}
Also used : FormException(hudson.model.Descriptor.FormException)

Example 4 with FormException

use of hudson.model.Descriptor.FormException in project hudson-2.x by hudson.

the class MatrixProject method checkAxisNames.

/**
 * Verifies that Axis names are valid and unique.
 */
private void checkAxisNames(Iterable<Axis> newAxes) throws FormException {
    HashSet<String> axisNames = new HashSet<String>();
    for (Axis a : newAxes) {
        FormValidation fv = a.getDescriptor().doCheckName(a.getName());
        if (fv.kind != Kind.OK)
            throw new FormException(Messages.MatrixProject_DuplicateAxisName(), fv, "axis.name");
        if (axisNames.contains(a.getName()))
            throw new FormException(Messages.MatrixProject_DuplicateAxisName(), "axis.name");
        axisNames.add(a.getName());
    }
}
Also used : FormException(hudson.model.Descriptor.FormException) FormValidation(hudson.util.FormValidation) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 5 with FormException

use of hudson.model.Descriptor.FormException in project vsphere-cloud-plugin by jenkinsci.

the class vSphereCloudSlaveTemplate method provision.

private vSphereCloudProvisionedSlave provision(final String cloneName, final PrintStream logger, final Map<String, String> resolvedExtraConfigParameters, final VSphere vSphere) throws VSphereException, FormException, IOException {
    final boolean POWER_ON = true;
    final boolean useCurrentSnapshot;
    final String snapshotToUse;
    if (getUseSnapshot()) {
        final String sn = getSnapshotName();
        if (sn != null && !sn.isEmpty()) {
            useCurrentSnapshot = false;
            snapshotToUse = sn;
        } else {
            useCurrentSnapshot = true;
            snapshotToUse = null;
        }
    } else {
        useCurrentSnapshot = false;
        snapshotToUse = null;
    }
    try {
        vSphere.cloneOrDeployVm(cloneName, this.masterImageName, this.linkedClone, this.resourcePool, this.cluster, this.datastore, this.folder, useCurrentSnapshot, snapshotToUse, POWER_ON, resolvedExtraConfigParameters, this.customizationSpec, logger);
        LOGGER.log(Level.FINE, "Created new VM {0} from image {1}", new Object[] { cloneName, this.masterImageName });
    } catch (VSphereDuplicateException ex) {
        final String vmJenkinsUrl = findWhichJenkinsThisVMBelongsTo(vSphere, cloneName);
        if (vmJenkinsUrl == null) {
            LOGGER.log(Level.SEVERE, "VM {0} name clashes with one we wanted to use, but it wasn't started by this plugin.", cloneName);
            throw ex;
        }
        final String ourJenkinsUrl = Jenkins.getActiveInstance().getRootUrl();
        if (vmJenkinsUrl.equals(ourJenkinsUrl)) {
            LOGGER.log(Level.INFO, "Found existing VM {0} that we started previously (and must have either lost track of it or failed to delete it).", cloneName);
        } else {
            LOGGER.log(Level.SEVERE, "VM {0} name clashes with one we wanted to use, but it doesn't belong to this Jenkins server: it belongs to {1}.  You MUST reconfigure one of these Jenkins servers to use a different naming strategy so that we no longer get clashes within vSphere host {2}. i.e. change the cloneNamePrefix on one/both to ensure uniqueness.", new Object[] { cloneName, vmJenkinsUrl, this.getParent().getVsHost() });
            throw ex;
        }
    } catch (VSphereException ex) {
        // if anything else went wrong, attempt to tidy up
        try {
            vSphere.destroyVm(cloneName, false);
        } catch (Exception logOnly) {
            LOGGER.log(Level.SEVERE, "Unable to create and power-on new VM " + cloneName + " (cloned from image " + this.masterImageName + ") and, worse, bits of the VM may still exist as the attempt to delete the remains also failed.", logOnly);
        }
        throw ex;
    }
    vSphereCloudProvisionedSlave slave = null;
    try {
        final ComputerLauncher configuredLauncher = determineLauncher(vSphere, cloneName);
        final RetentionStrategy<?> configuredStrategy = determineRetention();
        final String snapshotNameForLauncher = "";
        /* we don't make the launcher do anything with snapshots because our clone won't be created with any */
        slave = new vSphereCloudProvisionedSlave(cloneName, this.templateDescription, this.remoteFS, String.valueOf(this.numberOfExecutors), this.mode, this.labelString, configuredLauncher, configuredStrategy, this.nodeProperties, this.parent.getVsDescription(), cloneName, this.forceVMLaunch, this.waitForVMTools, snapshotNameForLauncher, String.valueOf(this.launchDelay), null, String.valueOf(this.limitedRunCount));
    } finally {
        // if anything went wrong, try to tidy up
        if (slave == null) {
            LOGGER.log(Level.FINER, "Creation of slave failed after cloning VM: destroying clone {0}", cloneName);
            vSphere.destroyVm(cloneName, false);
        }
    }
    return slave;
}
Also used : ComputerLauncher(hudson.slaves.ComputerLauncher) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) FormException(hudson.model.Descriptor.FormException) IOException(java.io.IOException) VSphereDuplicateException(org.jenkinsci.plugins.vsphere.tools.VSphereDuplicateException) VSphereDuplicateException(org.jenkinsci.plugins.vsphere.tools.VSphereDuplicateException)

Aggregations

FormException (hudson.model.Descriptor.FormException)6 BulkChange (hudson.BulkChange)2 TcpSlaveAgentListener (hudson.TcpSlaveAgentListener)1 MarkupFormatter (hudson.markup.MarkupFormatter)1 RawHtmlMarkupFormatter (hudson.markup.RawHtmlMarkupFormatter)1 Descriptor (hudson.model.Descriptor)1 Failure (hudson.model.Failure)1 ComputerLauncher (hudson.slaves.ComputerLauncher)1 BuildStepDescriptor (hudson.tasks.BuildStepDescriptor)1 FormValidation (hudson.util.FormValidation)1 DefaultMyViewsTabBar (hudson.views.DefaultMyViewsTabBar)1 DefaultViewsTabBar (hudson.views.DefaultViewsTabBar)1 MyViewsTabBar (hudson.views.MyViewsTabBar)1 ViewsTabBar (hudson.views.ViewsTabBar)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 JSONObject (net.sf.json.JSONObject)1 VSphereDuplicateException (org.jenkinsci.plugins.vsphere.tools.VSphereDuplicateException)1 VSphereException (org.jenkinsci.plugins.vsphere.tools.VSphereException)1