Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import() can now load URLs and recipes from third party and private composer repositories #3477

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions src/Command/ImportCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php declare(strict_types=1);

/* (c) Anton Medvedev <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Deployer\Command;

use Deployer\Deployer;
use Deployer\Importer\Importer;
use Deployer\Utility\Httpie;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\Process;

class ImportCommand extends Command
{
use CommandCommon;

private InputInterface $input;
private OutputInterface $output;
private string $cwd;

protected function configure(): void
{
$this
->setName('import')
->setDescription('Import a remote recipe')
->addOption('mode', 'm', InputOption::VALUE_REQUIRED, 'Can be either url or composer')
->addArgument('path', InputArgument::REQUIRED, 'Recipe file (can be URL or a path in the repo when using composer mode)')
->addArgument('package', InputArgument::OPTIONAL, 'Composer package name (can have an appended version string)')
->addArgument('repository', InputArgument::OPTIONAL, 'Composer package repository url')
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->input = $input;
$this->output = $output;

if(!defined('DEPLOYER_DEPLOY_FILE')) {
throw new \Exception("No deployfile present.");
}

$this->cwd = dirname(DEPLOYER_DEPLOY_FILE);


$io = new SymfonyStyle($this->input, $output);
$path = $this->input->getArgument('path');
$package = $this->input->getArgument('package');
$repository = $this->input->getArgument('repository');

if(!Deployer::isWorker()) { // is worker is not returning the correct value, as this command is runnning before the symfony console is initialized and running
// maybe the question should be asked only once (save the result), or not even be asked at all?
if (!$io->askQuestion(new ConfirmationQuestion("<question>Do you really want to trust this remote recipe: $path?</question>", true))) {
return 1;
}
}

if(Importer::isUrl($path)) {
$this->importUrl($path);
}

elseif(Importer::isRepo($path)) {
$this->importComposer($path, $package, $repository);
}
else {
throw new \Exception("Unrecognized path $path, make sure its a valid URL or a valid 'composer/package'");
}

return 0;
}

protected function importUrl(string $path): void
{
if ($data = Httpie::get($path)->send()) {
$tmpfname = tempnam("/tmp", "deployer_remote_recipe").".php";
$tmpfhandle = fopen($tmpfname, "w");
fwrite($tmpfhandle, $data);
fclose($tmpfhandle);
Deployer::get()->importer->import($tmpfname);
} else {
throw new \Exception("Could not download $path for import.");
}
}

protected function importComposer(string $path, string $package, string $repository = null): void
{

if(!$this->composerJsonExists()) {
try {
$process = Process::fromShellCommandline("composer init --no-interaction --name deployer/project", $this->cwd);
$output = trim($process->mustRun()->getOutput());
} catch (RuntimeException $e) {
throw new \Exception($e->getMessage());
}
echo "Initialized composer.json for you\n";
}

if($repository) {
$repoName = "deployer/".parse_url($repository, PHP_URL_HOST);

try {
$process = Process::fromShellCommandline("composer config repositories.$repoName composer $repository", $this->cwd);
$output = trim($process->mustRun()->getOutput());
} catch (RuntimeException $e) {
throw new \Exception($e->getMessage());
}
if ($this->output->isVerbose()) {
echo "Added repository to composer.json\n";
}
}

try {
$process = Process::fromShellCommandline("composer require --dev --no-plugins \"$package\"", $this->cwd);
$output = trim($process->mustRun()->getOutput());
} catch (RuntimeException $e) {
throw new \Exception($e->getMessage());
}
if ($this->output->isVerbose()) {
echo "Added require-dev package to composer.json\n";
}

list($packageWithoutVersion, $version) = explode(":", $package);
$target = $this->cwd . "/vendor/".$packageWithoutVersion."/".$path;
if(file_exists($target)) {
Importer::import($target);
} else {
throw new \Exception("Could not find imported composer file in ".$target);
}
}

protected function getComposerJsonFile(): string
{
return $this->cwd . "/composer.json";
}

private function composerJsonExists(): bool
{
return file_exists($this->getComposerJsonFile());
}

}
23 changes: 23 additions & 0 deletions src/Importer/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@

namespace Deployer\Importer;

use Deployer\Command\ImportCommand;
use Deployer\Deployer;
use Deployer\Exception\ConfigurationException;
use Deployer\Exception\Exception;
use JsonSchema\Constraints\Constraint;
use JsonSchema\Constraints\Factory;
use JsonSchema\SchemaStorage;
use JsonSchema\Validator;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Yaml\Yaml;
use function array_filter;
use function array_keys;
Expand Down Expand Up @@ -102,6 +105,26 @@ public static function import($paths)
}
}

public static function isUrl(string $path): bool {
return (bool) preg_match('/^https:\/\//i', $path);
}

public static function isRepo(string $path): bool {
list($repo, $version) = explode(":", $path.":");
return (bool) preg_match('@^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9](([_.]?|-{0,2})[a-z0-9]+)*$@', $repo);
}

public static function importUrl(string $url)
{

(new ImportCommand())->run(new ArrayInput(['--mode'=> 'url', 'path' => $url]), new ConsoleOutput());
}

public static function importComposer(string $file, string $package, string $repo = null)
{
(new ImportCommand())->run(new ArrayInput(['--mode'=> 'composer', 'path' => $file, 'package' => $package, 'repository' => $repo]), new ConsoleOutput());
}

protected static function hosts(array $hosts)
{
foreach ($hosts as $alias => $config) {
Expand Down
11 changes: 9 additions & 2 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,16 @@ function selectedHosts(): array
*
* @throws Exception
*/
function import(string $file): void
function import(string $file, string $package = null, string $repo = null): void
{
Importer::import($file);
if($file && $package) {
Importer::importComposer($file, $package, $repo);
}
elseif(Importer::isUrl($file)) {
Importer::importUrl($file);
} else {
Importer::import($file);
}
}

/**
Expand Down