/** * Gets the deployment status of the requested project, and loads the * status file if not already done so * @param \GitDeployer\Objects\Project $project The project to update * @return \GitDeployer\Objects\DeploymentStatus */ public function getDeploymentStatus(\GitDeployer\Objects\Project $project) { // -> Load status file, if not done so yet $this->loadDeploymentStatuses(); // -> Check if we already have a status object // that matches our project foreach ($this->deploymentStatuses as $status) { if ($status->project() == $project->name()) { return $status; } } // -> If we have no status object yet, return an empty one $status = new \GitDeployer\Objects\DeploymentStatus(); $status->project($project->name()); return $status; }
/** * 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'); }
/** * Get a list of history items for a project from GitLab * @return array of \Git-Deployer\Objects\History */ public function getHistory(\GitDeployer\Objects\Project $project, $url = 'projects/:id/repository/commits?page=0') { if (strlen($this->privateKey) > 0) { $client = $this->createClient($this->privateKey); try { $url = str_replace(':id', $project->id(), $url); $response = $client->get($url); $historyData = json_decode($response->getBody()); $historyData = array_map(function ($h) use($project) { $history = new \GitDeployer\Objects\History(); $history->projectname($project->name())->commit($h->id)->author($h->author_name)->authormail($h->author_email)->date($h->created_at)->message($h->message); return $history; }, $historyData); $cleanLink = null; if ($response->hasHeader('link')) { // -> We have more than one page, extract the next // page link and pass it to getProjects() again $link = $response->getHeader('link')[0]; preg_match('/<.*projects\\/.*\\/repository/commits(.*)>; rel="next"/', $link, $matches); if (isset($matches[1])) { $cleanLink = 'projects/:id/repository/commits' . $matches[1]; } } return array($cleanLink, $historyData); } catch (\GuzzleHttp\Exception\ClientException $e) { if ($e->getResponse()->getStatusCode() == 401 || $e->getResponse()->getStatusCode() == 403) { return $this->interactiveLogin(); } else { throw new \Exception($e->getResponse()->getStatusCode() . ' ' . $e->getResponse()->getReasonPhrase()); } } } else { throw new \Exception('You must log-in to a service first!'); } }
/** * Get a list of tag items for a project from GitHub * @return array of \Git-Deployer\Objects\Tag */ public function getTags(\GitDeployer\Objects\Project $project, $url = 'repos/:namespace/:repo/tags') { if (strlen($this->token) > 0) { $client = $this->createClient($this->token); try { $url = str_replace(':namespace', $project->namespace(), $url); $url = str_replace(':repo', $project->name(), $url); $response = $client->get($url); $tagData = json_decode($response->getBody()); $tagData = array_map(function ($t) { $tag = new \GitDeployer\Objects\Tag(); $tag->name($t->name())->commit($h->commit->sha); return $tag; }, $tagData); if ($response->hasHeader('link')) { // -> We have more than one page, extract the next // page link and pass it to getProjects() again $link = $response->getHeader('link')[0]; preg_match('/<.*repos.*\\/tags(.*)>; rel="next"/', $link, $matches); if (isset($matches[1])) { $cleanLink = 'repos/:namespace/:repo/tags' . $matches[1]; $moreTags = $this->getTags($project, $cleanLink); if (is_array($moreTags)) { $tagData = array_merge($tagData, $moreTags); } } } return $tagData; } catch (\GuzzleHttp\Exception\ClientException $e) { if ($e->getResponse()->getStatusCode() == 401 || $e->getResponse()->getStatusCode() == 403) { return $this->interactiveLogin(); } else { throw new \Exception($e->getResponse()->getStatusCode() . ' ' . $e->getResponse()->getReasonPhrase()); } } } else { throw new \Exception('You must log-in to a service first!'); } }