示例#1
0
    @Override
    public Integer call() {
      int exitstatus = Integer.MAX_VALUE;
      Boolean useIdentityFile = null;
      String credentials = null;
      String userName = nodeCredentials.getUsername();

      if (nodeCredentials instanceof LoginCredentialsPassword) {
        useIdentityFile = false;
        credentials = ((LoginCredentialsPassword) nodeCredentials).getPassword();
      } else if (nodeCredentials instanceof LoginCredentialsPrivateKey) {
        useIdentityFile = true;
        credentials = ((LoginCredentialsPrivateKey) nodeCredentials).getKey().getKeyPath();
      }

      ChannelExec c = null;
      SshClient ssh = null;

      try {
        ssh = new SshClient();
        ssh.createSession(nodeAddress, userName, false);

        log.info("connecting with username: "******"connecting using identity file: " + credentials);
        } else {
          log.info("connecting using password: "******"executing command: " + command);
        c.connect();

        new Thread(
                new Runnable() {
                  public void run() {
                    String line;
                    BufferedReader bufferedInputReader =
                        new BufferedReader(new InputStreamReader(is));
                    try {
                      while ((line = bufferedInputReader.readLine()) != null) {
                        output.append(line);
                        synchronized (timeStamp) {
                          timeStamp = System.currentTimeMillis();
                        }
                      }
                    } catch (IOException e) {
                      e.printStackTrace();
                    }
                  }
                })
            .start();

        new Thread(
                new Runnable() {
                  public void run() {
                    String line;
                    BufferedReader bufferedErrorReader =
                        new BufferedReader(new InputStreamReader(err));
                    try {
                      while ((line = bufferedErrorReader.readLine()) != null) {
                        error.append(line);
                        synchronized (timeStamp) {
                          timeStamp = System.currentTimeMillis();
                        }
                      }
                    } catch (IOException e) {
                      e.printStackTrace();
                    }
                  }
                })
            .start();

        while (!c.isClosed()) {
          synchronized (this.timeStamp) {
            if (System.currentTimeMillis() - this.timeStamp > MAXIMUM_WAITING_TIME_INACTIVITY) {
              log.warn("command execution seems inactive, canceling!");
              break;
            }
          }
          log.info("waiting for command to finish.");
          try {
            Thread.sleep(SshClient.DEFAULT_WAITING_TIME_PER_CYCLE);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        exitstatus = c.getExitStatus();

      } catch (JSchException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        if (c != null) {
          c.disconnect();
        }

        if (ssh != null) {
          try {
            ssh.disconnectSession();
          } catch (JSchException e) {
            e.printStackTrace();
          }
        }
      }
      return exitstatus;
    }
  /**
   * If knife is installed and configured on the local machine this function can be used to
   * bootstrap a node.
   *
   * <p>/etc/chef/validation.pem needs to be present as well!
   *
   * @param chefNodeName
   * @param node
   * @param nodeCredentials
   */
  public String bootstrapNode(
      String chefNodeName,
      NodeMetadata node,
      LoginCredentials nodeCredentials,
      OperatingSystem operatingSystem) {

    KnifeOperatingSystem os = this.osName2knifeOs(operatingSystem.getFamily().name());

    String ret = null;

    Boolean useIdentityFile = null;
    String credentials = null;
    String userName = null;

    Boolean doSudo = true; // for the moment we assume, that sudo is
    // necessary

    String publicAddress = null;
    for (String a : node.getPublicAddresses()) {
      publicAddress = a;
    }

    if (nodeCredentials instanceof LoginCredentialsPassword) {
      useIdentityFile = false;
      credentials = ((LoginCredentialsPassword) nodeCredentials).getPassword();
    } else if (nodeCredentials instanceof LoginCredentialsPrivateKey) {
      useIdentityFile = true;
      credentials = ((LoginCredentialsPrivateKey) nodeCredentials).getKey().getKeyPath();
    }
    userName = nodeCredentials.getUsername();

    try {
      KnifeController knife;
      // KnifeCOntroller without parameters: local knife
      File remoteCreds = File.createTempFile("creds", ".tmp");
      if (this.useRemoteKnife) {
        knife = new KnifeController(this.chefHost, this.chefHostCredentials);
        if (useIdentityFile) {
          gt.redundancyrouter.utils.Utils.copyFileToHost(
              new File(credentials),
              knife.getKnifeHost(),
              knife.getKnifeHostCredentials(),
              remoteCreds);
          credentials = remoteCreds.getAbsolutePath();
        }
      }
      // KnifeController with parameters: remote knife
      else knife = new KnifeController();

      int result =
          knife.bootstrap(
              publicAddress,
              credentials,
              userName,
              doSudo,
              false,
              os,
              chefNodeName,
              useIdentityFile);

      if (useIdentityFile && this.useRemoteKnife) {
        gt.redundancyrouter.utils.Utils.deleteFileOnHost(
            knife.getKnifeHost(), knife.getKnifeHostCredentials(), remoteCreds);
      }

      if (result == 0) {
        ret = chefNodeName;
        log.info("bootstrapping successfull");
      }

    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ExecutionException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (KnifeControllerException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return ret;
  }