Пример #1
0
  public synchronized TopLevelItem createProjectFromXML(String name, InputStream xml)
      throws IOException {
    acl.checkPermission(Job.CREATE);

    Jenkins.getInstance().getProjectNamingStrategy().checkName(name);
    // place it as config.xml
    File configXml = Items.getConfigFile(getRootDirFor(name)).getFile();
    configXml.getParentFile().mkdirs();
    try {
      IOUtils.copy(xml, configXml);

      // load it
      TopLevelItem result;
      Items.updatingByXml.set(true);
      try {
        result = (TopLevelItem) Items.load(parent, configXml.getParentFile());
      } finally {
        Items.updatingByXml.set(false);
      }
      add(result);

      ItemListener.fireOnCreated(result);
      Jenkins.getInstance().rebuildDependencyGraph();

      return result;
    } catch (IOException e) {
      // if anything fails, delete the config file to avoid further confusion
      Util.deleteRecursive(configXml.getParentFile());
      throw e;
    }
  }
Пример #2
0
    /**
     * Accepts <tt>config.xml</tt> submission, as well as serve it.
     */
    @WebMethod(name = "config.xml")
    public void doConfigDotXml(StaplerRequest req, StaplerResponse rsp)
            throws IOException {
        if (req.getMethod().equals("GET")) {
            // read
            checkPermission(EXTENDED_READ);
            rsp.setContentType("application/xml");
            IOUtils.copy(getConfigFile().getFile(),rsp.getOutputStream());
            return;
        }
        if (req.getMethod().equals("POST")) {
            // submission
            updateByXml((Source)new StreamSource(req.getReader()));
            return;
        }

        // huh?
        rsp.sendError(SC_BAD_REQUEST);
    }
  private void wmiLaunch(final EC2Computer computer, final TaskListener listener)
      throws IOException, InterruptedException, JIException {
    try {
      final PrintStream logger = listener.getLogger();
      final String name = host;

      logger.println(Messages.ManagedWindowsServiceLauncher_ConnectingTo(name));

      InetAddress host = InetAddress.getByName(name);

      try {
        Socket s = new Socket();
        s.connect(new InetSocketAddress(host, 135), 5000);
        s.close();
      } catch (IOException e) {
        logger.println(
            "Failed to connect to port 135 of "
                + name
                + ". Is Windows firewall blocking this port? Or did you disable DCOM service?");
        // again, let it continue.
      }

      JIDefaultAuthInfoImpl auth = createAuth();
      JISession session = JISession.createSession(auth);
      session.setGlobalSocketTimeout(60000);
      SWbemServices services = WMI.connect(session, name);

      String path = computer.getNode().getRemoteFS();
      if (path.indexOf(':') == -1)
        throw new IOException(
            "Remote file system root path of the slave needs to be absolute: " + path);
      SmbFile remoteRoot =
          new SmbFile(
              "smb://" + name + "/" + path.replace('\\', '/').replace(':', '$') + "/",
              createSmbAuth());

      if (!remoteRoot.exists()) remoteRoot.mkdirs();

      try { // does Java exist?
        logger.println("Checking if Java exists");
        WindowsRemoteProcessLauncher wrpl = new WindowsRemoteProcessLauncher(name, auth);
        Process proc = wrpl.launch("%JAVA_HOME%\\bin\\java -fullversion", "c:\\");
        proc.getOutputStream().close();
        IOUtils.copy(proc.getInputStream(), logger);
        proc.getInputStream().close();
        int exitCode = proc.waitFor();
        if (exitCode == 1) { // we'll get this error code if Java is not found
          logger.println("No Java found. Downloading JDK");
          JDKInstaller jdki = new JDKInstaller("jdk-6u16-oth-JPR@CDS-CDS_Developer", true);
          URL jdk = jdki.locate(listener, Platform.WINDOWS, CPU.i386);

          listener.getLogger().println("Installing JDK");
          copyStreamAndClose(
              jdk.openStream(), new SmbFile(remoteRoot, "jdk.exe").getOutputStream());

          String javaDir = path + "\\jdk"; // this is where we install Java to

          WindowsRemoteFileSystem fs = new WindowsRemoteFileSystem(name, createSmbAuth());
          fs.mkdirs(javaDir);

          jdki.install(
              new WindowsRemoteLauncher(listener, wrpl),
              Platform.WINDOWS,
              fs,
              listener,
              javaDir,
              path + "\\jdk.exe");
        }
      } catch (Exception e) {
        e.printStackTrace(listener.error("Failed to prepare Java"));
      }

      String id = generateServiceId(path);
      Win32Service slaveService = services.getService(id);
      if (slaveService == null) {
        logger.println(Messages.ManagedWindowsServiceLauncher_InstallingSlaveService());
        if (!DotNet.isInstalled(2, 0, name, auth)) {
          // abort the launch
          logger.println(Messages.ManagedWindowsServiceLauncher_DotNetRequired());
          return;
        }

        // copy exe
        logger.println(Messages.ManagedWindowsServiceLauncher_CopyingSlaveExe());
        copyStreamAndClose(
            getClass().getResource("/windows-service/jenkins.exe").openStream(),
            new SmbFile(remoteRoot, "jenkins-slave.exe").getOutputStream());

        copySlaveJar(logger, remoteRoot);

        // copy jenkins-slave.xml
        logger.println(Messages.ManagedWindowsServiceLauncher_CopyingSlaveXml());
        String nodeNameEncoded = java.net.URLEncoder.encode(nodeName, "UTF-8").replace("+", "%20");
        String xml =
            generateSlaveXml(
                id,
                "\"%JAVA_HOME%\\bin\\java\"",
                " -jnlpUrl "
                    + Hudson.getInstance().getRootUrl()
                    + "computer/"
                    + nodeNameEncoded
                    + "/slave-agent.jnlp");
        copyStreamAndClose(
            new ByteArrayInputStream(xml.getBytes("UTF-8")),
            new SmbFile(remoteRoot, "jenkins-slave.xml").getOutputStream());

        // install it as a service
        logger.println(Messages.ManagedWindowsServiceLauncher_RegisteringService());
        Document dom = new SAXReader().read(new StringReader(xml));
        Win32Service svc = services.Get("Win32_Service").cast(Win32Service.class);
        int r =
            svc.Create(
                id,
                dom.selectSingleNode("/service/name").getText() + " at " + path,
                path + "\\jenkins-slave.exe",
                Win32OwnProcess,
                0,
                "Manual",
                true);
        if (r != 0) {
          throw new JIException(-1, ("Failed to create a service: " + svc.getErrorMessage(r)));
        }
        slaveService = services.getService(id);
      } else {
        copySlaveJar(logger, remoteRoot);
      }

      logger.println(Messages.ManagedWindowsServiceLauncher_StartingService());
      slaveService.start();

      // wait until we see the port.txt, but don't do so forever
      logger.println(Messages.ManagedWindowsServiceLauncher_WaitingForService());
      SmbFile portFile = new SmbFile(remoteRoot, "port.txt");
      for (int i = 0; !portFile.exists(); i++) {
        if (i >= 30) {
          throw new JIException(-1, Messages.ManagedWindowsServiceLauncher_ServiceDidntRespond());
        }
        Thread.sleep(1000);
      }
      int p = readSmbFile(portFile);

      // connect
      logger.println(Messages.ManagedWindowsServiceLauncher_ConnectingToPort(p));
      final Socket s = new Socket(name, p);

      // ready
      computer.setChannel(
          new BufferedInputStream(new SocketInputStream(s)),
          new BufferedOutputStream(new SocketOutputStream(s)),
          listener.getLogger(),
          new Listener() {
            @Override
            public void onClosed(Channel channel, IOException cause) {
              afterDisconnect(computer, listener);
            }
          });
      // destroy session to free the socket
      JISession.destroySession(session);
    } catch (SmbException e) {
      e.printStackTrace(listener.error(e.getMessage()));
    } catch (DocumentException e) {
      e.printStackTrace(listener.error(e.getMessage()));
    }
  }
