Esempio n. 1
0
 public function __construct($image, $ports = [], $env = [], $volumes = [], $links = [])
 {
     $this->image = $image;
     $this->ports = $ports;
     $this->envs = $env;
     $this->volumes = $volumes;
     $this->links = $links;
     $docker = new \Docker\Docker();
     $this->containerManager = $docker->getContainerManager();
 }
Esempio n. 2
0
 public function authenticate()
 {
     $credentials = json_decode($this->account->credentials);
     try {
         $client = new Docker\Http\DockerClient(array(), $credentials->host . ':' . $credentials->port . '/containers/json');
         $docker = new Docker\Docker($client);
         $containers = $docker->getContainerManager()->findAll();
         if (empty($containers)) {
             return false;
         } else {
             return true;
         }
     } catch (Exception $ex) {
         Log::error('Failed with Authentication!' . $ex->getMessage());
         return false;
     }
 }
Esempio n. 3
0
 /**
  * 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');
 }
 /**
  * Run a docker image
  * @param  string[] $cmd The list of commands to invoke in the docker image
  */
 private function runAudfprint($cmd)
 {
     // Set up the log storage
     $logs = [];
     // Are we using audfprint directly?
     if (env('AUDFPRINT_PATH') != "") {
         // Run audfprint directly
         $command = env('AUDFPRINT_PATH') . " " . implode(" ", $cmd);
         exec($command, $output, $status_code);
         if ($status_code != 0) {
             throw new \Exception("Attempted to run audfprint directly (" . $command . "), exited with status code: " . $status_code . " - " . json_encode($output));
         }
         // Take the results and use them
         $logs = array_merge($logs, $output);
     } else {
         // We are using docker instead
         // Set up a guzzle connection with proper configuration
         // TODO: move timeouts to env setting
         $docker_client = \Docker\Http\DockerClient::createWithEnv();
         $docker_client->setDefaultOption('timeout', 3600);
         $docker_client->setDefaultOption('connect_timeout', 3600);
         // Create a connection with docker
         $docker = new \Docker\Docker($docker_client);
         // Create the docker container
         $container = new \Docker\Container(['Image' => env('DOCKER_FPRINT_IMAGE'), 'Cmd' => $cmd, 'Volumes' => ['/var/audfprint' => []], 'HostConfig' => ['Binds' => [env('FPRINT_STORE') . ':' . AudfprintFingerprinter::AUDFPRINT_DOCKER_PATH]]]);
         $manager = $docker->getContainerManager();
         // Gather the logs and return them to the caller
         $manager->run($container, function ($output, $type) use(&$logs) {
             // TODO: Process output more intelligently...
             $logs = array_merge($logs, explode("\n", $output));
         });
         // Clean up after yourself, it's only polite
         try {
             $manager->stop($container);
             $manager->remove($container, false, true);
         } catch (\Exception $e) {
             // Apparently sometimes containers don't remove, but we don't want to error when that happens.
             // TODO: figure out why container removal fails on occasion
         }
     }
     return $logs;
 }
 public function tearDown()
 {
     $docker = new \Docker\Docker();
     $containerManager = $docker->getContainerManager();
     foreach ($this->dockerContainerIds as $dockerContainerId) {
         try {
             $containerManager->kill($dockerContainerId);
         } catch (\Exception $e) {
             echo $e->getMessage() . PHP_EOL;
         }
         try {
             $containerManager->remove($dockerContainerId);
         } catch (\Exception $e) {
             echo $e->getMessage() . PHP_EOL;
         }
     }
 }
Esempio n. 6
0
 public static function export($id, $url, $port = '4243')
 {
     $client = new Docker\Http\DockerClient(array(), $url . ':' . $port . '/containers/json');
     $docker = new Docker\Docker($client);
     try {
         $container = $docker->getContainerManager()->find($id);
         $ret = $docker->getContainerManager()->export($container);
         return $ret;
     } catch (Exception $ex) {
         Log::error("Error while exporting from container " . $id);
         return array();
     }
 }