|
5 | 5 | use Platformsh\Cli\Command\CommandBase; |
6 | 6 | use Platformsh\Cli\Service\Table; |
7 | 7 | use Platformsh\Client\Exception\EnvironmentStateException; |
| 8 | +use Symfony\Component\Console\Input\ArgvInput; |
8 | 9 | use Symfony\Component\Console\Input\InputInterface; |
9 | 10 | use Symfony\Component\Console\Output\OutputInterface; |
10 | 11 |
|
@@ -53,66 +54,80 @@ protected function execute(InputInterface $input, OutputInterface $output) |
53 | 54 | throw $e; |
54 | 55 | } |
55 | 56 |
|
56 | | - $autoscalingSettings = $this->api()->getAutoscalingSettings($environment)->getData(); |
| 57 | + $autoscalingSettings = $this->api()->getAutoscalingSettings($environment); |
| 58 | + if (!$autoscalingSettings) { |
| 59 | + $this->stdErr->writeln(\sprintf('Autoscaling support is not currently available on the environment: %s', $this->api()->getEnvironmentLabel($environment, 'error'))); |
| 60 | + return 1; |
| 61 | + } |
| 62 | + $autoscalingSettings = $autoscalingSettings->getData(); |
57 | 63 |
|
58 | 64 | $services = array_merge($deployment->webapps, $deployment->workers); |
59 | 65 | if (empty($services)) { |
60 | 66 | $this->stdErr->writeln('No apps or workers found.'); |
61 | 67 | return 1; |
62 | 68 | } |
63 | 69 |
|
64 | | - /** @var Table $table */ |
65 | | - $table = $this->getService('table'); |
| 70 | + if (!empty($autoscalingSettings['services'])) { |
| 71 | + /** @var Table $table */ |
| 72 | + $table = $this->getService('table'); |
66 | 73 |
|
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 | | - } |
| 74 | + if (!$table->formatIsMachineReadable()) { |
| 75 | + $this->stdErr->writeln(sprintf('Autoscaling configuration for the project %s, environment %s:', $this->api()->getProjectLabel($this->getSelectedProject()), $this->api()->getEnvironmentLabel($environment))); |
| 76 | + } |
70 | 77 |
|
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; |
| 78 | + /** @var \Platformsh\Cli\Service\PropertyFormatter $formatter */ |
| 79 | + $formatter = $this->getService('property_formatter'); |
| 80 | + |
| 81 | + $empty = $table->formatIsMachineReadable() ? '' : '<comment>not set</comment>'; |
| 82 | + |
| 83 | + $rows = []; |
| 84 | + foreach ($autoscalingSettings['services'] as $service => $settings) { |
| 85 | + $row = [ |
| 86 | + 'service' => $service, |
| 87 | + 'metric' => $empty, |
| 88 | + 'direction' => $empty, |
| 89 | + 'threshold' => $empty, |
| 90 | + 'duration' => $empty, |
| 91 | + 'enabled' => $empty, |
| 92 | + 'cooldown' => $empty, |
| 93 | + 'min_instances' => $empty, |
| 94 | + 'max_instances' => $empty, |
| 95 | + 'instance_count' => $empty, |
| 96 | + ]; |
| 97 | + |
| 98 | + foreach ($settings['triggers'] as $metric => $conditions) { |
| 99 | + $row['metric'] = $metric; |
| 100 | + foreach ($conditions as $direction => $condition) { |
| 101 | + if ($direction == "enabled") { |
| 102 | + $row['enabled'] = $formatter->format($condition, 'enabled'); |
| 103 | + continue; |
| 104 | + } |
| 105 | + $row['direction'] = $direction; |
| 106 | + $row['threshold'] = sprintf('%.1f%%', $condition['threshold']); |
| 107 | + $row['duration'] = $condition['duration']; |
| 108 | + |
| 109 | + $row['cooldown'] = $settings['scale_cooldown'][$direction]; |
| 110 | + $row['min_instances'] = $settings['instances']['min']; |
| 111 | + $row['max_instances'] = $settings['instances']['max']; |
| 112 | + |
| 113 | + $properties = $services[$service]->getProperties(); |
| 114 | + $row['instance_count'] = isset($properties['instance_count']) ? $formatter->format($properties['instance_count'], 'instance_count') : '1'; |
| 115 | + |
| 116 | + |
| 117 | + $rows[] = $row; |
97 | 118 | } |
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 | 119 | } |
112 | 120 | } |
113 | | - } |
114 | 121 |
|
115 | | - $table->render($rows, $this->tableHeader, $this->defaultColumns); |
| 122 | + $table->render($rows, $this->tableHeader, $this->defaultColumns); |
| 123 | + } else { |
| 124 | + $this->stdErr->writeln(sprintf('No autoscaling configuration found for the project %s, environment %s.', $this->api()->getProjectLabel($this->getSelectedProject()), $this->api()->getEnvironmentLabel($environment))); |
| 125 | + $isOriginalCommand = $input instanceof ArgvInput; |
| 126 | + if ($isOriginalCommand) { |
| 127 | + $this->stdErr->writeln(''); |
| 128 | + $this->stdErr->writeln(sprintf('You can configure autoscaling by running: <info>%s autoscaling:set</info>', $this->config()->get('application.executable'))); |
| 129 | + } |
| 130 | + } |
116 | 131 |
|
117 | 132 | return 0; |
118 | 133 | } |
|
0 commit comments