Skip to content

Commit

Permalink
add env command and option to disable env replacement on install
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Feb 17, 2022
1 parent f6da8c9 commit 76d94d6
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 9 deletions.
74 changes: 74 additions & 0 deletions src/Console/Marketplace/EnvCommand.php
@@ -0,0 +1,74 @@
<?php
/*
* Fusio
* A web-application to create dynamically RESTful APIs
*
* Copyright (C) 2015-2021 Christoph Kappestein <christoph.kappestein@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Fusio\Impl\Console\Marketplace;

use Fusio\Impl\Authorization\UserContext;
use Fusio\Impl\Service;
use PSX\Http\Exception\BadRequestException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* EnvCommand
*
* @author Christoph Kappestein <christoph.kappestein@gmail.com>
* @license http://www.gnu.org/licenses/agpl-3.0
* @link https://www.fusio-project.org
*/
class EnvCommand extends Command
{
private Service\Marketplace\Installer $installer;

public function __construct(Service\Marketplace\Installer $installer)
{
parent::__construct();

$this->installer = $installer;
}

protected function configure()
{
$this
->setName('marketplace:env')
->setDescription('Replaces env variables of an existing app')
->addArgument('name', InputArgument::REQUIRED, 'The name of the app');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$app = $this->installer->env($input->getArgument('name'));

$output->writeln('');
$output->writeln('Replaced env ' . $app->getName());
$output->writeln('');
} catch (BadRequestException $e) {
$output->writeln('');
$output->writeln($e->getMessage());
$output->writeln('');
}

return 0;
}
}
10 changes: 8 additions & 2 deletions src/Console/Marketplace/InstallCommand.php
Expand Up @@ -57,7 +57,8 @@ protected function configure()
->setName('marketplace:install')
->setDescription('Installs an app from the marketplace')
->addArgument('name', InputArgument::REQUIRED, 'The name of the app')
->addOption('disable_ssl_verify', 'd', InputOption::VALUE_NONE, 'Disable SSL verification');
->addOption('disable_ssl_verify', 'd', InputOption::VALUE_NONE, 'Disable SSL verification')
->addOption('disable_env', 'x', InputOption::VALUE_NONE, 'Disable env replacement');
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand All @@ -66,11 +67,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->remoteRepository->setSslVerify(false);
}

$replaceEnv = true;
if ($input->getOption('disable_env')) {
$replaceEnv = false;
}

$install = new Marketplace_Install();
$install->setName($input->getArgument('name'));

try {
$app = $this->installer->install($install, UserContext::newAnonymousContext());
$app = $this->installer->install($install, UserContext::newAnonymousContext(), $replaceEnv);

$output->writeln('');
$output->writeln('Installed app ' . $app->getName());
Expand Down
5 changes: 3 additions & 2 deletions src/Dependency/Container.php
Expand Up @@ -288,10 +288,11 @@ protected function appendConsoleCommands(Application $application): void
Setup::appendCommands($application, $transport, $basePath, $envReplacer, $this->get('schema_parser_import_resolver'));

// internal commands
$application->add(new Console\Marketplace\ListCommand($this->get('marketplace_repository_remote')));
$application->add(new Console\Marketplace\EnvCommand($this->get('marketplace_installer')));
$application->add(new Console\Marketplace\InstallCommand($this->get('marketplace_installer'), $this->get('marketplace_repository_remote')));
$application->add(new Console\Marketplace\UpdateCommand($this->get('marketplace_installer'), $this->get('marketplace_repository_remote')));
$application->add(new Console\Marketplace\ListCommand($this->get('marketplace_repository_remote')));
$application->add(new Console\Marketplace\RemoveCommand($this->get('marketplace_installer')));
$application->add(new Console\Marketplace\UpdateCommand($this->get('marketplace_installer'), $this->get('marketplace_repository_remote')));

$application->add(new Console\Migration\ExecuteCommand($this->get('connection'), $this->get('connector')));
$application->add(new Console\Migration\GenerateCommand($this->get('connection'), $this->get('connector')));
Expand Down
27 changes: 22 additions & 5 deletions src/Service/Marketplace/Installer.php
Expand Up @@ -53,7 +53,7 @@ public function __construct(Repository\Local $localRepository, Repository\Remote
$this->filesystem = new Filesystem();
}

public function install(Marketplace_Install $install, UserContext $context): App
public function install(Marketplace_Install $install, UserContext $context, bool $replaceEnv = true): App
{
$remoteApp = $this->remoteRepository->fetchByName($install->getName());
$localApp = $this->localRepository->fetchByName($install->getName());
Expand All @@ -62,7 +62,7 @@ public function install(Marketplace_Install $install, UserContext $context): App
throw new StatusCode\BadRequestException('App already installed');
}

$this->deploy($remoteApp);
$this->deploy($remoteApp, $replaceEnv);

return $remoteApp;
}
Expand Down Expand Up @@ -94,7 +94,6 @@ public function update(string $name, UserContext $context): App
public function remove(string $name, UserContext $context): App
{
$localApp = $this->localRepository->fetchByName($name);

if (!$localApp instanceof App) {
throw new StatusCode\BadRequestException('App is not installed');
}
Expand All @@ -104,15 +103,33 @@ public function remove(string $name, UserContext $context): App
return $localApp;
}

private function deploy(App $remoteApp)
public function env(string $name): App
{
$localApp = $this->localRepository->fetchByName($name);
if (!$localApp instanceof App) {
throw new StatusCode\BadRequestException('App is not installed');
}

$appsDir = $this->config->get('fusio_apps_dir') ?: $this->config->get('psx_path_public');
$appsDir.= '/' . $localApp->getName();

$this->replaceVariables($appsDir);

return $localApp;
}

private function deploy(App $remoteApp, bool $replaceEnv = true)
{
$zipFile = $this->downloadZip($remoteApp);

$appDir = $this->config->get('psx_path_cache') . '/app-' . $remoteApp->getName();
$appDir = $this->unzipFile($zipFile, $appDir);

$this->writeMetaFile($appDir, $remoteApp);
$this->replaceVariables($appDir);

if ($replaceEnv) {
$this->replaceVariables($appDir);
}

$this->moveToPublic($appDir, $remoteApp);
}
Expand Down

0 comments on commit 76d94d6

Please sign in to comment.