|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Platformsh\Cli\Command\Autoscaling; |
| 4 | + |
| 5 | +use Platformsh\Cli\Command\CommandBase; |
| 6 | +use Platformsh\Cli\Service\Table; |
| 7 | +use Platformsh\Client\Exception\EnvironmentStateException; |
| 8 | +use Symfony\Component\Console\Input\InputInterface; |
| 9 | +use Symfony\Component\Console\Output\OutputInterface; |
| 10 | + |
| 11 | +class AutoscalingSettingsGetCommand extends CommandBase |
| 12 | +{ |
| 13 | + protected $tableHeader = [ |
| 14 | + 'service' => 'App or service', |
| 15 | + 'metric' => 'Metric', |
| 16 | + 'direction' => 'Direction', |
| 17 | + 'threshold' => 'Threshold (%)', |
| 18 | + 'duration' => 'Duration (s)', |
| 19 | + 'cooldown' => 'Cooldown (s)', |
| 20 | + 'enabled' => 'Enabled', |
| 21 | + 'instance_count' => 'Instances', |
| 22 | + 'min_instances' => 'Minimum instances', |
| 23 | + 'max_instances' => 'Maximum instances', |
| 24 | + ]; |
| 25 | + protected $defaultColumns = ['service', 'metric', 'direction', 'threshold', 'duration', 'enabled', 'instance_count']; |
| 26 | + |
| 27 | + protected function configure() |
| 28 | + { |
| 29 | + $this->setName('autoscaling:get') |
| 30 | + ->setAliases(['autoscaling']) |
| 31 | + ->setDescription('View the autoscaling configuration of apps and workers on an environment'); |
| 32 | + $this->addProjectOption()->addEnvironmentOption(); |
| 33 | + Table::configureInput($this->getDefinition(), $this->tableHeader, $this->defaultColumns); |
| 34 | + } |
| 35 | + |
| 36 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 37 | + { |
| 38 | + $this->validateInput($input); |
| 39 | + if (!$this->api()->supportsAutoscaling($this->getSelectedProject())) { |
| 40 | + $this->stdErr->writeln(sprintf('The autoscaling API is not enabled for the project %s.', $this->api()->getProjectLabel($this->getSelectedProject(), 'comment'))); |
| 41 | + return 1; |
| 42 | + } |
| 43 | + |
| 44 | + $environment = $this->getSelectedEnvironment(); |
| 45 | + |
| 46 | + try { |
| 47 | + $deployment = $this->api()->getCurrentDeployment($environment); |
| 48 | + } catch (EnvironmentStateException $e) { |
| 49 | + if ($environment->status === 'inactive') { |
| 50 | + $this->stdErr->writeln(sprintf('The environment %s is not active so autoscaling configuration cannot be read.', $this->api()->getEnvironmentLabel($environment, 'comment'))); |
| 51 | + return 1; |
| 52 | + } |
| 53 | + throw $e; |
| 54 | + } |
| 55 | + |
| 56 | + $autoscalingSettings = $this->api()->getAutoscalingSettings($environment)->getData(); |
| 57 | + |
| 58 | + $services = array_merge($deployment->webapps, $deployment->workers); |
| 59 | + if (empty($services)) { |
| 60 | + $this->stdErr->writeln('No apps or workers found.'); |
| 61 | + return 1; |
| 62 | + } |
| 63 | + |
| 64 | + /** @var Table $table */ |
| 65 | + $table = $this->getService('table'); |
| 66 | + |
| 67 | + if (!$table->formatIsMachineReadable()) { |
| 68 | + $this->stdErr->writeln(sprintf('Autoscaling configuration for the project %s, environment %s:', $this->api()->getProjectLabel($this->getSelectedProject()), $this->api()->getEnvironmentLabel($environment))); |
| 69 | + } |
| 70 | + |
| 71 | + /** @var \Platformsh\Cli\Service\PropertyFormatter $formatter */ |
| 72 | + $formatter = $this->getService('property_formatter'); |
| 73 | + |
| 74 | + $empty = $table->formatIsMachineReadable() ? '' : '<comment>not set</comment>'; |
| 75 | + |
| 76 | + $rows = []; |
| 77 | + foreach ($autoscalingSettings['services'] as $service => $settings) { |
| 78 | + $row = [ |
| 79 | + 'service' => $service, |
| 80 | + 'metric' => $empty, |
| 81 | + 'direction' => $empty, |
| 82 | + 'threshold' => $empty, |
| 83 | + 'duration' => $empty, |
| 84 | + 'enabled' => $empty, |
| 85 | + 'cooldown' => $empty, |
| 86 | + 'min_instances' => $empty, |
| 87 | + 'max_instances' => $empty, |
| 88 | + 'instance_count' => $empty, |
| 89 | + ]; |
| 90 | + |
| 91 | + foreach ($settings['triggers'] as $metric => $conditions) { |
| 92 | + $row['metric'] = $metric; |
| 93 | + foreach ($conditions as $direction => $condition) { |
| 94 | + if ($direction == "enabled") { |
| 95 | + $row['enabled'] = $formatter->format($condition, 'enabled'); |
| 96 | + continue; |
| 97 | + } |
| 98 | + $row['direction'] = $direction; |
| 99 | + $row['threshold'] = sprintf('%.1f%%', $condition['threshold']); |
| 100 | + $row['duration'] = $condition['duration']; |
| 101 | + |
| 102 | + $row['cooldown'] = $settings['scale_cooldown'][$direction]; |
| 103 | + $row['min_instances'] = $settings['instances']['min']; |
| 104 | + $row['max_instances'] = $settings['instances']['max']; |
| 105 | + |
| 106 | + $properties = $services[$service]->getProperties(); |
| 107 | + $row['instance_count'] = isset($properties['instance_count']) ? $formatter->format($properties['instance_count'], 'instance_count') : '1'; |
| 108 | + |
| 109 | + |
| 110 | + $rows[] = $row; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + $table->render($rows, $this->tableHeader, $this->defaultColumns); |
| 116 | + |
| 117 | + return 0; |
| 118 | + } |
| 119 | +} |
0 commit comments