/** * Builds an environment. * * @param string $name Name * @param array $environment Environment * @param Project $project Project instance * * @return Environment */ public function build($name, array $environment, Project $project) { Assertion::string($name); $this->logger->debug(sprintf("Building environment '%s'", $name)); if (empty($environment)) { throw new \DomainException(sprintf("No configuration for environment '%s' given", $name)); } if (!array_key_exists('servers', $environment) || empty($environment['servers'])) { throw new \DomainException(sprintf("No servers for environment '%s' given", $name)); } if (!array_key_exists('stages', $environment) || empty($environment['stages'])) { throw new \DomainException(sprintf("No stages for environment '%s' given", $name)); } $this->tasks = $project->getTasks(); $this->logger->debug("Creating environment instance."); $options = $environment; $environment = new Environment($name); if (array_key_exists('config', $options)) { $this->logger->debug("Config section given. Hydrating config instance."); $environment->getConfig()->merge(new Config($options['config'])); } else { $this->logger->debug("No config section given."); } foreach ($options['stages'] as $name => $stage) { if (!$stage) { throw new \DomainException(sprintf("No tasks for stage '%s' in environment '%s' given", $name, $environment->getName())); } $environment->addStage($this->buildStage($name, $stage)); } $this->logger->debug(sprintf("Resolving task/server mapping.")); $environment->setMapping($this->mappingResolver->resolve($options, $environment, $project)); return $environment; }
/** * Resolves the environment configuration and maps the given servers to the given tasks, * according to the defined environment. * * @param array $environmentOptions Environment options * @param Environment $environment Environment * @param Project $project Project * * @return Mapping */ public function resolve(array $environmentOptions, Environment $environment, Project $project) { if (!array_key_exists('servers', $environmentOptions) || empty($environmentOptions['servers'])) { throw new \DomainException(sprintf("No servers for environment '%s' given", $environment->getName())); } if (!array_key_exists('stages', $environmentOptions) || empty($environmentOptions['stages'])) { throw new \DomainException(sprintf("No stages for environment '%s' given", $environment->getName())); } $this->logger->debug(sprintf("Resolving mapping for environment '%s'.", $environment->getName())); $mapping = new Mapping(); foreach ($environmentOptions['stages'] as $stageName => $stageOptions) { foreach ($stageOptions as $taskName => $taskOptions) { $stage = $environment->getStage($stageName); $task = $stage->getTask($taskName); if ($task instanceof LocalTask) { $this->logger->debug(sprintf("Local task '%s' in stage '%s' given. No servers will be added.", $taskName, $stageName)); $mapping->add($stage, $task); continue; } if (array_key_exists('servers', $taskOptions)) { foreach ($taskOptions['servers'] as $serverName) { $this->logger->debug(sprintf("Adding server '%s' to task '%s' in stage '%s'.", $serverName, $taskName, $stageName)); $mapping->add($stage, $task, $project->getServer($serverName)); } } else { foreach ($environmentOptions['servers'] as $serverName) { $this->logger->debug(sprintf("Adding server '%s' to task '%s' in stage '%s'.", $serverName, $taskName, $stageName)); $mapping->add($stage, $task, $project->getServer($serverName)); } } } } return $mapping; }
/** * Creates a new project instance. * * @param string $project Project name * * @return Project */ public function create($project) { $this->logger->debug(sprintf("Creating config for project '%s'", $project)); $config = $this->configHandler->getProjectConfig($project); $this->logger->debug(sprintf("Creating project '%s'", $project)); $project = new Project($project); $project->addServers($this->createServers($config)); $project->addTasks($this->createTasks($config)); $project->addEnvironments($this->buildEnvironments($config, $project)); return $project; }
/** * Invokes a deployment. * * @param Project $project The project * @param string $environment The environment * @param bool $dryRun IF deployment is a dry run * * @return DeploymentPayload */ public function deploy(Project $project, $environment, $dryRun) { \Assert\that($environment)->string()->notEmpty(); Assertion::boolean($dryRun); $this->logger->debug(sprintf("Deploying '%s' to '%s'", $project->getName(), $environment)); $environment = $project->getEnvironment($environment); $runner = $this->mappingConverter->convert($environment->getMapping()); $projectDirectory = $this->projectDirectoryHelper->create($project); $this->interpolator->add('PROJECT', $environment->getName()); $this->interpolator->add('ENVIRONMENT', $environment->getName()); $this->interpolator->add('PROJECT_DIRECTORY', $projectDirectory); $payload = new DeploymentPayload(); $payload->setProject($project); $payload->setEnvironment($environment); $payload->setProjectDirectory($projectDirectory); $payload->setDryRun($dryRun); $payload = $runner->run($payload); $this->projectDirectoryHelper->cleanup($project); return $payload; }
/** * Removes old project revision directories. * * @param Project $project Project instance * @param int $copies Amount of copies to keep * * @return int Amount of deleted directories */ public function cleanup(Project $project, $copies = null) { Assertion::nullOrInteger($copies); Assertion::nullOrMin($copies, 1); if (is_null($copies)) { $copies = $this->copies; } $projectDirectory = sprintf('%s/%s', $this->baseDirectory, $project->getName()); $finder = new Finder(); $finder->directories()->in($projectDirectory)->sort($this->getSortByDateAscClosure()); $directories = iterator_to_array($finder); // calculate amount of directories to delete $length = count($directories) - $copies; if ($length < 0) { $length = 0; } // extract directories to delete $directories = array_slice($directories, 0, $length); $fs = new Filesystem(); $fs->remove($directories); $removedDirectoriesCount = count($directories); $this->logger->debug(sprintf("Removing %s old copies in working directory.", $removedDirectoriesCount)); return $removedDirectoriesCount; }