public function denormalize($data, $class, $format = null, array $context = [])
 {
     if (empty($data)) {
         return null;
     }
     if (isset($data->{'$ref'})) {
         return new Reference($data->{'$ref'}, $context['rootSchema'] ?: null);
     }
     $object = new \Docker\API\Model\RestartPolicy();
     if (!isset($context['rootSchema'])) {
         $context['rootSchema'] = $object;
     }
     if (property_exists($data, 'Name')) {
         $object->setName($data->{'Name'});
     }
     if (property_exists($data, 'MaximumRetryCount')) {
         $object->setMaximumRetryCount($data->{'MaximumRetryCount'});
     }
     return $object;
 }
 /**
  * Uses Docker to deploy the given project to a live server
  * @param  \GitDeployer\Objects\Project $project The project to deploy
  * @param  string                       $gitpath The path to the checked-out project
  * @param  array                        $config  The configuration options to pass to this deployer
  * @return mixed
  */
 public function deploy(\GitDeployer\Objects\Project $project, $gitpath, $config)
 {
     $useTunnel = false;
     // -> Connect to the docker daemon on a tcp or unix socket
     if (!isset($config['host']) || strlen($config['host']) < 1) {
         $config['host'] = getenv('DOCKER_HOST');
     }
     if (strlen($config['host']) < 1) {
         throw new \Exception('Neither the "host" parameter was specified in the .deployer file nor is the DOCKER_HOST environment variable set!');
     }
     if (stristr($config['host'], 'tcp://')) {
         // Setting the docker host to tcp:// may enable usage of the SSH tunnel functionality
         if (isset($config['ssh']) && is_array($config['ssh'])) {
             if (isset($config['ssh']['tunnel']) && $config['ssh']['tunnel'] == true) {
                 $useProc = false;
                 $useTunnel = true;
                 parent::showMessage('DOCKER', 'Connecting to Docker daemon via SSH...', $this->output);
                 // Check if the ssh binary is executable, else bail out
                 // since we can't open a tunnel without it
                 if (!$this->commandExists('ssh')) {
                     throw new \Exception('SSH client not found: Please make sure the "ssh" command is available, and in your $PATH!');
                 } else {
                     if (!isset($config['ssh']['host']) || strlen($config['ssh']['host']) < 1) {
                         throw new \Exception('Please specify at least a SSH host in your .deployerfile to connect to!');
                     }
                     if (!isset($config['ssh']['user']) || strlen($config['ssh']['user']) < 1) {
                         $config['ssh']['user'] = "******";
                     }
                     $config['ssh']['port'] = isset($config['ssh']['port']) && strlen($config['ssh']['port']) > 0 ? $config['ssh']['port'] : 22;
                     if (!isset($config['ssh']['privatekey']) || strlen($config['ssh']['privatekey']) < 1) {
                         throw new \Exception('Please correctly specify your SSH private key in the .deployerfile!');
                     }
                     // -> Open tunnel via SSH command
                     $randport = rand(60000, 65000);
                     $remotedesc = str_replace('tcp://', '', $config['host']);
                     $cmdstring = 'ssh -N -i ' . escapeshellarg($config['ssh']['privatekey']) . ' -L ' . $randport . ':' . $remotedesc . ' -p ' . $config['ssh']['port'] . ' ' . $config['ssh']['user'] . '@' . $config['ssh']['host'];
                     if (isset($config['ssh']['password']) && strlen($config['ssh']['password']) > 1) {
                         if (!extension_loaded('expect')) {
                             throw new \Exception('Expect extension not found: Please make sure the PHP expect extension is available in your PHP installation!');
                         }
                         $stream = fopen('expect://' . $cmdstring, 'r');
                         $cases = array(array('Enter passphrase', PASSWORD));
                         ini_set("expect.timeout", 30);
                         switch (expect_expectl($stream, $cases)) {
                             case PASSWORD:
                                 fwrite($stream, $config['ssh']['password'] . "\n");
                                 // Wait for tunnel port to be available
                                 while (true) {
                                     $socket = @fsockopen('127.0.0.1', $randport, $errno, $errstr, 5);
                                     if ($socket) {
                                         fclose($socket);
                                         break;
                                     }
                                 }
                                 break;
                             default:
                                 throw new \Exception('Unable to connect to the remote SSH host! Invalid string received: Expected passphrase prompt.');
                         }
                     } else {
                         $stream = proc_open('exec ' . $cmdstring, array(), $pipes);
                         $useProc = true;
                         // Wait for tunnel port to be available
                         while (true) {
                             $socket = @fsockopen('127.0.0.1', $randport, $errno, $errstr, 5);
                             if ($socket) {
                                 fclose($socket);
                                 break;
                             }
                         }
                     }
                 }
             }
         }
     }
     $client = new \Docker\DockerClient(array('remote_socket' => 'tcp://127.0.0.1:' . $randport, 'ssl' => isset($config['ssl']) && $config['ssl'] == true ? true : false));
     $docker = new \Docker\Docker($client);
     // -> Build the docker image if a Dockerfile is present
     if (!file_exists($gitpath . '/Dockerfile')) {
         throw new \Exception('No Dockerfile found - aborting build!');
     }
     parent::showMessage('DOCKER', 'Building image (no-cache)...', $this->output);
     parent::showMessage('DOCKER', 'Uploading context...', $this->output);
     $context = new \Docker\Context\Context($gitpath);
     $imageManager = $docker->getImageManager();
     $buildStream = $imageManager->build($context->toStream(), array('t' => 'git-deployer/' . $project->name()), \Docker\Manager\ContainerManager::FETCH_STREAM);
     $buildStream->onFrame(function (\Docker\API\Model\BuildInfo $buildInfo) {
         parent::showMessage('BUILD', $buildInfo->getStream(), $this->output);
     });
     $buildStream->wait();
     // -> Stop and remove the old container with the same name, sicne we're going
     // to replace the app here with the newly built container
     parent::showMessage('DOCKER', 'Getting running containers...', $this->output);
     $containersOnHost = $docker->getContainerManager()->findAll();
     if (count($containersOnHost) > 0) {
         // We check for a container with the same name as the one we are going to deploy
         foreach ($containersOnHost as $key => $container) {
             $containerFound = false;
             // Search by name
             foreach ($container->getNames() as $name) {
                 $cleanName = $this->cleanName($project->name());
                 preg_match('#\\/.*\\/(.*)#', $name, $matches);
                 if ($cleanName == $matches[1]) {
                     $containerFound = true;
                 }
             }
             // Search by image
             if ($container->getImage() == 'git-deployer/' . $project->name()) {
                 $containerFound = true;
             }
             if ($containerFound) {
                 parent::showMessage('DOCKER', 'Stopping old container ' . $container->getId() . '...', $this->output);
                 $docker->getContainerManager()->stop($container->getId());
                 $docker->getContainerManager()->remove($container->getId());
             }
         }
     }
     // -> Start the container up if we have built sucessfully
     parent::showMessage('DOCKER', 'Starting new container...', $this->output);
     $hostConfig = new \Docker\API\Model\HostConfig();
     $containerConfig = new \Docker\API\Model\ContainerConfig();
     $containerConfig->setNames(array('git-deployer/' . $this->cleanName($project->name())));
     $containerConfig->setImage('git-deployer/' . $project->name());
     // Add environment from the config file, if any
     $envArray = array();
     if (isset($config['environment'])) {
         foreach ($config['environment'] as $key => $value) {
             $envArray[] = $key . '=' . $value;
         }
     }
     $containerConfig->setEnv($envArray);
     // Add exposed ports from the config file, if any
     if (isset($config['ports']) && is_array($config['ports']) && count($config['ports']) > 0) {
         $exposedPorts = new \ArrayObject();
         $mapPorts = new \ArrayObject();
         foreach ($config['ports'] as $portdesc) {
             $portspec = $this->parsePortSpecification($portdesc);
             // Exposed port
             $exposedPort = $portspec['port'] . (strlen($portspec['protocol']) > 0 ? '/' . $portspec['protocol'] : '/tcp');
             $exposedPorts[$exposedPort] = new \stdClass();
             // Host port binding
             $hostPortBinding = new \Docker\API\Model\PortBinding();
             $mapPorts[$exposedPort] = array($hostPortBinding);
         }
         $containerConfig->setExposedPorts($exposedPorts);
         $hostConfig->setPortBindings($mapPorts);
     }
     // Add restart policy
     if (isset($config['restart']) && strlen($config['restart']) > 0) {
         $policy = $this->parseRestartPolicy($config['restart']);
         $restartPolicy = new \Docker\API\Model\RestartPolicy();
         $restartPolicy->setName($policy['Name']);
         if (isset($policy['MaximumRetryCount'])) {
             $restartPolicy->setMaximumRetryCount($policy['MaximumRetryCount']);
         }
         $hostConfig->setRestartPolicy($restartPolicy);
     }
     // Add binds
     if (isset($config['volumes']) && is_array($config['volumes']) && count($config['volumes']) > 0) {
         $binds = new \ArrayObject();
         foreach ($config['volumes'] as $volume) {
             $binds[] = $volume;
         }
         $hostConfig->setBinds($binds);
     }
     $containerConfig->setHostConfig($hostConfig);
     $containerCreateResult = $docker->getContainerManager()->create($containerConfig, array('name' => $this->cleanName($project->name())));
     if ($containerCreateResult->getId()) {
         $docker->getContainerManager()->start($containerCreateResult->getId());
     }
     // -> Clean up and close the SSH tunnel
     if ($useTunnel) {
         if ($useProc) {
             proc_terminate($stream, 9);
             proc_close($stream);
         } else {
             fclose($stream);
         }
     }
     return array(true, 'No trace');
 }