Skip to content
Closed
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
130 changes: 116 additions & 14 deletions src/Command/Project/ProjectCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Platformsh\Cli\Console\Bot;
use Platformsh\ConsoleForm\Field\Field;
use Platformsh\ConsoleForm\Field\OptionsField;
use Platformsh\ConsoleForm\Field\BooleanField;
use Platformsh\ConsoleForm\Field\UrlField;
use Platformsh\ConsoleForm\Form;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -63,6 +65,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$estimate = $this->api()
->getClient()
->getSubscriptionEstimate($options['plan'], $options['storage'], $options['environments'], 1);

$costConfirm = sprintf(
'The estimated monthly cost of this project is: <comment>%s</comment>',
$estimate['total']
Expand All @@ -78,8 +81,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}

// Grab the url of the yaml file.
if (!empty($options['catalog_url'])) {
$options['catalog'] = $options['catalog_url'];
}
else if (!empty($options['catalog_url'])) {
$options['catalog'] = $options['template_url'];
}

$subscription = $this->api()->getClient()
->createSubscription(
$options['catalog'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Push to the back of the arg list.

$options['region'],
$options['plan'],
$options['title'],
Expand Down Expand Up @@ -129,7 +141,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$timedOut = $totalTimeout ? time() - $start > $totalTimeout : false;
}
$this->stdErr->writeln('');

if (!$subscription->isActive()) {
if ($timedOut) {
$this->stdErr->writeln('<error>The project failed to activate on time</error>');
Expand All @@ -146,14 +157,30 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}

$this->stdErr->writeln("The project is now ready!");
if ($options['initialize']) {
// Use the existing initialize command.
$project = $this->selectProject($subscription->project_id);
$environment = $this->api()->getEnvironment('master', $project, null, true);
$environment->initialize($subscription->project_options['initialize']['profile'], $subscription->project_options['initialize']['repository']);
$this->api()->clearEnvironmentsCache($environment->project);
$this->stdErr->writeln("The project has been initialized and is ready!");
}
else {
$this->stdErr->writeln("The project is now ready!");
}

$output->writeln($subscription->project_id);
$this->stdErr->writeln('');

if (!empty($subscription->project_options['initialize'])) {
$this->stdErr->writeln(" Template: <info>{$subscription->project_options[initialize][repository] }</info>");
}
$this->stdErr->writeln(" Region: <info>{$subscription->project_region}</info>");
$this->stdErr->writeln(" Project ID: <info>{$subscription->project_id}</info>");
$this->stdErr->writeln(" Project title: <info>{$subscription->project_title}</info>");
$this->stdErr->writeln(" URL: <info>{$subscription->project_ui}</info>");


return 0;
}

Expand Down Expand Up @@ -216,29 +243,92 @@ protected function getAvailableRegions($runtime = false)
return $regions;
}

/**
* Return the catalog.
*
* The default list is in the config `service.catalog`. This is
* replaced at runtime by an API call.
*
* @param bool $runtime
*
* @return array
*/
protected function getAvailableCatalog($runtime = false)
{
if ($runtime) {
$catalog = [];
foreach ($this->api()->getClient()->getCatalog()->getData() as $item) {
if ($item['info'] && $item['template']) {
$catalog[$item['template']] = $item['info']['name'];
}
}
$catalog['empty'] = 'Empty Project';
} else {
$catalog = (array) $this->config()->get('service.catalog');
}

return $catalog;
}

/**
* Returns a list of ConsoleForm form fields for this command.
*
* @return Field[]
*/
protected function getFields()
{
return [
'title' => new Field('Project title', [
$fields = [];

$fields['title'] = new Field('Project title', [
'optionName' => 'title',
'description' => 'The initial project title',
'questionLine' => '',
'default' => 'Untitled Project',
]),
'region' => new OptionsField('Region', [
]);

$fields['template_option'] = new OptionsField('Template Options', [
'optionName' => 'template_option',
'options' => [
'Provide your own template url.',
'Choose a template from the catalog.',
'No template at this time.',
],
'description' => 'Choose a template, provide a url or choose not to use one.',
'includeAsOption' => false,
]);
$fields['catalog_url'] = new OptionsField('Catalog', [
'optionName' => 'catalog_url',
'conditions' => [
'template_option' => [
'Choose a template from the catalog.'
],
],
'description' => 'The template from which to create your project or your own blank project.',
'options' => $this->getAvailableCatalog(),
'asChoice' => FALSE,
'optionsCallback' => function () {
return $this->getAvailableCatalog(true);
},
]);
$fields['template_url'] = new UrlField('Template URL', [
'optionName' => 'template_url',
'conditions' => [
'template_option' => [
'Provide your own template url.'
],
],
'description' => 'The template url',
'questionLine' => 'What is the URL of the template?',
]);
$fields['region'] = new OptionsField('Region', [
'optionName' => 'region',
'description' => 'The region where the project will be hosted',
'options' => $this->getAvailableRegions(),
'optionsCallback' => function () {
return $this->getAvailableRegions(true);
},
]),
'plan' => new OptionsField('Plan', [
]);
$fields['plan'] = new OptionsField('Plan', [
'optionName' => 'plan',
'description' => 'The subscription plan',
'options' => $this->getAvailablePlans(),
Expand All @@ -247,23 +337,35 @@ protected function getFields()
},
'default' => in_array('development', $this->getAvailablePlans()) ? 'development' : null,
'allowOther' => true,
]),
'environments' => new Field('Environments', [
]);
$fields['environments'] = new Field('Environments', [
'optionName' => 'environments',
'description' => 'The number of environments',
'default' => 3,
'validator' => function ($value) {
return is_numeric($value) && $value > 0 && $value < 50;
},
]),
'storage' => new Field('Storage', [
]);
$fields['storage'] = new Field('Storage', [
'description' => 'The amount of storage per environment, in GiB',
'default' => 5,
'validator' => function ($value) {
return is_numeric($value) && $value > 0 && $value < 1024;
},
]),
];
]);
$fields['initialize'] = new BooleanField('Initialize', [
'optionName' => 'initialized',
'conditions' => [
'template_option' => [
'Provide your own template url.',
'Choose a template from the catalog.',
],
],
'description' => 'Initialize this environment?',
'questionLine' => 'Initialize this environment?',
]);

return $fields;
}

/**
Expand Down