|
10 | 10 | use GuzzleHttp\Event\ErrorEvent; |
11 | 11 | use GuzzleHttp\Exception\BadResponseException; |
12 | 12 | use GuzzleHttp\Message\ResponseInterface; |
| 13 | +use Platformsh\Cli\Console\ProgressMessage; |
13 | 14 | use Platformsh\Cli\CredentialHelper\KeyringUnavailableException; |
14 | 15 | use Platformsh\Cli\CredentialHelper\Manager; |
15 | 16 | use Platformsh\Cli\CredentialHelper\SessionStorage as CredentialHelperStorage; |
|
25 | 26 | use Platformsh\Client\Exception\EnvironmentStateException; |
26 | 27 | use Platformsh\Client\Model\BasicProjectInfo; |
27 | 28 | use Platformsh\Client\Model\Deployment\EnvironmentDeployment; |
| 29 | +use Platformsh\Client\Model\Deployment\Service; |
| 30 | +use Platformsh\Client\Model\Deployment\WebApp; |
| 31 | +use Platformsh\Client\Model\Deployment\Worker; |
28 | 32 | use Platformsh\Client\Model\Environment; |
29 | 33 | use Platformsh\Client\Model\EnvironmentType; |
30 | 34 | use Platformsh\Client\Model\Organization\Member; |
@@ -74,6 +78,13 @@ class Api |
74 | 78 | /** @var FileLock */ |
75 | 79 | private $fileLock; |
76 | 80 |
|
| 81 | + /** |
| 82 | + * Cached next deployment. |
| 83 | + * |
| 84 | + * @var array |
| 85 | + */ |
| 86 | + private static array $cachedNextDeployment = []; |
| 87 | + |
77 | 88 | /** |
78 | 89 | * The library's API client object. |
79 | 90 | * |
@@ -1811,4 +1822,92 @@ public function getAutoscalingSettings(Environment $environment) |
1811 | 1822 | } |
1812 | 1823 | return $settings; |
1813 | 1824 | } |
| 1825 | + |
| 1826 | + /** |
| 1827 | + * Loads the next environment deployment and caches it statically. |
| 1828 | + * |
| 1829 | + * The static cache means it can be reused while running a sub-command. |
| 1830 | + * |
| 1831 | + * @param Environment $environment |
| 1832 | + * @param bool $reset |
| 1833 | + * @return EnvironmentDeployment |
| 1834 | + */ |
| 1835 | + public function loadNextDeployment(Environment $environment, $reset = false) |
| 1836 | + { |
| 1837 | + $cacheKey = $environment->project . ':' . $environment->id; |
| 1838 | + if (isset(self::$cachedNextDeployment[$cacheKey]) && !$reset) { |
| 1839 | + return self::$cachedNextDeployment[$cacheKey]; |
| 1840 | + } |
| 1841 | + $progress = new ProgressMessage($this->stdErr); |
| 1842 | + try { |
| 1843 | + $progress->show('Loading deployment information...'); |
| 1844 | + $next = $environment->getNextDeployment(); |
| 1845 | + if (!$next) { |
| 1846 | + throw new EnvironmentStateException('No next deployment found', $environment); |
| 1847 | + } |
| 1848 | + } finally { |
| 1849 | + $progress->done(); |
| 1850 | + } |
| 1851 | + return self::$cachedNextDeployment[$cacheKey] = $next; |
| 1852 | + } |
| 1853 | + |
| 1854 | + /** |
| 1855 | + * Lists services in a deployment. |
| 1856 | + * |
| 1857 | + * @param EnvironmentDeployment $deployment |
| 1858 | + * |
| 1859 | + * @return array<string, WebApp||Worker|Service> |
| 1860 | + * An array of services keyed by the service name. |
| 1861 | + */ |
| 1862 | + public function allServices(EnvironmentDeployment $deployment) |
| 1863 | + { |
| 1864 | + $webapps = $deployment->webapps; |
| 1865 | + $workers = $deployment->workers; |
| 1866 | + $services = $deployment->services; |
| 1867 | + ksort($webapps, SORT_STRING|SORT_FLAG_CASE); |
| 1868 | + ksort($workers, SORT_STRING|SORT_FLAG_CASE); |
| 1869 | + ksort($services, SORT_STRING|SORT_FLAG_CASE); |
| 1870 | + return array_merge($webapps, $workers, $services); |
| 1871 | + } |
| 1872 | + |
| 1873 | + /** |
| 1874 | + * Check if project supports guaranteed resources. |
| 1875 | + * |
| 1876 | + * @param array $projectInfo |
| 1877 | + * |
| 1878 | + * @return bool |
| 1879 | + * True if guaranteed CPU is supported, false otherwise. |
| 1880 | + */ |
| 1881 | + public function supportsGuaranteedCPU(array $projectInfo) |
| 1882 | + { |
| 1883 | + return !empty($projectInfo["settings"]["enable_guaranteed_resources"]) && |
| 1884 | + !empty($projectInfo["capabilities"]["guaranteed_resources"]["enabled"]); |
| 1885 | + } |
| 1886 | + |
| 1887 | + /** |
| 1888 | + * Check if environment has guaranteed CPU. |
| 1889 | + * |
| 1890 | + * @param \Platformsh\Client\Model\Environment $environment |
| 1891 | + * |
| 1892 | + * @return bool |
| 1893 | + */ |
| 1894 | + public function environmentHasGuaranteedCPU(Environment $environment) |
| 1895 | + { |
| 1896 | + $nextDeployment = $this->loadNextDeployment($environment); |
| 1897 | + if ($this->supportsGuaranteedCPU($nextDeployment->project_info)) { |
| 1898 | + $containerProfiles = $nextDeployment->container_profiles; |
| 1899 | + $services = $this->allServices($nextDeployment); |
| 1900 | + foreach ($services as $service) { |
| 1901 | + $properties = $service->getProperties(); |
| 1902 | + if (isset($properties['container_profile']) && isset($containerProfiles[$properties['container_profile']][$properties['resources']['profile_size']])) { |
| 1903 | + $profileInfo = $containerProfiles[$properties['container_profile']][$properties['resources']['profile_size']]; |
| 1904 | + if (isset($profileInfo['cpu_type']) && $profileInfo['cpu_type'] == 'guaranteed') { |
| 1905 | + return true; |
| 1906 | + } |
| 1907 | + } |
| 1908 | + } |
| 1909 | + } |
| 1910 | + |
| 1911 | + return false; |
| 1912 | + } |
1814 | 1913 | } |
0 commit comments