Skip to content
This repository was archived by the owner on Apr 27, 2026. It is now read-only.

Commit abb87d2

Browse files
Add autoscaling:set command to configure CPU-based autoscaling
1 parent 176dc4c commit abb87d2

6 files changed

Lines changed: 1007 additions & 70 deletions

File tree

src/Application.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ protected function getCommands()
126126
$commands[] = new Command\Auth\BrowserLoginCommand();
127127
$commands[] = new Command\Auth\VerifyPhoneNumberCommand();
128128
$commands[] = new Command\Autoscaling\AutoscalingSettingsGetCommand();
129+
$commands[] = new Command\Autoscaling\AutoscalingSettingsSetCommand();
129130
$commands[] = new Command\BlueGreen\BlueGreenConcludeCommand();
130131
$commands[] = new Command\BlueGreen\BlueGreenDeployCommand();
131132
$commands[] = new Command\BlueGreen\BlueGreenEnableCommand();

src/Command/Autoscaling/AutoscalingSettingsGetCommand.php

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Platformsh\Cli\Command\CommandBase;
66
use Platformsh\Cli\Service\Table;
77
use Platformsh\Client\Exception\EnvironmentStateException;
8+
use Symfony\Component\Console\Input\ArgvInput;
89
use Symfony\Component\Console\Input\InputInterface;
910
use Symfony\Component\Console\Output\OutputInterface;
1011

@@ -53,66 +54,80 @@ protected function execute(InputInterface $input, OutputInterface $output)
5354
throw $e;
5455
}
5556

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();
5763

5864
$services = array_merge($deployment->webapps, $deployment->workers);
5965
if (empty($services)) {
6066
$this->stdErr->writeln('No apps or workers found.');
6167
return 1;
6268
}
6369

64-
/** @var Table $table */
65-
$table = $this->getService('table');
70+
if (!empty($autoscalingSettings['services'])) {
71+
/** @var Table $table */
72+
$table = $this->getService('table');
6673

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+
}
7077

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;
97118
}
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;
111119
}
112120
}
113-
}
114121

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+
}
116131

117132
return 0;
118133
}

0 commit comments

Comments
 (0)