Пример #4
0
  private void generateStatusPNG(
      String branch,
      String commitSHA1,
      Job project,
      final StaplerRequest req,
      final StaplerResponse rsp)
      throws ServletException, IOException {
    SCMTriggerItem item = SCMTriggerItems.asSCMTriggerItem(project);
    GitSCM gitSCM = getGitSCM(item);

    if (gitSCM == null) {
      throw new IllegalArgumentException("This repo does not use git.");
    }

    Run mainBuild = null;

    if (branch != null) {
      mainBuild = this.getBuildByBranch(project, branch);
    } else if (commitSHA1 != null) {
      mainBuild = this.getBuildBySHA1(project, commitSHA1, false);
    }

    String baseUrl = Jenkins.getInstance().getRootUrl();
    // Remove trailing slash
    if (baseUrl.endsWith("/")) {
      baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
    }
    String imageUrl = "images/unknown.png";
    if (null != mainBuild) {
      Result res = mainBuild.getResult();
      if (mainBuild.isBuilding()) {
        imageUrl = "images/running.png";
      } else if (res == Result.SUCCESS) {
        imageUrl = "images/success.png";
      } else if (res == Result.FAILURE) {
        imageUrl = "images/failed.png";
      } else if (res == Result.UNSTABLE) {
        imageUrl = "images/unstable.png";
      } else {
        imageUrl = "images/unknown.png";
      }
    }
    Authentication old = SecurityContextHolder.getContext().getAuthentication();
    SecurityContextHolder.getContext().setAuthentication(ACL.SYSTEM);
    try {
      URL resourceUrl =
          new URL(
              Jenkins.getInstance().getPlugin("gitlab-plugin").getWrapper().baseResourceURL
                  + imageUrl);
      LOGGER.info("serving image " + resourceUrl.toExternalForm());
      rsp.setHeader("Expires", "Fri, 01 Jan 1984 00:00:00 GMT");
      rsp.setHeader("Cache-Control", "no-cache, private");
      rsp.setHeader("Content-Type", "image/png");
      hudson.util.IOUtils.copy(new File(resourceUrl.toURI()), rsp.getOutputStream());
      rsp.flushBuffer();
    } catch (Exception e) {
      throw HttpResponses.error(500, "Could not generate response.");
    } finally {
      SecurityContextHolder.getContext().setAuthentication(old);
    }
  }