/**
  * @param name
  * @param conf
  * @throws IOException
  */
 public void replaceGlobalConfigurations(String name, SeleniumGlobalConfiguration conf)
     throws IOException {
   validateAdmin();
   removeGlobalConfigurations(name, false);
   configurations.add(conf);
   PluginImpl.getPlugin().save();
 }
  /**
   * @param req StaplerRequest
   * @param rsp StaplerResponse to redirect with
   * @throws IOException if redirection goes wrong
   */
  public void doCreate(StaplerRequest req, StaplerResponse rsp) throws Exception {
    validateAdmin();
    SeleniumGlobalConfiguration conf =
        req.bindJSON(SeleniumGlobalConfiguration.class, req.getSubmittedForm());
    if (null == conf.getName() || conf.getName().trim().equals("")) {
      throw new Failure("You must specify a name for the configuration");
    }

    if (PluginImpl.getPlugin().hasGlobalConfiguration(conf.getName())) {
      throw new Failure(
          "The configuration name you have chosen is already taken, please choose a unique name.");
    }

    PluginImpl.getPlugin().getGlobalConfigurations().add(conf);
    PluginImpl.getPlugin().save();
    rsp.sendRedirect2("configurations");
  }
 public List<SeleniumGlobalConfiguration> getGlobalConfigurationForComputer(Computer computer) {
   List<SeleniumGlobalConfiguration> confs = new ArrayList<SeleniumGlobalConfiguration>();
   for (SeleniumGlobalConfiguration c : PluginImpl.getPlugin().getGlobalConfigurations()) {
     if (c.getMatcher().match(computer.getNode())) {
       confs.add(c);
     }
   }
   return confs;
 }
  public static void startSeleniumNode(Computer c, TaskListener listener, String conf)
      throws IOException, InterruptedException {
    LOGGER.fine("Examining if we need to start Selenium Grid Node");

    final PluginImpl p = Hudson.getInstance().getPlugin(PluginImpl.class);

    final String exclusions = p.getExclusionPatterns();
    List<String> exclusionPatterns = new ArrayList<String>();
    if (StringUtils.hasText(exclusions)) {
      exclusionPatterns = Arrays.asList(exclusions.split(SEPARATOR));
    }
    if (exclusionPatterns.size() > 0) {
      // loop over all the labels and check if we need to exclude a node
      // based on the exlusionPatterns
      for (Label label : c.getNode().getAssignedLabels()) {
        for (String pattern : exclusionPatterns) {
          if (label.toString().matches(pattern)) {
            LOGGER.fine(
                "Node "
                    + c.getNode().getDisplayName()
                    + " is excluded from Selenium Grid because its label '"
                    + label
                    + "' matches exclusion pattern '"
                    + pattern
                    + "'");
            return;
          }
        }
      }
    }

    final String masterName = PluginImpl.getMasterHostName();
    if (masterName == null) {
      listener
          .getLogger()
          .println(
              "Unable to determine the host name of the master. Skipping Selenium execution. "
                  + "Please "
                  + HyperlinkNote.encodeTo("/configure", "configure the Jenkins URL")
                  + " from the system configuration screen.");
      return;
    }

    // make sure that Selenium Hub is started before we start RCs.
    try {
      p.waitForHubLaunch();
    } catch (ExecutionException e) {
      throw new IOException2("Failed to wait for the Hub launch to complete", e);
    }

    List<SeleniumGlobalConfiguration> confs = getPlugin().getGlobalConfigurationForComputer(c);
    if (confs == null || confs.size() == 0) {
      LOGGER.fine(
          "There is no matching configurations for that computer. Skipping selenium execution.");
      return;
    }

    String nodehost = c.getHostName();
    if (nodehost == null) {
      LOGGER.warning("Unable to determine node's hostname. Skipping");
      return;
    }

    listener
        .getLogger()
        .println(
            "Starting Selenium nodes on " + ("".equals(c.getName()) ? "(master)" : c.getName()));

    for (SeleniumGlobalConfiguration config : confs) {
      if ((conf != null && config.getName().equals(conf)) || conf == null) {
        try {
          config.start(c, listener);
        } catch (ExecutionException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
  }
 /** Starts a selenium Grid node remotely. */
 @Override
 public void onOnline(Computer c, final TaskListener listener)
     throws IOException, InterruptedException {
   PluginImpl.startSeleniumNode(c, listener, null);
 }