Install Command fix

- Add missing constructor parameter
- Add .env.local generation
- Add interactive commands
This commit is contained in:
Clemente Raposo 2021-03-26 01:25:41 +00:00 committed by Dillon-Brown
parent d9eacd5f78
commit 432abf8bf7
2 changed files with 166 additions and 63 deletions

View file

@ -5,11 +5,11 @@ namespace App\Install\Command;
use App\Install\LegacyHandler\InstallHandler; use App\Install\LegacyHandler\InstallHandler;
use Exception; use Exception;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\Question;
/** /**
* Class LegacyInstallCommand * Class LegacyInstallCommand
@ -25,7 +25,12 @@ class LegacyInstallCommand extends Command
/** /**
* @var InstallHandler * @var InstallHandler
*/ */
private $installHandler; protected $installHandler;
/**
* @var array
*/
protected $inputs = [];
/** /**
* LegacyInstallCommand constructor. * LegacyInstallCommand constructor.
@ -33,59 +38,114 @@ class LegacyInstallCommand extends Command
*/ */
public function __construct(InstallHandler $installHandler) public function __construct(InstallHandler $installHandler)
{ {
$this->inputs['db_username'] = [
'question' => new Question('Please enter the db username: '),
'argument' => new InputOption(
'db_username',
'dbu',
InputOption::VALUE_REQUIRED,
'database username'
)
];
$dbPasswordQuestion = new Question('Please enter the db password: ');
$dbPasswordQuestion->setHidden(true);
$dbPasswordQuestion->setHiddenFallback(false);
$this->inputs['db_password'] = [
'question' => $dbPasswordQuestion,
'argument' => new InputOption(
'db_password',
'dbp',
InputOption::VALUE_REQUIRED,
'database password'
),
];
$this->inputs['db_host'] = [
'question' => new Question('Please enter the db host: '),
'argument' => new InputOption(
'db_host',
'dbh',
InputOption::VALUE_REQUIRED,
'database host'
)
];
$this->inputs['db_name'] = [
'question' => new Question('Please enter the db name: '),
'argument' => new InputOption(
'db_name',
'dbn',
InputOption::VALUE_REQUIRED,
'database name'
),
];
$this->inputs['site_username'] = [
'question' => new Question('Please enter the admin username: '),
'argument' => new InputOption(
'site_username',
'u',
InputOption::VALUE_REQUIRED,
'site username'
),
];
$adminPasswordQuestion = new Question('Please enter the admin password: ');
$adminPasswordQuestion->setHidden(true);
$adminPasswordQuestion->setHiddenFallback(false);
$this->inputs['site_password'] = [
'question' => $adminPasswordQuestion,
'argument' => new InputOption(
'site_password',
'p',
InputOption::VALUE_REQUIRED,
'site password'
),
];
$this->inputs['site_host'] = [
'question' => new Question('Please enter the suite 8 address (e.g. https://<your_host/): '),
'argument' => new InputOption(
'site_host',
'a',
InputOption::VALUE_REQUIRED,
'site host'
),
];
$this->inputs['demoData'] = [
'question' => new ChoiceQuestion(
'Install demo data?: ',
['yes', 'no'],
'no'
),
'argument' => new InputOption(
'demoData',
'd',
InputOption::VALUE_OPTIONAL,
'Install "demo data" during install process'
),
];
parent::__construct(); parent::__construct();
$this->installHandler = $installHandler; $this->installHandler = $installHandler;
} }
protected function configure(): void protected function configure(): void
{ {
$inputs = [];
foreach ($this->inputs as $key => $item) {
$inputs[$key] = $item['argument'];
}
$this $this
->setDescription('Install the legacy application') ->setDescription('Install the application')
->setHelp('This command will install the legacy application') ->setHelp('This command will install the suite 8 and legacy application')
->setDefinition( ->setDefinition(
new InputDefinition([ $inputs
new InputArgument(
'db_username',
InputOption::VALUE_REQUIRED,
'database username'
),
new InputArgument(
'db_password',
InputOption::VALUE_REQUIRED,
'database password'
),
new InputArgument(
'db_host',
InputOption::VALUE_REQUIRED,
'database host'
),
new InputArgument(
'db_name',
InputOption::VALUE_REQUIRED,
'database name'
),
new InputArgument(
'site_username',
InputOption::VALUE_REQUIRED,
'site username'
),
new InputArgument(
'site_password',
InputOption::VALUE_REQUIRED,
'site password'
),
new InputArgument(
'site_host',
InputOption::VALUE_REQUIRED,
'site host'
),
new InputOption(
'demo',
'd',
InputOption::VALUE_REQUIRED,
'Install "demo data" during install process'
),
])
); );
} }
@ -104,20 +164,26 @@ class LegacyInstallCommand extends Command
'', '',
]); ]);
$demoData = $input->getOption('demo'); if ($this->installHandler->isInstalled()) {
$demoDataOutput = ($demoData !== false); $output->writeln('Already installed. Stopping');
$inputArray = [ return 0;
'db_username' => $input->getArgument('db_username'), }
'db_password' => $input->getArgument('db_password'),
'db_host' => $input->getArgument('db_host'),
'db_name' => $input->getArgument('db_name'),
'site_username' => $input->getArgument('site_username'),
'site_password' => $input->getArgument('site_password'),
'site_host' => $input->getArgument('site_host'),
'demo' => $demoDataOutput
];
$inputArray = [];
$helper = $this->getHelper('question');
foreach ($this->inputs as $key => $option) {
$value = $input->getOption($key);
if (empty($value)) {
$value = $helper->ask($input, $output, $option['question']);
}
$inputArray[$key] = $value;
}
$this->installHandler->createEnv($inputArray);
$this->installHandler->createConfig($inputArray); $this->installHandler->createConfig($inputArray);
$output->writeln('Step 1: Config Creation Complete'); $output->writeln('Step 1: Config Creation Complete');

