Exemplo n.º 1
0
 /**
  * Maps a given server to a task.
  *
  * @param Stage        $stage  Stage instance
  * @param AbstractTask $task   Task instance
  * @param Server       $server Server instance
  *
  * @return null
  */
 private function addServer(Stage $stage, AbstractTask $task, Server $server)
 {
     $stageName = $stage->getName();
     $taskName = $task->getName();
     if (!array_key_exists('servers', $this->mapping[$stageName]['tasks'][$taskName])) {
         $this->mapping[$stageName]['tasks'][$taskName]['servers'] = array();
     }
     foreach ($this->mapping[$stageName]['tasks'][$taskName]['servers'] as $serverAdded) {
         if ($serverAdded === $server) {
             throw new \LogicException(sprintf("Can't add server '%s' to task '%s' twice.", $server->getName(), $task->getName()));
         }
     }
     $this->mapping[$stageName]['tasks'][$taskName]['servers'][] = $server;
 }
Exemplo n.º 2
0
 public function testToArrayRemovesSshSession()
 {
     $server = Server::create(array('name' => 'appServer1.foo.bar', 'host' => 'foo.bar', 'port' => 22, 'username' => 'root', 'password' => '12341234'));
     /** @var Session $session */
     $session = $this->getMockBuilder('Ssh\\Session')->disableOriginalConstructor()->getMock();
     $server->setSession($session);
     $this->assertArrayNotHasKey('session', $server->toArray());
 }
Exemplo n.º 3
0
 /**
  * Creates a server instance.
  *
  * @param array $server Server
  *
  * @return Server
  */
 public function create(array $server)
 {
     $server = array_filter($server, function ($value) {
         return !is_null($value) || !empty($value);
     });
     if (!array_key_exists('host', $server)) {
         throw new \DomainException('Host is missing for passed server.');
     }
     if (!array_key_exists('name', $server)) {
         throw new \DomainException('Name is missing for passed server.');
     }
     if (!array_key_exists('password', $server) && !array_key_exists('privateKey', $server)) {
         throw new \DomainException(sprintf("No password and no private key file given for server '%s'.", $server['name']));
     }
     if (array_key_exists('password', $server) && array_key_exists('privateKey', $server)) {
         throw new \DomainException(sprintf("Both password and private key file given for server '%s'. Choose only one.", $server['name']));
     }
     if (array_key_exists('password', $server) && !array_key_exists('username', $server)) {
         throw new \DomainException(sprintf("Password given but username is missing for server '%s'.", $server['name']));
     }
     if (array_key_exists('privateKey', $server) && !array_key_exists('username', $server)) {
         throw new \DomainException(sprintf("Private key file given but username missing for server '%s'.", $server['name']));
     }
     if (array_key_exists('privateKey', $server) && !array_key_exists('publicKey', $server)) {
         throw new \DomainException(sprintf("Private key file given but public key file missing for server '%s'.", $server['name']));
     }
     if (array_key_exists('publicKey', $server) && !array_key_exists('privateKey', $server)) {
         throw new \DomainException(sprintf("Public key file given but private key file missing for server '%s'.", $server['name']));
     }
     if (!array_key_exists('tags', $server) || empty($server['tags'])) {
         $server['tags'] = array('all');
     }
     if (!array_key_exists('port', $server) || empty($server['port'])) {
         $server['port'] = 22;
     }
     $server = Server::create($server);
     $session = $this->sessionFactory->createFromServer($server);
     $server->setSession($session);
     return $server;
 }
Exemplo n.º 4
0
 /**
  * Creates a ssh session for a given server.
  *
  * @param Server $server
  *
  * @return Session
  */
 public function createFromServer(Server $server)
 {
     $config = new Configuration($server->getHost(), $server->getPort());
     switch (true) {
         case $server->getUsername() && $server->getPassword():
             $auth = new Password($server->getUsername(), $server->getPassword());
             break;
         case $server->getPrivateKey():
             $auth = new PublicKeyFile($server->getUsername(), $server->getPublicKey(), $server->getPrivateKey(), $server->getKeyPhrase());
             break;
         default:
             throw new \DomainException(sprintf('No authentication credentials given for server: %s', $server->getName()));
             break;
     }
     $session = new Session($config, $auth);
     return $session;
 }