[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/drydock/interface/command/ -> DrydockSSHCommandInterface.php (source)

   1  <?php
   2  
   3  final class DrydockSSHCommandInterface extends DrydockCommandInterface {
   4  
   5    private $passphraseSSHKey;
   6    private $connectTimeout;
   7  
   8    private function openCredentialsIfNotOpen() {
   9      if ($this->passphraseSSHKey !== null) {
  10        return;
  11      }
  12  
  13      $credential = id(new PassphraseCredentialQuery())
  14        ->setViewer(PhabricatorUser::getOmnipotentUser())
  15        ->withIDs(array($this->getConfig('credential')))
  16        ->needSecrets(true)
  17        ->executeOne();
  18  
  19      if ($credential->getProvidesType() !==
  20        PassphraseCredentialTypeSSHPrivateKey::PROVIDES_TYPE) {
  21        throw new Exception('Only private key credentials are supported.');
  22      }
  23  
  24      $this->passphraseSSHKey = PassphraseSSHKey::loadFromPHID(
  25        $credential->getPHID(),
  26        PhabricatorUser::getOmnipotentUser());
  27    }
  28  
  29    public function setConnectTimeout($timeout) {
  30      $this->connectTimeout = $timeout;
  31      return $this;
  32    }
  33  
  34    public function getExecFuture($command) {
  35      $this->openCredentialsIfNotOpen();
  36  
  37      $argv = func_get_args();
  38  
  39      if ($this->getConfig('platform') === 'windows') {
  40        // Handle Windows by executing the command under PowerShell.
  41        $command = id(new PhutilCommandString($argv))
  42          ->setEscapingMode(PhutilCommandString::MODE_POWERSHELL);
  43  
  44        $change_directory = '';
  45        if ($this->getWorkingDirectory() !== null) {
  46          $change_directory .= 'cd '.$this->getWorkingDirectory();
  47        }
  48  
  49        $script = <<<EOF
  50  $change_directory
  51  $command
  52  if (\$LastExitCode -ne 0) {
  53    exit \$LastExitCode
  54  }
  55  EOF;
  56  
  57        // When Microsoft says "Unicode" they don't mean UTF-8.
  58        $script = mb_convert_encoding($script, 'UTF-16LE');
  59  
  60        $script = base64_encode($script);
  61  
  62        $powershell =
  63          'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe';
  64        $powershell .=
  65          ' -ExecutionPolicy Bypass'.
  66          ' -NonInteractive'.
  67          ' -InputFormat Text'.
  68          ' -OutputFormat Text'.
  69          ' -EncodedCommand '.$script;
  70  
  71        $full_command = $powershell;
  72      } else {
  73        // Handle UNIX by executing under the native shell.
  74        $argv = $this->applyWorkingDirectoryToArgv($argv);
  75  
  76        $full_command = call_user_func_array('csprintf', $argv);
  77      }
  78  
  79      $command_timeout = '';
  80      if ($this->connectTimeout !== null) {
  81        $command_timeout = csprintf(
  82          '-o %s',
  83          'ConnectTimeout='.$this->connectTimeout);
  84      }
  85  
  86      return new ExecFuture(
  87        'ssh '.
  88        '-o LogLevel=quiet '.
  89        '-o StrictHostKeyChecking=no '.
  90        '-o UserKnownHostsFile=/dev/null '.
  91        '-o BatchMode=yes '.
  92        '%C -p %s -i %P %P@%s -- %s',
  93        $command_timeout,
  94        $this->getConfig('port'),
  95        $this->passphraseSSHKey->getKeyfileEnvelope(),
  96        $this->passphraseSSHKey->getUsernameEnvelope(),
  97        $this->getConfig('host'),
  98        $full_command);
  99    }
 100  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1