Example #1
0
    VirtualChannel start(TaskListener listener, String rootPassword)
        throws IOException, InterruptedException {
      final int uid = LIBC.geteuid();

      if (uid == 0) // already running as root
      return newLocalChannel();

      String javaExe = System.getProperty("java.home") + "/bin/java";
      File slaveJar = Which.jarFile(Launcher.class);

      ArgumentListBuilder args = new ArgumentListBuilder().add(javaExe);
      if (slaveJar.isFile()) args.add("-jar").add(slaveJar);
      else // in production code this never happens, but during debugging this is convenientud
      args.add("-cp").add(slaveJar).add(hudson.remoting.Launcher.class.getName());

      if (rootPassword == null) {
        // try sudo, in the hope that the user has the permission to do so without password
        return new LocalLauncher(listener)
            .launchChannel(
                args.prepend(sudoExe()).toCommandArray(),
                listener.getLogger(),
                null,
                Collections.<String, String>emptyMap());
      } else {
        // try sudo with the given password. Also run in pfexec so that we can elevate the
        // privileges
        Process proc = sudoWithPass(args);
        return Channels.forProcess(
            args.toStringWithQuote(), Computer.threadPoolForRemoting, proc, listener.getLogger());
      }
    }
Example #2
0
  /**
   * main method
   *
   * @param args command line arguments
   */
  public static void main(final String[] args) {
    CONF_FILE_PATH = FileUtils.getConfigurationFilePath(args);

    try {
      // read configuration silently, to avoid logging before initializing the logger
      configuration = FileUtils.getConfiguration(args, true);
    } catch (ConfigurationException ex) {
      LOGGER.info("Starting " + ansi().fg(RED).bold().a("RESTHeart").reset().toString());

      if (RESTHEART_VERSION != null) {
        LOGGER.info("version {}", RESTHEART_VERSION);
      }

      LOGGER.error(ex.getMessage() + ", exiting...", ex);
      stopServer(false);
      System.exit(-1);
    }

    if (!hasForkOption(args)) {
      startServer(false);
    } else {
      if (OSChecker.isWindows()) {
        LOGGER.info("Starting " + ansi().fg(RED).bold().a("RESTHeart").reset().toString());

        if (RESTHEART_VERSION != null) {
          LOGGER.info("version {}", RESTHEART_VERSION);
        }

        LOGGER.error("Fork is not supported on Windows");

        LOGGER.info(ansi().fg(GREEN).bold().a("RESTHeart stopped").reset().toString());

        System.exit(-1);
      }

      // RHDaemon only works on POSIX OSes
      final boolean isPosix =
          FileSystems.getDefault().supportedFileAttributeViews().contains("posix");

      if (!isPosix) {
        LOGGER.info("Unable to fork process, " + "this is only supported on POSIX compliant OSes");

        stopServer(false);
        System.exit(-1);
      }

      RHDaemon d = new RHDaemon();

      if (d.isDaemonized()) {
        try {
          d.init();
          LOGGER.info("Forked process: {}", LIBC.getpid());
          initLogging(args, d);
        } catch (Throwable t) {
          LOGGER.error("Error staring forked process", t);
          stopServer(false, false);
          System.exit(-1);
        }

        startServer(true);
      } else {
        initLogging(args, d);

        try {
          LOGGER.info("Starting " + ansi().fg(RED).bold().a("RESTHeart").reset().toString());

          if (RESTHEART_VERSION != null) {
            LOGGER.info("version {}", RESTHEART_VERSION);
          }

          logLoggingConfiguration(true);

          d.daemonize();
        } catch (Throwable t) {
          LOGGER.error("Error forking", t);
          stopServer(false, false);
          System.exit(-1);
        }
      }
    }
  }