View file

@ -54,7 +54,14 @@ class InstallHandler extends LegacyHandler
SessionInterface $session, SessionInterface $session,
LoggerInterface $logger LoggerInterface $logger
) { ) {
parent::__construct($projectDir, $legacyDir, $legacySessionName, $defaultSessionName, $legacyScopeState, $session); parent::__construct(
$projectDir,
$legacyDir,
$legacySessionName,
$defaultSessionName,
$legacyScopeState,
$session
);
$this->legacyDir = $legacyDir; $this->legacyDir = $legacyDir;
$this->logger = $logger; $this->logger = $logger;
} }
@ -115,6 +122,7 @@ class InstallHandler extends LegacyHandler
*/ */
public function createConfig(array $inputArray): void public function createConfig(array $inputArray): void
{ {
$siteURL = $inputArray['site_host'] . '/legacy';
$configArray = [ $configArray = [
'dbUSRData' => 'same', 'dbUSRData' => 'same',
'default_currency_iso4217' => 'USD', 'default_currency_iso4217' => 'USD',
@ -135,12 +143,12 @@ class InstallHandler extends LegacyHandler
'setup_db_database_name' => $inputArray['db_name'], 'setup_db_database_name' => $inputArray['db_name'],
'setup_db_drop_tables' => 0, 'setup_db_drop_tables' => 0,
'setup_db_host_name' => $inputArray['db_host'], 'setup_db_host_name' => $inputArray['db_host'],
'setup_db_pop_demo_data' => $inputArray['demo'], 'demoData' => $inputArray['demoData'],
'setup_db_type' => 'mysql', 'setup_db_type' => 'mysql',
'setup_db_username_is_privileged' => true, 'setup_db_username_is_privileged' => true,
'setup_site_admin_password' => $inputArray['site_password'], 'setup_site_admin_password' => $inputArray['site_password'],
'setup_site_admin_user_name' => $inputArray['site_username'], 'setup_site_admin_user_name' => $inputArray['site_username'],
'setup_site_url' => $inputArray['site_host'], 'setup_site_url' => $siteURL,
'setup_system_name' => 'SuiteCRM', 'setup_system_name' => 'SuiteCRM',
]; ];
@ -155,4 +163,33 @@ class InstallHandler extends LegacyHandler
echo 'An error occurred while creating your silent install config at ' . $exception->getPath(); echo 'An error occurred while creating your silent install config at ' . $exception->getPath();
} }
} }
/**
* Create local env file
* @param array $inputArray
*/
public function createEnv(array $inputArray): void
{
$password = $inputArray['db_password'] ?? '';
$username = $inputArray['db_username'] ?? '';
$dbName = $inputArray['db_name'] ?? '';
$host = $inputArray['db_host'] ?? '';
$port = $inputArray['db_port'] ?? '3306';
$dbUrl = "DATABASE_URL=\"mysql://$username:$password@$host:$port/$dbName\"";
$filesystem = new Filesystem();
$filesystem->dumpFile('.env.local', $dbUrl);
}
/**
* Check if is installed
* @return bool is installed
*/
public function isInstalled(): bool
{
$filesystem = new Filesystem();
return $filesystem->exists('.env.local');
}
} }