protected function execute(InputInterface $input, OutputInterface $output) { $service = $input->getArgument('service'); // -> We check if there is already an app instance, and use that // if it exists to log-in or pre-populate data $existingService = false; try { $instance = \GitDeployer\AppInstance::getInstance(); $appService = $instance->service(); $existingService = true; } catch (\Exception $e) { $instance = new \GitDeployer\AppInstance(); // -> We first create a new instance of the service, and let it // configure itself (it may ask questions, etc...) $appService = \GitDeployer\Services\BaseService::createServiceInstance($service, $input, $output, $this->getHelperSet()); $existingService = false; } if ($existingService) { // -> We need to throw an error if we're logging in to another // service than the currently logged in! preg_match('#.*\\.*\\\\(.*)Service#', get_class($appService), $matches); if ($matches[1] !== $service) { throw new \Exception('You are already logged in to ' . $matches[1] . "!\nTo change to another service, please logout first."); } $appService->setInstances($input, $output, $this->getHelperSet()); } $appService->login(); // -> Once the configuration goes through (no exception thrown), we // can save the service to the AppInstance, and save it to the file system $instance->service($appService)->save(); $output->writeln('You have successfully logged in to <info>' . $service . '</info>'); $output->writeln('Don\'t forget to configure your Git-Deployer instance by executing the <info>config</info> command!'); }
protected function execute(InputInterface $input, OutputInterface $output) { $repository = $input->getArgument('repository'); // -> Get logged-in service $instance = \GitDeployer\AppInstance::getInstance(); $appService = $instance->service(); $appService->setInstances($input, $output, $this->getHelperSet()); // .. and storage $storage = $instance->storage(); // -> Get the current projects, and check if // the project exists and has not yet been added to // Git-Deployer $output->writeln('Adding new project <info>"' . $repository . '"</info>...'); $hasBeenFound = false; foreach ($appService->getProjects() as $key => $project) { if ($project->name() == $repository) { // -> Once we have our project, add it to the deployer projects // This will fail if the project was already added $storage->addNewDeploymentStatus($project); $output->writeln('The project <info>"' . $repository . '"</info> was successfully added to Git-Deployer!'); $hasBeenFound = true; break; } } if (!$hasBeenFound) { throw new \Exception('Project "' . $repository . '" could not be found! Please check your spelling!'); } }
protected function execute(InputInterface $input, OutputInterface $output) { $repository = $input->getArgument('repository'); // -> Get logged-in service $instance = \GitDeployer\AppInstance::getInstance(); $appService = $instance->service(); $appService->setInstances($input, $output, $this->getHelperSet()); // -> Get currently known repositories $hasBeenFound = false; foreach ($appService->getProjects() as $key => $project) { if ($project->name() == $repository) { list($link, $history) = $appService->getHistory($project); // History information $output->writeln('<comment>History information about project</comment> <info>"' . $repository . '"</info>:' . "\n"); $this->printHistory($history, $output); $isRunning = true; while (strlen($link) > 0 && $isRunning) { $helper = $this->getHelper('question'); $question = new ConfirmationQuestion('Continue to next page? (y/n)', false); if (!$helper->ask($input, $output, $question)) { $isRunning = false; } else { list($link, $history) = $appService->getHistory($project); $this->printHistory($history, $output); } } $hasBeenFound = true; break; } } if (!$hasBeenFound) { throw new \Exception('Project "' . $repository . '" could not be found! Please check your spelling!'); } }
protected function execute(InputInterface $input, OutputInterface $output) { // -> We check if there is already an app instance, and use that // if it exists to log-in or pre-populate data try { $instance = \GitDeployer\AppInstance::getInstance(); $instance->delete(); } catch (\Exception $e) { // No app instance? We're done here. } $output->writeln('You have successfully logged out!'); }
protected function execute(InputInterface $input, OutputInterface $output) { $repository = $input->getArgument('repository'); // -> Get logged-in service $instance = \GitDeployer\AppInstance::getInstance(); $appService = $instance->service(); $appService->setInstances($input, $output, $this->getHelperSet()); // .. and storage $storage = $instance->storage(); // -> Get the current projects, and check if // the project exists and has not yet been added to // Git-Deployer $output->writeln('Removing project <info>"' . $repository . '"</info>...'); // -> Once we have our project, add it to the deployer projects // This will fail if the project does not exist $storage->removeDeploymentStatusForProject($repository); $output->writeln('The project <info>"' . $repository . '"</info> was successfully removed from Git-Deployer!'); }
protected function execute(InputInterface $input, OutputInterface $output) { // -> Get logged-in service $instance = \GitDeployer\AppInstance::getInstance(); $appService = $instance->service(); $appService->setInstances($input, $output, $this->getHelperSet()); // .. and storage $storage = $instance->storage(); if ($storage == null) { throw new \RuntimeException('Please configure Git-Deployer first!'); } $projects = $appService->getProjects(); // -> Print out status table $table = new Table($output); $table->setHeaders(array('ID', 'Name', 'Version', 'Status')); foreach ($projects as $key => $project) { $status = $storage->getDeploymentStatus($project); $table->addRow(array($project->id(), $project->name(), $status->added() ? $status->getDeployedVersion() : 'N/A', $status->added() ? $status->getDeploymentInfo() : 'Not added to Git-Deployer yet')); } $table->render(); }
protected function execute(InputInterface $input, OutputInterface $output) { // -> We check if there is already an app instance, else we // can not configure git-deployer yet $hasStorage = false; try { // -> We need to throw an error if we're loggin in to another // service than the currently logged in! $instance = \GitDeployer\AppInstance::getInstance(); $storageService = $instance->storage(); if (is_object($storageService) && $storageService instanceof \GitDeployer\Storage\BaseStorage) { $hasStorage = str_replace(array('GitDeployer\\Storage\\', 'Storage'), array('', ''), get_class($storageService)); } } catch (\Exception $e) { throw new \RuntimeException('Please log-in to a service first!'); } $output->writeln(<<<INTRO Welcome to the configuration wizard for Git-Deployer! Let's get started! INTRO ); // -> Storage service // Get available storage services to display to the user $services = \GitDeployer\Storage\BaseStorage::getStorageServicesForHelp(); $output->writeln(<<<INTRO First, you will need to configure a <info>storage service</info>: A <info>storage service</info> is responsible of storing Git-Deployer's state, i such as what repositories are deployed, which version/tag was deployed, where they are deployed, etc... The following <info>storage services</info> exist in this build: <comment>{$services}</comment> INTRO ); $services = \GitDeployer\Storage\BaseStorage::getStorageServicesForIterating(); $default = $hasStorage ? array_search($hasStorage, $services) : 0; $helper = $this->getHelper('question'); // -> Get storage service to use $question = new ChoiceQuestion('Which storage service would you like to use?', $services, $default); $question->setValidator(function ($answer) use($services) { if (!isset($services[$answer])) { throw new \RuntimeException('Please select a correct value!'); } return $answer; }); $number = $helper->ask($input, $output, $question); // If we selected the already existing storage instance, load // it up so we can see the already defined values if ($hasStorage && $number == $default) { $storageService->setInstances($input, $output, $this->getHelperSet()); } else { $storageService = \GitDeployer\Storage\BaseStorage::createServiceInstance($services[$number], $input, $output, $this->getHelperSet()); } // -> Now that we have a storage service, add this to // the AppInstance, to be opened later again if ($storageService->configure()) { $instance->storage($storageService)->save(); } $output->writeln(<<<OUTRO This finishes the configuration of Git-Deployer! Thank you! OUTRO ); }
protected function execute(InputInterface $input, OutputInterface $output) { $repository = $input->getArgument('repository'); $revision = $input->getArgument('revision'); $configuration = $input->getOption('configuration'); $bag = $input->getOption('bag'); $force = $input->getOption('force-redeploy'); // -> Check that the revision matches what we expect preg_match('#^(tag|branch|rev):(.*)#iu', $revision, $matches); if (count($matches) < 3) { throw new \Exception('You must correctly specify the tag/branch/revision you want to deploy!' . "\n" . 'Check "help deploy" for an explanation.'); } else { $type = $matches[1]; $version = $matches[2]; } // -> Get logged-in service $instance = \GitDeployer\AppInstance::getInstance(); $appService = $instance->service(); $appService->setInstances($input, $output, $this->getHelperSet()); // .. and storage $storage = $instance->storage(); $hasBeenFound = false; $this->showMessage('INIT', 'Getting repository...', $output); foreach ($appService->getProjects() as $key => $project) { if ($project->name() == $repository) { $hasBeenFound = true; $status = $storage->getDeploymentStatus($project); // -> Check if we have already been added to Git-Deployer yet if (!$status->added()) { throw new \Exception('This repository is not yet added to Git-Deployer!' . "\n" . 'Please add it first via the add command.'); } // -> Check we are not deploying the same version again. Ask // if we do, so the user can decide if ($status->isDeployd()) { if ($status->getDeployedVersion() == $revision && !$force) { $helper = $this->getHelper('question'); $question = new ConfirmationQuestion('Version ' . $status->getDeployedVersion() . ' was already deployed on ' . $status->deployedWhen() . '. Continue anyway? (y/[n]) ', false); if (!$helper->ask($input, $output, $question)) { break; } } } // -> Now clone it to a temp directory, if it doesn't exist already $tmpDir = sys_get_temp_dir() . '/git-deploy-' . strtolower($project->name()); if (\Gitonomy\Git\Admin::isValidRepository($tmpDir)) { $this->showMessage('GIT', 'Pulling latest changes from repository...', $output); $repository = \Gitonomy\Git\Admin::init($tmpDir, false); $repository->run('pull'); } else { $this->showMessage('GIT', 'Cloning repository...', $output); $repository = \Gitonomy\Git\Admin::cloneTo($tmpDir, $project->url(), false); } // -> Check out the correct branch/revision/tag $this->showMessage('GIT', 'Checking out ' . $revision . '...', $output); $wc = $repository->getWorkingCopy(); $wc->checkout($version); // -> Open .deployerfile and parse it $this->showMessage('DEPLOY', 'Checking .deployerfile...', $output); if (file_exists($tmpDir . '/.deployerfile')) { $deployerfile = json_decode(file_get_contents($tmpDir . '/.deployerfile')); } else { throw new \Exception('This repository has no .deployerfile!' . "\n" . 'Please add one first!'); } if ($deployerfile == null) { throw new \Exception('Could not parse .deployerfile: ' . json_last_error_msg() . "\n" . 'Please check that your JSON is valid!'); } // -> Load the correct deployer from the .deployerfile, and fire it up with // the correct configuration options if (!isset($deployerfile->type)) { throw new \Exception('Could not parse .deployerfile: ' . "\n" . 'Please specify a deployer via the "type" option!'); } if (!isset($deployerfile->configurations) || isset($deployerfile->configurations) && !is_object($deployerfile->configurations)) { throw new \Exception('Could not parse .deployerfile: ' . "\n" . 'Please specify at least one deployment configuration in the "configurations" option!'); } // -> Get available configurations, and if not set, ask the user // which configuration to use $configs = get_object_vars($deployerfile->configurations); if ($configuration == null && count(get_object_vars($deployerfile->configurations)) > 1) { // If no configuration has been specified via command line, // ask the user which one to use $confignames = array(); foreach ($configs as $name => $values) { $confignames[] = $name; } // -> Get storage service to use $question = new ChoiceQuestion('Which deployment configuration would you like to use?', $confignames); $question->setValidator(function ($answer) use($configs, $confignames) { if (!isset($configs[$confignames[$answer]])) { throw new \RuntimeException('Please select a correct value!'); } return $confignames[$answer]; }); $configuration = $helper->ask($input, $output, $question); } elseif ($configuration != null) { // Try to use the current specified configuration if (!isset($configs[$configuration])) { throw new \Exception('The configuration "' . $configuration . '" was not found in this .deployefile!'); } } else { foreach ($configs as $name => $values) { $configuration = $name; } } // -> Merge the current configuration inheritance chain, if any if (!isset($deployerfile->inheritance)) { $mergedConfig = json_decode(json_encode($configs[$configuration]), true); } else { $chain = $deployerfile->inheritance; $minChainFound = false; $smallerChain = array(); foreach ($chain as $key) { if ($key == $configuration) { $minChainFound = true; $smallerChain[] = $key; } else { if ($minChainFound) { $smallerChain[] = $key; } } } if (count($smallerChain) > 1) { $mergedConfig = array(); foreach (array_reverse($smallerChain) as $configmerge) { $mergedConfig = array_replace_recursive($mergedConfig, json_decode(json_encode($configs[$configmerge]), true)); } } else { $mergedConfig = json_decode(json_encode($configs[$smallerChain[0]]), true); } } // -> Check if we have saved parameter bags, and offer the user // to choose one if so, or use the one provided from the command line $xdg = new \XdgBaseDir\Xdg(); $bagPath = $xdg->getHomeConfigDir() . '/git-deployer'; if ($bag != null) { if (!file_exists($bagPath . '/' . $project->name() . '-' . $bag . '.bag')) { throw new \Exception('This parameter bag has not been found!' . "\n" . 'Please check your naming!'); } else { $answers = unserialize(file_get_contents($bagPath . '/' . $project->name() . '-' . $bag . '.bag')); } } else { $availableBags = array(); if (file_exists($bagPath)) { $dh = opendir($bagPath); while (($file = readdir($dh)) !== false) { $fileInfo = pathinfo($file); if ($fileInfo['extension'] == 'bag') { if (stristr($fileInfo['filename'], $project->name())) { $availableBags[] = str_replace($project->name() . '-', '', $fileInfo['filename']); } } } closedir($dh); } if (count($availableBags) > 0) { array_unshift($availableBags, "Don't use a bag"); $helper = $this->getHelper('question'); $question = new ChoiceQuestion('One or more parameter bags have been found. Which one do you want to use?', $availableBags); $question->setValidator(function ($answer) use($availableBags) { if ($answer == 0) { return false; } if (!isset($availableBags[$answer])) { throw new \RuntimeException('Please select a correct value!'); } return $availableBags[$answer]; }); $parameterBag = $helper->ask($input, $output, $question); if ($parameterBag) { $answers = unserialize(file_get_contents($bagPath . '/' . $project->name() . '-' . $parameterBag . '.bag')); } else { $answers = array(); } } else { $answers = array(); } } // -> Replace placeholders in our config using parameter bags $bagModified = false; if (isset($deployerfile->parameters)) { foreach ($deployerfile->parameters as $key => $questionhelp) { if (!isset($answers[$key])) { $helper = $this->getHelper('question'); $question = new Question($questionhelp . ' '); $questionanswer = $helper->ask($input, $output, $question); if ($questionanswer == null) { $answers[$key] = ''; } else { $answers[$key] = $questionanswer; } $bagModified = true; } } // -> Ask the user to save this parameter bag if ($bagModified) { $helper = $this->getHelper('question'); $question = new ConfirmationQuestion('Do you want to save these answers in a parameter bag for next time? ([y]/n) ', true); if ($helper->ask($input, $output, $question)) { $question = new Question('Please provide a name for the new (or existing, will overwrite) parameter bag: '); $question->setValidator(function ($answer) { if (strlen($answer) < 1) { throw new \RuntimeException('Please provide a name for this parameter bag!'); } else { if (!preg_match('#\\w+#iu', $answer)) { throw new \RuntimeException('The name provided for this parameter bag is invalid!'); } } return $answer; }); $bagname = $helper->ask($input, $output, $question); // -> Save this bag! $xdg = new \XdgBaseDir\Xdg(); $savePath = $xdg->getHomeConfigDir() . '/git-deployer'; $saveFile = $savePath . '/' . $project->name() . '-' . $bagname . '.bag'; if (!file_exists($savePath)) { mkdir($savePath); } file_put_contents($saveFile, serialize($answers)); } } // -> Now traverse the array to replace the values, and as such // get our merged config ready to pass it into the deployer $replacedConfig = $this->replacePlaceholders($mergedConfig, $answers); } // -> Execute the requested deployer with our merged configuration array $deployer = \GitDeployer\Deployers\BaseDeployer::createServiceInstance(ucwords($deployerfile->type), $input, $output, $this->getHelperSet()); list($statusok, $trace) = $deployer->deploy($project, $tmpDir, $replacedConfig); // -> Check if the deployment went well, return any errors, // or update the deployment status for the project otherwise if ($statusok) { $status->deployedWhen(date('c')); $status->deployedType($type); $status->deployedString($version); $storage->setDeploymentStatus($project, $status); } else { $output->writeln($trace); throw new \Exception('Deployment did not completely finish! See trace above.'); } // -> Finish up! $this->showMessage('FINISH', '<info>Done!</info>', $output); break; } } if (!$hasBeenFound) { throw new \Exception('Project "' . $repository . '" could not be found! Please check your spelling!'); } }